From: <jom...@us...> - 2016-03-13 20:16:56
|
Revision: 1879 http://sourceforge.net/p/jason/svn/1879 Author: jomifred Date: 2016-03-13 20:16:54 +0000 (Sun, 13 Mar 2016) Log Message: ----------- handle fail_goal with fork/join Modified Paths: -------------- trunk/src/jason/asSemantics/Intention.java trunk/src/jason/asSemantics/TransitionSystem.java trunk/src/jason/asSyntax/PlanLibrary.java trunk/src/jason/stdlib/fail_goal.java trunk/src/jason/stdlib/fork.java trunk/src/jason/util/Pair.java Modified: trunk/src/jason/asSemantics/Intention.java =================================================================== --- trunk/src/jason/asSemantics/Intention.java 2016-03-13 15:27:52 UTC (rev 1878) +++ trunk/src/jason/asSemantics/Intention.java 2016-03-13 20:16:54 UTC (rev 1879) @@ -26,9 +26,12 @@ import jason.asSyntax.ListTerm; import jason.asSyntax.ListTermImpl; import jason.asSyntax.NumberTermImpl; +import jason.asSyntax.PlanLibrary; import jason.asSyntax.Structure; import jason.asSyntax.Term; import jason.asSyntax.Trigger; +import jason.asSyntax.Trigger.TEOperator; +import jason.util.Pair; import java.io.Serializable; import java.util.ArrayDeque; @@ -106,7 +109,6 @@ atomicCount = a; } - public Iterator<IntendedMeans> iterator() { return intendedMeans.iterator(); } @@ -167,6 +169,29 @@ return false; } + public void fail(Circumstance c) { + } + + public Pair<Event, Integer> findEventForFailure(Trigger tevent, PlanLibrary pl, Circumstance c) { + Trigger failTrigger = new Trigger(TEOperator.del, tevent.getType(), tevent.getLiteral()); + Iterator<IntendedMeans> ii = iterator(); + int posInStak = size(); + while (!pl.hasCandidatePlan(failTrigger) && ii.hasNext()) { + // TODO: pop IM until +!g or *!g (this TODO is valid only if meta events are pushed on top of the intention) + // If *!g is found first, no failure event + // - while popping, if some meta event (* > !) is in the stack, stop and simple pop instead of producing an failure event + IntendedMeans im = ii.next(); + tevent = im.getTrigger(); + failTrigger = new Trigger(TEOperator.del, tevent.getType(), tevent.getLiteral()); + posInStak--; + } + if (tevent.isGoal() && tevent.isAddition() && pl.hasCandidatePlan(failTrigger)) + return new Pair<Event, Integer>(new Event(failTrigger.clone(), this), posInStak); + else + return new Pair<Event, Integer>(null, 0); + } + + /** implements atomic intentions > not atomic intentions */ public int compareTo(Intention o) { if (o.atomicCount > this.atomicCount) return 1; Modified: trunk/src/jason/asSemantics/TransitionSystem.java =================================================================== --- trunk/src/jason/asSemantics/TransitionSystem.java 2016-03-13 15:27:52 UTC (rev 1878) +++ trunk/src/jason/asSemantics/TransitionSystem.java 2016-03-13 20:16:54 UTC (rev 1879) @@ -932,7 +932,7 @@ } // if has finished a failure handling IM ... - if (im.getTrigger().isGoal() && !im.getTrigger().isAddition() && !i.isFinished()) {//i.size() > 0) { + if (im.getTrigger().isGoal() && !im.getTrigger().isAddition() && !i.isFinished()) { // needs to get rid of the IM until a goal that // has failure handling. E.g, // -!b @@ -1116,6 +1116,7 @@ if (logger.isLoggable(Level.FINE)) logger.fine("Generating goal deletion " + failEvent.getTrigger() + " from goal: " + im.getTrigger()); } else { logger.warning("No failure event was generated for " + failEvent.getTrigger()); + i.fail(getC()); } } // if "discard" is set, we are deleting the whole intention! @@ -1156,6 +1157,8 @@ //logger.warning("Generating goal deletion " + failEvent.getTrigger() + " from event: " + ev.getTrigger()); } else { logger.warning("No fail event was generated for " + ev.getTrigger()); + if (ev.intention != null) + ev.intention.fail(getC()); } } else if (ev.isInternal()) { logger.warning("Could not finish intention:\n" + ev.intention); @@ -1171,22 +1174,13 @@ } public Event findEventForFailure(Intention i, Trigger tevent) { - Trigger failTrigger = new Trigger(TEOperator.del, tevent.getType(), tevent.getLiteral()); if (i != Intention.EmptyInt) { - Iterator<IntendedMeans> ii = i.iterator(); - while (!getAg().getPL().hasCandidatePlan(failTrigger) && ii.hasNext()) { - // TODO: pop IM until +!g or *!g (this TODO is valid only if meta events are pushed on top of the intention) - // If *!g is found first, no failure event - // - while popping, if some meta event (* > !) is in the stack, stop and simple pop instead of producing an failure event - IntendedMeans im = ii.next(); - tevent = im.getTrigger(); - failTrigger = new Trigger(TEOperator.del, tevent.getType(), tevent.getLiteral()); - } + return i.findEventForFailure(tevent, getAg().getPL(), getC()).getFirst(); + } else if (tevent.isGoal() && tevent.isAddition()) { + Trigger failTrigger = new Trigger(TEOperator.del, tevent.getType(), tevent.getLiteral()); + if (getAg().getPL().hasCandidatePlan(failTrigger)) + return new Event(failTrigger.clone(), i); } - // if some failure handling plan is found - if (tevent.isGoal() && tevent.isAddition() && getAg().getPL().hasCandidatePlan(failTrigger)) { - return new Event(failTrigger.clone(), i); - } return null; } Modified: trunk/src/jason/asSyntax/PlanLibrary.java =================================================================== --- trunk/src/jason/asSyntax/PlanLibrary.java 2016-03-13 15:27:52 UTC (rev 1878) +++ trunk/src/jason/asSyntax/PlanLibrary.java 2016-03-13 20:16:54 UTC (rev 1879) @@ -344,7 +344,10 @@ } public boolean hasCandidatePlan(Trigger te) { - return getCandidatePlans(te) != null; + if (te == null) + return false; + else + return getCandidatePlans(te) != null; } Modified: trunk/src/jason/stdlib/fail_goal.java =================================================================== --- trunk/src/jason/stdlib/fail_goal.java 2016-03-13 15:27:52 UTC (rev 1878) +++ trunk/src/jason/stdlib/fail_goal.java 2016-03-13 20:16:54 UTC (rev 1879) @@ -26,10 +26,10 @@ import jason.JasonException; import jason.asSemantics.Event; import jason.asSemantics.GoalListener; +import jason.asSemantics.GoalListener.FinishStates; import jason.asSemantics.Intention; import jason.asSemantics.TransitionSystem; import jason.asSemantics.Unifier; -import jason.asSemantics.GoalListener.FinishStates; import jason.asSyntax.Literal; import jason.asSyntax.Term; import jason.asSyntax.Trigger; @@ -115,6 +115,8 @@ if (ts.hasGoalListener()) for (GoalListener gl: ts.getGoalListeners()) gl.goalFinished(g, FinishStates.unachieved); + + i.fail(ts.getC()); return 3; } } Modified: trunk/src/jason/stdlib/fork.java =================================================================== --- trunk/src/jason/stdlib/fork.java 2016-03-13 15:27:52 UTC (rev 1878) +++ trunk/src/jason/stdlib/fork.java 2016-03-13 20:16:54 UTC (rev 1879) @@ -24,7 +24,9 @@ package jason.stdlib; import jason.JasonException; +import jason.asSemantics.Circumstance; import jason.asSemantics.DefaultInternalAction; +import jason.asSemantics.Event; import jason.asSemantics.IntendedMeans; import jason.asSemantics.Intention; import jason.asSemantics.InternalAction; @@ -37,9 +39,11 @@ import jason.asSyntax.PlanBody; import jason.asSyntax.PlanBody.BodyType; import jason.asSyntax.PlanBodyImpl; +import jason.asSyntax.PlanLibrary; import jason.asSyntax.Structure; import jason.asSyntax.Term; import jason.asSyntax.Trigger; +import jason.util.Pair; import java.util.HashSet; import java.util.Set; @@ -180,5 +184,32 @@ } return r; } + + @Override + public void fail(Circumstance c) { + if (size() >= forkPoint && fd.isAnd) { // the fail is above fork, is an fork and, remove the others + for (Intention ifo: fd.intentions) { + drop_intention.dropInt(c, ifo); + } + } + } + + @Override + public Pair<Event, Integer> findEventForFailure(Trigger tevent, PlanLibrary pl, Circumstance c) { + Pair<Event, Integer> p = super.findEventForFailure(tevent, pl, c); + if (p.getSecond() <= forkPoint) { + if (fd.isAnd) { + //System.out.println("*** remove other forks"); + fd.intentions.remove(this); + for (Intention ifo: fd.intentions) { + drop_intention.dropInt(c, ifo); + } + } else { + //System.out.println("*** case or, do not search for fail plan below fork point"); + return new Pair<Event, Integer>(null, p.getSecond()); + } + } + return p; + } } } Modified: trunk/src/jason/util/Pair.java =================================================================== --- trunk/src/jason/util/Pair.java 2016-03-13 15:27:52 UTC (rev 1878) +++ trunk/src/jason/util/Pair.java 2016-03-13 20:16:54 UTC (rev 1879) @@ -5,12 +5,14 @@ final T1 o1; final T2 o2; - int hc; + int hc = 0; public Pair(T1 o1, T2 o2) { this.o1 = o1; this.o2 = o2; - hc = (o1.hashCode() + o2.hashCode()) * 31; + if (o1 != null) hc =+ o1.hashCode(); + if (o2 != null) hc =+ o2.hashCode(); + hc = hc * 31; } public T1 getFirst() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |