|
From: <jom...@us...> - 2008-06-09 20:39:50
|
Revision: 1331
http://jason.svn.sourceforge.net/jason/?rev=1331&view=rev
Author: jomifred
Date: 2008-06-09 13:39:41 -0700 (Mon, 09 Jun 2008)
Log Message:
-----------
improve implementation of .at and .wait based on ThreadPoolScheduler
Modified Paths:
--------------
trunk/applications/jasdl-owlapi/build.xml
trunk/applications/jasdl-owlapi/examples/commerce/MAS.mas2j
trunk/applications/jasdl-owlapi/examples/commerce/bin/build.xml
trunk/applications/jasdl-owlapi/lib/owl2mas.jar
trunk/applications/owl2mas/lib/owl2mas.jar
trunk/src/jason/architecture/AgArch.java
trunk/src/jason/asSemantics/Agent.java
trunk/src/jason/asSemantics/SuspendInternalAction.java
trunk/src/jason/stdlib/at.java
trunk/src/jason/stdlib/drop_all_intentions.java
trunk/src/jason/stdlib/print.java
trunk/src/jason/stdlib/wait.java
Modified: trunk/applications/jasdl-owlapi/build.xml
===================================================================
--- trunk/applications/jasdl-owlapi/build.xml 2008-06-09 19:58:41 UTC (rev 1330)
+++ trunk/applications/jasdl-owlapi/build.xml 2008-06-09 20:39:41 UTC (rev 1331)
@@ -18,7 +18,7 @@
<!-- For conveniently building bebops, jason and jmca if present -->
- <property name="jason.dir" value="../../../jason"/>
+ <property name="jason.dir" value="../.."/>
<property name="apps.dir" value="${jason.dir}/applications"/>
<property name="bebops.dir" value="${apps.dir}/bebops"/>
<property name="bebops.jar" value="${bebops.dir}/lib/bebops.jar"/>
Modified: trunk/applications/jasdl-owlapi/examples/commerce/MAS.mas2j
===================================================================
--- trunk/applications/jasdl-owlapi/examples/commerce/MAS.mas2j 2008-06-09 19:58:41 UTC (rev 1330)
+++ trunk/applications/jasdl-owlapi/examples/commerce/MAS.mas2j 2008-06-09 20:39:41 UTC (rev 1331)
@@ -1,7 +1,7 @@
/*
Jason Project
- -- created on June 06, 2008
+ -- created on June 09, 2008
*/
MAS soc {
@@ -10,19 +10,19 @@
environment: commerce.env.CommerceEnvironment
agents:
- a_greengrocers shop.asl [jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_ontologies="c,s", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
- delivery_van_4 delivery_van.asl [jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_ontologies="c,s", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
- a_supermarket shop.asl [jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_ontologies="c,s", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
- toms_pa pa.asl [jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_ontologies="c,s", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
- a_butchers shop.asl [jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_ontologies="c,s", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
- jims_pa pa.asl [jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_ontologies="c,s", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
- caz customer.asl [jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_ontologies="c,s", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
- cazs_pa pa.asl [jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_ontologies="c,s", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
- delivery_van_1 delivery_van.asl [jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_ontologies="c,s", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
- delivery_van_3 delivery_van.asl [jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_ontologies="c,s", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
- jim customer.asl [jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_ontologies="c,s", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
- tom customer.asl [jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_ontologies="c,s", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
- delivery_van_2 delivery_van.asl [jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_ontologies="c,s", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
+ delivery_van_1 delivery_van.asl [jasdl_ontologies="c,s", jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
+ caz customer.asl [jasdl_ontologies="c,s", jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
+ a_butchers shop.asl [jasdl_ontologies="c,s", jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
+ a_greengrocers shop.asl [jasdl_ontologies="c,s", jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
+ jims_pa pa.asl [jasdl_ontologies="c,s", jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
+ tom customer.asl [jasdl_ontologies="c,s", jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
+ delivery_van_2 delivery_van.asl [jasdl_ontologies="c,s", jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
+ delivery_van_3 delivery_van.asl [jasdl_ontologies="c,s", jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
+ delivery_van_4 delivery_van.asl [jasdl_ontologies="c,s", jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
+ toms_pa pa.asl [jasdl_ontologies="c,s", jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
+ a_supermarket shop.asl [jasdl_ontologies="c,s", jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
+ cazs_pa pa.asl [jasdl_ontologies="c,s", jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
+ jim customer.asl [jasdl_ontologies="c,s", jasdl_s_uri="/onts/society.owl", jasdl_c_uri="/onts/commerce.owl", jasdl_useContraction="true"] agentArchClass jasdl.architecture.JASDLAgArch agentClass jasdl.asSemantics.JASDLAgent beliefBaseClass jasdl.bb.JASDLBeliefBase;
classpath: "../../lib/**/*.jar";
aslSourcePath: "src/asl";
Modified: trunk/applications/jasdl-owlapi/examples/commerce/bin/build.xml
===================================================================
--- trunk/applications/jasdl-owlapi/examples/commerce/bin/build.xml 2008-06-09 19:58:41 UTC (rev 1330)
+++ trunk/applications/jasdl-owlapi/examples/commerce/bin/build.xml 2008-06-09 20:39:41 UTC (rev 1331)
@@ -2,10 +2,10 @@
<!--
- This file was generated by Jason 1.0.2
+ This file was generated by Jason 1.1.2
http://jason.sf.net
- June 06, 2008 - 17:38:11
+ June 09, 2008 - 22:03:32
-->
<project name ="soc"
@@ -16,7 +16,7 @@
<property name="debug" value=""/> <!-- use "-debug" to run in debug mode -->
<property name="build.dir" value="${basedir}/bin/classes" />
- <property name="jasonJar" value="/home/tom/workspace/jason/bin/../lib/jason.jar"/>
+ <property name="jasonJar" value="/Users/jomi/Jason/Jason-svn/lib/jason.jar"/>
<path id="project.classpath">
<pathelement location="${basedir}"/>
@@ -73,10 +73,10 @@
<copy todir="${basedir}/${ant.project.name}-jws" failonerror="no">
<fileset dir="${basedir}/lib" includes="**/*.jar" />
<fileset dir="${basedir}" includes="${ant.project.name}.jar" />
- <fileset dir="/home/tom/workspace/jason/bin/../src/images" includes="Jason-GMoreau-Icon.jpg" />
+ <fileset dir="/Users/jomi/Jason/Jason-svn/src/images" includes="Jason-GMoreau-Icon.jpg" />
</copy>
<signjar jar="${basedir}/${ant.project.name}-jws/${ant.project.name}.jar" alias="jason"
- storepass="rbjhja" keypass="rbjhja" keystore="/home/tom/workspace/jason/bin/../src/jasonKeystore" />
+ storepass="rbjhja" keypass="rbjhja" keystore="/Users/jomi/Jason/Jason-svn/src/jasonKeystore" />
<echo message="**" />
<echo message="** Java Web Start application created in directory ${ant.project.name}-jws" />
<echo message="** Update the codebase (in the second line of the .jnlp file)" />
Modified: trunk/applications/jasdl-owlapi/lib/owl2mas.jar
===================================================================
(Binary files differ)
Modified: trunk/applications/owl2mas/lib/owl2mas.jar
===================================================================
(Binary files differ)
Modified: trunk/src/jason/architecture/AgArch.java
===================================================================
--- trunk/src/jason/architecture/AgArch.java 2008-06-09 19:58:41 UTC (rev 1330)
+++ trunk/src/jason/architecture/AgArch.java 2008-06-09 20:39:41 UTC (rev 1331)
@@ -31,7 +31,6 @@
import jason.bb.BeliefBase;
import jason.mas2j.ClassParameters;
import jason.runtime.Settings;
-import jason.stdlib.at;
import java.util.List;
@@ -85,17 +84,7 @@
* when the agent is about to be killed.
*/
public void stopAg() {
- ts.getAg().getBB().stop();
-
- // cancel future events generated by .at
- try {
- ((at)getTS().getAg().getIA(at.atAtom)).stopAllWaits();
- } catch (Exception e) {}
-
- // stop wait threads
- try {
- ((jason.stdlib.wait)getTS().getAg().getIA(jason.stdlib.wait.waitAtom)).stopAllWaits();
- } catch (Exception e) {}
+ ts.getAg().stopAg();
}
public void setArchInfraTier(AgArchInfraTier ai) {
Modified: trunk/src/jason/asSemantics/Agent.java
===================================================================
--- trunk/src/jason/asSemantics/Agent.java 2008-06-09 19:58:41 UTC (rev 1330)
+++ trunk/src/jason/asSemantics/Agent.java 2008-06-09 20:39:41 UTC (rev 1331)
@@ -60,6 +60,8 @@
import java.util.List;
import java.util.Map;
import java.util.Queue;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -92,6 +94,9 @@
private boolean hasCustomSelOp = true;
+ private ScheduledExecutorService scheduler = null;
+
+
protected Logger logger = Logger.getLogger(Agent.class.getName());
public Agent() {
@@ -156,6 +161,13 @@
throw new JasonException("Error creating the agent class! - " + e);
}
}
+
+ public void stopAg() {
+ bb.stop();
+
+ if (scheduler != null)
+ scheduler.shutdownNow();
+ }
/**
* Clone BB, PL, Circumstance.
@@ -184,6 +196,13 @@
public Logger getLogger() {
return logger;
}
+
+ public ScheduledExecutorService getScheduler() {
+ if (scheduler == null)
+ scheduler = Executors.newScheduledThreadPool(1);
+ return scheduler;
+ }
+
/** Returns the .asl file source used to create this agent */
public String getASLSrc() {
Modified: trunk/src/jason/asSemantics/SuspendInternalAction.java
===================================================================
--- trunk/src/jason/asSemantics/SuspendInternalAction.java 2008-06-09 19:58:41 UTC (rev 1330)
+++ trunk/src/jason/asSemantics/SuspendInternalAction.java 2008-06-09 20:39:41 UTC (rev 1331)
@@ -1,16 +1,10 @@
package jason.asSemantics;
-import jason.asSemantics.Circumstance;
-import jason.asSemantics.Intention;
-import jason.asSemantics.InternalAction;
-import jason.asSemantics.TransitionSystem;
import jason.asSyntax.InternalActionLiteral;
import jason.asSyntax.PlanBody;
import jason.asSyntax.PlanBodyImpl;
import jason.asSyntax.PlanBody.BodyType;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
@@ -27,8 +21,7 @@
*/
public abstract class SuspendInternalAction implements InternalAction {
- protected ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
- private static int actcount = 0;
+ private static int actcount = 0;
public boolean canBeUsedInContext() {
return false;
@@ -54,7 +47,7 @@
if (timeout > 0) {
// schedule a future test of the end of the action
- scheduler.schedule( new Runnable() {
+ 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
Modified: trunk/src/jason/stdlib/at.java
===================================================================
--- trunk/src/jason/stdlib/at.java 2008-06-09 19:58:41 UTC (rev 1330)
+++ trunk/src/jason/stdlib/at.java 2008-06-09 20:39:41 UTC (rev 1331)
@@ -37,6 +37,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
/**
<p>Internal action: <b><code>.at</code></b>.
@@ -74,12 +75,11 @@
public static final String atAtom = ".at";
@Override
- public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
+ public Object execute(final TransitionSystem ts, Unifier un, Term[] args) throws Exception {
try {
- StringTerm time = (StringTerm)args[0];
- StringTerm sevent = (StringTerm)args[1];
- Trigger te = Trigger.parseTrigger(sevent.getString());
- String stime = time.getString();
+ StringTerm time = (StringTerm)args[0];
+ String stime = time.getString();
+ StringTerm sevent = (StringTerm)args[1];
// parse time
long deadline = -1;
@@ -118,9 +118,10 @@
if (deadline == -1) {
throw new JasonException("The time parameter ('"+time+"') of the internal action 'at' did not parse correctly!");
}
+
+ Trigger te = Trigger.parseTrigger(sevent.getString());
- new CheckDeadline(deadline, te, ts.getC()).start();
-
+ ts.getAg().getScheduler().schedule(new CheckDeadline(te, ts.getC()), deadline, TimeUnit.MILLISECONDS);
return true;
} catch (ArrayIndexOutOfBoundsException e) {
throw new JasonException("The internal action 'at' has not received two arguments.");
@@ -128,39 +129,38 @@
}
private static int idCount = 0;
- private Map<Integer,CheckDeadline> threads = new ConcurrentHashMap<Integer,CheckDeadline>();
+ private Map<Integer,CheckDeadline> ats = new ConcurrentHashMap<Integer,CheckDeadline>();
- public void stopAllWaits() {
- for (CheckDeadline t: threads.values()) {
- t.interrupt();
- }
+ public void cancelAts() {
+ for (CheckDeadline t: ats.values())
+ t.cancel();
}
- class CheckDeadline extends Thread {
+ class CheckDeadline implements Runnable {
private int id = 0;
- private long deadline = 0;
private Event event;
private Circumstance c;
+ private boolean cancelled = false;
- public CheckDeadline(long d, Trigger te, Circumstance c) {
+ public CheckDeadline(Trigger te, Circumstance c) {
idCount++;
this.id = idCount;
- this.deadline = d;
this.event = new Event(te, Intention.EmptyInt);
this.c = c;
-
- threads.put(id, this);
+ ats.put(id, this);
}
- synchronized public void run() {
+ void cancel() {
+ cancelled = true;
+ }
+
+ public void run() {
try {
- wait(deadline);
- // add event to Circumstance.Events
- c.addEvent(event);
- } catch (InterruptedException e) {
+ if (!cancelled)
+ c.addEvent(event);
} finally {
- threads.remove(id);
+ ats.remove(id);
}
}
}
Modified: trunk/src/jason/stdlib/drop_all_intentions.java
===================================================================
--- trunk/src/jason/stdlib/drop_all_intentions.java 2008-06-09 19:58:41 UTC (rev 1330)
+++ trunk/src/jason/stdlib/drop_all_intentions.java 2008-06-09 20:39:41 UTC (rev 1331)
@@ -83,7 +83,7 @@
// cancel future events generated by .at
at atia = (at)ts.getAg().getIA(at.atAtom);
- atia.stopAllWaits();
+ atia.cancelAts();
return true;
}
Modified: trunk/src/jason/stdlib/print.java
===================================================================
--- trunk/src/jason/stdlib/print.java 2008-06-09 19:58:41 UTC (rev 1330)
+++ trunk/src/jason/stdlib/print.java 2008-06-09 20:39:41 UTC (rev 1331)
@@ -61,7 +61,7 @@
private static InternalAction singleton = null;
public static InternalAction create() {
if (singleton == null)
- singleton = new println();
+ singleton = new print();
return singleton;
}
Modified: trunk/src/jason/stdlib/wait.java
===================================================================
--- trunk/src/jason/stdlib/wait.java 2008-06-09 19:58:41 UTC (rev 1330)
+++ trunk/src/jason/stdlib/wait.java 2008-06-09 20:39:41 UTC (rev 1331)
@@ -40,9 +40,7 @@
import jason.asSyntax.Trigger;
import jason.asSyntax.PlanBody.BodyType;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
+import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
/**
@@ -99,14 +97,12 @@
public Object execute(final TransitionSystem ts, Unifier un, Term[] args) throws Exception {
long timeout = -1;
Trigger te = null;
-
- Term elapseTime = null;
+ Term elapsedTime = null;
try {
if (args[0].isNumeric()) {
// time in milliseconds
NumberTerm time = (NumberTerm)args[0];
timeout = (long) time.solve();
- // Thread.sleep((long) time.solve());
} else if (args[0].isString()) {
// wait for event
StringTerm st = (StringTerm) args[0];
@@ -116,15 +112,14 @@
if (args.length >= 2)
timeout = (long) ((NumberTerm) args[1]).solve();
if (args.length == 3)
- elapseTime = args[2];
+ elapsedTime = args[2];
}
+ new WaitEvent(te, un, ts, timeout, elapsedTime);
+ return true;
} catch (Exception e) {
ts.getLogger().log(Level.SEVERE, "Error at .wait.", e);
- return false;
}
- WaitEvent wet = new WaitEvent(te, un, ts, timeout, elapseTime);
- wet.start();
- return true;
+ return false;
}
@Override
@@ -132,38 +127,24 @@
return true;
}
- private List<WaitEvent> threads = Collections.synchronizedList(new ArrayList<WaitEvent>());
-
- public void stopAllWaits() {
- for (WaitEvent t: threads) {
- t.interrupt();
- }
- }
-
-
- class WaitEvent extends Thread implements CircumstanceListener {
- Trigger te;
- String sTE; // a string version of TE
- Unifier un;
- Intention si;
- TransitionSystem ts;
- Circumstance c;
- boolean ok = false;
- boolean drop = false;
- boolean stopByTimeout = false;
- Term elapseTimeTerm;
- long timeout = -1;
- long elapseTime;
+ class WaitEvent implements CircumstanceListener {
+ private Trigger te;
+ private String sTE; // a string version of TE
+ private Unifier un;
+ private Intention si;
+ private TransitionSystem ts;
+ private Circumstance c;
+ private boolean dropped = false;
+ private Term elapsedTimeTerm;
+ private long startTime;
- WaitEvent(Trigger te, Unifier un, TransitionSystem ts, long to, Term elapseTimeTerm) {
- super("wait "+te);
+ WaitEvent(Trigger te, Unifier un, TransitionSystem ts, long timeout, Term elapsedTimeTerm) {
this.te = te;
this.un = un;
this.ts = ts;
c = ts.getC();
si = c.getSelectedIntention();
- this.timeout = to;
- this.elapseTimeTerm = elapseTimeTerm;
+ this.elapsedTimeTerm = elapsedTimeTerm;
// register listener
c.addEventListener(this);
@@ -176,27 +157,34 @@
sTE = si.getId()+"/"+sTE;
c.getPendingIntentions().put(sTE, si);
- threads.add(this);
+ startTime = System.currentTimeMillis();
+
+ if (timeout > 0) {
+ ts.getAg().getScheduler().schedule(new Runnable() {
+ public void run() {
+ resume(true);
+ }
+ }, timeout, TimeUnit.MILLISECONDS);
+ }
}
- public void run() {
+ void resume(boolean stopByTimeout) {
try {
- waitEvent();
-
// unregister (for not to receive intentionAdded again)
c.removeEventListener(this);
- // add SI again in C.I if it was not removed and this
- // wait was not dropped
- if (c.getPendingIntentions().remove(sTE) == si && !c.getIntentions().contains(si) && !drop) {
- if (stopByTimeout && te != null && elapseTimeTerm == null) {
+ // add SI again in C.I if it was not removed and this wait was not dropped
+ if (c.getPendingIntentions().remove(sTE) == si && !c.getIntentions().contains(si) && !dropped) {
+ if (stopByTimeout && te != null && elapsedTimeTerm == null) {
// fail the .wait
PlanBody body = si.peek().getPlan().getBody();
body.add(1, new PlanBodyImpl(BodyType.internalAction, new InternalActionLiteral(".fail")));
}
si.peek().removeCurrentStep();
- if (elapseTimeTerm != null)
- si.peek().getUnif().unifies(elapseTimeTerm, new NumberTermImpl(elapseTime));
+ if (elapsedTimeTerm != null) {
+ long elapsedTime = System.currentTimeMillis() - startTime;
+ un.unifies(elapsedTimeTerm, new NumberTermImpl(elapsedTime));
+ }
if (si.isSuspended()) { // if the intention was suspended by .suspend
String k = suspend.SUSPENDED_INT+si.getId();
c.getPendingIntentions().put(k, si);
@@ -204,52 +192,21 @@
c.addIntention(si);
}
}
-
} catch (Exception e) {
ts.getLogger().log(Level.SEVERE, "Error at .wait thread", e);
- } finally {
- threads.remove(this);
}
}
- synchronized public void waitEvent() {
- long init = System.currentTimeMillis();
- elapseTime = 0;
- while (!ok && !drop) {
- try {
- if (timeout == -1) {
- wait();
- } else {
- long to = timeout - elapseTime;
- if (to <= 0)
- to = 100;
- wait(to);
- elapseTime = System.currentTimeMillis() - init;
- if (elapseTime >= timeout) {
- stopByTimeout = true;
- break;
- }
- }
- } catch (InterruptedException e) {
- drop = true;
- } catch (Exception e) {
- e.printStackTrace();
- }
+ public void eventAdded(Event e) {
+ if (te != null && !dropped && un.unifies(te, e.getTrigger())) {
+ resume(false);
}
}
- synchronized public void eventAdded(Event e) {
- if (te != null && !drop && un.unifies(te, e.getTrigger())) {
- ok = true;
- notifyAll();
- }
- }
-
- synchronized public void intentionDropped(Intention i) {
+ public void intentionDropped(Intention i) {
if (i.equals(si)) {
- ok = false;
- drop = true;
- notifyAll();
+ dropped = true;
+ resume(false);
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|