|
From: <jom...@us...> - 2008-04-25 14:39:46
|
Revision: 1259
http://jason.svn.sourceforge.net/jason/?rev=1259&view=rev
Author: jomifred
Date: 2008-04-25 07:39:27 -0700 (Fri, 25 Apr 2008)
Log Message:
-----------
add .while internal action
Modified Paths:
--------------
trunk/src/jason/asSemantics/Agent.java
trunk/src/jason/asSyntax/ArithFunctionTerm.java
trunk/src/jason/asSyntax/InternalActionLiteral.java
trunk/src/jason/infra/centralised/CentralisedAgArch.java
trunk/src/jason/stdlib/wait.java
Added Paths:
-----------
trunk/src/jason/ReceiverDoesNotExistException.java
trunk/src/jason/stdlib/loop.java
Added: trunk/src/jason/ReceiverDoesNotExistException.java
===================================================================
--- trunk/src/jason/ReceiverDoesNotExistException.java (rev 0)
+++ trunk/src/jason/ReceiverDoesNotExistException.java 2008-04-25 14:39:27 UTC (rev 1259)
@@ -0,0 +1,44 @@
+//----------------------------------------------------------------------------
+// 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.dur.ac.uk/r.bordini
+// http://www.inf.furb.br/~jomi
+//
+//----------------------------------------------------------------------------
+
+
+package jason;
+
+
+public class ReceiverDoesNotExistException extends java.lang.Exception {
+
+ private static final long serialVersionUID = 1L;
+
+ public ReceiverDoesNotExistException() {
+ }
+
+ public ReceiverDoesNotExistException(String msg) {
+ super(msg);
+ }
+
+ public ReceiverDoesNotExistException(String msg, Exception cause) {
+ super(msg);
+ initCause(cause);
+ }
+
+}
Modified: trunk/src/jason/asSemantics/Agent.java
===================================================================
--- trunk/src/jason/asSemantics/Agent.java 2008-04-25 13:29:38 UTC (rev 1258)
+++ trunk/src/jason/asSemantics/Agent.java 2008-04-25 14:39:27 UTC (rev 1259)
@@ -45,6 +45,7 @@
import jason.functions.RuleToFunction;
import jason.runtime.Settings;
import jason.stdlib.conditional;
+import jason.stdlib.loop;
import java.io.File;
import java.io.FileInputStream;
@@ -244,6 +245,8 @@
String iaName = action.getFunctor();
if (iaName.equals(".if"))
return conditional.create();
+ if (iaName.equals(".while"))
+ return loop.create();
if (iaName.charAt(0) == '.')
iaName = "jason.stdlib" + iaName;
InternalAction objIA = internalActions.get(iaName);
Modified: trunk/src/jason/asSyntax/ArithFunctionTerm.java
===================================================================
--- trunk/src/jason/asSyntax/ArithFunctionTerm.java 2008-04-25 13:29:38 UTC (rev 1258)
+++ trunk/src/jason/asSyntax/ArithFunctionTerm.java 2008-04-25 14:39:27 UTC (rev 1259)
@@ -99,8 +99,8 @@
} catch (Exception e) {
logger.log(Level.SEVERE, getErrorMsg()+ " -- "+ e);
}
- } else {
- logger.warning(getErrorMsg()+ " -- this function has unground arguments and can not be evaluated!");
+ //} else {
+ // logger.warning(getErrorMsg()+ " -- this function has unground arguments and can not be evaluated! Unifier is "+u);
}
return false;
Modified: trunk/src/jason/asSyntax/InternalActionLiteral.java
===================================================================
--- trunk/src/jason/asSyntax/InternalActionLiteral.java 2008-04-25 13:29:38 UTC (rev 1258)
+++ trunk/src/jason/asSyntax/InternalActionLiteral.java 2008-04-25 14:39:27 UTC (rev 1259)
@@ -26,6 +26,7 @@
import jason.asSemantics.Agent;
import jason.asSemantics.InternalAction;
import jason.asSemantics.Unifier;
+import jason.stdlib.loop;
import java.util.Iterator;
import java.util.logging.Level;
@@ -61,7 +62,7 @@
if (ag != null)
ia = ag.getIA(this);
}
-
+
@Override
public boolean isInternalAction() {
return true;
@@ -76,7 +77,16 @@
public boolean canBeAddedInBB() {
return false;
}
-
+
+ @Override
+ public boolean apply(Unifier u) {
+ if (this.ia != null && this.ia instanceof loop)
+ return false;
+ else
+ return super.apply(u);
+ }
+
+
@SuppressWarnings("unchecked")
public Iterator<Unifier> logicalConsequence(Agent ag, Unifier un) {
if (ag.getTS().getUserAgArch().isRunning()) {
Modified: trunk/src/jason/infra/centralised/CentralisedAgArch.java
===================================================================
--- trunk/src/jason/infra/centralised/CentralisedAgArch.java 2008-04-25 13:29:38 UTC (rev 1258)
+++ trunk/src/jason/infra/centralised/CentralisedAgArch.java 2008-04-25 14:39:27 UTC (rev 1259)
@@ -24,6 +24,7 @@
package jason.infra.centralised;
import jason.JasonException;
+import jason.ReceiverDoesNotExistException;
import jason.architecture.AgArch;
import jason.architecture.AgArchInfraTier;
import jason.asSemantics.ActionExec;
@@ -230,14 +231,14 @@
}
// this is used by the .send internal action in stdlib
- public void sendMsg(Message m) throws Exception {
+ public void sendMsg(Message m) throws ReceiverDoesNotExistException {
// actually send the message
m.setSender(getAgName());
CentralisedAgArch rec = masRunner.getAg(m.getReceiver());
if (rec == null) {
if (isRunning())
- throw new JasonException("Receiver '" + m.getReceiver() + "' does not exists! Could not send " + m);
+ throw new ReceiverDoesNotExistException("Receiver '" + m.getReceiver() + "' does not exists! Could not send " + m);
else
return;
}
Added: trunk/src/jason/stdlib/loop.java
===================================================================
--- trunk/src/jason/stdlib/loop.java (rev 0)
+++ trunk/src/jason/stdlib/loop.java 2008-04-25 14:39:27 UTC (rev 1259)
@@ -0,0 +1,103 @@
+//----------------------------------------------------------------------------
+// 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.dur.ac.uk/r.bordini
+// http://www.inf.furb.br/~jomi
+//
+//----------------------------------------------------------------------------
+
+package jason.stdlib;
+
+import jason.JasonException;
+import jason.asSemantics.DefaultInternalAction;
+import jason.asSemantics.IntendedMeans;
+import jason.asSemantics.InternalAction;
+import jason.asSemantics.TransitionSystem;
+import jason.asSemantics.Unifier;
+import jason.asSyntax.LogicalFormula;
+import jason.asSyntax.ObjectTerm;
+import jason.asSyntax.ObjectTermImpl;
+import jason.asSyntax.PlanBody;
+import jason.asSyntax.PlanBodyImpl;
+import jason.asSyntax.Structure;
+import jason.asSyntax.Term;
+import jason.asSyntax.PlanBody.BodyType;
+
+import java.util.Iterator;
+
+// TODO: comments
+public class loop extends DefaultInternalAction {
+
+ private static InternalAction singleton = null;
+ public static InternalAction create() {
+ if (singleton == null)
+ singleton = new loop();
+ return singleton;
+ }
+
+ @Override
+ public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
+ try {
+ if ( !(args[0] instanceof LogicalFormula))
+ throw new JasonException("The first argument of .while must be a logical formula.");
+
+ IntendedMeans im = ts.getC().getSelectedIntention().peek();
+ PlanBody whileia = im.getCurrentStep();
+
+ // store a backup of the unifier for the next iteration
+
+ // if the IA has a backup unifier, use that (it is an object term)
+ if (args.length == 3) {
+ // restore the unifier of previous iterations
+ Unifier ubak = (Unifier)((ObjectTerm)args[2]).getObject();
+ un.clear();
+ un.compose(ubak);
+ } else {
+ // add backup unifier in the IA
+ //ubak = (Unifier)un.clone();
+ ((Structure)whileia.getBodyTerm()).addTerm(new ObjectTermImpl(un.clone()));
+ }
+
+ LogicalFormula logExpr = (LogicalFormula)args[0].clone();
+ logExpr.apply(un); // need to apply since the internal action literal for while does not apply
+ Iterator<Unifier> iu = logExpr.logicalConsequence(ts.getAg(), un); //(Unifier)un.clone());
+ if (iu.hasNext()) {
+ if ( !args[1].isPlanBody())
+ throw new JasonException("The second argument of .while must be a plan body term.");
+
+ un.compose(iu.next());
+
+ PlanBody whattoadd = (PlanBody)args[1]; //.clone();
+ whattoadd.add(new PlanBodyImpl(BodyType.internalAction, (Term)whileia.getBodyTerm().clone())); //(PlanBody)whileia.clone()); // add the while after
+ whattoadd.setAsBodyTerm(false);
+ if (whileia.getPlanSize() == 1)
+ whileia.add(whattoadd);
+ else
+ whileia.add(1,whattoadd);
+ //System.out.println("new body="+whileia.getBodyNext());
+ }
+ return true;
+ } catch (ArrayIndexOutOfBoundsException e) {
+ throw new JasonException("The internal action 'while' has not received the required arguments.");
+ } catch (JasonException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new JasonException("Error in internal action 'while': " + e, e);
+ }
+ }
+}
Modified: trunk/src/jason/stdlib/wait.java
===================================================================
--- trunk/src/jason/stdlib/wait.java 2008-04-25 13:29:38 UTC (rev 1258)
+++ trunk/src/jason/stdlib/wait.java 2008-04-25 14:39:27 UTC (rev 1259)
@@ -67,8 +67,12 @@
<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
- fails.
+ fails.
+ <li> <code>.wait("+!g", 2000, nofail)</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 does not
+ fail.
</ul>
@see jason.stdlib.at
@@ -88,6 +92,8 @@
public Object execute(final TransitionSystem ts, Unifier un, Term[] args) throws Exception {
long timeout = -1;
Trigger te = null;
+
+ boolean failontimeout = true;
try {
if (args[0].isNumeric()) {
// time in milliseconds
@@ -101,8 +107,9 @@
te = Trigger.parseTrigger(st.getString());
if (args.length == 2) {
- NumberTerm tot = (NumberTerm) args[1];
- timeout = (long) tot.solve();
+ timeout = (long) ((NumberTerm) args[1]).solve();
+ if (args.length == 3 && args[2].toString().equals("nofail"));
+ failontimeout = false;
}
}
@@ -110,7 +117,7 @@
ts.getLogger().log(Level.SEVERE, "Error at .wait.", e);
return false;
}
- WaitEvent wet = new WaitEvent(te, un, ts, timeout);
+ WaitEvent wet = new WaitEvent(te, un, ts, timeout, failontimeout);
wet.start();
return true;
}
@@ -139,9 +146,10 @@
boolean ok = false;
boolean drop = false;
boolean stopByTimeout = false;
+ boolean failontimeout;
long timeout = -1;
- WaitEvent(Trigger te, Unifier un, TransitionSystem ts, long to) {
+ WaitEvent(Trigger te, Unifier un, TransitionSystem ts, long to, boolean failontimeout) {
super("wait "+te);
this.te = te;
this.un = un;
@@ -149,6 +157,7 @@
c = ts.getC();
si = c.getSelectedIntention();
this.timeout = to;
+ this.failontimeout = failontimeout;
// register listener
c.addEventListener(this);
@@ -175,7 +184,7 @@
// wait was not dropped
if (c.getPendingIntentions().remove(sTE) == si && !c.getIntentions().contains(si) && !drop) {
si.peek().removeCurrentStep();
- if (stopByTimeout && te != null) {
+ if (stopByTimeout && te != null && failontimeout) {
// fail the .wait
si.peek().getPlan().getBody().add(0, new PlanBodyImpl(BodyType.internalAction, new InternalActionLiteral(".fail")));
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|