tags:

views:

398

answers:

2

Hello guys.

I'm having trouble with some of my code and I really cant trouble shoot this error.

I'm using Interprolog(Java+Prolog) see here

here is the stacktrace:

  Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(Unknown Source)
    at      com.declarativa.interprolog.AbstractPrologEngine.copyToTemp(AbstractPrologEngine.java)
 at com.declarativa.interprolog.AbstractPrologEngine.consultFromPackage(AbstractPrologEngine.java)
 at LoginHandler.actionPerformed(LoginHandler.java:24)
 at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
 at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
 at java.awt.Component.processMouseEvent(Unknown Source)
 at javax.swing.JComponent.processMouseEvent(Unknown Source)
 at java.awt.Component.processEvent(Unknown Source)
 at java.awt.Container.processEvent(Unknown Source)
 at java.awt.Component.dispatchEventImpl(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Window.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.EventQueue.dispatchEvent(Unknown Source)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.run(Unknown Source)

and the code in question:

      myEngine = new NativeEngine();
  myEngine.consultFromPackage("C:\\interprolog212a\\userlist.p", LoginHandler.class);
  boolean x = myEngine.deterministicGoal("hasAccess(user,pass)");

any ideas? I'm hitting a brickwall...

oh and line 24 is the second line of code i posted.

edit: content of userlist.p:

hasAccess(tom,123).
hasAccess(bob,456).

following on from some of the suggestions below, i moved loginhandler and related classes to their own package and also userlist.p to the package, and now i get this error:

com.declarativa.interprolog.util.IPException: Problem consulting from package         archive:C:\Users\Keval\AppData\Local\Temp\IP_5283895338735856757\userlist.p
A: 

Is the second \\ in the path confusing it? If it's doing anything to examine each directory in the path it could be confused by an empty string, maybe?

Alex Poole
+2  A: 

Yay for open source I suppose. It's doing this:

String className = rc.getName();    
String packageName = className.substring(0,className.lastIndexOf('.'));

rc is your calling class. So I suppose your LoginHandler class is in the default package and that is tripping this code up? Try putting it in a package, i.e. add package blah; to the top of it and move it to a directory with that name.

It does this because it assumes the userlist.pl is actually in the same location as your code and it's trying to use the package name to find the location your code was loaded from. I don't think you can use consultFromPackage() the way you are trying to actually, since it supposedly can only parse a filename, not a full blown path.

Oh and you can get the source. It's in the zip file linked from the website.

wds
hi there. I have also tried ConsultFromPackage(userlist.pl, class) as the pl file is indeed in the same package/dir as the java files/code location.
KP65
@keval You might have missed my second edit. Try putting `LoginHandler` in a package. I assume it's currently in the default package (i.e. no package declaration at the top).
wds
I have moved it to its own package now, will let you know what happens.
KP65
Right, moved both the consulting file(userlist.pl) and the loginhandler to its own package, now getting this error:com.declarativa.interprolog.util.IPException: Problem consulting from package archive:C:\Users\Keval\AppData\Local\Temp\IP_5283895338735856757\userlist.p
KP65