use of nars.operator.Operation 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.operator.Operation in project opennars by opennars.
the class Memory method localInference.
public void localInference(Task task) {
DerivationContext cont = new DerivationContext(this);
cont.setCurrentTask(task);
cont.setCurrentTerm(task.getTerm());
cont.setCurrentConcept(conceptualize(task.budget, cont.getCurrentTerm()));
if (cont.getCurrentConcept() != null) {
boolean processed = ConceptProcessing.processTask(cont.getCurrentConcept(), cont, task);
if (processed) {
event.emit(Events.ConceptDirectProcessedTask.class, task);
}
}
if (!task.sentence.isEternal() && !(task.sentence.term instanceof Operation)) {
TemporalInferenceControl.eventInference(task, cont);
}
// memory.logic.TASK_IMMEDIATE_PROCESS.commit();
emit(Events.TaskImmediateProcess.class, task, cont);
}
use of nars.operator.Operation in project opennars by opennars.
the class ConceptProcessing method bestReactionForGoal.
/**
* When a goal is processed, use the best memorized reaction
* that is applicable to the current context (recent events) in case that it exists.
* This is a special case of the choice rule and allows certain behaviors to be automated.
*/
protected static void bestReactionForGoal(Concept concept, final DerivationContext nal, Sentence projectedGoal, final Task task) {
try {
Operation bestop = null;
float bestop_truthexp = 0.0f;
TruthValue bestop_truth = null;
Task executable_precond = null;
long mintime = -1;
long maxtime = -1;
for (Task t : concept.executable_preconditions) {
Term[] prec = ((Conjunction) ((Implication) t.getTerm()).getSubject()).term;
Term[] newprec = new Term[prec.length - 3];
System.arraycopy(prec, 0, newprec, 0, prec.length - 3);
long add_tolerance = (long) (((Interval) prec[prec.length - 1]).time * Parameters.ANTICIPATION_TOLERANCE);
mintime = nal.memory.time();
maxtime = nal.memory.time() + add_tolerance;
Operation op = (Operation) prec[prec.length - 2];
Term precondition = Conjunction.make(newprec, TemporalRules.ORDER_FORWARD);
Concept preconc = nal.memory.concept(precondition);
long newesttime = -1;
Task bestsofar = null;
if (preconc != null) {
// check recent events in event bag
for (Task p : concept.memory.seq_current) {
if (p.sentence.term.equals(preconc.term) && p.sentence.isJudgment() && !p.sentence.isEternal() && p.sentence.getOccurenceTime() > newesttime && p.sentence.getOccurenceTime() <= concept.memory.time()) {
newesttime = p.sentence.getOccurenceTime();
// we use the newest for now
bestsofar = p;
}
}
if (bestsofar == null) {
continue;
}
// ok now we can take the desire value:
TruthValue A = projectedGoal.getTruth();
// and the truth of the hypothesis:
TruthValue Hyp = t.sentence.truth;
// overlap will almost never happen, but to make sure
if (Stamp.baseOverlap(projectedGoal.stamp.evidentialBase, t.sentence.stamp.evidentialBase)) {
// base overlap
continue;
}
if (Stamp.baseOverlap(bestsofar.sentence.stamp.evidentialBase, t.sentence.stamp.evidentialBase)) {
// base overlap
continue;
}
if (Stamp.baseOverlap(projectedGoal.stamp.evidentialBase, bestsofar.sentence.stamp.evidentialBase)) {
// base overlap
continue;
}
// and the truth of the precondition:
Sentence projectedPrecon = bestsofar.sentence.projection(concept.memory.time(), /*- distance*/
concept.memory.time());
if (projectedPrecon.isEternal()) {
// projection wasn't better than eternalization, too long in the past
continue;
}
// debug start
// long timeA = memory.time();
// long timeOLD = bestsofar.sentence.stamp.getOccurrenceTime();
// long timeNEW = projectedPrecon.stamp.getOccurrenceTime();
// debug end
TruthValue precon = projectedPrecon.truth;
// and derive the conjunction of the left side:
TruthValue leftside = TruthFunctions.desireDed(A, Hyp);
// in order to derive the operator desire value:
TruthValue opdesire = TruthFunctions.desireDed(precon, leftside);
float expecdesire = opdesire.getExpectation();
if (expecdesire > bestop_truthexp) {
bestop = op;
bestop_truthexp = expecdesire;
bestop_truth = opdesire;
executable_precond = t;
}
}
}
if (bestop != null && bestop_truthexp > concept.memory.param.decisionThreshold.get()) /*&& Math.random() < bestop_truthexp */
{
Sentence createdSentence = new Sentence(bestop, Symbols.JUDGMENT_MARK, bestop_truth, projectedGoal.stamp);
Task t = new Task(createdSentence, new BudgetValue(1.0f, 1.0f, 1.0f), false);
// System.out.println("used " +t.getTerm().toString() + String.valueOf(memory.randomNumber.nextInt()));
if (!task.sentence.stamp.evidenceIsCyclic()) {
if (!executeDecision(nal, t)) {
// this task is just used as dummy
concept.memory.emit(Events.UnexecutableGoal.class, task, concept, nal);
} else {
concept.memory.decisionBlock = concept.memory.time() + Parameters.AUTOMATIC_DECISION_USUAL_DECISION_BLOCK_CYCLES;
generatePotentialNegConfirmation(nal, executable_precond.sentence, executable_precond.budget, mintime, maxtime, 2);
}
}
}
} catch (Exception ex) {
System.out.println("Failure in operation choice rule, analyze!");
}
}
use of nars.operator.Operation in project opennars by opennars.
the class ConceptProcessing method executeDecision.
/**
* Entry point for all potentially executable tasks.
* Returns true if the Task has a Term which can be executed
*/
public static boolean executeDecision(DerivationContext nal, final Task t) {
// if (isDesired())
if (nal.memory.allowExecution) {
Term content = t.getTerm();
if (content instanceof Operation) {
Operation op = (Operation) content;
Operator oper = op.getOperator();
Product prod = (Product) op.getSubject();
Term arg = prod.term[0];
if (oper instanceof FunctionOperator) {
for (int i = 0; i < prod.term.length - 1; i++) {
// except last one, the output arg
if (prod.term[i].hasVarDep() || prod.term[i].hasVarIndep()) {
return false;
}
}
} else {
if (content.hasVarDep() || content.hasVarIndep()) {
return false;
}
}
if (!arg.equals(Term.SELF)) {
// will be deprecated in the future
return false;
}
op.setTask(t);
if (!oper.call(op, nal.memory)) {
return false;
}
System.out.println(t.toStringLong());
// this.memory.sequenceTasks = new LevelBag<>(Parameters.SEQUENCE_BAG_LEVELS, Parameters.SEQUENCE_BAG_SIZE);
return true;
}
}
return false;
}
use of nars.operator.Operation in project opennars by opennars.
the class ConceptProcessing method processTask.
/**
* Directly process a new task. Called exactly once on each task. Using
* local information and finishing in a constant time. Provide feedback in
* the taskBudget value of the task.
* <p>
* called in Memory.immediateProcess only
*
* @param task The task to be processed
* @return whether it was processed
*/
public static boolean processTask(Concept concept, final DerivationContext nal, final Task task) {
if (task.isInput()) {
if (task.sentence.isJudgment() && !task.sentence.isEternal() && task.sentence.term instanceof Operation) {
Operation op = (Operation) task.sentence.term;
Operator o = (Operator) op.getPredicate();
// only consider these mental ops an operation to track when executed not already when generated as internal event
if (!(o instanceof Believe) && !(o instanceof Want) && !(o instanceof Wonder) && !(o instanceof Evaluate) && !(o instanceof Anticipate)) {
TemporalInferenceControl.NewOperationFrame(nal.memory, task);
}
}
concept.observable = true;
}
char type = task.sentence.punctuation;
switch(type) {
case Symbols.JUDGMENT_MARK:
// memory.logic.JUDGMENT_PROCESS.commit();
processJudgment(concept, nal, task);
break;
case Symbols.GOAL_MARK:
// memory.logic.GOAL_PROCESS.commit();
processGoal(concept, nal, task, true);
break;
case Symbols.QUESTION_MARK:
case Symbols.QUEST_MARK:
// memory.logic.QUESTION_PROCESS.commit();
processQuestion(concept, nal, task);
break;
default:
return false;
}
maintainDisappointedAnticipations(concept);
if (task.aboveThreshold()) {
// still need to be processed
// memory.logic.LINK_TO_TASK.commit();
concept.linkToTask(task, nal);
}
return true;
}
Aggregations