Data structure for rules in JPL

Hi. It is very convenient to construct rules in SWIPL terminal. But wen using JPL, I didn’t find a proper data structure for rules. Current solution I found is to use a “universal” parser for Term in JPL, e.g.

Term rule = Term.textToTerm("ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y)");

However, it’s a little bit … ugly :frowning:
Any other ideas?

If you want to add a rule, i.e., the string is not composed in Java but added literally, I think the above is not too bad. If you want to add a rule based on Java data structures it may get really ugly :frowning: I don’t have a solution for that. Never use Java, but it roughly looks as ugly in C or C++, which I do use.

My overall solution is to do the term manipulation work as much as possible in Prolog, creating a clean API between the Java and Prolog part that, where possible, only communicates simple data structures. I’ve seen lots of example of people using JPL doing as much as possible in Java: adding data to Prolog, adding rules to Prolog and finally call some query. Often all this can be simplified a lot by writing a Prolog program that does most of the work and calling that from Java. Also note that if you have a lot of data in Java you can migrate it to Prolog. You can also write a Prolog predicate using JPL that accesses the data in Java without transferring it.

1 Like

I’m in a research project and so I need to perform some quick experiments. I use Java and Python for a long time so I chose Java without any second thought :joy: And I need to perform other operations such as (maybe) RDBMS operations and custom index design … then maybe I don’t have too much choice :broken_heart:
I just began to learn Prolog recently and not very familiar with things behind. I will try my best to explore more in the future since logic programming is getting more interesting to me :smiley:

The move to Prolog is hard :frowning: Prolog can also chat do databases and typically that looks very neat as RDBMS and Prolog are both relational languages :slight_smile: Good luck and keep asking!

You can read how to access an RDBMS from SWI-Prolog using ODBC here