From: <jom...@us...> - 2008-09-10 11:52:54
|
Revision: 1364 http://jason.svn.sourceforge.net/jason/?rev=1364&view=rev Author: jomifred Date: 2008-09-10 11:52:51 +0000 (Wed, 10 Sep 2008) Log Message: ----------- rename SuspendIA to ConcurrentIA wake up the agent in timeout operations (.wait, .send ask, .at, ...) Modified Paths: -------------- trunk/demos/gui/gui1/gui/yes_no.java trunk/demos/gui/gui1/sample.asl trunk/src/jason/asSemantics/Agent.java trunk/src/jason/stdlib/at.java trunk/src/jason/stdlib/send.java trunk/src/jason/stdlib/wait.java Added Paths: ----------- trunk/src/jason/asSemantics/ConcurrentInternalAction.java Removed Paths: ------------- trunk/src/jason/asSemantics/SuspendInternalAction.java Modified: trunk/demos/gui/gui1/gui/yes_no.java =================================================================== --- trunk/demos/gui/gui1/gui/yes_no.java 2008-09-10 09:42:28 UTC (rev 1363) +++ trunk/demos/gui/gui1/gui/yes_no.java 2008-09-10 11:52:51 UTC (rev 1364) @@ -6,7 +6,7 @@ import javax.swing.JOptionPane; -public class yes_no extends SuspendInternalAction { +public class yes_no extends ConcurrentInternalAction { private Logger logger = Logger.getLogger("gui."+yes_no.class.getName()); @@ -14,20 +14,20 @@ public Object execute(final TransitionSystem ts, Unifier un, final Term[] args) throws Exception { try { // suspend the intention (max 5 seconds) - final String key = suspend(ts, "gui", 5000); + final String key = suspendInt(ts, "gui", 5000); // to not block the agent thread, - // create a new thread that show the GUI and resume the intention latter - new Thread() { + // create a new task that show the GUI and resume the intention latter + startInternalAction(ts, new Runnable() { public void run() { int answer = JOptionPane.showConfirmDialog(null, args[0].toString()); - // resume the intention with success + if (answer == JOptionPane.YES_OPTION) - yes_no.this.resume(ts, key); + resumeInt(ts, key); // resume the intention with success else - fail(ts, key); + failInt(ts, key); // resume the intention with fail } - }.start(); + }); return true; } catch (Exception e) { @@ -44,6 +44,6 @@ //resume(ts,intentionKey); // 2: fail - fail(ts, intentionKey); + failInt(ts, intentionKey); } } Modified: trunk/demos/gui/gui1/sample.asl =================================================================== --- trunk/demos/gui/gui1/sample.asl 2008-09-10 09:42:28 UTC (rev 1363) +++ trunk/demos/gui/gui1/sample.asl 2008-09-10 11:52:51 UTC (rev 1364) @@ -1,7 +1,7 @@ /* Initial goals */ !start. -!print. +!print(system.time). /* Plans */ @@ -14,6 +14,6 @@ +!start <- gui.yes_no("Is it Ok?"); .print(ok). -!start <- .print(nok). -+!print <- .wait(500); .print("."); !!print. ++!print(S) <- .wait(500); .print(". ", system.time-S); !!print(system.time). Modified: trunk/src/jason/asSemantics/Agent.java =================================================================== --- trunk/src/jason/asSemantics/Agent.java 2008-09-10 09:42:28 UTC (rev 1363) +++ trunk/src/jason/asSemantics/Agent.java 2008-09-10 11:52:51 UTC (rev 1364) @@ -206,7 +206,7 @@ public ScheduledExecutorService getScheduler() { if (scheduler == null) - scheduler = Executors.newScheduledThreadPool(1); + scheduler = Executors.newScheduledThreadPool(2); return scheduler; } Copied: trunk/src/jason/asSemantics/ConcurrentInternalAction.java (from rev 1363, trunk/src/jason/asSemantics/SuspendInternalAction.java) =================================================================== --- trunk/src/jason/asSemantics/ConcurrentInternalAction.java (rev 0) +++ trunk/src/jason/asSemantics/ConcurrentInternalAction.java 2008-09-10 11:52:51 UTC (rev 1364) @@ -0,0 +1,102 @@ +package jason.asSemantics; + +import jason.asSyntax.InternalActionLiteral; +import jason.asSyntax.PlanBody; +import jason.asSyntax.PlanBodyImpl; +import jason.asSyntax.Term; +import jason.asSyntax.PlanBody.BodyType; + +import java.util.concurrent.TimeUnit; + +/** + + This class can be used in place of DefaultInternalAction to create an IA that + suspend the intention while it is being executed. + + Example: a plan may ask something to an user and wait the answer. + If DefaultInternalAction is used for that, all the agent thread is blocked until + the answer. With SuspendInternalAction, only the intention using the IA is + suspended. See demos/gui/gui1. + + @author jomi +*/ +public abstract class ConcurrentInternalAction implements InternalAction { + + private static int actcount = 0; + + public boolean canBeUsedInContext() { + return false; + } + + public boolean suspendIntention() { + return true; + } + + public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception { + return false; + } + + /** + * Suspend the current intention, put it in the PendingIntention (PI) structure and assigns it to a key. + * + * @param ts the "engine" of the agent + * @param basekey the base key to form final key used to get the intention back from PI (e.g. "moise", "cartago", ...) + * @param timeout the max time the intention will be in PI, the value 0 means until "resume" + * @return the final key used to store the intention in PI, this key is used the resume the intention + */ + public String suspendInt(final TransitionSystem ts, String basekey, int timeout) { + final String key = basekey + "/" + (actcount++); + final Circumstance C = ts.getC(); + Intention i = C.getSelectedIntention(); + i.setSuspended(true); + C.getPendingIntentions().put(key, i); + + if (timeout > 0) { + // schedule a future test of the end of the action + ts.getAg().getScheduler().schedule( new Runnable() { + public void run() { + // finish the IA by timeout + if (C.getPendingIntentions().get(key) != null) { // test if the intention is still there + timeout(ts,key); + } + } + }, timeout, TimeUnit.MILLISECONDS); + } + return key; + } + + public void startInternalAction(TransitionSystem ts, Runnable code) { + ts.getAg().getScheduler().execute(code); + //new Thread(code).start(); + } + + /** called back when some intention should be resumed/failed by timeout */ + abstract public void timeout(TransitionSystem ts, String intentionKey); + + /** resume the intention identified by intentionKey */ + public void resumeInt(TransitionSystem ts, String intentionKey) { + resume(ts, intentionKey, false); + } + + /** fails the intention identified by intentionKey */ + public void failInt(TransitionSystem ts, String intentionKey) { + resume(ts, intentionKey, true); + } + + synchronized private void resume(TransitionSystem ts, String intentionKey, boolean abort) { + Circumstance C = ts.getC(); + Intention pi = C.getPendingIntentions().remove(intentionKey); + if (pi != null) { + pi.setSuspended(false); + C.addIntention(pi); // add it back in I + pi.peek().removeCurrentStep(); // remove the internal action that put the intention in suspend + + if (abort) { + // fail the IA + PlanBody pbody = pi.peek().getPlan().getBody(); + pbody.add(0, new PlanBodyImpl(BodyType.internalAction, new InternalActionLiteral(".fail"))); + } + ts.getUserAgArch().getArchInfraTier().wake(); + } + } +} Deleted: trunk/src/jason/asSemantics/SuspendInternalAction.java =================================================================== --- trunk/src/jason/asSemantics/SuspendInternalAction.java 2008-09-10 09:42:28 UTC (rev 1363) +++ trunk/src/jason/asSemantics/SuspendInternalAction.java 2008-09-10 11:52:51 UTC (rev 1364) @@ -1,97 +0,0 @@ -package jason.asSemantics; - -import jason.asSyntax.InternalActionLiteral; -import jason.asSyntax.PlanBody; -import jason.asSyntax.PlanBodyImpl; -import jason.asSyntax.Term; -import jason.asSyntax.PlanBody.BodyType; - -import java.util.concurrent.TimeUnit; - -/** - - This class can be used in place of DefaultInternalAction to create an IA that - suspend the intention while it is being executed. - - Example: a plan may ask something to an user and wait the answer. - If DefaultInternalAction is used for that, all the agent thread is blocked until - the answer. With SuspendInternalAction, only the intention using the IA is - suspended. See demos/gui/gui1. - - @author jomi -*/ -public abstract class SuspendInternalAction implements InternalAction { - - private static int actcount = 0; - - public boolean canBeUsedInContext() { - return false; - } - - public boolean suspendIntention() { - return true; - } - - public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception { - return false; - } - - /** - * Suspend the current intention, put it in the PendingIntention (PI) structure and assigns it to a key. - * - * @param ts the "engine" of the agent - * @param basekey the base key to form final key used to get the intention back from PI (e.g. "moise", "cartago", ...) - * @param timeout the max time the intention will be in PI, the value 0 means until "resume" - * @return the final key used to store the intention in PI - */ - public String suspend(final TransitionSystem ts, String basekey, int timeout) { - final String key = basekey + "/" + (actcount++); - final Circumstance C = ts.getC(); - Intention i = C.getSelectedIntention(); - i.setSuspended(true); - C.getPendingIntentions().put(key, i); - - if (timeout > 0) { - // schedule a future test of the end of the action - ts.getAg().getScheduler().schedule( new Runnable() { - public void run() { - // finish the IA by timeout - if (C.getPendingIntentions().get(key) != null) { // test if the intention is still there - timeout(ts,key); - } - } - }, timeout, TimeUnit.MILLISECONDS); - } - return key; - } - - /** called back when some intention should be resumed/failed by timeout */ - abstract public void timeout(TransitionSystem ts, String intentionKey); - - /** resume the intention identified by intentionKey */ - public void resume(TransitionSystem ts, String intentionKey) { - resume(ts, intentionKey, false); - } - - /** fails the intention identified by intentionKey */ - public void fail(TransitionSystem ts, String intentionKey) { - resume(ts, intentionKey, true); - } - - synchronized private void resume(TransitionSystem ts, String intentionKey, boolean abort) { - Circumstance C = ts.getC(); - Intention pi = C.getPendingIntentions().remove(intentionKey); - if (pi != null) { - pi.setSuspended(false); - C.addIntention(pi); // add it back in I - pi.peek().removeCurrentStep(); // remove the internal action that put the intention in suspend - - if (abort) { - // fail the IA - PlanBody pbody = pi.peek().getPlan().getBody(); - pbody.add(0, new PlanBodyImpl(BodyType.internalAction, new InternalActionLiteral(".fail"))); - } - ts.getUserAgArch().getArchInfraTier().wake(); - } - } -} Modified: trunk/src/jason/stdlib/at.java =================================================================== --- trunk/src/jason/stdlib/at.java 2008-09-10 09:42:28 UTC (rev 1363) +++ trunk/src/jason/stdlib/at.java 2008-09-10 11:52:51 UTC (rev 1364) @@ -25,7 +25,6 @@ package jason.stdlib; import jason.JasonException; -import jason.asSemantics.Circumstance; import jason.asSemantics.DefaultInternalAction; import jason.asSemantics.Event; import jason.asSemantics.Intention; @@ -121,7 +120,7 @@ Trigger te = Trigger.parseTrigger(sevent.getString()); - ts.getAg().getScheduler().schedule(new CheckDeadline(te, ts.getC()), deadline, TimeUnit.MILLISECONDS); + ts.getAg().getScheduler().schedule(new CheckDeadline(te, ts), deadline, TimeUnit.MILLISECONDS); return true; } catch (ArrayIndexOutOfBoundsException e) { throw new JasonException("The internal action 'at' has not received two arguments."); @@ -137,17 +136,16 @@ } class CheckDeadline implements Runnable { - private int id = 0; private Event event; - private Circumstance c; + private TransitionSystem ts; private boolean cancelled = false; - public CheckDeadline(Trigger te, Circumstance c) { + public CheckDeadline(Trigger te, TransitionSystem ts) { idCount++; this.id = idCount; this.event = new Event(te, Intention.EmptyInt); - this.c = c; + this.ts = ts; ats.put(id, this); } @@ -157,8 +155,10 @@ public void run() { try { - if (!cancelled) - c.addEvent(event); + if (!cancelled) { + ts.getC().addEvent(event); + ts.getUserAgArch().getArchInfraTier().wake(); + } } finally { ats.remove(id); } Modified: trunk/src/jason/stdlib/send.java =================================================================== --- trunk/src/jason/stdlib/send.java 2008-09-10 09:42:28 UTC (rev 1363) +++ trunk/src/jason/stdlib/send.java 2008-09-10 11:52:51 UTC (rev 1364) @@ -204,7 +204,7 @@ intention.peek().getUnif().unifies(send.getTerm(3), new Atom("timeout")); // add the intention back in C.I ts.getC().addIntention(intention); - System.out.println("OK"); + ts.getUserAgArch().getArchInfraTier().wake(); } } }, (long)((NumberTerm)tto).solve(), TimeUnit.MILLISECONDS); Modified: trunk/src/jason/stdlib/wait.java =================================================================== --- trunk/src/jason/stdlib/wait.java 2008-09-10 09:42:28 UTC (rev 1363) +++ trunk/src/jason/stdlib/wait.java 2008-09-10 11:52:51 UTC (rev 1364) @@ -191,6 +191,7 @@ } else { c.addIntention(si); } + ts.getUserAgArch().getArchInfraTier().wake(); } } catch (Exception e) { ts.getLogger().log(Level.SEVERE, "Error at .wait thread", e); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |