|
From: <jom...@us...> - 2008-09-14 14:55:05
|
Revision: 1372
http://jason.svn.sourceforge.net/jason/?rev=1372&view=rev
Author: jomifred
Date: 2008-09-14 14:54:57 +0000 (Sun, 14 Sep 2008)
Log Message:
-----------
use the new features (related to error annotations) in the standard internal action of Jason
Modified Paths:
--------------
trunk/examples/gold-miners-II/jia/random.java
trunk/src/jason/JasonException.java
trunk/src/jason/asSemantics/DefaultInternalAction.java
trunk/src/jason/stdlib/abolish.java
trunk/src/jason/stdlib/add_annot.java
trunk/src/jason/stdlib/add_nested_source.java
trunk/src/jason/stdlib/add_plan.java
trunk/src/jason/stdlib/at.java
trunk/src/jason/stdlib/atom.java
trunk/src/jason/stdlib/broadcast.java
trunk/src/jason/stdlib/concat.java
trunk/src/jason/stdlib/count.java
trunk/src/jason/stdlib/create_agent.java
trunk/src/jason/stdlib/current_intention.java
trunk/src/jason/stdlib/date.java
trunk/src/jason/stdlib/delete.java
trunk/src/jason/stdlib/desire.java
trunk/src/jason/stdlib/difference.java
trunk/src/jason/stdlib/drop_all_desires.java
trunk/src/jason/stdlib/drop_all_events.java
trunk/src/jason/stdlib/drop_all_intentions.java
trunk/src/jason/stdlib/drop_desire.java
trunk/src/jason/stdlib/drop_event.java
trunk/src/jason/stdlib/drop_intention.java
trunk/src/jason/stdlib/fail.java
trunk/src/jason/stdlib/fail_goal.java
trunk/src/jason/stdlib/findall.java
trunk/src/jason/stdlib/foreach.java
trunk/src/jason/stdlib/ground.java
trunk/src/jason/stdlib/if_then_else.java
trunk/src/jason/stdlib/intend.java
trunk/src/jason/stdlib/intersection.java
trunk/src/jason/stdlib/kill_agent.java
trunk/src/jason/stdlib/length.java
trunk/src/jason/stdlib/list.java
trunk/src/jason/stdlib/literal.java
trunk/src/jason/stdlib/loop.java
trunk/src/jason/stdlib/member.java
trunk/src/jason/stdlib/min.java
trunk/src/jason/stdlib/my_name.java
trunk/src/jason/stdlib/nth.java
trunk/src/jason/stdlib/number.java
trunk/src/jason/stdlib/perceive.java
trunk/src/jason/stdlib/plan_label.java
trunk/src/jason/stdlib/random.java
trunk/src/jason/stdlib/range.java
trunk/src/jason/stdlib/relevant_plans.java
trunk/src/jason/stdlib/remove_plan.java
trunk/src/jason/stdlib/resume.java
trunk/src/jason/stdlib/reverse.java
trunk/src/jason/stdlib/send.java
trunk/src/jason/stdlib/sort.java
trunk/src/jason/stdlib/stopMAS.java
trunk/src/jason/stdlib/string.java
trunk/src/jason/stdlib/structure.java
trunk/src/jason/stdlib/substring.java
trunk/src/jason/stdlib/succeed_goal.java
trunk/src/jason/stdlib/suspend.java
trunk/src/jason/stdlib/term2string.java
trunk/src/jason/stdlib/time.java
trunk/src/jason/stdlib/union.java
trunk/src/jason/stdlib/wait.java
trunk/src/templates/ia
Modified: trunk/examples/gold-miners-II/jia/random.java
===================================================================
--- trunk/examples/gold-miners-II/jia/random.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/examples/gold-miners-II/jia/random.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -38,7 +38,7 @@
public Unifier next() {
i++;
- Unifier c = un.copy();
+ Unifier c = un.clone();
c.unifies(args[0], new NumberTermImpl(random.nextInt(max)));
return c;
}
Modified: trunk/src/jason/JasonException.java
===================================================================
--- trunk/src/jason/JasonException.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/JasonException.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -24,6 +24,8 @@
package jason;
+import jason.asSemantics.DefaultInternalAction;
+import jason.asSemantics.InternalAction;
import jason.asSyntax.Atom;
import jason.asSyntax.ListTerm;
import jason.asSyntax.ListTermImpl;
@@ -35,6 +37,8 @@
public class JasonException extends java.lang.Exception {
private static final long serialVersionUID = 1L;
+
+ public static final Term WRONG_ARGS = new Atom("wrong_arguments");
private static final Term defaultError = new Atom("internal_action");
private Term error = defaultError;
@@ -80,6 +84,24 @@
return createBasicErrorAnnots(error, getMessage());
}
+ @SuppressWarnings("unchecked")
+ public static JasonException createWrongArgumentNb(DefaultInternalAction ia) {
+ String msg;
+ if (ia.getMinArgs() == ia.getMaxArgs())
+ if (ia.getMinArgs() == 1)
+ msg = " One argument is expected.";
+ else
+ msg = " "+ia.getMinArgs()+" arguments are expected.";
+ else
+ msg = " From "+ia.getMinArgs()+" to "+ia.getMaxArgs()+" arguments are expected.";
+ return new JasonException("The internal action '"+ia.getClass().getSimpleName()+"' has not received the required number of argument(s)."+msg , WRONG_ARGS);
+ }
+
+ @SuppressWarnings("unchecked")
+ public static JasonException createWrongArgument(InternalAction ia, String reason) {
+ return new JasonException("Wrong argument for internal action '"+ia.getClass().getSimpleName()+"': "+reason, WRONG_ARGS);
+ }
+
public static ListTerm createBasicErrorAnnots(String id, String msg) {
return createBasicErrorAnnots(new Atom(id), msg);
}
Modified: trunk/src/jason/asSemantics/DefaultInternalAction.java
===================================================================
--- trunk/src/jason/asSemantics/DefaultInternalAction.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/asSemantics/DefaultInternalAction.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -1,5 +1,6 @@
package jason.asSemantics;
+import jason.JasonException;
import jason.asSyntax.Term;
import java.io.Serializable;
@@ -16,12 +17,15 @@
private static final long serialVersionUID = 1L;
- public boolean suspendIntention() {
- return false;
- }
+ public boolean suspendIntention() { return false; }
+ public boolean canBeUsedInContext() { return true; }
+
+ public int getMinArgs() { return 0; }
+ public int getMaxArgs() { return Integer.MAX_VALUE; }
- public boolean canBeUsedInContext() {
- return true;
+ protected void checkArguments(Term[] args) throws JasonException {
+ if (args.length < getMinArgs() || args.length > getMaxArgs())
+ throw JasonException.createWrongArgumentNb(this);
}
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
Modified: trunk/src/jason/stdlib/abolish.java
===================================================================
--- trunk/src/jason/stdlib/abolish.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/abolish.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -27,7 +27,6 @@
import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
-import jason.asSyntax.Atom;
import jason.asSyntax.Literal;
import jason.asSyntax.Term;
@@ -50,13 +49,20 @@
*/
public class abolish extends DefaultInternalAction {
- @Override
- public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- ts.getAg().abolish((Literal)args[0], un);
- return true;
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'abolish' has not received the required argument.", new Atom("wrong_arguments"));
- }
+ @Override public int getMinArgs() { return 1; }
+ @Override public int getMaxArgs() { return 1; }
+
+ @Override protected void checkArguments(Term[] args) throws JasonException {
+ super.checkArguments(args); // check number of arguments
+ if (!args[0].isLiteral())
+ throw JasonException.createWrongArgument(this,"first argument must be a literal");
}
+
+ @Override public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
+ checkArguments(args);
+
+ ts.getAg().abolish((Literal)args[0], un);
+ return true;
+ }
+
}
Modified: trunk/src/jason/stdlib/add_annot.java
===================================================================
--- trunk/src/jason/stdlib/add_annot.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/add_annot.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -77,18 +77,17 @@
singleton = new add_annot();
return singleton;
}
+
+ @Override public int getMinArgs() { return 3; }
+ @Override public int getMaxArgs() { return 3; }
- @Override
- public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- Term result = addAnnotToList(un, args[0], args[1]);
- return un.unifies(result,args[2]);
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'add_annot' requires three arguments.");
- }
+ @Override public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
+ checkArguments(args);
+ Term result = addAnnotToList(un, args[0], args[1]);
+ return un.unifies(result,args[2]);
}
- public Term addAnnotToList(Unifier unif, Term l, Term annot) {
+ public Term addAnnotToList(Unifier unif, Term l, Term annot) throws JasonException {
if (l.isList()) {
ListTerm result = new ListTermImpl();
for (Term lTerm: (ListTerm)l) {
@@ -98,21 +97,15 @@
}
}
return result;
- } else {
- try {
- // if it can be parsed as a literal and is not an atom, OK to add annot
- Literal result;
- if (l.isAtom())
- result = new Literal(((Structure)l).getFunctor());
- else
- result = Literal.parseLiteral(l.toString());
- result.addAnnot(annot);
- return result;
- } catch (Exception e) {
- // no problem, the content is not a pred (it is a number,
- // string, ....) received in a message, for instance
- }
+ } else if (l.isLiteral()) {
+ Literal result;
+ if (l.isAtom())
+ result = new Literal(((Structure)l).getFunctor());
+ else
+ result = Literal.parseLiteral(l.toString());
+ result.addAnnot(annot);
+ return result;
}
- return null;
+ return l;
}
}
Modified: trunk/src/jason/stdlib/add_nested_source.java
===================================================================
--- trunk/src/jason/stdlib/add_nested_source.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/add_nested_source.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -24,7 +24,6 @@
package jason.stdlib;
-import jason.JasonException;
import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.InternalAction;
import jason.asSemantics.TransitionSystem;
@@ -62,7 +61,7 @@
<li> <code>.add_nested_source(a[source(bob)],jomi,B)</code>: <code>B</code>
unifies with <code>a[source(jomi)[source(bob)]]</code>.</li>
- <li> <code>.add_annot([a1,a2], jomi, B)</code>: <code>B</code>
+ <li> <code>.add_nested_source([a1,a2], jomi, B)</code>: <code>B</code>
unifies with <code>[a1[source(jomi)], a2[source(jomi)]]</code>.</li>
</ul>
@@ -78,15 +77,14 @@
singleton = new add_nested_source();
return singleton;
}
+
+ @Override public int getMinArgs() { return 3; }
+ @Override public int getMaxArgs() { return 3; }
- @Override
- public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- Term result = addAnnotToList(un, args[0], (Structure)args[1].clone());
- return un.unifies(result,args[2]);
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'add_nested_source' requires three arguments.");
- }
+ @Override public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
+ checkArguments(args);
+ Term result = addAnnotToList(un, args[0], args[1].clone());
+ return un.unifies(result,args[2]);
}
public Term addAnnotToList(Unifier unif, Term l, Term source) {
@@ -99,28 +97,23 @@
}
}
return result;
+ } else if (l.isLiteral()) {
+ Literal result;
+ if (l.isAtom())
+ result = new Literal(((Structure)l).getFunctor());
+ else
+ result = (Literal)l.clone();
+
+ // create the source annots
+ Pred ts = new Pred("source",1);
+ ts.addTerm(source);
+ ts.addAnnots(result.getAnnots("source"));
+
+ result.delSources();
+ result.addAnnot(ts);
+ return result;
} else {
- try {
- // if it can be parsed as a literal and is not an atom, OK to add annot
- Literal result;
- if (l.isAtom())
- result = new Literal(((Structure)l).getFunctor());
- else
- result = Literal.parseLiteral(l.toString());
-
- // create the source annots
- Pred ts = new Pred("source",1);
- ts.addTerm(source);
- ts.addAnnots(result.getAnnots("source"));
-
- result.delSources();
- result.addAnnot(ts);
- return result;
- } catch (Exception e) {
- // no problem, the content is not a pred (it is a number,
- // string, ....) received in a message, for instance
- }
+ return l;
}
- return null;
}
}
Modified: trunk/src/jason/stdlib/add_plan.java
===================================================================
--- trunk/src/jason/stdlib/add_plan.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/add_plan.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -23,7 +23,6 @@
package jason.stdlib;
-import jason.JasonException;
import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
@@ -75,28 +74,27 @@
*/
public class add_plan extends DefaultInternalAction {
+ @Override public int getMinArgs() { return 1; }
+ @Override public int getMaxArgs() { return 2; }
+
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- Term plans = DefaultTerm.parse(args[0].toString());
+ checkArguments(args);
- Structure source = new Atom("self");
- if (args.length > 1) {
- source = (Structure) args[1];
- }
+ Term plans = DefaultTerm.parse(args[0].toString());
- if (plans.isList()) { // arg[0] is a list of strings
- for (Term t: (ListTerm) plans) {
- ts.getAg().getPL().add((StringTerm) t, source);
- }
- } else { // args[0] is a plan
- ts.getAg().getPL().add((StringTerm) plans, source);
+ Structure source = new Atom("self");
+ if (args.length > 1) {
+ source = (Structure) args[1];
+ }
+
+ if (plans.isList()) { // arg[0] is a list of strings
+ for (Term t: (ListTerm) plans) {
+ ts.getAg().getPL().add((StringTerm) t, source);
}
- return true;
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'add_plan' has not received two arguments (a plan as a string and the source).");
- } catch (Exception e) {
- throw new JasonException("Error in internal action 'add_plan': " + e, e);
+ } else { // args[0] is a plan
+ ts.getAg().getPL().add((StringTerm) plans, source);
}
+ return true;
}
}
Modified: trunk/src/jason/stdlib/at.java
===================================================================
--- trunk/src/jason/stdlib/at.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/at.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -72,59 +72,60 @@
public class at extends DefaultInternalAction {
public static final String atAtom = ".at";
+
+ @Override public int getMinArgs() { return 2; }
+ @Override public int getMaxArgs() { return 2; }
@Override
public Object execute(final TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- StringTerm time = (StringTerm)args[0];
- String stime = time.getString();
- StringTerm sevent = (StringTerm)args[1];
+ checkArguments(args);
+
+ StringTerm time = (StringTerm)args[0];
+ String stime = time.getString();
+ StringTerm sevent = (StringTerm)args[1];
- // parse time
- long deadline = -1;
+ // parse time
+ long deadline = -1;
- // if it starts with now
- if (stime.startsWith("now")) {
- // it is something like "now +3 minutes"
- stime = stime.substring(3).trim();
- // get the amount of time
- if (stime.startsWith("+")) {
- stime = stime.substring(1).trim();
- int pos = stime.indexOf(" ");
- if (pos > 0) {
- deadline = Integer.parseInt(stime.substring(0,pos));
- // get the time unit
- stime = stime.substring(pos).trim();
- if (stime.equals("s") || stime.startsWith("second")) {
- deadline *= 1000;
- }
- if (stime.equals("m") || stime.startsWith("minute")) {
- deadline *= 1000 * 60;
- }
- if (stime.equals("h") || stime.startsWith("hour")) {
- deadline *= 1000 * 60 * 60;
- }
- if (stime.equals("d") || stime.startsWith("day")) {
- deadline *= 1000 * 60 * 60 * 24;
- }
+ // if it starts with now
+ if (stime.startsWith("now")) {
+ // it is something like "now +3 minutes"
+ stime = stime.substring(3).trim();
+ // get the amount of time
+ if (stime.startsWith("+")) {
+ stime = stime.substring(1).trim();
+ int pos = stime.indexOf(" ");
+ if (pos > 0) {
+ deadline = Integer.parseInt(stime.substring(0,pos));
+ // get the time unit
+ stime = stime.substring(pos).trim();
+ if (stime.equals("s") || stime.startsWith("second")) {
+ deadline *= 1000;
}
+ if (stime.equals("m") || stime.startsWith("minute")) {
+ deadline *= 1000 * 60;
+ }
+ if (stime.equals("h") || stime.startsWith("hour")) {
+ deadline *= 1000 * 60 * 60;
+ }
+ if (stime.equals("d") || stime.startsWith("day")) {
+ deadline *= 1000 * 60 * 60 * 24;
+ }
}
-
- } else {
- throw new JasonException("The time parameter ('"+stime+"') of the internal action 'at' is not implemented!");
- }
-
- 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());
+
+ } else {
+ throw new JasonException("The time parameter ('"+stime+"') of the internal action 'at' is not implemented!");
+ }
- 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.");
- }
+ 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());
+
+ ts.getAg().getScheduler().schedule(new CheckDeadline(te, ts), deadline, TimeUnit.MILLISECONDS);
+ return true;
}
private static int idCount = 0;
Modified: trunk/src/jason/stdlib/atom.java
===================================================================
--- trunk/src/jason/stdlib/atom.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/atom.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -1,6 +1,5 @@
package jason.stdlib;
-import jason.JasonException;
import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.InternalAction;
import jason.asSemantics.TransitionSystem;
@@ -48,14 +47,12 @@
return singleton;
}
+ @Override public int getMinArgs() { return 1; }
+ @Override public int getMaxArgs() { return 1; }
+
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- return args[0].isAtom();
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'atom' has not received the required argument.");
- } catch (Exception e) {
- throw new JasonException("Error in internal action 'atom': " + e, e);
- }
+ checkArguments(args);
+ return args[0].isAtom();
}
}
Modified: trunk/src/jason/stdlib/broadcast.java
===================================================================
--- trunk/src/jason/stdlib/broadcast.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/broadcast.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -57,34 +57,32 @@
*/
public class broadcast extends DefaultInternalAction {
+ @Override public int getMinArgs() { return 2; }
+ @Override public int getMaxArgs() { return 2; }
+
@Override
+ protected void checkArguments(Term[] args) throws JasonException {
+ super.checkArguments(args);
+ if (!args[0].isAtom()) {
+ throw JasonException.createWrongArgument(this,"illocutionary force argument must be an atom");
+ }
+ }
+
+ @Override
public boolean canBeUsedInContext() {
return false;
}
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- Term ilf = null;
- Term pcnt = null;
+ checkArguments(args);
- try {
- ilf = args[0];
- if (!ilf.isAtom()) {
- throw new JasonException("The illocutionary force parameter of the internal action 'broadcast' is not an atom!");
- }
+ Term ilf = args[0];
+ Term pcnt = args[1];
- pcnt = args[1];
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'broadcast' has not received two arguments.");
- }
Message m = new Message(ilf.toString(), ts.getUserAgArch().getAgName(), null, pcnt);
-
- try {
- ts.getUserAgArch().broadcast(m);
- return true;
- } catch (Exception e) {
- throw new JasonException("Error broadcasting message " + pcnt, e);
- }
+ ts.getUserAgArch().broadcast(m);
+ return true;
}
}
Modified: trunk/src/jason/stdlib/concat.java
===================================================================
--- trunk/src/jason/stdlib/concat.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/concat.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -88,7 +88,7 @@
ListTerm result = (ListTerm)args[0].clone();
for (int i=1; i<args.length-1; i++) {
if (!args[i].isList())
- throw new JasonException("arg["+i+"] is not a list in concat.");
+ throw JasonException.createWrongArgument(this,"arg["+i+"] is not a list");
result.concat((ListTerm)args[i].clone());
}
return un.unifies(result, args[args.length-1]);
@@ -97,7 +97,7 @@
} else {
// string concat
if (!args[args.length-1].isVar() && !args[args.length-1].isString()) {
- throw new JasonException("Last argument of concat '"+args[args.length-1]+"' is not a string nor a variable.");
+ throw JasonException.createWrongArgument(this,"Last argument '"+args[args.length-1]+"' is not a string nor a variable.");
}
String vl = args[0].toString();
if (args[0].isString()) {
Modified: trunk/src/jason/stdlib/count.java
===================================================================
--- trunk/src/jason/stdlib/count.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/count.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -70,21 +70,26 @@
*/
public class count extends DefaultInternalAction {
+ @Override public int getMinArgs() { return 2; }
+ @Override public int getMaxArgs() { return 2; }
+
+ @Override protected void checkArguments(Term[] args) throws JasonException {
+ super.checkArguments(args); // check number of arguments
+ if (!(args[0] instanceof LogicalFormula))
+ throw JasonException.createWrongArgument(this,"first argument must be a formula");
+ }
+
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- LogicalFormula logExpr = (LogicalFormula)args[0];
- int n = 0;
- Iterator<Unifier> iu = logExpr.logicalConsequence(ts.getAg(), un);
- while (iu.hasNext()) {
- iu.next();
- n++;
- }
- return un.unifies(args[1], new NumberTermImpl(n));
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'count' has not received two arguments.");
- } catch (Exception e) {
- throw new JasonException("Error in internal action 'count': " + e, e);
+ checkArguments(args);
+
+ LogicalFormula logExpr = (LogicalFormula)args[0];
+ int n = 0;
+ Iterator<Unifier> iu = logExpr.logicalConsequence(ts.getAg(), un);
+ while (iu.hasNext()) {
+ iu.next();
+ n++;
}
+ return un.unifies(args[1], new NumberTermImpl(n));
}
}
Modified: trunk/src/jason/stdlib/create_agent.java
===================================================================
--- trunk/src/jason/stdlib/create_agent.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/create_agent.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -82,46 +82,51 @@
*/
public class create_agent extends DefaultInternalAction {
+ @Override public int getMinArgs() { return 2; }
+ @Override public int getMaxArgs() { return 3; }
+
+ @Override protected void checkArguments(Term[] args) throws JasonException {
+ super.checkArguments(args); // check number of arguments
+ if (!args[1].isString())
+ throw JasonException.createWrongArgument(this,"second argument must be a string");
+ if (args.length == 3 && !args[2].isString())
+ throw JasonException.createWrongArgument(this,"third argument must be a list");
+ }
+
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
+ checkArguments(args);
+
+ Term name = args[0];
+ StringTerm source = (StringTerm)args[1];
- try {
- Term name = args[0];
- StringTerm source = (StringTerm)args[1];
+ File fSource = new File(source.getString());
+ if (!fSource.exists()) {
+ throw new JasonException("The source file " + source + " was not found!");
+ }
- File fSource = new File(source.getString());
- if (!fSource.exists()) {
- throw new JasonException("The source file " + source + " was not found!");
- }
-
- String agClass = null;
- String agArchClass = null;
- ClassParameters bbPars = null;
- if (args.length > 2) { // optional parameter
- // get the parameters
- for (Term t: (ListTerm)args[2]) {
- if (t.isStructure()) {
- Structure s = (Structure)t;
- if (s.getFunctor().equals("beliefBaseClass")) {
- bbPars = new ClassParameters(testString(s.getTerm(0)));
- } else if (s.getFunctor().equals("agentClass")) {
- agClass = testString(s.getTerm(0)).toString();
- } else if (s.getFunctor().equals("agentArchClass")) {
- agArchClass = testString(s.getTerm(0)).toString();
- }
+ String agClass = null;
+ String agArchClass = null;
+ ClassParameters bbPars = null;
+ if (args.length > 2) { // optional parameter
+ // get the parameters
+ for (Term t: (ListTerm)args[2]) {
+ if (t.isStructure()) {
+ Structure s = (Structure)t;
+ if (s.getFunctor().equals("beliefBaseClass")) {
+ bbPars = new ClassParameters(testString(s.getTerm(0)));
+ } else if (s.getFunctor().equals("agentClass")) {
+ agClass = testString(s.getTerm(0)).toString();
+ } else if (s.getFunctor().equals("agentArchClass")) {
+ agArchClass = testString(s.getTerm(0)).toString();
}
}
-
}
- RuntimeServicesInfraTier rs = ts.getUserAgArch().getArchInfraTier().getRuntimeServices();
- return rs.createAgent(name.toString(), fSource.getAbsolutePath(), agClass, agArchClass, bbPars, ts.getSettings());
+ }
- } catch (IndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'create_agent' received a wrong number of arguments.");
- } catch (Exception e) {
- throw new JasonException("Error in internal action 'create_agent': " + e, e);
- }
+ RuntimeServicesInfraTier rs = ts.getUserAgArch().getArchInfraTier().getRuntimeServices();
+ return rs.createAgent(name.toString(), fSource.getAbsolutePath(), agClass, agArchClass, bbPars, ts.getSettings());
}
private Structure testString(Term t) {
Modified: trunk/src/jason/stdlib/current_intention.java
===================================================================
--- trunk/src/jason/stdlib/current_intention.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/current_intention.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -23,7 +23,6 @@
package jason.stdlib;
-import jason.JasonException;
import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.Event;
import jason.asSemantics.Intention;
@@ -93,27 +92,25 @@
*/
public class current_intention extends DefaultInternalAction {
+ @Override public int getMinArgs() { return 1; }
+ @Override public int getMaxArgs() { return 1; }
+
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- // try to get the intention from the "body"
- Intention i = ts.getC().getSelectedIntention();
+ checkArguments(args);
+
+ // try to get the intention from the "body"
+ Intention i = ts.getC().getSelectedIntention();
- if (i == null) {
- // try to get the intention from the event
- Event evt = ts.getC().getSelectedEvent();
- if (evt != null)
- i = evt.getIntention();
- }
- if (i != null)
- return un.unifies(i.getAsTerm(), args[0]);
- else
- return false;
-
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'current_intention' has not received the required argument.");
- } catch (Exception e) {
- throw new JasonException("Error in internal action 'current_intention': " + e, e);
- }
+ if (i == null) {
+ // try to get the intention from the event
+ Event evt = ts.getC().getSelectedEvent();
+ if (evt != null)
+ i = evt.getIntention();
+ }
+ if (i != null)
+ return un.unifies(i.getAsTerm(), args[0]);
+ else
+ return false;
}
}
Modified: trunk/src/jason/stdlib/date.java
===================================================================
--- trunk/src/jason/stdlib/date.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/date.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -1,6 +1,5 @@
package jason.stdlib;
-import jason.JasonException;
import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.InternalAction;
import jason.asSemantics.TransitionSystem;
@@ -46,18 +45,16 @@
return singleton;
}
+ @Override public int getMinArgs() { return 3; }
+ @Override public int getMaxArgs() { return 3; }
+
/** date(YY,MM,DD) */
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- Calendar now = new GregorianCalendar();
- return un.unifies(args[0], new NumberTermImpl(now.get(Calendar.YEAR))) &&
- un.unifies(args[1], new NumberTermImpl(now.get(Calendar.MONTH))) &&
- un.unifies(args[2], new NumberTermImpl(now.get(Calendar.DAY_OF_MONTH)));
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'date' has not received three arguments.");
- } catch (Exception e) {
- throw new JasonException("Error in internal action 'date': " + e, e);
- }
+ checkArguments(args);
+ Calendar now = new GregorianCalendar();
+ return un.unifies(args[0], new NumberTermImpl(now.get(Calendar.YEAR))) &&
+ un.unifies(args[1], new NumberTermImpl(now.get(Calendar.MONTH))) &&
+ un.unifies(args[2], new NumberTermImpl(now.get(Calendar.DAY_OF_MONTH)));
}
}
Modified: trunk/src/jason/stdlib/delete.java
===================================================================
--- trunk/src/jason/stdlib/delete.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/delete.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -79,27 +79,28 @@
return singleton;
}
+ @Override public int getMinArgs() { return 3; }
+ @Override public int getMaxArgs() { return 3; }
+
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- if (args[0].isNumeric() && args[1].isString()) {
- return un.unifies(args[2], deleteFromString((int)((NumberTerm)args[0]).solve(),(StringTerm)args[1]));
- }
- if (args[0].isNumeric() && args[1].isList()) {
- return un.unifies(args[2], deleteFromList((int)((NumberTerm)args[0]).solve(),(ListTerm)args[1]));
- }
- if (args[0].isString() && args[1].isString()) {
- return un.unifies(args[2], deleteFromString((StringTerm)args[0],(StringTerm)args[1]));
- }
-
- // first element as term
- if (args[1].isList()) {
- return un.unifies(args[2], deleteFromList(args[0],(ListTerm)args[1], un.clone()));
- }
- throw new JasonException("Incorrect use of the internal action '.delete' (see documentation).");
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action '.delete' has not received three arguments.");
+ checkArguments(args);
+
+ if (args[0].isNumeric() && args[1].isString()) {
+ return un.unifies(args[2], deleteFromString((int)((NumberTerm)args[0]).solve(),(StringTerm)args[1]));
+ }
+ if (args[0].isNumeric() && args[1].isList()) {
+ return un.unifies(args[2], deleteFromList((int)((NumberTerm)args[0]).solve(),(ListTerm)args[1]));
}
+ if (args[0].isString() && args[1].isString()) {
+ return un.unifies(args[2], deleteFromString((StringTerm)args[0],(StringTerm)args[1]));
+ }
+
+ // first element as term
+ if (args[1].isList()) {
+ return un.unifies(args[2], deleteFromList(args[0],(ListTerm)args[1], un.clone()));
+ }
+ throw new JasonException("Incorrect use of the internal action '.delete' (see documentation).");
}
ListTerm deleteFromList(Term element, ListTerm l, Unifier un) {
Modified: trunk/src/jason/stdlib/desire.java
===================================================================
--- trunk/src/jason/stdlib/desire.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/desire.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -24,7 +24,6 @@
package jason.stdlib;
-import jason.JasonException;
import jason.asSemantics.Circumstance;
import jason.asSemantics.Event;
import jason.asSemantics.Intention;
@@ -67,13 +66,8 @@
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- return desires(ts.getC(),(Literal)args[0],un);
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'desire' has not received the required argument.");
- } catch (Exception e) {
- throw new JasonException("Error in internal action 'desire': " + e, e);
- }
+ checkArguments(args);
+ return desires(ts.getC(),(Literal)args[0],un);
}
public boolean desires(Circumstance C, Literal l, Unifier un) {
Modified: trunk/src/jason/stdlib/difference.java
===================================================================
--- trunk/src/jason/stdlib/difference.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/difference.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -71,17 +71,22 @@
return singleton;
}
- @Override
- public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
-
- if (!args[0].isList())
- throw new JasonException("First argument of difference '"+args[0]+"'is not a list.");
+ @Override public int getMinArgs() { return 3; }
+ @Override public int getMaxArgs() { return 3; }
+
+ @Override protected void checkArguments(Term[] args) throws JasonException {
+ super.checkArguments(args); // check number of arguments
+ if (!args[0].isList())
+ throw JasonException.createWrongArgument(this,"first argument '"+args[0]+"'is not a list.");
if (!args[1].isList())
- throw new JasonException("Second argument of difference '"+args[1]+"'is not a list.");
-
+ throw JasonException.createWrongArgument(this,"second argument '"+args[1]+"'is not a list.");
if (!args[2].isVar() && !args[2].isList())
- throw new JasonException("Last argument of difference '"+args[2]+"'is not a list nor a variable.");
+ throw JasonException.createWrongArgument(this,"last argument '"+args[2]+"'is not a list nor a variable.");
+ }
+ @Override
+ public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
+ checkArguments(args);
return un.unifies(args[2], ((ListTerm)args[0]).difference( (ListTerm)args[1]) );
}
}
Modified: trunk/src/jason/stdlib/drop_all_desires.java
===================================================================
--- trunk/src/jason/stdlib/drop_all_desires.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/drop_all_desires.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -62,7 +62,7 @@
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- super.execute(ts, un, args);
+ super.execute(ts, un, args);
ts.getC().clearEvents();
return true;
}
Modified: trunk/src/jason/stdlib/drop_all_events.java
===================================================================
--- trunk/src/jason/stdlib/drop_all_events.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/drop_all_events.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -63,9 +63,13 @@
*/
public class drop_all_events extends DefaultInternalAction {
+
+ @Override public int getMinArgs() { return 0; }
+ @Override public int getMaxArgs() { return 0; }
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
+ checkArguments(args);
ts.getC().clearEvents();
return true;
}
Modified: trunk/src/jason/stdlib/drop_all_intentions.java
===================================================================
--- trunk/src/jason/stdlib/drop_all_intentions.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/drop_all_intentions.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -65,9 +65,14 @@
*/
public class drop_all_intentions extends DefaultInternalAction {
-
+
+ @Override public int getMinArgs() { return 0; }
+ @Override public int getMaxArgs() { return 0; }
+
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
+ checkArguments(args);
+
Circumstance C = ts.getC();
C.clearIntentions();
C.clearPendingIntentions();
Modified: trunk/src/jason/stdlib/drop_desire.java
===================================================================
--- trunk/src/jason/stdlib/drop_desire.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/drop_desire.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -23,7 +23,6 @@
package jason.stdlib;
-import jason.JasonException;
import jason.asSemantics.Circumstance;
import jason.asSemantics.Event;
import jason.asSemantics.Intention;
@@ -73,15 +72,11 @@
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- dropEvt(ts.getC(), (Literal)args[0], un);
- dropInt(ts.getC(), (Literal)args[0], un);
- return true;
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'drop_desire' has not received the required argument.");
- } catch (Exception e) {
- throw new JasonException("Error in internal action 'drop_desire': " + e, e);
- }
+ checkArguments(args);
+
+ dropEvt(ts.getC(), (Literal)args[0], un);
+ dropInt(ts.getC(), (Literal)args[0], un);
+ return true;
}
public void dropEvt(Circumstance C, Literal l, Unifier un) {
Modified: trunk/src/jason/stdlib/drop_event.java
===================================================================
--- trunk/src/jason/stdlib/drop_event.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/drop_event.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -23,7 +23,6 @@
package jason.stdlib;
-import jason.JasonException;
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
import jason.asSyntax.Literal;
@@ -65,13 +64,8 @@
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- dropEvt(ts.getC(), (Literal)args[0], un);
- return true;
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'drop_event' has not received the required argument.");
- } catch (Exception e) {
- throw new JasonException("Error in internal action 'drop_event': " + e, e);
- }
+ checkArguments(args);
+ dropEvt(ts.getC(), (Literal)args[0], un);
+ return true;
}
}
Modified: trunk/src/jason/stdlib/drop_intention.java
===================================================================
--- trunk/src/jason/stdlib/drop_intention.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/drop_intention.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -68,16 +68,20 @@
*/
public class drop_intention extends DefaultInternalAction {
+ @Override public int getMinArgs() { return 1; }
+ @Override public int getMaxArgs() { return 1; }
+
+ @Override protected void checkArguments(Term[] args) throws JasonException {
+ super.checkArguments(args); // check number of arguments
+ if (!args[0].isLiteral())
+ throw JasonException.createWrongArgument(this,"first argument must be a literal");
+ }
+
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- dropInt(ts.getC(),(Literal)args[0],un);
- return true;
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'drop_intention' has not received the required argument.");
- } catch (Exception e) {
- throw new JasonException("Error in internal action 'drop_intention': " + e, e);
- }
+ checkArguments(args);
+ dropInt(ts.getC(),(Literal)args[0],un);
+ return true;
}
public void dropInt(Circumstance C, Literal l, Unifier un) {
Modified: trunk/src/jason/stdlib/fail.java
===================================================================
--- trunk/src/jason/stdlib/fail.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/fail.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -52,8 +52,12 @@
return singleton;
}
+ @Override public int getMinArgs() { return 0; }
+ @Override public int getMaxArgs() { return 0; }
+
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
+ checkArguments(args);
return false;
}
}
Modified: trunk/src/jason/stdlib/fail_goal.java
===================================================================
--- trunk/src/jason/stdlib/fail_goal.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/fail_goal.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -75,15 +75,9 @@
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- drop(ts, (Literal)args[0], un);
- return true;
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'fail_goal' has not received one argument.");
- } catch (Exception e) {
- e.printStackTrace();
- throw new JasonException("Error in internal action 'fail_goal': " + e, e);
- }
+ checkArguments(args);
+ drop(ts, (Literal)args[0], un);
+ return true;
}
/* returns: >0 the intention was changed
Modified: trunk/src/jason/stdlib/findall.java
===================================================================
--- trunk/src/jason/stdlib/findall.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/findall.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -52,7 +52,7 @@
is has the same syntax as the plan context.
<br/>
- <li>+/- result (list): the resulting populated list.<br/>
+ <li>+/- result (list): the result list populated with found solutions for the query.<br/>
</ul>
@@ -76,24 +76,31 @@
*/
public class findall extends DefaultInternalAction {
+ @Override public int getMinArgs() { return 3; }
+ @Override public int getMaxArgs() { return 3; }
+
+ @Override protected void checkArguments(Term[] args) throws JasonException {
+ super.checkArguments(args); // check number of arguments
+ if (! (args[1] instanceof LogicalFormula))
+ throw JasonException.createWrongArgument(this,"second argument must be a formula");
+ }
+
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- Term var = args[0];
- LogicalFormula logExpr = (LogicalFormula)args[1];
+ checkArguments(args);
- ListTerm all = new ListTermImpl();
- ListTerm tail = all;
- Iterator<Unifier> iu = logExpr.logicalConsequence(ts.getAg(), un);
- while (iu.hasNext()) {
- Unifier nu = iu.next();
- Term vl = var.clone();
- vl.apply(nu);
- tail = tail.append(vl);
- }
- return un.unifies(args[2], all);
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'findall' has not received three arguments.");
+ Term var = args[0];
+ LogicalFormula logExpr = (LogicalFormula)args[1];
+
+ ListTerm all = new ListTermImpl();
+ ListTerm tail = all;
+ Iterator<Unifier> iu = logExpr.logicalConsequence(ts.getAg(), un);
+ while (iu.hasNext()) {
+ Unifier nu = iu.next();
+ Term vl = var.clone();
+ vl.apply(nu);
+ tail = tail.append(vl);
}
+ return un.unifies(args[2], all);
}
}
Modified: trunk/src/jason/stdlib/foreach.java
===================================================================
--- trunk/src/jason/stdlib/foreach.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/foreach.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -87,52 +87,55 @@
return singleton;
}
+ @Override public int getMinArgs() { return 2; }
+ @Override public int getMaxArgs() { return 2; }
+
+ @Override protected void checkArguments(Term[] args) throws JasonException {
+ super.checkArguments(args); // check number of arguments
+ if ( !(args[0] instanceof LogicalFormula))
+ throw JasonException.createWrongArgument(this,"first argument must be a logical formula.");
+ if ( !args[1].isPlanBody())
+ throw JasonException.createWrongArgument(this,"second argument must be a plan body term.");
+ }
+
+
@SuppressWarnings("unchecked")
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- IntendedMeans im = ts.getC().getSelectedIntention().peek();
- PlanBody foria = im.getCurrentStep();
+ IntendedMeans im = ts.getC().getSelectedIntention().peek();
+ PlanBody foria = im.getCurrentStep();
- Iterator<Unifier> iu;
+ Iterator<Unifier> iu;
+
+ if (args.length == 2) {
+ // first execution of while
+ checkArguments(args);
- if (args.length != 3) {
- // first execution of while
- if ( !(args[0] instanceof LogicalFormula))
- throw new JasonException("The first argument of for must be a logical formula.");
- if ( !args[1].isPlanBody())
- throw new JasonException("The second argument of for must be a plan body term.");
-
- // get all solutions for the loop
- // Note: you should get all solutions here, otherwise I concurrent modification will occur for the iterator
- LogicalFormula logExpr = (LogicalFormula)args[0];
- iu = logExpr.logicalConsequence(ts.getAg(), un.clone());
- List<Unifier> allsol = new ArrayList<Unifier>();
- while (iu.hasNext())
- allsol.add(iu.next());
- iu = allsol.iterator();
- ((Structure)foria.getBodyTerm()).addTerm(new ObjectTermImpl(iu));
- } else {
- // restore the solutions
- iu = (Iterator<Unifier>)((ObjectTerm)args[2]).getObject();
- }
-
- if (iu.hasNext()) {
- un.clear();
- un.compose(iu.next());
- PlanBody whattoadd = (PlanBody)args[1].clone();
- whattoadd.add(new PlanBodyImpl(BodyType.internalAction, foria.getBodyTerm().clone()));
- whattoadd.setAsBodyTerm(false);
- foria.add(1,whattoadd);
- //System.out.println("new body="+foria.getBodyNext());
- }
- return true;
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("'for' has not received the required arguments.");
- } catch (JasonException e) {
- throw e;
- } catch (Exception e) {
- throw new JasonException("Error in 'for': " + e, e);
+ // get all solutions for the loop
+ // Note: you should get all solutions here, otherwise I concurrent modification will occur for the iterator
+ LogicalFormula logExpr = (LogicalFormula)args[0];
+ iu = logExpr.logicalConsequence(ts.getAg(), un.clone());
+ List<Unifier> allsol = new ArrayList<Unifier>();
+ while (iu.hasNext())
+ allsol.add(iu.next());
+ iu = allsol.iterator();
+ ((Structure)foria.getBodyTerm()).addTerm(new ObjectTermImpl(iu));
+ } else if (args.length == 3) {
+ // restore the solutions
+ iu = (Iterator<Unifier>)((ObjectTerm)args[2]).getObject();
+ } else {
+ throw JasonException.createWrongArgumentNb(this);
}
+
+ if (iu.hasNext()) {
+ un.clear();
+ un.compose(iu.next());
+ PlanBody whattoadd = (PlanBody)args[1].clone();
+ whattoadd.add(new PlanBodyImpl(BodyType.internalAction, foria.getBodyTerm().clone()));
+ whattoadd.setAsBodyTerm(false);
+ foria.add(1,whattoadd);
+ //System.out.println("new body="+foria.getBodyNext());
+ }
+ return true;
}
}
Modified: trunk/src/jason/stdlib/ground.java
===================================================================
--- trunk/src/jason/stdlib/ground.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/ground.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -1,6 +1,5 @@
package jason.stdlib;
-import jason.JasonException;
import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.InternalAction;
import jason.asSemantics.TransitionSystem;
@@ -42,15 +41,13 @@
singleton = new ground();
return singleton;
}
+
+ @Override public int getMinArgs() { return 1; }
+ @Override public int getMaxArgs() { return 1; }
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- return args[0].isGround();
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'ground' has not received the required argument.");
- } catch (Exception e) {
- throw new JasonException("Error in internal action 'ground': " + e, e);
- }
+ checkArguments(args);
+ return args[0].isGround();
}
}
Modified: trunk/src/jason/stdlib/if_then_else.java
===================================================================
--- trunk/src/jason/stdlib/if_then_else.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/if_then_else.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -72,40 +72,40 @@
return singleton;
}
+ @Override public int getMinArgs() { return 2; }
+ @Override public int getMaxArgs() { return 3; }
+
+ @Override protected void checkArguments(Term[] args) throws JasonException {
+ super.checkArguments(args); // check number of arguments
+ if ( !(args[0] instanceof LogicalFormula))
+ throw JasonException.createWrongArgument(this,"first argument (test) must be a logical formula.");
+ if ( !args[1].isPlanBody())
+ throw JasonException.createWrongArgument(this,"second argument (then) must be a plan body term.");
+ if ( args.length == 3 && !args[2].isPlanBody())
+ throw JasonException.createWrongArgument(this,"third argument (else) must be a plan body term.");
+ }
+
@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 if must be a logical formula.");
+ checkArguments(args);
- LogicalFormula logExpr = (LogicalFormula)args[0];
- PlanBody whattoadd = null;
+ LogicalFormula logExpr = (LogicalFormula)args[0];
+ PlanBody whattoadd = null;
- Iterator<Unifier> iu = logExpr.logicalConsequence(ts.getAg(), un);
- if (iu.hasNext()) { // .if THEN
- if ( !args[1].isPlanBody())
- throw new JasonException("The second argument of if must be a plan body term.");
- whattoadd = (PlanBody)args[1];
- un.compose(iu.next());
- } else if (args.length == 3) { // .if ELSE
- if ( !args[2].isPlanBody())
- throw new JasonException("The third argument of if must be a plan body term.");
- whattoadd = (PlanBody)args[2];
- }
+ Iterator<Unifier> iu = logExpr.logicalConsequence(ts.getAg(), un);
+ if (iu.hasNext()) { // .if THEN
+ whattoadd = (PlanBody)args[1];
+ un.compose(iu.next());
+ } else if (args.length == 3) { // .if ELSE
+ whattoadd = (PlanBody)args[2];
+ }
- if (whattoadd != null) {
- IntendedMeans im = ts.getC().getSelectedIntention().peek();
- PlanBody ifia = im.getCurrentStep();
- whattoadd.setAsBodyTerm(false);
- ifia.add(1,whattoadd);
- }
- return true;
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The 'if' has not received the required arguments.");
- } catch (JasonException e) {
- throw e;
- } catch (Exception e) {
- throw new JasonException("Error in 'if': " + e, e);
+ if (whattoadd != null) {
+ IntendedMeans im = ts.getC().getSelectedIntention().peek();
+ PlanBody ifia = im.getCurrentStep();
+ whattoadd.setAsBodyTerm(false);
+ ifia.add(1,whattoadd);
}
+ return true;
}
}
Modified: trunk/src/jason/stdlib/intend.java
===================================================================
--- trunk/src/jason/stdlib/intend.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/intend.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -69,15 +69,19 @@
*/
public class intend extends DefaultInternalAction {
+ @Override public int getMinArgs() { return 1; }
+ @Override public int getMaxArgs() { return 1; }
+
+ @Override protected void checkArguments(Term[] args) throws JasonException {
+ super.checkArguments(args); // check number of arguments
+ if (!args[0].isLiteral())
+ throw JasonException.createWrongArgument(this,"first argument must be a literal");
+ }
+
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
- try {
- return intends(ts.getC(),(Literal)args[0],un);
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new JasonException("The internal action 'intend' has not received the required argument.");
- } catch (Exception e) {
- throw new JasonException("Error in internal action 'intend': " + e, e);
- }
+ checkArguments(args);
+ return intends(ts.getC(),(Literal)args[0],un);
}
public boolean intends(Circumstance C, Literal l, Unifier un) {
Modified: trunk/src/jason/stdlib/intersection.java
===================================================================
--- trunk/src/jason/stdlib/intersection.java 2008-09-14 10:34:29 UTC (rev 1371)
+++ trunk/src/jason/stdlib/intersection.java 2008-09-14 14:54:57 UTC (rev 1372)
@@ -23,8 +23,6 @@
package jason.stdlib;
-import jason.JasonException;
-import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.InternalAction;
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
@@ -62,7 +60,7 @@
@see jason.stdlib.difference
@see jason.stdlib.union
*/
-public class intersection extends DefaultInternalAction {
+public class intersection extends difference { // to inherit checkArgs
private static InternalAction singleton = null;
public static InternalAction create() {
@@ -73,15 +71,7 @@
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
-
- if (!args[0].isList())
- throw new JasonException("First argument of intersection '"+args[0]+"'is not a list.");
- if (!args[1].isList())
- throw new JasonException("Second argument of intersection '"+args[1]+"'is not a list.");
-
- if (!args[2].isVar() && !args[2].isList())
- throw new JasonException("Last argument of intersection '"+args[2]+"'is not a list nor a variable.");
-
+ checkArguments(args);
return un.unifies(args[2], ((ListTerm)args[0]).intersection( (ListTerm)args[1]) );
}
}
Modified: trunk/src/jason/stdlib/kill_agent.java
===================================================================
--- trunk/src/jason/stdlib/kill_agent.java 2008-09-14 10:34:2...
[truncated message content] |