aichaos / rivescript-java

A RiveScript interpreter for Java. RiveScript is a scripting language for chatterbots.
http://www.rivescript.com
MIT License
64 stars 38 forks source link

Error when using library on Android #43

Closed kjw1801 closed 7 years ago

kjw1801 commented 7 years ago

java.nio.file Assertions are going to break a lot of Android projects

Error:(28, 12) error: cannot access Path class file for java.nio.file.Path not found

Is there a way to use it in Java-based Android Native?

marceloverdijk commented 7 years ago

hi @kjw1801, thanks for raising this!

I'm not so familiar with Android to be honest. Is it the java.nio.file.Path import that causes the issue as I read Android is Java 6 based?

I wonder how other libraries do that. What if we move RiveScript-java to JDK8 one day?

kjw1801 commented 7 years ago

@marceloverdijk

Thanks for the quick reply. When I commented out and built loadDirectory and loadFile, it was processed normally. It's not just a simple jdk problem, it's because Android does not support java.nio.file.Path. I have identified issues with similar issues, including:

https://github.com/joel-costigliola/assertj-core/issues/345

http://stackoverflow.com/questions/41787774/class-file-for-java-nio-file-path-not-found

marceloverdijk commented 7 years ago

@kjw1801 what did you exactly comment out? Did you commented out loadDirectory / loadFile in your own app or did you made a custom RiveScript-java build? Can you maybe share some code?

marceloverdijk commented 7 years ago

If just the java.nio.file.Path import causing the issue, I will remove it (it was only added recently).

kjw1801 commented 7 years ago

@marceloverdijk

I commented out the following.

I see this as a java.nio.file.Path import problem.

// Create a new bot with the default settings. //RiveScript bot = new RiveScript();

// To enable UTF-8 mode, you'd have initialized the bot like: RiveScript bot = new RiveScript(Config.utf8());

// Load a directory full of RiveScript documents (.rive files) //bot.loadDirectory("assets");

// Load an individual file. //bot.loadFile("./assets/test.rive");

// Sort the replies after loading them! bot.sortReplies();

// Get a reply. String reply = bot.reply("user", "Hello bot!");

marceloverdijk commented 7 years ago

OK so effectively you are not loading any rivescript documents and this gave no problem? I mean in that case Android can handle the java.nio.file.Path being imported.

Now the weird thing is calling either bot.loadDirectory("assets") or bot.loadFile("./assets/test.rive"); does not use java.nio.file.Path at all. It will use java.io.File.

What is the full exception trace you are getting when loading the rivescript documents again?

kjw1801 commented 7 years ago

@marceloverdijk It can be checked by gradle build error.

Error: (23, 12) error: can not access Path class file for java.nio.file.Path not found

56 lines from RiveScript.java import java.nio.file.Path.

Can not resolve symbol 'path'

Does not include java.nio.file.Path.

https://developer.android.com/reference/java/nio/package-summary.html

I hope that removing java.nio.file.Path and using java.io.file will not be a problem.

marceloverdijk commented 7 years ago

And if you only comment out bot.loadDirectory("assets"); and bot.loadFile("./assets/test.rive"); the build does not complain? Hmm, is Android or the build doing some specific checks then? I mean RiveScript.java import java.nio.file.Path. is also there if you comment both load methods calls.

kjw1801 commented 7 years ago

@marceloverdijk When commented out, it was build. An error is detected in RiveScript.java, but no error is detected until you call loadFile or loadDirectory. I do not know exactly.

marceloverdijk commented 7 years ago

Interesting, I will do some further investigation on SO :-)

marceloverdijk commented 7 years ago

@kjw1801 if these is any other information you can share like stacktrace, github project or build file project let me know.

marceloverdijk commented 7 years ago

@kjw1801 I added 0.9.1-SNAPSHOT to https://oss.sonatype.org/content/repositories/snapshots/com/rivescript/rivescript-core/0.9.1-SNAPSHOT/ in which I removed the java.nio.file.Path reference.

Would you be able to give this snapshot a try? Probably you have to add Sonatype Snapshot repository (https://oss.sonatype.org/content/repositories/snapshots/) to your list of repositories in your Gradle build file.

If that works for you I can make a release without the java.nio stuff.

kirsle commented 7 years ago

When I wrote the initial version of rivescript-java, one of my goals was to be able to use it in Android; and I ran into this problem when trying that out, but then I kinda lost interest in it.

My idea how to resolve it back then was to create a new class, like public class AndroidRiveScript extends RiveScript and override the loadFile()/loadDirectory() functions to get the source files using Android's assets API rather than the filesystem API.

Would something like that work?

marceloverdijk commented 7 years ago

I think the problem @kjw1801 is facing now is because of the convenient loadFile and loadDirectory methods I added accepting a java.nio.path.File. I removed these in a new 0.9.1 snapshot for @kjw1801 to test. If that works I will just remove these methods again (which should not be a problem).

But atm I'm not sure that java.nio.path.File is the only problem.

kjw1801 commented 7 years ago

@marceloverdijk I tested the version of the newly deployed snapshot.

The previous java.nio.path.File error has disappeared.

But there was a new issue.

The way I did it was to create an 'assets' folder inside my Android project and put the test.rive example script in it.

However, 'assets' does not work as a file. I think I need to find another way.

kjw1801 commented 7 years ago

As a further suggestion, if you support uri, you can easily use rive file in 'assets' folder on Android if you support it later. For now, it seems to be the only way to copy and use files.

kjw1801 commented 7 years ago

Tested it and found an error. The contents of the test.rive script are shown below.

! version 2.0
+ hello bot
- Hello human!

I used the loadFile () method.

bot.loadFile("/data/data/" + this.getPackageName() + "/" + "test.rive");

Below is the error content.

03-07 14:42:22.543 18436-18436/com.hellowoo.wooju E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.hellowoo.wooju, PID: 18436
                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hellowoo.wooju/com.hellowoo.wooju.MainActivity}: com.rivescript.parser.ParserException: Invalid format for !Definition line: must be '! type name = value' OR '! type = value'
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
                                                                        at android.app.ActivityThread.access$900(ActivityThread.java:157)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5525)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
                                                                     Caused by: com.rivescript.parser.ParserException: Invalid format for !Definition line: must be '! type name = value' OR '! type = value'
                                                                        at com.rivescript.parser.Parser.checkSyntax(Parser.java:555)
                                                                        at com.rivescript.parser.Parser.parse(Parser.java:203)
                                                                        at com.rivescript.RiveScript.parse(RiveScript.java:700)
                                                                        at com.rivescript.RiveScript.loadFile(RiveScript.java:591)
                                                                        at com.rivescript.RiveScript.loadFile(RiveScript.java:603)
                                                                        at com.hellowoo.wooju.MainActivity.onCreate(MainActivity.java:42)
                                                                        at android.app.Activity.performCreate(Activity.java:6272)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
                                                                        at android.app.ActivityThread.access$900(ActivityThread.java:157) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:148) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5525) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
marceloverdijk commented 7 years ago

Hi @kjw1801 thanks for confirming!

Searching the net about get contents from assets folder I found this http://stackoverflow.com/questions/10980852/android-access-file-from-assets-pdf-display. So probably you have to copy the files.

The other issue you see is because a invalid definition. You have ! version 2.0 but it should be ! version = 2.0 (note the =).

I will make a new release 0.9.1 right now, or did you encounter any other issues?

kjw1801 commented 7 years ago

@marceloverdijk

Something has come up that can be modified and applied again.

! version = 2.0
+ hello bot
- Hello human!

I think the following is the problem.

