Esempio n. 1
0
  public static void main(String[] args) {
    YProlog engine = new YProlog();

    engine.consult("append([],L,L).");
    engine.consult("append([A|B],L,[A|Tail]) :- append(B,L,Tail).");

    // String query = "append(X,[c],[a,n(J),cs])" ;
    String query = "asserta(a(1))";
    String value = engine.queryToString(query);

    if (value != null) log.debug(value.toString());

    String[] result = engine.queryToTable(query);

    if (result != null)
      for (int i = 0; i < result.length; i++) log.debug("Result " + i + ":" + result[i]);

    Vector results = engine.queryToTable(query, 0, true);

    if (results != null)
      for (int i = 0; i < results.size(); i++) {
        log.debug("Results " + i);
        result = (String[]) results.elementAt(i);
        for (int j = 0; j < result.length; j++) log.debug("Result" + j + ": " + result[j]);
      }
  }
Esempio n. 2
0
  public void unifyTree(Tree tree, String unifiedGoal) throws InferenceEngineException {
    String query =
        "old("
            + tree.getGoal()
            + ","
            + tree.getLastExpandedGoal()
            + ","
            + tree.getResolvent()
            + ")";

    log.debug("Unify new query: " + unifiedGoal + " and old query: " + query);

    checkEngine();

    //		PrologTerm oldQuery = PrologTools.getTerm(query) ;
    //		PrologTerm newQuery = PrologTools.getTerm(unifiedGoal) ;

    log.debug("unification(" + query + "," + unifiedGoal + ",Result)");

    String[] result = _engine.queryToTable("unification(" + query + "," + unifiedGoal + ",Result)");
    if (result == null) {
      log.debug("No answers");
      throw new InferenceEngineException("Error unifying " + query + " and " + unifiedGoal);
    }

    PrologTerm term;
    try {
      term = PrologTools.getTerm(result[0]);
    } catch (ParseException e) {
      throw new InferenceEngineException(e);
    }

    if ((term == null) || (!(term instanceof PrologCompoundTerm)))
      throw new InferenceEngineException("Resulting value '" + term + "' is wrong");
    else {
      log.debug("Term: " + term);

      PrologCompoundTerm t = (PrologCompoundTerm) term;

      tree.setLastExpandedGoal(null);
      tree.setGoal(t.getArg(0).getText());
      tree.setResolvent(t.getArg(2).getText());
    }
  }
Esempio n. 3
0
  public LogicAnswer[] processTree(LogicQuery logicQuery) throws InferenceEngineException {

    log.debug("Process logic query: " + logicQuery.getGoal() + " - " + logicQuery.getSubgoals());

    checkEngine();

    String query =
        "tree("
            + logicQuery.getGoal()
            + ","
            + logicQuery.getSubgoals()
            + ","
            +
            //								"[]," +
            logicQuery.getRequester()
            + ")";

    log.debug("Query: " + query);

    Vector results = _engine.queryToTable("processTree(" + query + ",Result)", 0, true);

    if (results == null) {
      log.debug("No answers");
      return null;
    }

    log.debug("Receiving from engine " + results.size() + " results");

    String[] result;
    PrologTerm term;

    LogicAnswer[] answers = new LogicAnswer[results.size()];

    for (int i = 0; i < results.size(); i++) {
      log.debug("Results " + i);
      result = (String[]) results.elementAt(i);

      try {
        for (int j = 0; j < result.length; j++) log.debug("Term: " + result[j]);
        term = PrologTools.getTerm(result[1]);
      } catch (ParseException e) {
        throw new InferenceEngineException(e);
      }

      if ((term == null) || (!(term instanceof PrologCompoundTerm)))
        throw new InferenceEngineException("Resulting value '" + term + "' is wrong");
      else {
        PrologCompoundTerm t = (PrologCompoundTerm) term;

        String delegator = t.getArg(5).getText();
        if (delegator.equals("nil")) delegator = null;

        answers[i] =
            new LogicAnswer(
                t.getArg(0).getText(),
                t.getArg(4).getText(),
                t.getArg(1).getText(),
                t.getArg(2).getText(),
                t.getArg(3).getText(),
                delegator);

        log.debug("Answer: " + answers[i]);
      }
    }

    return answers;
  }