1

I'm using tuProlog (http://tuprolog.alice.unibo.it/) to run some prolog clauses from inside java. I'm having some problems with Definite Clause Grammars, and I think Stackoverflow might be the right place.

Using the Definite Clause Grammar example from http://www.learnprolognow.org/lpnpage.php?pagetype=html&pageid=lpn-htmlse29, we have

s  -->  np,vp . 
np  -->  det,n. 
vp  -->  v,np. 
vp  -->  v. 
det  -->  [the]. 
det  -->  [a]. 
n  -->  [woman]. 
n  -->  [man]. 
v  -->  [shoots].

I pull this into tuProlog with the following java code (which has been tested on other prolog examples)

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import alice.tuprolog.NoMoreSolutionException;
import alice.tuprolog.NoSolutionException;
import alice.tuprolog.Prolog;
import alice.tuprolog.SolveInfo;
import alice.tuprolog.Theory;

public class TestDefinateClauseGrammar {
    public static void main(String[] args) throws Exception {
        Prolog engine = new Prolog();
        engine.addTheory(new Theory(readFile("/Users/josephreddington/Documents/workspace/com.plancomps.prolog.helloworld/grammar.pl")));
    }

    private static String readFile(String file) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line = null;
        StringBuilder stringBuilder = new StringBuilder();
        String ls = System.getProperty("line.separator");
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
            stringBuilder.append(ls);
        }
        return stringBuilder.toString();
    }
}

But unfortuantely I get the exception:

Exception in thread "main" alice.tuprolog.InvalidTheoryException: The term 's' is not ended with a period.
    at alice.tuprolog.TheoryManager.consult(TheoryManager.java:193)
    at alice.tuprolog.Prolog.addTheory(Prolog.java:242)
    at TestDefinateClauseGrammar.main(TestDefinateClauseGrammar.java:13) 

Can anyone tell me the problem? I understand that tuprolog should support Definite Clause Grammars because their manual (http://tuprolog.sourceforge.net/doc/2p-guide.pdf) includes the quote:

5.2 ISOLibrary
Library Dependencies: BasicLibrary.
This library contains almost1 all the built-in predicates and functors that
are part of the ISO standard and that are not part directly of the tuProlog
core engine or other core libraries. Moreover, some features are added, not
currently ISO, such as the support for definite clause grammars (DCGs).

Ideas welcome....

manlio
  • 18,345
  • 14
  • 76
  • 126
Joe
  • 4,367
  • 7
  • 33
  • 52

2 Answers2

4

You must load the DCG library explicitly since it is not loaded by default.

You can do it in two ways:

  1. use the load_library directive inside the theory, like: :-load_library('alice.tuprolog.lib.DCGLibrary').
  2. call the load library method on engine: engine.loadLibrary("alice.tuprolog.lib.DCGLibrary")

Please refer to the Google Code repository here (https://code.google.com/p/tuprolog/) where you can find the most updated version of the engine and of the manual.

Cheers

Ale

Alessandro
  • 120
  • 1
  • 2
  • 9
  • Perfect! (although only the second alternative worked) Thank you very much indeed :) – Joe May 21 '13 at 14:49
  • you may be interested in this question... http://stackoverflow.com/q/16895960/170243 – Joe Jun 03 '13 at 11:46
1

So I got an answer to this by email ( might be useful for other people...)

Your exception means that your prolog theory is not written as it is expected in 2Prolog. In order to test your prolog code, you can run 2p.jar ...it opens a GUI that tells you where the error is. From java you cannot quite understand what is going on. Is it possible that you want to write something like : s:-np,vp? I have never used the --> notation in 2Prolog, I do not think it is possible.

Joe
  • 4,367
  • 7
  • 33
  • 52