03-07 17:03:52.942 1138-1138/com.hellowoo.wooju E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.hellowoo.wooju, PID: 1138
                                                                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hellowoo.wooju/com.hellowoo.wooju.MainActivity}: java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 11:
                                                                  \{__call__}
                                                                             ^
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
                                                                      at android.app.ActivityThread.access$900(ActivityThread.java:157)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5525)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
                                                                   Caused by: java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 11:
                                                                  \{__call__}
                                                                             ^
                                                                      at java.util.regex.Pattern.compileImpl(Native Method)
                                                                      at java.util.regex.Pattern.compile(Pattern.java:411)
                                                                      at java.util.regex.Pattern.<init>(Pattern.java:394)
                                                                      at java.util.regex.Pattern.compile(Pattern.java:381)
                                                                      at java.lang.String.replaceAll(String.java:1353)
                                                                      at com.rivescript.RiveScript.processTags(RiveScript.java:2008)
                                                                      at com.rivescript.RiveScript.getReply(RiveScript.java:1662)
                                                                      at com.rivescript.RiveScript.reply(RiveScript.java:1296)
                                                                      at com.hellowoo.wooju.MainActivity.onCreate(MainActivity.java:49)
                                                                      at android.app.Activity.performCreate(Activity.java:6272)
                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
                                                                        ... 9 more
marceloverdijk commented 7 years ago

@kjw1801 I found an issue and deployed a new snapshot (0.9.1-SNAPSHOT). Can you please try again.

kjw1801 commented 7 years ago

@marceloverdijk

After gradle rebuild with 0.9.1-SNAPSHOT. I'm testing, but I still get the same error.

03-07 18:57:24.557 29116-29116/com.hellojoo.wooju E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.hellojoo.wooju, PID: 29116
                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hellojoo.wooju/com.hellojoo.wooju.MainActivity}: java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 11:
                                                                    \{__call__}
                                                                               ^
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
                                                                        at android.app.ActivityThread.access$900(ActivityThread.java:157)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5525)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
                                                                     Caused by: java.util.regex.PatternSyntaxException: Syntax error in regexp pattern near index 11:
                                                                    \{__call__}
                                                                               ^
                                                                        at java.util.regex.Pattern.compileImpl(Native Method)
                                                                        at java.util.regex.Pattern.compile(Pattern.java:411)
                                                                        at java.util.regex.Pattern.<init>(Pattern.java:394)
                                                                        at java.util.regex.Pattern.compile(Pattern.java:381)
                                                                        at java.lang.String.replaceAll(String.java:1353)
                                                                        at com.rivescript.RiveScript.processTags(RiveScript.java:2008)
                                                                        at com.rivescript.RiveScript.getReply(RiveScript.java:1662)
                                                                        at com.rivescript.RiveScript.reply(RiveScript.java:1296)
                                                                        at com.hellojoo.wooju.MainActivity.onCreate(MainActivity.java:48)
                                                                        at android.app.Activity.performCreate(Activity.java:6272)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
                                                                            ... 9 more
marceloverdijk commented 7 years ago

Hmm, I would have expected \{__call__\} and I see \{__call__} in your logging. Can you check the version (= timestamp) of the snapshot or even force it to refresh?

It should be rivescript-core-0.9.1-20170307.082843-2

kjw1801 commented 7 years ago

I used the gradle build, but I do not think the new version has been applied.

How do I fix the version name here?

compile 'com.rivescript:rivescript-core:0.9.1-SNAPSHOT'

marceloverdijk commented 7 years ago

Just add --refresh-dependencies to your gradle command like gradle build --refresh-dependencies.

kjw1801 commented 7 years ago

I knew I was working normally. Thank you.

marceloverdijk commented 7 years ago

Did this mean the changes worked for you and I can make new release containing the fixes?

kjw1801 commented 7 years ago

I checked to see if your error was fixed.

marceloverdijk commented 7 years ago

And were they fixed? Does it work for you now?

kjw1801 commented 7 years ago

Yes I have run the example properly in the Android studio environment.

marceloverdijk commented 7 years ago

Great, then I will make a release! Thx for raising this.

marceloverdijk commented 7 years ago

Release is done and release 0.9.1 will be available in Maven Central shortly.