From: <jom...@us...> - 2016-03-13 15:27:55
|
Revision: 1878 http://sourceforge.net/p/jason/svn/1878 Author: jomifred Date: 2016-03-13 15:27:52 +0000 (Sun, 13 Mar 2016) Log Message: ----------- initial implementation of internal actions .fork and .join Modified Paths: -------------- trunk/src/jason/asSemantics/Circumstance.java trunk/src/jason/asSemantics/Intention.java trunk/src/jason/stdlib/drop_intention.java trunk/src/jason/stdlib/succeed_goal.java trunk/src/jason/stdlib/wait.java Added Paths: ----------- trunk/src/jason/stdlib/fork.java trunk/src/jason/stdlib/join.java Modified: trunk/src/jason/asSemantics/Circumstance.java =================================================================== --- trunk/src/jason/asSemantics/Circumstance.java 2016-03-13 11:31:38 UTC (rev 1877) +++ trunk/src/jason/asSemantics/Circumstance.java 2016-03-13 15:27:52 UTC (rev 1878) @@ -332,6 +332,7 @@ el.intentionResumed(intention); } + /** remove intention from set I */ public boolean removeIntention(Intention i) { if (i == AI) { setAtomicIntention(null); Modified: trunk/src/jason/asSemantics/Intention.java =================================================================== --- trunk/src/jason/asSemantics/Intention.java 2016-03-13 11:31:38 UTC (rev 1877) +++ trunk/src/jason/asSemantics/Intention.java 2016-03-13 15:27:52 UTC (rev 1878) @@ -54,12 +54,12 @@ private static AtomicInteger idCount = new AtomicInteger(0); private int id; - private int atomicCount = 0; // how many atomic intended means there are in the intention + private int atomicCount = 0; // number of atomic intended means in the intention private boolean isSuspended = false; private Deque<IntendedMeans> intendedMeans = new ArrayDeque<IntendedMeans>(); - //private Trigger initialTrigger = null; // just for adicional information/debug (not really necessary) + //private Trigger initialTrigger = null; // just for additional information/debug (not really necessary) //static private Logger logger = Logger.getLogger(Intention.class.getName()); @@ -118,6 +118,10 @@ public int size() { return intendedMeans.size(); } + + public void clearIM() { + intendedMeans.clear(); + } public void setSuspended(boolean b) { isSuspended = b; @@ -192,8 +196,14 @@ for (IntendedMeans im: intendedMeans) { i.intendedMeans.add((IntendedMeans)im.clone()); } - return i; + return i; } + + // used by fork + public void copyTo(Intention i) { + i.atomicCount = atomicCount; + i.intendedMeans = new ArrayDeque<IntendedMeans>(intendedMeans); + } public String toString() { StringBuilder s = new StringBuilder("intention "+id+": \n"); Modified: trunk/src/jason/stdlib/drop_intention.java =================================================================== --- trunk/src/jason/stdlib/drop_intention.java 2016-03-13 11:31:38 UTC (rev 1877) +++ trunk/src/jason/stdlib/drop_intention.java 2016-03-13 15:27:52 UTC (rev 1878) @@ -136,4 +136,31 @@ } } } + + public static void dropInt(Circumstance C, Intention del) { + + // intention may be suspended in E or PE + Iterator<Event> ie = C.getEventsPlusAtomic(); + while (ie.hasNext()) { + Event e = ie.next(); + Intention i = e.getIntention(); + if (i != null && i.equals(del)) { + C.removeEvent(e); + } + } + for (String k: C.getPendingEvents().keySet()) { + Intention i = C.getPendingEvents().get(k).getIntention(); + if (i != null && i.equals(del)) { + C.removePendingEvent(k); + } + } + + // intention may be suspended in PA! (in the new semantics) + C.dropPendingAction(del); + C.dropIntention(del); + + // intention may be suspended in PI! (in the new semantics) + C.dropPendingIntention(del); + } + } Added: trunk/src/jason/stdlib/fork.java =================================================================== --- trunk/src/jason/stdlib/fork.java (rev 0) +++ trunk/src/jason/stdlib/fork.java 2016-03-13 15:27:52 UTC (rev 1878) @@ -0,0 +1,184 @@ +//---------------------------------------------------------------------------- +// Copyright (C) 2003 Rafael H. Bordini, Jomi F. Hubner, et al. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// To contact the authors: +// http://www.inf.ufrgs.br/~bordini +// http://www.das.ufsc.br/~jomi +// +//---------------------------------------------------------------------------- + +package jason.stdlib; + +import jason.JasonException; +import jason.asSemantics.DefaultInternalAction; +import jason.asSemantics.IntendedMeans; +import jason.asSemantics.Intention; +import jason.asSemantics.InternalAction; +import jason.asSemantics.TransitionSystem; +import jason.asSemantics.Unifier; +import jason.asSyntax.Atom; +import jason.asSyntax.InternalActionLiteral; +import jason.asSyntax.Literal; +import jason.asSyntax.ObjectTermImpl; +import jason.asSyntax.PlanBody; +import jason.asSyntax.PlanBody.BodyType; +import jason.asSyntax.PlanBodyImpl; +import jason.asSyntax.Structure; +import jason.asSyntax.Term; +import jason.asSyntax.Trigger; + +import java.util.HashSet; +import java.util.Set; + +/** +Implementation of <b>.fort</b> (used for |& and || operators). + +<p>Syntax: +<pre> + <i>plan_body1</i> "|&" | "||" <i>plan_body2</i> .... +</pre> +</p> + +|& is concurrent and: both plan_body1 and plan_body2 have to finishes successfully +|| is concurrent or : either plan_body1 or plan_body2 have to finishes successfully + + +<p>Example: +<pre> +</pre> +</p> + +*/ +public class fork extends DefaultInternalAction { + + private static InternalAction singleton = null; + public static InternalAction create() { + if (singleton == null) + singleton = new fork(); + return singleton; + } + + @Override public Term[] prepareArguments(Literal body, Unifier un) { + return body.getTermsArray(); + } + + @Override public int getMinArgs() { return 2; } + + @Override protected void checkArguments(Term[] args) throws JasonException { + super.checkArguments(args); // check number of arguments + if ( !(args[0] instanceof Atom)) + throw JasonException.createWrongArgument(this,"first argument must be 'and' or 'or'."); + } + + @Override public boolean suspendIntention() { return true; } + + @Override public boolean canBeUsedInContext() { return false; } + + private static final Structure joinS = new Structure(".join"); + + @Override + public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception { + checkArguments(args); + + ForkData fd = new ForkData( ((Atom)args[0]).getFunctor().equals("and")) ; + + Intention currentInt = ts.getC().getSelectedIntention(); + for (int iPlans = 1; iPlans < args.length; iPlans++) { + Intention i = new ForkIntention(currentInt, fd); + fd.addIntention(i); + i.pop(); // remove the top IM, it will be introduced back later (modified) + IntendedMeans im = (IntendedMeans)currentInt.peek().clone(); + + // adds the .join in the plan + InternalActionLiteral joinL = new InternalActionLiteral(joinS, ts.getAg()); + joinL.addTerm(new ObjectTermImpl(fd)); + PlanBody joinPB = new PlanBodyImpl(BodyType.internalAction, joinL); + joinPB.setBodyNext(im.getCurrentStep().getBodyNext()); + + // adds the argument in the plan (before join) + PlanBody whattoadd = (PlanBody)args[iPlans].clone(); + whattoadd.add(joinPB); + whattoadd.setAsBodyTerm(false); + im.insertAsNextStep(whattoadd); + im.removeCurrentStep(); // remove the .fork + i.push(im); + ts.getC().addIntention(i); + } + + + return true; + } + + class ForkData { + boolean isAnd = true; + Set<Intention> intentions = new HashSet<Intention>(); + int toFinish = 0; + + public ForkData(boolean isAnd) { + this.isAnd = isAnd; + } + + public void addIntention(Intention i) { + intentions.add(i); + toFinish++; + } + + @Override + public String toString() { + StringBuilder s = new StringBuilder("fork data"); + if (isAnd) + s.append(" (and) "); + else + s.append(" (or) "); + s.append(" intentions = { "); + for (Intention i: intentions) { + s.append(" "+i.getId()); + } + s.append(" } waiting for "+toFinish); + return s.toString(); + } + } + + class ForkIntention extends Intention { + ForkData fd; + int forkPoint; + + ForkIntention(Intention i, ForkData fd) { + i.copyTo(this); + forkPoint = i.size(); + this.fd = fd; + } + + @Override + public boolean dropGoal(Trigger te, Unifier un) { + boolean r = super.dropGoal(te, un); + if (r && size() < forkPoint) { + //System.out.println("drop "+te+" i.size = "+size()+" fork point "+forkPoint+" to f "+fd+"\n"+this); + if (fd.toFinish > 0) { // the first intentions of the fork being dropped, keep it and ignore the rest + fd.toFinish = 0; + //System.out.println("put it back"); + return true; + } else { + clearIM(); + //System.out.println("ignore intention"); + return false; + } + } + return r; + } + } +} Added: trunk/src/jason/stdlib/join.java =================================================================== --- trunk/src/jason/stdlib/join.java (rev 0) +++ trunk/src/jason/stdlib/join.java 2016-03-13 15:27:52 UTC (rev 1878) @@ -0,0 +1,87 @@ +//---------------------------------------------------------------------------- +// Copyright (C) 2003 Rafael H. Bordini, Jomi F. Hubner, et al. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// To contact the authors: +// http://www.inf.ufrgs.br/~bordini +// http://www.das.ufsc.br/~jomi +// +//---------------------------------------------------------------------------- + +package jason.stdlib; + +import jason.JasonException; +import jason.asSemantics.DefaultInternalAction; +import jason.asSemantics.Intention; +import jason.asSemantics.InternalAction; +import jason.asSemantics.TransitionSystem; +import jason.asSemantics.Unifier; +import jason.asSyntax.Literal; +import jason.asSyntax.ObjectTerm; +import jason.asSyntax.Term; +import jason.stdlib.fork.ForkData; + +/** injected by .fork */ +public class join extends DefaultInternalAction { + + private static InternalAction singleton = null; + public static InternalAction create() { + if (singleton == null) + singleton = new join(); + return singleton; + } + + @Override public Term[] prepareArguments(Literal body, Unifier un) { + return body.getTermsArray(); + } + + @Override protected void checkArguments(Term[] args) throws JasonException { + } + + @Override public boolean suspendIntention() { return true; } + @Override public boolean canBeUsedInContext() { return false; } + + @Override + public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception { + checkArguments(args); + + Intention currentInt = ts.getC().getSelectedIntention(); + ForkData fd = (ForkData) ((ObjectTerm)args[0]).getObject(); + fd.toFinish--; + //System.out.println("** in join for "+currentInt.getId()+ " with "+fd); + + // in the case of fork and, all intentions should be finished to continue + if (fd.isAnd) { + if (fd.toFinish == 0) { + //System.out.println("join finished!"); + currentInt.peek().removeCurrentStep(); + ts.getC().addIntention(currentInt); + } + } else { + // the first intention has finished, drop others + fd.intentions.remove(currentInt); + for (Intention i: fd.intentions) { + //System.out.println("drop "+i.getId()); + drop_intention.dropInt(ts.getC(), i); + } + currentInt.peek().removeCurrentStep(); + ts.getC().addIntention(currentInt); + } + + return true; + } + +} Modified: trunk/src/jason/stdlib/succeed_goal.java =================================================================== --- trunk/src/jason/stdlib/succeed_goal.java 2016-03-13 11:31:38 UTC (rev 1877) +++ trunk/src/jason/stdlib/succeed_goal.java 2016-03-13 15:27:52 UTC (rev 1878) @@ -129,7 +129,7 @@ } else { // test in the event Trigger t = e.getTrigger(); - if (i != Intention.EmptyInt && !i.isFinished()) { //i.size() > 0) { + if (i != Intention.EmptyInt && !i.isFinished()) { t = t.capply(i.peek().getUnif()); } if (un.unifies(g, t)) { Modified: trunk/src/jason/stdlib/wait.java =================================================================== --- trunk/src/jason/stdlib/wait.java 2016-03-13 11:31:38 UTC (rev 1877) +++ trunk/src/jason/stdlib/wait.java 2016-03-13 15:27:52 UTC (rev 1878) @@ -189,7 +189,7 @@ } else { ts.generateGoalDeletion(si, JasonException.createBasicErrorAnnots("wait_timeout", "timeout in .wait")); } - } else { + } else if (! si.isFinished()) { si.peek().removeCurrentStep(); if (elapsedTimeTerm != null) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |