Search in sources :

Example 26 with Sentence

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

the class StructuralRules method structuralCompose1.

/**
 * {<S --> P>, P@(P|Q)} |- <S --> (P|Q)>
 *
 * @param compound The compound term
 * @param index The location of the indicated term in the compound
 * @param statement The premise
 * @param nal Reference to the memory
 */
static void structuralCompose1(CompoundTerm compound, short index, Statement statement, DerivationContext nal) {
    if (!nal.getCurrentTask().sentence.isJudgment()) {
        // forward inference only
        return;
    }
    Term component = compound.term[index];
    Task task = nal.getCurrentTask();
    Sentence sentence = task.sentence;
    int order = sentence.getTemporalOrder();
    TruthValue truth = sentence.truth;
    final float reliance = Parameters.reliance;
    TruthValue truthDed = TruthFunctions.deduction(truth, reliance);
    TruthValue truthNDed = TruthFunctions.negation(TruthFunctions.deduction(truth, reliance));
    Term subj = statement.getSubject();
    Term pred = statement.getPredicate();
    if (component.equals(subj)) {
        if (compound instanceof IntersectionExt) {
            structuralStatement(compound, pred, order, truthDed, nal);
        } else if (compound instanceof IntersectionInt) {
        } else if ((compound instanceof DifferenceExt) && (index == 0)) {
            structuralStatement(compound, pred, order, truthDed, nal);
        } else if (compound instanceof DifferenceInt) {
            if (index == 0) {
            } else {
                structuralStatement(compound, pred, order, truthNDed, nal);
            }
        }
    } else if (component.equals(pred)) {
        if (compound instanceof IntersectionExt) {
        } else if (compound instanceof IntersectionInt) {
            structuralStatement(subj, compound, order, truthDed, nal);
        } else if (compound instanceof DifferenceExt) {
            if (index == 0) {
            } else {
                structuralStatement(subj, compound, order, truthNDed, nal);
            }
        } else if ((compound instanceof DifferenceInt) && (index == 0)) {
            structuralStatement(subj, compound, order, truthDed, nal);
        }
    }
}
Also used : Task(nars.entity.Task) IntersectionExt(nars.language.IntersectionExt) TruthValue(nars.entity.TruthValue) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) DifferenceExt(nars.language.DifferenceExt) Sentence(nars.entity.Sentence) IntersectionInt(nars.language.IntersectionInt) DifferenceInt(nars.language.DifferenceInt)

Example 27 with Sentence

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

the class SyllogisticRules method conditionalAbd.

/**
 * {<(&&, S2, S3) ==> P>, <(&&, S1, S3) ==> P>} |- <S1 ==> S2>
 *
 * @param cond1 The condition of the first premise
 * @param cond2 The condition of the second premise
 * @param taskContent The first premise
 * @param st2 The second premise
 * @param nal Reference to the memory
 * @return Whether there are derived tasks
 */
static boolean conditionalAbd(Term cond1, Term cond2, Statement st1, Statement st2, DerivationContext nal) {
    if (!(st1 instanceof Implication) || !(st2 instanceof Implication)) {
        return false;
    }
    if (!(cond1 instanceof Conjunction) && !(cond2 instanceof Conjunction)) {
        return false;
    }
    int order1 = st1.getTemporalOrder();
    int order2 = st2.getTemporalOrder();
    if (order1 != reverseOrder(order2)) {
        return false;
    }
    Term term1 = null;
    Term term2 = null;
    if (cond1 instanceof Conjunction) {
        term1 = reduceComponents((CompoundTerm) cond1, cond2, nal.mem());
    }
    if (cond2 instanceof Conjunction) {
        term2 = reduceComponents((CompoundTerm) cond2, cond1, nal.mem());
    }
    if ((term1 == null) && (term2 == null)) {
        return false;
    }
    Task task = nal.getCurrentTask();
    Sentence sentence = task.sentence;
    Sentence belief = nal.getCurrentBelief();
    TruthValue value1 = sentence.truth;
    TruthValue value2 = belief.truth;
    Term content;
    boolean keepOrder = Variables.hasSubstitute(Symbols.VAR_INDEPENDENT, st1, task.getTerm());
    TruthValue truth = null;
    BudgetValue budget;
    if (term1 != null) {
        if (term2 != null) {
            content = Statement.make(st2, term2, term1, st2.getTemporalOrder());
        } else {
            content = term1;
            if (content.hasVarIndep()) {
                return false;
            }
        }
        if (sentence.isQuestion() || sentence.isQuest()) {
            budget = BudgetFunctions.backwardWeak(value2, nal);
        } else {
            if (sentence.isGoal()) {
                if (keepOrder) {
                    truth = TruthFunctions.desireDed(value1, value2);
                } else {
                    truth = TruthFunctions.desireInd(value1, value2);
                }
            } else {
                // isJudgment
                truth = TruthFunctions.abduction(value2, value1);
            }
            budget = BudgetFunctions.forward(truth, nal);
        }
        nal.doublePremiseTask(content, truth, budget, false, false);
    }
    if (term2 != null) {
        if (term1 != null) {
            content = Statement.make(st1, term1, term2, st1.getTemporalOrder());
        } else {
            content = term2;
            if (content.hasVarIndep()) {
                return false;
            }
        }
        if (sentence.isQuestion() || sentence.isQuest()) {
            budget = BudgetFunctions.backwardWeak(value2, nal);
        } else {
            if (sentence.isGoal()) {
                if (keepOrder) {
                    truth = TruthFunctions.desireDed(value1, value2);
                } else {
                    truth = TruthFunctions.desireInd(value1, value2);
                }
            } else {
                // isJudgment
                truth = TruthFunctions.abduction(value1, value2);
            }
            budget = BudgetFunctions.forward(truth, nal);
        }
        nal.doublePremiseTask(content, truth, budget, false, false);
    }
    return true;
}
Also used : CompoundTerm(nars.language.CompoundTerm) BudgetValue(nars.entity.BudgetValue) Task(nars.entity.Task) TruthValue(nars.entity.TruthValue) Conjunction(nars.language.Conjunction) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Implication(nars.language.Implication) Sentence(nars.entity.Sentence)

Example 28 with Sentence

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

the class SyllogisticRules method analogy.

/**
 * {<S ==> P>, <M <=> P>} |- <S ==> P>
 *
 * @param subj Subject of the new task
 * @param pred Predicate of the new task
 * @param asym The asymmetric premise
 * @param sym The symmetric premise
 * @param figure Locations of the shared term in premises
 * @param nal Reference to the memory
 */
static void analogy(Term subj, Term pred, Sentence asym, Sentence sym, int figure, DerivationContext nal) {
    if (Statement.invalidStatement(subj, pred)) {
        return;
    }
    int order1 = asym.term.getTemporalOrder();
    int order2 = sym.term.getTemporalOrder();
    int order = analogyOrder(order1, order2, figure);
    if (order == ORDER_INVALID) {
        return;
    }
    Statement st = (Statement) asym.term;
    TruthValue truth = null;
    BudgetValue budget;
    Sentence sentence = nal.getCurrentTask().sentence;
    CompoundTerm taskTerm = (CompoundTerm) sentence.term;
    if (sentence.isQuestion() || sentence.isQuest()) {
        if (taskTerm.isCommutative()) {
            if (asym.truth == null) {
                // a question for example
                return;
            }
            budget = BudgetFunctions.backwardWeak(asym.truth, nal);
        } else {
            if (sym.truth == null) {
                // a question for example
                return;
            }
            budget = BudgetFunctions.backward(sym.truth, nal);
        }
    } else {
        if (sentence.isGoal()) {
            if (taskTerm.isCommutative()) {
                truth = TruthFunctions.desireWeak(asym.truth, sym.truth);
            } else {
                truth = TruthFunctions.desireStrong(asym.truth, sym.truth);
            }
        } else {
            truth = TruthFunctions.analogy(asym.truth, sym.truth);
        }
        budget = BudgetFunctions.forward(truth, nal);
    }
    // nal.mem().logic.ANALOGY.commit();
    // (allow overlap) but not needed here, isn't detachment
    nal.doublePremiseTask(Statement.make(st, subj, pred, order), truth, budget, false, false);
}
Also used : BudgetValue(nars.entity.BudgetValue) CompoundTerm(nars.language.CompoundTerm) Statement(nars.language.Statement) TruthValue(nars.entity.TruthValue) Sentence(nars.entity.Sentence)

Example 29 with Sentence

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

the class SyllogisticRules method conditionalDedInd.

/**
 * {<(&&, S1, S2, S3) ==> P>, S1} |- <(&&, S2, S3) ==> P> {<(&&, S2, S3) ==>
 * P>, <S1 ==> S2>} |- <(&&, S1, S3) ==> P> {<(&&, S1, S3) ==> P>, <S1 ==>
 * S2>} |- <(&&, S2, S3) ==> P>
 *
 * @param premise1 The conditional premise
 * @param index The location of the shared term in the condition of premise1
 * @param premise2 The premise which, or part of which, appears in the
 * condition of premise1
 * @param side The location of the shared term in premise2: 0 for subject, 1
 * for predicate, -1 for the whole term
 * @param nal Reference to the memory
 */
static void conditionalDedInd(Sentence premise1Sentence, Implication premise1, short index, Term premise2, int side, DerivationContext nal) {
    Task task = nal.getCurrentTask();
    Sentence taskSentence = task.sentence;
    Sentence belief = nal.getCurrentBelief();
    boolean deduction = (side != 0);
    boolean conditionalTask = Variables.hasSubstitute(Symbols.VAR_INDEPENDENT, premise2, belief.term);
    Term commonComponent;
    Term newComponent = null;
    if (side == 0) {
        commonComponent = ((Statement) premise2).getSubject();
        newComponent = ((Statement) premise2).getPredicate();
    } else if (side == 1) {
        commonComponent = ((Statement) premise2).getPredicate();
        newComponent = ((Statement) premise2).getSubject();
    } else {
        commonComponent = premise2;
    }
    Term subj = premise1.getSubject();
    if (!(subj instanceof Conjunction)) {
        return;
    }
    Conjunction oldCondition = (Conjunction) subj;
    int index2 = Terms.indexOf(oldCondition.term, commonComponent);
    if (index2 >= 0) {
        index = (short) index2;
    } else {
        Term[] u = new Term[] { premise1, premise2 };
        boolean match = Variables.unify(Symbols.VAR_INDEPENDENT, oldCondition.term[index], commonComponent, u);
        premise1 = (Implication) u[0];
        premise2 = u[1];
        if (!match && (commonComponent.getClass() == oldCondition.getClass())) {
            CompoundTerm compoundCommonComponent = ((CompoundTerm) commonComponent);
            if ((oldCondition.term.length > index) && (compoundCommonComponent.term.length > index)) {
                // assumption: { was missing
                u = new Term[] { premise1, premise2 };
                match = Variables.unify(Symbols.VAR_INDEPENDENT, oldCondition.term[index], compoundCommonComponent.term[index], u);
                premise1 = (Implication) u[0];
                premise2 = u[1];
            }
        }
        if (!match) {
            return;
        }
    }
    int conjunctionOrder = subj.getTemporalOrder();
    if (conjunctionOrder == ORDER_FORWARD) {
        if (index > 0) {
            return;
        }
        if ((side == 0) && (premise2.getTemporalOrder() == ORDER_FORWARD)) {
            return;
        }
        if ((side == 1) && (premise2.getTemporalOrder() == ORDER_BACKWARD)) {
            return;
        }
    }
    Term newCondition;
    if (oldCondition.equals(commonComponent)) {
        newCondition = null;
    } else {
        newCondition = oldCondition.setComponent(index, newComponent, nal.mem());
    }
    Term content;
    long delta = 0;
    long mintime = 0;
    long maxtime = 0;
    boolean predictedEvent = false;
    if (newCondition != null) {
        if (newCondition instanceof Interval) {
            content = premise1.getPredicate();
            delta = ((Interval) newCondition).time;
            if (taskSentence.getOccurenceTime() != Stamp.ETERNAL) {
                mintime = taskSentence.getOccurenceTime() + ((Interval) newCondition).time - 1;
                maxtime = taskSentence.getOccurenceTime() + ((Interval) newCondition).time + 2;
                predictedEvent = true;
            }
        } else {
            while ((newCondition instanceof Conjunction) && (((CompoundTerm) newCondition).term[0] instanceof Interval)) {
                Interval interval = (Interval) ((CompoundTerm) newCondition).term[0];
                delta += interval.time;
                newCondition = ((CompoundTerm) newCondition).setComponent(0, null, nal.mem());
            }
            content = Statement.make(premise1, newCondition, premise1.getPredicate(), premise1.getTemporalOrder());
        }
    } else {
        content = premise1.getPredicate();
    }
    if (content == null)
        return;
    long occurrence_time = nal.getTheNewStamp().getOccurrenceTime();
    if (delta != 0) {
        long baseTime = taskSentence.getOccurenceTime();
        if (baseTime != Stamp.ETERNAL) {
            baseTime += delta;
            occurrence_time = baseTime;
        }
    }
    TruthValue truth1 = taskSentence.truth;
    TruthValue truth2 = belief.truth;
    TruthValue truth = null;
    BudgetValue budget;
    if (taskSentence.isQuestion() || taskSentence.isQuest()) {
        budget = BudgetFunctions.backwardWeak(truth2, nal);
    } else {
        if (taskSentence.isGoal()) {
            if (conditionalTask) {
                truth = TruthFunctions.desireWeak(truth1, truth2);
            } else if (deduction) {
                truth = TruthFunctions.desireInd(truth1, truth2);
            } else {
                truth = TruthFunctions.desireDed(truth1, truth2);
            }
        } else {
            if (deduction) {
                truth = TruthFunctions.deduction(truth1, truth2);
            } else if (conditionalTask) {
                truth = TruthFunctions.induction(truth2, truth1);
            } else {
                truth = TruthFunctions.induction(truth1, truth2);
            }
        }
        budget = BudgetFunctions.forward(truth, nal);
    }
    nal.getTheNewStamp().setOccurrenceTime(occurrence_time);
    // (allow overlap) when deduction on judgment
    List<Task> ret = nal.doublePremiseTask(content, truth, budget, false, taskSentence.isJudgment() && deduction);
    if (!nal.evidentalOverlap && ret != null && ret.size() > 0) {
        if (predictedEvent && taskSentence.isJudgment() && truth != null && truth.getExpectation() > Parameters.DEFAULT_CONFIRMATION_EXPECTATION && !premise1Sentence.stamp.alreadyAnticipatedNegConfirmation) {
            premise1Sentence.stamp.alreadyAnticipatedNegConfirmation = true;
            ConceptProcessing.generatePotentialNegConfirmation(nal, premise1Sentence, budget, mintime, maxtime, 1);
        }
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) BudgetValue(nars.entity.BudgetValue) Task(nars.entity.Task) Statement(nars.language.Statement) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) TruthValue(nars.entity.TruthValue) Conjunction(nars.language.Conjunction) Sentence(nars.entity.Sentence) Interval(nars.language.Interval)

