Search in sources :

Example 16 with TruthValue

use of nars.entity.TruthValue in project opennars by opennars.

the class CompositionalRules method eliminateVariableOfConditionAbductive.

/*
    The other inversion (abduction) should also be studied:
 IN: <<lock1 --> (/,open,$1,_)> ==> <$1 --> key>>.
 IN: <(&&,<#1 --> lock>,<#1 --> (/,open,$2,_)>) ==> <$2 --> key>>.
OUT: <lock1 --> lock>.
    http://code.google.com/p/open-nars/issues/detail?id=40&can=1
    */
public static void eliminateVariableOfConditionAbductive(final int figure, final Sentence sentence, final Sentence belief, final DerivationContext nal) {
    Statement T1 = (Statement) sentence.term;
    Statement T2 = (Statement) belief.term;
    Term S1 = T2.getSubject();
    Term S2 = T1.getSubject();
    Term P1 = T2.getPredicate();
    Term P2 = T1.getPredicate();
    HashMap<Term, Term> res1 = new HashMap<>();
    HashMap<Term, Term> res2 = new HashMap<>();
    HashMap<Term, Term> res3 = new HashMap<>();
    HashMap<Term, Term> res4 = new HashMap<>();
    if (figure == 21) {
        res1.clear();
        res2.clear();
        // this part is
        Variables.findSubstitute(Symbols.VAR_INDEPENDENT, P1, S2, res1, res2);
        // independent, the rule works if it unifies
        T1 = (Statement) T1.applySubstitute(res2);
        if (T1 == null) {
            return;
        }
        T2 = (Statement) T2.applySubstitute(res1);
        if (T2 == null) {
            return;
        }
        S1 = T2.getSubject();
        S2 = T1.getSubject();
        P1 = T2.getPredicate();
        // update the variables because T1 and T2 may have changed
        P2 = T1.getPredicate();
        if (S1 instanceof Conjunction) {
            // try to unify P2 with a component
            for (final Term s1 : ((CompoundTerm) S1).term) {
                res3.clear();
                // here the dependent part matters, see example of Issue40
                res4.clear();
                if (Variables.findSubstitute(Symbols.VAR_DEPENDENT, s1, P2, res3, res4)) {
                    for (Term s2 : ((CompoundTerm) S1).term) {
                        if (!(s2 instanceof CompoundTerm)) {
                            continue;
                        }
                        s2 = ((CompoundTerm) s2).applySubstitute(res3);
                        if (s2 == null || s2.hasVarIndep()) {
                            continue;
                        }
                        if (!s2.equals(s1) && (sentence.truth != null) && (belief.truth != null)) {
                            TruthValue truth = abduction(sentence.truth, belief.truth);
                            BudgetValue budget = BudgetFunctions.compoundForward(truth, s2, nal);
                            nal.doublePremiseTask(s2, truth, budget, false, false);
                        }
                    }
                }
            }
        }
        if (P2 instanceof Conjunction) {
            // try to unify S1 with a component
            for (final Term s1 : ((CompoundTerm) P2).term) {
                res3.clear();
                // here the dependent part matters, see example of Issue40
                res4.clear();
                if (Variables.findSubstitute(Symbols.VAR_DEPENDENT, s1, S1, res3, res4)) {
                    for (Term s2 : ((CompoundTerm) P2).term) {
                        if (!(s2 instanceof CompoundTerm)) {
                            continue;
                        }
                        s2 = ((CompoundTerm) s2).applySubstitute(res3);
                        if (s2 == null || s2.hasVarIndep()) {
                            continue;
                        }
                        if (!s2.equals(s1) && (sentence.truth != null) && (belief.truth != null)) {
                            TruthValue truth = abduction(sentence.truth, belief.truth);
                            BudgetValue budget = BudgetFunctions.compoundForward(truth, s2, nal);
                            nal.doublePremiseTask(s2, truth, budget, false, false);
                        }
                    }
                }
            }
        }
    }
    if (figure == 12) {
        res1.clear();
        res2.clear();
        // this part is
        Variables.findSubstitute(Symbols.VAR_INDEPENDENT, S1, P2, res1, res2);
        // independent, the rule works if it unifies
        T1 = (Statement) T1.applySubstitute(res2);
        if (T1 == null) {
            return;
        }
        T2 = (Statement) T2.applySubstitute(res1);
        if (T2 == null) {
            return;
        }
        S1 = T2.getSubject();
        S2 = T1.getSubject();
        P1 = T2.getPredicate();
        // update the variables because T1 and T2 may have changed
        P2 = T1.getPredicate();
        if (S2 instanceof Conjunction) {
            // try to unify P1 with a component
            for (final Term s1 : ((CompoundTerm) S2).term) {
                res3.clear();
                // here the dependent part matters, see example of Issue40
                res4.clear();
                if (Variables.findSubstitute(Symbols.VAR_DEPENDENT, s1, P1, res3, res4)) {
                    for (Term s2 : ((CompoundTerm) S2).term) {
                        if (!(s2 instanceof CompoundTerm)) {
                            continue;
                        }
                        s2 = ((CompoundTerm) s2).applySubstitute(res3);
                        if (s2 == null || s2.hasVarIndep()) {
                            continue;
                        }
                        if (!s2.equals(s1) && (sentence.truth != null) && (belief.truth != null)) {
                            TruthValue truth = abduction(sentence.truth, belief.truth);
                            BudgetValue budget = BudgetFunctions.compoundForward(truth, s2, nal);
                            nal.doublePremiseTask(s2, truth, budget, false, false);
                        }
                    }
                }
            }
        }
        if (P1 instanceof Conjunction) {
            // try to unify S2 with a component
            for (final Term s1 : ((CompoundTerm) P1).term) {
                res3.clear();
                // here the dependent part matters, see example of Issue40
                res4.clear();
                if (Variables.findSubstitute(Symbols.VAR_DEPENDENT, s1, S2, res3, res4)) {
                    for (Term s2 : ((CompoundTerm) P1).term) {
                        if (!(s2 instanceof CompoundTerm)) {
                            continue;
                        }
                        s2 = ((CompoundTerm) s2).applySubstitute(res3);
                        if (s2 == null || s2.hasVarIndep()) {
                            continue;
                        }
                        if (!s2.equals(s1) && (sentence.truth != null) && (belief.truth != null)) {
                            TruthValue truth = abduction(sentence.truth, belief.truth);
                            BudgetValue budget = BudgetFunctions.compoundForward(truth, s2, nal);
                            nal.doublePremiseTask(s2, truth, budget, false, false);
                        }
                    }
                }
            }
        }
    }
    if (figure == 11) {
        res1.clear();
        res2.clear();
        // this part is
        Variables.findSubstitute(Symbols.VAR_INDEPENDENT, S1, S2, res1, res2);
        // independent, the rule works if it unifies
        T1 = (Statement) T1.applySubstitute(res2);
        if (T1 == null) {
            return;
        }
        T2 = (Statement) T2.applySubstitute(res1);
        if (T2 == null) {
            return;
        }
        S1 = T2.getSubject();
        S2 = T1.getSubject();
        P1 = T2.getPredicate();
        // update the variables because T1 and T2 may have changed
        P2 = T1.getPredicate();
        if (P1 instanceof Conjunction) {
            // try to unify P2 with a component
            for (final Term s1 : ((CompoundTerm) P1).term) {
                res3.clear();
                // here the dependent part matters, see example of Issue40
                res4.clear();
                if (Variables.findSubstitute(Symbols.VAR_DEPENDENT, s1, P2, res3, res4)) {
                    for (Term s2 : ((CompoundTerm) P1).term) {
                        if (!(s2 instanceof CompoundTerm)) {
                            continue;
                        }
                        s2 = ((CompoundTerm) s2).applySubstitute(res3);
                        if (s2 == null || s2.hasVarIndep()) {
                            continue;
                        }
                        if ((!s2.equals(s1)) && (sentence.truth != null) && (belief.truth != null)) {
                            TruthValue truth = abduction(sentence.truth, belief.truth);
                            BudgetValue budget = BudgetFunctions.compoundForward(truth, s2, nal);
                            nal.doublePremiseTask(s2, truth, budget, false, false);
                        }
                    }
                }
            }
        }
        if (P2 instanceof Conjunction) {
            // try to unify P1 with a component
            for (final Term s1 : ((CompoundTerm) P2).term) {
                res3.clear();
                // here the dependent part matters, see example of Issue40
                res4.clear();
                if (Variables.findSubstitute(Symbols.VAR_DEPENDENT, s1, P1, res3, res4)) {
                    for (Term s2 : ((CompoundTerm) P2).term) {
                        if (!(s2 instanceof CompoundTerm)) {
                            continue;
                        }
                        s2 = ((CompoundTerm) s2).applySubstitute(res3);
                        if (s2 == null || s2.hasVarIndep()) {
                            continue;
                        }
                        if (!s2.equals(s1) && (sentence.truth != null) && (belief.truth != null)) {
                            TruthValue truth = abduction(sentence.truth, belief.truth);
                            BudgetValue budget = BudgetFunctions.compoundForward(truth, s2, nal);
                            nal.doublePremiseTask(s2, truth, budget, false, false);
                        }
                    }
                }
            }
        }
    }
    if (figure == 22) {
        res1.clear();
        res2.clear();
        // this part is
        Variables.findSubstitute(Symbols.VAR_INDEPENDENT, P1, P2, res1, res2);
        // independent, the rule works if it unifies
        T1 = (Statement) T1.applySubstitute(res2);
        if (T1 == null) {
            return;
        }
        T2 = (Statement) T2.applySubstitute(res1);
        if (T2 == null) {
            return;
        }
        S1 = T2.getSubject();
        S2 = T1.getSubject();
        P1 = T2.getPredicate();
        // update the variables because T1 and T2 may have changed
        P2 = T1.getPredicate();
        if (S1 instanceof Conjunction) {
            // try to unify S2 with a component
            for (final Term s1 : ((CompoundTerm) S1).term) {
                res3.clear();
                // here the dependent part matters, see example of Issue40
                res4.clear();
                if (Variables.findSubstitute(Symbols.VAR_DEPENDENT, s1, S2, res3, res4)) {
                    for (Term s2 : ((CompoundTerm) S1).term) {
                        if (!(s2 instanceof CompoundTerm)) {
                            continue;
                        }
                        s2 = ((CompoundTerm) s2).applySubstitute(res3);
                        if (s2 == null || s2.hasVarIndep()) {
                            continue;
                        }
                        if (s2 != null && !s2.equals(s1) && (sentence.truth != null) && (belief.truth != null)) {
                            TruthValue truth = abduction(sentence.truth, belief.truth);
                            BudgetValue budget = BudgetFunctions.compoundForward(truth, s2, nal);
                            nal.doublePremiseTask(s2, truth, budget, false, false);
                        }
                    }
                }
            }
        }
        if (S2 instanceof Conjunction) {
            // try to unify S1 with a component
            for (final Term s1 : ((CompoundTerm) S2).term) {
                res3.clear();
                // here the dependent part matters, see example of Issue40
                res4.clear();
                if (Variables.findSubstitute(Symbols.VAR_DEPENDENT, s1, S1, res3, res4)) {
                    for (Term s2 : ((CompoundTerm) S2).term) {
                        if (!(s2 instanceof CompoundTerm)) {
                            continue;
                        }
                        s2 = ((CompoundTerm) s2).applySubstitute(res3);
                        if (s2 == null || s2.hasVarIndep()) {
                            continue;
                        }
                        if (s2 != null && !s2.equals(s1) && (sentence.truth != null) && (belief.truth != null)) {
                            TruthValue truth = abduction(sentence.truth, belief.truth);
                            BudgetValue budget = BudgetFunctions.compoundForward(truth, s2, nal);
                            nal.doublePremiseTask(s2, truth, budget, false, false);
                        }
                    }
                }
            }
        }
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) BudgetValue(nars.entity.BudgetValue) HashMap(java.util.HashMap) Statement(nars.language.Statement) TruthValue(nars.entity.TruthValue) TruthFunctions.reduceConjunction(nars.inference.TruthFunctions.reduceConjunction) Conjunction(nars.language.Conjunction) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term)

Example 17 with TruthValue

use of nars.entity.TruthValue in project opennars by opennars.

the class CompositionalRules method introVarOuter.

/* --------------- rules used for variable introduction --------------- */
/**
 * Introduce a dependent variable in an outer-layer conjunction {<S --> P1>,
 * <S --> P2>} |- (&&, <#x --> P1>, <#x --> P2>)
 *
 * @param taskContent The first premise <M --> S>
 * @param beliefContent The second premise <M --> P>
 * @param index The location of the shared term: 0 for subject, 1 for
 * predicate
 * @param nal Reference to the memory
 */
public static void introVarOuter(final Statement taskContent, final Statement beliefContent, final int index, final DerivationContext nal) {
    if (!(taskContent instanceof Inheritance)) {
        return;
    }
    Variable varInd1 = new Variable("$varInd1");
    Variable varInd2 = new Variable("$varInd2");
    Term term11dependent = null, term12dependent = null, term21dependent = null, term22dependent = null;
    Term term11, term12, term21, term22, commonTerm = null;
    HashMap<Term, Term> subs = new HashMap<>();
    if (index == 0) {
        term11 = varInd1;
        term21 = varInd1;
        term12 = taskContent.getPredicate();
        term22 = beliefContent.getPredicate();
        term12dependent = term12;
        term22dependent = term22;
        if (term12 instanceof ImageExt) {
            if ((/*(ImageExt)*/
            term12).containsTermRecursively(term22)) {
                commonTerm = term22;
            }
            if (commonTerm == null && term12 instanceof ImageExt) {
                commonTerm = ((ImageExt) term12).getTheOtherComponent();
                if (!(term22.containsTermRecursively(commonTerm))) {
                    commonTerm = null;
                }
                if (term22 instanceof ImageExt && ((commonTerm == null) || !(term22).containsTermRecursively(commonTerm))) {
                    commonTerm = ((ImageExt) term22).getTheOtherComponent();
                    if ((commonTerm == null) || !(term12).containsTermRecursively(commonTerm)) {
                        commonTerm = null;
                    }
                }
            }
            if (commonTerm != null) {
                subs.put(commonTerm, varInd2);
                term12 = ((CompoundTerm) term12).applySubstitute(subs);
                if (!(term22 instanceof CompoundTerm)) {
                    term22 = varInd2;
                } else {
                    term22 = ((CompoundTerm) term22).applySubstitute(subs);
                }
            }
        }
        if (commonTerm == null && term22 instanceof ImageExt) {
            if ((/*(ImageExt)*/
            term22).containsTermRecursively(term12)) {
                commonTerm = term12;
            }
            if (commonTerm == null && term22 instanceof ImageExt) {
                commonTerm = ((ImageExt) term22).getTheOtherComponent();
                if (!(term12.containsTermRecursively(commonTerm))) {
                    commonTerm = null;
                }
                if (term12 instanceof ImageExt && ((commonTerm == null) || !(term12).containsTermRecursively(commonTerm))) {
                    commonTerm = ((ImageExt) term12).getTheOtherComponent();
                    if ((commonTerm == null) || !(term22).containsTermRecursively(commonTerm)) {
                        commonTerm = null;
                    }
                }
            }
            if (commonTerm != null) {
                subs.put(commonTerm, varInd2);
                term22 = ((CompoundTerm) term22).applySubstitute(subs);
                if (!(term12 instanceof CompoundTerm)) {
                    term12 = varInd2;
                } else {
                    term12 = ((CompoundTerm) term12).applySubstitute(subs);
                }
            }
        }
    } else {
        term11 = taskContent.getSubject();
        term21 = beliefContent.getSubject();
        term12 = varInd1;
        term22 = varInd1;
        term11dependent = term11;
        term21dependent = term21;
        if (term21 instanceof ImageInt) {
            if ((/*(ImageInt)*/
            term21).containsTermRecursively(term11)) {
                commonTerm = term11;
            }
            if (term11 instanceof ImageInt && commonTerm == null && term21 instanceof ImageInt) {
                commonTerm = ((ImageInt) term11).getTheOtherComponent();
                if (!(term21.containsTermRecursively(commonTerm))) {
                    commonTerm = null;
                }
                if ((commonTerm == null) || !(term21).containsTermRecursively(commonTerm)) {
                    commonTerm = ((ImageInt) term21).getTheOtherComponent();
                    if ((commonTerm == null) || !(term11).containsTermRecursively(commonTerm)) {
                        commonTerm = null;
                    }
                }
            }
            if (commonTerm != null) {
                subs.put(commonTerm, varInd2);
                term21 = ((CompoundTerm) term21).applySubstitute(subs);
                if (!(term11 instanceof CompoundTerm)) {
                    term11 = varInd2;
                } else {
                    term11 = ((CompoundTerm) term11).applySubstitute(subs);
                }
            }
        }
        if (commonTerm == null && term11 instanceof ImageInt) {
            if ((/*(ImageInt)*/
            term11).containsTermRecursively(term21)) {
                commonTerm = term21;
            }
            if (term21 instanceof ImageInt && commonTerm == null && term11 instanceof ImageInt) {
                commonTerm = ((ImageInt) term21).getTheOtherComponent();
                if (!(term11.containsTermRecursively(commonTerm))) {
                    commonTerm = null;
                }
                if ((commonTerm == null) || !(term11).containsTermRecursively(commonTerm)) {
                    commonTerm = ((ImageInt) term11).getTheOtherComponent();
                    if ((commonTerm == null) || !(term21).containsTermRecursively(commonTerm)) {
                        commonTerm = null;
                    }
                }
            }
            if (commonTerm != null) {
                subs.put(commonTerm, varInd2);
                term11 = ((CompoundTerm) term11).applySubstitute(subs);
                if (!(term21 instanceof CompoundTerm)) {
                    term21 = varInd2;
                } else {
                    term21 = ((CompoundTerm) term21).applySubstitute(subs);
                }
            }
        }
    }
    Statement state1 = Inheritance.make(term11, term12);
    Statement state2 = Inheritance.make(term21, term22);
    Term content = Implication.make(state1, state2);
    if (content == null) {
        return;
    }
    TruthValue truthT = nal.getCurrentTask().sentence.truth;
    TruthValue truthB = nal.getCurrentBelief().truth;
    if ((truthT == null) || (truthB == null)) {
        if (Parameters.DEBUG) {
            System.out.println("ERROR: Belief with null truth value. (introVarOuter)");
        }
        return;
    }
    TruthValue truth = induction(truthT, truthB);
    BudgetValue budget = BudgetFunctions.compoundForward(truth, content, nal);
    nal.doublePremiseTask(content, truth, budget, false, false);
    content = Implication.make(state2, state1);
    truth = induction(truthB, truthT);
    budget = BudgetFunctions.compoundForward(truth, content, nal);
    nal.doublePremiseTask(content, truth, budget, false, false);
    content = Equivalence.make(state1, state2);
    truth = comparison(truthT, truthB);
    budget = BudgetFunctions.compoundForward(truth, content, nal);
    nal.doublePremiseTask(content, truth, budget, false, false);
    Variable varDep = new Variable("#varDep");
    if (index == 0) {
        state1 = Inheritance.make(varDep, term12dependent);
        state2 = Inheritance.make(varDep, term22dependent);
    } else {
        state1 = Inheritance.make(term11dependent, varDep);
        state2 = Inheritance.make(term21dependent, varDep);
    }
    if ((state1 == null) || (state2 == null))
        return;
    content = Conjunction.make(state1, state2);
    truth = intersection(truthT, truthB);
    budget = BudgetFunctions.compoundForward(truth, content, nal);
    nal.doublePremiseTask(content, truth, budget, false, false);
}
Also used : CompoundTerm(nars.language.CompoundTerm) BudgetValue(nars.entity.BudgetValue) Variable(nars.language.Variable) HashMap(java.util.HashMap) Statement(nars.language.Statement) TruthValue(nars.entity.TruthValue) Inheritance(nars.language.Inheritance) ImageInt(nars.language.ImageInt) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) ImageExt(nars.language.ImageExt)

Example 18 with TruthValue

use of nars.entity.TruthValue in project opennars by opennars.

the class LocalRules method conversion.

/* -------------------- one-premise inference rules -------------------- */
/**
 * {<P --> S>} |- <S --> P> Produce an Inheritance/Implication from a
 * reversed Inheritance/Implication
 *
 * @param nal Reference to the memory
 */
private static void conversion(final DerivationContext nal) {
    TruthValue truth = TruthFunctions.conversion(nal.getCurrentBelief().truth);
    BudgetValue budget = BudgetFunctions.forward(truth, nal);
    convertedJudgment(truth, budget, nal);
}
Also used : BudgetValue(nars.entity.BudgetValue) TruthValue(nars.entity.TruthValue)

Example 19 with TruthValue

use of nars.entity.TruthValue in project opennars by opennars.

the class LocalRules method inferToSym.

/* -------------------- two-premise inference rules -------------------- */
/**
 * {<S --> P>, <P --> S} |- <S <-> p> Produce Similarity/Equivalence from a
 * pair of reversed Inheritance/Implication
 *
 * @param judgment1 The first premise
 * @param judgment2 The second premise
 * @param nal Reference to the memory
 */
private static void inferToSym(Sentence judgment1, Sentence judgment2, DerivationContext nal) {
    Statement s1 = (Statement) judgment1.term;
    Term t1 = s1.getSubject();
    Term t2 = s1.getPredicate();
    Term content;
    if (s1 instanceof Inheritance) {
        content = Similarity.make(t1, t2);
    } else {
        content = Equivalence.make(t1, t2, s1.getTemporalOrder());
    }
    TruthValue value1 = judgment1.truth;
    TruthValue value2 = judgment2.truth;
    TruthValue truth = TruthFunctions.intersection(value1, value2);
    BudgetValue budget = BudgetFunctions.forward(truth, nal);
    // (allow overlap) but not needed here, isn't detachment
    nal.doublePremiseTask(content, truth, budget, false, false);
}
Also used : BudgetValue(nars.entity.BudgetValue) Statement(nars.language.Statement) TruthValue(nars.entity.TruthValue) Inheritance(nars.language.Inheritance) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term)

Example 20 with TruthValue

use of nars.entity.TruthValue in project opennars by opennars.

the class LocalRules method convertRelation.

/**
 * {<S --> P>} |- <S <-> P> {<S <-> P>} |- <S --> P> Switch between
 * Inheritance/Implication and Similarity/Equivalence
 *
 * @param nal Reference to the memory
 */
private static void convertRelation(final DerivationContext nal) {
    TruthValue truth = nal.getCurrentBelief().truth;
    if (((CompoundTerm) nal.getCurrentTask().getTerm()).isCommutative()) {
        truth = TruthFunctions.abduction(truth, 1.0f);
    } else {
        truth = TruthFunctions.deduction(truth, 1.0f);
    }
    BudgetValue budget = BudgetFunctions.forward(truth, nal);
    convertedJudgment(truth, budget, nal);
}
Also used : CompoundTerm(nars.language.CompoundTerm) BudgetValue(nars.entity.BudgetValue) TruthValue(nars.entity.TruthValue)

Aggregations

TruthValue (nars.entity.TruthValue)64 BudgetValue (nars.entity.BudgetValue)52 Term (nars.language.Term)46 Sentence (nars.entity.Sentence)39 CompoundTerm (nars.language.CompoundTerm)36 Task (nars.entity.Task)34 Statement (nars.language.Statement)20 Stamp (nars.entity.Stamp)18 Conjunction (nars.language.Conjunction)18 Inheritance (nars.language.Inheritance)12 Interval (nars.language.Interval)10 Implication (nars.language.Implication)9 Product (nars.language.Product)7 Variable (nars.language.Variable)7 Concept (nars.entity.Concept)5 SetExt (nars.language.SetExt)5 SetInt (nars.language.SetInt)5 Operation (nars.operator.Operation)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4