import java.util.*; import jtp.*; import jtp.context.*; import jtp.disp.DispatcherUtils; import jtp.fol.*; import jtp.func.Less; import jtp.gmp.*; import jtp.modelim.AskingQueryProcessor; public class Test1 { public static void main (String [] argv) throws Exception { //Set up reasoners, same as before... BasicReasoningContext context = new BasicReasoningContext(); context.defaultSetup(); context.setMaxDepth(10); //A KB for FOL clauses ClauseOrientationKB ckb = new ClauseOrientationKB(); context.add(ckb); //Add to context //A reasoner for processing complex queries AskingQueryProcessor aqp = new AskingQueryProcessor(); context.add(aqp); //Add to context //Add basic reasoners to asking dispatcher DispatcherUtils.addToDispatcher(ckb.getAskingReasoner(), context.getAskingDispatcher()); DispatcherUtils.addToDispatcher(aqp, context.getAskingDispatcher()); //Add a reasoner that determines if one number is less than //another to asking dispatcher DispatcherUtils.addToDispatcher(new Less(), context.getAskingDispatcher()); //Add basic reasoners to telling dispatcher DispatcherUtils.addToDispatcher(ckb.getTellingReasoner(), context.getTellingDispatcher()); context.tellString("(age fred 42)"); //The query is no longer a literal String query = "(and (age fred ?x)(< ?x 43))"; ReasoningStepIterator rsi = context.getAskingReasoner().process(query); ReasoningStep rs = null; while ((rs = rsi.next()) != null) { // If the query is not a literal, then it is transformed into one. The top level // reasoning step then will be the transformation step. Its sole child will have // a goal which is a literal and which is equivalent to the original goal. We // extract the variables from this child's goal. if (query.equals(rs.getGoal()) && rs.getSubProofs().size() == 1) { ReasoningStep subRs = (ReasoningStep)rs.getSubProofs().get(0); if (subRs.getGoal() instanceof Literal) rs = subRs; //We are considering the sole child instead of the top-level //step (see above) } Literal answerLit = (Literal)rs.getGoal();//The answer literal with all the query variables Map bindings = jtp.rs.RSUtils.getRecursiveBindings(rs, null);//Variable bindings for (Iterator it = answerLit.getArgs().iterator(); it.hasNext();) { //Find the right query variable and extract its value Variable v = (Variable)it.next(); if ("x".equals(v.getName())) { System.out.println("X = " + bindings.get(v)); break; } } } } }