use of nars.language.Implication 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.language.Implication 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.Implication 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;
}
use of nars.language.Implication in project opennars by opennars.
the class InternalExperience method beliefReason.
/**
* used in full internal experience mode only
*/
protected void beliefReason(Sentence belief, Term beliefTerm, Term taskTerm, DerivationContext nal) {
Memory memory = nal.memory;
if (Memory.randomNumber.nextDouble() < INTERNAL_EXPERIENCE_RARE_PROBABILITY) {
// the operators which dont have a innate belief
// also get a chance to reveal its effects to the system this way
Operator op = memory.getOperator(nonInnateBeliefOperators[Memory.randomNumber.nextInt(nonInnateBeliefOperators.length)]);
Product prod = new Product(new Term[] { belief.term });
if (op != null && prod != null) {
Term new_term = Inheritance.make(prod, op);
Sentence sentence = new Sentence(new_term, Symbols.GOAL_MARK, // a naming convension
new TruthValue(1, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(memory));
float quality = BudgetFunctions.truthToQuality(sentence.truth);
BudgetValue budget = new BudgetValue(Parameters.DEFAULT_GOAL_PRIORITY * INTERNAL_EXPERIENCE_PRIORITY_MUL, Parameters.DEFAULT_GOAL_DURABILITY * INTERNAL_EXPERIENCE_DURABILITY_MUL, quality);
Task newTask = new Task(sentence, budget, true);
nal.derivedTask(newTask, false, false, false);
}
}
if (beliefTerm instanceof Implication && Memory.randomNumber.nextDouble() <= INTERNAL_EXPERIENCE_PROBABILITY) {
Implication imp = (Implication) beliefTerm;
if (imp.getTemporalOrder() == TemporalRules.ORDER_FORWARD) {
// 1. check if its (&/,term,+i1,...,+in) =/> anticipateTerm form:
boolean valid = true;
if (imp.getSubject() instanceof Conjunction) {
Conjunction conj = (Conjunction) imp.getSubject();
if (!conj.term[0].equals(taskTerm)) {
// the expected needed term is not included
valid = false;
}
for (int i = 1; i < conj.term.length; i++) {
if (!(conj.term[i] instanceof Interval)) {
valid = false;
break;
}
}
} else {
if (!imp.getSubject().equals(taskTerm)) {
valid = false;
}
}
if (valid) {
Operator op = memory.getOperator("^anticipate");
if (op == null)
throw new RuntimeException(this + " requires ^anticipate operator");
Product args = new Product(new Term[] { imp.getPredicate() });
Term new_term = Operation.make(args, op);
Sentence sentence = new Sentence(new_term, Symbols.GOAL_MARK, // a naming convension
new TruthValue(1, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(memory));
float quality = BudgetFunctions.truthToQuality(sentence.truth);
BudgetValue budget = new BudgetValue(Parameters.DEFAULT_GOAL_PRIORITY * INTERNAL_EXPERIENCE_PRIORITY_MUL, Parameters.DEFAULT_GOAL_DURABILITY * INTERNAL_EXPERIENCE_DURABILITY_MUL, quality);
Task newTask = new Task(sentence, budget, true);
nal.derivedTask(newTask, false, false, false);
}
}
}
}
use of nars.language.Implication in project opennars by opennars.
the class CompositionalRules method composeCompound.
/* -------------------- intersections and differences -------------------- */
/**
* {<S ==> M>, <P ==> M>} |- {<(S|P) ==> M>, <(S&P) ==> M>, <(S-P) ==>
* M>,
* <(P-S) ==> M>}
*
* @param taskSentence The first premise
* @param belief The second premise
* @param index The location of the shared term
* @param nal Reference to the memory
*/
static void composeCompound(final Statement taskContent, final Statement beliefContent, final int index, final DerivationContext nal) {
if ((!nal.getCurrentTask().sentence.isJudgment()) || (taskContent.getClass() != beliefContent.getClass())) {
return;
}
final Term componentT = taskContent.term[1 - index];
final Term componentB = beliefContent.term[1 - index];
final Term componentCommon = taskContent.term[index];
int order1 = taskContent.getTemporalOrder();
int order2 = beliefContent.getTemporalOrder();
int order = TemporalRules.composeOrder(order1, order2);
if (order == TemporalRules.ORDER_INVALID) {
return;
}
if ((componentT instanceof CompoundTerm) && ((CompoundTerm) componentT).containsAllTermsOf(componentB)) {
decomposeCompound((CompoundTerm) componentT, componentB, componentCommon, index, true, order, nal);
return;
} else if ((componentB instanceof CompoundTerm) && ((CompoundTerm) componentB).containsAllTermsOf(componentT)) {
decomposeCompound((CompoundTerm) componentB, componentT, componentCommon, index, false, order, nal);
return;
}
final TruthValue truthT = nal.getCurrentTask().sentence.truth;
final TruthValue truthB = nal.getCurrentBelief().truth;
final TruthValue truthOr = union(truthT, truthB);
final TruthValue truthAnd = intersection(truthT, truthB);
TruthValue truthDif = null;
Term termOr = null;
Term termAnd = null;
Term termDif = null;
if (index == 0) {
if (taskContent instanceof Inheritance) {
termOr = IntersectionInt.make(componentT, componentB);
termAnd = IntersectionExt.make(componentT, componentB);
if (truthB.isNegative()) {
if (!truthT.isNegative()) {
termDif = DifferenceExt.make(componentT, componentB);
truthDif = intersection(truthT, negation(truthB));
}
} else if (truthT.isNegative()) {
termDif = DifferenceExt.make(componentB, componentT);
truthDif = intersection(truthB, negation(truthT));
}
} else if (taskContent instanceof Implication) {
termOr = Disjunction.make(componentT, componentB);
termAnd = Conjunction.make(componentT, componentB);
}
processComposed(taskContent, componentCommon, termOr, order, truthOr, nal);
processComposed(taskContent, componentCommon, termAnd, order, truthAnd, nal);
processComposed(taskContent, componentCommon, termDif, order, truthDif, nal);
} else {
// index == 1
if (taskContent instanceof Inheritance) {
termOr = IntersectionExt.make(componentT, componentB);
termAnd = IntersectionInt.make(componentT, componentB);
if (truthB.isNegative()) {
if (!truthT.isNegative()) {
termDif = DifferenceInt.make(componentT, componentB);
truthDif = intersection(truthT, negation(truthB));
}
} else if (truthT.isNegative()) {
termDif = DifferenceInt.make(componentB, componentT);
truthDif = intersection(truthB, negation(truthT));
}
} else if (taskContent instanceof Implication) {
termOr = Conjunction.make(componentT, componentB);
termAnd = Disjunction.make(componentT, componentB);
}
processComposed(taskContent, termOr, componentCommon, order, truthOr, nal);
processComposed(taskContent, termAnd, componentCommon, order, truthAnd, nal);
processComposed(taskContent, termDif, componentCommon, order, truthDif, nal);
}
}
Aggregations