Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question on adding prolog rules dynamically #209

Open
sreeramvenkat opened this issue Jun 3, 2022 · 1 comment
Open

Question on adding prolog rules dynamically #209

sreeramvenkat opened this issue Jun 3, 2022 · 1 comment
Labels

Comments

@sreeramvenkat
Copy link

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

@s-webber
Copy link
Owner

s-webber commented Jun 4, 2022

Thank you for asking this question.

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:

http://projog.org/javadoc/org/projog/api/Projog.html#addPredicateFactory-org.projog.core.predicate.PredicateKey-org.projog.core.predicate.PredicateFactory-

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:

https://github.com/s-webber/prolog-expert-system/blob/main/src/main/java/org/projog/expert/RulesEngine.java

If this answer does not provide the information you require then please let me know and I will do my best to help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants