|
From: <jom...@us...> - 2008-04-30 15:33:36
|
Revision: 1280
http://jason.svn.sourceforge.net/jason/?rev=1280&view=rev
Author: jomifred
Date: 2008-04-30 08:33:32 -0700 (Wed, 30 Apr 2008)
Log Message:
-----------
add internal action .for
Modified Paths:
--------------
trunk/src/jason/asSemantics/Agent.java
trunk/src/jason/asSemantics/Circumstance.java
trunk/src/jason/asSyntax/InternalActionLiteral.java
trunk/src/jason/asSyntax/ObjectTermImpl.java
trunk/src/jason/stdlib/loop.java
trunk/src/xml/agInspection.xsl
Added Paths:
-----------
trunk/src/jason/stdlib/foreach.java
Modified: trunk/src/jason/asSemantics/Agent.java
===================================================================
--- trunk/src/jason/asSemantics/Agent.java 2008-04-29 21:13:16 UTC (rev 1279)
+++ trunk/src/jason/asSemantics/Agent.java 2008-04-30 15:33:32 UTC (rev 1280)
@@ -45,6 +45,7 @@
import jason.functions.RuleToFunction;
import jason.runtime.Settings;
import jason.stdlib.conditional;
+import jason.stdlib.foreach;
import jason.stdlib.loop;
import java.io.File;
@@ -247,6 +248,8 @@
return conditional.create();
if (iaName.equals(".while"))
return loop.create();
+ if (iaName.equals(".for"))
+ return foreach.create();
if (iaName.charAt(0) == '.')
iaName = "jason.stdlib" + iaName;
InternalAction objIA = internalActions.get(iaName);
@@ -675,6 +678,8 @@
}
}
Document document = builder.newDocument();
+ document.appendChild(document.createProcessingInstruction("xml-stylesheet", "href='agInspection.xsl' type='text/xsl' "));
+
Element ag = getAsDOM(document);
document.appendChild(ag);
Modified: trunk/src/jason/asSemantics/Circumstance.java
===================================================================
--- trunk/src/jason/asSemantics/Circumstance.java 2008-04-29 21:13:16 UTC (rev 1279)
+++ trunk/src/jason/asSemantics/Circumstance.java 2008-04-30 15:33:32 UTC (rev 1280)
@@ -441,8 +441,9 @@
// intentions
Element ints = (Element) document.createElement("intentions");
Element selIntEle = null;
- if (getSelectedIntention() != null && !getSelectedIntention().isFinished()) {
- selIntEle = getSelectedIntention().getAsDOM(document);
+ Intention ci = getSelectedIntention();
+ if (ci != null && !ci.isFinished()) {
+ selIntEle = ci.getAsDOM(document);
selIntEle.setAttribute("selected", "true");
ints.appendChild(selIntEle);
}
Modified: trunk/src/jason/asSyntax/InternalActionLiteral.java
===================================================================
--- trunk/src/jason/asSyntax/InternalActionLiteral.java 2008-04-29 21:13:16 UTC (rev 1279)
+++ trunk/src/jason/asSyntax/InternalActionLiteral.java 2008-04-30 15:33:32 UTC (rev 1280)
@@ -26,6 +26,7 @@
import jason.asSemantics.Agent;
import jason.asSemantics.InternalAction;
import jason.asSemantics.Unifier;
+import jason.stdlib.foreach;
import jason.stdlib.loop;
import java.util.Iterator;
@@ -80,7 +81,7 @@
@Override
public boolean apply(Unifier u) {
- if (this.ia != null && this.ia instanceof loop)
+ if (this.ia != null && (this.ia instanceof loop || this.ia instanceof foreach))
return false;
else
return super.apply(u);
Modified: trunk/src/jason/asSyntax/ObjectTermImpl.java
===================================================================
--- trunk/src/jason/asSyntax/ObjectTermImpl.java 2008-04-29 21:13:16 UTC (rev 1279)
+++ trunk/src/jason/asSyntax/ObjectTermImpl.java 2008-04-30 15:33:32 UTC (rev 1280)
@@ -1,11 +1,15 @@
package jason.asSyntax;
+import java.lang.reflect.Method;
+
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class ObjectTermImpl extends DefaultTerm implements ObjectTerm {
private final Object o;
+ private Method mclone;
+ private boolean hasTestedClone = false;
/** Creates a new Term Wrapper for java object */
public ObjectTermImpl(Object o) {
@@ -29,12 +33,18 @@
@Override
public Object clone() {
try {
- return new ObjectTermImpl(o.getClass().getMethod("clone", (Class[])null).invoke(o, (Object[])null));
+ if (!hasTestedClone) {
+ hasTestedClone = true;
+ mclone = o.getClass().getMethod("clone", (Class[])null);
+ }
+ if (mclone != null) {
+ return new ObjectTermImpl(mclone.invoke(o, (Object[])null));
+ }
} catch (Exception e) {
- System.err.println("The object inside ObjectTerm should be clonable!");
- e.printStackTrace();
- return null;
+ //System.err.println("The object inside ObjectTerm should be clonable!");
+ //e.printStackTrace();
}
+ return this;
}
@Override
Added: trunk/src/jason/stdlib/foreach.java
===================================================================
--- trunk/src/jason/stdlib/foreach.java (rev 0)
+++ trunk/src/jason/stdlib/foreach.java 2008-04-30 15:33:32 UTC (rev 1280)
@@ -0,0 +1,96 @@
+//----------------------------------------------------------------------------
+// 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 foreach extends DefaultInternalAction {
+
+ private static InternalAction singleton = null;
+ public static InternalAction create() {
+ if (singleton == null)
+ singleton = new foreach();
+ return singleton;
+ }
+
+ @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();
+
+ Iterator<Unifier> iu;
+
+ 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 the solutions for the loop
+ LogicalFormula logExpr = (LogicalFormula)args[0];
+ iu = logExpr.logicalConsequence(ts.getAg(), un.copy());
+ ((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, (Term)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("The internal action 'for' has not received the required arguments.");
+ } catch (JasonException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new JasonException("Error in internal action 'for': " + e, e);
+ }
+ }
+}
Modified: trunk/src/jason/stdlib/loop.java
===================================================================
--- trunk/src/jason/stdlib/loop.java 2008-04-29 21:13:16 UTC (rev 1279)
+++ trunk/src/jason/stdlib/loop.java 2008-04-30 15:33:32 UTC (rev 1280)
@@ -53,8 +53,6 @@
@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();
@@ -62,33 +60,33 @@
// 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) {
+ if (args.length != 3) {
+ // first execution of while
+ if ( !(args[0] instanceof LogicalFormula))
+ throw new JasonException("The first argument of .while must be a logical formula.");
+ if ( !args[1].isPlanBody())
+ throw new JasonException("The second argument of .while must be a plan body term.");
+
+ // add backup unifier in the IA
+ ((Structure)whileia.getBodyTerm()).addTerm(new ObjectTermImpl(un.clone()));
+ } else {
// 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());
+ 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);
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);
+ whileia.add(1,whattoadd);
//System.out.println("new body="+whileia.getBodyNext());
}
return true;
Modified: trunk/src/xml/agInspection.xsl
===================================================================
--- trunk/src/xml/agInspection.xsl 2008-04-29 21:13:16 UTC (rev 1279)
+++ trunk/src/xml/agInspection.xsl 2008-04-30 15:33:32 UTC (rev 1280)
@@ -27,8 +27,8 @@
<xsl:param name="show-mb" select="'true'" />
<xsl:param name="show-plan" select="'true'" />
<xsl:param name="show-int" select="'true'" />
- <xsl:param name="show-plan-details" select="'false'" />
- <xsl:param name="show-int-details" select="'false'" />
+ <xsl:param name="show-plan-details" select="'true'" />
+ <xsl:param name="show-int-details" select="'true'" />
<xsl:output method="html" />
<xsl:strip-space elements="*" />
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|