Search in sources :

Example 1 with Operator

use of nars.operator.Operator in project opennars by opennars.

the class Abbreviation method setEnabled.

@Override
public boolean setEnabled(final NAR n, final boolean enabled) {
    final Memory memory = n.memory;
    Operator _abbreviate = memory.getOperator("^abbreviate");
    if (_abbreviate == null) {
        _abbreviate = memory.addOperator(new Abbreviate());
    }
    final Operator abbreviate = _abbreviate;
    if (obs == null) {
        obs = new EventObserver() {

            @Override
            public void event(Class event, Object[] a) {
                if (event != TaskDerive.class)
                    return;
                if ((abbreviationProbability < 1.0) && (Memory.randomNumber.nextDouble() > abbreviationProbability))
                    return;
                Task task = (Task) a[0];
                // is it complex and also important? then give it a name:
                if (canAbbreviate(task)) {
                    Operation operation = Operation.make(abbreviate, termArray(task.sentence.term), false);
                    operation.setTask(task);
                    abbreviate.call(operation, memory);
                }
            }
        };
    }
    memory.event.set(obs, enabled, TaskDerive.class);
    return true;
}
Also used : Operator(nars.operator.Operator) EventObserver(nars.io.events.EventEmitter.EventObserver) Task(nars.entity.Task) Memory(nars.storage.Memory) TaskDerive(nars.io.events.Events.TaskDerive) Operation(nars.operator.Operation)

Example 2 with Operator

use of nars.operator.Operator in project opennars by opennars.

the class NAR method LoadFromFile.

public static NAR LoadFromFile(String name) throws FileNotFoundException, IOException, ClassNotFoundException {
    FileInputStream inStream = new FileInputStream(name);
    ObjectInputStream stream = new ObjectInputStream(inStream);
    NAR ret = (NAR) stream.readObject();
    ret.memory.event = new EventEmitter();
    ret.plugins = new ArrayList<>();
    for (Operator o : Operators.get(ret)) ret.memory.addOperator(o);
    new Plugins().init(ret);
    return ret;
}
Also used : Operator(nars.operator.Operator) EventEmitter(nars.io.events.EventEmitter) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 3 with Operator

use of nars.operator.Operator in project opennars by opennars.

the class InternalExperience method beliefReason.

/**
 * used in full internal experience mode only
 */
protected void beliefReason(Sentence belief, Term beliefTerm, Term taskTerm, DerivationContext nal) {
    Memory memory = nal.memory;
    if (Memory.randomNumber.nextDouble() < INTERNAL_EXPERIENCE_RARE_PROBABILITY) {
        // the operators which dont have a innate belief
        // also get a chance to reveal its effects to the system this way
        Operator op = memory.getOperator(nonInnateBeliefOperators[Memory.randomNumber.nextInt(nonInnateBeliefOperators.length)]);
        Product prod = new Product(new Term[] { belief.term });
        if (op != null && prod != null) {
            Term new_term = Inheritance.make(prod, op);
            Sentence sentence = new Sentence(new_term, Symbols.GOAL_MARK, // a naming convension
            new TruthValue(1, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(memory));
            float quality = BudgetFunctions.truthToQuality(sentence.truth);
            BudgetValue budget = new BudgetValue(Parameters.DEFAULT_GOAL_PRIORITY * INTERNAL_EXPERIENCE_PRIORITY_MUL, Parameters.DEFAULT_GOAL_DURABILITY * INTERNAL_EXPERIENCE_DURABILITY_MUL, quality);
            Task newTask = new Task(sentence, budget, true);
            nal.derivedTask(newTask, false, false, false);
        }
    }
    if (beliefTerm instanceof Implication && Memory.randomNumber.nextDouble() <= INTERNAL_EXPERIENCE_PROBABILITY) {
        Implication imp = (Implication) beliefTerm;
        if (imp.getTemporalOrder() == TemporalRules.ORDER_FORWARD) {
            // 1. check if its (&/,term,+i1,...,+in) =/> anticipateTerm form:
            boolean valid = true;
            if (imp.getSubject() instanceof Conjunction) {
                Conjunction conj = (Conjunction) imp.getSubject();
                if (!conj.term[0].equals(taskTerm)) {
                    // the expected needed term is not included
                    valid = false;
                }
                for (int i = 1; i < conj.term.length; i++) {
                    if (!(conj.term[i] instanceof Interval)) {
                        valid = false;
                        break;
                    }
                }
            } else {
                if (!imp.getSubject().equals(taskTerm)) {
                    valid = false;
                }
            }
            if (valid) {
                Operator op = memory.getOperator("^anticipate");
                if (op == null)
                    throw new RuntimeException(this + " requires ^anticipate operator");
                Product args = new Product(new Term[] { imp.getPredicate() });
                Term new_term = Operation.make(args, op);
                Sentence sentence = new Sentence(new_term, Symbols.GOAL_MARK, // a naming convension
                new TruthValue(1, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(memory));
                float quality = BudgetFunctions.truthToQuality(sentence.truth);
                BudgetValue budget = new BudgetValue(Parameters.DEFAULT_GOAL_PRIORITY * INTERNAL_EXPERIENCE_PRIORITY_MUL, Parameters.DEFAULT_GOAL_DURABILITY * INTERNAL_EXPERIENCE_DURABILITY_MUL, quality);
                Task newTask = new Task(sentence, budget, true);
                nal.derivedTask(newTask, false, false, false);
            }
        }
    }
}
Also used : Operator(nars.operator.Operator) BudgetValue(nars.entity.BudgetValue) Task(nars.entity.Task) Stamp(nars.entity.Stamp) Memory(nars.storage.Memory) Product(nars.language.Product) Term(nars.language.Term) Implication(nars.language.Implication) TruthValue(nars.entity.TruthValue) Conjunction(nars.language.Conjunction) Sentence(nars.entity.Sentence) Interval(nars.language.Interval)

Example 4 with Operator

use of nars.operator.Operator in project opennars by opennars.

the class ConceptProcessing method executeDecision.

/**
 * Entry point for all potentially executable tasks.
 * Returns true if the Task has a Term which can be executed
 */
public static boolean executeDecision(DerivationContext nal, final Task t) {
    // if (isDesired())
    if (nal.memory.allowExecution) {
        Term content = t.getTerm();
        if (content instanceof Operation) {
            Operation op = (Operation) content;
            Operator oper = op.getOperator();
            Product prod = (Product) op.getSubject();
            Term arg = prod.term[0];
            if (oper instanceof FunctionOperator) {
                for (int i = 0; i < prod.term.length - 1; i++) {
                    // except last one, the output arg
                    if (prod.term[i].hasVarDep() || prod.term[i].hasVarIndep()) {
                        return false;
                    }
                }
            } else {
                if (content.hasVarDep() || content.hasVarIndep()) {
                    return false;
                }
            }
            if (!arg.equals(Term.SELF)) {
                // will be deprecated in the future
                return false;
            }
            op.setTask(t);
            if (!oper.call(op, nal.memory)) {
                return false;
            }
            System.out.println(t.toStringLong());
            // this.memory.sequenceTasks = new LevelBag<>(Parameters.SEQUENCE_BAG_LEVELS, Parameters.SEQUENCE_BAG_SIZE);
            return true;
        }
    }
    return false;
}
Also used : Operator(nars.operator.Operator) FunctionOperator(nars.operator.FunctionOperator) Operation(nars.operator.Operation) FunctionOperator(nars.operator.FunctionOperator)

Example 5 with Operator

use of nars.operator.Operator in project opennars by opennars.

the class ConceptProcessing method processTask.

/**
 * Directly process a new task. Called exactly once on each task. Using
 * local information and finishing in a constant time. Provide feedback in
 * the taskBudget value of the task.
 * <p>
 * called in Memory.immediateProcess only
 *
 * @param task The task to be processed
 * @return whether it was processed
 */
public static boolean processTask(Concept concept, final DerivationContext nal, final Task task) {
    if (task.isInput()) {
        if (task.sentence.isJudgment() && !task.sentence.isEternal() && task.sentence.term instanceof Operation) {
            Operation op = (Operation) task.sentence.term;
            Operator o = (Operator) op.getPredicate();
            // only consider these mental ops an operation to track when executed not already when generated as internal event
            if (!(o instanceof Believe) && !(o instanceof Want) && !(o instanceof Wonder) && !(o instanceof Evaluate) && !(o instanceof Anticipate)) {
                TemporalInferenceControl.NewOperationFrame(nal.memory, task);
            }
        }
        concept.observable = true;
    }
    char type = task.sentence.punctuation;
    switch(type) {
        case Symbols.JUDGMENT_MARK:
            // memory.logic.JUDGMENT_PROCESS.commit();
            processJudgment(concept, nal, task);
            break;
        case Symbols.GOAL_MARK:
            // memory.logic.GOAL_PROCESS.commit();
            processGoal(concept, nal, task, true);
            break;
        case Symbols.QUESTION_MARK:
        case Symbols.QUEST_MARK:
            // memory.logic.QUESTION_PROCESS.commit();
            processQuestion(concept, nal, task);
            break;
        default:
            return false;
    }
    maintainDisappointedAnticipations(concept);
    if (task.aboveThreshold()) {
        // still need to be processed
        // memory.logic.LINK_TO_TASK.commit();
        concept.linkToTask(task, nal);
    }
    return true;
}
Also used : Operator(nars.operator.Operator) FunctionOperator(nars.operator.FunctionOperator) Believe(nars.operator.mental.Believe) Anticipate(nars.operator.mental.Anticipate) Evaluate(nars.operator.mental.Evaluate) Want(nars.operator.mental.Want) Operation(nars.operator.Operation) Wonder(nars.operator.mental.Wonder)

Aggregations

Operator (nars.operator.Operator)10 Operation (nars.operator.Operation)4 NativeOperator (nars.io.Symbols.NativeOperator)3 Symbols.getOperator (nars.io.Symbols.getOperator)3 Task (nars.entity.Task)2 Term (nars.language.Term)2 FunctionOperator (nars.operator.FunctionOperator)2 Memory (nars.storage.Memory)2 FileInputStream (java.io.FileInputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ArrayList (java.util.ArrayList)1 BudgetValue (nars.entity.BudgetValue)1 Sentence (nars.entity.Sentence)1 Stamp (nars.entity.Stamp)1 TruthValue (nars.entity.TruthValue)1 EventEmitter (nars.io.events.EventEmitter)1 EventObserver (nars.io.events.EventEmitter.EventObserver)1 Events (nars.io.events.Events)1 TaskDerive (nars.io.events.Events.TaskDerive)1 Conjunction (nars.language.Conjunction)1