# Calling prolog from Java with JPL, hasSolution() not responding

**URL:** <https://swi-prolog.discourse.group/t/calling-prolog-from-java-with-jpl-hassolution-not-responding/6396>\
**Category:** Help!\
**Created:** [March 22, 2023, 11:30am UTC](https://swi-prolog.discourse.group/t/calling-prolog-from-java-with-jpl-hassolution-not-responding/6396 "2023-03-22T11:30:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicolep](https://avatars.discourse-cdn.com/v4/letter/n/a87d85/32.png) [@nicolep](https://swi-prolog.discourse.group/u/nicolep)\
**Post date:** [March 22, 2023, 11:30am UTC](https://swi-prolog.discourse.group/t/calling-prolog-from-java-with-jpl-hassolution-not-responding/6396/1 "2023-03-22T11:30:43Z")

</div>

I am devloping a java application that generates prolog files and tests these files against a set of data. I am using SWI-Prolog 9.0.4 and Java jdk-17.0.1.

At each iteration of the process, a prolog file is consulted N times, where N is the number of datapoints in the dataset. During each consultation, the prolog file is further queried 2 times.  
At some point, the testing process is not responding (q.hasSolution() in particular). (q is a Query object)

Any ideas what could be causing this behavior?

Thanks

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [March 22, 2023, 2:29pm UTC](https://swi-prolog.discourse.group/t/calling-prolog-from-java-with-jpl-hassolution-not-responding/6396/2 "2023-03-22T14:29:28Z")

</div>

Hard to say. Repeated calls on Prolog from external languages need precautions from building up resources. In the C interface this is done by PL\_open\_foreign\_frame(). I know too little about JPL to know whether this could be a problem. I have some problems understanding

> [@nicolep](#):
>
> a prolog file is consulted N times, where N is the number of datapoints in the dataset. During each consultation, the prolog file is further queried 2 times.

What is a datapoint here? Do new datapoints add to or replace existing ones? I guess “During” must be “after”?

Without more details it is hard to say what could be wrong. You might add a call to statistics/0 after each iteration to get an idea whether resources are building up.

Surely for data, I would not write a file and consult it. At best, this is slow. Writing and parsing is slow and Prolog’s consult aims at program code rather than data and thus there is a lot of overhead and admin. Instead, use assertz/1 and other parts of the dynamic database management.

---

<div class="post-metadata">

**Author:** ![nicolep](https://avatars.discourse-cdn.com/v4/letter/n/a87d85/32.png) [@nicolep](https://swi-prolog.discourse.group/u/nicolep)\
**Post date:** [March 23, 2023, 4:33am UTC](https://swi-prolog.discourse.group/t/calling-prolog-from-java-with-jpl-hassolution-not-responding/6396/3 "2023-03-23T04:33:09Z")

</div>

Hello, thanks for your quick reply.  
A datapoint is an entry (record) in a dataset. I am using a background file for new datapoints, that is, before consulting the prolog file, I write one entry in the background file. Yes, “during” means after consulting the prolog file.  
The prolog file is developed incrementally, at each iteration of the process I write a new version of the file. I could consider asserting data (facts) instead of writing them in the background file. I will give it a try.  
Thanks.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [March 23, 2023, 8:28am UTC](https://swi-prolog.discourse.group/t/calling-prolog-from-java-with-jpl-hassolution-not-responding/6396/4 "2023-03-23T08:28:10Z")

</div>

So you create an ever growing file that you reload over and over? That is surely hugely inefficient. It should work though. The reason for failure is probably in how you call Prolog repeatedly from Java (and possibly in JPL). Again, calling statistics/0 on each iteration to see what is happening would be my first step.

Asserting the data may not fix this. Surely it will be many times faster 🙂
