use of nars.entity.Stamp in project opennars by opennars.
the class Anticipate method anticipationFeedback.
public void anticipationFeedback(Term content, Task t, Memory memory) {
if (anticipationOperator) {
Operation op = (Operation) Operation.make(Product.make(Term.SELF, content), this);
TruthValue truth = new TruthValue(1.0f, Parameters.DEFAULT_JUDGMENT_CONFIDENCE);
Stamp st;
if (t == null) {
st = new Stamp(memory);
} else {
st = t.sentence.stamp.clone();
st.setOccurrenceTime(memory.time());
}
Sentence s = new Sentence(op, Symbols.JUDGMENT_MARK, truth, st);
Task newTask = new Task(s, new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY * InternalExperience.INTERNAL_EXPERIENCE_PRIORITY_MUL, Parameters.DEFAULT_JUDGMENT_DURABILITY * InternalExperience.INTERNAL_EXPERIENCE_DURABILITY_MUL, BudgetFunctions.truthToQuality(truth)), true);
memory.addNewTask(newTask, "Perceived (Internal Experience: Anticipation)");
}
}
use of nars.entity.Stamp in project opennars by opennars.
the class NAR method ask.
public NAR ask(String termString, AnswerHandler answered) throws InvalidInputException {
Task t;
addInput(t = new Task(new Sentence(new Narsese(this).parseTerm(termString), Symbols.QUESTION_MARK, null, new Stamp(memory, Tense.Eternal)), new BudgetValue(Parameters.DEFAULT_QUESTION_PRIORITY, Parameters.DEFAULT_QUESTION_DURABILITY, 1), true));
if (answered != null) {
answered.start(t, this);
}
return this;
}
use of nars.entity.Stamp in project opennars by opennars.
the class Narsese method parseTask.
/**
* Enter a new Task in String into the memory, called from InputWindow or
* locally.
*
* @param s the single-line addInput String
* @param memory Reference to the memory
* @param time The current time
* @return An experienced task
*/
public Task parseTask(String s) throws InvalidInputException {
StringBuilder buffer = new StringBuilder(Texts.escape(s));
String budgetString = getBudgetString(buffer);
String truthString = getTruthString(buffer);
Tense tense = parseTense(buffer);
String str = buffer.toString().trim();
int last = str.length() - 1;
char punc = str.charAt(last);
Stamp stamp = new Stamp(-1, /* if -1, will be set right before the Task is input */
tense, memory.newStampSerial(), Parameters.DURATION);
TruthValue truth = parseTruth(truthString, punc);
Term content = parseTerm(str.substring(0, last));
if (content == null)
throw new InvalidInputException("Content term missing");
Sentence sentence = new Sentence(content, punc, truth, stamp);
// if ((content instanceof Conjunction) && Variable.containVarDep(content.getName())) {
// sentence.setRevisible(false);
// }
BudgetValue budget = parseBudget(budgetString, punc, truth);
Task task = new Task(sentence, budget, true);
return task;
}
use of nars.entity.Stamp 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.entity.Stamp 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;
}
Aggregations