From: <jom...@us...> - 2013-11-07 14:02:16
|
Revision: 1757 http://sourceforge.net/p/jason/svn/1757 Author: jomifred Date: 2013-11-07 14:02:11 +0000 (Thu, 07 Nov 2013) Log Message: ----------- new method capply that clones and applies Modified Paths: -------------- trunk/applications/as-unit-test/src/jason/tests/TestKQML.java trunk/src/jason/architecture/MindInspectorAgArch.java trunk/src/jason/asSemantics/Agent.java trunk/src/jason/asSemantics/ConcurrentInternalAction.java trunk/src/jason/asSemantics/DefaultInternalAction.java trunk/src/jason/asSemantics/IntendedMeans.java trunk/src/jason/asSemantics/TransitionSystem.java trunk/src/jason/asSemantics/Unifier.java trunk/src/jason/asSyntax/ArithExpr.java trunk/src/jason/asSyntax/ArithFunctionTerm.java trunk/src/jason/asSyntax/Atom.java trunk/src/jason/asSyntax/BinaryStructure.java trunk/src/jason/asSyntax/BodyLiteral.java trunk/src/jason/asSyntax/CyclicTerm.java trunk/src/jason/asSyntax/DefaultTerm.java trunk/src/jason/asSyntax/InternalActionLiteral.java trunk/src/jason/asSyntax/ListTermImpl.java trunk/src/jason/asSyntax/Literal.java trunk/src/jason/asSyntax/LiteralImpl.java trunk/src/jason/asSyntax/LogExpr.java trunk/src/jason/asSyntax/NumberTerm.java trunk/src/jason/asSyntax/NumberTermImpl.java trunk/src/jason/asSyntax/ObjectTermImpl.java trunk/src/jason/asSyntax/Plan.java trunk/src/jason/asSyntax/PlanBodyImpl.java trunk/src/jason/asSyntax/Pred.java trunk/src/jason/asSyntax/RelExpr.java trunk/src/jason/asSyntax/Rule.java trunk/src/jason/asSyntax/Structure.java trunk/src/jason/asSyntax/Term.java trunk/src/jason/asSyntax/Trigger.java trunk/src/jason/asSyntax/UnnamedVar.java trunk/src/jason/asSyntax/VarTerm.java trunk/src/jason/bb/AgentJDBCPersistentBB.java trunk/src/jason/bb/JDBCPersistentBB.java trunk/src/jason/mas2j/parser/MAS2JavaParser.jcc trunk/src/jason/mas2j/parser/mas2j.java trunk/src/jason/stdlib/add_plan.java trunk/src/jason/stdlib/desire.java trunk/src/jason/stdlib/drop_desire.java trunk/src/jason/stdlib/findall.java trunk/src/jason/stdlib/puts.java trunk/src/jason/stdlib/setof.java trunk/src/jason/stdlib/succeed_goal.java trunk/src/test/ASParserTest.java trunk/src/test/BeliefBaseTest.java trunk/src/test/ExprTermTest.java trunk/src/test/ListTermTest.java trunk/src/test/PlanTest.java trunk/src/test/RuleTest.java trunk/src/test/StdLibTest.java trunk/src/test/TermTest.java trunk/src/test/VarTermTest.java Modified: trunk/applications/as-unit-test/src/jason/tests/TestKQML.java =================================================================== --- trunk/applications/as-unit-test/src/jason/tests/TestKQML.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/applications/as-unit-test/src/jason/tests/TestKQML.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -107,7 +107,7 @@ public void testTellHow() { bob.addGoal("send_tellHow"); bob.assertIdle(10); - maria.assertPrint("Hello bob", 10); + maria.assertPrint("Hello bob", 20); bob.addGoal("send_untellHow"); bob.assertIdle(10); Modified: trunk/src/jason/architecture/MindInspectorAgArch.java =================================================================== --- trunk/src/jason/architecture/MindInspectorAgArch.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/architecture/MindInspectorAgArch.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -22,6 +22,7 @@ package jason.architecture; +import jason.NoValueForVarException; import jason.asSyntax.ASSyntax; import jason.asSyntax.NumberTerm; import jason.asSyntax.Structure; @@ -117,7 +118,11 @@ hasMindInspectorByCycle = sConf.getTerm(0).toString().equals("cycle"); if (! hasMindInspectorByCycle) { - updateInterval = (int)((NumberTerm)sConf.getTerm(0)).solve(); + try { + updateInterval = (int)((NumberTerm)sConf.getTerm(0)).solve(); + } catch (NoValueForVarException e1) { + e1.printStackTrace(); + } new Thread("update agent mind inspector") { public void run() { try { Modified: trunk/src/jason/asSemantics/Agent.java =================================================================== --- trunk/src/jason/asSemantics/Agent.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSemantics/Agent.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -488,8 +488,8 @@ if (!b.isRule() && !b.isGround()) b = new Rule(b,Literal.LTrue); - b.apply(new Unifier()); // to solve arithmetic expressions - + b = (Literal)b.capply(null); // to solve arithmetic expressions + // does not do BRF for rules (and so do not produce events +bel for rules) if (b.isRule()) getBB().add(b); @@ -843,7 +843,7 @@ boolean removed = getBB().remove(beliefToDel); if (!removed && !beliefToDel.isGround()) { // then try to unify the parameter with a belief in BB if (believes(beliefToDel, u)) { - beliefToDel.apply(u); + beliefToDel = (Literal)beliefToDel.capply(u); removed = getBB().remove(beliefToDel); } } Modified: trunk/src/jason/asSemantics/ConcurrentInternalAction.java =================================================================== --- trunk/src/jason/asSemantics/ConcurrentInternalAction.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSemantics/ConcurrentInternalAction.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -68,8 +68,7 @@ public Term[] prepareArguments(Literal body, Unifier un) { Term[] terms = new Term[body.getArity()]; for (int i=0; i<terms.length; i++) { - terms[i] = body.getTerm(i).clone(); - terms[i].apply(un); + terms[i] = body.getTerm(i).capply(un); } return terms; } Modified: trunk/src/jason/asSemantics/DefaultInternalAction.java =================================================================== --- trunk/src/jason/asSemantics/DefaultInternalAction.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSemantics/DefaultInternalAction.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -32,8 +32,7 @@ public Term[] prepareArguments(Literal body, Unifier un) { Term[] terms = new Term[body.getArity()]; for (int i=0; i<terms.length; i++) { - terms[i] = body.getTerm(i).clone(); - terms[i].apply(un); + terms[i] = body.getTerm(i).capply(un); } return terms; } Modified: trunk/src/jason/asSemantics/IntendedMeans.java =================================================================== --- trunk/src/jason/asSemantics/IntendedMeans.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSemantics/IntendedMeans.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -53,11 +53,10 @@ unif = opt.getUnifier(); if (te == null) { - trigger = plan.getTrigger().clone(); + trigger = plan.getTrigger().capply(unif); } else { - trigger = te; + trigger = te.capply(unif); } - trigger.apply(unif); } // used by clone Modified: trunk/src/jason/asSemantics/TransitionSystem.java =================================================================== --- trunk/src/jason/asSemantics/TransitionSystem.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSemantics/TransitionSystem.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -24,6 +24,7 @@ package jason.asSemantics; import jason.JasonException; +import jason.NoValueForVarException; import jason.RevisionFailedException; import jason.architecture.AgArch; import jason.asSyntax.ASSyntax; @@ -279,8 +280,7 @@ // test the case of sync ask with many receivers Unifier un = intention.peek().getUnif(); - Term rec = send.getTerm(0).clone(); - rec.apply(un); + Term rec = send.getTerm(0).capply(un); if (rec.isList()) { // send to many receivers // put the answers in the unifier VarTerm answers = new VarTerm("AnsList___"+m.getInReplyTo()); @@ -497,33 +497,37 @@ // begin tail recursion optimisation (TRO) if (setts.isTROon()) { IntendedMeans top = confP.C.SE.intention.peek(); // top = the IM that will be removed from the intention due to TRO - if (top.getTrigger().isGoal() && im.getTrigger().isGoal() && // are both goal - top.getTrigger().getPredicateIndicator().equals( im.getTrigger().getPredicateIndicator()) && // are they equals - top.getCurrentStep().getBodyNext() == null) { // the plan below is finished + if (top != null && top.getTrigger().isGoal() && im.getTrigger().isGoal() && // are both goal + top.getCurrentStep().getBodyNext() == null && // the plan below is finished + top.getTrigger().getPredicateIndicator().equals( im.getTrigger().getPredicateIndicator()) // goals are equals + ) { confP.C.SE.intention.pop(); // remove the top IM IntendedMeans imBase = confP.C.SE.intention.peek(); // base = where the new IM will be place on top of - // move top relevant values into the base (relevant = renamed vars in base) - for (VarTerm v: imBase.renamedVars) { - VarTerm vvl = (VarTerm)imBase.renamedVars.function.get(v); - Term t = top.unif.get(vvl); - if (t != null) { // if v has got a value in top unif, put the value in the unifier - if (t instanceof Literal) { - Literal l= (Literal)t.clone(); - l.apply(top.unif); - l.makeVarsAnnon(top.renamedVars); - im.unif.function.put(vvl, l); + if (imBase != null) { + // move top relevant values into the base (relevant = renamed vars in base) + for (VarTerm v: imBase.renamedVars) { + VarTerm vvl = (VarTerm)imBase.renamedVars.function.get(v); + Term t = top.unif.get(vvl); + if (t != null) { // if v has got a value in top unif, put the value in the unifier + if (t instanceof Literal) { + //Literal l= (Literal)t.clone(); + //l.apply(top.unif); + Literal l= (Literal)t.capply(top.unif); + l.makeVarsAnnon(top.renamedVars); + im.unif.function.put(vvl, l); + } else { + im.unif.function.put(vvl, t); + } } else { - im.unif.function.put(vvl, t); + // the vvl was renamed again in top, just replace in base the new value + VarTerm v0 = (VarTerm)top.renamedVars.function.get(vvl); + if (v0 != null) { + imBase.renamedVars.function.put(v, v0); + } } - } else { - // the vvl was renamed again in top, just replace in base the new value - VarTerm v0 = (VarTerm)top.renamedVars.function.get(vvl); - if (v0 != null) { - imBase.renamedVars.function.put(v, v0); - } - } - } + } + } } // end of TRO } @@ -616,8 +620,7 @@ Term bTerm = h.getBodyTerm(); if (bTerm instanceof VarTerm) { // de-var bTerm - bTerm = bTerm.clone(); // clone before apply - bTerm.apply(u); + bTerm = bTerm.capply(u); if (bTerm.isVar()) { // the case of !A with A not ground String msg = h.getSrcInfo()+": "+ "Variable '"+bTerm+"' must be ground."; if (!generateGoalDeletion(conf.C.SI, JasonException.createBasicErrorAnnots("body_var_without_value", msg))) @@ -650,7 +653,7 @@ // Rule Action case action: - body = body.copy(); body.apply(u); + body = (Literal)body.capply(u); confP.C.A = new ActionExec(body, conf.C.SI); break; @@ -839,8 +842,7 @@ // add the self source in the body in case no other source was given private Literal prepareBodyForEvent(Literal body, Unifier u, IntendedMeans imRenamedVars) { - body = body.copy(); - body.apply(u); + body = (Literal)body.capply(u); Unifier renamedVars = new Unifier(); //System.out.println("antes "+body+" "+u+" "); body.makeVarsAnnon(renamedVars); // free variables in an event cannot conflict with those in the plan @@ -955,8 +957,7 @@ //System.out.println(ov+"="+vt+"="+vl); if (vl != null) { // vt has value in top //System.out.println(" and found "+vl); - vl = vl.clone(); - vl.apply(topIM.unif); + vl = vl.capply(topIM.unif); if (vl.isLiteral()) ((Literal)vl).makeVarsAnnon(); im.unif.bind(vt, vl); @@ -1069,8 +1070,7 @@ } else { failEvent = new Event(im.getTrigger().clone(), i); } - Term bodyPart = im.getCurrentStep().getBodyTerm().clone(); - bodyPart.apply(im.unif); + Term bodyPart = im.getCurrentStep().getBodyTerm().capply(im.unif); setDefaultFailureAnnots(failEvent, bodyPart, failAnnots); if (im.isGoalAdd()) { @@ -1213,7 +1213,12 @@ isize = 0; else isize = intention.size(); - int deadline = (int)((NumberTerm)hdl.getTerm(0)).solve(); + int deadline = 0; + try { + deadline = (int)((NumberTerm)hdl.getTerm(0)).solve(); + } catch (NoValueForVarException e1) { + e1.printStackTrace(); + } getAg().getScheduler().schedule(new Runnable() { public void run() { Modified: trunk/src/jason/asSemantics/Unifier.java =================================================================== --- trunk/src/jason/asSemantics/Unifier.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSemantics/Unifier.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -51,20 +51,6 @@ protected Map<VarTerm, Term> function = new HashMap<VarTerm, Term>(); - /** - * @deprecated use t.apply(un) instead. - */ - public void apply(Term t) { - t.apply(this); - } - - /** - * @deprecated use p.apply(un) instead. - */ - public void apply(Pred p) { - p.apply(this); - } - /** * gets the value for a Var, if it is unified with another var, gets this * other's value @@ -235,14 +221,10 @@ protected boolean unifyTerms(Term t1g, Term t2g) { // if args are expressions, apply them and use their values - if (t1g.isArithExpr()) { - t1g = t1g.clone(); - t1g.apply(this); - } - if (t2g.isArithExpr()) { - t2g = t2g.clone(); - t2g.apply(this); - } + if (t1g.isArithExpr()) + t1g = t1g.capply(this); + if (t2g.isArithExpr()) + t2g = t2g.capply(this); final boolean t1gisvar = t1g.isVar(); final boolean t2gisvar = t2g.isVar(); Modified: trunk/src/jason/asSyntax/ArithExpr.java =================================================================== --- trunk/src/jason/asSyntax/ArithExpr.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/ArithExpr.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -23,7 +23,9 @@ package jason.asSyntax; +import jason.NoValueForVarException; import jason.asSemantics.Agent; +import jason.asSemantics.Unifier; import jason.asSyntax.parser.as2j; import java.io.StringReader; @@ -38,7 +40,7 @@ @navassoc - op - ArithmeticOp */ -public class ArithExpr extends ArithFunctionTerm { +public class ArithExpr extends ArithFunctionTerm implements NumberTerm { private static final long serialVersionUID = 1L; private static Logger logger = Logger.getLogger(ArithExpr.class.getName()); @@ -162,34 +164,34 @@ return null; } } - + @Override - public double solve() { - double l = ((NumberTerm)getTerm(0)).solve(); - if (isUnary()) { - if (op == ArithmeticOp.minus) { - return -l; + public Term capply(Unifier u) { + try { + double l = ((NumberTerm)getTerm(0).capply(u)).solve(); + if (isUnary()) { + if (op == ArithmeticOp.minus) { + value = new NumberTermImpl(-l); + } else { + value = new NumberTermImpl(l); + } } else { - return l; + double r = ((NumberTerm)getTerm(1).capply(u)).solve(); + value = new NumberTermImpl(op.eval(l, r)); } - } else { - double r = ((NumberTerm)getTerm(1)).solve(); - return op.eval(l, r); + return value; + } catch (NoValueForVarException e) { + return clone(); } } - - @Override + public boolean checkArity(int a) { return a == 1 || a == 2; } /** make a hard copy of the terms */ public NumberTerm clone() { - if (isEvaluated()) { - return getValue(); - } else { - return new ArithExpr(this); - } + return new ArithExpr(this); } /** gets the Operation of this Expression */ @@ -209,38 +211,30 @@ @Override public String toString() { - if (isEvaluated()) { - return getValue().toString(); + if (isUnary()) { + return "(" + op + getTerm(0) + ")"; } else { - if (isUnary()) { - return "(" + op + getTerm(0) + ")"; - } else { - return "(" + getTerm(0) + op + getTerm(1) + ")"; - } + return "(" + getTerm(0) + op + getTerm(1) + ")"; } } /** get as XML */ public Element getAsDOM(Document document) { - if (isEvaluated()) { - return getValue().getAsDOM(document); + Element u = (Element) document.createElement("expression"); + u.setAttribute("type", "arithmetic"); + u.setAttribute("operator", op.toString()); + if (isUnary()) { + Element r = (Element) document.createElement("right"); + r.appendChild(getTerm(0).getAsDOM(document)); // put the left argument indeed! + u.appendChild(r); } else { - Element u = (Element) document.createElement("expression"); - u.setAttribute("type", "arithmetic"); - u.setAttribute("operator", op.toString()); - if (isUnary()) { - Element r = (Element) document.createElement("right"); - r.appendChild(getTerm(0).getAsDOM(document)); // put the left argument indeed! - u.appendChild(r); - } else { - Element l = (Element) document.createElement("left"); - l.appendChild(getTerm(0).getAsDOM(document)); - u.appendChild(l); - Element r = (Element) document.createElement("right"); - r.appendChild(getTerm(1).getAsDOM(document)); - u.appendChild(r); - } - return u; + Element l = (Element) document.createElement("left"); + l.appendChild(getTerm(0).getAsDOM(document)); + u.appendChild(l); + Element r = (Element) document.createElement("right"); + r.appendChild(getTerm(1).getAsDOM(document)); + u.appendChild(r); } + return u; } } Modified: trunk/src/jason/asSyntax/ArithFunctionTerm.java =================================================================== --- trunk/src/jason/asSyntax/ArithFunctionTerm.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/ArithFunctionTerm.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -1,5 +1,6 @@ package jason.asSyntax; +import jason.NoValueForVarException; import jason.asSemantics.Agent; import jason.asSemantics.ArithFunction; import jason.asSemantics.Unifier; @@ -26,16 +27,12 @@ private static Logger logger = Logger.getLogger(ArithFunctionTerm.class.getName()); - private NumberTerm value = null; // value, when evaluated + protected NumberTerm value = null; // value, when evaluated private ArithFunction function = null; private Agent agent = null; // the agent where this function was used - - public ArithFunctionTerm(String functor, int termsSize) { - super(functor, termsSize); - } - + public ArithFunctionTerm(ArithFunction function) { super(function.getName(), 2); this.function = function; @@ -43,15 +40,14 @@ public ArithFunctionTerm(ArithFunctionTerm af) { super(af); // clone args from af - value = af.value; function = af.function; agent = af.agent; } - public NumberTerm getValue() { - return value; + public ArithFunctionTerm(String functor, int arity) { + super(functor,arity); } - + @Override public boolean isNumeric() { return true; @@ -68,62 +64,15 @@ } @Override - public Literal makeVarsAnnon(Unifier un) { - if (isEvaluated()) { - return null; - } else { - return super.makeVarsAnnon(un); - } - } - - @Override public boolean isLiteral() { return false; } @Override public boolean isArithExpr() { - return !isEvaluated(); + return true; } - - /** returns true if the function/expression was already evaluated */ - public boolean isEvaluated() { - return value != null; - } - @Override - public boolean isGround() { - return isEvaluated() || super.isGround(); - } - - public boolean isUnary() { - return getArity() == 1; - } - - /** - * Does a "normal" apply and then solve the expression and store the result, - * so future calls of solve do not need to compute the value again - */ - @Override - public boolean apply(Unifier u) { - if (isEvaluated()) - return false; - - super.apply(u); - if ((function != null && function.allowUngroundTerms()) || isGround()) { - try { - value = new NumberTermImpl(solve()); - return true; - } catch (Exception e) { - logger.log(Level.SEVERE, getErrorMsg()+ " -- "+ e); - } - //} else { - // logger.warning(getErrorMsg()+ " -- this function has unground arguments and can not be evaluated! Unifier is "+u); - } - - return false; - } - public void setAgent(Agent ag) { agent = ag; } @@ -132,20 +81,38 @@ } /** computes the value for this arithmetic function (as defined in the NumberTerm interface) */ - public double solve() { - if (isEvaluated()) - return value.solve(); - else if (function != null) - try { - return function.evaluate((agent == null ? null : agent.getTS()),getTermsArray()); - } catch (Exception e) { - logger.log(Level.SEVERE, getErrorMsg()+ " -- error in evaluate!", e); + @Override + public Term capply(Unifier u) { + if (function == null) { + logger.log(Level.SEVERE, getErrorMsg()+ " -- the function can not be evalutated, it has no function assigned to it!", new Exception()); + } else { + Term v = super.capply(u); + if (function.allowUngroundTerms() || v.isGround()) { + try { + value = new NumberTermImpl(function.evaluate((agent == null ? null : agent.getTS()), ((Literal)v).getTermsArray())); + return value; + } catch (NoValueForVarException e) { + // ignore and return this; + } catch (Exception e) { + logger.log(Level.SEVERE, getErrorMsg()+ " -- error in evaluate!", e); + } + //} else { + // logger.warning(getErrorMsg()+ " -- this function has unground arguments and can not be evaluated! Unifier is "+u); } - else - logger.log(Level.SEVERE, getErrorMsg()+ " -- the function can not be evalutated, it has no function assigned to it!", new Exception()); - return 0; + } + return clone(); } + @Override + public double solve() throws NoValueForVarException { + if (value == null) // try to solve without unifier + capply(null); + if (value == null) + throw new NoValueForVarException(); + else + return value.solve(); + } + public boolean checkArity(int a) { return function != null && function.checkArity(a); } @@ -159,46 +126,24 @@ @Override public boolean equals(Object t) { if (t == null) return false; - if (isEvaluated()) return value.equals(t); return super.equals(t); } @Override public int compareTo(Term o) { - /*if (o instanceof NumberTerm) { - NumberTerm st = (NumberTerm)o; - if (solve() > st.solve()) return 1; - if (solve() < st.solve()) return -1; - } - return 0; */ if (o instanceof VarTerm) { return o.compareTo(this) * -1; } - if (o instanceof NumberTerm) { + return super.compareTo(o); + /*if (o instanceof NumberTerm) { NumberTerm st = (NumberTerm)o; if (solve() > st.solve()) return 1; if (solve() < st.solve()) return -1; return 0; } - return -1; + return -1;*/ } - @Override - protected int calcHashCode() { - if (isEvaluated()) - return value.hashCode(); - else - return super.calcHashCode(); - } - - @Override - public String toString() { - if (isEvaluated()) - return value.toString(); - else - return super.toString(); - } - @Override public String getErrorMsg() { return "Error in '"+this+"' ("+ super.getErrorMsg() + ")"; @@ -206,22 +151,15 @@ @Override public NumberTerm clone() { - if (isEvaluated()) - return value; - else - return new ArithFunctionTerm(this); + return new ArithFunctionTerm(this); } public Element getAsDOM(Document document) { - if (isEvaluated()) { - return value.getAsDOM(document); - } else { - Element u = (Element) document.createElement("expression"); - u.setAttribute("type", "arithmetic"); - Element r = (Element) document.createElement("right"); - r.appendChild(super.getAsDOM(document)); // put the left argument indeed! - u.appendChild(r); - return u; - } + Element u = (Element) document.createElement("expression"); + u.setAttribute("type", "arithmetic"); + Element r = (Element) document.createElement("right"); + r.appendChild(super.getAsDOM(document)); // put the left argument indeed! + u.appendChild(r); + return u; } } Modified: trunk/src/jason/asSyntax/Atom.java =================================================================== --- trunk/src/jason/asSyntax/Atom.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/Atom.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -59,7 +59,7 @@ public Term clone() { return this; // since this object is immutable } - + @Override public boolean isAtom() { return true; Modified: trunk/src/jason/asSyntax/BinaryStructure.java =================================================================== --- trunk/src/jason/asSyntax/BinaryStructure.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/BinaryStructure.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -56,10 +56,6 @@ srcInfo = arg.getSrcInfo(); } - public boolean isUnary() { - return getArity() == 1; - } - /** gets the LHS of this operation */ public Term getLHS() { return getTerm(0); Modified: trunk/src/jason/asSyntax/BodyLiteral.java =================================================================== --- trunk/src/jason/asSyntax/BodyLiteral.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/BodyLiteral.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -37,6 +37,7 @@ case delAddBel: return PlanBody.BodyType.delAddBel; case achieveNF: return PlanBody.BodyType.achieveNF; case constraint: return PlanBody.BodyType.constraint; + default: break; } return PlanBody.BodyType.none; } Modified: trunk/src/jason/asSyntax/CyclicTerm.java =================================================================== --- trunk/src/jason/asSyntax/CyclicTerm.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/CyclicTerm.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -45,6 +45,11 @@ cyclicVar = v; } + public CyclicTerm(Literal t, VarTerm v, Unifier u) { + super(t,u); + cyclicVar = v; + } + public VarTerm getCyclicVar() { return cyclicVar; } @@ -75,6 +80,8 @@ return this; } + + /* @Override public boolean apply(Unifier u) { Term v = u.remove(cyclicVar); @@ -83,9 +90,19 @@ u.bind(cyclicVar, v); return b; } + */ + @Override + public Term capply(Unifier u) { + Term v = u.remove(cyclicVar); + Term r = new CyclicTerm(this, (VarTerm)cyclicVar.clone(), u); + if (v != null) + u.bind(cyclicVar, v); + return r; + } + public Term clone() { - return new CyclicTerm(this, (VarTerm)cyclicVar.copy()); + return new CyclicTerm(this, (VarTerm)cyclicVar.clone()); } @Override Modified: trunk/src/jason/asSyntax/DefaultTerm.java =================================================================== --- trunk/src/jason/asSyntax/DefaultTerm.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/DefaultTerm.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -110,9 +110,8 @@ return true; } - - public boolean apply(Unifier u) { - return false; + public Term capply(Unifier u) { + return clone(); } public SourceInfo getSrcInfo() { Modified: trunk/src/jason/asSyntax/InternalActionLiteral.java =================================================================== --- trunk/src/jason/asSyntax/InternalActionLiteral.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/InternalActionLiteral.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -24,9 +24,7 @@ package jason.asSyntax; import jason.asSemantics.Agent; -import jason.asSemantics.QueryCacheKey; import jason.asSemantics.InternalAction; -import jason.asSemantics.QueryCacheAdv; import jason.asSemantics.Unifier; import java.util.ConcurrentModificationException; @@ -61,6 +59,12 @@ this.ia = l.ia; } + // used by capply + private InternalActionLiteral(InternalActionLiteral l, Unifier u) { + super((Structure) l, u); + this.ia = l.ia; + } + // used by the parser public InternalActionLiteral(Structure p, Agent ag) throws Exception { super(p); @@ -146,6 +150,11 @@ return "Error in internal action '"+this+"'"+ src; } + @Override + public Term capply(Unifier u) { + return new InternalActionLiteral(this, u); + } + public InternalActionLiteral clone() { return new InternalActionLiteral(this); } Modified: trunk/src/jason/asSyntax/ListTermImpl.java =================================================================== --- trunk/src/jason/asSyntax/ListTermImpl.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/ListTermImpl.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -96,7 +96,16 @@ public ListTerm cloneLT() { return clone(); } - + + /** make a hard copy of the terms */ + @Override + public ListTerm capply(Unifier u) { + ListTermImpl t = new ListTermImpl(); + if (term != null) t.term = this.term.capply(u); + if (next != null) t.next = this.next.capply(u); + return t; + } + /** make a shallow copy of the list (terms are not cloned, only the structure) */ public ListTerm cloneLTShallow() { ListTermImpl t = new ListTermImpl(); @@ -242,6 +251,7 @@ return false; } + /* @Override public boolean apply(Unifier u) { if (isEmpty()) { @@ -253,6 +263,7 @@ } return false; } + */ @Override public Iterator<Unifier> logicalConsequence(Agent ag, Unifier un) { @@ -625,7 +636,7 @@ s.append(l.getTerm()); if (l.isTail()) { s.append('|'); - s.append(l.getNext()); + s.append(l.getTail()); break; } l = l.getNext(); Modified: trunk/src/jason/asSyntax/Literal.java =================================================================== --- trunk/src/jason/asSyntax/Literal.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/Literal.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -332,8 +332,7 @@ // try cache iterator if (cacheIt != null) { while (cacheIt.hasNext()) { - Literal ltmp = Literal.this.copy(); - ltmp.apply( cacheIt.next() ); + Literal ltmp = (Literal)Literal.this.capply( cacheIt.next() ); Unifier u = un.clone(); //System.out.println(" try "+ltmp); if (u.unifiesNoUndo(Literal.this, ltmp)) { @@ -365,8 +364,9 @@ while (ruleIt.hasNext()) { // unifies the rule head with the result of rule evaluation Unifier ruleUn = ruleIt.next(); // evaluation result - Literal rhead = rule.headClone(); - rhead.apply(ruleUn); + //Literal rhead = rule.headClone(); + //rhead = (Literal)rhead.capply(ruleUn); + Literal rhead = rule.headCApply(ruleUn); useDerefVars(rhead, ruleUn); // replace vars by the bottom in the var clusters (e.g. X=_2; Y=_2, a(X,Y) ===> A(_2,_2)) rhead.makeVarsAnnon(); // to remove vars in head with original names @@ -399,8 +399,7 @@ // it is used to define what will be the unifier used // inside the rule. if (cloneAnnon == null) { - cloneAnnon = Literal.this.copy(); - cloneAnnon.apply(un); + cloneAnnon = (Literal)Literal.this.capply(un); cloneAnnon.makeVarsAnnon(); } @@ -418,8 +417,7 @@ get(); return; }*/ - kForChache = Literal.this.copy(); - kForChache.apply(un); + kForChache = (Literal)Literal.this.capply(un); //System.out.println("try "+kForChache); cacheIt = qCache.getCache(kForChache); if (cacheIt != null) { Modified: trunk/src/jason/asSyntax/LiteralImpl.java =================================================================== --- trunk/src/jason/asSyntax/LiteralImpl.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/LiteralImpl.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -23,6 +23,8 @@ package jason.asSyntax; +import jason.asSemantics.Unifier; + import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -53,6 +55,13 @@ type = !l.negated(); } + // used by capply + protected LiteralImpl(Literal l, Unifier u) { + super(l,u); + type = !l.negated(); + } + + /** if pos == true, the literal is positive, otherwise it is negative */ public LiteralImpl(boolean pos, Literal l) { super(l); @@ -119,11 +128,17 @@ return super.compareTo(t); } + @Override public Term clone() { return new LiteralImpl(this); } @Override + public Term capply(Unifier u) { + return new LiteralImpl(this,u); + } + + @Override protected int calcHashCode() { int result = super.calcHashCode(); if (negated()) result += 3271; Modified: trunk/src/jason/asSyntax/LogExpr.java =================================================================== --- trunk/src/jason/asSyntax/LogExpr.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/LogExpr.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -104,6 +104,8 @@ switch (op) { + case none: break; + case not: if (!getLHS().logicalConsequence(ag,un).hasNext()) { return createUnifIterator(un); @@ -238,6 +240,15 @@ return null; } + @Override + public Term capply(Unifier u) { + // do not call constructor with term parameter! + if (isUnary()) + return new LogExpr(op, (LogicalFormula)getTerm(0).capply(u)); + else + return new LogExpr((LogicalFormula)getTerm(0).capply(u), op, (LogicalFormula)getTerm(1).capply(u)); + } + /** make a hard copy of the terms */ public LogicalFormula clone() { // do not call constructor with term parameter! Modified: trunk/src/jason/asSyntax/NumberTerm.java =================================================================== --- trunk/src/jason/asSyntax/NumberTerm.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/NumberTerm.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -1,6 +1,8 @@ package jason.asSyntax; +import jason.NoValueForVarException; + /** The interface for numeric terms of AgentSpeak language * * @opt nodefillcolor lightgoldenrodyellow @@ -8,5 +10,5 @@ public interface NumberTerm extends Term { /** returns the numeric value of the term */ - public double solve(); + public double solve() throws NoValueForVarException; } Modified: trunk/src/jason/asSyntax/NumberTermImpl.java =================================================================== --- trunk/src/jason/asSyntax/NumberTermImpl.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/NumberTermImpl.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -58,7 +58,7 @@ } public NumberTermImpl(NumberTermImpl t) { - value = t.solve(); + value = t.value; srcInfo = t.srcInfo; } @@ -90,7 +90,7 @@ @Override protected int calcHashCode() { - return 37 * (int)solve(); + return 37 * (int)value; } @Override @@ -98,10 +98,10 @@ if (o instanceof VarTerm) { return o.compareTo(this) * -1; } - if (o instanceof NumberTerm) { - NumberTerm st = (NumberTerm)o; - if (solve() > st.solve()) return 1; - if (solve() < st.solve()) return -1; + if (o instanceof NumberTermImpl) { + NumberTermImpl st = (NumberTermImpl)o; + if (value > st.value) return 1; + if (value < st.value) return -1; return 0; } return -1; Modified: trunk/src/jason/asSyntax/ObjectTermImpl.java =================================================================== --- trunk/src/jason/asSyntax/ObjectTermImpl.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/ObjectTermImpl.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -35,12 +35,12 @@ return this.o.equals(((ObjectTermImpl) o).o); } - if (o instanceof VarTerm) { + /*if (o instanceof VarTerm) { Term value = ((VarTerm) o).getValue(); if (value instanceof ObjectTermImpl) { return this.o.equals(((ObjectTermImpl) value).o); } - } + }*/ return false; } Modified: trunk/src/jason/asSyntax/Plan.java =================================================================== --- trunk/src/jason/asSyntax/Plan.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/Plan.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -207,6 +207,25 @@ return false; } + public Plan capply(Unifier u) { + Plan p = new Plan(); + if (label != null) { + p.label = (Pred) label.capply(u); + p.isAtomic = isAtomic; + p.hasBreakpoint = hasBreakpoint; + p.isAllUnifs = isAllUnifs; + } + + p.tevent = tevent.capply(u); + if (context != null) + p.context = (LogicalFormula)context.capply(u); + p.body = (PlanBody)body.capply(u); + p.setSrcInfo(srcInfo); + p.isTerm = isTerm; + + return p; + } + public Term clone() { Plan p = new Plan(); if (label != null) { Modified: trunk/src/jason/asSyntax/PlanBodyImpl.java =================================================================== --- trunk/src/jason/asSyntax/PlanBodyImpl.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/PlanBodyImpl.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -152,6 +152,7 @@ } } + /* private boolean applyHead(Unifier u) { if (term != null && term.apply(u)) { if (term.isPlanBody()) { // we cannot have "inner" body literals @@ -168,7 +169,9 @@ } return false; } + */ + /* @Override public boolean apply(Unifier u) { boolean ok = next != null && next.apply(u); @@ -181,6 +184,7 @@ return ok; } + */ @Override public Iterator<Unifier> logicalConsequence(Agent ag, Unifier un) { @@ -277,6 +281,29 @@ bl.setBodyTerm(l); } + @Override + public PlanBody capply(Unifier u) { + //System.out.println(this+" with "+u); + PlanBodyImpl c; + if (term == null) { // (NIDE) must copy c.isTerm even if cloning empty plan + c = new PlanBodyImpl(); + } else { + c = new PlanBodyImpl(formType, term.capply(u)); + if (c.term.isPlanBody()) { // we cannot have "inner" body literals + c.formType = ((PlanBody)c.term).getBodyType(); + c.next = ((PlanBody)c.term).getBodyNext(); + c.term = ((PlanBody)c.term).getBodyTerm(); + } + } + c.isTerm = isTerm; + + if (next != null) + c.add((PlanBody)next.capply(u)); + + //System.out.println(this+" = "+c+" using "+u+" term="+c.term+"/"+term.capply(u)); + return c; + } + public PlanBody clone() { PlanBodyImpl c; if (term == null) // (NIDE) must copy c.isTerm even if cloning empty plan Modified: trunk/src/jason/asSyntax/Pred.java =================================================================== --- trunk/src/jason/asSyntax/Pred.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/Pred.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -27,7 +27,6 @@ import jason.asSyntax.parser.as2j; import java.io.StringReader; -import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -61,10 +60,20 @@ } } + // used by capply + protected Pred(Literal l, Unifier u) { + super(l,u); + if (l.hasAnnot()) { + setAnnots( (ListTerm)l.getAnnots().capply(u) ); + } else { + annots = null; + } + } + public Pred(String functor, int termsSize) { super(functor, termsSize); } - + public static Pred parsePred(String sPred) { as2j parser = new as2j(new StringReader(sPred)); try { @@ -94,19 +103,27 @@ } } + /* @Override public boolean apply(Unifier u) { - boolean r = super.apply(u); + boolean r1 = super.apply(u); + boolean r2 = applyAnnots(u); + return r1 || r2; + } + */ + /* + private final boolean applyAnnots(Unifier u) { + boolean r = false; if (annots != null) { // if some annotation has variables that become ground, they need to be replaced in the list to maintain the order - List<Term> toAdd = null; + List<Term> toAdd = null; Iterator<ListTerm> i = annots.listTermIterator(); while (i.hasNext()) { ListTerm lt = i.next(); if (lt.isTail() && lt.getTail().apply(u)) { // have to test tail before term, since term test may lead to i.remove that remove also the tail r = true; lt.getTerm().apply(u); // apply for the term - setAnnots(annots); // sort all annots given in from tail ground + setAnnots(annots); // sort all annots given from tail ground break; // the iterator is inconsistent } else if (lt.getTerm() != null && lt.getTerm().apply(u)) { r = true; @@ -120,8 +137,9 @@ for (Term t: toAdd) addAnnot(t); } - return r; + return r; } + */ @Override public Literal setAnnots(ListTerm l) { @@ -542,6 +560,11 @@ return 0; } + @Override + public Term capply(Unifier u) { + return new Pred(this,u); + } + public Term clone() { return new Pred(this); } Modified: trunk/src/jason/asSyntax/RelExpr.java =================================================================== --- trunk/src/jason/asSyntax/RelExpr.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/RelExpr.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -81,13 +81,13 @@ } public Iterator<Unifier> logicalConsequence(final Agent ag, Unifier un) { - Term xp = getTerm(0).clone(); - Term yp = getTerm(1).clone(); - xp.apply(un); - yp.apply(un); + Term xp = getTerm(0).capply(un); + Term yp = getTerm(1).capply(un); switch (op) { + case none: break; + case gt : if (xp.compareTo(yp) > 0) return LogExpr.createUnifIterator(un); break; case gte: if (xp.compareTo(yp) >= 0) return LogExpr.createUnifIterator(un); break; case lt : if (xp.compareTo(yp) < 0) return LogExpr.createUnifIterator(un); break; @@ -143,9 +143,14 @@ return null; } + @Override + public Term capply(Unifier u) { + return new RelExpr(getTerm(0).capply(u), op, getTerm(1).capply(u)); + } + /** make a hard copy of the terms */ public LogicalFormula clone() { - return new RelExpr(getTerm(0).clone(), op, getTerm(1).clone()); + return new RelExpr(getTerm(0).clone(), op, getTerm(1).clone()); } /** gets the Operation of this Expression */ Modified: trunk/src/jason/asSyntax/Rule.java =================================================================== --- trunk/src/jason/asSyntax/Rule.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/Rule.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -56,6 +56,13 @@ this.body = body; } + public Rule(Rule r, Unifier u) { + super(r,u); + body = (LogicalFormula)r.body.capply(u); + predicateIndicatorCache = null; + } + + @Override public boolean isRule() { return true; @@ -71,8 +78,8 @@ } @Override - public int hashCode() { - return super.hashCode() + body.hashCode(); + protected int calcHashCode() { + return super.calcHashCode() + body.hashCode(); } public LogicalFormula getBody() { @@ -86,17 +93,26 @@ return super.makeVarsAnnon(un); } + @Override + public Term capply(Unifier u) { + return new Rule(this,u); + } + public Rule clone() { Rule r = new Rule((Literal)super.clone(), (LogicalFormula)body.clone()); r.predicateIndicatorCache = null; r.resetHashCodeCache(); return r; } - + public Literal headClone() { return (Literal)super.clone(); } + public Literal headCApply(Unifier u) { + return (Literal)super.capply(u); + } + public String toString() { return super.toString() + " :- " + body; } Modified: trunk/src/jason/asSyntax/Structure.java =================================================================== --- trunk/src/jason/asSyntax/Structure.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/Structure.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -69,6 +69,16 @@ terms.add(l.getTerm(i).clone()); } + // used by capply + protected Structure(Literal l, Unifier u) { + super(l); + final int tss = l.getArity(); + terms = new ArrayList<Term>(tss); + for (int i = 0; i < tss; i++) + terms.add(l.getTerm(i).capply(u)); + resetHashCodeCache(); + } + /** * Create a structure with a defined number of terms. * @@ -172,6 +182,7 @@ } + /* @Override public boolean apply(Unifier u) { boolean r = false; @@ -185,7 +196,12 @@ resetHashCodeCache(); return r; } + */ + @Override + public Term capply(Unifier u) { + return new Structure(this,u); + } /** make a deep copy of the terms */ public Term clone() { @@ -287,6 +303,10 @@ return true; } + public boolean isUnary() { + return getArity() == 1; + } + @Override public Literal makeVarsAnnon() { return makeVarsAnnon(new Unifier()); Modified: trunk/src/jason/asSyntax/Term.java =================================================================== --- trunk/src/jason/asSyntax/Term.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/Term.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -41,8 +41,11 @@ public boolean subsumes(Term l); /** replaces variables by their values in the unifier, returns true if some variable was applied */ - public boolean apply(Unifier u); + //public boolean apply(Unifier u); + /** clone and applies together (and faster than clone and then apply) */ + public Term capply(Unifier u); + /** Removes the value cached for hashCode */ //public void resetHashCodeCache(); Modified: trunk/src/jason/asSyntax/Trigger.java =================================================================== --- trunk/src/jason/asSyntax/Trigger.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/Trigger.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -167,6 +167,15 @@ c.isTerm = isTerm; return c; } + + @Override + public Trigger capply(Unifier u) { + Trigger c = new Trigger(operator, type, (Literal)literal.capply(u)); + c.predicateIndicatorCache = this.predicateIndicatorCache; + c.isTerm = isTerm; + return c; + } + /** return [+|-][!|?] super.getPredicateIndicator */ @Override @@ -177,9 +186,9 @@ return predicateIndicatorCache; } - public boolean apply(Unifier u) { + /*public boolean apply(Unifier u) { return literal.apply(u); - } + }*/ public Literal getLiteral() { return literal; @@ -211,7 +220,7 @@ if (t instanceof Trigger) { return (Trigger)t; } - if (t instanceof VarTerm) { + /*if (t instanceof VarTerm) { VarTerm v = (VarTerm)t; if (v.hasValue() && v.getValue() instanceof Trigger) { return (Trigger)v.getValue(); @@ -219,7 +228,7 @@ if (v.hasValue() && v.getValue() instanceof Plan) { return ((Plan)v.getValue()).getTrigger(); } - } + }*/ if (t.isString()) { return ASSyntax.parseTrigger(((StringTerm)t).getString()); } Modified: trunk/src/jason/asSyntax/UnnamedVar.java =================================================================== --- trunk/src/jason/asSyntax/UnnamedVar.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/UnnamedVar.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -66,22 +66,22 @@ */ public Term clone() { - if (hasValue()) { + /*if (hasValue()) { return getValue().clone(); - } else { + } else {*/ UnnamedVar newv = new UnnamedVar(getFunctor()); newv.myId = this.myId; //newv.fromRename = this.fromRename; if (hasAnnot()) newv.addAnnots(this.getAnnots().cloneLT()); return newv; - } + //} } public int compareTo(Term t) { - if (hasValue()) { + /*if (hasValue()) { return super.compareTo(t); - } else if (t instanceof UnnamedVar) { + } else */if (t instanceof UnnamedVar) { if (myId > ((UnnamedVar)t).myId) return 1; else if (myId < ((UnnamedVar)t).myId) @@ -97,6 +97,6 @@ @Override public boolean isUnnamedVar() { - return !hasValue(); + return true; //!hasValue(); } } Modified: trunk/src/jason/asSyntax/VarTerm.java =================================================================== --- trunk/src/jason/asSyntax/VarTerm.java 2013-11-04 11:27:47 UTC (rev 1756) +++ trunk/src/jason/asSyntax/VarTerm.java 2013-11-07 14:02:11 UTC (rev 1757) @@ -23,6 +23,7 @@ package jason.asSyntax; +import jason.NoValueForVarException; import jason.asSemantics.Agent; import jason.asSemantics.Unifier; import jason.asSyntax.parser.as2j; @@ -50,12 +51,12 @@ * * @author jomi */ -public class VarTerm extends LiteralImpl implements NumberTerm, ListTerm, StringTerm, ObjectTerm, PlanBody { +public class VarTerm extends LiteralImpl implements NumberTerm, ListTerm { //, StringTerm, ObjectTerm, PlanBody { private static final long serialVersionUID = 1L; private static Logger logger = Logger.getLogger(VarTerm.class.getName()); - private Term value = null; + //private Term value = null; public VarTerm(String s) { super(s); @@ -79,33 +80,71 @@ return null; } } + + @Override + public Term capply(Unifier u) { + if (u != null) { + Term vl = u.get(this); + if (vl != null) { + if (!vl.isCyclicTerm() && vl.hasVar(this, u)) { + //logger.warning("The value of a variable contains itself, variable "+super.getFunctor()+" "+super.getSrcInfo()+", value="+vl+", unifier="+u); + + u.remove(this); // remove this var to avoid loops in the apply below + Term tempVl = vl.capply(u); + u.bind(this, vl); + + CyclicTerm ct = new CyclicTerm((Literal)tempVl, this); + Unifier renamedVars = new Unifier(); // remove "this" from the value to avoid loops in apply + ct.makeVarsAnnon(renamedVars); + renamedVars.remove(this); + u.compose(renamedVars); + vl = ct; + } + + vl = vl.capply(u); // should clone here, since there is no cloning in unify + // decide whether to use var annots in apply + // X = p[a] + // !X[b] + // what's the event: + // +!p[a] + // or + // +!p[a,b] + // Answer: use annots of var, useful for meta-programming like + // P[step(N)] + if (vl.isLiteral() && this.hasAnnot()) { // if this var has annots, add them in the value's annots (Experimental) + vl = ((Literal)vl).forceFullLiteralImpl().addAnnots((ListTerm)this.getAnnots().capply(u)); + } + return vl; + } + } + return clone(); + } public Term clone() { - if (value != null) { - return value.clone(); - } else { - // do not call constructor with term parameter! - VarTerm t = new VarTerm(super.getFunctor()); - t.setNegated(!negated()); - t.srcInfo = this.srcInfo; - if (hasAnnot()) - t.setAnnots(getAnnots().cloneLT()); - return t; - } + // do not call constructor with term parameter! + VarTerm t = new VarTerm(super.getFunctor()); + t.setNegated(!negated()); + t.srcInfo = this.srcInfo; + if (hasAnnot()) + t.setAnnots(getAnnots().cloneLT()); + return t; } - + + /* public PlanBody clonePB() { return (PlanBody)clone(); } + */ public ListTerm cloneLT() { return (ListTerm)clone(); } + @Override public boolean isVar() { - return value == null; + return true; } public boolean isUnnamedVar() { @@ -114,14 +153,14 @@ @Override public boolean isGround() { - return value != null && value.isGround(); + return false; } /** * grounds a variable, set a value for this var * (e.g. X = 10; Y = a(b,c); ...) */ - public boolean setValue(Term vl) { + /*public boolean setValue(Term vl) { if (vl.isVar()) { logger.log(Level.WARNING, "Attempted set a variable as a value for a variable, in " + this.getFunctor(), new Exception()); return false; @@ -144,13 +183,15 @@ value = vl; resetHashCodeCache(); return true; - } + }*/ /** returns true if this var has a value */ +/* public boolean hasValue() { return value != null; } - +*/ + /* public boolean apply(Unifier u) { if (value == null) { ... [truncated message content] |