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

Unable to use macros successfully #63

Closed ArjunVermaGitHub closed 5 years ago

ArjunVermaGitHub commented 5 years ago

Hi again, and sorry for making this my personal forum but I don't know a better place to go than this. I have tried copying the perl macro from the website:

> object hash perl
  my ($rs, $args) = @_;
  my $method = shift @{$args};
  my $string = join " ", @{$args};

  # Here, $method is a hashing method (MD5 or SHA1), and $string
  # is the text to hash.

  if ($method eq "MD5") {
    require Digest::MD5;
    return Digest::MD5::md5_hex($string);
  }
  elsif ($method eq "SHA1") {
    require Digest::SHA1;
    return Digest::SHA1::sha1_hex($string);
  }
< object

// You call an object using the <call> tag.
+ what is the md5 hash of *
- The hash of "<star>" is: <call>hash MD5 <star></call>

+ what is the sha1 hash of *
- The hash of "<star>" is: <call>hash SHA1 <star></call>

but it doesn't work. I have also tried javascript macros which have worked on the online compiler but they don't work here. I even used these imports: import com.rivescript.; import com.rivescript.lang.; import com.rivescript.macro.*; Am I missing something? Whenever I call the object macros I get error:object not found in place of my object in the output.

marceloverdijk commented 5 years ago

@ArjunVermaGitHub No worries :-) What do you mean with it doesn't work? Do you get an exception?

And did you configure the lang specific object handler like:

rs = new RiveScript();
rs.setHandler("javascript", new JavaScriptHandler());

We have various integration tests using macros. See e.g. https://github.com/aichaos/rivescript-java/blob/master/rivescript-core/src/integration-test/java/com/rivescript/JavaScriptObjectIT.java

ArjunVermaGitHub commented 5 years ago

Thank you so much. When I said it doesn't work, I meant it didnt give me the output I required. For example, in your reverse hello world program, instead of elloh dlrow, I got err:object not found. The program ran fine, just the output is unexpected. No, I did not configure it like you said to, but something weird is happening now that I did. When I try to use the setHandler function outside my onCreate() function for my app, the program cant find the function in the packages, but it does get recognized when I use the function inside it. Still, the application throws an error when I launch the activity.

Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.script.ScriptEngineManager" on path: DexPathList[[zip file "/data/app/com.example.myapplication-2/base.apk", zip file "/data/app/com.example.myapplication-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.myapplication-2/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.myapplication-2/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.myapplication-2/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.myapplication-2/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.myapplication-2/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.myapplication-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.myapplication-2/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.myapplication-2/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.myapplication-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.myapplication-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.myapplication-2/lib/arm, /system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)

is what seems relevant to me, please tell me if I should paste more of the stackTrace

marceloverdijk commented 5 years ago

Based on the exception (Didn't find class "javax.script.ScriptEngineManager") I think you are missing the javax.script dependency. Probably it's not part of the Dalvik JVM but I'm not sure as I have no experience with Dalvik...

Maybe this helps: https://stackoverflow.com/questions/25371949/is-it-possible-to-use-javascript-code-in-android

ArjunVermaGitHub commented 5 years ago

It can't be fixed with an import statement. I cant seem to install a dependency either. Would it be faster to switch over to perl or some other type of macro? Or is it possible to use java functions in some way to get the same work done?

marceloverdijk commented 5 years ago

@ArjunVermaGitHub yes you could also use Java subroutines as , see https://github.com/aichaos/rivescript-java/blob/master/rivescript-core/src/integration-test/java/com/rivescript/JavaObjectIT.java for an example in an integration test.

But I wonder why you say it can't be fixed? It's not only about adding an import statement. You need to addition libraries (jars) to your project that contain the script engine implementation.

ArjunVermaGitHub commented 5 years ago

OK I installed the jar file, and now the import happens properly, but I got a new kind of exception: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Enumeration java.lang.ClassLoader.getResources(java.lang.String)' on a null object reference This is originating from the same line: bot.setHandler("javascript", new JavaScriptHandler()); Why is it a null object reference when I have already used the constructor?

marceloverdijk commented 5 years ago

I have no idea. Probably you have to look at the full stack trace to see from where it originates...

ArjunVermaGitHub commented 5 years ago

Well, I managed to do pretty well using Java macros. I've had a lot of fun with this and sincerely thank you for the help. I do feel there may have been something weird happening with the javascript for android business, but even if thats true, the workaround is really good. I have been developing a bot for the past few days and can envision the enormous applications this platform has to offer.

marceloverdijk commented 5 years ago

Thx for the feedback @ArjunVermaGitHub. I'm closing this issue for now. If you anything else feel free to raise another issue or re-open this one.