use of nars.entity.TruthValue in project opennars by opennars.
the class FunctionOperator method execute.
// abstract protected int getMinArity();
// abstract protected int getMaxArity();
@Override
protected ArrayList<Task> execute(Operation operation, Term[] args, Memory m) {
// TODO make memory access optional by constructor argument
// TODO allow access to NAR instance?
int numArgs = args.length - 1;
if (numArgs < 1) {
throw new RuntimeException("Requires at least 1 arguments");
}
if (numArgs < 2) /*&& !(this instanceof Javascript)*/
{
throw new RuntimeException("Requires at least 2 arguments");
}
// last argument a variable?
Term lastTerm = args[numArgs];
boolean variable = lastTerm instanceof Variable;
if (!variable) /*&& !(this instanceof Javascript)*/
{
throw new RuntimeException("output can not be specified");
}
int numParam = numArgs - 1;
/*if(this instanceof Javascript && !variable) {
numParam++;
}*/
Term[] x = new Term[numParam];
System.arraycopy(args, 1, x, 0, numParam);
Term y;
// try {
y = function(m, x);
if (y == null) {
return null;
}
/*if(!variable && this instanceof Javascript) {
return null;
}*/
// m.emit(SynchronousFunctionOperator.class, Arrays.toString(x) + " | " + y);
/*}
catch (Exception e) {
throw e;
}*/
Variable var = new Variable("$1");
// Term actual_part = Similarity.make(var, y);
// Variable vardep=new Variable("#1");
// Term actual_dep_part = Similarity.make(vardep, y);
operation = (Operation) operation.setComponent(0, ((CompoundTerm) operation.getSubject()).setComponent(numArgs, y, m), m);
float confidence = Parameters.DEFAULT_JUDGMENT_CONFIDENCE;
if (variable) {
Sentence s = new Sentence(operation, Symbols.JUDGMENT_MARK, new TruthValue(1.0f, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(m));
return Lists.newArrayList(new Task(s, new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY, Parameters.DEFAULT_FEEDBACK_DURABILITY, truthToQuality(s.getTruth())), true));
} else {
return null;
}
}
use of nars.entity.TruthValue in project opennars by opennars.
the class Operator method call.
/**
* The standard way to carry out an operation, which invokes the execute
* method defined for the operator, and handles feedback tasks as input
*
* @param op The operator to be executed
* @param args The arguments to be taken by the operator
* @param memory The memory on which the operation is executed
* @return true if successful, false if an error occurred
*/
public final boolean call(final Operation operation, final Term[] args, final Memory memory) {
try {
List<Task> feedback = execute(operation, args, memory);
if (feedback == null || feedback.isEmpty()) {
// null operator case
memory.executedTask(operation, new TruthValue(1f, executionConfidence));
}
reportExecution(operation, args, feedback, memory);
if (feedback != null) {
for (final Task t : feedback) {
memory.inputTask(t);
}
}
return true;
}// }
catch (Exception e) {
// reportExecution(operation, args, e, memory);
}
return false;
}
use of nars.entity.TruthValue 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.TruthValue 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.TruthValue in project opennars by opennars.
the class BudgetFunctions method update.
/**
* Update a belief
*
* @param task The task containing new belief
* @param bTruth Truth value of the previous belief
* @return Budget value of the updating task
*/
static BudgetValue update(final Task task, final TruthValue bTruth) {
final TruthValue tTruth = task.sentence.truth;
final float dif = tTruth.getExpDifAbs(bTruth);
final float priority = or(dif, task.getPriority());
final float durability = aveAri(dif, task.getDurability());
final float quality = truthToQuality(bTruth);
return new BudgetValue(priority, durability, quality);
}
Aggregations