use of nars.entity.Task in project opennars by opennars.
the class NAR method addInput.
public void addInput(final String text) {
Narsese narsese = new Narsese(this);
if (addMultiLineInput(text)) {
return;
}
try {
if (addCommand(text)) {
return;
}
Task t = narsese.parseTask(text.trim());
this.memory.inputTask(t);
} catch (Exception ex) {
// Logger.getLogger(NAR.class.getName()).log(Level.SEVERE, null, ex);
}
}
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 CharSequence getOutputString(Class channel, Object signal, final boolean showStamp, final NAR nar, final StringBuilder buffer) {
buffer.setLength(0);
if (signal instanceof Exception) {
Exception e = (Exception) signal;
buffer.append(e.getClass().getSimpleName()).append(": ").append(e.getMessage());
/*if (showStackTrace)*/
{
buffer.append(" ").append(Arrays.asList(e.getStackTrace()));
}
} else if (signal instanceof Task) {
Task t = (Task) signal;
Sentence s = t.sentence;
buffer.append(s.toString(nar, showStamp));
} else if (signal instanceof Sentence) {
Sentence s = (Sentence) signal;
buffer.append(s.toString(nar, showStamp));
} else if (signal instanceof Object[]) {
if (channel == Answer.class) {
Object[] o = (Object[]) signal;
Task task = (Task) o[0];
Sentence belief = (Sentence) o[1];
Sentence question = task.sentence;
Sentence answer = belief;
buffer.append(answer.toString(nar, showStamp));
} else
buffer.append(Arrays.toString((Object[]) signal));
} else {
buffer.append(signal.toString());
}
return Texts.unescape(buffer);
}
use of nars.entity.Task in project opennars by opennars.
the class DerivationContext method doublePremiseTaskRevised.
/* --------------- new task building --------------- */
/**
* Shared final operations by all double-premise rules, called from the
* rules except StructuralRules
*
* @param newContent The content of the sentence in task
* @param newTruth The truth value of the sentence in task
* @param newBudget The budget value in task
*/
public boolean doublePremiseTaskRevised(final Term newContent, final TruthValue newTruth, final BudgetValue newBudget) {
Stamp derived_stamp = getTheNewStamp().clone();
// stamp was already obsorbed
this.resetOccurrenceTime();
Sentence newSentence = new Sentence(newContent, getCurrentTask().sentence.punctuation, newTruth, derived_stamp);
Task newTask = new Task(newSentence, newBudget, getCurrentBelief());
// allows overlap since overlap was already checked on revisable( function
return derivedTask(newTask, true, false, true);
}
use of nars.entity.Task in project opennars by opennars.
the class TemporalInferenceControl method addToSequenceTasks.
public static void addToSequenceTasks(DerivationContext nal, final Task newEvent) {
// multiple versions are necessary, but we do not allow duplicates
List<Task> removals = new LinkedList<Task>();
for (Task s : nal.memory.seq_current) {
if (CompoundTerm.replaceIntervals(s.getTerm()).equals(CompoundTerm.replaceIntervals(newEvent.getTerm()))) {
// check term indices
if (s.getTerm().term_indices != null && newEvent.getTerm().term_indices != null) {
boolean differentTermIndices = false;
for (int i = 0; i < s.getTerm().term_indices.length; i++) {
if (s.getTerm().term_indices[i] != newEvent.getTerm().term_indices[i]) {
differentTermIndices = true;
}
}
if (differentTermIndices) {
continue;
}
}
removals.add(s);
break;
}
}
for (Task removal : removals) {
nal.memory.seq_current.take(removal);
}
// making sure we do not mess with budget of the task:
if (!(newEvent.sentence.getTerm() instanceof Operation)) {
Concept c = nal.memory.concept(newEvent.getTerm());
float event_quality = BudgetFunctions.truthToQuality(newEvent.sentence.truth);
float event_priority = event_quality;
if (c != null) {
event_priority = Math.max(event_quality, c.getPriority());
}
Task t2 = new Task(newEvent.sentence, new BudgetValue(event_priority, 1.0f / (float) newEvent.sentence.term.getComplexity(), event_quality), newEvent.getParentBelief(), newEvent.getBestSolution());
nal.memory.seq_current.putIn(t2);
}
}
use of nars.entity.Task in project opennars by opennars.
the class TemporalInferenceControl method eventInference.
public static boolean eventInference(final Task newEvent, DerivationContext nal) {
if (newEvent.getTerm() == null || newEvent.budget == null || !newEvent.isElemOfSequenceBuffer()) {
// todo refine, add directbool in task
return false;
}
nal.emit(Events.InduceSucceedingEvent.class, newEvent, nal);
if (!newEvent.sentence.isJudgment() || newEvent.sentence.isEternal() || !newEvent.isInput()) {
return false;
}
HashSet<Task> already_attempted = new HashSet<Task>();
HashSet<Task> already_attempted_ops = new HashSet<Task>();
// Sequence formation:
for (int i = 0; i < Parameters.SEQUENCE_BAG_ATTEMPTS; i++) {
Task takeout = nal.memory.seq_current.takeNext();
if (takeout == null) {
// there were no elements in the bag to try
break;
}
if (already_attempted.contains(takeout) || Stamp.baseOverlap(newEvent.sentence.stamp.evidentialBase, takeout.sentence.stamp.evidentialBase)) {
nal.memory.seq_current.putBack(takeout, nal.memory.cycles(nal.memory.param.eventForgetDurations), nal.memory);
continue;
}
already_attempted.add(takeout);
try {
proceedWithTemporalInduction(newEvent.sentence, takeout.sentence, newEvent, nal, true, true, true);
} catch (Exception ex) {
if (Parameters.DEBUG) {
System.out.println("issue in temporal induction");
}
}
nal.memory.seq_current.putBack(takeout, nal.memory.cycles(nal.memory.param.eventForgetDurations), nal.memory);
}
// Conditioning:
if (nal.memory.lastDecision != null && newEvent != nal.memory.lastDecision) {
already_attempted_ops.clear();
for (int k = 0; k < Parameters.OPERATION_SAMPLES; k++) {
// todo move into k loop
already_attempted.clear();
Task Toperation = k == 0 ? nal.memory.lastDecision : nal.memory.recent_operations.takeNext();
if (Toperation == null) {
// there were no elements in the bag to try
break;
}
if (already_attempted_ops.contains(Toperation)) {
// put opc back into bag
// (k>0 holds here):
nal.memory.recent_operations.putBack(Toperation, nal.memory.cycles(nal.memory.param.eventForgetDurations), nal.memory);
continue;
}
already_attempted_ops.add(Toperation);
Concept opc = nal.memory.concept(Toperation.getTerm());
if (opc != null) {
if (opc.seq_before == null) {
opc.seq_before = new LevelBag<>(Parameters.SEQUENCE_BAG_LEVELS, Parameters.SEQUENCE_BAG_SIZE);
}
for (int i = 0; i < Parameters.CONDITION_BAG_ATTEMPTS; i++) {
Task takeout = opc.seq_before.takeNext();
if (takeout == null) {
// there were no elements in the bag to try
break;
}
if (already_attempted.contains(takeout)) {
opc.seq_before.putBack(takeout, nal.memory.cycles(nal.memory.param.eventForgetDurations), nal.memory);
continue;
}
already_attempted.add(takeout);
try {
long x = Toperation.sentence.getOccurenceTime();
long y = takeout.sentence.getOccurenceTime();
if (y > x) {
// something wrong here?
System.out.println("analyze case in TemporalInferenceControl!");
continue;
}
List<Task> seq_op = proceedWithTemporalInduction(Toperation.sentence, takeout.sentence, nal.memory.lastDecision, nal, true, false, true);
for (Task t : seq_op) {
if (!t.sentence.isEternal()) {
// TODO do not return the eternal here probably..;
// only =/> </> ..
List<Task> res = proceedWithTemporalInduction(newEvent.sentence, t.sentence, newEvent, nal, true, true, false);
/*DEBUG: for(Task seq_op_cons : res) {
System.out.println(seq_op_cons.toString());
}*/
}
}
} catch (Exception ex) {
if (Parameters.DEBUG) {
System.out.println("issue in temporal induction");
}
}
opc.seq_before.putBack(takeout, nal.memory.cycles(nal.memory.param.eventForgetDurations), nal.memory);
}
}
// put Toperation back into bag if it was taken out
if (k > 0) {
nal.memory.recent_operations.putBack(Toperation, nal.memory.cycles(nal.memory.param.eventForgetDurations), nal.memory);
}
}
}
addToSequenceTasks(nal, newEvent);
return true;
}
Aggregations