Example 30 with Sentence

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

the class SyllogisticRules method conditionalAna.

/**
 * {<(&&, S1, S2, S3) <=> P>, S1} |- <(&&, S2, S3) <=> P> {<(&&, S2, S3) <=> P>,
 * <S1 ==> S2>} |- <(&&, S1, S3) <=> P> {<(&&, S1, S3) <=> P>, <S1 ==>
 *
 * @param premise1 The equivalence premise
 * @param index The location of the shared term in the condition of premise1
 * @param premise2 The premise which, or part of which, appears in the
 * condition of premise1
 * @param side The location of the shared term in premise2: 0 for subject, 1
 * for predicate, -1 for the whole term
 * @param nal Reference to the memory
 */
static void conditionalAna(Equivalence premise1, short index, Term premise2, int side, DerivationContext nal) {
    Task task = nal.getCurrentTask();
    Sentence taskSentence = task.sentence;
    Sentence belief = nal.getCurrentBelief();
    boolean conditionalTask = Variables.hasSubstitute(Symbols.VAR_INDEPENDENT, premise2, belief.term);
    Term commonComponent;
    Term newComponent = null;
    if (side == 0) {
        commonComponent = ((Statement) premise2).getSubject();
        newComponent = ((Statement) premise2).getPredicate();
    } else if (side == 1) {
        commonComponent = ((Statement) premise2).getPredicate();
        newComponent = ((Statement) premise2).getSubject();
    } else {
        commonComponent = premise2;
    }
    Term tm = premise1.getSubject();
    if (!(tm instanceof Conjunction)) {
        return;
    }
    Conjunction oldCondition = (Conjunction) tm;
    Term[] u = new Term[] { premise1, premise2 };
    boolean match = Variables.unify(Symbols.VAR_DEPENDENT, oldCondition.term[index], commonComponent, u);
    premise1 = (Equivalence) u[0];
    premise2 = u[1];
    if (!match && (commonComponent.getClass() == oldCondition.getClass())) {
        u = new Term[] { premise1, premise2 };
        match = Variables.unify(Symbols.VAR_DEPENDENT, oldCondition.term[index], ((CompoundTerm) commonComponent).term[index], u);
        premise1 = (Equivalence) u[0];
        premise2 = u[1];
    }
    if (!match) {
        return;
    }
    int conjunctionOrder = oldCondition.getTemporalOrder();
    if (conjunctionOrder == ORDER_FORWARD) {
        if (index > 0) {
            return;
        }
        if ((side == 0) && (premise2.getTemporalOrder() == ORDER_FORWARD)) {
            return;
        }
        if ((side == 1) && (premise2.getTemporalOrder() == ORDER_BACKWARD)) {
            return;
        }
    }
    Term newCondition;
    if (oldCondition.equals(commonComponent)) {
        newCondition = null;
    } else {
        newCondition = oldCondition.setComponent(index, newComponent, nal.mem());
    }
    Term content;
    if (newCondition != null) {
        content = Statement.make(premise1, newCondition, premise1.getPredicate(), premise1.getTemporalOrder());
    } else {
        content = premise1.getPredicate();
    }
    if (content == null)
        return;
    TruthValue truth1 = taskSentence.truth;
    TruthValue truth2 = belief.truth;
    TruthValue truth = null;
    BudgetValue budget;
    if (taskSentence.isQuestion() || taskSentence.isQuest()) {
        budget = BudgetFunctions.backwardWeak(truth2, nal);
    } else {
        if (taskSentence.isGoal()) {
            if (conditionalTask) {
                truth = TruthFunctions.desireWeak(truth1, truth2);
            } else {
                truth = TruthFunctions.desireDed(truth1, truth2);
            }
        } else {
            if (conditionalTask) {
                truth = TruthFunctions.comparison(truth1, truth2);
            } else {
                truth = TruthFunctions.analogy(truth1, truth2);
            }
        }
        budget = BudgetFunctions.forward(truth, nal);
    }
    // (allow overlap) when !conditionalTask on judgment
    nal.doublePremiseTask(content, truth, budget, false, taskSentence.isJudgment() && !conditionalTask);
}
Also used : BudgetValue(nars.entity.BudgetValue) Task(nars.entity.Task) Statement(nars.language.Statement) TruthValue(nars.entity.TruthValue) Conjunction(nars.language.Conjunction) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Sentence(nars.entity.Sentence)

Aggregations

Sentence (nars.entity.Sentence)70 Task (nars.entity.Task)54 Term (nars.language.Term)40 BudgetValue (nars.entity.BudgetValue)39 TruthValue (nars.entity.TruthValue)39 Stamp (nars.entity.Stamp)25 CompoundTerm (nars.language.CompoundTerm)24 Statement (nars.language.Statement)13 Conjunction (nars.language.Conjunction)11 Concept (nars.entity.Concept)10 Implication (nars.language.Implication)10 Inheritance (nars.language.Inheritance)8 Interval (nars.language.Interval)8 ArrayList (java.util.ArrayList)6 Equivalence (nars.language.Equivalence)6 HashMap (java.util.HashMap)5 AnswerHandler (nars.io.events.AnswerHandler)5 NARSwing (nars.gui.NARSwing)4 Narsese (nars.io.Narsese)4 Product (nars.language.Product)4