use of nars.entity.TruthValue in project opennars by opennars.
the class StructuralRules method splitConjunctionApart.
/* --------------- Split sequence apart --------------- */
/**
* {(#,A,B,C,D,E), C@(#,A,B,C,D,E)} |- (#,A,B,C), (#,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 splitConjunctionApart(CompoundTerm compound, Term component, boolean compoundTask, int index, DerivationContext nal) {
if (compound instanceof Conjunction) {
Conjunction conjCompound = (Conjunction) compound;
Term[] newTermLeft = new Term[index + 1];
Term[] newTermRight = new Term[conjCompound.size() - index];
if (// since nothing was splitted
newTermLeft.length == compound.size() || newTermRight.length == compound.size()) {
return;
}
System.arraycopy(conjCompound.term, 0, newTermLeft, 0, newTermLeft.length);
System.arraycopy(conjCompound.term, 0 + index, newTermRight, 0, newTermRight.length);
Conjunction cont1 = (Conjunction) Conjunction.make(newTermLeft, conjCompound.getTemporalOrder(), conjCompound.getIsSpatial());
Conjunction cont2 = (Conjunction) Conjunction.make(newTermRight, conjCompound.getTemporalOrder(), conjCompound.getIsSpatial());
TruthValue truth = TruthFunctions.deduction(nal.getCurrentTask().sentence.truth, Parameters.reliance);
BudgetValue budget = BudgetFunctions.forward(truth, nal);
nal.singlePremiseTask(cont1, truth, budget);
nal.singlePremiseTask(cont2, truth.clone(), budget.clone());
}
}
use of nars.entity.TruthValue in project opennars by opennars.
the class SyllogisticRules method conditionalAbd.
/**
* {<(&&, S2, S3) ==> P>, <(&&, S1, S3) ==> P>} |- <S1 ==> S2>
*
* @param cond1 The condition of the first premise
* @param cond2 The condition of the second premise
* @param taskContent The first premise
* @param st2 The second premise
* @param nal Reference to the memory
* @return Whether there are derived tasks
*/
static boolean conditionalAbd(Term cond1, Term cond2, Statement st1, Statement st2, DerivationContext nal) {
if (!(st1 instanceof Implication) || !(st2 instanceof Implication)) {
return false;
}
if (!(cond1 instanceof Conjunction) && !(cond2 instanceof Conjunction)) {
return false;
}
int order1 = st1.getTemporalOrder();
int order2 = st2.getTemporalOrder();
if (order1 != reverseOrder(order2)) {
return false;
}
Term term1 = null;
Term term2 = null;
if (cond1 instanceof Conjunction) {
term1 = reduceComponents((CompoundTerm) cond1, cond2, nal.mem());
}
if (cond2 instanceof Conjunction) {
term2 = reduceComponents((CompoundTerm) cond2, cond1, nal.mem());
}
if ((term1 == null) && (term2 == null)) {
return false;
}
Task task = nal.getCurrentTask();
Sentence sentence = task.sentence;
Sentence belief = nal.getCurrentBelief();
TruthValue value1 = sentence.truth;
TruthValue value2 = belief.truth;
Term content;
boolean keepOrder = Variables.hasSubstitute(Symbols.VAR_INDEPENDENT, st1, task.getTerm());
TruthValue truth = null;
BudgetValue budget;
if (term1 != null) {
if (term2 != null) {
content = Statement.make(st2, term2, term1, st2.getTemporalOrder());
} else {
content = term1;
if (content.hasVarIndep()) {
return false;
}
}
if (sentence.isQuestion() || sentence.isQuest()) {
budget = BudgetFunctions.backwardWeak(value2, nal);
} else {
if (sentence.isGoal()) {
if (keepOrder) {
truth = TruthFunctions.desireDed(value1, value2);
} else {
truth = TruthFunctions.desireInd(value1, value2);
}
} else {
// isJudgment
truth = TruthFunctions.abduction(value2, value1);
}
budget = BudgetFunctions.forward(truth, nal);
}
nal.doublePremiseTask(content, truth, budget, false, false);
}
if (term2 != null) {
if (term1 != null) {
content = Statement.make(st1, term1, term2, st1.getTemporalOrder());
} else {
content = term2;
if (content.hasVarIndep()) {
return false;
}
}
if (sentence.isQuestion() || sentence.isQuest()) {
budget = BudgetFunctions.backwardWeak(value2, nal);
} else {
if (sentence.isGoal()) {
if (keepOrder) {
truth = TruthFunctions.desireDed(value1, value2);
} else {
truth = TruthFunctions.desireInd(value1, value2);
}
} else {
// isJudgment
truth = TruthFunctions.abduction(value1, value2);
}
budget = BudgetFunctions.forward(truth, nal);
}
nal.doublePremiseTask(content, truth, budget, false, false);
}
return true;
}
use of nars.entity.TruthValue in project opennars by opennars.
the class SyllogisticRules method analogy.
/**
* {<S ==> P>, <M <=> P>} |- <S ==> P>
*
* @param subj Subject of the new task
* @param pred Predicate of the new task
* @param asym The asymmetric premise
* @param sym The symmetric premise
* @param figure Locations of the shared term in premises
* @param nal Reference to the memory
*/
static void analogy(Term subj, Term pred, Sentence asym, Sentence sym, int figure, DerivationContext nal) {
if (Statement.invalidStatement(subj, pred)) {
return;
}
int order1 = asym.term.getTemporalOrder();
int order2 = sym.term.getTemporalOrder();
int order = analogyOrder(order1, order2, figure);
if (order == ORDER_INVALID) {
return;
}
Statement st = (Statement) asym.term;
TruthValue truth = null;
BudgetValue budget;
Sentence sentence = nal.getCurrentTask().sentence;
CompoundTerm taskTerm = (CompoundTerm) sentence.term;
if (sentence.isQuestion() || sentence.isQuest()) {
if (taskTerm.isCommutative()) {
if (asym.truth == null) {
// a question for example
return;
}
budget = BudgetFunctions.backwardWeak(asym.truth, nal);
} else {
if (sym.truth == null) {
// a question for example
return;
}
budget = BudgetFunctions.backward(sym.truth, nal);
}
} else {
if (sentence.isGoal()) {
if (taskTerm.isCommutative()) {
truth = TruthFunctions.desireWeak(asym.truth, sym.truth);
} else {
truth = TruthFunctions.desireStrong(asym.truth, sym.truth);
}
} else {
truth = TruthFunctions.analogy(asym.truth, sym.truth);
}
budget = BudgetFunctions.forward(truth, nal);
}
// nal.mem().logic.ANALOGY.commit();
// (allow overlap) but not needed here, isn't detachment
nal.doublePremiseTask(Statement.make(st, subj, pred, order), truth, budget, false, false);
}
use of nars.entity.TruthValue 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.entity.TruthValue in project opennars by opennars.
the class SyllogisticRules method conditionalDedInd.
/**
* {<(&&, S1, S2, S3) ==> P>, S1} |- <(&&, S2, S3) ==> P> {<(&&, S2, S3) ==>
* P>, <S1 ==> S2>} |- <(&&, S1, S3) ==> P> {<(&&, S1, S3) ==> P>, <S1 ==>
* S2>} |- <(&&, S2, S3) ==> P>
*
* @param premise1 The conditional premise
* @param index The location of the shared term in the condition of premise1
* @param premise2 The premise which, or part of which, appears in the
* condition of premise1
* @param side The location of the shared term in premise2: 0 for subject, 1
* for predicate, -1 for the whole term
* @param nal Reference to the memory
*/
static void conditionalDedInd(Sentence premise1Sentence, Implication premise1, short index, Term premise2, int side, DerivationContext nal) {
Task task = nal.getCurrentTask();
Sentence taskSentence = task.sentence;
Sentence belief = nal.getCurrentBelief();
boolean deduction = (side != 0);
boolean conditionalTask = Variables.hasSubstitute(Symbols.VAR_INDEPENDENT, premise2, belief.term);
Term commonComponent;
Term newComponent = null;
if (side == 0) {
commonComponent = ((Statement) premise2).getSubject();
newComponent = ((Statement) premise2).getPredicate();
} else if (side == 1) {
commonComponent = ((Statement) premise2).getPredicate();
newComponent = ((Statement) premise2).getSubject();
} else {
commonComponent = premise2;
}
Term subj = premise1.getSubject();
if (!(subj instanceof Conjunction)) {
return;
}
Conjunction oldCondition = (Conjunction) subj;
int index2 = Terms.indexOf(oldCondition.term, commonComponent);
if (index2 >= 0) {
index = (short) index2;
} else {
Term[] u = new Term[] { premise1, premise2 };
boolean match = Variables.unify(Symbols.VAR_INDEPENDENT, oldCondition.term[index], commonComponent, u);
premise1 = (Implication) u[0];
premise2 = u[1];
if (!match && (commonComponent.getClass() == oldCondition.getClass())) {
CompoundTerm compoundCommonComponent = ((CompoundTerm) commonComponent);
if ((oldCondition.term.length > index) && (compoundCommonComponent.term.length > index)) {
// assumption: { was missing
u = new Term[] { premise1, premise2 };
match = Variables.unify(Symbols.VAR_INDEPENDENT, oldCondition.term[index], compoundCommonComponent.term[index], u);
premise1 = (Implication) u[0];
premise2 = u[1];
}
}
if (!match) {
return;
}
}
int conjunctionOrder = subj.getTemporalOrder();
if (conjunctionOrder == ORDER_FORWARD) {
if (index > 0) {
return;
}
if ((side == 0) && (premise2.getTemporalOrder() == ORDER_FORWARD)) {
return;
}
if ((side == 1) && (premise2.getTemporalOrder() == ORDER_BACKWARD)) {
return;
}
}
Term newCondition;
if (oldCondition.equals(commonComponent)) {
newCondition = null;
} else {
newCondition = oldCondition.setComponent(index, newComponent, nal.mem());
}
Term content;
long delta = 0;
long mintime = 0;
long maxtime = 0;
boolean predictedEvent = false;
if (newCondition != null) {
if (newCondition instanceof Interval) {
content = premise1.getPredicate();
delta = ((Interval) newCondition).time;
if (taskSentence.getOccurenceTime() != Stamp.ETERNAL) {
mintime = taskSentence.getOccurenceTime() + ((Interval) newCondition).time - 1;
maxtime = taskSentence.getOccurenceTime() + ((Interval) newCondition).time + 2;
predictedEvent = true;
}
} else {
while ((newCondition instanceof Conjunction) && (((CompoundTerm) newCondition).term[0] instanceof Interval)) {
Interval interval = (Interval) ((CompoundTerm) newCondition).term[0];
delta += interval.time;
newCondition = ((CompoundTerm) newCondition).setComponent(0, null, nal.mem());
}
content = Statement.make(premise1, newCondition, premise1.getPredicate(), premise1.getTemporalOrder());
}
} else {
content = premise1.getPredicate();
}
if (content == null)
return;
long occurrence_time = nal.getTheNewStamp().getOccurrenceTime();
if (delta != 0) {
long baseTime = taskSentence.getOccurenceTime();
if (baseTime != Stamp.ETERNAL) {
baseTime += delta;
occurrence_time = baseTime;
}
}
TruthValue truth1 = taskSentence.truth;
TruthValue truth2 = belief.truth;
TruthValue truth = null;
BudgetValue budget;
if (taskSentence.isQuestion() || taskSentence.isQuest()) {
budget = BudgetFunctions.backwardWeak(truth2, nal);
} else {
if (taskSentence.isGoal()) {
if (conditionalTask) {
truth = TruthFunctions.desireWeak(truth1, truth2);
} else if (deduction) {
truth = TruthFunctions.desireInd(truth1, truth2);
} else {
truth = TruthFunctions.desireDed(truth1, truth2);
}
} else {
if (deduction) {
truth = TruthFunctions.deduction(truth1, truth2);
} else if (conditionalTask) {
truth = TruthFunctions.induction(truth2, truth1);
} else {
truth = TruthFunctions.induction(truth1, truth2);
}
}
budget = BudgetFunctions.forward(truth, nal);
}
nal.getTheNewStamp().setOccurrenceTime(occurrence_time);
// (allow overlap) when deduction on judgment
List<Task> ret = nal.doublePremiseTask(content, truth, budget, false, taskSentence.isJudgment() && deduction);
if (!nal.evidentalOverlap && ret != null && ret.size() > 0) {
if (predictedEvent && taskSentence.isJudgment() && truth != null && truth.getExpectation() > Parameters.DEFAULT_CONFIRMATION_EXPECTATION && !premise1Sentence.stamp.alreadyAnticipatedNegConfirmation) {
premise1Sentence.stamp.alreadyAnticipatedNegConfirmation = true;
ConceptProcessing.generatePotentialNegConfirmation(nal, premise1Sentence, budget, mintime, maxtime, 1);
}
}
}
Aggregations