|
From: <jom...@us...> - 2013-07-18 16:54:26
|
Revision: 1741
http://sourceforge.net/p/jason/svn/1741
Author: jomifred
Date: 2013-07-18 16:54:23 +0000 (Thu, 18 Jul 2013)
Log Message:
-----------
initial implementation of .wait(logical formula)
Modified Paths:
--------------
trunk/examples/auction/auctioneer.asl
trunk/src/jason/stdlib/wait.java
Modified: trunk/examples/auction/auctioneer.asl
===================================================================
--- trunk/examples/auction/auctioneer.asl 2013-07-18 00:44:04 UTC (rev 1740)
+++ trunk/examples/auction/auctioneer.asl 2013-07-18 16:54:23 UTC (rev 1741)
@@ -13,4 +13,3 @@
show_winner(N,W); // show it in the GUI
.broadcast(tell, winner(W));
.abolish(place_bid(N,_)).
-
Modified: trunk/src/jason/stdlib/wait.java
===================================================================
--- trunk/src/jason/stdlib/wait.java 2013-07-18 00:44:04 UTC (rev 1740)
+++ trunk/src/jason/stdlib/wait.java 2013-07-18 16:54:23 UTC (rev 1741)
@@ -32,6 +32,7 @@
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
import jason.asSyntax.InternalActionLiteral;
+import jason.asSyntax.LogicalFormula;
import jason.asSyntax.NumberTerm;
import jason.asSyntax.NumberTermImpl;
import jason.asSyntax.PlanBody;
@@ -52,8 +53,10 @@
<code>{+!go(X,Y)}</code>.
<p>Parameters:<ul>
- <li><i>+ event</i> (trigger term): the event to wait for.<br/>
- <li>+ timeout (number).<br/>
+ <li><i>+ event</i> (trigger term [optional]): the event to wait for.<br/>
+ <li><i>+ logical expression</i> ([optional]): the expression (as used on plans context) to wait to holds.<br/>
+ <li>+ timeout (number [optional]): how many miliseconds should be waited.<br/>
+ <li>- elapse time (var [optional]): the amount of time the intention was suspended waiting.<br/>
</ul>
@@ -63,6 +66,9 @@
<li> <code>.wait({+b(1)})</code>: suspend the intention until the belief
<code>b(1)</code> is added in the belief base.
+ <li> <code>.wait(b(X) & X > 10)</code>: suspend the intention until the agent believes
+ <code>b(X)</code> with X greater than 10.
+
<li> <code>.wait({+!g}, 2000)</code>: suspend the intention until the goal
<code>g</code> is triggered or 2 seconds have passed, whatever happens
first. In case the event does not happens in two seconds, the internal action
@@ -95,29 +101,31 @@
long timeout = -1;
Trigger te = null;
+ LogicalFormula f = null;
Term elapsedTime = null;
if (args[0].isNumeric()) {
// time in milliseconds
NumberTerm time = (NumberTerm)args[0];
timeout = (long) time.solve();
- } else { // if (args[0].isString())
- // wait for event
- te = Trigger.tryToGetTrigger(args[0]); //ASSyntax.parseTrigger( ((StringTerm) args[0]).getString());
- //te.getLiteral().apply(un);
-
+ } else {
+ te = Trigger.tryToGetTrigger(args[0]); // wait for event
+ if (te == null && args[0] instanceof LogicalFormula) { // wait for an expression to become true
+ f = (LogicalFormula)args[0];
+ }
if (args.length >= 2)
timeout = (long) ((NumberTerm) args[1]).solve();
if (args.length == 3)
elapsedTime = args[2];
}
- new WaitEvent(te, un, ts, timeout, elapsedTime);
+ new WaitEvent(te, f, un, ts, timeout, elapsedTime);
return true;
}
class WaitEvent implements CircumstanceListener {
private Trigger te;
- private String sTE; // a string version of TE
+ private LogicalFormula formula;
+ private String sEvt; // a string version of what is being waited
private Unifier un;
private Intention si;
private TransitionSystem ts;
@@ -126,8 +134,9 @@
private Term elapsedTimeTerm;
private long startTime;
- WaitEvent(Trigger te, Unifier un, TransitionSystem ts, long timeout, Term elapsedTimeTerm) {
+ WaitEvent(Trigger te, LogicalFormula f, Unifier un, TransitionSystem ts, long timeout, Term elapsedTimeTerm) {
this.te = te;
+ this.formula = f;
this.un = un;
this.ts = ts;
c = ts.getC();
@@ -138,12 +147,14 @@
c.addEventListener(this);
if (te != null) {
- sTE = te.toString();
+ sEvt = te.toString();
+ } else if (formula != null) {
+ sEvt = formula.toString();
} else {
- sTE = "time"+(timeout);
+ sEvt = "time"+(timeout);
}
- sTE = si.getId()+"/"+sTE;
- c.addPendingIntention(sTE, si);
+ sEvt = si.getId()+"/"+sEvt;
+ c.addPendingIntention(sEvt, si);
startTime = System.currentTimeMillis();
@@ -165,7 +176,7 @@
public void run() {
try {
// add SI again in C.I if it was not removed and this wait was not dropped
- if (c.removePendingIntention(sTE) == si && !c.hasIntention(si) && !dropped) {
+ if (c.removePendingIntention(sEvt) == si && !c.hasIntention(si) && !dropped) {
if (stopByTimeout && te != null && elapsedTimeTerm == null) {
// fail the .wait by timeout
if (si.isSuspended()) { // if the intention was suspended by .suspend
@@ -198,8 +209,12 @@
}
public void eventAdded(Event e) {
- if (te != null && !dropped && un.unifies(te, e.getTrigger())) {
+ if (dropped)
+ return;
+ if (te != null && un.unifies(te, e.getTrigger())) {
resume(false);
+ } else if (formula != null && ts.getAg().believes(formula, un)) { // each new event, just test the formula being waited
+ resume(false);
}
}
@@ -214,7 +229,7 @@
public void intentionResumed(Intention i) { }
public void intentionSuspended(Intention i, String reason) { }
public String toString() {
- return sTE;
+ return sEvt;
}
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|