{"id":4,"date":"2016-05-13T20:37:42","date_gmt":"2016-05-13T17:37:42","guid":{"rendered":"http:\/\/siteblog.tuc.gr\/nispanoudakis\/?p=4"},"modified":"2021-09-24T16:53:02","modified_gmt":"2021-09-24T13:53:02","slug":"quering-prolog-from-java","status":"publish","type":"post","link":"https:\/\/siteblog.tuc.gr\/nispanoudakis\/2016\/05\/13\/quering-prolog-from-java\/","title":{"rendered":"Querying Prolog from Java"},"content":{"rendered":"<p>This blog entry is for those who want to use Prolog queries from their Java program. I use swi-prolog, a free implementation of the Prolog language.<\/p>\n<p>The SWI-prolog installation&nbsp;includes&nbsp;the java jpl7 package (in the lib installation folder):<br \/>\n<a class=\"moz-txt-link-freetext\" href=\"http:\/\/jpl7.org\/JavaApiOverview.jsp\">http:\/\/jpl7.org\/JavaApiOverview.jsp<\/a><\/p>\n<p>To use it from java you need to setup some system variables before executing your app. Moreover, the java and swi-prolog installations must both be 32 bit or 64bit. For example you can edit a run.bat batch file with the following commands (assuming that swi-prolog is installed in c:\/program files\/swipl and your app is in the executable &lt;your jar name&gt;.jar file):<\/p>\n<pre>  set SWI_PROLOG_PATH=c:\\program files\\swipl\n  set path=%path%;%SWI_PROLOG_PATH%\\bin\\;%SWI_PROLOG_PATH%\\lib\\\n  swipl.exe --dump-runtime-variables=cmd &gt; plvars.bat\n  call plvars.bat\n  java -jar &lt;your jar name&gt;.jar<\/pre>\n<p>This approach assumes that the jpl.jar library file is included in your jar file, otherwise you must add the jpl.jar to the classpath variable. In the <a href=\"http:\/\/siteblog.tuc.gr\/nispanoudakis\/2016\/07\/13\/debugging-a-java-application-querying-prolog-using-eclipse\/\">next blog entry<\/a> I explain how to do that using eclipse.<\/p>\n<p>See an example that adds in the fact <em>father(john,nikos)<\/em> to the prolog database and then runs the query <em>father(X,nikos)<\/em>:<\/p>\n<pre>  \nimport java.util.Map;\nimport org.jpl7.*;\n\npublic class TestPrologInterface {\n\n  public static void main(String[] args) {\n    String s = \"assert(father(john,nikos)).\";\n    Query q = new Query(s);\n    \/\/ run the query\n    if (q.hasNext()) {\n      \/\/ now I can ask for the father of nikos\n      s = \"father(X,nikos).\";\n      q = new Query(s);\n      \/\/ run the query, repeat while there are more results\n      while (q.hasNext()){\n        \/\/get a mapping of variables to instantiations:\n\tMap&lt;String, Term&gt; map = q.next();\n\t\/\/get the instance of variable X\n\tString varX = map.get(\"X\").toString();\n\t\/\/print variable instance\n\tSystem.out.println(varX);\n      }\n    }\n  }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This blog entry is for those who want to use Prolog queries from their Java program. I use swi-prolog, a free implementation of the Prolog language. The SWI-prolog installation&nbsp;includes&nbsp;the java jpl7 package (in the lib installation folder): http:\/\/jpl7.org\/JavaApiOverview.jsp To use it from java you need to setup some system variables before executing your app. Moreover, &hellip; <a href=\"https:\/\/siteblog.tuc.gr\/nispanoudakis\/2016\/05\/13\/quering-prolog-from-java\/\" class=\"more-link\">\u03a3\u03c5\u03bd\u03ad\u03c7\u03b5\u03b9\u03b1 \u03b1\u03bd\u03ac\u03b3\u03bd\u03c9\u03c3\u03b7\u03c2 <span class=\"screen-reader-text\">Querying Prolog from Java<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":77,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,13,36,8,12],"tags":[],"class_list":["post-4","post","type-post","status-publish","format-standard","hentry","category-java","category-jpl","category-prolog","category-windows","category-windows-batch-file"],"_links":{"self":[{"href":"https:\/\/siteblog.tuc.gr\/nispanoudakis\/wp-json\/wp\/v2\/posts\/4","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/siteblog.tuc.gr\/nispanoudakis\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/siteblog.tuc.gr\/nispanoudakis\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/siteblog.tuc.gr\/nispanoudakis\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/siteblog.tuc.gr\/nispanoudakis\/wp-json\/wp\/v2\/comments?post=4"}],"version-history":[{"count":10,"href":"https:\/\/siteblog.tuc.gr\/nispanoudakis\/wp-json\/wp\/v2\/posts\/4\/revisions"}],"predecessor-version":[{"id":177,"href":"https:\/\/siteblog.tuc.gr\/nispanoudakis\/wp-json\/wp\/v2\/posts\/4\/revisions\/177"}],"wp:attachment":[{"href":"https:\/\/siteblog.tuc.gr\/nispanoudakis\/wp-json\/wp\/v2\/media?parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/siteblog.tuc.gr\/nispanoudakis\/wp-json\/wp\/v2\/categories?post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/siteblog.tuc.gr\/nispanoudakis\/wp-json\/wp\/v2\/tags?post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}