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.