diff --git a/nars_core_java/nars/inference/CompositionalRules.java b/nars_core_java/nars/inference/CompositionalRules.java index 8c63adebe6a4393143c53f7c457b01f335683a74..5c4c2997ff523c1766361de2f43e536e54ebddbd 100644 --- a/nars_core_java/nars/inference/CompositionalRules.java +++ b/nars_core_java/nars/inference/CompositionalRules.java @@ -37,7 +37,184 @@ import nars.storage.Memory; */ public final class CompositionalRules { - static void IntroVarSameSubject(Sentence originalMainSentence, Sentence subSentence, Term component, Term content, int index,Memory memory) { + public static void EliminateVariableOfConditionAbductive(int figure,Sentence sentence,Sentence belief,Memory memory) { + Term T1=(Term) sentence.getContent().clone(); + Term T2=(Term) belief.getContent().clone(); + Term S1=((Statement)T2).getSubject(); + Term P1=((Statement)T2).getPredicate(); + Term S2=((Statement)T1).getSubject(); + Term P2=((Statement)T1).getPredicate(); + + if(figure==21) { + HashMap<Term,Term> res1=new HashMap<>(); + HashMap<Term,Term> res2=new HashMap<>(); + Variable.findSubstitute(Symbols.VAR_INDEPENDENT, P1, S2, res1, res2); //this part is + ((CompoundTerm) T1).applySubstitute(res2); //independent, the rule works if it unifies + ((CompoundTerm) T2).applySubstitute(res1); + if(S1 instanceof Conjunction) { + //try to unify P2 with a component + for(Term s1 : ((CompoundTerm)S1).cloneComponents()) { + HashMap<Term,Term> res3=new HashMap<>(); + HashMap<Term,Term> res4=new HashMap<>(); //here the dependent part matters, see example of Issue40 + if(Variable.findSubstitute(Symbols.VAR_DEPENDENT, s1, P2, res3, res4)) { + for(Term s2 : ((CompoundTerm)S1).cloneComponents()) { + ((CompoundTerm) s2).applySubstitute(res3); + if(!s2.equals(s1)) { + TruthValue truth = TruthFunctions.abduction(sentence.getTruth(), belief.getTruth()); + BudgetValue budget = BudgetFunctions.compoundForward(truth, s2, memory); + memory.doublePremiseTask(s2, truth, budget); + } + } + } + } + } + if(P2 instanceof Conjunction) { + //try to unify S1 with a component + for(Term s1 : ((CompoundTerm)P2).cloneComponents()) { + HashMap<Term,Term> res3=new HashMap<>(); + HashMap<Term,Term> res4=new HashMap<>(); //here the dependent part matters, see example of Issue40 + if(Variable.findSubstitute(Symbols.VAR_DEPENDENT, s1, S1, res3, res4)) { + for(Term s2 : ((CompoundTerm)P2).cloneComponents()) { + ((CompoundTerm) s2).applySubstitute(res3); + if(!s2.equals(s1)) { + TruthValue truth = TruthFunctions.abduction(sentence.getTruth(), belief.getTruth()); + BudgetValue budget = BudgetFunctions.compoundForward(truth, s2, memory); + memory.doublePremiseTask(s2, truth, budget); + } + } + } + } + } + } + + if(figure==12) { + HashMap<Term,Term> res1=new HashMap<>(); + HashMap<Term,Term> res2=new HashMap<>(); + Variable.findSubstitute(Symbols.VAR_INDEPENDENT, S1, P2, res1, res2); //this part is + ((CompoundTerm) T1).applySubstitute(res2); //independent, the rule works if it unifies + ((CompoundTerm) T2).applySubstitute(res1); + if(S2 instanceof Conjunction) { + //try to unify P1 with a component + for(Term s1 : ((CompoundTerm)S2).cloneComponents()) { + HashMap<Term,Term> res3=new HashMap<>(); + HashMap<Term,Term> res4=new HashMap<>(); //here the dependent part matters, see example of Issue40 + if(Variable.findSubstitute(Symbols.VAR_DEPENDENT, s1, P1, res3, res4)) { + for(Term s2 : ((CompoundTerm)S2).cloneComponents()) { + ((CompoundTerm) s2).applySubstitute(res3); + if(!s2.equals(s1)) { + TruthValue truth = TruthFunctions.abduction(sentence.getTruth(), belief.getTruth()); + BudgetValue budget = BudgetFunctions.compoundForward(truth, s2, memory); + memory.doublePremiseTask(s2, truth, budget); + } + } + } + } + } + if(P1 instanceof Conjunction) { + //try to unify S2 with a component + for(Term s1 : ((CompoundTerm)P1).cloneComponents()) { + HashMap<Term,Term> res3=new HashMap<>(); + HashMap<Term,Term> res4=new HashMap<>(); //here the dependent part matters, see example of Issue40 + if(Variable.findSubstitute(Symbols.VAR_DEPENDENT, s1, S2, res3, res4)) { + for(Term s2 : ((CompoundTerm)P1).cloneComponents()) { + ((CompoundTerm) s2).applySubstitute(res3); + if(!s2.equals(s1)) { + TruthValue truth = TruthFunctions.abduction(sentence.getTruth(), belief.getTruth()); + BudgetValue budget = BudgetFunctions.compoundForward(truth, s2, memory); + memory.doublePremiseTask(s2, truth, budget); + } + } + } + } + } + } + + if(figure==11) { + HashMap<Term,Term> res1=new HashMap<>(); + HashMap<Term,Term> res2=new HashMap<>(); + Variable.findSubstitute(Symbols.VAR_INDEPENDENT, S1, S2, res1, res2); //this part is + ((CompoundTerm) T1).applySubstitute(res2); //independent, the rule works if it unifies + ((CompoundTerm) T2).applySubstitute(res1); + if(P1 instanceof Conjunction) { + //try to unify P2 with a component + for(Term s1 : ((CompoundTerm)P1).cloneComponents()) { + HashMap<Term,Term> res3=new HashMap<>(); + HashMap<Term,Term> res4=new HashMap<>(); //here the dependent part matters, see example of Issue40 + if(Variable.findSubstitute(Symbols.VAR_DEPENDENT, s1, P2, res3, res4)) { + for(Term s2 : ((CompoundTerm)P1).cloneComponents()) { + ((CompoundTerm) s2).applySubstitute(res3); + if(!s2.equals(s1)) { + TruthValue truth = TruthFunctions.abduction(sentence.getTruth(), belief.getTruth()); + BudgetValue budget = BudgetFunctions.compoundForward(truth, s2, memory); + memory.doublePremiseTask(s2, truth, budget); + } + } + } + } + } + if(P2 instanceof Conjunction) { + //try to unify P1 with a component + for(Term s1 : ((CompoundTerm)P2).cloneComponents()) { + HashMap<Term,Term> res3=new HashMap<>(); + HashMap<Term,Term> res4=new HashMap<>(); //here the dependent part matters, see example of Issue40 + if(Variable.findSubstitute(Symbols.VAR_DEPENDENT, s1, P1, res3, res4)) { + for(Term s2 : ((CompoundTerm)P2).cloneComponents()) { + ((CompoundTerm) s2).applySubstitute(res3); + if(!s2.equals(s1)) { + TruthValue truth = TruthFunctions.abduction(sentence.getTruth(), belief.getTruth()); + BudgetValue budget = BudgetFunctions.compoundForward(truth, s2, memory); + memory.doublePremiseTask(s2, truth, budget); + } + } + } + } + } + } + + if(figure==22) { + HashMap<Term,Term> res1=new HashMap<>(); + HashMap<Term,Term> res2=new HashMap<>(); + Variable.findSubstitute(Symbols.VAR_INDEPENDENT, P1, P2, res1, res2); //this part is + ((CompoundTerm) T1).applySubstitute(res2); //independent, the rule works if it unifies + ((CompoundTerm) T2).applySubstitute(res1); + if(S1 instanceof Conjunction) { + //try to unify S2 with a component + for(Term s1 : ((CompoundTerm)S1).cloneComponents()) { + HashMap<Term,Term> res3=new HashMap<>(); + HashMap<Term,Term> res4=new HashMap<>(); //here the dependent part matters, see example of Issue40 + if(Variable.findSubstitute(Symbols.VAR_DEPENDENT, s1, S2, res3, res4)) { + for(Term s2 : ((CompoundTerm)S1).cloneComponents()) { + ((CompoundTerm) s2).applySubstitute(res3); + if(!s2.equals(s1)) { + TruthValue truth = TruthFunctions.abduction(sentence.getTruth(), belief.getTruth()); + BudgetValue budget = BudgetFunctions.compoundForward(truth, s2, memory); + memory.doublePremiseTask(s2, truth, budget); + } + } + } + } + } + if(S2 instanceof Conjunction) { + //try to unify S1 with a component + for(Term s1 : ((CompoundTerm)S2).cloneComponents()) { + HashMap<Term,Term> res3=new HashMap<>(); + HashMap<Term,Term> res4=new HashMap<>(); //here the dependent part matters, see example of Issue40 + if(Variable.findSubstitute(Symbols.VAR_DEPENDENT, s1, S1, res3, res4)) { + for(Term s2 : ((CompoundTerm)S2).cloneComponents()) { + ((CompoundTerm) s2).applySubstitute(res3); + if(!s2.equals(s1)) { + TruthValue truth = TruthFunctions.abduction(sentence.getTruth(), belief.getTruth()); + BudgetValue budget = BudgetFunctions.compoundForward(truth, s2, memory); + memory.doublePremiseTask(s2, truth, budget); + } + } + } + } + } + } + } + + static void IntroVarSameSubjectOrPredicate(Sentence originalMainSentence, Sentence subSentence, Term component, Term content, int index,Memory memory) { Sentence cloned=(Sentence) originalMainSentence.clone(); Term T1=cloned.getContent(); if(!(T1 instanceof CompoundTerm) || !(content instanceof CompoundTerm)) { diff --git a/nars_core_java/nars/inference/RuleTables.java b/nars_core_java/nars/inference/RuleTables.java index b0360c03daa90a6637dc419a45de59b6ed51ea2d..c9eb41902c57370b43cafa8d23fdb5f9ad282a21 100644 --- a/nars_core_java/nars/inference/RuleTables.java +++ b/nars_core_java/nars/inference/RuleTables.java @@ -267,6 +267,7 @@ public class RuleTables { t2 = s1.getPredicate(); CompositionalRules.composeCompound(s1, s2, 0, memory); SyllogisticRules.abdIndCom(t1, t2, sentence, belief, figure, memory); + CompositionalRules.EliminateVariableOfConditionAbductive(figure,sentence,belief,memory); } break; @@ -309,6 +310,7 @@ public class RuleTables { CompositionalRules.composeCompound(s1, s2, 1, memory); SyllogisticRules.abdIndCom(t1, t2, sentence, belief, figure, memory); } + CompositionalRules.EliminateVariableOfConditionAbductive(figure,sentence,belief,memory); } break; default: @@ -391,21 +393,25 @@ public class RuleTables { case 11: if (Variable.unify(Symbols.VAR_INDEPENDENT, s1.getSubject(), s2.getSubject(), s1, s2)) { SyllogisticRules.resemblance(s1.getPredicate(), s2.getPredicate(), belief, taskSentence, figure, memory); + CompositionalRules.EliminateVariableOfConditionAbductive(figure,taskSentence,belief,memory); } break; case 12: if (Variable.unify(Symbols.VAR_INDEPENDENT, s1.getSubject(), s2.getPredicate(), s1, s2)) { SyllogisticRules.resemblance(s1.getPredicate(), s2.getSubject(), belief, taskSentence, figure, memory); + CompositionalRules.EliminateVariableOfConditionAbductive(figure,taskSentence,belief,memory); } break; case 21: if (Variable.unify(Symbols.VAR_INDEPENDENT, s1.getPredicate(), s2.getSubject(), s1, s2)) { SyllogisticRules.resemblance(s1.getSubject(), s2.getPredicate(), belief, taskSentence, figure, memory); + CompositionalRules.EliminateVariableOfConditionAbductive(figure,taskSentence,belief,memory); } break; case 22: if (Variable.unify(Symbols.VAR_INDEPENDENT, s1.getPredicate(), s2.getPredicate(), s1, s2)) { SyllogisticRules.resemblance(s1.getSubject(), s2.getSubject(), belief, taskSentence, figure, memory); + CompositionalRules.EliminateVariableOfConditionAbductive(figure,taskSentence,belief,memory); } break; } @@ -437,9 +443,9 @@ public class RuleTables { if (s2.getSubject().equals(((Statement) content).getSubject())) { CompositionalRules.introVarInner((Statement) content, s2, statement, memory); } - CompositionalRules.IntroVarSameSubject(originalMainSentence,subSentence,component,content,index,memory); + CompositionalRules.IntroVarSameSubjectOrPredicate(originalMainSentence,subSentence,component,content,index,memory); } else if ((statement instanceof Equivalence) && (statement.getPredicate() instanceof Statement) && (memory.currentTask.getSentence().isJudgment())) { - CompositionalRules.IntroVarSameSubject(originalMainSentence,subSentence,component,content,index,memory); + CompositionalRules.IntroVarSameSubjectOrPredicate(originalMainSentence,subSentence,component,content,index,memory); } } }