From: <jom...@us...> - 2011-07-18 10:10:00
|
Revision: 1652 http://jason.svn.sourceforge.net/jason/?rev=1652&view=rev Author: jomifred Date: 2011-07-18 10:09:53 +0000 (Mon, 18 Jul 2011) Log Message: ----------- implementation of backtracking for .intend and .desire Modified Paths: -------------- trunk/src/jason/asSyntax/parser/AS2JavaParser.jcc trunk/src/jason/asSyntax/parser/as2j.java trunk/src/jason/stdlib/desire.java trunk/src/jason/stdlib/intend.java Modified: trunk/src/jason/asSyntax/parser/AS2JavaParser.jcc =================================================================== --- trunk/src/jason/asSyntax/parser/AS2JavaParser.jcc 2011-06-24 13:39:37 UTC (rev 1651) +++ trunk/src/jason/asSyntax/parser/AS2JavaParser.jcc 2011-07-18 10:09:53 UTC (rev 1652) @@ -805,7 +805,7 @@ StringTerm string():{ Token k; StringTermImpl s; } { - k = <STRING> { s = new StringTermImpl(k.image.substring(1,k.image.length()-1)); + k = <STRING> { s = new StringTermImpl(k.image.substring(1,k.image.length()-1).replaceAll("\\\\n", "\n")); s.setSrcInfo(new SourceInfo(asSource,k.beginLine)); return s; } Modified: trunk/src/jason/asSyntax/parser/as2j.java =================================================================== --- trunk/src/jason/asSyntax/parser/as2j.java 2011-06-24 13:39:37 UTC (rev 1651) +++ trunk/src/jason/asSyntax/parser/as2j.java 2011-07-18 10:09:53 UTC (rev 1652) @@ -1446,7 +1446,7 @@ final public StringTerm string() throws ParseException { Token k; StringTermImpl s; k = jj_consume_token(STRING); - s = new StringTermImpl(k.image.substring(1,k.image.length()-1)); + s = new StringTermImpl(k.image.substring(1,k.image.length()-1).replaceAll("\\\\n", "\n")); s.setSrcInfo(new SourceInfo(asSource,k.beginLine)); {if (true) return s;} throw new Error("Missing return statement in function"); Modified: trunk/src/jason/stdlib/desire.java =================================================================== --- trunk/src/jason/stdlib/desire.java 2011-06-24 13:39:37 UTC (rev 1651) +++ trunk/src/jason/stdlib/desire.java 2011-07-18 10:09:53 UTC (rev 1652) @@ -35,6 +35,8 @@ import jason.asSyntax.Trigger.TEOperator; import jason.asSyntax.Trigger.TEType; +import java.util.Iterator; + /** <p>Internal action: <b><code>.desire(<i>D</i>)</code></b>. @@ -68,10 +70,15 @@ @Override public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception { checkArguments(args); - return desires(ts.getC(),(Literal)args[0],un); + if (args[0].isGround()) + return desires(ts.getC(),(Literal)args[0],un); + else + return allDesires(ts.getC(),(Literal)args[0],un); } public boolean desires(Circumstance C, Literal l, Unifier un) { + return allDesires(C, l, un).hasNext(); + /* Trigger teFromL = new Trigger(TEOperator.add, TEType.achieve, l); // we need to check the selected event in this cycle (already removed from E) @@ -100,5 +107,90 @@ } return super.intends(C, l, un); // Int subset Des (see the formal definitions) + */ } + + enum Step { selEvt, evt, useIntends, end } + + protected Iterator<Unifier> allDesires(final Circumstance C, final Literal l, final Unifier un) { + final Trigger teFromL = new Trigger(TEOperator.add, TEType.achieve, l); + + return new Iterator<Unifier>() { + Step curStep = Step.selEvt; + Unifier solution = null; // the current response (which is an unifier) + Iterator<Event> evtIterator = null; + Iterator<Unifier> intendInterator = null; + + public boolean hasNext() { + if (solution == null) // the first call of hasNext should find the first response + find(); + return solution != null; + } + + public Unifier next() { + if (solution == null) find(); + Unifier b = solution; + find(); // find next response + return b; + } + public void remove() {} + + void find() { + switch (curStep) { + + case selEvt: + curStep = Step.evt; // set next step + + // we need to check the selected event in this cycle (already removed from E) + if (C.getSelectedEvent() != null) { + Trigger t = C.getSelectedEvent().getTrigger(); + Intention i = C.getSelectedEvent().getIntention(); + if (i != Intention.EmptyInt && i.size() > 0) { + t = t.clone(); + t.apply(i.peek().getUnif()); + } + solution = un.clone(); + if (solution.unifiesNoUndo(teFromL, t)) { + return; + } + } + find(); + return; + + case evt: + if (evtIterator == null) + evtIterator = C.getEvents().iterator(); + + if (evtIterator.hasNext()) { + Event ei = evtIterator.next(); + Trigger t = ei.getTrigger(); + Intention i = ei.getIntention(); + if (i != Intention.EmptyInt && i.size() > 0) { + t = t.clone(); + t.apply(i.peek().getUnif()); + } + solution = un.clone(); + if (solution.unifiesNoUndo(teFromL, t)) { + return; + } + } + curStep = Step.useIntends; // set next step + find(); + return; + + case useIntends: + if (intendInterator == null) + intendInterator = allIntentions(C,l,un); + + if (intendInterator.hasNext()) { + solution = intendInterator.next(); + return; + } + curStep = Step.end; // set next step + + } + solution = null; // nothing found + } + }; + } } Modified: trunk/src/jason/stdlib/intend.java =================================================================== --- trunk/src/jason/stdlib/intend.java 2011-06-24 13:39:37 UTC (rev 1651) +++ trunk/src/jason/stdlib/intend.java 2011-07-18 10:09:53 UTC (rev 1652) @@ -24,6 +24,8 @@ package jason.stdlib; +import java.util.Iterator; + import jason.JasonException; import jason.asSemantics.ActionExec; import jason.asSemantics.Circumstance; @@ -75,17 +77,22 @@ @Override protected void checkArguments(Term[] args) throws JasonException { super.checkArguments(args); // check number of arguments - if (!args[0].isLiteral()) - throw JasonException.createWrongArgument(this,"first argument must be a literal"); + if (!args[0].isLiteral() && !args[0].isVar()) + throw JasonException.createWrongArgument(this,"first argument must be a literal or variable"); } @Override public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception { checkArguments(args); - return intends(ts.getC(),(Literal)args[0],un); + if (args[0].isGround()) + return intends(ts.getC(),(Literal)args[0],un); + else + return allIntentions(ts.getC(),(Literal)args[0],un); } public boolean intends(Circumstance C, Literal l, Unifier un) { + return allIntentions(C, l, un).hasNext(); + /* Trigger g = new Trigger(TEOperator.add, TEType.achieve, l); // we need to check the intention in the selected event in this cycle!!! @@ -97,7 +104,7 @@ return true; } - // we need to check the slected intention in this cycle too!!! + // we need to check the selected intention in this cycle too!!! if (C.getSelectedIntention() != null) { // logger.log(Level.SEVERE,"Int: "+g+" unif "+ts.C.SI); if (C.getSelectedIntention().hasTrigger(g, un)) @@ -138,6 +145,145 @@ } return false; + */ } + + enum Step { selEvt, selInt, evt, pendEvt, pendAct, pendInt, intentions, end } + + protected Iterator<Unifier> allIntentions(final Circumstance C, final Literal l, final Unifier un) { + final Trigger g = new Trigger(TEOperator.add, TEType.achieve, l); + + return new Iterator<Unifier>() { + Step curStep = Step.selEvt; + Unifier solution = null; // the current response (which is an unifier) + Iterator<Event> evtIterator = null; + Iterator<Event> pendEvtIterator = null; + Iterator<ActionExec> pendActIterator = null; + Iterator<Intention> pendIntIterator = null; + Iterator<Intention> intInterator = null; + + public boolean hasNext() { + if (solution == null) // the first call of hasNext should find the first response + find(); + return solution != null; + } + + public Unifier next() { + if (solution == null) find(); + Unifier b = solution; + find(); // find next response + return b; + } + public void remove() {} + + void find() { + switch (curStep) { + + case selEvt: + curStep = Step.selInt; // set next step + // we need to check the intention in the selected event in this cycle!!! + // (as it was already removed from E) + if (C.getSelectedEvent() != null) { + // logger.log(Level.SEVERE,"Int: "+g+" unif "+ts.C.SE); + if (C.getSelectedEvent().getIntention() != null) { + solution = un.clone(); + if (C.getSelectedEvent().getIntention().hasTrigger(g, solution)) + return; + } + } + find(); + return; + + case selInt: + curStep = Step.evt; // set next step + // we need to check the selected intention in this cycle too!!! + if (C.getSelectedIntention() != null) { + // logger.log(Level.SEVERE,"Int: "+g+" unif "+ts.C.SI); + solution = un.clone(); + if (C.getSelectedIntention().hasTrigger(g, solution)) + return; + } + find(); + return; + + case evt: + if (evtIterator == null) + evtIterator = C.getEvents().iterator(); + + if (evtIterator.hasNext()) { + solution = un.clone(); + Event e = evtIterator.next(); + if (e.getIntention() != null && e.getIntention().hasTrigger(g, solution)) + return; + } + curStep = Step.pendEvt; // set next step + find(); + return; + + case pendEvt: + if (pendEvtIterator == null) + pendEvtIterator = C.getPendingEvents().values().iterator(); + + if (pendEvtIterator.hasNext()) { + solution = un.clone(); + Event e = pendEvtIterator.next(); + if (e.getIntention() != null && e.getIntention().hasTrigger(g, solution)) + return; + } + curStep = Step.pendAct; // set next step + find(); + return; + + case pendAct: + // intention may be suspended in PA! (in the new semantics) + if (C.hasPendingAction()) { + if (pendActIterator == null) + pendActIterator = C.getPendingActions().values().iterator(); + + if (pendActIterator.hasNext()) { + solution = un.clone(); + ActionExec ac = pendActIterator.next(); + if (ac.getIntention().hasTrigger(g, solution)) + return; + } + } + curStep = Step.pendInt; // set next step + find(); + return; + + case pendInt: + // intention may be suspended in PI! (in the new semantics) + if (C.hasPendingIntention()) { + if (pendIntIterator == null) + pendIntIterator = C.getPendingIntentions().values().iterator(); + + if (pendIntIterator.hasNext()) { + solution = un.clone(); + Intention i = pendIntIterator.next(); + if (i.hasTrigger(g, solution)) + return; + } + } + curStep = Step.intentions; // set next step + find(); + return; + + case intentions: + if (intInterator == null) + intInterator = C.getIntentions().iterator(); + + if (intInterator.hasNext()) { + solution = un.clone(); + Intention i = intInterator.next(); + if (i.hasTrigger(g, solution)) + return; + } + curStep = Step.end; // set next step + + } + solution = null; // nothing found + } + }; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |