Search in sources :

Example 41 with Term

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

the class RuleTables method detachmentWithVar.

private static void detachmentWithVar(Sentence originalMainSentence, Sentence subSentence, int index, boolean checkTermAgain, DerivationContext nal) {
    if (originalMainSentence == null) {
        return;
    }
    // for substitution
    Sentence mainSentence = originalMainSentence;
    if (!(mainSentence.term instanceof Statement))
        return;
    Statement statement = (Statement) mainSentence.term;
    Term component = statement.term[index];
    Term content = subSentence.term;
    if (nal.getCurrentBelief() != null) {
        Term[] u = new Term[] { statement, content };
        if (!component.hasVarIndep() && !component.hasVarDep()) {
            // because of example: <<(*,w1,#2) --> [good]> ==> <w1 --> TRANSLATE>>. <(*,w1,w2) --> [good]>.
            SyllogisticRules.detachment(mainSentence, subSentence, index, checkTermAgain, nal);
        } else if (Variables.unify(VAR_INDEPENDENT, component, content, u)) {
            // happens through syllogisms
            mainSentence = mainSentence.clone(u[0]);
            subSentence = subSentence.clone(u[1]);
            SyllogisticRules.detachment(mainSentence, subSentence, index, false, nal);
        } else if ((statement instanceof Implication) && (statement.getPredicate() instanceof Statement) && (nal.getCurrentTask().sentence.isJudgment())) {
            Statement s2 = (Statement) statement.getPredicate();
            if ((content instanceof Statement) && (s2.getSubject().equals(((Statement) content).getSubject()))) {
                CompositionalRules.introVarInner((Statement) content, s2, statement, nal);
            }
            CompositionalRules.IntroVarSameSubjectOrPredicate(originalMainSentence, subSentence, component, content, index, nal);
        } else if ((statement instanceof Equivalence) && (statement.getPredicate() instanceof Statement) && (nal.getCurrentTask().sentence.isJudgment())) {
            CompositionalRules.IntroVarSameSubjectOrPredicate(originalMainSentence, subSentence, component, content, index, nal);
        }
    }
}
Also used : Equivalence(nars.language.Equivalence) Statement(nars.language.Statement) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Sentence(nars.entity.Sentence) Implication(nars.language.Implication)

Example 42 with Term

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

the class RuleTables method reason.

/**
 * Entry point of the inference engine
 *
 * @param tLink The selected TaskLink, which will provide a task
 * @param bLink The selected TermLink, which may provide a belief
 * @param memory Reference to the memory
 */
public static void reason(final TaskLink tLink, final TermLink bLink, final DerivationContext nal) {
    final Memory memory = nal.mem();
    final Task task = nal.getCurrentTask();
    final Sentence taskSentence = task.sentence;
    // cloning for substitution
    final Term taskTerm = taskSentence.term;
    // cloning for substitution
    Term beliefTerm = bLink.target;
    final Concept beliefConcept = memory.concept(beliefTerm);
    Sentence belief = (beliefConcept != null) ? beliefConcept.getBelief(nal, task) : null;
    nal.setCurrentBelief(belief);
    if (belief != null) {
        // because interval handling that differs on conceptual level
        beliefTerm = belief.term;
        /*Sentence belief_event = beliefConcept.getBeliefForTemporalInference(task);
            if(belief_event != null) {
                boolean found_overlap = false;
                if(Stamp.baseOverlap(task.sentence.stamp.evidentialBase, belief_event.stamp.evidentialBase)) {
                    found_overlap = true;
                }
                if(!found_overlap) { //temporal rules are inductive so no chance to succeed if there is an overlap
                                     //and since the temporal rule is relatively expensive the check here was good.
                    Sentence inference_belief = belief;
                    nal.setCurrentBelief(belief_event);
                    nal.setTheNewStamp(task.sentence.stamp, belief_event.stamp, nal.memory.time());
                    TemporalRules.temporalInduction(task.sentence, belief_event, nal, true);
                    nal.setCurrentBelief(inference_belief);
                    nal.setTheNewStamp(task.sentence.stamp, belief.stamp, nal.memory.time());
                }
            }*/
        // too restrictive, its checked for non-deductive inference rules in derivedTask (also for single prem)
        nal.evidentalOverlap = Stamp.baseOverlap(task.sentence.stamp.evidentialBase, belief.stamp.evidentialBase);
        if (nal.evidentalOverlap && (!task.sentence.isEternal() || !belief.isEternal())) {
            // only allow for eternal reasoning for now to prevent derived event floods
            return;
        }
        nal.emit(Events.BeliefReason.class, belief, beliefTerm, taskTerm, nal);
        if (LocalRules.match(task, belief, nal)) {
            // new tasks resulted from the match, so return
            return;
        }
    }
    // current belief and task may have changed, so set again:
    nal.setCurrentBelief(belief);
    nal.setCurrentTask(task);
    // put here since LocalRules match should be possible even if the belief is foreign
    if (equalSubTermsInRespectToImageAndProduct(taskTerm, beliefTerm))
        return;
    /*if ((memory.getNewTaskCount() > 0) && taskSentence.isJudgment()) {
            return;
        }*/
    final short tIndex = tLink.getIndex(0);
    short bIndex = bLink.getIndex(0);
    switch(// dispatch first by TaskLink type
    tLink.type) {
        case TermLink.SELF:
            switch(bLink.type) {
                case TermLink.COMPONENT:
                    compoundAndSelf((CompoundTerm) taskTerm, beliefTerm, true, bIndex, nal);
                    break;
                case TermLink.COMPOUND:
                    compoundAndSelf((CompoundTerm) beliefTerm, taskTerm, false, bIndex, nal);
                    break;
                case TermLink.COMPONENT_STATEMENT:
                    if (belief != null) {
                        if (taskTerm instanceof Statement) {
                            SyllogisticRules.detachment(taskSentence, belief, bIndex, nal);
                        }
                    }
                    // else {
                    if (taskSentence.term instanceof Inheritance || taskSentence.term instanceof Similarity) {
                        StructuralRules.transformNegation((CompoundTerm) Negation.make(taskSentence.term), nal);
                    }
                    try {
                        goalFromQuestion(task, taskTerm, nal);
                    } catch (Exception ex) {
                        if (Parameters.DEBUG) {
                            System.out.print("Error in goalFromQuestion");
                        }
                    }
                    // }
                    break;
                case TermLink.COMPOUND_STATEMENT:
                    if (belief != null) {
                        SyllogisticRules.detachment(belief, taskSentence, bIndex, nal);
                    }
                    break;
                case TermLink.COMPONENT_CONDITION:
                    if ((belief != null) && (taskTerm instanceof Implication)) {
                        bIndex = bLink.getIndex(1);
                        SyllogisticRules.conditionalDedInd(task.sentence, (Implication) taskTerm, bIndex, beliefTerm, tIndex, nal);
                    }
                    break;
                case TermLink.COMPOUND_CONDITION:
                    if ((belief != null) && (taskTerm instanceof Implication) && (beliefTerm instanceof Implication)) {
                        bIndex = bLink.getIndex(1);
                        SyllogisticRules.conditionalDedInd(belief, (Implication) beliefTerm, bIndex, taskTerm, tIndex, nal);
                    }
                    break;
            }
            break;
        case TermLink.COMPOUND:
            switch(bLink.type) {
                case TermLink.COMPOUND:
                    compoundAndCompound((CompoundTerm) taskTerm, (CompoundTerm) beliefTerm, tIndex, bIndex, nal);
                    break;
                case TermLink.COMPOUND_STATEMENT:
                    compoundAndStatement((CompoundTerm) taskTerm, tIndex, (Statement) beliefTerm, bIndex, beliefTerm, nal);
                    break;
                case TermLink.COMPOUND_CONDITION:
                    if (belief != null) {
                        if (beliefTerm instanceof Implication) {
                            Term[] u = new Term[] { beliefTerm, taskTerm };
                            if (Variables.unify(VAR_INDEPENDENT, ((Statement) beliefTerm).getSubject(), taskTerm, u, true)) {
                                // only secure place that
                                // allows partial match
                                Sentence newBelief = belief.clone(u[0]);
                                Sentence newTaskSentence = taskSentence.clone(u[1]);
                                detachmentWithVar(newBelief, newTaskSentence, bIndex, false, nal);
                            } else {
                                SyllogisticRules.conditionalDedInd(belief, (Implication) beliefTerm, bIndex, taskTerm, -1, nal);
                            }
                        } else if (beliefTerm instanceof Equivalence) {
                            SyllogisticRules.conditionalAna((Equivalence) beliefTerm, bIndex, taskTerm, -1, nal);
                        }
                    }
                    break;
            }
            break;
        case TermLink.COMPOUND_STATEMENT:
            switch(bLink.type) {
                case TermLink.COMPONENT:
                    if (taskTerm instanceof Statement) {
                        goalFromWantBelief(task, tIndex, bIndex, taskTerm, nal, beliefTerm);
                        componentAndStatement((CompoundTerm) nal.getCurrentTerm(), bIndex, (Statement) taskTerm, tIndex, nal);
                    }
                    break;
                case TermLink.COMPOUND:
                    if (taskTerm instanceof Statement) {
                        compoundAndStatement((CompoundTerm) beliefTerm, bIndex, (Statement) taskTerm, tIndex, beliefTerm, nal);
                    }
                    break;
                case TermLink.COMPOUND_STATEMENT:
                    if (belief != null) {
                        syllogisms(tLink, bLink, taskTerm, beliefTerm, nal);
                    }
                    break;
                case TermLink.COMPOUND_CONDITION:
                    if (belief != null) {
                        bIndex = bLink.getIndex(1);
                        if ((taskTerm instanceof Statement) && (beliefTerm instanceof Implication)) {
                            conditionalDedIndWithVar(belief, (Implication) beliefTerm, bIndex, (Statement) taskTerm, tIndex, nal);
                        }
                    }
                    break;
            }
            break;
        case TermLink.COMPOUND_CONDITION:
            switch(bLink.type) {
                case TermLink.COMPOUND:
                    if (belief != null) {
                        detachmentWithVar(taskSentence, belief, tIndex, nal);
                    }
                    break;
                case TermLink.COMPOUND_STATEMENT:
                    if (belief != null) {
                        if (// TODO maybe put instanceof test within conditionalDedIndWithVar()
                        taskTerm instanceof Implication) {
                            Term subj = ((Statement) taskTerm).getSubject();
                            if (subj instanceof Negation) {
                                if (taskSentence.isJudgment()) {
                                    componentAndStatement((CompoundTerm) subj, bIndex, (Statement) taskTerm, tIndex, nal);
                                } else {
                                    componentAndStatement((CompoundTerm) subj, tIndex, (Statement) beliefTerm, bIndex, nal);
                                }
                            } else {
                                conditionalDedIndWithVar(task.sentence, (Implication) taskTerm, tIndex, (Statement) beliefTerm, bIndex, nal);
                            }
                        }
                        break;
                    }
                    break;
            }
    }
}
Also used : Concept(nars.entity.Concept) Task(nars.entity.Task) Similarity(nars.language.Similarity) Negation(nars.language.Negation) Memory(nars.storage.Memory) Statement(nars.language.Statement) Inheritance(nars.language.Inheritance) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Implication(nars.language.Implication) Equivalence(nars.language.Equivalence) Events(nars.io.events.Events) Sentence(nars.entity.Sentence)

