From: <jom...@us...> - 2008-09-12 14:16:55
|
Revision: 1366 http://jason.svn.sourceforge.net/jason/?rev=1366&view=rev Author: jomifred Date: 2008-09-12 14:16:52 +0000 (Fri, 12 Sep 2008) Log Message: ----------- new method to include plans in the begin of the PlanLibrary Message class is now serializable Modified Paths: -------------- trunk/demos/clone/main.asl trunk/demos/gui/gui2/sample.asl trunk/demos/tell-rule/receiver.asl trunk/src/jason/asSemantics/Message.java trunk/src/jason/asSyntax/BinaryStructure.java trunk/src/jason/asSyntax/LogExpr.java trunk/src/jason/asSyntax/PlanLibrary.java Modified: trunk/demos/clone/main.asl =================================================================== --- trunk/demos/clone/main.asl 2008-09-11 14:22:12 UTC (rev 1365) +++ trunk/demos/clone/main.asl 2008-09-12 14:16:52 UTC (rev 1366) @@ -6,18 +6,14 @@ /* Initial goals */ -!print. !start. /* Plans */ +!start <- +a(40); - .wait(100); myia.clone("bob"); .send(bob,tell,p(10)). -+!print <- .wait(500); .print(hello); !print. ++p(X) : a(V) <- .print(X," : ",V). -+p(X) <- .print(X). - Modified: trunk/demos/gui/gui2/sample.asl =================================================================== --- trunk/demos/gui/gui2/sample.asl 2008-09-11 14:22:12 UTC (rev 1365) +++ trunk/demos/gui/gui2/sample.asl 2008-09-12 14:16:52 UTC (rev 1366) @@ -8,7 +8,7 @@ +!start <- gui.create("Example GUI"). // the event !run is created by the GUI -+!run(X) <- .print("running ",X,"...."); .wait(100); !!run(X). ++!run(X) <- .print("running ",X,"...."); .wait(500); !!run(X). // the event !stop is created by the GUI Modified: trunk/demos/tell-rule/receiver.asl =================================================================== --- trunk/demos/tell-rule/receiver.asl 2008-09-11 14:22:12 UTC (rev 1365) +++ trunk/demos/tell-rule/receiver.asl 2008-09-12 14:16:52 UTC (rev 1366) @@ -12,7 +12,7 @@ // customisation of KQML performative tellRule +!kqml_received(A,tellRule,Rule,_) - <- .print("Received rule(s) ",Rule); + <- .print("Received rule(s) ",Rule, " from ",A); rules.add_rule(Rule); // get all rules and print them rules.get_rules(_,LR); Modified: trunk/src/jason/asSemantics/Message.java =================================================================== --- trunk/src/jason/asSemantics/Message.java 2008-09-11 14:22:12 UTC (rev 1365) +++ trunk/src/jason/asSemantics/Message.java 2008-09-12 14:16:52 UTC (rev 1366) @@ -24,8 +24,12 @@ package jason.asSemantics; +import jason.asSyntax.DefaultTerm; -public class Message { +import java.io.Serializable; + + +public class Message implements Serializable { private String ilForce = null; private String sender = null; @@ -126,6 +130,39 @@ return new Message(this); } + /** + * Creates a new message object based on a string that + * follows the format of the toString of Message class. + * + * @author Rogier + * @param msg the string message + * @return the parsed Message + */ + public static Message parseMsg(String msg) { + int one, two; + Message newmsg = new Message(); + if (msg.startsWith("<")) { + one = msg.indexOf(","); + int arrowIndex = msg.indexOf("->"); + if (one < arrowIndex) { // If there is an arrow before the first comma + newmsg.msgId = msg.substring(1, arrowIndex); + newmsg.inReplyTo = msg.substring(arrowIndex + 2, one); + } else { // If not (either there is no arrow, or there is one behind the first comma) + newmsg.msgId = msg.substring(1, one); + } + two = msg.indexOf(",", one + 1); + newmsg.sender = msg.substring(one + 1, two); + one = msg.indexOf(",", two + 1); + newmsg.ilForce = msg.substring(two + 1, one); + two = msg.indexOf(",", one + 1); + newmsg.receiver = msg.substring(one + 1, two); + one = msg.indexOf(">", two + 1); + String content = msg.substring(two + 1, one); + newmsg.propCont = DefaultTerm.parse(content); + } + return newmsg; + } + public String toString() { String irt = (inReplyTo == null ? "" : "->"+inReplyTo); return "<"+msgId+irt+","+sender+","+ilForce+","+receiver+","+propCont+">"; Modified: trunk/src/jason/asSyntax/BinaryStructure.java =================================================================== --- trunk/src/jason/asSyntax/BinaryStructure.java 2008-09-11 14:22:12 UTC (rev 1365) +++ trunk/src/jason/asSyntax/BinaryStructure.java 2008-09-12 14:16:52 UTC (rev 1366) @@ -51,6 +51,16 @@ return getArity() == 1; } + /** gets the LHS of this Expression */ + public LogicalFormula getLHS() { + return (LogicalFormula)getTerm(0); + } + + /** gets the RHS of this Expression */ + public LogicalFormula getRHS() { + return (LogicalFormula)getTerm(1); + } + @Override public String toString() { if (isUnary()) { Modified: trunk/src/jason/asSyntax/LogExpr.java =================================================================== --- trunk/src/jason/asSyntax/LogExpr.java 2008-09-11 14:22:12 UTC (rev 1365) +++ trunk/src/jason/asSyntax/LogExpr.java 2008-09-12 14:16:52 UTC (rev 1366) @@ -216,16 +216,6 @@ return op; } - /** gets the LHS of this Expression */ - public LogicalFormula getLHS() { - return (LogicalFormula)getTerm(0); - } - - /** gets the RHS of this Expression */ - public LogicalFormula getRHS() { - return (LogicalFormula)getTerm(1); - } - /** get as XML */ public Element getAsDOM(Document document) { Element u = super.getAsDOM(document); Modified: trunk/src/jason/asSyntax/PlanLibrary.java =================================================================== --- trunk/src/jason/asSyntax/PlanLibrary.java 2008-09-11 14:22:12 UTC (rev 1365) +++ trunk/src/jason/asSyntax/PlanLibrary.java 2008-09-12 14:16:52 UTC (rev 1366) @@ -87,7 +87,7 @@ if (i < 0) { // add label, if necessary if (p.getLabel() == null) { - setAutoLabel(p); + p.setLabel(getUniqueLabel()); } p.getLabel().addSource(tSource); add(p); @@ -104,8 +104,19 @@ } - public void add(Plan p) throws JasonException { + add(p,false); + } + + /** + * Adds a plan into the plan library, either before or after all other + * plans depending on the boolean parameter. + * + * @param p The plan to be added to the plan library + * @param before Whether or not to place the new plan before others + * @throws JasonException + */ + public void add(Plan p, boolean before) throws JasonException { // test p.label if (p.getLabel() != null && planLabels.keySet().contains(p.getLabel().getFunctor())) { // test if the new plan is equal, in this case, just add a source @@ -119,7 +130,8 @@ } // add label, if necessary - if (p.getLabel() == null) setAutoLabel(p); + if (p.getLabel() == null) + p.setLabel(getUniqueLabel()); // add self source if (!p.getLabel().hasSource()) p.getLabel().addAnnot(BeliefBase.TSelf); @@ -128,11 +140,17 @@ Trigger pte = p.getTrigger(); if (pte.getLiteral().isVar()) { - varPlans.add(p); + if (before) + varPlans.add(0,p); + else + varPlans.add(p); // add plan p in all entries for (List<Plan> lp: relPlans.values()) if (!lp.isEmpty() && lp.get(0).getTrigger().sameType(pte)) // only add if same type - lp.add(p); + if (before) + lp.add(0,p); + else + lp.add(p); } else { List<Plan> codesList = relPlans.get(pte.getPredicateIndicator()); if (codesList == null) { @@ -140,13 +158,19 @@ // copy plans from var plans for (Plan vp: varPlans) if (vp.getTrigger().sameType(pte)) - codesList.add(vp); + codesList.add(vp); relPlans.put(pte.getPredicateIndicator(), codesList); } - codesList.add(p); + if (before) + codesList.add(0,p); + else + codesList.add(p); } - plans.add(p); + if (before) + plans.add(0,p); + else + plans.add(p); } public void addAll(PlanLibrary pl) throws JasonException { @@ -162,12 +186,12 @@ } /** add a label to the plan */ - private void setAutoLabel(Plan p) { + private Pred getUniqueLabel() { String l; do { l = "l__" + (lastPlanLabel++); } while (planLabels.keySet().contains(l)); - p.setLabel(new Pred(l)); + return new Pred(l); } /** return a plan for a label */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |