Search in sources :

Example 1 with Similarity

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

the class Name method execute.

/**
 * To create a judgment with a given statement
 * @param args Arguments, a Statement followed by an optional tense
 * @param memory The memory in which the operation is executed
 * @return Immediate results as Tasks
 */
@Override
protected ArrayList<Task> execute(Operation operation, Term[] args, Memory memory) {
    Term compound = args[1];
    Term atomic = args[2];
    Similarity content = Similarity.make(compound, atomic);
    // a naming convension
    TruthValue truth = new TruthValue(1, 0.9999f);
    Sentence sentence = new Sentence(content, Symbols.JUDGMENT_MARK, truth, new Stamp(memory));
    BudgetValue budget = new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY, Parameters.DEFAULT_JUDGMENT_DURABILITY, truth);
    return Lists.newArrayList(new Task(sentence, budget, true));
}
Also used : BudgetValue(nars.entity.BudgetValue) Task(nars.entity.Task) Similarity(nars.language.Similarity) Stamp(nars.entity.Stamp) TruthValue(nars.entity.TruthValue) Term(nars.language.Term) Sentence(nars.entity.Sentence)

Example 2 with Similarity

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

the class RuleTables method compoundAndStatement.

/**
 * Inference between a compound term and a statement
 *
 * @param compound The compound term
 * @param index The location of the current term in the compound
 * @param statement The statement
 * @param side The location of the current term in the statement
 * @param beliefTerm The content of the belief
 * @param nal Reference to the memory
 */
private static void compoundAndStatement(CompoundTerm compound, short index, Statement statement, short side, Term beliefTerm, DerivationContext nal) {
    if (index >= compound.term.length) {
        return;
    }
    Term component = compound.term[index];
    Task task = nal.getCurrentTask();
    if (component.getClass() == statement.getClass()) {
        if ((compound instanceof Conjunction) && (nal.getCurrentBelief() != null)) {
            Conjunction conj = (Conjunction) compound;
            Term[] u = new Term[] { compound, statement };
            if (Variables.unify(VAR_DEPENDENT, component, statement, u)) {
                compound = (Conjunction) u[0];
                statement = (Statement) u[1];
                if (// only allow dep var elimination
                conj.isSpatial || compound.getTemporalOrder() != TemporalRules.ORDER_FORWARD || index == 0) {
                    // for (&/ on first component!!
                    SyllogisticRules.elimiVarDep(compound, component, statement.equals(beliefTerm), nal);
                }
            } else if (task.sentence.isJudgment()) {
                // && !compound.containsTerm(component)) {
                CompositionalRules.introVarInner(statement, (Statement) component, compound, nal);
            }
        }
    } else {
        if (task.sentence.isJudgment()) {
            if (statement instanceof Inheritance) {
                StructuralRules.structuralCompose1(compound, index, statement, nal);
                if (!(compound instanceof SetExt || compound instanceof SetInt || compound instanceof Negation)) {
                    StructuralRules.structuralCompose2(compound, index, statement, side, nal);
                }
            // {A --> B, A @ (A&C)} |- (A&C) --> (B&C)
            } else if ((statement instanceof Similarity) && !(compound instanceof Conjunction)) {
                StructuralRules.structuralCompose2(compound, index, statement, side, nal);
            }
        // {A <-> B, A @ (A&C)} |- (A&C) <-> (B&C)
        }
    }
}
Also used : Task(nars.entity.Task) Negation(nars.language.Negation) Similarity(nars.language.Similarity) Statement(nars.language.Statement) Inheritance(nars.language.Inheritance) Conjunction(nars.language.Conjunction) SetExt(nars.language.SetExt) SetInt(nars.language.SetInt) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term)

Example 3 with Similarity

use of nars.language.Similarity 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 4 with Similarity

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

the class CompositionalRules method IntroVarSameSubjectOrPredicate.

static void IntroVarSameSubjectOrPredicate(final Sentence originalMainSentence, final Sentence subSentence, final Term component, final Term content, final int index, final DerivationContext nal) {
    Term T1 = originalMainSentence.term;
    if (!(T1 instanceof CompoundTerm) || !(content instanceof CompoundTerm)) {
        return;
    }
    CompoundTerm T = (CompoundTerm) T1;
    CompoundTerm T2 = (CompoundTerm) content;
    if ((component instanceof Inheritance && content instanceof Inheritance) || (component instanceof Similarity && content instanceof Similarity)) {
        // CompoundTerm result = T;
        if (component.equals(content)) {
            // wouldnt make sense to create a conjunction here, would contain a statement twice
            return;
        }
        Variable depIndVar1 = new Variable("#depIndVar1");
        Variable depIndVar2 = new Variable("#depIndVar2");
        if (((Statement) component).getPredicate().equals(((Statement) content).getPredicate()) && !(((Statement) component).getPredicate() instanceof Variable)) {
            CompoundTerm zw = (CompoundTerm) T.term[index];
            zw = (CompoundTerm) zw.setComponent(1, depIndVar1, nal.mem());
            T2 = (CompoundTerm) T2.setComponent(1, depIndVar1, nal.mem());
            Conjunction res = (Conjunction) Conjunction.make(zw, T2);
            T = (CompoundTerm) T.setComponent(index, res, nal.mem());
        } else if (((Statement) component).getSubject().equals(((Statement) content).getSubject()) && !(((Statement) component).getSubject() instanceof Variable)) {
            CompoundTerm zw = (CompoundTerm) T.term[index];
            zw = (CompoundTerm) zw.setComponent(0, depIndVar2, nal.mem());
            T2 = (CompoundTerm) T2.setComponent(0, depIndVar2, nal.mem());
            Conjunction res = (Conjunction) Conjunction.make(zw, T2);
            T = (CompoundTerm) T.setComponent(index, res, nal.mem());
        }
        TruthValue truth = induction(originalMainSentence.truth, subSentence.truth);
        BudgetValue budget = BudgetFunctions.compoundForward(truth, T, nal);
        nal.doublePremiseTask(T, truth, budget, false, false);
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) BudgetValue(nars.entity.BudgetValue) Variable(nars.language.Variable) Similarity(nars.language.Similarity) Statement(nars.language.Statement) TruthValue(nars.entity.TruthValue) Inheritance(nars.language.Inheritance) TruthFunctions.reduceConjunction(nars.inference.TruthFunctions.reduceConjunction) Conjunction(nars.language.Conjunction) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term)

Example 5 with Similarity

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

the class RuleTables method syllogisms.

/* ----- syllogistic inferences ----- */
/**
 * Meta-table of syllogistic rules, indexed by the content classes of the
 * taskSentence and the belief
 *
 * @param tLink The link to task
 * @param bLink The link to belief
 * @param taskTerm The content of task
 * @param beliefTerm The content of belief
 * @param nal Reference to the memory
 */
private static void syllogisms(TaskLink tLink, TermLink bLink, Term taskTerm, Term beliefTerm, DerivationContext nal) {
    Sentence taskSentence = nal.getCurrentTask().sentence;
    Sentence belief = nal.getCurrentBelief();
    int figure;
    if (taskTerm instanceof Inheritance) {
        if (beliefTerm instanceof Inheritance) {
            figure = indexToFigure(tLink, bLink);
            asymmetricAsymmetric(taskSentence, belief, figure, nal);
        } else if (beliefTerm instanceof Similarity) {
            figure = indexToFigure(tLink, bLink);
            asymmetricSymmetric(taskSentence, belief, figure, nal);
        } else {
            detachmentWithVar(belief, taskSentence, bLink.getIndex(0), nal);
        }
    } else if (taskTerm instanceof Similarity) {
        if (beliefTerm instanceof Inheritance) {
            figure = indexToFigure(bLink, tLink);
            asymmetricSymmetric(belief, taskSentence, figure, nal);
        } else if (beliefTerm instanceof Similarity) {
            figure = indexToFigure(bLink, tLink);
            symmetricSymmetric(belief, taskSentence, figure, nal);
        } else if (beliefTerm instanceof Implication) {
            // Bridge to higher order statements:
            figure = indexToFigure(tLink, bLink);
            asymmetricSymmetric(belief, taskSentence, figure, nal);
        } else if (beliefTerm instanceof Equivalence) {
            // Bridge to higher order statements:
            figure = indexToFigure(tLink, bLink);
            symmetricSymmetric(belief, taskSentence, figure, nal);
        }
    } else if (taskTerm instanceof Implication) {
        if (beliefTerm instanceof Implication) {
            figure = indexToFigure(tLink, bLink);
            asymmetricAsymmetric(taskSentence, belief, figure, nal);
        } else if (beliefTerm instanceof Equivalence) {
            figure = indexToFigure(tLink, bLink);
            asymmetricSymmetric(taskSentence, belief, figure, nal);
        } else if (beliefTerm instanceof Inheritance) {
            detachmentWithVar(taskSentence, belief, tLink.getIndex(0), nal);
        } else if (beliefTerm instanceof Similarity) {
            // Bridge to higher order statements:
            figure = indexToFigure(tLink, bLink);
            asymmetricSymmetric(taskSentence, belief, figure, nal);
        }
    } else if (taskTerm instanceof Equivalence) {
        if (beliefTerm instanceof Implication) {
            figure = indexToFigure(bLink, tLink);
            asymmetricSymmetric(belief, taskSentence, figure, nal);
        } else if (beliefTerm instanceof Equivalence) {
            figure = indexToFigure(bLink, tLink);
            symmetricSymmetric(belief, taskSentence, figure, nal);
        } else if (beliefTerm instanceof Inheritance) {
            detachmentWithVar(taskSentence, belief, tLink.getIndex(0), nal);
        } else if (beliefTerm instanceof Similarity) {
            // Bridge to higher order statements:
            figure = indexToFigure(tLink, bLink);
            symmetricSymmetric(belief, taskSentence, figure, nal);
        }
    }
}
Also used : Similarity(nars.language.Similarity) Equivalence(nars.language.Equivalence) Inheritance(nars.language.Inheritance) Sentence(nars.entity.Sentence) Implication(nars.language.Implication)

Aggregations

Similarity (nars.language.Similarity)5 Inheritance (nars.language.Inheritance)4 Term (nars.language.Term)4 Sentence (nars.entity.Sentence)3 Task (nars.entity.Task)3 CompoundTerm (nars.language.CompoundTerm)3 Statement (nars.language.Statement)3 BudgetValue (nars.entity.BudgetValue)2 TruthValue (nars.entity.TruthValue)2 Conjunction (nars.language.Conjunction)2 Equivalence (nars.language.Equivalence)2 Implication (nars.language.Implication)2 Negation (nars.language.Negation)2 Concept (nars.entity.Concept)1 Stamp (nars.entity.Stamp)1 TruthFunctions.reduceConjunction (nars.inference.TruthFunctions.reduceConjunction)1 Events (nars.io.events.Events)1 SetExt (nars.language.SetExt)1 SetInt (nars.language.SetInt)1 Variable (nars.language.Variable)1