use of nars.language.Implication in project opennars by opennars.
the class GlobalAnticipation method event.
@Override
public void event(Class event, Object[] args) {
if (event == Events.TaskDerive.class) {
Task derivedTask = (Task) args[0];
if (derivedTask.sentence.term instanceof Implication && (((Implication) derivedTask.sentence.term).getTemporalOrder() == TemporalRules.ORDER_FORWARD || ((Implication) derivedTask.sentence.term).getTemporalOrder() == TemporalRules.ORDER_CONCURRENT)) {
if (!current_tasks.contains(derivedTask)) {
current_tasks.add(derivedTask);
}
}
} else if (event == Events.ConceptBeliefRemove.class) {
// task is 3nd
Task removedTask = (Task) args[2];
if (current_tasks.contains(removedTask)) {
current_tasks.remove(removedTask);
}
} else if (event == Events.InduceSucceedingEvent.class) {
Task newEvent = (Task) args[0];
DerivationContext nal = (DerivationContext) args[1];
if (newEvent.sentence.truth != null) {
stm.add(newEvent);
while (stm.size() > MatchUpTo) {
stm.removeFirst();
}
}
temporalPredictionsAdapt(nal);
}
}
use of nars.language.Implication in project opennars by opennars.
the class GlobalAnticipation method temporalPredictionsAdapt.
// check all predictive statements, match them with last events
public void temporalPredictionsAdapt(DerivationContext nal) {
if (TEMPORAL_PREDICTION_FEEDBACK_ACCURACY_DIV == 0.0f) {
return;
}
ArrayList<Task> lastEvents = new ArrayList<Task>();
for (Task stmLast : stm) {
lastEvents.add(stmLast);
}
if (lastEvents.isEmpty()) {
return;
}
final long duration = Parameters.DURATION;
ArrayList<Task> derivetasks = new ArrayList<Task>();
for (final Task c : current_tasks) {
// a =/> b or (&/ a1...an) =/> b
boolean concurrent_conjunction = false;
Term[] args = new Term[1];
Implication imp = (Implication) c.sentence.term.clone();
boolean concurrent_implication = imp.getTemporalOrder() == TemporalRules.ORDER_CONCURRENT;
args[0] = imp.getSubject();
if (imp.getSubject() instanceof Conjunction) {
Conjunction conj = (Conjunction) imp.getSubject();
if (!conj.isSpatial) {
if (conj.temporalOrder == TemporalRules.ORDER_FORWARD || conj.temporalOrder == TemporalRules.ORDER_CONCURRENT) {
concurrent_conjunction = conj.temporalOrder == TemporalRules.ORDER_CONCURRENT;
// in case of &/ this are the terms
args = conj.term;
}
}
}
int i = 0;
boolean matched = true;
int off = 0;
long expected_time = lastEvents.get(0).sentence.getOccurenceTime();
for (i = 0; i < args.length; i++) {
// handling of intervals:
if (args[i] instanceof Interval) {
if (!concurrent_conjunction) {
expected_time += ((Interval) args[i]).time;
}
off++;
continue;
}
if (i - off >= lastEvents.size()) {
break;
}
if (!Variables.hasSubstitute(Symbols.VAR_INDEPENDENT, args[i], lastEvents.get(i - off).sentence.term)) {
// it didnt match, instead sth different unexpected happened
// whether intermediate events should be tolerated or not was a important question when considering this,
matched = false;
// if it should be allowed, the sequential match does not matter only if the events come like predicted.
break;
} else {
if (lastEvents.get(i - off).sentence.truth.getExpectation() <= 0.5f) {
// it matched according to sequence, but is its expectation bigger than 0.5? todo: decide how truth values of the expected events
// it didn't happen
matched = false;
break;
}
long occurence = lastEvents.get(i - off).sentence.getOccurenceTime();
boolean right_in_time = Math.abs(occurence - expected_time) < ((double) duration) / TEMPORAL_PREDICTION_FEEDBACK_ACCURACY_DIV;
if (!right_in_time) {
// it matched so far, but is the timing right or did it happen when not relevant anymore?
matched = false;
break;
}
}
if (!concurrent_conjunction) {
expected_time += duration;
}
}
if (concurrent_conjunction && !concurrent_implication) {
// implication is not concurrent
// so here we have to add duration
expected_time += duration;
} else if (!concurrent_conjunction && concurrent_implication) {
expected_time -= duration;
}
// ok it matched, is the consequence also right?
if (matched && lastEvents.size() > args.length - off) {
long occurence = lastEvents.get(args.length - off).sentence.getOccurenceTime();
boolean right_in_time = Math.abs(occurence - expected_time) < ((double) duration) / TEMPORAL_PREDICTION_FEEDBACK_ACCURACY_DIV;
if (right_in_time && Variables.hasSubstitute(Symbols.VAR_INDEPENDENT, imp.getPredicate(), lastEvents.get(args.length - off).sentence.term)) {
// it matched and same consequence, so positive evidence
// c.sentence.truth=TruthFunctions.revision(c.sentence.truth, new TruthValue(1.0f,Parameters.DEFAULT_JUDGMENT_CONFIDENCE));
Sentence s2 = new Sentence(c.sentence.term.clone(), Symbols.JUDGMENT_MARK, new TruthValue(1.0f, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(nal.memory));
Task t = new Task(s2, new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY, Parameters.DEFAULT_JUDGMENT_DURABILITY, s2.truth), true);
derivetasks.add(t);
} else {
// it matched and other consequence, so negative evidence
// c.sentence.truth=TruthFunctions.revision(c.sentence.truth, new TruthValue(0.0f,Parameters.DEFAULT_JUDGMENT_CONFIDENCE));
Sentence s2 = new Sentence(c.sentence.term.clone(), Symbols.JUDGMENT_MARK, new TruthValue(0.0f, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(nal.memory));
Task t = new Task(s2, new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY, Parameters.DEFAULT_JUDGMENT_DURABILITY, s2.truth), true);
derivetasks.add(t);
}
// todo use derived task with revision instead
}
}
for (Task t : derivetasks) {
if (nal.derivedTask(t, false, false, false)) {
boolean debug = true;
}
}
ArrayList<Task> toDelete = new ArrayList<Task>();
for (Task t : current_tasks) {
Concept w = nal.memory.concept(t.sentence.term);
if (w == null) {
// concept does not exist anymore, delete
toDelete.add(t);
}
}
for (Task t : toDelete) {
current_tasks.remove(t);
}
}
use of nars.language.Implication in project opennars by opennars.
the class CompositionalRules method decomposeCompound.
/**
* {<(S|P) ==> M>, <P ==> M>} |- <S ==> M>
*
* @param implication The implication term to be decomposed
* @param componentCommon The part of the implication to be removed
* @param term1 The other term in the contentInd
* @param index The location of the shared term: 0 for subject, 1 for
* predicate
* @param compoundTask Whether the implication comes from the task
* @param nal Reference to the memory
*/
private static void decomposeCompound(CompoundTerm compound, Term component, Term term1, int index, boolean compoundTask, int order, DerivationContext nal) {
if ((compound instanceof Statement) || (compound instanceof ImageExt) || (compound instanceof ImageInt)) {
return;
}
Term term2 = reduceComponents(compound, component, nal.mem());
if (term2 == null) {
return;
}
long delta = 0;
while ((term2 instanceof Conjunction) && (((CompoundTerm) term2).term[0] instanceof Interval)) {
Interval interval = (Interval) ((CompoundTerm) term2).term[0];
delta += interval.time;
term2 = ((CompoundTerm) term2).setComponent(0, null, nal.mem());
}
Task task = nal.getCurrentTask();
Sentence sentence = task.sentence;
Sentence belief = nal.getCurrentBelief();
Statement oldContent = (Statement) task.getTerm();
TruthValue v1, v2;
if (compoundTask) {
v1 = sentence.truth;
v2 = belief.truth;
} else {
v1 = belief.truth;
v2 = sentence.truth;
}
TruthValue truth = null;
Term content;
if (index == 0) {
content = Statement.make(oldContent, term1, term2, order);
if (content == null) {
return;
}
if (oldContent instanceof Inheritance) {
if (compound instanceof IntersectionExt) {
truth = reduceConjunction(v1, v2);
} else if (compound instanceof IntersectionInt) {
truth = reduceDisjunction(v1, v2);
} else if ((compound instanceof SetInt) && (component instanceof SetInt)) {
truth = reduceConjunction(v1, v2);
} else if ((compound instanceof SetExt) && (component instanceof SetExt)) {
truth = reduceDisjunction(v1, v2);
} else if (compound instanceof DifferenceExt) {
if (compound.term[0].equals(component)) {
truth = reduceDisjunction(v2, v1);
} else {
truth = reduceConjunctionNeg(v1, v2);
}
}
} else if (oldContent instanceof Implication) {
if (compound instanceof Conjunction) {
truth = reduceConjunction(v1, v2);
} else if (compound instanceof Disjunction) {
truth = reduceDisjunction(v1, v2);
}
}
} else {
content = Statement.make(oldContent, term2, term1, order);
if (content == null) {
return;
}
if (oldContent instanceof Inheritance) {
if (compound instanceof IntersectionInt) {
truth = reduceConjunction(v1, v2);
} else if (compound instanceof IntersectionExt) {
truth = reduceDisjunction(v1, v2);
} else if ((compound instanceof SetExt) && (component instanceof SetExt)) {
truth = reduceConjunction(v1, v2);
} else if ((compound instanceof SetInt) && (component instanceof SetInt)) {
truth = reduceDisjunction(v1, v2);
} else if (compound instanceof DifferenceInt) {
if (compound.term[1].equals(component)) {
truth = reduceDisjunction(v2, v1);
} else {
truth = reduceConjunctionNeg(v1, v2);
}
}
} else if (oldContent instanceof Implication) {
if (compound instanceof Disjunction) {
truth = reduceConjunction(v1, v2);
} else if (compound instanceof Conjunction) {
truth = reduceDisjunction(v1, v2);
}
}
}
if (truth != null) {
BudgetValue budget = BudgetFunctions.compoundForward(truth, content, nal);
if (delta != 0) {
long baseTime = task.sentence.getOccurenceTime();
if (baseTime != Stamp.ETERNAL) {
baseTime += delta;
nal.getTheNewStamp().setOccurrenceTime(baseTime);
}
}
// (allow overlap), a form of detachment
nal.doublePremiseTask(content, truth, budget, false, true);
}
}
use of nars.language.Implication 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.Implication 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;
}
}
}
Aggregations