|
From: <jom...@us...> - 2012-03-05 21:23:18
|
Revision: 1675
http://jason.svn.sourceforge.net/jason/?rev=1675&view=rev
Author: jomifred
Date: 2012-03-05 21:23:12 +0000 (Mon, 05 Mar 2012)
Log Message:
-----------
improvements in cycled terms
Modified Paths:
--------------
trunk/src/jason/asSemantics/Unifier.java
trunk/src/jason/asSyntax/Structure.java
trunk/src/jason/asSyntax/VarTerm.java
trunk/src/test/TermTest.java
Modified: trunk/src/jason/asSemantics/Unifier.java
===================================================================
--- trunk/src/jason/asSemantics/Unifier.java 2012-03-01 18:16:35 UTC (rev 1674)
+++ trunk/src/jason/asSemantics/Unifier.java 2012-03-05 21:23:12 UTC (rev 1675)
@@ -28,6 +28,7 @@
import jason.asSyntax.ListTerm;
import jason.asSyntax.ListTermImpl;
import jason.asSyntax.Literal;
+import jason.asSyntax.LiteralImpl;
import jason.asSyntax.Pred;
import jason.asSyntax.Structure;
import jason.asSyntax.Term;
@@ -137,10 +138,26 @@
}
}
- if (t1g.isCyclicTerm()) // reintroduce cycles in the unifier
- bind(t1g.getCyclicVar(), t1g);
- if (t2g.isCyclicTerm())
- bind(t2g.getCyclicVar(), t2g);
+ if (t1g.isCyclicTerm() && t2g.isCyclicTerm()) { // both are cycled terms
+ // unification of cyclic terms:
+ // remove the vars (to avoid loops) and test just the structure, then reintroduce the vars
+ VarTerm v1 = t1g.getCyclicVar();
+ VarTerm v2 = t2g.getCyclicVar();
+ remove(v1);
+ remove(v2);
+ try {
+ return unifiesNoUndo(new LiteralImpl((Literal)t1g), new LiteralImpl((Literal)t2g));
+ } finally {
+ function.put(v1, t1g);
+ function.put(v2, t1g);
+ }
+
+ } else {
+ if (t1g.isCyclicTerm() && get(t1g.getCyclicVar()) == null) // reintroduce cycles in the unifier
+ function.put(t1g.getCyclicVar(), t1g);
+ if (t2g.isCyclicTerm() && get(t2g.getCyclicVar()) == null)
+ function.put(t2g.getCyclicVar(), t2g);
+ }
// unify as Term
boolean ok = unifyTerms(t1g, t2g);
@@ -291,6 +308,10 @@
}
public void bind(VarTerm vt, Term vl) {
+ if (!vl.isCyclicTerm() && vl.hasVar(vt, this)) {
+ vl = new CyclicTerm((Literal)vl, (VarTerm)vt.clone());
+ //System.out.println("changing "+vt+" <- "+vl+" in "+this);
+ }
function.put((VarTerm) vt.clone(), vl);
}
Modified: trunk/src/jason/asSyntax/Structure.java
===================================================================
--- trunk/src/jason/asSyntax/Structure.java 2012-03-01 18:16:35 UTC (rev 1674)
+++ trunk/src/jason/asSyntax/Structure.java 2012-03-05 21:23:12 UTC (rev 1675)
@@ -295,7 +295,7 @@
// if the variable hasn't been renamed given the input unifier, then rename it.
if (deref.equals(vt)) {
// forget the name
- VarTerm var = new UnnamedVar("_" + UnnamedVar.getUniqueId() + t);
+ VarTerm var = new UnnamedVar(); //("_" + UnnamedVar.getUniqueId() + t);
// if deref has annotations then we need to replicate these in the new variable
if (deref.hasAnnot()) {
var.setAnnots(deref.getAnnots().cloneLT());
Modified: trunk/src/jason/asSyntax/VarTerm.java
===================================================================
--- trunk/src/jason/asSyntax/VarTerm.java 2012-03-01 18:16:35 UTC (rev 1674)
+++ trunk/src/jason/asSyntax/VarTerm.java 2012-03-05 21:23:12 UTC (rev 1675)
@@ -151,10 +151,10 @@
Term vl = u.get(this);
//System.out.println("applying "+this+"="+vl+" un="+u);
if (vl != null) {
- if (vl.hasVar(this, u)) {
+ 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
+ u.remove(this); // remove this var to avoid loops in the apply below
Term tempVl = vl.clone();
tempVl.apply(u);
u.bind(this, vl);
Modified: trunk/src/test/TermTest.java
===================================================================
--- trunk/src/test/TermTest.java 2012-03-01 18:16:35 UTC (rev 1674)
+++ trunk/src/test/TermTest.java 2012-03-05 21:23:12 UTC (rev 1675)
@@ -1065,4 +1065,19 @@
assertTrue(y.isCyclicTerm());
}
+ public void testCyclicTerm5() throws ParseException {
+ Term t1 = parseTerm("f(f(g(X)))");
+ VarTerm v1 = new VarTerm("X");
+
+ Term t2 = parseTerm("f(f(g(Y)))");
+ VarTerm v2 = new VarTerm("Y");
+
+ Unifier u = new Unifier();
+
+ assertTrue(u.unifies(t1, v1));
+ assertTrue(u.get(v1).isCyclicTerm());
+ assertTrue(u.unifies(t2, v2));
+ assertTrue(new Unifier().unifies(t1, t2));
+ assertTrue(u.unifies(v1, v2));
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|