use of nars.language.Statement 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.language.Statement 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.Statement 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);
}
}
}
use of nars.language.Statement in project opennars by opennars.
the class SyllogisticRules method conditionalAna.
/**
* {<(&&, S1, S2, S3) <=> P>, S1} |- <(&&, S2, S3) <=> P> {<(&&, S2, S3) <=> P>,
* <S1 ==> S2>} |- <(&&, S1, S3) <=> P> {<(&&, S1, S3) <=> P>, <S1 ==>
*
* @param premise1 The equivalence 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 conditionalAna(Equivalence premise1, short index, Term premise2, int side, DerivationContext nal) {
Task task = nal.getCurrentTask();
Sentence taskSentence = task.sentence;
Sentence belief = nal.getCurrentBelief();
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 tm = premise1.getSubject();
if (!(tm instanceof Conjunction)) {
return;
}
Conjunction oldCondition = (Conjunction) tm;
Term[] u = new Term[] { premise1, premise2 };
boolean match = Variables.unify(Symbols.VAR_DEPENDENT, oldCondition.term[index], commonComponent, u);
premise1 = (Equivalence) u[0];
premise2 = u[1];
if (!match && (commonComponent.getClass() == oldCondition.getClass())) {
u = new Term[] { premise1, premise2 };
match = Variables.unify(Symbols.VAR_DEPENDENT, oldCondition.term[index], ((CompoundTerm) commonComponent).term[index], u);
premise1 = (Equivalence) u[0];
premise2 = u[1];
}
if (!match) {
return;
}
int conjunctionOrder = oldCondition.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;
if (newCondition != null) {
content = Statement.make(premise1, newCondition, premise1.getPredicate(), premise1.getTemporalOrder());
} else {
content = premise1.getPredicate();
}
if (content == null)
return;
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 {
truth = TruthFunctions.desireDed(truth1, truth2);
}
} else {
if (conditionalTask) {
truth = TruthFunctions.comparison(truth1, truth2);
} else {
truth = TruthFunctions.analogy(truth1, truth2);
}
}
budget = BudgetFunctions.forward(truth, nal);
}
// (allow overlap) when !conditionalTask on judgment
nal.doublePremiseTask(content, truth, budget, false, taskSentence.isJudgment() && !conditionalTask);
}
use of nars.language.Statement 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);
}
}
Aggregations