use of nars.storage.Memory in project opennars by opennars.
the class Abbreviation method setEnabled.
@Override
public boolean setEnabled(final NAR n, final boolean enabled) {
final Memory memory = n.memory;
Operator _abbreviate = memory.getOperator("^abbreviate");
if (_abbreviate == null) {
_abbreviate = memory.addOperator(new Abbreviate());
}
final Operator abbreviate = _abbreviate;
if (obs == null) {
obs = new EventObserver() {
@Override
public void event(Class event, Object[] a) {
if (event != TaskDerive.class)
return;
if ((abbreviationProbability < 1.0) && (Memory.randomNumber.nextDouble() > abbreviationProbability))
return;
Task task = (Task) a[0];
// is it complex and also important? then give it a name:
if (canAbbreviate(task)) {
Operation operation = Operation.make(abbreviate, termArray(task.sentence.term), false);
operation.setTask(task);
abbreviate.call(operation, memory);
}
}
};
}
memory.event.set(obs, enabled, TaskDerive.class);
return true;
}
use of nars.storage.Memory in project opennars by opennars.
the class ComplexEmotions method setEnabled.
@Override
public boolean setEnabled(NAR n, boolean enabled) {
if (enabled) {
Memory memory = n.memory;
if (obs == null) {
obs = new EventObserver() {
@Override
public void event(Class event, Object[] a) {
if (event != Events.TaskDerive.class && event != Events.InduceSucceedingEvent.class)
return;
Task future_task = (Task) a[0];
if (future_task.sentence.getOccurenceTime() > n.time()) {
Concept c = n.memory.concept(future_task.getTerm());
float true_expectation = 0.5f;
float false_expectation = 0.5f;
if (c != null) {
if (c.desires.size() > 0 && c.beliefs.size() > 0) {
// Fear:
if (future_task.sentence.truth.getExpectation() > true_expectation && c.desires.get(0).sentence.truth.getExpectation() < false_expectation) {
// n.addInput("<(*,{SELF},fear) --> ^feel>. :|:");
float weight = future_task.getPriority();
float fear = solutionQuality(true, c.desires.get(0), future_task.sentence, memory);
float newValue = fear * weight;
fear += newValue * weight;
fear /= 1.0f + weight;
// incrase concept priority by fear value:
Concept C1 = memory.concept(future_task.getTerm());
if (C1 != null) {
C1.incPriority(fear);
}
memory.emit(Answer.class, "Fear value=" + fear);
System.out.println("Fear value=" + fear);
}
}
}
}
}
};
}
memory.event.set(obs, enabled, Events.InduceSucceedingEvent.class, Events.TaskDerive.class);
}
return true;
}
use of nars.storage.Memory 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.storage.Memory in project opennars by opennars.
the class Counting method setEnabled.
@Override
public boolean setEnabled(NAR n, boolean enabled) {
Memory memory = n.memory;
if (obs == null) {
obs = new EventObserver() {
@Override
public void event(Class event, Object[] a) {
if ((event != Events.TaskDerive.class && event != Events.TaskAdd.class))
return;
Task task = (Task) a[0];
if (task.getPriority() < InternalExperience.MINIMUM_PRIORITY_TO_CREATE_WANT_BELIEVE_ETC) {
return;
}
if (task.sentence.punctuation == Symbols.JUDGMENT_MARK) {
// lets say we have <{...} --> M>.
if (task.sentence.term instanceof Inheritance) {
Inheritance inh = (Inheritance) task.sentence.term;
if (inh.getSubject() instanceof SetExt) {
SetExt set_term = (SetExt) inh.getSubject();
// this gets the cardinality of M
int cardinality = set_term.size();
// now create term <(*,M,cardinality) --> CARDINALITY>.
Term[] product_args = new Term[] { inh.getPredicate(), Term.get(cardinality) };
// TODO CARDINATLITY can be a static final instance shared by all
Term new_term = Inheritance.make(new Product(product_args), /* --> */
CARDINALITY);
if (new_term == null) {
// this usually happens when product_args contains the term CARDINALITY in which case it is an invalid Inheritance statement
return;
}
TruthValue truth = task.sentence.truth.clone();
Stamp stampi = task.sentence.stamp.clone();
Sentence j = new Sentence(new_term, Symbols.JUDGMENT_MARK, truth, stampi);
BudgetValue budg = task.budget.clone();
Task newTask = new Task(j, budg, true);
memory.addNewTask(newTask, "Derived (Cardinality)");
}
}
}
}
};
}
memory.event.set(obs, enabled, Events.TaskDerive.class);
return true;
}
use of nars.storage.Memory 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);
}
}
}
}
Aggregations