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
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 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.
The move to Prolog is hard Prolog can also chat do databases and typically that looks very neat as RDBMS and Prolog are both relational languages Good luck and keep asking!
You can read how to access an RDBMS from SWI-Prolog using ODBC here