Example 43 with Term

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

the class RuleTables method asymmetricSymmetric.

/**
 * Syllogistic rules whose first premise is on an asymmetric relation, and
 * the second on a symmetric relation
 *
 * @param asym The asymmetric premise
 * @param sym The symmetric premise
 * @param figure The location of the shared term
 * @param nal Reference to the memory
 */
private static void asymmetricSymmetric(final Sentence asym, final Sentence sym, final int figure, final DerivationContext nal) {
    Statement asymSt = (Statement) asym.term;
    Statement symSt = (Statement) sym.term;
    Term t1, t2;
    Term[] u = new Term[] { asymSt, symSt };
    switch(figure) {
        case 11:
            if (Variables.unify(VAR_INDEPENDENT, asymSt.getSubject(), symSt.getSubject(), u)) {
                asymSt = (Statement) u[0];
                symSt = (Statement) u[1];
                t1 = asymSt.getPredicate();
                t2 = symSt.getPredicate();
                if (Variables.unify(VAR_QUERY, t1, t2, u)) {
                    LocalRules.matchAsymSym(asym, sym, figure, nal);
                } else {
                    SyllogisticRules.analogy(t2, t1, asym, sym, figure, nal);
                }
            }
            break;
        case 12:
            if (Variables.unify(VAR_INDEPENDENT, asymSt.getSubject(), symSt.getPredicate(), u)) {
                asymSt = (Statement) u[0];
                symSt = (Statement) u[1];
                t1 = asymSt.getPredicate();
                t2 = symSt.getSubject();
                if (Variables.unify(VAR_QUERY, t1, t2, u)) {
                    LocalRules.matchAsymSym(asym, sym, figure, nal);
                } else {
                    SyllogisticRules.analogy(t2, t1, asym, sym, figure, nal);
                }
            }
            break;
        case 21:
            if (Variables.unify(VAR_INDEPENDENT, asymSt.getPredicate(), symSt.getSubject(), u)) {
                asymSt = (Statement) u[0];
                symSt = (Statement) u[1];
                t1 = asymSt.getSubject();
                t2 = symSt.getPredicate();
                if (Variables.unify(VAR_QUERY, t1, t2, u)) {
                    LocalRules.matchAsymSym(asym, sym, figure, nal);
                } else {
                    SyllogisticRules.analogy(t1, t2, asym, sym, figure, nal);
                }
            }
            break;
        case 22:
            if (Variables.unify(VAR_INDEPENDENT, asymSt.getPredicate(), symSt.getPredicate(), u)) {
                asymSt = (Statement) u[0];
                symSt = (Statement) u[1];
                t1 = asymSt.getSubject();
                t2 = symSt.getSubject();
                if (Variables.unify(VAR_QUERY, t1, t2, u)) {
                    LocalRules.matchAsymSym(asym, sym, figure, nal);
                } else {
                    SyllogisticRules.analogy(t1, t2, asym, sym, figure, nal);
                }
            }
            break;
    }
}
Also used : Statement(nars.language.Statement) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term)

Example 44 with Term

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

the class StructuralRules method transformSubjectPI.

/**
 * Equivalent transformation between products and images when the subject is
 * a compound {<(*, S, M) --> P>, S@(*, S, M)} |- <S --> (/, P, _, M)> {<S
 * --> (/, P, _, M)>, P@(/, P, _, M)} |- <(*, S, M) --> P> {<S --> (/, P, _,
 * M)>, M@(/, P, _, M)} |- <M --> (/, P, S, _)>
 *
 * @param subject The subject term
 * @param predicate The predicate term
 * @param nal Reference to the memory
 */
private static void transformSubjectPI(short index, CompoundTerm subject, Term predicate, DerivationContext nal) {
    TruthValue truth = nal.getCurrentTask().sentence.truth;
    BudgetValue budget;
    Inheritance inheritance;
    Term newSubj, newPred;
    if (subject instanceof Product) {
        Product product = (Product) subject;
        short i = index;
        /*for (short i = 0; i < product.size(); i++)*/
        {
            newSubj = product.term[i];
            newPred = ImageExt.make(product, predicate, i);
            if (!(newSubj instanceof Interval)) {
                // no intervals as subjects
                inheritance = Inheritance.make(newSubj, newPred);
                if (inheritance != null) {
                    if (truth == null) {
                        budget = BudgetFunctions.compoundBackward(inheritance, nal);
                    } else {
                        budget = BudgetFunctions.compoundForward(truth, inheritance, nal);
                    }
                    nal.singlePremiseTask(inheritance, truth, budget);
                }
            }
        }
    } else if (subject instanceof ImageInt) {
        ImageInt image = (ImageInt) subject;
        int relationIndex = image.relationIndex;
        for (short i = 0; i < image.size(); i++) {
            if (i == relationIndex) {
                newSubj = image.term[relationIndex];
                newPred = Product.make(image, predicate, relationIndex);
            } else {
                newSubj = ImageInt.make(image, predicate, i);
                newPred = image.term[i];
            }
            inheritance = Inheritance.make(newSubj, newPred);
            if (inheritance != null) {
                if (truth == null) {
                    budget = BudgetFunctions.compoundBackward(inheritance, nal);
                } else {
                    budget = BudgetFunctions.compoundForward(truth, inheritance, nal);
                }
                nal.singlePremiseTask(inheritance, truth, budget);
            }
        }
    }
}
Also used : BudgetValue(nars.entity.BudgetValue) TruthValue(nars.entity.TruthValue) Inheritance(nars.language.Inheritance) Product(nars.language.Product) ImageInt(nars.language.ImageInt) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Interval(nars.language.Interval)

Example 45 with Term

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

the class StructuralRules method groupSequence.

/* --------------- Group sequence left and right --------------- */
/**
 * {(#,A,B,C,D,E), C@(#,A,B,C,D,E)} |- (#,(#,A,B),C,D,E), (#,A,B,C,(#,D,E))
 * Works for all conjunctions
 * @param compound The premise
 * @param component The recognized component in the premise
 * @param compoundTask Whether the compound comes from the task
 * @param nal Reference to the memory
 */
