Search in sources :

Example 46 with Task

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

Example 47 with Task

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

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

Example 49 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 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();
}
Also used : Task(nars.entity.Task) EXE(nars.io.events.OutputHandler.EXE) IN(nars.io.events.OutputHandler.IN) Sentence(nars.entity.Sentence) OUT(nars.io.events.OutputHandler.OUT)

Example 50 with Task

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;
}
Also used : BudgetValue(nars.entity.BudgetValue) Task(nars.entity.Task) Stamp(nars.entity.Stamp) Memory(nars.storage.Memory) Inheritance(nars.language.Inheritance) Product(nars.language.Product) Term(nars.language.Term) EventObserver(nars.io.events.EventEmitter.EventObserver) Events(nars.io.events.Events) TruthValue(nars.entity.TruthValue) SetExt(nars.language.SetExt) Sentence(nars.entity.Sentence)

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