|
From: <jom...@us...> - 2009-03-16 22:06:52
|
Revision: 1470
http://jason.svn.sourceforge.net/jason/?rev=1470&view=rev
Author: jomifred
Date: 2009-03-16 22:06:38 +0000 (Mon, 16 Mar 2009)
Log Message:
-----------
improve performance by avoiding several clone of plans
. intended means does not clone the plan
Modified Paths:
--------------
trunk/applications/as-unit-test/src/jason/tests/TestIF.java
trunk/examples/auction/auctioneer.asl
trunk/src/jason/asSemantics/IntendedMeans.java
trunk/src/jason/asSemantics/TransitionSystem.java
trunk/src/jason/asSyntax/InternalActionLiteral.java
trunk/src/jason/asSyntax/PlanBody.java
trunk/src/jason/asSyntax/PlanBodyImpl.java
trunk/src/jason/asSyntax/VarTerm.java
trunk/src/jason/stdlib/foreach.java
trunk/src/jason/stdlib/if_then_else.java
trunk/src/jason/stdlib/loop.java
Modified: trunk/applications/as-unit-test/src/jason/tests/TestIF.java
===================================================================
--- trunk/applications/as-unit-test/src/jason/tests/TestIF.java 2009-03-16 14:35:31 UTC (rev 1469)
+++ trunk/applications/as-unit-test/src/jason/tests/TestIF.java 2009-03-16 22:06:38 UTC (rev 1470)
@@ -42,7 +42,7 @@
ag.assertPrint("no", 5);
ag.assertAct("a2", 5);
}
-
+
@Test
public void testUnifiyInThenElse() {
ag.addGoal("test3");
Modified: trunk/examples/auction/auctioneer.asl
===================================================================
--- trunk/examples/auction/auctioneer.asl 2009-03-16 14:35:31 UTC (rev 1469)
+++ trunk/examples/auction/auctioneer.asl 2009-03-16 22:06:38 UTC (rev 1470)
@@ -16,7 +16,7 @@
+place_bid(N,V)[source(S)]
: auction(N) & winner(N,CurWin,CurVl) & V > CurVl
<- -winner(N,CurWin,CurVl);
- +winner(N,S,V);
+ +winner(N,S,V); .print("New winner is ",S, " with value ",V);
!check_end(N).
@pb2[atomic]
Modified: trunk/src/jason/asSemantics/IntendedMeans.java
===================================================================
--- trunk/src/jason/asSemantics/IntendedMeans.java 2009-03-16 14:35:31 UTC (rev 1469)
+++ trunk/src/jason/asSemantics/IntendedMeans.java 2009-03-16 22:06:38 UTC (rev 1470)
@@ -43,40 +43,48 @@
private static final long serialVersionUID = 1L;
- protected Unifier unif = null;
- protected Plan plan;
- private Trigger trigger; // the trigger which created this IM
+ protected Unifier unif = null;
+ protected PlanBody planBody;
+ protected Plan plan;
+ private Trigger trigger; // the trigger which created this IM
public IntendedMeans(Option opt, Trigger te) {
- plan = opt.getPlan().cloneOnlyBody();
- unif = opt.getUnifier(); //(Unifier)opt.getUnifier().clone();
+ plan = opt.getPlan();
+ planBody = plan.getBody();
+ unif = opt.getUnifier();
if (te == null) {
- trigger = plan.getTrigger();
+ trigger = plan.getTrigger().clone();
} else {
- trigger = (Trigger)te.clone();
- trigger.getLiteral().apply(unif);
+ trigger = te;
}
+ trigger.apply(unif);
}
- private IntendedMeans() {
- // used for clone
- }
+ // used by clone
+ private IntendedMeans() { }
/** removes the current action of the IM and returns the term of the body */
public Term removeCurrentStep() {
- PlanBody current = plan.getBody();
- if (current.isEmptyBody()) {
+ if (isFinished()) {
return null;
} else {
- return current.removeBody(0);
+ Term r = planBody.getBodyTerm();
+ planBody = planBody.getBodyNext();
+ return r;
}
}
public PlanBody getCurrentStep() {
- return plan.getBody();
+ return planBody;
}
+ // used by if/for/loop internal actions
+ public PlanBody insertAsNextStep(PlanBody pb2add) {
+ planBody = new PlanBodyImpl(planBody.getBodyType(), planBody.getBodyTerm());
+ planBody.setBodyNext(pb2add);
+ return planBody;
+ }
public Plan getPlan() {
return plan;
@@ -103,7 +111,7 @@
}
public boolean isFinished() {
- return plan.getBody().isEmptyBody();
+ return planBody == null || planBody.isEmptyBody();
}
public boolean isGoalAdd() {
@@ -112,23 +120,23 @@
public Object clone() {
IntendedMeans c = new IntendedMeans();
- c.unif = this.unif.clone();
- c.plan = (Plan)this.plan.clone();
- c.trigger = (Trigger)this.trigger.clone();
+ c.unif = this.unif.clone();
+ c.planBody = this.planBody.clonePB();
+ c.trigger = this.trigger.clone();
return c;
}
public String toString() {
- return plan + " / " + unif;
+ return planBody + " / " + unif;
}
public Term getAsTerm() {
Structure im = new Structure("im");
im.addTerm(new StringTermImpl(plan.getLabel().toString()));
- if (plan.getBody() instanceof PlanBodyImpl) {
+ if (planBody instanceof PlanBodyImpl) {
ListTerm lt = new ListTermImpl();
- for (PlanBody bd: (PlanBodyImpl)plan.getBody()) {
- PlanBody c = (PlanBody)bd.clone();
+ for (PlanBody bd: (PlanBodyImpl)planBody) {
+ PlanBody c = bd.clonePB();
c.apply(unif);
lt.add(new StringTermImpl(c.getBodyType().toString()+c.getBodyTerm()));
}
@@ -141,8 +149,8 @@
public Element getAsDOM(Document document) {
Element eim = (Element) document.createElement("intended-means");
eim.setAttribute("trigger", trigger.toString());
- if (plan != null) {
- eim.appendChild(plan.getAsDOM(document));
+ if (planBody != null) {
+ eim.appendChild(planBody.getAsDOM(document));
}
if (unif != null && unif.size() > 0) {
eim.appendChild(unif.getAsDOM(document));
Modified: trunk/src/jason/asSemantics/TransitionSystem.java
===================================================================
--- trunk/src/jason/asSemantics/TransitionSystem.java 2009-03-16 14:35:31 UTC (rev 1469)
+++ trunk/src/jason/asSemantics/TransitionSystem.java 2009-03-16 22:06:38 UTC (rev 1470)
@@ -42,11 +42,14 @@
import jason.asSyntax.Term;
import jason.asSyntax.Trigger;
import jason.asSyntax.VarTerm;
+import jason.asSyntax.PlanBody.BodyType;
import jason.asSyntax.Trigger.TEOperator;
import jason.asSyntax.Trigger.TEType;
import jason.asSyntax.parser.ParseException;
import jason.bb.BeliefBase;
import jason.runtime.Settings;
+import jason.stdlib.foreach;
+import jason.stdlib.loop;
import java.util.Iterator;
import java.util.LinkedList;
@@ -427,10 +430,33 @@
}
Unifier u = im.unif;
PlanBody h = im.getCurrentStep();
- h.applyHead(u);
-
+
+ Term bTerm = h.getBodyTerm();
+ // de-var bTerm
+ while (bTerm instanceof VarTerm) { // TODO: test var in var
+ // h should be ground
+ Term bValue = u.get((VarTerm)bTerm);
+ if (bValue == null) { // the case of !A with A not ground
+ if (!generateGoalDeletion(conf.C.SI, null)) {
+ logger.log(Level.SEVERE, h.getSrcInfo()+": "+ "Variable '"+bTerm+"' must be ground.");
+ }
+ return;
+ }
+ if (bValue.isPlanBody()) {
+ if (h.getBodyType() != BodyType.action) { // the case of !A with A unified with +g
+ if (!generateGoalDeletion(conf.C.SI, null)) {
+ logger.log(Level.SEVERE, h.getSrcInfo()+": "+ "The operator '"+h.getBodyType()+"' is lost with the variable unified with a plan body. ");
+ }
+ return;
+ }
+ h = (PlanBody)bValue; // TODO: change the current plan for the apply of the plan of VAR // the case of A unified with {a;b;c}
+ bTerm = h.getBodyTerm();
+ } else {
+ bTerm = bValue;
+ }
+ }
+
Literal body = null;
- Term bTerm = h.getBodyTerm();
if (bTerm instanceof Literal)
body = (Literal)bTerm;
@@ -438,6 +464,7 @@
// Rule Action
case action:
+ body = body.copy(); body.apply(u);
confP.C.A = new ActionExec(body, conf.C.SI);
break;
@@ -446,7 +473,14 @@
List<Term> errorAnnots = null;
try {
InternalAction ia = ((InternalActionLiteral)bTerm).getIA(ag);
- Object oresult = ia.execute(this, u, body.getTermsArray());
+ // clone and apply args
+ Term[] terms = new Term[body.getArity()];
+ for (int i=0; i<terms.length; i++) {
+ terms[i] = body.getTerm(i).clone();
+ if ( !(ia instanceof loop) && !(ia instanceof foreach)) // loop e foreach must not apply
+ terms[i].apply(u);
+ }
+ Object oresult = ia.execute(this, u, terms);
if (oresult != null) {
ok = oresult instanceof Boolean && (Boolean)oresult;
if (!ok && oresult instanceof Iterator) { // ia result is an Iterator
@@ -494,16 +528,14 @@
// Rule Achieve
case achieve:
- body = addSelfSource(body.copy());
- body.makeVarsAnnon(u); // free variables in an event cannot conflict with those in the plan
+ body = prepareBodyForEvent(body, u);
conf.C.addAchvGoal(body, conf.C.SI);
confP.step = State.StartRC;
break;
// Rule Achieve as a New Focus (the !! operator)
case achieveNF:
- body = addSelfSource(body.copy());
- body.makeVarsAnnon(u); // free variables in an event cannot conflict with those in the plan
+ body = prepareBodyForEvent(body, u);
conf.C.addAchvGoal(body, Intention.EmptyInt);
updateIntention();
break;
@@ -516,9 +548,8 @@
} else {
boolean fail = true;
if (f instanceof Literal) { // generate event when using literal in the test (no events for log. expr. like ?(a & b))
- body = (Literal)f.clone();
+ body = prepareBodyForEvent(body, u);
if (body.isLiteral()) { // in case body is a var with content that is not a literal (note the VarTerm pass in the instanceof Literal)
- body.makeVarsAnnon(u);
Trigger te = new Trigger(TEOperator.add, TEType.test, body);
if (ag.getPL().hasCandidatePlan(te)) {
Event evt = new Event(te, conf.C.SI);
@@ -540,13 +571,14 @@
case delAddBel:
// -+a(1,X) ===> remove a(_,_), add a(1,X)
// change all vars to anon vars to remove it
- body = addSelfSource(body.copy());
- body.makeTermsAnnon();
+ Literal b2 = prepareBodyForEvent(body, u);
+ b2.makeTermsAnnon(); // do not change body (but b2), to not interfere in addBel
// to delete, create events as external to avoid that
// remove/add create two events for the same intention
+ // (in future releases, creates a two branches for this operator)
try {
- List<Literal>[] result = ag.brf(null, body, conf.C.SI); // the intention is not the new focus
+ List<Literal>[] result = ag.brf(null, b2, conf.C.SI); // the intention is not the new focus
if (result != null) { // really delete something
// generate events
updateEvents(result,Intention.EmptyInt);
@@ -560,15 +592,12 @@
// Rule AddBel
case addBel:
- body = addSelfSource(body);
+ body = prepareBodyForEvent(body, u);
// calculate focus
Intention newfocus = Intention.EmptyInt;
if (setts.sameFocus())
newfocus = conf.C.SI;
-
- // rename free vars
- body.makeVarsAnnon(u);
// call BRF
try {
@@ -588,7 +617,7 @@
break;
case delBel:
- body = addSelfSource(body);
+ body = prepareBodyForEvent(body, u);
newfocus = Intention.EmptyInt;
if (setts.sameFocus())
@@ -614,17 +643,13 @@
}
// add the self source in the body in case no other source was given
- private Literal addSelfSource(Literal body) {
- // manage the case of var unified with atom
- if (body instanceof VarTerm) {
- Term v = ((VarTerm)body).getValue();
- if (v != null && v.isAtom())
- body = ((Atom)v);
- }
+ private Literal prepareBodyForEvent(Literal body, Unifier u) {
+ body = body.copy();
+ body.apply(u);
+ body.makeVarsAnnon(u); // free variables in an event cannot conflict with those in the plan
body = body.forceFullLiteralImpl();
if (!body.hasSource()) {
- // do not add source(self) in case the
- // programmer set the source
+ // do not add source(self) in case the programmer set the source
body.addAnnot(BeliefBase.TSelf);
}
return body;
Modified: trunk/src/jason/asSyntax/InternalActionLiteral.java
===================================================================
--- trunk/src/jason/asSyntax/InternalActionLiteral.java 2009-03-16 14:35:31 UTC (rev 1469)
+++ trunk/src/jason/asSyntax/InternalActionLiteral.java 2009-03-16 22:06:38 UTC (rev 1470)
@@ -26,8 +26,6 @@
import jason.asSemantics.Agent;
import jason.asSemantics.InternalAction;
import jason.asSemantics.Unifier;
-import jason.stdlib.foreach;
-import jason.stdlib.loop;
import java.util.Iterator;
import java.util.logging.Level;
@@ -76,15 +74,7 @@
public boolean isAtom() {
return false;
}
-
- @Override
- public boolean apply(Unifier u) {
- if (this.ia != null && (this.ia instanceof loop || this.ia instanceof foreach))
- return false;
- else
- return super.apply(u);
- }
-
+
@SuppressWarnings("unchecked")
public Iterator<Unifier> logicalConsequence(Agent ag, Unifier un) {
if (ag.getTS().getUserAgArch().isRunning()) {
Modified: trunk/src/jason/asSyntax/PlanBody.java
===================================================================
--- trunk/src/jason/asSyntax/PlanBody.java 2009-03-16 14:35:31 UTC (rev 1469)
+++ trunk/src/jason/asSyntax/PlanBody.java 2009-03-16 22:06:38 UTC (rev 1470)
@@ -1,6 +1,5 @@
package jason.asSyntax;
-import jason.asSemantics.Unifier;
/**
* Interface for elements of a plans's body.
@@ -29,9 +28,6 @@
public boolean isEmptyBody();
public int getPlanSize();
- /** apply unifier only on first element of the body */
- public boolean applyHead(Unifier u);
-
public void setBodyType(BodyType bt);
public void setBodyTerm(Term t);
public void setBodyNext(PlanBody bl);
Modified: trunk/src/jason/asSyntax/PlanBodyImpl.java
===================================================================
--- trunk/src/jason/asSyntax/PlanBodyImpl.java 2009-03-16 14:35:31 UTC (rev 1469)
+++ trunk/src/jason/asSyntax/PlanBodyImpl.java 2009-03-16 22:06:38 UTC (rev 1470)
@@ -145,7 +145,7 @@
if (i == 1) System.out.println("Should not setTerm(1) of body literal!");
}
- public boolean applyHead(Unifier u) {
+ private boolean applyHead(Unifier u) {
if (term != null && term.apply(u)) {
if (term.isPlanBody()) { // we cannot have "inner" body literals
PlanBody baknext = next;
Modified: trunk/src/jason/asSyntax/VarTerm.java
===================================================================
--- trunk/src/jason/asSyntax/VarTerm.java 2009-03-16 14:35:31 UTC (rev 1469)
+++ trunk/src/jason/asSyntax/VarTerm.java 2009-03-16 22:06:38 UTC (rev 1470)
@@ -955,13 +955,6 @@
return null;
}
- public boolean applyHead(Unifier u) {
- if (value != null && getValue() instanceof PlanBody)
- return ((PlanBody) getValue()).applyHead(u);
- else
- return true;
- }
-
public boolean isEmptyBody() {
if (value != null && getValue() instanceof PlanBody)
return ((PlanBody) getValue()).isEmptyBody();
Modified: trunk/src/jason/stdlib/foreach.java
===================================================================
--- trunk/src/jason/stdlib/foreach.java 2009-03-16 14:35:31 UTC (rev 1469)
+++ trunk/src/jason/stdlib/foreach.java 2009-03-16 22:06:38 UTC (rev 1470)
@@ -114,7 +114,7 @@
// get all solutions for the loop
// 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());
+ iu = logExpr.logicalConsequence(ts.getAg(), un);
List<Unifier> allsol = new ArrayList<Unifier>();
while (iu.hasNext())
allsol.add(iu.next());
@@ -122,6 +122,9 @@
if (allsol.isEmpty()) // none iteration
return true;
iu = allsol.iterator();
+
+ foria = new PlanBodyImpl(BodyType.internalAction, foria.getBodyTerm().clone());
+ foria.add(im.getCurrentStep().getBodyNext());
((Structure)foria.getBodyTerm()).addTerm(new ObjectTermImpl(iu));
} else if (args.length == 3) {
// restore the solutions
@@ -132,16 +135,18 @@
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
+ // 2. 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()));
+ PlanBody whattoadd = (PlanBody)args[1];
+ if (iu.hasNext()) {
+ whattoadd.add(foria);
+ } else {
+ whattoadd.add(foria.getBodyNext());
+ }
whattoadd.setAsBodyTerm(false);
- foria.add(1,whattoadd);
+ im.insertAsNextStep(whattoadd);
return true;
}
}
Modified: trunk/src/jason/stdlib/if_then_else.java
===================================================================
--- trunk/src/jason/stdlib/if_then_else.java 2009-03-16 14:35:31 UTC (rev 1469)
+++ trunk/src/jason/stdlib/if_then_else.java 2009-03-16 22:06:38 UTC (rev 1470)
@@ -102,9 +102,9 @@
if (whattoadd != null) {
IntendedMeans im = ts.getC().getSelectedIntention().peek();
- PlanBody ifia = im.getCurrentStep();
+ whattoadd.add(im.getCurrentStep().getBodyNext());
whattoadd.setAsBodyTerm(false);
- ifia.add(1,whattoadd);
+ im.insertAsNextStep(whattoadd);
}
return true;
}
Modified: trunk/src/jason/stdlib/loop.java
===================================================================
--- trunk/src/jason/stdlib/loop.java 2009-03-16 14:35:31 UTC (rev 1469)
+++ trunk/src/jason/stdlib/loop.java 2009-03-16 22:06:38 UTC (rev 1470)
@@ -101,8 +101,9 @@
// first execution of while
checkArguments(args);
// add backup unifier in the IA
+ whileia = new PlanBodyImpl(BodyType.internalAction, whileia.getBodyTerm().clone());
((Structure)whileia.getBodyTerm()).addTerm(new ObjectTermImpl(un.clone()));
- } else if (args.length == 3) {
+ } else if (args.length == 3) {
// restore the unifier of previous iterations
Unifier ubak = (Unifier)((ObjectTerm)args[2]).getObject();
un.clear();
@@ -119,12 +120,12 @@
// add in the current intention:
// 1. the body argument and
- // 2. a copy of the while internal action after the execution of the body
+ // 2. 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.add(whileia);
whattoadd.setAsBodyTerm(false);
- whileia.add(1,whattoadd);
+ im.insertAsNextStep(whattoadd);
}
return true;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|