You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to use projog for my java application by importing it as a library. Some of the rules will be implemented in java using AbstractSingleResultPredicate.
Is it possible to add such rules (i.e., those implemented in Java) dynamically during the execution of the java program ?
The example program SingleResultPredicateExample.java shows an implementation where such rules can be added from prolog console. But I am not sure the rules can be added dynamically from Java.
Can you please tell me if this is possible ? Thanks for your time.
Best regards,
Sreeram
The text was updated successfully, but these errors were encountered:
Yes, you can add new predicates dynamically at runtime. You can do this using the addPredicateFactory method of org.projog.api.Projog. Here is a simple example which I hope demonstrates the behavior you want:
package org.projog.example;
import org.projog.api.Projog;
import org.projog.core.predicate.AbstractSingleResultPredicate;
import org.projog.core.predicate.PredicateKey;
import org.projog.core.term.Term;
public class Example {
public static void main(String[] args) {
Projog p = new Projog();
p.addPredicateFactory(new PredicateKey("hello", 1), new ExamplePredicate());
p.executeQuery("hello(world).");
}
}
class ExamplePredicate extends AbstractSingleResultPredicate {
@Override
protected boolean evaluate(Term arg) {
System.out.println("Hello, " + arg + "!");
return true;
}
}
The PredicateKey specifies the name and arity (i.e. number of arguments) that will be used in Prolog code to call the Java code. So in the example above I have configured a new predicate named "hello" which takes one argument.
Here is a link to the Javadoc for the addPredicateFactory method:
I used this approach in my prolog-expert-system project where I created a new predicate named menuask that takes three arguments. This happens in the createProjog method of RulesEngine:
Hi,
I would like to use projog for my java application by importing it as a library. Some of the rules will be implemented in java using AbstractSingleResultPredicate.
Is it possible to add such rules (i.e., those implemented in Java) dynamically during the execution of the java program ?
The example program SingleResultPredicateExample.java shows an implementation where such rules can be added from prolog console. But I am not sure the rules can be added dynamically from Java.
Can you please tell me if this is possible ? Thanks for your time.
Best regards,
Sreeram
The text was updated successfully, but these errors were encountered: