package bots.prologbot.prologsystem; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import alice.tuprolog.InvalidLibraryException; import alice.tuprolog.InvalidTheoryException; import alice.tuprolog.NoSolutionException; import alice.tuprolog.Prolog; import alice.tuprolog.SolveInfo; import alice.tuprolog.Struct; import alice.tuprolog.Term; import alice.tuprolog.Theory; import alice.tuprolog.Var; import alice.tuprolog.lib.IOLibrary; public class TuPrologSystem implements PrologSystem { private final Theory bkTheory; private Theory botTheory; private Theory stateTheory; private final Prolog engine; public TuPrologSystem() { engine = new Prolog(); try { engine.loadLibrary(new IOLibrary()); } catch (InvalidLibraryException e) { throw new IllegalStateException(e); } try { bkTheory = new Theory(new FileInputStream(new File("./data/prologbot/bk-2p.pl"))); } catch (IOException e1) { throw new IllegalStateException(e1); } } public synchronized void updateBotProlog(String prolog) { try { botTheory = new Theory(prolog); } catch (InvalidTheoryException e) { throw new IllegalStateException(e); } } public synchronized void updateState(String state) { try { stateTheory = new Theory(state); } catch (InvalidTheoryException e) { throw new IllegalStateException(e); } } public synchronized String executeQuery() { try { Theory allTheories = new Theory(bkTheory.toString() + "\n" + botTheory.toString() + "\n" + stateTheory.toString() + "\n"); //TODO first compile into Clause list //TODO error: facts go missing // allTheories.append(bkTheory); // allTheories.append(botTheory); // allTheories.append(stateTheory); engine.setTheory(allTheories); } catch (InvalidTheoryException e) { throw new IllegalStateException(e); } Var action = new Var("Action"); Var rule = new Var("Rule"); // System.out.println("THEORY: \n" + engine.getTheory().toString()); Term query = new Struct("do", action, rule);//Term.createTerm("round(A)"); //Term.createTerm("do(A)"); // SolveInfo result = engine.solve(query); // System.out.println("RESULT: " + result.toString()); try { // System.out.println("ACTION: " + result.getVarValue(action.getOriginalName())); return result.getVarValue(action.getOriginalName()).toString() + "," + result.getVarValue(rule.getOriginalName()).toString(); } catch (NoSolutionException e) { return ",Geen"; } } }