use of nars.entity.Task 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.Task 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.Task in project opennars by opennars.
the class AnswerHandler method event.
@Override
public void event(Class event, Object[] args) {
if (event == Answer.class) {
Task task = (Task) args[0];
Sentence belief = (Sentence) args[1];
if (task.equals(question)) {
onSolution(belief);
}
}
}
use of nars.entity.Task in project opennars by opennars.
the class TextOutputHandler method getOutputString.
/**
* generates a human-readable string from an output channel and signal
*/
public static String getOutputString(final Class channel, Object signal, final boolean showChannel, final boolean showStamp, final NAR nar, final StringBuilder buffer, float minPriority) {
buffer.setLength(0);
if (showChannel)
buffer.append(channel.getSimpleName()).append(": ");
if (channel == ERR.class) {
if (signal instanceof Throwable) {
Throwable e = (Throwable) signal;
buffer.append(e.toString());
/*if (showStackTrace)*/
{
// buffer.append(" ").append(Arrays.asList(e.getStackTrace()));
}
} else {
buffer.append(signal.toString());
}
} else if ((channel == OUT.class) || (channel == IN.class) || (channel == ECHO.class) || (channel == EXE.class) || (channel == Answer.class) || (channel == ANTICIPATE.class) || (channel == DISAPPOINT.class) || (channel == CONFIRM.class)) {
if (channel == CONFIRM.class) {
buffer.append(signal.toString());
}
if (signal instanceof Task) {
Task t = (Task) signal;
if (t.getPriority() < minPriority)
return null;
if ((channel == ANTICIPATE.class) || (channel == DISAPPOINT.class)) {
buffer.append(t.sentence.toString(nar, showStamp));
} else if (channel == Answer.class) {
// server / NARRun
Task task = t;
Sentence answer = task.getBestSolution();
if (answer != null)
buffer.append(answer.toString(nar, showStamp));
else
buffer.append(t.sentence.toString(nar, showStamp));
} else
buffer.append(t.sentence.toString(nar, showStamp));
} else {
buffer.append(signal.toString());
}
} else {
buffer.append(signal.toString());
}
return Texts.unescape(buffer).toString();
}
use of nars.entity.Task 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