|
From: <jom...@us...> - 2008-03-29 23:55:57
|
Revision: 1179
http://jason.svn.sourceforge.net/jason/?rev=1179&view=rev
Author: jomifred
Date: 2008-03-29 16:55:50 -0700 (Sat, 29 Mar 2008)
Log Message:
-----------
first implementation of body plan as term
Modified Paths:
--------------
trunk/applications/as-unit-test/src/jason/asunit/TestAgent.java
trunk/applications/as-unit-test/src/jason/asunit/TestArch.java
trunk/applications/as-unit-test/src/jason/tests/TestAll.java
trunk/src/jason/asSyntax/BodyLiteral.java
trunk/src/jason/asSyntax/DefaultTerm.java
trunk/src/jason/asSyntax/InternalActionLiteral.java
trunk/src/jason/asSyntax/ListTermImpl.java
trunk/src/jason/asSyntax/Plan.java
trunk/src/jason/asSyntax/Term.java
trunk/src/jason/asSyntax/parser/AS2JavaParser.jcc
trunk/src/jason/asSyntax/parser/as2j.java
trunk/src/jason/jeditplugin/JasonID.java
trunk/src/test/ASParserTest.java
trunk/src/test/PlanTest.java
Added Paths:
-----------
trunk/applications/as-unit-test/src/jason/tests/TestPlanbodyAsTerm.java
Modified: trunk/applications/as-unit-test/src/jason/asunit/TestAgent.java
===================================================================
--- trunk/applications/as-unit-test/src/jason/asunit/TestAgent.java 2008-03-28 18:07:59 UTC (rev 1178)
+++ trunk/applications/as-unit-test/src/jason/asunit/TestAgent.java 2008-03-29 23:55:50 UTC (rev 1179)
@@ -166,7 +166,6 @@
public void assertAct(final Structure act, final int maxCycles) {
Condition c = new Condition() {
public boolean test(TestArch arch) {
- //System.out.println(arch.getCycle() + " " + arch.getActions()+ getTS().getC().getFeedbackActions());
return arch.getActions().contains(act);
}
};
Modified: trunk/applications/as-unit-test/src/jason/asunit/TestArch.java
===================================================================
--- trunk/applications/as-unit-test/src/jason/asunit/TestArch.java 2008-03-28 18:07:59 UTC (rev 1178)
+++ trunk/applications/as-unit-test/src/jason/asunit/TestArch.java 2008-03-29 23:55:50 UTC (rev 1179)
@@ -5,6 +5,8 @@
import jason.asSemantics.ActionExec;
import jason.asSyntax.Literal;
import jason.asSyntax.Structure;
+import jason.asSyntax.Term;
+import jason.asSyntax.VarTerm;
import jason.environment.Environment;
import jason.infra.centralised.CentralisedAgArch;
import jason.infra.centralised.CentralisedEnvironment;
@@ -83,9 +85,18 @@
@Override
public void act(ActionExec action, List<ActionExec> feedback) {
+ Term t = action.getActionTerm();
+ if (t instanceof VarTerm) {
+ t = ((VarTerm)t).getValue();
+ }
actions.add(action.getActionTerm());
- if (getEnvInfraTier() != null)
+ System.out.println("*"+action.getActionTerm().getClass().getName()+" "+t.getClass().getName());
+ if (getEnvInfraTier() != null) {
super.act(action, feedback); //env.scheduleAction(getAgName(), action.getActionTerm(), action);
+ } else {
+ action.setResult(true);
+ feedback.add(action);
+ }
}
public void print(String s) {
Modified: trunk/applications/as-unit-test/src/jason/tests/TestAll.java
===================================================================
--- trunk/applications/as-unit-test/src/jason/tests/TestAll.java 2008-03-28 18:07:59 UTC (rev 1178)
+++ trunk/applications/as-unit-test/src/jason/tests/TestAll.java 2008-03-29 23:55:50 UTC (rev 1179)
@@ -9,6 +9,7 @@
BugVarsInInitBels.class,
TestAddLogExprInBB.class,
TestKQML.class,
- TestVarInContext.class
+ TestVarInContext.class,
+ TestPlanbodyAsTerm.class
})
public class TestAll { }
Added: trunk/applications/as-unit-test/src/jason/tests/TestPlanbodyAsTerm.java
===================================================================
--- trunk/applications/as-unit-test/src/jason/tests/TestPlanbodyAsTerm.java (rev 0)
+++ trunk/applications/as-unit-test/src/jason/tests/TestPlanbodyAsTerm.java 2008-03-29 23:55:50 UTC (rev 1179)
@@ -0,0 +1,34 @@
+package jason.tests;
+
+import jason.asunit.TestAgent;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class TestPlanbodyAsTerm {
+
+ TestAgent ag;
+
+ // initialisation of the agent test
+ @Before
+ public void setupAg() {
+ ag = new TestAgent();
+
+ // defines the agent's AgentSpeak code
+ ag.parseAScode(
+ "+!start <- +g(a; b; c); ?g(X); !g(X). "+
+ "+!g(A; R) <- A; !g(R). "+
+ "+!g(A) <- .print(A); A; .print(end)."
+ );
+ }
+
+ @Test
+ public void testProgram() {
+ ag.addGoal("start");
+ ag.assertBel("g(a;b;c)", 5);
+ ag.assertAct("a", 4);
+ ag.assertAct("b", 4);
+ ag.assertAct("c", 4);
+ }
+
+}
Modified: trunk/src/jason/asSyntax/BodyLiteral.java
===================================================================
--- trunk/src/jason/asSyntax/BodyLiteral.java 2008-03-28 18:07:59 UTC (rev 1178)
+++ trunk/src/jason/asSyntax/BodyLiteral.java 2008-03-29 23:55:50 UTC (rev 1179)
@@ -1,40 +1,14 @@
-//----------------------------------------------------------------------------
-// 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.asSyntax;
+import java.util.Iterator;
+
import org.w3c.dom.Document;
import org.w3c.dom.Element;
-/**
- * Represents a plan body item (achieve, test, action, ...) and its successors,
- * it is thus like a list term.
- *
- * @author Jomi
- */
-public class BodyLiteral extends ListTermImpl implements Cloneable {
+public class BodyLiteral extends Structure implements Iterable<BodyLiteral> {
public enum BodyType {
+ none { public String toString() { return ""; }},
action { public String toString() { return ""; }},
internalAction { public String toString() { return ""; }},
achieve { public String toString() { return "!"; }},
@@ -46,28 +20,100 @@
constraint { public String toString() { return ""; }}
}
- private BodyType formType;
+ public static final String BODY_PLAN_FUNCTOR = ";";
+
+ private Term term = null;
+ private BodyLiteral next = null;
+ private BodyType formType = BodyType.none;
+ /** constructor for empty plan body */
public BodyLiteral() {
+ super(BODY_PLAN_FUNCTOR, 0);
}
public BodyLiteral(BodyType t, Term b) {
- setTerm(b);
+ super(BODY_PLAN_FUNCTOR, 0);
+ term = b;
+ formType = t;
setSrc(b);
- formType = t;
}
+ public void setNext(BodyLiteral next) {
+ this.next = next;
+ }
+ public BodyLiteral getNext() {
+ return next;
+ }
+
+ public boolean isEmpty() {
+ return term == null;
+ }
+
public BodyType getType() {
return formType;
}
-
+
+ public Term getTerm() {
+ return term;
+ }
+
public Literal getLiteralFormula() {
- Term t = getTerm();
- if (t instanceof Literal)
- return (Literal)t;
+ if (term instanceof Literal)
+ return (Literal)term;
else
return null;
}
+
+ public Iterator<BodyLiteral> iterator() {
+ return new Iterator<BodyLiteral>() {
+ BodyLiteral current = BodyLiteral.this;
+ public boolean hasNext() {
+ return current != null && current.term != null && current.next != null;
+ }
+ public BodyLiteral next() {
+ BodyLiteral r = current;
+ if (current != null)
+ current = current.next;
+ return r;
+ }
+ public void remove() { }
+ };
+ }
+
+ // Override some structure methods to work with unification/equals
+ @Override
+ public int getArity() {
+ if (term == null)
+ return 0;
+ else if (next == null)
+ return 1;
+ else
+ return 2;
+ }
+
+ @Override
+ public Term getTerm(int i) {
+ if (i == 0) return term;
+ if (i == 1) {
+ if (next != null && next.term.isVar() && next.next == null)
+ // if next is the last VAR, return that var
+ return next.term;
+ else
+ return next;
+ }
+ return null;
+ }
+
+ @Override
+ public void setTerm(int i, Term t) {
+ if (i == 0) term = t;
+ if (i == 1) System.out.println("Should not set next of body literal!");
+ }
+
+ @Override
+ public boolean isPlanBody() {
+ return true;
+ }
@Override
public boolean equals(Object o) {
@@ -81,36 +127,82 @@
}
@Override
- public int hashCode() {
- return formType.hashCode() + super.hashCode();
+ public int calcHashCode() {
+ return formType.hashCode() + super.calcHashCode();
}
- public Object clone() {
- BodyLiteral c;
- Term t = getTerm();
- if (t == null) { // empty body
- c = new BodyLiteral();
- } else {
- c = new BodyLiteral(formType, (Term)t.clone());
- c.setNext((Term)getNext().clone());
+ public boolean add(BodyLiteral bl) {
+ if (term == null)
+ swap(bl);
+ else if (next == null)
+ next = bl;
+ else
+ next.add(bl);
+ return true;
+ }
+
+ public boolean add(int index, BodyLiteral bl) {
+ if (index == 0) {
+ swap(bl);
+ this.next = bl;
+ } else {
+ next.add(index - 1, bl);
}
- return c;
+ return true;
}
- @Override
- protected void setValuesFrom(ListTerm lt) {
- super.setValuesFrom(lt);
- formType = ((BodyLiteral)lt).formType;
+ public Term remove(int index) {
+ if (index == 0) {
+ if (next == null) {
+ term = null; // becomes an empty
+ } else {
+ Term oldvalue = term;
+ swap(next); // get values of text
+ next = next.next;
+ return oldvalue;
+ }
+ return this;
+ } else {
+ return next.remove(index - 1);
+ }
}
+
+ public int size() {
+ if (term == null)
+ return 0;
+ else if (next == null)
+ return 1;
+ else
+ return next.size() + 1;
+ }
+
+ private void swap(BodyLiteral bl) {
+ BodyType bt = this.formType;
+ this.formType = bl.formType;
+ bl.formType = bt;
+
+ Term l = this.term;
+ this.term = bl.term;
+ bl.term = l;
+ }
+
+ public Object clone() {
+ if (term == null) // empty
+ return new BodyLiteral();
+
+ BodyLiteral c = new BodyLiteral(formType, (Term)term.clone());
+ if (next != null)
+ c.setNext((BodyLiteral)getNext().clone());
+ return c;
+ }
-
public String toString() {
- if (isEmpty())
+ if (term == null)
return "";
- else if (getNext().isEmpty())
- return formType.toString() + getTerm() + ".";
+ else if (next == null)
+ return formType.toString() + term;
else
- return formType.toString() + getTerm() + "; " + getNext();
+ return formType.toString() + term + "; " + next;
}
/** get as XML */
@@ -119,7 +211,7 @@
if (formType.toString().length() > 0) {
u.setAttribute("type", formType.toString());
}
- u.appendChild( ((Structure)getTerm()).getAsDOM(document));
+ u.appendChild( ((Structure)term).getAsDOM(document));
return u;
}
-}
+}
\ No newline at end of file
Modified: trunk/src/jason/asSyntax/DefaultTerm.java
===================================================================
--- trunk/src/jason/asSyntax/DefaultTerm.java 2008-03-28 18:07:59 UTC (rev 1178)
+++ trunk/src/jason/asSyntax/DefaultTerm.java 2008-03-29 23:55:50 UTC (rev 1179)
@@ -63,6 +63,7 @@
public boolean isPred() { return false; }
public boolean isStructure() { return false; }
public boolean isAtom() { return false; }
+ public boolean isPlanBody() { return false; }
public boolean isGround() { return true; }
public boolean hasVar(VarTerm t) { return false; }
Modified: trunk/src/jason/asSyntax/InternalActionLiteral.java
===================================================================
--- trunk/src/jason/asSyntax/InternalActionLiteral.java 2008-03-28 18:07:59 UTC (rev 1178)
+++ trunk/src/jason/asSyntax/InternalActionLiteral.java 2008-03-29 23:55:50 UTC (rev 1179)
@@ -117,7 +117,7 @@
public Object clone() {
InternalActionLiteral c = new InternalActionLiteral(this);
c.predicateIndicatorCache = this.predicateIndicatorCache;
- c.hashCodeCache = this.hashCodeCache;
+ c.hashCodeCache = this.hashCodeCache;
return c;
}
Modified: trunk/src/jason/asSyntax/ListTermImpl.java
===================================================================
--- trunk/src/jason/asSyntax/ListTermImpl.java 2008-03-28 18:07:59 UTC (rev 1178)
+++ trunk/src/jason/asSyntax/ListTermImpl.java 2008-03-29 23:55:50 UTC (rev 1179)
@@ -280,8 +280,7 @@
*/
public ListTerm concat(ListTerm lt) {
if (isEmpty()) {
- term = lt.getTerm();
- next = (Term)lt.getNext();
+ setValuesFrom(lt);
} else if (((ListTerm)next).isEmpty() ) {
next = (Term)lt;
} else {
@@ -420,7 +419,7 @@
public void add(int index, Term o) {
if (index == 0) {
- ListTermImpl n = new ListTermImpl(term,next);
+ ListTerm n = new ListTermImpl(term,next);
this.term = o;
this.next = n;
} else if (index > 0 && getNext() != null) {
Modified: trunk/src/jason/asSyntax/Plan.java
===================================================================
--- trunk/src/jason/asSyntax/Plan.java 2008-03-28 18:07:59 UTC (rev 1178)
+++ trunk/src/jason/asSyntax/Plan.java 2008-03-29 23:55:50 UTC (rev 1179)
@@ -30,7 +30,6 @@
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
@@ -235,7 +234,8 @@
public String toASString() {
return ((label == null) ? "" : "@" + label + " ") +
tevent + ((context == null) ? "" : " : " + context) +
- " <- " + body;
+ (body.isEmpty() ? "" : " <- " + body) +
+ ".";
}
/** get as XML */
@@ -256,10 +256,15 @@
if (body.size() > 0) {
Element eb = (Element) document.createElement("body");
+ for (BodyLiteral bl: body) {
+ eb.appendChild(bl.getAsDOM(document));
+ }
+ /*
Iterator<ListTerm> i = body.listTermIterator();
while (i.hasNext()) {
eb.appendChild(i.next().getAsDOM(document));
}
+ */
u.appendChild(eb);
}
Modified: trunk/src/jason/asSyntax/Term.java
===================================================================
--- trunk/src/jason/asSyntax/Term.java 2008-03-28 18:07:59 UTC (rev 1178)
+++ trunk/src/jason/asSyntax/Term.java 2008-03-29 23:55:50 UTC (rev 1179)
@@ -35,6 +35,8 @@
public boolean isAtom();
+ public boolean isPlanBody();
+
public boolean hasVar(VarTerm t);
public void countVars(Map<VarTerm, Integer> c);
Modified: trunk/src/jason/asSyntax/parser/AS2JavaParser.jcc
===================================================================
--- trunk/src/jason/asSyntax/parser/AS2JavaParser.jcc 2008-03-28 18:07:59 UTC (rev 1178)
+++ trunk/src/jason/asSyntax/parser/AS2JavaParser.jcc 2008-03-29 23:55:50 UTC (rev 1179)
@@ -171,7 +171,7 @@
Literal g;
Plan p;
curAg = a;
- if (a != null) asSource = a.getASLSrc();
+ if (a != null && a.getASLSrc() != null) asSource = a.getASLSrc();
boolean endDir = false;
}
{
@@ -229,6 +229,7 @@
{
Pred dir = null;
Agent resultOfDirective = null;
+ Agent bakAg = curAg;
boolean isEOF = false;
}
{
@@ -253,6 +254,7 @@
// import bels, plans and initial goals from agent resultOfDirective
outerAg.importComponents(resultOfDirective);
}
+ curAg = bakAg;
return false;
}
}
@@ -286,13 +288,14 @@
/* Plan */
Plan plan() : { Token k; Pred L = null;
- Trigger T; Object C = null;
- BodyLiteral B = null;
+ Trigger T;
+ Object C = null; BodyLiteral bl = null;
+ Object B = null;
int start = -1, end;}
{
[ k = <TK_LABEL_AT> L=pred() { start = k.beginLine; } ]
T=trigger()
- [ k = ":" C=log_expr() { if (start == -1) start = k.beginLine; } ]
+ [ k = ":" C = log_expr() { if (start == -1) start = k.beginLine; } ]
[ k = "<-" B = plan_body() { if (start == -1) start = k.beginLine; } ]
k = "." { if (start == -1) start = k.beginLine; }
{ end = k.beginLine;
@@ -300,7 +303,14 @@
try { ial = checkInternalActionsInContext((LogicalFormula)C, curAg); } catch (Exception e) {}
if (ial != null)
throw new ParseException(getSourceRef(ial)+" The internal action '"+ial+"' can not be used in plan's context!");
- Plan p = new Plan(L,T,(LogicalFormula)C,B);
+ if (B != null) {
+ if (!(B instanceof BodyLiteral))
+ throw new ParseException(getSourceRef(B)+" Unknown body formula:"+B);
+ bl = (BodyLiteral)B;
+ if (bl.getTerm().equals(Literal.LTrue))
+ bl = (BodyLiteral)bl.getNext();
+ }
+ Plan p = new Plan(L,T,(LogicalFormula)C, bl);
p.setSrcLines(start,end);
p.setSrc(asSource);
return p;
@@ -352,23 +362,22 @@
/* Plan body */
-BodyLiteral plan_body() : { BodyLiteral F; BodyLiteral R = null; }
+Object plan_body() : { Object F; Object R = null; }
{
F = body_formula()
- [ ";" R = plan_body() ]
- { if (F.getTerm().equals(Literal.LTrue))
- return R;
- if (R == null)
- F.setNext(new BodyLiteral());
- else
- F.setNext(R);
+ [ ";" { if (!(F instanceof BodyLiteral)) throw new ParseException(getSourceRef(F)+" "+F+" is not a body literal!"); }
+ R = plan_body() { if (!(R instanceof BodyLiteral)) throw new ParseException(getSourceRef(R)+" "+R+" is not a body literal!"); }
+ ]
+ { if (F instanceof BodyLiteral && R instanceof BodyLiteral) {
+ ((BodyLiteral)F).setNext( (BodyLiteral)R );
+ }
return F;
}
}
-BodyLiteral body_formula() :
+Object body_formula() :
{ BodyType formType = BodyType.action; Object B; }
{
[ "!" { formType = BodyType.achieve; }
@@ -395,7 +404,7 @@
else
throw new ParseException(getSourceRef(B)+" The argument for ? is not a logical formula.");
} else {
- throw new ParseException(getSourceRef(B)+" Unknown body formula.");
+ return B;
}
}
}
@@ -440,9 +449,17 @@
Term term() : { Object o;}
{
( o=list()
- | o=log_expr() // log expr includes literals/atoms/structures
+ | o=plan_body() // plan_body includes literals/atoms/structures
)
- { return changeToAtom(o); }
+ { // if the result is a BodyLiteral action with size = 1, it is indeed a literal and not a body literal
+ if (o instanceof BodyLiteral) {
+ BodyLiteral bl = (BodyLiteral)o;
+ if (bl.getType() == BodyType.action && bl.size() == 1) {
+ o = bl.getTerm();
+ }
+ }
+ return changeToAtom(o);
+ }
}
@@ -462,7 +479,7 @@
"]" { return lt; }
}
-// term_in_list is the same as term, but log_expr must be enclosed by "("....")" to avoid problem with |
+// term_in_list is the same as term, but log_expr/plan_body must be enclosed by "("....")" to avoid problem with |
Term term_in_list() : { Object o; }
{
( o=list()
Modified: trunk/src/jason/asSyntax/parser/as2j.java
===================================================================
--- trunk/src/jason/asSyntax/parser/as2j.java 2008-03-28 18:07:59 UTC (rev 1178)
+++ trunk/src/jason/asSyntax/parser/as2j.java 2008-03-29 23:55:50 UTC (rev 1179)
@@ -89,7 +89,7 @@
Literal g;
Plan p;
curAg = a;
- if (a != null) asSource = a.getASLSrc();
+ if (a != null && a.getASLSrc() != null) asSource = a.getASLSrc();
boolean endDir = false;
label_1:
while (true) {
@@ -237,6 +237,7 @@
final public boolean directive(Agent outerAg) throws ParseException, jason.JasonException {
Pred dir = null;
Agent resultOfDirective = null;
+ Agent bakAg = curAg;
boolean isEOF = false;
if (jj_2_1(4)) {
jj_consume_token(27);
@@ -270,6 +271,7 @@
// import bels, plans and initial goals from agent resultOfDirective
outerAg.importComponents(resultOfDirective);
}
+ curAg = bakAg;
{if (true) return false;}
throw new Error("Missing return statement in function");
}
@@ -313,8 +315,9 @@
/* Plan */
final public Plan plan() throws ParseException {
Token k; Pred L = null;
- Trigger T; Object C = null;
- BodyLiteral B = null;
+ Trigger T;
+ Object C = null; BodyLiteral bl = null;
+ Object B = null;
int start = -1, end;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case TK_LABEL_AT:
@@ -354,7 +357,14 @@
try { ial = checkInternalActionsInContext((LogicalFormula)C, curAg); } catch (Exception e) {}
if (ial != null)
{if (true) throw new ParseException(getSourceRef(ial)+" The internal action '"+ial+"' can not be used in plan's context!");}
- Plan p = new Plan(L,T,(LogicalFormula)C,B);
+ if (B != null) {
+ if (!(B instanceof BodyLiteral))
+ {if (true) throw new ParseException(getSourceRef(B)+" Unknown body formula:"+B);}
+ bl = (BodyLiteral)B;
+ if (bl.getTerm().equals(Literal.LTrue))
+ bl = (BodyLiteral)bl.getNext();
+ }
+ Plan p = new Plan(L,T,(LogicalFormula)C, bl);
p.setSrcLines(start,end);
p.setSrc(asSource);
{if (true) return p;}
@@ -469,29 +479,28 @@
}
/* Plan body */
- final public BodyLiteral plan_body() throws ParseException {
- BodyLiteral F; BodyLiteral R = null;
+ final public Object plan_body() throws ParseException {
+ Object F; Object R = null;
F = body_formula();
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case 37:
jj_consume_token(37);
+ if (!(F instanceof BodyLiteral)) {if (true) throw new ParseException(getSourceRef(F)+" "+F+" is not a body literal!");}
R = plan_body();
+ if (!(R instanceof BodyLiteral)) {if (true) throw new ParseException(getSourceRef(R)+" "+R+" is not a body literal!");}
break;
default:
jj_la1[19] = jj_gen;
;
}
- if (F.getTerm().equals(Literal.LTrue))
- {if (true) return R;}
- if (R == null)
- F.setNext(new BodyLiteral());
- else
- F.setNext(R);
+ if (F instanceof BodyLiteral && R instanceof BodyLiteral) {
+ ((BodyLiteral)F).setNext( (BodyLiteral)R );
+ }
{if (true) return F;}
throw new Error("Missing return statement in function");
}
- final public BodyLiteral body_formula() throws ParseException {
+ final public Object body_formula() throws ParseException {
BodyType formType = BodyType.action; Object B;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case 31:
@@ -553,7 +562,7 @@
else
{if (true) throw new ParseException(getSourceRef(B)+" The argument for ? is not a logical formula.");}
} else {
- {if (true) throw new ParseException(getSourceRef(B)+" Unknown body formula.");}
+ {if (true) return B;}
}
}
throw new Error("Missing return statement in function");
@@ -644,15 +653,26 @@
case STRING:
case ATOM:
case UNNAMEDVAR:
+ case 31:
+ case 34:
case 35:
+ case 36:
+ case 38:
case 39:
- o = log_expr();
+ o = plan_body();
break;
default:
jj_la1[27] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
+ // if the result is a BodyLiteral action with size = 1, it is indeed a literal and not a body literal
+ if (o instanceof BodyLiteral) {
+ BodyLiteral bl = (BodyLiteral)o;
+ if (bl.getType() == BodyType.action && bl.size() == 1) {
+ o = bl.getTerm();
+ }
+ }
{if (true) return changeToAtom(o);}
throw new Error("Missing return statement in function");
}
@@ -726,7 +746,7 @@
throw new Error("Missing return statement in function");
}
-// term_in_list is the same as term, but log_expr must be enclosed by "("....")" to avoid problem with |
+// term_in_list is the same as term, but log_expr/plan_body must be enclosed by "("....")" to avoid problem with |
final public Term term_in_list() throws ParseException {
Object o;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
@@ -1163,11 +1183,6 @@
finally { jj_save(0, xla); }
}
- final private boolean jj_3R_12() {
- if (jj_scan_token(39)) return true;
- return false;
- }
-
final private boolean jj_3_1() {
if (jj_scan_token(27)) return true;
if (jj_scan_token(TK_BEGIN)) return true;
@@ -1176,6 +1191,16 @@
return false;
}
+ final private boolean jj_3R_14() {
+ if (jj_scan_token(42)) return true;
+ return false;
+ }
+
+ final private boolean jj_3R_12() {
+ if (jj_scan_token(39)) return true;
+ return false;
+ }
+
final private boolean jj_3R_11() {
Token xsp;
xsp = jj_scanpos;
@@ -1193,11 +1218,6 @@
return false;
}
- final private boolean jj_3R_14() {
- if (jj_scan_token(42)) return true;
- return false;
- }
-
final private boolean jj_3R_13() {
if (jj_3R_14()) return true;
return false;
@@ -1220,10 +1240,10 @@
jj_la1_1();
}
private static void jj_la1_0() {
- jj_la1_0 = new int[] {0x8000000,0x10cb00,0x8000000,0x80000000,0x8000000,0x10000,0x10cb00,0x8000000,0x8000000,0x20000000,0x10000,0x0,0x0,0x0,0x80000000,0x80000000,0x30cb80,0x800,0x10cb00,0x0,0x0,0x80000000,0x80000000,0x10c000,0x0,0x0,0x0,0x3acf80,0x0,0x200080,0x0,0x3acb80,0x3acb80,0x0,0x0,0x3acf80,0x3acb80,0x0,0x3acb80,0x0,0x0,0x0,0x3000,0x3000,0x0,0x32cb80,0x200080,0x0,};
+ jj_la1_0 = new int[] {0x8000000,0x10cb00,0x8000000,0x80000000,0x8000000,0x10000,0x10cb00,0x8000000,0x8000000,0x20000000,0x10000,0x0,0x0,0x0,0x80000000,0x80000000,0x30cb80,0x800,0x10cb00,0x0,0x0,0x80000000,0x80000000,0x10c000,0x0,0x0,0x0,0x803acf80,0x0,0x200080,0x0,0x3acb80,0x3acb80,0x0,0x0,0x3acf80,0x3acb80,0x0,0x3acb80,0x0,0x0,0x0,0x3000,0x3000,0x0,0x32cb80,0x200080,0x0,};
}
private static void jj_la1_1() {
- jj_la1_1 = new int[] {0x0,0x0,0x0,0x0,0x0,0xc,0x0,0x0,0x0,0x0,0x0,0x1,0x2,0xc,0x10,0x10,0x0,0x0,0x0,0x20,0x4,0x5c,0x5c,0x0,0x80,0x400,0x200,0x488,0x200,0x400,0x800,0x488,0x488,0x800,0x2000,0x88,0x88,0x3fc000,0x488,0x3fc000,0xc,0xc,0xc00000,0xc00000,0x1000000,0x88,0x0,0x400,};
+ jj_la1_1 = new int[] {0x0,0x0,0x0,0x0,0x0,0xc,0x0,0x0,0x0,0x0,0x0,0x1,0x2,0xc,0x10,0x10,0x0,0x0,0x0,0x20,0x4,0x5c,0x5c,0x0,0x80,0x400,0x200,0x4dc,0x200,0x400,0x800,0x488,0x488,0x800,0x2000,0x88,0x88,0x3fc000,0x488,0x3fc000,0xc,0xc,0xc00000,0xc00000,0x1000000,0x88,0x0,0x400,};
}
final private JJCalls[] jj_2_rtns = new JJCalls[1];
private boolean jj_rescan = false;
Modified: trunk/src/jason/jeditplugin/JasonID.java
===================================================================
--- trunk/src/jason/jeditplugin/JasonID.java 2008-03-28 18:07:59 UTC (rev 1178)
+++ trunk/src/jason/jeditplugin/JasonID.java 2008-03-29 23:55:50 UTC (rev 1179)
@@ -31,6 +31,8 @@
import jason.mas2j.parser.TokenMgrError;
import jason.runtime.OutputStreamAdapter;
import jason.util.asl2html;
+import jason.util.asl2tex;
+import jason.util.asl2xml;
import java.awt.BorderLayout;
import java.awt.Dimension;
@@ -77,7 +79,6 @@
import errorlist.DefaultErrorSource;
import errorlist.ErrorSource;
-import jason.util.*;
public class JasonID extends JPanel implements EBComponent, RunProjectListener {
@@ -379,7 +380,7 @@
return null;
}
- private boolean parseProjectAS(MAS2JProject project) {
+ protected boolean parseProjectAS(MAS2JProject project) {
// compile
File asFile = null;
try {
@@ -456,16 +457,14 @@
max++;
try {
Thread.sleep(200);
- } catch (Exception e) {
- }
+ } catch (Exception e) { }
}
}
MAS2JProject project = parseProject(b);
- if (project == null || !parseProjectAS(project)) {
+ if (project == null) // || !parseProjectAS(project)) {
return;
- }
-
+
// launch the MAS
animation.start();
btStop.setEnabled(true);
@@ -636,9 +635,8 @@
return;
}
MAS2JProject project = parseProject(b);
- if (project == null || !parseProjectAS(project)) {
+ if (project == null) // || !parseProjectAS(project)) {
return;
- }
CentralisedMASLauncherAnt script = new CentralisedMASLauncherAnt("jar");
script.setProject(project);
if (script.writeScripts(false)) {
@@ -653,9 +651,8 @@
return;
}
MAS2JProject project = parseProject(b);
- if (project == null || !parseProjectAS(project)) {
+ if (project == null) // || !parseProjectAS(project)) {
return;
- }
CentralisedMASLauncherAnt script = new CentralisedMASLauncherAnt("jnlp");
script.setProject(project);
if (script.writeScripts(false)) {
Modified: trunk/src/test/ASParserTest.java
===================================================================
--- trunk/src/test/ASParserTest.java 2008-03-28 18:07:59 UTC (rev 1178)
+++ trunk/src/test/ASParserTest.java 2008-03-29 23:55:50 UTC (rev 1179)
@@ -5,6 +5,7 @@
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
import jason.asSyntax.BodyLiteral;
+import jason.asSyntax.Literal;
import jason.asSyntax.LogExpr;
import jason.asSyntax.LogicalFormula;
import jason.asSyntax.NumberTerm;
@@ -163,6 +164,19 @@
}
+ public void testParsingPlanBody() {
+ Literal l = Literal.parseLiteral("p(a1;a2, a3, !g, ?b;.print(oi), 10)");
+ assertEquals(5,l.getArity());
+ assertTrue(l.getTerm(0) instanceof BodyLiteral);
+ assertTrue(l.getTerm(0).isPlanBody());
+
+ assertFalse(l.getTerm(1).isPlanBody());
+ assertTrue(l.getTerm(2).isPlanBody());
+ assertTrue(l.getTerm(3).isPlanBody());
+ assertFalse(l.getTerm(4).isPlanBody());
+
+ }
+
public void testParsingAllSources() {
parseDir(new File("./examples"));
parseDir(new File("./demos"));
@@ -192,7 +206,8 @@
parser.mas();
}
} catch (Exception e) {
- fail("Error parsing "+f);
+ e.printStackTrace();
+ fail("Error parsing "+f+": "+e);
}
}
}
Modified: trunk/src/test/PlanTest.java
===================================================================
--- trunk/src/test/PlanTest.java 2008-03-28 18:07:59 UTC (rev 1178)
+++ trunk/src/test/PlanTest.java 2008-03-29 23:55:50 UTC (rev 1179)
@@ -1,11 +1,13 @@
package test;
import jason.JasonException;
+import jason.asSemantics.Unifier;
import jason.asSyntax.BodyLiteral;
-import jason.asSyntax.ListTerm;
import jason.asSyntax.Plan;
import jason.asSyntax.PlanLibrary;
import jason.asSyntax.Trigger;
+import jason.asSyntax.VarTerm;
+import jason.asSyntax.BodyLiteral.BodyType;
import jason.asSyntax.parser.ParseException;
import java.util.Iterator;
@@ -54,7 +56,7 @@
public void testParser() {
Plan p = Plan.parse("+te : a & b <- a1; a2; .print(a); !g1; !!g2; ?test1; 10 > 3; +b1; -b2; -+b3.");
p = (Plan)p.clone();
- Iterator<ListTerm> i = p.getBody().listTermIterator();
+ Iterator<BodyLiteral> i = p.getBody().iterator();
assertEquals( BodyLiteral.BodyType.action, ((BodyLiteral)i.next()).getType());
assertEquals( BodyLiteral.BodyType.action, ((BodyLiteral)i.next()).getType());
assertEquals( BodyLiteral.BodyType.internalAction, ((BodyLiteral)i.next()).getType());
@@ -80,4 +82,18 @@
p.getBody().remove(0); // 1
assertTrue(p.getBody().isEmpty());
}
+
+ public void testUnifyBody() {
+ Plan p1 = Plan.parse("+te : a & b <- !a1; ?a2; .print(a); !g1.");
+ BodyLiteral bl = new BodyLiteral(BodyType.action, new VarTerm("A1"));
+ bl.add(new BodyLiteral(BodyType.action, new VarTerm("A2")));
+ bl.add(new BodyLiteral(BodyType.action, new VarTerm("A3")));
+ assertEquals(p1.getBody().getArity(), bl.getArity());
+ Unifier u = new Unifier();
+ assertTrue(u.unifies(p1.getBody(), bl));
+ assertEquals("a1", u.get("A1").toString());
+ assertEquals("a2", u.get("A2").toString());
+ assertEquals(".print(a); !g1", u.get("A3").toString());
+ }
+
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|