static void groupSequence(CompoundTerm compound, Term component, boolean compoundTask, int index, DerivationContext nal) {
    if (compound instanceof Conjunction) {
        Conjunction conjCompound = (Conjunction) compound;
        if (conjCompound.getTemporalOrder() == TemporalRules.ORDER_FORWARD) {
            boolean hasLeft = index > 1;
            boolean hasRight = index < compound.size() - 2;
            if (hasLeft) {
                // if index-1 it would have length 1, no group
                int minIndex = Memory.randomNumber.nextInt(index - 1);
                Term[] newTermLeft = new Term[(index - minIndex)];
                System.arraycopy(conjCompound.term, minIndex, newTermLeft, minIndex - minIndex, index - minIndex);
                Term contLeft = Conjunction.make(newTermLeft, conjCompound.getTemporalOrder(), conjCompound.getIsSpatial());
                Term[] totalLeft = new Term[conjCompound.size() - newTermLeft.length + 1];
                // 1. add left of min index
                int k = 0;
                for (int i = 0; i < minIndex; i++) {
                    totalLeft[k++] = conjCompound.term[i];
                }
                // add formed group
                totalLeft[k] = contLeft;
                k += newTermLeft.length - 1;
                // and add what is after
                for (int i = index; i < conjCompound.size(); i++) {
                    totalLeft[k++] = conjCompound.term[i];
                }
                Term cont1 = Conjunction.make(totalLeft, conjCompound.getTemporalOrder(), conjCompound.getIsSpatial());
                if (cont1 instanceof Conjunction && totalLeft.length != conjCompound.size()) {
                    TruthValue truth = nal.getCurrentTask().sentence.truth.clone();
                    BudgetValue budget = BudgetFunctions.forward(truth, nal);
                    nal.singlePremiseTask(cont1, truth, budget);
                }
            }
            if (hasRight) {
                int maxIndex = compound.term.length - 1 - (Memory.randomNumber.nextInt(1 + (compound.term.length - 1) - (index + 2)));
                Term[] newTermRight = new Term[maxIndex - index];
                System.arraycopy(conjCompound.term, index + 1, newTermRight, index + 1 - (index + 1), maxIndex + 1 - (index + 1));
                Term contRight = Conjunction.make(newTermRight, conjCompound.getTemporalOrder(), conjCompound.getIsSpatial());
                Term[] totalRight = new Term[conjCompound.size() - newTermRight.length + 1];
                // 2. add left of index
                int k = 0;
                for (int i = 0; i <= index; i++) {
                    totalRight[k++] = conjCompound.term[i];
                }
                // add formed group
                totalRight[k] = contRight;
                k += newTermRight.length - 1 - 1;
                // and add what is after
                for (int i = maxIndex + 1; i < conjCompound.size(); i++) {
                    totalRight[k++] = conjCompound.term[i];
                }
                Term cont2 = Conjunction.make(totalRight, conjCompound.getTemporalOrder(), conjCompound.getIsSpatial());
                if (cont2 instanceof Conjunction && totalRight.length != conjCompound.size()) {
                    TruthValue truth = nal.getCurrentTask().sentence.truth.clone();
                    BudgetValue budget = BudgetFunctions.forward(truth, nal);
                    nal.singlePremiseTask(cont2, truth, budget);
                }
            }
        }
    }
}
Also used : BudgetValue(nars.entity.BudgetValue) TruthValue(nars.entity.TruthValue) Conjunction(nars.language.Conjunction) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term)

Aggregations

Term (nars.language.Term)109 CompoundTerm (nars.language.CompoundTerm)66 BudgetValue (nars.entity.BudgetValue)48 TruthValue (nars.entity.TruthValue)46 Sentence (nars.entity.Sentence)40 Task (nars.entity.Task)37 Statement (nars.language.Statement)28 Conjunction (nars.language.Conjunction)20 Inheritance (nars.language.Inheritance)19 Stamp (nars.entity.Stamp)17 Concept (nars.entity.Concept)14 Implication (nars.language.Implication)13 Product (nars.language.Product)11 NAR (nars.main.NAR)9 Interval (nars.language.Interval)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)7 SetExt (nars.language.SetExt)7 SetInt (nars.language.SetInt)7 Variable (nars.language.Variable)7