From: <jom...@us...> - 2008-09-18 10:42:56
|
Revision: 1374 http://jason.svn.sourceforge.net/jason/?rev=1374&view=rev Author: jomifred Date: 2008-09-18 17:42:52 +0000 (Thu, 18 Sep 2008) Log Message: ----------- add the option to include plans in the begin for the .add_plan internal action Modified Paths: -------------- trunk/applications/as-unit-test/src/jason/tests/TestAll.java trunk/applications/jason-team/src/java/agent/OrgMaintenanceGoal.java trunk/src/jason/asSemantics/Agent.java trunk/src/jason/asSyntax/PlanLibrary.java trunk/src/jason/stdlib/add_plan.java Added Paths: ----------- trunk/applications/as-unit-test/src/jason/tests/TestIA.java Modified: trunk/applications/as-unit-test/src/jason/tests/TestAll.java =================================================================== --- trunk/applications/as-unit-test/src/jason/tests/TestAll.java 2008-09-16 17:43:27 UTC (rev 1373) +++ trunk/applications/as-unit-test/src/jason/tests/TestAll.java 2008-09-18 17:42:52 UTC (rev 1374) @@ -7,13 +7,15 @@ @RunWith(Suite.class) @SuiteClasses({ BugListReturnUnification.class, + BugVarsAsArg.class, BugVarsInInitBels.class, - BugVarsAsArg.class, TestAddLogExprInBB.class, - TestGoalSource.class, + TestGoalSource.class, + TestIA.class, + TestIAdelete.class, TestIF.class, + TestKQML.class, TestLoop.class, - TestKQML.class, TestPlanbodyAsTerm.class, TestPlanFailure.class, TestVarInContext.class Added: trunk/applications/as-unit-test/src/jason/tests/TestIA.java =================================================================== --- trunk/applications/as-unit-test/src/jason/tests/TestIA.java (rev 0) +++ trunk/applications/as-unit-test/src/jason/tests/TestIA.java 2008-09-18 17:42:52 UTC (rev 1374) @@ -0,0 +1,33 @@ +package jason.tests; + +import jason.asunit.TestAgent; + +import org.junit.Assert; +import org.junit.Test; + +public class TestIA { + + @Test + public void testAddPlan() { + TestAgent ag = new TestAgent(); + + // defines the agent's AgentSpeak code + ag.parseAScode( + "+!test <- act1. \n"+ + "+!add <- .add_plan(\"@l0 +!test <- act0.\", bob, begin). "+ + "+!remove <- .remove_plan(l0, bob). " + ); + ag.addGoal("test"); + ag.assertAct("act1", 10); + ag.addGoal("add"); + ag.addGoal("test"); + ag.assertAct("act0", 10); + ag.addGoal("remove"); + int size = ag.getPL().size(); + ag.assertIdle(30); + Assert.assertEquals(size-1, ag.getPL().size()); + ag.addGoal("test"); + ag.assertAct("act1", 10); + } + +} Modified: trunk/applications/jason-team/src/java/agent/OrgMaintenanceGoal.java =================================================================== --- trunk/applications/jason-team/src/java/agent/OrgMaintenanceGoal.java 2008-09-16 17:43:27 UTC (rev 1373) +++ trunk/applications/jason-team/src/java/agent/OrgMaintenanceGoal.java 2008-09-18 17:42:52 UTC (rev 1374) @@ -39,7 +39,7 @@ // add in the end of plan p.getBody().add(endofplan); - newAg.getPL().add(p); + newAg.getPL().add(p, false); } // add failure plan: Modified: trunk/src/jason/asSemantics/Agent.java =================================================================== --- trunk/src/jason/asSemantics/Agent.java 2008-09-16 17:43:27 UTC (rev 1373) +++ trunk/src/jason/asSemantics/Agent.java 2008-09-18 17:42:52 UTC (rev 1374) @@ -390,7 +390,7 @@ this.addInitialGoal(g); } for (Plan p: a.getPL()) { - this.getPL().add(p); + this.getPL().add(p, false); } } } Modified: trunk/src/jason/asSyntax/PlanLibrary.java =================================================================== --- trunk/src/jason/asSyntax/PlanLibrary.java 2008-09-16 17:43:27 UTC (rev 1373) +++ trunk/src/jason/asSyntax/PlanLibrary.java 2008-09-18 17:42:52 UTC (rev 1374) @@ -62,15 +62,33 @@ private Logger logger = Logger.getLogger(PlanLibrary.class.getName()); + /** + * Add a new plan written as a String. The source + * normally is "self" or the agent that sent this plan. + * If the already has a plan equals to "stPlan", only a + * new source is added. + * + * The plan is added in the end of the PlanLibrary. + * + * returns the plan added, null if it does not work. + */ + public Plan add(StringTerm stPlan, Structure tSource) { + return add(stPlan, tSource, false); + } + /** - * Add a new plan based on a String. The source + * Add a new plan written as a String. The source * normally is "self" or the agent that sent this plan. * If the already has a plan equals to "stPlan", only a * new source is added. + * + * If <i>before</i> is true, the plan will be added in the + * begin of the PlanLibrary; otherwise, it is added in + * the end. * * returns the plan added, null if it does not work. */ - public Plan add(StringTerm stPlan, Structure tSource) { + public Plan add(StringTerm stPlan, Structure tSource, boolean before) { String sPlan = stPlan.getString(); try { // remove quotes \" -> " @@ -90,9 +108,9 @@ p.setLabel(getUniqueLabel()); } p.getLabel().addSource(tSource); - add(p); + add(p, before); } else { - p = (Plan) plans.get(i); + p = plans.get(i); p.getLabel().addSource(tSource); } return p; @@ -175,13 +193,13 @@ public void addAll(PlanLibrary pl) throws JasonException { for (Plan p: pl) { - add(p); + add(p, false); } } public void addAll(List<Plan> plans) throws JasonException { for (Plan p: plans) { - add(p); + add(p, false); } } @@ -199,6 +217,10 @@ return planLabels.get(label); } + public int size() { + return plans.size(); + } + public List<Plan> getPlans() { return plans; } Modified: trunk/src/jason/stdlib/add_plan.java =================================================================== --- trunk/src/jason/stdlib/add_plan.java 2008-09-16 17:43:27 UTC (rev 1373) +++ trunk/src/jason/stdlib/add_plan.java 2008-09-18 17:42:52 UTC (rev 1374) @@ -49,8 +49,15 @@ <li><i>+ source</i> (atom [optional]): the source of the plan(s). The default value for the source is <code>self</code>.<br/> + <li><i>+ position</i> (atom [optional]): if value is "before" the plan + will be added in the begin of the plan library. + The default value is <code>end</code>.<br/> + </ul> + Note that if only two parameter is informed, the second will be the source and not + the position. + <p>Examples:<ul> <li> <code>.add_plan("+b : true <- .print(b).")</code>: adds the plan @@ -61,6 +68,9 @@ the previous example, but the source of the plan is agent "rafa".</li> + <li> <code>.add_plan("+b : true <- .print(b).", rafa, begin)</code>: same as + the previous example, but the plan is added in the begin of the plan library.</li> + <li> <code>.add_plan(["+b : true <- .print(b).", "+b : bel <- .print(bel)."], rafa)</code>: adds both plans with "rafa" as their sources.</li> @@ -75,7 +85,7 @@ public class add_plan extends DefaultInternalAction { @Override public int getMinArgs() { return 1; } - @Override public int getMaxArgs() { return 2; } + @Override public int getMaxArgs() { return 3; } @Override public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception { @@ -84,16 +94,18 @@ Term plans = DefaultTerm.parse(args[0].toString()); Structure source = new Atom("self"); - if (args.length > 1) { + if (args.length > 1) source = (Structure) args[1]; - } + boolean before = false; + if (args.length > 2) + before = args[2].toString().equals("begin"); + if (plans.isList()) { // arg[0] is a list of strings - for (Term t: (ListTerm) plans) { + 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); + ts.getAg().getPL().add((StringTerm) plans, source, before); } return true; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |