Skip to content
Snippets Groups Projects
Commit c34acbae authored by NARS.Wang@gmail.com's avatar NARS.Wang@gmail.com
Browse files

Resolve Issue 43 and the related issues in variable unification

parent 4a3ef3f6
No related branches found
No related tags found
No related merge requests found
...@@ -426,6 +426,9 @@ public final class CompositionalRules { ...@@ -426,6 +426,9 @@ public final class CompositionalRules {
substitute.put(commonTerm2, new Variable("$varInd2")); substitute.put(commonTerm2, new Variable("$varInd2"));
} }
content = Implication.make(premise1, oldCompound, memory); content = Implication.make(premise1, oldCompound, memory);
if (content == null) {
return;
}
content.applySubstitute(substitute); content.applySubstitute(substitute);
if (premise1.equals(taskSentence.getContent())) { if (premise1.equals(taskSentence.getContent())) {
truth = TruthFunctions.induction(belief.getTruth(), taskSentence.getTruth()); truth = TruthFunctions.induction(belief.getTruth(), taskSentence.getTruth());
......
...@@ -110,8 +110,8 @@ public class LocalRules { ...@@ -110,8 +110,8 @@ public class LocalRules {
Variable.unify(Symbols.VAR_INDEPENDENT, content, problem.cloneContent()); Variable.unify(Symbols.VAR_INDEPENDENT, content, problem.cloneContent());
belief = (Sentence) belief.clone(); belief = (Sentence) belief.clone();
belief.setContent(content); belief.setContent(content);
Stamp st = new Stamp(memory.currentBelief.getStamp(), memory.getTime()); Stamp st = new Stamp(belief.getStamp(), memory.getTime());
st.addToChain(memory.currentBelief.getContent()); st.addToChain(belief.getContent());
belief.setStamp(st); belief.setStamp(st);
} }
task.setBestSolution(belief); task.setBestSolution(belief);
......
...@@ -645,8 +645,11 @@ public abstract class CompoundTerm extends Term { ...@@ -645,8 +645,11 @@ public abstract class CompoundTerm extends Term {
Term t1, t2; Term t1, t2;
for (int i = 0; i < size(); i++) { for (int i = 0; i < size(); i++) {
t1 = componentAt(i); t1 = componentAt(i);
t2 = subs.get(t1); if (subs.containsKey(t1)) {
if (t2 != null) { t2 = subs.get(t1);
while (subs.containsKey(t2)) {
t2 = subs.get(t2);
}
components.set(i, (Term) t2.clone()); components.set(i, (Term) t2.clone());
} else if (t1 instanceof CompoundTerm) { } else if (t1 instanceof CompoundTerm) {
((CompoundTerm) t1).applySubstitute(subs); ((CompoundTerm) t1).applySubstitute(subs);
......
...@@ -149,13 +149,15 @@ public class Variable extends Term { ...@@ -149,13 +149,15 @@ public class Variable extends Term {
HashMap<Term, Term> map2 = new HashMap<>(); HashMap<Term, Term> map2 = new HashMap<>();
boolean hasSubs = findSubstitute(type, t1, t2, map1, map2); // find substitution boolean hasSubs = findSubstitute(type, t1, t2, map1, map2); // find substitution
if (hasSubs) { if (hasSubs) {
renameVar(map1, compound1, "-1"); // renameVar(map1, compound1, "-1");
renameVar(map2, compound2, "-2"); // renameVar(map2, compound2, "-2");
if (!map1.isEmpty()) { if (!map1.isEmpty()) {
((CompoundTerm) compound1).applySubstitute(map1); ((CompoundTerm) compound1).applySubstitute(map1);
((CompoundTerm) compound1).renameVariables();
} }
if (!map2.isEmpty()) { if (!map2.isEmpty()) {
((CompoundTerm) compound2).applySubstitute(map2); ((CompoundTerm) compound2).applySubstitute(map2);
((CompoundTerm) compound2).renameVariables();
} }
} }
return hasSubs; return hasSubs;
...@@ -175,52 +177,46 @@ public class Variable extends Term { ...@@ -175,52 +177,46 @@ public class Variable extends Term {
private static boolean findSubstitute(char type, Term term1, Term term2, private static boolean findSubstitute(char type, Term term1, Term term2,
HashMap<Term, Term> map1, HashMap<Term, Term> map2) { HashMap<Term, Term> map1, HashMap<Term, Term> map2) {
Term t; Term t;
if (term1 instanceof Variable) { if ((term1 instanceof Variable) && (((Variable) term1).getType() == type)) {
Variable var1 = (Variable) term1; Variable var1 = (Variable) term1;
t = map1.get(var1); t = map1.get(var1);
if (t != null) { // already mapped if (t != null) { // already mapped
return findSubstitute(type, t, term2, map1, map2); return findSubstitute(type, t, term2, map1, map2);
} else { // not mapped yet } else { // not mapped yet
if (var1.getType() == type) { if ((term2 instanceof Variable) && (((Variable) term2).getType() == type)) {
if ((term2 instanceof Variable) && (((Variable) term2).getType() == type)) { Variable CommonVar = makeCommonVariable(term1, term2);
Variable var = new Variable(var1.getName() + term2.getName()); map1.put(var1, CommonVar); // unify
map1.put(var1, var); // unify map2.put(term2, CommonVar); // unify
map2.put(term2, var); // unify } else {
} else { map1.put(var1, term2); // elimination
map1.put(var1, term2); // elimination if (isCommonVariable(var1)) {
map2.put(var1, term2);
} }
} else { // different type
// map1.put(var1, new Variable(var1.getName() + "-1")); // rename
return false;
} }
return true; return true;
} }
} } else if ((term2 instanceof Variable) && (((Variable) term2).getType() == type)) {
if (term2 instanceof Variable) {
Variable var2 = (Variable) term2; Variable var2 = (Variable) term2;
t = map2.get(var2); t = map2.get(var2);
if (t != null) { // already mapped if (t != null) { // already mapped
return findSubstitute(type, term1, t, map1, map2); return findSubstitute(type, term1, t, map1, map2);
} else { // not mapped yet } else { // not mapped yet
if (var2.getType() == type) { map2.put(var2, term1); // elimination
map2.put(var2, term1); // elimination if (isCommonVariable(var2)) {
} else { map1.put(var2, term1);
// map2.put(var2, new Variable(var2.getName() + "-2")); // rename
return false;
} }
return true; return true;
} }
} } else if ((term1 instanceof CompoundTerm) && term1.getClass().equals(term2.getClass())) {
if ((term1 instanceof CompoundTerm) && term1.getClass().equals(term2.getClass())) {
CompoundTerm cTerm1 = (CompoundTerm) term1; CompoundTerm cTerm1 = (CompoundTerm) term1;
CompoundTerm cTerm2 = (CompoundTerm) term2; CompoundTerm cTerm2 = (CompoundTerm) term2;
if (cTerm1.size() != (cTerm2).size()) { if (cTerm1.size() != (cTerm2).size()) {
return false; return false;
} }
if ((cTerm1 instanceof ImageExt) && (((ImageExt)cTerm1).getRelationIndex() != ((ImageExt)cTerm2).getRelationIndex())) { if ((cTerm1 instanceof ImageExt) && (((ImageExt) cTerm1).getRelationIndex() != ((ImageExt) cTerm2).getRelationIndex())) {
return false; return false;
} }
if ((cTerm1 instanceof ImageInt) && (((ImageInt)cTerm1).getRelationIndex() != ((ImageInt)cTerm2).getRelationIndex())) { if ((cTerm1 instanceof ImageInt) && (((ImageInt) cTerm1).getRelationIndex() != ((ImageInt) cTerm2).getRelationIndex())) {
return false; return false;
} }
for (int i = 0; i < cTerm1.size(); i++) { // assuming matching order, to be refined in the future for (int i = 0; i < cTerm1.size(); i++) { // assuming matching order, to be refined in the future
...@@ -236,6 +232,15 @@ public class Variable extends Term { ...@@ -236,6 +232,15 @@ public class Variable extends Term {
return term1.equals(term2); // for atomic constant terms return term1.equals(term2); // for atomic constant terms
} }
private static Variable makeCommonVariable(Term v1, Term v2) {
return new Variable(v1.getName() + v2.getName() + '$');
}
private static boolean isCommonVariable(Variable v) {
String s = v.getName();
return s.charAt(s.length() - 1) == '$';
}
/** /**
* Check if two terms can be unified * Check if two terms can be unified
* *
...@@ -250,23 +255,24 @@ public class Variable extends Term { ...@@ -250,23 +255,24 @@ public class Variable extends Term {
/** /**
* Rename the variables to prepare for unification of two terms * Rename the variables to prepare for unification of two terms
*
* @param map The substitution so far * @param map The substitution so far
* @param term The term to be processed * @param term The term to be processed
* @param suffix The suffix that distinguish the variables in one premise from those from the other * @param suffix The suffix that distinguish the variables in one premise
* from those from the other
*/ */
private static void renameVar(HashMap<Term, Term> map, Term term, String suffix) { // private static void renameVar(HashMap<Term, Term> map, Term term, String suffix) {
if (term instanceof Variable) { // if (term instanceof Variable) {
Term t = map.get(term); // Term t = map.get(term);
if (t == null) { // new mapped yet // if (t == null) { // new mapped yet
map.put(term, new Variable(term.getName() + suffix)); // rename // map.put(term, new Variable(term.getName() + suffix)); // rename
} // }
} else if (term instanceof CompoundTerm) { // } else if (term instanceof CompoundTerm) {
for (Term t : ((CompoundTerm) term).components) { // assuming matching order, to be refined in the future // for (Term t : ((CompoundTerm) term).components) { // assuming matching order, to be refined in the future
renameVar(map, t, suffix); // renameVar(map, t, suffix);
} // }
} // }
} // }
/** /**
* variable terms are listed first alphabetically * variable terms are listed first alphabetically
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment