Search in sources :

Example 1 with Conjunction

use of nars.language.Conjunction in project opennars by opennars.

the class PerceptionAccel method perceive.

public void perceive(DerivationContext nal) {
    // implement Peis idea here now
    // we start with length 2 compounds, and search for patterns which are one longer than the longest observed one
    boolean longest_result_derived_already = false;
    for (int Len = cur_maxlen + 1; Len >= 2; Len--) {
        // ok, this is the length we have to collect, measured from the end of event buffer
        // there is a interval term for every event
        Term[] relterms = new Term[2 * Len - 1];
        // measuring its distance to the next event, but for the last event this is obsolete
        // thus it are 2*Len-1] terms
        Task newEvent = eventbuffer.get(eventbuffer.size() - 1);
        TruthValue truth = newEvent.sentence.truth;
        Stamp st = new Stamp(nal.memory);
        ArrayList<Long> evBase = new ArrayList<Long>();
        int k = 0;
        for (int i = 0; i < Len; i++) {
            // we go till to the end of the event buffer
            int j = eventbuffer.size() - 1 - (Len - 1) + i;
            if (j < 0) {
                // but the mechanism already looks for length 2 patterns on the occurence of the first event
                break;
            }
            Task current = eventbuffer.get(j);
            for (long l : current.sentence.stamp.evidentialBase) {
                evBase.add(l);
            }
            relterms[k] = current.sentence.term;
            if (i != Len - 1) {
                // if its not the last one, then there is a next one for which we have to put an interval
                truth = TruthFunctions.deduction(truth, current.sentence.truth);
                Task next = eventbuffer.get(j + 1);
                relterms[k + 1] = new Interval(next.sentence.getOccurenceTime() - current.sentence.getOccurenceTime());
            }
            k += 2;
        }
        long[] evB = new long[evBase.size()];
        int u = 0;
        for (long l : evBase) {
            evB[u] = l;
            u++;
        }
        st.baseLength = evB.length;
        st.evidentialBase = evB;
        boolean eventBufferDidNotHaveSoMuchEvents = false;
        for (int i = 0; i < relterms.length; i++) {
            if (relterms[i] == null) {
                eventBufferDidNotHaveSoMuchEvents = true;
            }
        }
        if (eventBufferDidNotHaveSoMuchEvents) {
            continue;
        }
        // decide on the tense of &/ by looking if the first event happens parallel with the last one
        // Todo refine in 1.6.3 if we want to allow input of difference occurence time
        boolean after = newEvent.sentence.stamp.after(eventbuffer.get(eventbuffer.size() - 1 - (Len - 1)).sentence.stamp, Parameters.DURATION);
        // critical part: (not checked for correctness yet):
        // we now have to look at if the first half + the second half already exists as concept, before we add it
        Term[] firstHalf;
        Term[] secondHalf;
        if (relterms[Len - 1] instanceof Interval) {
            // the middle can be a interval, for example in case of a,+1,b , in which case we dont use it
            // so we skip the middle here
            firstHalf = new Term[Len - 1];
            // as well as here
            secondHalf = new Term[Len - 1];
            // make index mapping easier by counting
            int h = 0;
            for (int i = 0; i < Len - 1; i++) {
                firstHalf[i] = relterms[h];
                h++;
            }
            // we have to overjump the middle element this is why
            h += 1;
            for (int i = 0; i < Len - 1; i++) {
                secondHalf[i] = relterms[h];
                h++;
            }
        } else {
            // it is a event so its fine
            // 2*Len-1 in total
            firstHalf = new Term[Len];
            // but the middle is also used in the second one
            secondHalf = new Term[Len];
            // make index mapping easier by counting
            int h = 0;
            for (int i = 0; i < Len; i++) {
                firstHalf[i] = relterms[h];
                h++;
            }
            // we have to use the middle twice this is why
            h--;
            for (int i = 0; i < Len; i++) {
                secondHalf[i] = relterms[h];
                h++;
            }
        }
        Term firstC = Conjunction.make(firstHalf, after ? ORDER_FORWARD : ORDER_CONCURRENT);
        Term secondC = Conjunction.make(secondHalf, after ? ORDER_FORWARD : ORDER_CONCURRENT);
        Concept C1 = nal.memory.concept(firstC);
        Concept C2 = nal.memory.concept(secondC);
        if (C1 == null || C2 == null) {
            if (debugMechanism) {
                System.out.println("one didn't exist: " + firstC.toString() + " or " + secondC.toString());
            }
            // the components were not observed, so don't allow creating this compound
            continue;
        }
        if (C1.getPriority() < partConceptsPrioThreshold || C2.getPriority() < partConceptsPrioThreshold) {
            // too less priority
            continue;
        }
        Conjunction C = (Conjunction) Conjunction.make(relterms, after ? ORDER_FORWARD : ORDER_CONCURRENT);
        // importance "summation"
        Sentence S = new Sentence(C, Symbols.JUDGMENT_MARK, truth, st);
        Task T = new Task(S, new BudgetValue(BudgetFunctions.or(C1.getPriority(), C2.getPriority()), Parameters.DEFAULT_JUDGMENT_DURABILITY, truth), true);
        if (debugMechanism) {
            System.out.println("success: " + T.toString());
        }
        if (longest_result_derived_already) {
            T.setElemOfSequenceBuffer(false);
        }
        longest_result_derived_already = true;
        // lets make the new event the parent task, and derive it
        nal.derivedTask(T, false, false, false);
    }
}
Also used : Concept(nars.entity.Concept) BudgetValue(nars.entity.BudgetValue) Task(nars.entity.Task) Stamp(nars.entity.Stamp) ArrayList(java.util.ArrayList) Term(nars.language.Term) TruthValue(nars.entity.TruthValue) Conjunction(nars.language.Conjunction) Sentence(nars.entity.Sentence) Interval(nars.language.Interval)

Example 2 with Conjunction

use of nars.language.Conjunction in project opennars by opennars.

the class GlobalAnticipation method temporalPredictionsAdapt.

// check all predictive statements, match them with last events
public void temporalPredictionsAdapt(DerivationContext nal) {
    if (TEMPORAL_PREDICTION_FEEDBACK_ACCURACY_DIV == 0.0f) {
        return;
    }
    ArrayList<Task> lastEvents = new ArrayList<Task>();
    for (Task stmLast : stm) {
        lastEvents.add(stmLast);
    }
    if (lastEvents.isEmpty()) {
        return;
    }
    final long duration = Parameters.DURATION;
    ArrayList<Task> derivetasks = new ArrayList<Task>();
    for (final Task c : current_tasks) {
        // a =/> b or (&/ a1...an) =/> b
        boolean concurrent_conjunction = false;
        Term[] args = new Term[1];
        Implication imp = (Implication) c.sentence.term.clone();
        boolean concurrent_implication = imp.getTemporalOrder() == TemporalRules.ORDER_CONCURRENT;
        args[0] = imp.getSubject();
        if (imp.getSubject() instanceof Conjunction) {
            Conjunction conj = (Conjunction) imp.getSubject();
            if (!conj.isSpatial) {
                if (conj.temporalOrder == TemporalRules.ORDER_FORWARD || conj.temporalOrder == TemporalRules.ORDER_CONCURRENT) {
                    concurrent_conjunction = conj.temporalOrder == TemporalRules.ORDER_CONCURRENT;
                    // in case of &/ this are the terms
                    args = conj.term;
                }
            }
        }
        int i = 0;
        boolean matched = true;
        int off = 0;
        long expected_time = lastEvents.get(0).sentence.getOccurenceTime();
        for (i = 0; i < args.length; i++) {
            // handling of intervals:
            if (args[i] instanceof Interval) {
                if (!concurrent_conjunction) {
                    expected_time += ((Interval) args[i]).time;
                }
                off++;
                continue;
            }
            if (i - off >= lastEvents.size()) {
                break;
            }
            if (!Variables.hasSubstitute(Symbols.VAR_INDEPENDENT, args[i], lastEvents.get(i - off).sentence.term)) {
                // it didnt match, instead sth different unexpected happened
                // whether intermediate events should be tolerated or not was a important question when considering this,
                matched = false;
                // if it should be allowed, the sequential match does not matter only if the events come like predicted.
                break;
            } else {
                if (lastEvents.get(i - off).sentence.truth.getExpectation() <= 0.5f) {
                    // it matched according to sequence, but is its expectation bigger than 0.5? todo: decide how truth values of the expected events
                    // it didn't happen
                    matched = false;
                    break;
                }
                long occurence = lastEvents.get(i - off).sentence.getOccurenceTime();
                boolean right_in_time = Math.abs(occurence - expected_time) < ((double) duration) / TEMPORAL_PREDICTION_FEEDBACK_ACCURACY_DIV;
                if (!right_in_time) {
                    // it matched so far, but is the timing right or did it happen when not relevant anymore?
                    matched = false;
                    break;
                }
            }
            if (!concurrent_conjunction) {
                expected_time += duration;
            }
        }
        if (concurrent_conjunction && !concurrent_implication) {
            // implication is not concurrent
            // so here we have to add duration
            expected_time += duration;
        } else if (!concurrent_conjunction && concurrent_implication) {
            expected_time -= duration;
        }
        // ok it matched, is the consequence also right?
        if (matched && lastEvents.size() > args.length - off) {
            long occurence = lastEvents.get(args.length - off).sentence.getOccurenceTime();
            boolean right_in_time = Math.abs(occurence - expected_time) < ((double) duration) / TEMPORAL_PREDICTION_FEEDBACK_ACCURACY_DIV;
            if (right_in_time && Variables.hasSubstitute(Symbols.VAR_INDEPENDENT, imp.getPredicate(), lastEvents.get(args.length - off).sentence.term)) {
                // it matched and same consequence, so positive evidence
                // c.sentence.truth=TruthFunctions.revision(c.sentence.truth, new TruthValue(1.0f,Parameters.DEFAULT_JUDGMENT_CONFIDENCE));
                Sentence s2 = new Sentence(c.sentence.term.clone(), Symbols.JUDGMENT_MARK, new TruthValue(1.0f, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(nal.memory));
                Task t = new Task(s2, new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY, Parameters.DEFAULT_JUDGMENT_DURABILITY, s2.truth), true);
                derivetasks.add(t);
            } else {
                // it matched and other consequence, so negative evidence
                // c.sentence.truth=TruthFunctions.revision(c.sentence.truth, new TruthValue(0.0f,Parameters.DEFAULT_JUDGMENT_CONFIDENCE));
                Sentence s2 = new Sentence(c.sentence.term.clone(), Symbols.JUDGMENT_MARK, new TruthValue(0.0f, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(nal.memory));
                Task t = new Task(s2, new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY, Parameters.DEFAULT_JUDGMENT_DURABILITY, s2.truth), true);
                derivetasks.add(t);
            }
        // todo use derived task with revision instead
        }
    }
    for (Task t : derivetasks) {
        if (nal.derivedTask(t, false, false, false)) {
            boolean debug = true;
        }
    }
    ArrayList<Task> toDelete = new ArrayList<Task>();
    for (Task t : current_tasks) {
        Concept w = nal.memory.concept(t.sentence.term);
        if (w == null) {
            // concept does not exist anymore, delete
            toDelete.add(t);
        }
    }
    for (Task t : toDelete) {
        current_tasks.remove(t);
    }
}
Also used : BudgetValue(nars.entity.BudgetValue) Concept(nars.entity.Concept) Task(nars.entity.Task) Stamp(nars.entity.Stamp) ArrayList(java.util.ArrayList) Term(nars.language.Term) Implication(nars.language.Implication) TruthValue(nars.entity.TruthValue) Conjunction(nars.language.Conjunction) Sentence(nars.entity.Sentence) Interval(nars.language.Interval)

Example 3 with Conjunction

use of nars.language.Conjunction in project opennars by opennars.

the class CompositionalRules method decomposeCompound.

/**
 * {<(S|P) ==> M>, <P ==> M>} |- <S ==> M>
 *
 * @param implication The implication term to be decomposed
 * @param componentCommon The part of the implication to be removed
 * @param term1 The other term in the contentInd
 * @param index The location of the shared term: 0 for subject, 1 for
 * predicate
 * @param compoundTask Whether the implication comes from the task
 * @param nal Reference to the memory
 */
private static void decomposeCompound(CompoundTerm compound, Term component, Term term1, int index, boolean compoundTask, int order, DerivationContext nal) {
    if ((compound instanceof Statement) || (compound instanceof ImageExt) || (compound instanceof ImageInt)) {
        return;
    }
    Term term2 = reduceComponents(compound, component, nal.mem());
    if (term2 == null) {
        return;
    }
    long delta = 0;
    while ((term2 instanceof Conjunction) && (((CompoundTerm) term2).term[0] instanceof Interval)) {
        Interval interval = (Interval) ((CompoundTerm) term2).term[0];
        delta += interval.time;
        term2 = ((CompoundTerm) term2).setComponent(0, null, nal.mem());
    }
    Task task = nal.getCurrentTask();
    Sentence sentence = task.sentence;
    Sentence belief = nal.getCurrentBelief();
    Statement oldContent = (Statement) task.getTerm();
    TruthValue v1, v2;
    if (compoundTask) {
        v1 = sentence.truth;
        v2 = belief.truth;
    } else {
        v1 = belief.truth;
        v2 = sentence.truth;
    }
    TruthValue truth = null;
    Term content;
    if (index == 0) {
        content = Statement.make(oldContent, term1, term2, order);
        if (content == null) {
            return;
        }
        if (oldContent instanceof Inheritance) {
            if (compound instanceof IntersectionExt) {
                truth = reduceConjunction(v1, v2);
            } else if (compound instanceof IntersectionInt) {
                truth = reduceDisjunction(v1, v2);
            } else if ((compound instanceof SetInt) && (component instanceof SetInt)) {
                truth = reduceConjunction(v1, v2);
            } else if ((compound instanceof SetExt) && (component instanceof SetExt)) {
                truth = reduceDisjunction(v1, v2);
            } else if (compound instanceof DifferenceExt) {
                if (compound.term[0].equals(component)) {
                    truth = reduceDisjunction(v2, v1);
                } else {
                    truth = reduceConjunctionNeg(v1, v2);
                }
            }
        } else if (oldContent instanceof Implication) {
            if (compound instanceof Conjunction) {
                truth = reduceConjunction(v1, v2);
            } else if (compound instanceof Disjunction) {
                truth = reduceDisjunction(v1, v2);
            }
        }
    } else {
        content = Statement.make(oldContent, term2, term1, order);
        if (content == null) {
            return;
        }
        if (oldContent instanceof Inheritance) {
            if (compound instanceof IntersectionInt) {
                truth = reduceConjunction(v1, v2);
            } else if (compound instanceof IntersectionExt) {
                truth = reduceDisjunction(v1, v2);
            } else if ((compound instanceof SetExt) && (component instanceof SetExt)) {
                truth = reduceConjunction(v1, v2);
            } else if ((compound instanceof SetInt) && (component instanceof SetInt)) {
                truth = reduceDisjunction(v1, v2);
            } else if (compound instanceof DifferenceInt) {
                if (compound.term[1].equals(component)) {
                    truth = reduceDisjunction(v2, v1);
                } else {
                    truth = reduceConjunctionNeg(v1, v2);
                }
            }
        } else if (oldContent instanceof Implication) {
            if (compound instanceof Disjunction) {
                truth = reduceConjunction(v1, v2);
            } else if (compound instanceof Conjunction) {
                truth = reduceDisjunction(v1, v2);
            }
        }
    }
    if (truth != null) {
        BudgetValue budget = BudgetFunctions.compoundForward(truth, content, nal);
        if (delta != 0) {
            long baseTime = task.sentence.getOccurenceTime();
            if (baseTime != Stamp.ETERNAL) {
                baseTime += delta;
                nal.getTheNewStamp().setOccurrenceTime(baseTime);
            }
        }
        // (allow overlap), a form of detachment
        nal.doublePremiseTask(content, truth, budget, false, true);
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) BudgetValue(nars.entity.BudgetValue) Task(nars.entity.Task) Statement(nars.language.Statement) Inheritance(nars.language.Inheritance) ImageInt(nars.language.ImageInt) SetInt(nars.language.SetInt) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) IntersectionInt(nars.language.IntersectionInt) Implication(nars.language.Implication) DifferenceInt(nars.language.DifferenceInt) Disjunction(nars.language.Disjunction) TruthFunctions.reduceDisjunction(nars.inference.TruthFunctions.reduceDisjunction) IntersectionExt(nars.language.IntersectionExt) TruthValue(nars.entity.TruthValue) TruthFunctions.reduceConjunction(nars.inference.TruthFunctions.reduceConjunction) Conjunction(nars.language.Conjunction) SetExt(nars.language.SetExt) ImageExt(nars.language.ImageExt) DifferenceExt(nars.language.DifferenceExt) Sentence(nars.entity.Sentence) Interval(nars.language.Interval)

Example 4 with Conjunction

use of nars.language.Conjunction in project opennars by opennars.

the class CompositionalRules method decomposeStatement.

/**
 * {(||, S, P), P} |- S {(&&, S, P), P} |- S
 *
 * @param implication The implication term to be decomposed
 * @param componentCommon The part of the implication to be removed
 * @param compoundTask Whether the implication comes from the task
 * @param nal Reference to the memory
 */
static void decomposeStatement(CompoundTerm compound, Term component, boolean compoundTask, int index, DerivationContext nal) {
    boolean isTemporalConjunction = (compound instanceof Conjunction) && !((Conjunction) compound).isSpatial;
    if (isTemporalConjunction && (compound.getTemporalOrder() == TemporalRules.ORDER_FORWARD) && (index != 0)) {
        return;
    }
    long occurrence_time = nal.getTheNewStamp().getOccurrenceTime();
    if (isTemporalConjunction && (compound.getTemporalOrder() == TemporalRules.ORDER_FORWARD)) {
        if (!nal.getCurrentTask().sentence.isEternal() && compound.term[index + 1] instanceof Interval) {
            long shift_occurrence = ((Interval) compound.term[1]).time;
            occurrence_time = nal.getCurrentTask().sentence.getOccurenceTime() + shift_occurrence;
        }
    }
    Task task = nal.getCurrentTask();
    Sentence taskSentence = task.sentence;
    Sentence belief = nal.getCurrentBelief();
    Term content = reduceComponents(compound, component, nal.mem());
    if (content == null) {
        return;
    }
    TruthValue truth = null;
    BudgetValue budget;
    if (taskSentence.isQuestion() || taskSentence.isQuest()) {
        budget = BudgetFunctions.compoundBackward(content, nal);
        nal.getTheNewStamp().setOccurrenceTime(occurrence_time);
        nal.doublePremiseTask(content, truth, budget, false, false);
        // special inference to answer conjunctive questions with query variables
        if (taskSentence.term.hasVarQuery()) {
            Concept contentConcept = nal.mem().concept(content);
            if (contentConcept == null) {
                return;
            }
            Sentence contentBelief = contentConcept.getBelief(nal, task);
            if (contentBelief == null) {
                return;
            }
            Task contentTask = new Task(contentBelief, task.budget, false);
            nal.setCurrentTask(contentTask);
            Term conj = Conjunction.make(component, content);
            truth = intersection(contentBelief.truth, belief.truth);
            budget = BudgetFunctions.compoundForward(truth, conj, nal);
            nal.getTheNewStamp().setOccurrenceTime(occurrence_time);
            nal.doublePremiseTask(conj, truth, budget, false, false);
        }
    } else {
        TruthValue v1, v2;
        if (compoundTask) {
            v1 = taskSentence.truth;
            v2 = belief.truth;
        } else {
            v1 = belief.truth;
            v2 = taskSentence.truth;
        }
        if (compound instanceof Conjunction) {
            if (taskSentence.isGoal()) {
                if (compoundTask) {
                    truth = intersection(v1, v2);
                } else {
                    return;
                }
            } else {
                // isJudgment
                truth = reduceConjunction(v1, v2);
            }
        } else if (compound instanceof Disjunction) {
            if (taskSentence.isGoal()) {
                if (compoundTask) {
                    truth = reduceConjunction(v2, v1);
                } else {
                    return;
                }
            } else {
                // isJudgment
                truth = reduceDisjunction(v1, v2);
            }
        } else {
            return;
        }
        budget = BudgetFunctions.compoundForward(truth, content, nal);
    }
    nal.getTheNewStamp().setOccurrenceTime(occurrence_time);
    nal.doublePremiseTask(content, truth, budget, false, false);
}
Also used : BudgetValue(nars.entity.BudgetValue) Concept(nars.entity.Concept) Task(nars.entity.Task) Disjunction(nars.language.Disjunction) TruthFunctions.reduceDisjunction(nars.inference.TruthFunctions.reduceDisjunction) TruthValue(nars.entity.TruthValue) TruthFunctions.reduceConjunction(nars.inference.TruthFunctions.reduceConjunction) Conjunction(nars.language.Conjunction) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Sentence(nars.entity.Sentence) Interval(nars.language.Interval)

Example 5 with Conjunction

use of nars.language.Conjunction 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)

Aggregations

Conjunction (nars.language.Conjunction)22 Term (nars.language.Term)20 BudgetValue (nars.entity.BudgetValue)18 TruthValue (nars.entity.TruthValue)18 CompoundTerm (nars.language.CompoundTerm)18 Sentence (nars.entity.Sentence)11 Task (nars.entity.Task)11 Statement (nars.language.Statement)9 Interval (nars.language.Interval)8 Implication (nars.language.Implication)7 Inheritance (nars.language.Inheritance)6 TruthFunctions.reduceConjunction (nars.inference.TruthFunctions.reduceConjunction)4 ArrayList (java.util.ArrayList)3 Concept (nars.entity.Concept)3 Stamp (nars.entity.Stamp)3 Disjunction (nars.language.Disjunction)3 Product (nars.language.Product)3 Variable (nars.language.Variable)3 HashMap (java.util.HashMap)2 TruthFunctions.reduceDisjunction (nars.inference.TruthFunctions.reduceDisjunction)2