use of nars.language.CompoundTerm in project opennars by opennars.
the class CompositionalRules method introVarOuter.
/* --------------- rules used for variable introduction --------------- */
/**
* Introduce a dependent variable in an outer-layer conjunction {<S --> P1>,
* <S --> P2>} |- (&&, <#x --> P1>, <#x --> P2>)
*
* @param taskContent The first premise <M --> S>
* @param beliefContent The second premise <M --> P>
* @param index The location of the shared term: 0 for subject, 1 for
* predicate
* @param nal Reference to the memory
*/
public static void introVarOuter(final Statement taskContent, final Statement beliefContent, final int index, final DerivationContext nal) {
if (!(taskContent instanceof Inheritance)) {
return;
}
Variable varInd1 = new Variable("$varInd1");
Variable varInd2 = new Variable("$varInd2");
Term term11dependent = null, term12dependent = null, term21dependent = null, term22dependent = null;
Term term11, term12, term21, term22, commonTerm = null;
HashMap<Term, Term> subs = new HashMap<>();
if (index == 0) {
term11 = varInd1;
term21 = varInd1;
term12 = taskContent.getPredicate();
term22 = beliefContent.getPredicate();
term12dependent = term12;
term22dependent = term22;
if (term12 instanceof ImageExt) {
if ((/*(ImageExt)*/
term12).containsTermRecursively(term22)) {
commonTerm = term22;
}
if (commonTerm == null && term12 instanceof ImageExt) {
commonTerm = ((ImageExt) term12).getTheOtherComponent();
if (!(term22.containsTermRecursively(commonTerm))) {
commonTerm = null;
}
if (term22 instanceof ImageExt && ((commonTerm == null) || !(term22).containsTermRecursively(commonTerm))) {
commonTerm = ((ImageExt) term22).getTheOtherComponent();
if ((commonTerm == null) || !(term12).containsTermRecursively(commonTerm)) {
commonTerm = null;
}
}
}
if (commonTerm != null) {
subs.put(commonTerm, varInd2);
term12 = ((CompoundTerm) term12).applySubstitute(subs);
if (!(term22 instanceof CompoundTerm)) {
term22 = varInd2;
} else {
term22 = ((CompoundTerm) term22).applySubstitute(subs);
}
}
}
if (commonTerm == null && term22 instanceof ImageExt) {
if ((/*(ImageExt)*/
term22).containsTermRecursively(term12)) {
commonTerm = term12;
}
if (commonTerm == null && term22 instanceof ImageExt) {
commonTerm = ((ImageExt) term22).getTheOtherComponent();
if (!(term12.containsTermRecursively(commonTerm))) {
commonTerm = null;
}
if (term12 instanceof ImageExt && ((commonTerm == null) || !(term12).containsTermRecursively(commonTerm))) {
commonTerm = ((ImageExt) term12).getTheOtherComponent();
if ((commonTerm == null) || !(term22).containsTermRecursively(commonTerm)) {
commonTerm = null;
}
}
}
if (commonTerm != null) {
subs.put(commonTerm, varInd2);
term22 = ((CompoundTerm) term22).applySubstitute(subs);
if (!(term12 instanceof CompoundTerm)) {
term12 = varInd2;
} else {
term12 = ((CompoundTerm) term12).applySubstitute(subs);
}
}
}
} else {
term11 = taskContent.getSubject();
term21 = beliefContent.getSubject();
term12 = varInd1;
term22 = varInd1;
term11dependent = term11;
term21dependent = term21;
if (term21 instanceof ImageInt) {
if ((/*(ImageInt)*/
term21).containsTermRecursively(term11)) {
commonTerm = term11;
}
if (term11 instanceof ImageInt && commonTerm == null && term21 instanceof ImageInt) {
commonTerm = ((ImageInt) term11).getTheOtherComponent();
if (!(term21.containsTermRecursively(commonTerm))) {
commonTerm = null;
}
if ((commonTerm == null) || !(term21).containsTermRecursively(commonTerm)) {
commonTerm = ((ImageInt) term21).getTheOtherComponent();
if ((commonTerm == null) || !(term11).containsTermRecursively(commonTerm)) {
commonTerm = null;
}
}
}
if (commonTerm != null) {
subs.put(commonTerm, varInd2);
term21 = ((CompoundTerm) term21).applySubstitute(subs);
if (!(term11 instanceof CompoundTerm)) {
term11 = varInd2;
} else {
term11 = ((CompoundTerm) term11).applySubstitute(subs);
}
}
}
if (commonTerm == null && term11 instanceof ImageInt) {
if ((/*(ImageInt)*/
term11).containsTermRecursively(term21)) {
commonTerm = term21;
}
if (term21 instanceof ImageInt && commonTerm == null && term11 instanceof ImageInt) {
commonTerm = ((ImageInt) term21).getTheOtherComponent();
if (!(term11.containsTermRecursively(commonTerm))) {
commonTerm = null;
}
if ((commonTerm == null) || !(term11).containsTermRecursively(commonTerm)) {
commonTerm = ((ImageInt) term11).getTheOtherComponent();
if ((commonTerm == null) || !(term21).containsTermRecursively(commonTerm)) {
commonTerm = null;
}
}
}
if (commonTerm != null) {
subs.put(commonTerm, varInd2);
term11 = ((CompoundTerm) term11).applySubstitute(subs);
if (!(term21 instanceof CompoundTerm)) {
term21 = varInd2;
} else {
term21 = ((CompoundTerm) term21).applySubstitute(subs);
}
}
}
}
Statement state1 = Inheritance.make(term11, term12);
Statement state2 = Inheritance.make(term21, term22);
Term content = Implication.make(state1, state2);
if (content == null) {
return;
}
TruthValue truthT = nal.getCurrentTask().sentence.truth;
TruthValue truthB = nal.getCurrentBelief().truth;
if ((truthT == null) || (truthB == null)) {
if (Parameters.DEBUG) {
System.out.println("ERROR: Belief with null truth value. (introVarOuter)");
}
return;
}
TruthValue truth = induction(truthT, truthB);
BudgetValue budget = BudgetFunctions.compoundForward(truth, content, nal);
nal.doublePremiseTask(content, truth, budget, false, false);
content = Implication.make(state2, state1);
truth = induction(truthB, truthT);
budget = BudgetFunctions.compoundForward(truth, content, nal);
nal.doublePremiseTask(content, truth, budget, false, false);
content = Equivalence.make(state1, state2);
truth = comparison(truthT, truthB);
budget = BudgetFunctions.compoundForward(truth, content, nal);
nal.doublePremiseTask(content, truth, budget, false, false);
Variable varDep = new Variable("#varDep");
if (index == 0) {
state1 = Inheritance.make(varDep, term12dependent);
state2 = Inheritance.make(varDep, term22dependent);
} else {
state1 = Inheritance.make(term11dependent, varDep);
state2 = Inheritance.make(term21dependent, varDep);
}
if ((state1 == null) || (state2 == null))
return;
content = Conjunction.make(state1, state2);
truth = intersection(truthT, truthB);
budget = BudgetFunctions.compoundForward(truth, content, nal);
nal.doublePremiseTask(content, truth, budget, false, false);
}
use of nars.language.CompoundTerm in project opennars by opennars.
the class LocalRules method convertRelation.
/**
* {<S --> P>} |- <S <-> P> {<S <-> P>} |- <S --> P> Switch between
* Inheritance/Implication and Similarity/Equivalence
*
* @param nal Reference to the memory
*/
private static void convertRelation(final DerivationContext nal) {
TruthValue truth = nal.getCurrentBelief().truth;
if (((CompoundTerm) nal.getCurrentTask().getTerm()).isCommutative()) {
truth = TruthFunctions.abduction(truth, 1.0f);
} else {
truth = TruthFunctions.deduction(truth, 1.0f);
}
BudgetValue budget = BudgetFunctions.forward(truth, nal);
convertedJudgment(truth, budget, nal);
}
use of nars.language.CompoundTerm 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)
}
}
}
use of nars.language.CompoundTerm 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;
}
}
}
use of nars.language.CompoundTerm 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);
}
}
}
}
Aggregations