Search in sources :

Example 71 with Task

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);
    }
}
Also used : Task(nars.entity.Task) Narsese(nars.io.Narsese) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InvalidInputException(nars.io.Narsese.InvalidInputException)

Example 72 with Task

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);
}
Also used : Answer(nars.io.events.Events.Answer) Task(nars.entity.Task) Sentence(nars.entity.Sentence) IOException(java.io.IOException)

Example 73 with Task

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);
}
Also used : Task(nars.entity.Task) Stamp(nars.entity.Stamp) Sentence(nars.entity.Sentence)

Example 74 with Task

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);
    }
}
Also used : Concept(nars.entity.Concept) BudgetValue(nars.entity.BudgetValue) Task(nars.entity.Task) Operation(nars.operator.Operation) LinkedList(java.util.LinkedList)

Example 75 with Task

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;
}
Also used : Concept(nars.entity.Concept) Task(nars.entity.Task) Events(nars.io.events.Events) HashSet(java.util.HashSet)

Aggregations

Task (nars.entity.Task)78 Sentence (nars.entity.Sentence)54 Term (nars.language.Term)37 BudgetValue (nars.entity.BudgetValue)36 TruthValue (nars.entity.TruthValue)34 Stamp (nars.entity.Stamp)25 CompoundTerm (nars.language.CompoundTerm)21 Concept (nars.entity.Concept)18 Conjunction (nars.language.Conjunction)11 Statement (nars.language.Statement)10 Events (nars.io.events.Events)8 Inheritance (nars.language.Inheritance)8 Interval (nars.language.Interval)8 Narsese (nars.io.Narsese)6 SetExt (nars.language.SetExt)6 SetInt (nars.language.SetInt)6 ArrayList (java.util.ArrayList)5 DerivationContext (nars.control.DerivationContext)5 Implication (nars.language.Implication)5 Memory (nars.storage.Memory)5