/** * 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 memory Reference to the memory */ private static void syllogisms( TaskLink tLink, TermLink bLink, Term taskTerm, Term beliefTerm, Memory memory) { Sentence taskSentence = memory.currentTask.getSentence(); Sentence belief = memory.currentBelief; int figure; if (taskTerm instanceof Inheritance) { if (beliefTerm instanceof Inheritance) { figure = indexToFigure(tLink, bLink); asymmetricAsymmetric(taskSentence, belief, figure, memory); } else if (beliefTerm instanceof Similarity) { figure = indexToFigure(tLink, bLink); asymmetricSymmetric(taskSentence, belief, figure, memory); } else { detachmentWithVar(belief, taskSentence, bLink.getIndex(0), memory); } } else if (taskTerm instanceof Similarity) { if (beliefTerm instanceof Inheritance) { figure = indexToFigure(bLink, tLink); asymmetricSymmetric(belief, taskSentence, figure, memory); } else if (beliefTerm instanceof Similarity) { figure = indexToFigure(bLink, tLink); symmetricSymmetric(belief, taskSentence, figure, memory); } } else if (taskTerm instanceof Implication) { if (beliefTerm instanceof Implication) { figure = indexToFigure(tLink, bLink); asymmetricAsymmetric(taskSentence, belief, figure, memory); } else if (beliefTerm instanceof Equivalence) { figure = indexToFigure(tLink, bLink); asymmetricSymmetric(taskSentence, belief, figure, memory); } else if (beliefTerm instanceof Inheritance) { detachmentWithVar(taskSentence, belief, tLink.getIndex(0), memory); } } else if (taskTerm instanceof Equivalence) { if (beliefTerm instanceof Implication) { figure = indexToFigure(bLink, tLink); asymmetricSymmetric(belief, taskSentence, figure, memory); } else if (beliefTerm instanceof Equivalence) { figure = indexToFigure(bLink, tLink); symmetricSymmetric(belief, taskSentence, figure, memory); } else if (beliefTerm instanceof Inheritance) { detachmentWithVar(taskSentence, belief, tLink.getIndex(0), memory); } } }
/** * 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(TaskLink tLink, TermLink bLink, Memory memory) { Task task = memory.currentTask; Sentence taskSentence = task.getSentence(); Term taskTerm = (Term) taskSentence.getContent().clone(); // cloning for substitution Term beliefTerm = (Term) bLink.getTarget().clone(); // cloning for substitution Concept beliefConcept = memory.termToConcept(beliefTerm); Sentence belief = null; if (beliefConcept != null) { belief = beliefConcept.getBelief(task); } memory.currentBelief = belief; // may be null if (belief != null) { LocalRules.match(task, belief, memory); } if (!memory.noResult()) { return; } short tIndex = tLink.getIndex(0); short bIndex = bLink.getIndex(0); switch (tLink.getType()) { // dispatch first by TaskLink type case TermLink.SELF: switch (bLink.getType()) { case TermLink.COMPONENT: compoundAndSelf((CompoundTerm) taskTerm, beliefTerm, true, memory); break; case TermLink.COMPOUND: compoundAndSelf((CompoundTerm) beliefTerm, taskTerm, false, memory); break; case TermLink.COMPONENT_STATEMENT: if (belief != null) { SyllogisticRules.detachment(task.getSentence(), belief, bIndex, memory); } break; case TermLink.COMPOUND_STATEMENT: if (belief != null) { SyllogisticRules.detachment(belief, task.getSentence(), bIndex, memory); } break; case TermLink.COMPONENT_CONDITION: if (belief != null) { bIndex = bLink.getIndex(1); SyllogisticRules.conditionalDedInd( (Implication) taskTerm, bIndex, beliefTerm, tIndex, memory); } break; case TermLink.COMPOUND_CONDITION: if (belief != null) { bIndex = bLink.getIndex(1); SyllogisticRules.conditionalDedInd( (Implication) beliefTerm, bIndex, taskTerm, tIndex, memory); } break; } break; case TermLink.COMPOUND: switch (bLink.getType()) { case TermLink.COMPOUND: compoundAndCompound((CompoundTerm) taskTerm, (CompoundTerm) beliefTerm, memory); break; case TermLink.COMPOUND_STATEMENT: compoundAndStatement( (CompoundTerm) taskTerm, tIndex, (Statement) beliefTerm, bIndex, beliefTerm, memory); break; case TermLink.COMPOUND_CONDITION: if (belief != null) { if (beliefTerm instanceof Implication) { SyllogisticRules.conditionalDedInd( (Implication) beliefTerm, bIndex, taskTerm, -1, memory); } else if (beliefTerm instanceof Equivalence) { SyllogisticRules.conditionalAna( (Equivalence) beliefTerm, bIndex, taskTerm, -1, memory); } } break; } break; case TermLink.COMPOUND_STATEMENT: switch (bLink.getType()) { case TermLink.COMPONENT: componentAndStatement( (CompoundTerm) memory.currentTerm, bIndex, (Statement) taskTerm, tIndex, memory); break; case TermLink.COMPOUND: compoundAndStatement( (CompoundTerm) beliefTerm, bIndex, (Statement) taskTerm, tIndex, beliefTerm, memory); break; case TermLink.COMPOUND_STATEMENT: if (belief != null) { // bIndex = bLink.getIndex(1); syllogisms(tLink, bLink, taskTerm, beliefTerm, memory); } break; case TermLink.COMPOUND_CONDITION: if (belief != null) { bIndex = bLink.getIndex(1); if (beliefTerm instanceof Implication) { conditionalDedIndWithVar( (Implication) beliefTerm, bIndex, (Statement) taskTerm, tIndex, memory); } } break; } break; case TermLink.COMPOUND_CONDITION: switch (bLink.getType()) { case TermLink.COMPOUND_STATEMENT: if (belief != null) { // if (beliefTerm instanceof Implication) // TODO adding instanceof test changes results of Example-NAL6-in.txt // TODO maybe put instanceof test within conditionalDedIndWithVar() conditionalDedIndWithVar( (Implication) taskTerm, tIndex, (Statement) beliefTerm, bIndex, memory); } break; } break; } }
/** * Decide the figure of syllogism according to the locations of the common term in the premises * * @param link1 The link to the first premise * @param link2 The link to the second premise * @return The figure of the syllogism, one of the four: 11, 12, 21, or 22 */ private static int indexToFigure(TermLink link1, TermLink link2) { return (link1.getIndex(0) + 1) * 10 + (link2.getIndex(0) + 1); }