|
From: <jom...@us...> - 2009-03-03 07:42:20
|
Revision: 1457
http://jason.svn.sourceforge.net/jason/?rev=1457&view=rev
Author: jomifred
Date: 2009-03-03 07:42:18 +0000 (Tue, 03 Mar 2009)
Log Message:
-----------
fix doc of term2string, improve foreach loop
Modified Paths:
--------------
trunk/applications/as-unit-test/src/jason/tests/BugVarsAsArg.java
trunk/applications/as-unit-test/src/jason/tests/BugVarsAsGoal.java
trunk/src/jason/asSyntax/LiteralImpl.java
trunk/src/jason/asSyntax/Pred.java
trunk/src/jason/asSyntax/Structure.java
trunk/src/jason/stdlib/foreach.java
trunk/src/jason/stdlib/loop.java
trunk/src/jason/stdlib/term2string.java
trunk/src/test/TermTest.java
Modified: trunk/applications/as-unit-test/src/jason/tests/BugVarsAsArg.java
===================================================================
--- trunk/applications/as-unit-test/src/jason/tests/BugVarsAsArg.java 2009-03-02 15:20:39 UTC (rev 1456)
+++ trunk/applications/as-unit-test/src/jason/tests/BugVarsAsArg.java 2009-03-03 07:42:18 UTC (rev 1457)
@@ -5,7 +5,7 @@
import org.junit.Before;
import org.junit.Test;
-/** based on bug found by J\xBFrgen Villadsen */
+/** based on bug found by Stephen Cranefield -- see jason list */
public class BugVarsAsArg {
TestAgent ag;
@@ -17,17 +17,35 @@
// defines the agent's AgentSpeak code
ag.parseAScode(
- "+!g <- +p(ggg); !gg. " +
- "+!gg : p(X) <- !!X. " +
- "+!ggg[source(A)] <- jason.asunit.print(A). "
+ "test_rule(A,a(A)). "+
+ "ml0(L,L). "+
+ "ml(V1,V2,R) :- ml0([V1,V2],R). "+
+
+ "append([], L, L). "+
+ "append([H|T], L1, [H|L2]) :- append(T, L1, L2). "+
+
+ "+!test1 <- ?test_rule(T,A); A = a(V); T=45; jason.asunit.print(V). "+
+ "+!test2 <- ?ml(A,B,L); A=1; B=2; jason.asunit.print(L). "+
+ "+!test3 <- L=[X,Y]; ?append(L, [Z], L2); Z=a; X=f; Y=i; jason.asunit.print(L2). "
);
}
-
-
+
@Test
- public void testGoal() {
- ag.addGoal("g");
- ag.assertPrint("self", 10);
+ public void testRule1() {
+ ag.addGoal("test1");
+ ag.assertPrint("45", 5);
}
+
+ @Test
+ public void testRule2() {
+ ag.addGoal("test2");
+ ag.assertPrint("[1,2]", 5);
+ }
+
+ @Test
+ public void testRule3() {
+ ag.addGoal("test3");
+ ag.assertPrint("[f,i,a]", 10);
+ }
}
Modified: trunk/applications/as-unit-test/src/jason/tests/BugVarsAsGoal.java
===================================================================
--- trunk/applications/as-unit-test/src/jason/tests/BugVarsAsGoal.java 2009-03-02 15:20:39 UTC (rev 1456)
+++ trunk/applications/as-unit-test/src/jason/tests/BugVarsAsGoal.java 2009-03-03 07:42:18 UTC (rev 1457)
@@ -5,7 +5,7 @@
import org.junit.Before;
import org.junit.Test;
-/** based on bug found by Stephen Cranefield -- see jason list */
+/** based on bug found by Jorgen Villadsen */
public class BugVarsAsGoal {
TestAgent ag;
@@ -17,36 +17,15 @@
// defines the agent's AgentSpeak code
ag.parseAScode(
- "test_rule(A,a(A)). "+
- "ml0(L,L). "+
- "ml(V1,V2,R) :- ml0([V1,V2],R). "+
-
- "append([], L, L). "+
- "append([H|T], L1, [H|L2]) :- append(T, L1, L2). "+
-
- "+!test1 <- ?test_rule(T,A); A = a(V); T=45; jason.asunit.print(V). "+
- "+!test2 <- ?ml(A,B,L); A=1; B=2; jason.asunit.print(L). "+
- "+!test3 <- L=[X,Y]; ?append(L, [Z], L2); Z=a; X=f; Y=i; jason.asunit.print(L2). "
+ "+!g <- +p(ggg); !gg. " +
+ "+!gg : p(X) <- !!X. " +
+ "+!ggg[source(A)] <- jason.asunit.print(A). "
);
}
-
-
-
+
@Test
- public void testRule1() {
- ag.addGoal("test1");
- ag.assertPrint("45", 5);
+ public void testGoal() {
+ ag.addGoal("g");
+ ag.assertPrint("self", 10);
}
-
- @Test
- public void testRule2() {
- ag.addGoal("test2");
- ag.assertPrint("[1,2]", 5);
- }
-
- @Test
- public void testRule3() {
- ag.addGoal("test3");
- ag.assertPrint("[f,i,a]", 10);
- }
}
Modified: trunk/src/jason/asSyntax/LiteralImpl.java
===================================================================
--- trunk/src/jason/asSyntax/LiteralImpl.java 2009-03-02 15:20:39 UTC (rev 1456)
+++ trunk/src/jason/asSyntax/LiteralImpl.java 2009-03-03 07:42:18 UTC (rev 1457)
@@ -93,8 +93,8 @@
if (o instanceof LiteralImpl) {
final LiteralImpl l = (LiteralImpl) o;
return type == l.type && hashCode() == l.hashCode() && super.equals(l);
- } else if (o instanceof Atom) {
- return !negated() && super.equals(o);
+ } else if (o instanceof Atom && !negated()) {
+ return super.equals(o);
}
return false;
}
Modified: trunk/src/jason/asSyntax/Pred.java
===================================================================
--- trunk/src/jason/asSyntax/Pred.java 2009-03-02 15:20:39 UTC (rev 1456)
+++ trunk/src/jason/asSyntax/Pred.java 2009-03-03 07:42:18 UTC (rev 1457)
@@ -425,8 +425,8 @@
if (o instanceof Pred) {
final Pred p = (Pred) o;
return super.equals(o) && this.hasSubsetAnnot(p) && p.hasSubsetAnnot(this);
- } else if (o instanceof Atom) {
- return !hasAnnot() && super.equals(o);
+ } else if (o instanceof Atom && !hasAnnot() ) { // if o is some object that extends Atom (e.g. structure), goes to super equals
+ return super.equals(o); // consider super equals only when this has no annots
}
return false;
}
Modified: trunk/src/jason/asSyntax/Structure.java
===================================================================
--- trunk/src/jason/asSyntax/Structure.java 2009-03-02 15:20:39 UTC (rev 1456)
+++ trunk/src/jason/asSyntax/Structure.java 2009-03-03 07:42:18 UTC (rev 1457)
@@ -71,7 +71,7 @@
/**
* Create a structure with a defined number of terms.
*
- * It is used by list term and atom to not create the array list for terms.
+ * It is used by list term, plan body, ... to not create the array list for terms.
*/
public Structure(String functor, int termsSize) {
super(functor);
@@ -126,8 +126,9 @@
return true;
}
- if (t instanceof Atom) {
- return super.equals(t);
+ if (t instanceof Atom && this.isAtom()) {
+ // consider atom equals only when this is an atom
+ return super.equals(t);
}
return false;
}
Modified: trunk/src/jason/stdlib/foreach.java
===================================================================
--- trunk/src/jason/stdlib/foreach.java 2009-03-02 15:20:39 UTC (rev 1456)
+++ trunk/src/jason/stdlib/foreach.java 2009-03-03 07:42:18 UTC (rev 1457)
@@ -112,12 +112,15 @@
checkArguments(args);
// get all solutions for the loop
- // Note: you should get all solutions here, otherwise I concurrent modification will occur for the iterator
+ // Note: you should get all solutions here, otherwise a 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());
+
+ if (allsol.isEmpty()) // none iteration
+ return true;
iu = allsol.iterator();
((Structure)foria.getBodyTerm()).addTerm(new ObjectTermImpl(iu));
} else if (args.length == 3) {
@@ -127,15 +130,18 @@
throw JasonException.createWrongArgumentNb(this);
}
- if (iu.hasNext()) {
- un.clear();
- un.compose(iu.next());
- PlanBody whattoadd = (PlanBody)args[1].clone();
+ un.clear();
+ un.compose(iu.next());
+
+ // add in the current intention:
+ // 1. the body argument of for and
+ // 2. a copy of the for internal action after the execution of the body
+ // (to perform the next iteration)
+ PlanBody whattoadd = (PlanBody)args[1].clone();
+ if (iu.hasNext())
whattoadd.add(new PlanBodyImpl(BodyType.internalAction, foria.getBodyTerm().clone()));
- whattoadd.setAsBodyTerm(false);
- foria.add(1,whattoadd);
- //System.out.println("new body="+foria.getBodyNext());
- }
+ whattoadd.setAsBodyTerm(false);
+ foria.add(1,whattoadd);
return true;
}
}
Modified: trunk/src/jason/stdlib/loop.java
===================================================================
--- trunk/src/jason/stdlib/loop.java 2009-03-02 15:20:39 UTC (rev 1456)
+++ trunk/src/jason/stdlib/loop.java 2009-03-03 07:42:18 UTC (rev 1457)
@@ -111,18 +111,20 @@
throw JasonException.createWrongArgumentNb(this);
}
- LogicalFormula logExpr = (LogicalFormula)args[0]; //.clone();
- //logExpr.apply(un); // need to apply since the internal action literal for while does not apply
+ LogicalFormula logExpr = (LogicalFormula)args[0];
+ // perform one iteration of the loop
Iterator<Unifier> iu = logExpr.logicalConsequence(ts.getAg(), un);
if (iu.hasNext()) {
-
un.compose(iu.next());
- PlanBody whattoadd = (PlanBody)args[1]; //.clone();
- whattoadd.add(new PlanBodyImpl(BodyType.internalAction, whileia.getBodyTerm().clone())); //(PlanBody)whileia.clone()); // add the while after
+ // add in the current intention:
+ // 1. the body argument and
+ // 2. a copy of the while internal action after the execution of the body
+ // (to test the loop again)
+ PlanBody whattoadd = (PlanBody)args[1];
+ whattoadd.add(new PlanBodyImpl(BodyType.internalAction, whileia.getBodyTerm().clone()));
whattoadd.setAsBodyTerm(false);
whileia.add(1,whattoadd);
- //System.out.println("new body="+whileia.getBodyNext());
}
return true;
}
Modified: trunk/src/jason/stdlib/term2string.java
===================================================================
--- trunk/src/jason/stdlib/term2string.java 2009-03-02 15:20:39 UTC (rev 1456)
+++ trunk/src/jason/stdlib/term2string.java 2009-03-03 07:42:18 UTC (rev 1457)
@@ -22,9 +22,9 @@
</ul>
<p>Examples:<ul>
- <li> <code>.substring(b,"b")</code>: true.
- <li> <code>.substring(b,X)</code>: unifies X to "b".
- <li> <code>.substring(X,"b")</code>: unified X to b.
+ <li> <code>.term2string(b,"b")</code>: true.
+ <li> <code>.term2string(b,X)</code>: unifies X with "b".
+ <li> <code>.term2string(X,"b")</code>: unified X with b.
</ul>
@see jason.stdlib.concat
Modified: trunk/src/test/TermTest.java
===================================================================
--- trunk/src/test/TermTest.java 2009-03-02 15:20:39 UTC (rev 1456)
+++ trunk/src/test/TermTest.java 2009-03-03 07:42:18 UTC (rev 1457)
@@ -77,6 +77,7 @@
assertEquals(l3, l4);
Term tpos = new Atom("pos");
+ assertFalse(l3.isAtom());
assertFalse(l3.equals(tpos));
assertFalse(tpos.equals(l3));
//System.out.println(new Term("pos")+"="+l3+" --> "+new Term("pos").equals(l3));
@@ -301,6 +302,7 @@
assertFalse(u.unifies(lp, a));
assertFalse(u.unifies(ln, a));
assertTrue(u.unifies(a, Literal.parseLiteral("s")));
+ assertTrue(Literal.parseLiteral("s").isAtom());
assertTrue(Literal.parseLiteral("s").equals(a));
assertTrue(u.unifies(Literal.parseLiteral("s"), a));
assertFalse(u.unifies(Literal.parseLiteral("~s"), a));
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|