use of nars.language.Equivalence 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);
}
}
}
use of nars.language.Equivalence 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.Equivalence in project opennars by opennars.
the class SyllogisticRules method resemblance.
/**
* {<S <=> M>, <M <=> P>} |- <S <=> P>
*
* @param term1 Subject of the new task
* @param term2 Predicate of the new task
* @param belief The first premise
* @param sentence The second premise
* @param figure Locations of the shared term in premises
* @param nal Reference to the memory
*/
static void resemblance(Term term1, Term term2, Sentence belief, Sentence sentence, int figure, DerivationContext nal) {
if (Statement.invalidStatement(term1, term2)) {
return;
}
int order1 = belief.term.getTemporalOrder();
int order2 = sentence.term.getTemporalOrder();
int order = resemblanceOrder(order1, order2, figure);
if (order == ORDER_INVALID) {
return;
}
Statement st = (Statement) belief.term;
TruthValue truth = null;
BudgetValue budget;
if (sentence.isQuestion() || sentence.isQuest()) {
budget = BudgetFunctions.backward(belief.truth, nal);
} else {
if (sentence.isGoal()) {
truth = TruthFunctions.desireStrong(sentence.truth, belief.truth);
} else {
truth = TruthFunctions.resemblance(belief.truth, sentence.truth);
}
budget = BudgetFunctions.forward(truth, nal);
}
boolean higherOrder = (belief.term.isHigherOrderStatement() || sentence.term.isHigherOrderStatement());
boolean bothHigherOrder = (belief.term.isHigherOrderStatement() && sentence.term.isHigherOrderStatement());
if (!bothHigherOrder && higherOrder) {
if (belief.term.isHigherOrderStatement()) {
order = belief.term.getTemporalOrder();
} else if (sentence.term.isHigherOrderStatement()) {
order = sentence.term.getTemporalOrder();
}
}
Statement s = Statement.make(higherOrder ? NativeOperator.EQUIVALENCE : NativeOperator.SIMILARITY, term1, term2, order);
// (allow overlap) but not needed here, isn't detachment
nal.doublePremiseTask(s, truth, budget, false, false);
if (Parameters.BREAK_NAL_HOL_BOUNDARY && !sentence.term.hasVarIndep() && (st instanceof Equivalence) && order1 == order2 && belief.term.isHigherOrderStatement() && sentence.term.isHigherOrderStatement()) {
BudgetValue budget1 = null, budget2 = null, budget3 = null;
TruthValue truth1 = null, truth2 = null, truth3 = null;
TruthValue value1 = sentence.truth;
TruthValue value2 = belief.truth;
if (sentence.isQuestion()) {
/* budget1 = BudgetFunctions.backward(value2, nal);
budget2 = BudgetFunctions.backwardWeak(value2, nal);*/
budget3 = BudgetFunctions.backward(value2, nal);
} else if (sentence.isQuest()) {
/* budget1 = BudgetFunctions.backwardWeak(value2, nal);
budget2 = BudgetFunctions.backward(value2, nal);*/
budget3 = BudgetFunctions.backwardWeak(value2, nal);
} else {
if (sentence.isGoal()) {
/* truth1 = TruthFunctions.desireStrong(value1, value2);
truth2 = TruthFunctions.desireWeak(value2, value1);*/
truth3 = TruthFunctions.desireStrong(value1, value2);
} else {
// isJudgment
/* truth1 = TruthFunctions.abduction(value1, value2);
truth2 = TruthFunctions.abduction(value2, value1);*/
truth3 = TruthFunctions.comparison(value1, value2);
}
/*budget1 = BudgetFunctions.forward(truth1, nal);
budget2 = BudgetFunctions.forward(truth2, nal);*/
budget3 = BudgetFunctions.forward(truth3, nal);
}
/* Bridge to higher order statements:
<b <=> k>.
<b <=> c>.
|-
<k <-> c>. %F_cmp%
*/
/* nal.doublePremiseTask(
Statement.make(NativeOperator.INHERITANCE, term1, term2),
truth1, budget1.clone(),false, false);
nal.doublePremiseTask(
Statement.make(NativeOperator.INHERITANCE, term2, term1),
truth2, budget2.clone(),false, false);*/
nal.doublePremiseTask(Statement.make(NativeOperator.SIMILARITY, term1, term2, TemporalRules.ORDER_NONE), truth3, budget3.clone(), false, false);
}
}
use of nars.language.Equivalence in project opennars by opennars.
the class SyllogisticRules method detachment.
static void detachment(Sentence mainSentence, Sentence subSentence, int side, boolean checkTermAgain, DerivationContext nal) {
Statement statement = (Statement) mainSentence.term;
if (!(statement instanceof Implication) && !(statement instanceof Equivalence)) {
return;
}
Term subject = statement.getSubject();
Term predicate = statement.getPredicate();
Term content;
Term term = subSentence.term;
if ((side == 0) && (!checkTermAgain || term.equals(subject))) {
content = predicate;
} else if ((side == 1) && (!checkTermAgain || term.equals(predicate))) {
content = subject;
} else {
return;
}
if ((content instanceof Statement) && ((Statement) content).invalid()) {
return;
}
Sentence taskSentence = nal.getCurrentTask().sentence;
Sentence beliefSentence = nal.getCurrentBelief();
if (beliefSentence == null)
return;
int order = statement.getTemporalOrder();
long occurrence_time = nal.getTheNewStamp().getOccurrenceTime();
if ((order != ORDER_NONE) && (order != ORDER_INVALID)) {
long baseTime = subSentence.getOccurenceTime();
if (baseTime == Stamp.ETERNAL) {
// =/> always should produce events
baseTime = nal.getTime();
}
long inc = order * Parameters.DURATION;
occurrence_time = (side == 0) ? baseTime + inc : baseTime - inc;
}
TruthValue beliefTruth = beliefSentence.truth;
TruthValue truth1 = mainSentence.truth;
TruthValue truth2 = subSentence.truth;
TruthValue truth = null;
boolean strong = false;
BudgetValue budget;
if (taskSentence.isQuestion()) {
if (statement instanceof Equivalence) {
budget = BudgetFunctions.backward(beliefTruth, nal);
} else if (side == 0) {
budget = BudgetFunctions.backwardWeak(beliefTruth, nal);
} else {
budget = BudgetFunctions.backward(beliefTruth, nal);
}
} else if (taskSentence.isQuest()) {
if (statement instanceof Equivalence) {
budget = BudgetFunctions.backwardWeak(beliefTruth, nal);
} else if (side == 0) {
budget = BudgetFunctions.backward(beliefTruth, nal);
} else {
budget = BudgetFunctions.backwardWeak(beliefTruth, nal);
}
} else {
if (taskSentence.isGoal()) {
if (statement instanceof Equivalence) {
truth = TruthFunctions.desireStrong(truth1, truth2);
// not for goals anymore
strong = true;
} else if (side == 0) {
truth = TruthFunctions.desireInd(truth1, truth2);
} else {
truth = TruthFunctions.desireDed(truth1, truth2);
// not for goals anymore
strong = true;
}
} else {
// isJudgment
if (statement instanceof Equivalence) {
truth = TruthFunctions.analogy(truth2, truth1);
strong = true;
} else if (side == 0) {
truth = TruthFunctions.deduction(truth1, truth2);
strong = true;
} else {
truth = TruthFunctions.abduction(truth2, truth1);
}
}
budget = BudgetFunctions.forward(truth, nal);
}
if (!Variables.indepVarUsedInvalid(content)) {
boolean allowOverlap = taskSentence.isJudgment() && strong;
nal.getTheNewStamp().setOccurrenceTime(occurrence_time);
// (strong) when strong on judgement
nal.doublePremiseTask(content, truth, budget, false, allowOverlap);
}
}
use of nars.language.Equivalence in project opennars by opennars.
the class DerivationContext method derivedTask.
public boolean derivedTask(final Task task, final boolean revised, final boolean single, boolean overlapAllowed, boolean addToMemory) {
if (task.sentence.isGoal() && (task.sentence.term instanceof Implication || task.sentence.term instanceof Equivalence)) {
// implication and equivalence goals are not supported anymore
return false;
}
if (!task.budget.aboveThreshold()) {
memory.removeTask(task, "Insufficient Budget");
return false;
}
if (task.sentence != null && task.sentence.truth != null) {
float conf = task.sentence.truth.getConfidence();
if (conf == 0) {
// no confidence - we can delete the wrongs out that way.
memory.removeTask(task, "Ignored (zero confidence)");
return false;
}
}
if (task.sentence.term instanceof Operation) {
Operation op = (Operation) task.sentence.term;
if (op.getSubject() instanceof Variable || op.getPredicate() instanceof Variable) {
memory.removeTask(task, "Operation with variable as subject or predicate");
return false;
}
}
final Stamp stamp = task.sentence.stamp;
// its revision, of course its cyclic, apply evidental base policy
if (!overlapAllowed) {
// todo reconsider
final int stampLength = stamp.baseLength;
for (int i = 0; i < stampLength; i++) {
final long baseI = stamp.evidentialBase[i];
for (int j = 0; j < stampLength; j++) {
// !single since the derivation shouldn't depend on whether there is a current belief or not!!
if ((!single && this.evidentalOverlap) || ((i != j) && (baseI == stamp.evidentialBase[j]))) {
memory.removeTask(task, "Overlapping Evidenctal Base");
// "(i=" + i + ",j=" + j +')' /* + " in " + stamp.toString()*/
return false;
}
}
}
}
// deactivated, new anticipation handling is attempted instead
/*if(task.sentence.getOccurenceTime()>memory.time() && ((this.getCurrentTask()!=null && (this.getCurrentTask().isInput() || this.getCurrentTask().sentence.producedByTemporalInduction)) || (this.getCurrentBelief()!=null && this.getCurrentBelief().producedByTemporalInduction))) {
Anticipate ret = ((Anticipate)memory.getOperator("^anticipate"));
if(ret!=null) {
ret.anticipate(task.sentence.term, memory, task.sentence.getOccurenceTime(),task);
}
}*/
task.setElemOfSequenceBuffer(false);
if (!revised) {
task.getBudget().setDurability(task.getBudget().getDurability() * Parameters.DERIVATION_DURABILITY_LEAK);
task.getBudget().setPriority(task.getBudget().getPriority() * Parameters.DERIVATION_PRIORITY_LEAK);
}
memory.event.emit(Events.TaskDerive.class, task, revised, single);
if (addToMemory) {
addTask(task, "Derived");
}
return true;
}
Aggregations