From e802424d3027546df7e1b1c9ff1edee664a00c33 Mon Sep 17 00:00:00 2001
From: patham9 <patham9@91dfdad4-c543-0410-b26a-7d79dded8189>
Date: Sat, 26 Jul 2014 13:20:29 +0000
Subject: [PATCH] solving the correct but useless statement problem initially
 r313 wanted to solve by not letting equivalent premises in respect to image
 and product transformations on first subterm level to be selected

---
 nars_core_java/nars/inference/RuleTables.java |   6 +-
 .../nars/language/CompoundTerm.java           | 218 ++++++++++++++++++
 2 files changed, 222 insertions(+), 2 deletions(-)

diff --git a/nars_core_java/nars/inference/RuleTables.java b/nars_core_java/nars/inference/RuleTables.java
index e72b326..ab65f53 100644
--- a/nars_core_java/nars/inference/RuleTables.java
+++ b/nars_core_java/nars/inference/RuleTables.java
@@ -21,9 +21,9 @@
 package nars.inference;
 
 import nars.entity.*;
+import nars.io.Symbols;
 import nars.language.*;
 import nars.storage.Memory;
-import nars.io.Symbols;
 
 /**
  * Table of inference rules, indexed by the TermLinks for the task and the
@@ -31,7 +31,6 @@ import nars.io.Symbols;
  * to the relevant inference rules.
  */
 public class RuleTables {
-
     /**
      * Entry point of the inference engine
      *
@@ -44,6 +43,9 @@ public class RuleTables {
         Sentence taskSentence = task.getSentence();
         Term taskTerm = (Term) taskSentence.getContent().clone();         // cloning for substitution
         Term beliefTerm = (Term) bLink.getTarget().clone();       // cloning for substitution
+        if(CompoundTerm.EqualSubTermsInRespectToImageAndProduct(taskTerm,beliefTerm)) {
+           return;
+        }
         Concept beliefConcept = memory.termToConcept(beliefTerm);
         Sentence belief = null;
         if (beliefConcept != null) {
diff --git a/nars_core_java/nars/language/CompoundTerm.java b/nars_core_java/nars/language/CompoundTerm.java
index 6f87a0c..48b5168 100644
--- a/nars_core_java/nars/language/CompoundTerm.java
+++ b/nars_core_java/nars/language/CompoundTerm.java
@@ -735,4 +735,222 @@ public abstract class CompoundTerm extends Term {
             }
         }
     }
+    
+    static boolean EqualSubjectPredicateInRespectToImageAndProduct(Term a, Term b) {
+        if(a==null || b==null) {
+            return false;
+        }
+        if(!(a instanceof Statement) && !(b instanceof Statement))
+            return false;
+        if(a.equals(b)) {
+            return true;
+        }
+        Statement A=(Statement) a;
+        Statement B=(Statement) b;
+        if(A instanceof Similarity && B instanceof Similarity || A instanceof Inheritance && B instanceof Inheritance) {
+            Term subjA=A.getSubject();
+            Term predA=A.getPredicate();
+            Term subjB=B.getSubject();
+            Term predB=B.getPredicate();
+
+            //ok we know they are not equal, its time to determine how image structure could make them equal:
+            if(subjA instanceof Product) { //A is a product, so B must be a extensional image to make true
+                if(predB instanceof ImageExt) {
+                    //now the components of both statements need to be the same:
+                    ArrayList<Term> componentsA=new ArrayList<>();
+                    ArrayList<Term> componentsB=new ArrayList<>();
+                    componentsA.add(predA);
+                    componentsB.add(subjB);
+                    for(Term t : ((CompoundTerm)subjA).getComponents()) {
+                        componentsA.add(t);
+                    }
+                    for(Term t : ((CompoundTerm)predB).getComponents()) {
+                        componentsB.add(t);
+                    }
+                    if(componentsA.containsAll(componentsB)) {
+                        return true;
+                    }
+                }
+            }
+            
+            if(subjB instanceof Product) { //B is a product, so A must be a extensional image to make true
+                if(predA instanceof ImageExt) {
+                    //now the components of both statements need to be the same:
+                    ArrayList<Term> componentsA=new ArrayList<>();
+                    ArrayList<Term> componentsB=new ArrayList<>();
+                    componentsA.add(subjA);
+                    componentsB.add(predB);
+                    for(Term t : ((CompoundTerm)predA).getComponents()) {
+                        componentsA.add(t);
+                    }
+                    for(Term t : ((CompoundTerm)subjB).getComponents()) {
+                        componentsB.add(t);
+                    }
+                    if(componentsA.containsAll(componentsB)) {
+                        return true;
+                    }
+                }
+            }
+            
+            if(predA instanceof ImageExt) { //A is a extensional image, so B must be a extensional image to make true
+                if(predB instanceof ImageExt) {
+                    //now the components of both statements need to be the same:
+                    ArrayList<Term> componentsA=new ArrayList<>();
+                    ArrayList<Term> componentsB=new ArrayList<>();
+                    componentsA.add(subjA);
+                    componentsB.add(subjB);
+                    for(Term t : ((CompoundTerm)predA).getComponents()) {
+                        componentsA.add(t);
+                    }
+                    for(Term t : ((CompoundTerm)predB).getComponents()) {
+                        componentsB.add(t);
+                    }
+                    if(componentsA.containsAll(componentsB)) {
+                        return true;
+                    }
+                }
+            }
+            
+            if(predA instanceof ImageExt) { //A is a extensional image, so B must be a extensional image to make true
+                if(predB instanceof ImageExt) {
+                    //now the components of both statements need to be the same:
+                    ArrayList<Term> componentsA=new ArrayList<>();
+                    ArrayList<Term> componentsB=new ArrayList<>();
+                    componentsA.add(subjA);
+                    componentsB.add(subjB);
+                    for(Term t : ((CompoundTerm)predA).getComponents()) {
+                        componentsA.add(t);
+                    }
+                    for(Term t : ((CompoundTerm)predB).getComponents()) {
+                        componentsB.add(t);
+                    }
+                    if(componentsA.containsAll(componentsB)) {
+                        return true;
+                    }
+                }
+            }
+            
+            if(subjA instanceof ImageInt) { //A is a intensional image, so B must be a intensional image to make true
+                if(subjB instanceof ImageInt) {
+                    //now the components of both statements need to be the same:
+                    ArrayList<Term> componentsA=new ArrayList<>();
+                    ArrayList<Term> componentsB=new ArrayList<>();
+                    componentsA.add(predA);
+                    componentsB.add(predB);
+                    for(Term t : ((CompoundTerm)subjA).getComponents()) {
+                        componentsA.add(t);
+                    }
+                    for(Term t : ((CompoundTerm)subjB).getComponents()) {
+                        componentsB.add(t);
+                    }
+                    if(componentsA.containsAll(componentsB)) {
+                        return true;
+                    }
+                }
+            }
+            
+            if(subjA instanceof ImageInt) { //A is a intensional image, so B must be a intensional image to make true
+                if(subjB instanceof ImageInt) {
+                    //now the components of both statements need to be the same:
+                    ArrayList<Term> componentsA=new ArrayList<>();
+                    ArrayList<Term> componentsB=new ArrayList<>();
+                    componentsA.add(predA);
+                    componentsB.add(predB);
+                    for(Term t : ((CompoundTerm)subjA).getComponents()) {
+                        componentsA.add(t);
+                    }
+                    for(Term t : ((CompoundTerm)subjB).getComponents()) {
+                        componentsB.add(t);
+                    }
+                    if(componentsA.containsAll(componentsB)) {
+                        return true;
+                    }
+                }
+            }
+            
+            if(predA instanceof Product) { //A is a product, so B must be a intensional image to make true
+                if(subjB instanceof ImageInt) {
+                    //now the components of both statements need to be the same:
+                    ArrayList<Term> componentsA=new ArrayList<>();
+                    ArrayList<Term> componentsB=new ArrayList<>();
+                    componentsA.add(subjA);
+                    componentsB.add(predB);
+                    for(Term t : ((CompoundTerm)predA).getComponents()) {
+                        componentsA.add(t);
+                    }
+                    for(Term t : ((CompoundTerm)subjB).getComponents()) {
+                        componentsB.add(t);
+                    }
+                    if(componentsA.containsAll(componentsB)) {
+                        return true;
+                    }
+                }
+            }
+            
+            if(predB instanceof Product) { //A is a product, so B must be a intensional image to make true
+                if(subjA instanceof ImageInt) {
+                    //now the components of both statements need to be the same:
+                    ArrayList<Term> componentsA=new ArrayList<>();
+                    ArrayList<Term> componentsB=new ArrayList<>();
+                    componentsA.add(predA);
+                    componentsB.add(subjB);
+                    for(Term t : ((CompoundTerm)subjA).getComponents()) {
+                        componentsA.add(t);
+                    }
+                    for(Term t : ((CompoundTerm)predB).getComponents()) {
+                        componentsB.add(t);
+                    }
+                    if(componentsA.containsAll(componentsB)) {
+                        return true;
+                    }
+                }
+            }
+        }
+        return false;
+    }
+    public static boolean EqualSubTermsInRespectToImageAndProduct(Term a,Term b) {
+        if(a==null || b==null) {
+            return false;
+        }
+        if(!((a instanceof CompoundTerm) && (b instanceof CompoundTerm))) {
+            return a.equals(b);
+        }
+        if(a instanceof Inheritance && b instanceof Inheritance) {
+            return EqualSubjectPredicateInRespectToImageAndProduct(a,b);
+        }
+        if(a instanceof Similarity && b instanceof Similarity) {
+            return EqualSubjectPredicateInRespectToImageAndProduct(a,b) || EqualSubjectPredicateInRespectToImageAndProduct(b,a);
+        }
+        ArrayList<Term> A=((CompoundTerm) a).getComponents();
+        ArrayList<Term> B=((CompoundTerm) b).getComponents();
+        if(A.size()!=B.size()) {
+            return false;
+        }
+        else {
+            for(int i=0;i<A.size();i++) {
+                Term x = A.get(i);
+                Term y = B.get(i);
+                if(!x.equals(y)) {
+                    if(x instanceof Inheritance && y instanceof Inheritance) {
+                        if(!EqualSubjectPredicateInRespectToImageAndProduct(x,y)) {
+                            return false;
+                        }
+                        else {
+                            continue;
+                        }
+                    }
+                    if(x instanceof Similarity && y instanceof Similarity) {
+                        if(!EqualSubjectPredicateInRespectToImageAndProduct(x,y) && !EqualSubjectPredicateInRespectToImageAndProduct(y,x)) {
+                            return false;
+                        }
+                        else {
+                            continue;
+                        }
+                    }
+                    return false;
+                }
+            }
+            return true;
+        }
+    }
 }
-- 
GitLab