Search in sources :

Example 1 with EventObserver

use of nars.io.events.EventEmitter.EventObserver 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 EventObserver

use of nars.io.events.EventEmitter.EventObserver in project opennars by opennars.

the class ComplexEmotions method setEnabled.

@Override
public boolean setEnabled(NAR n, boolean enabled) {
    if (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.InduceSucceedingEvent.class)
                        return;
                    Task future_task = (Task) a[0];
                    if (future_task.sentence.getOccurenceTime() > n.time()) {
                        Concept c = n.memory.concept(future_task.getTerm());
                        float true_expectation = 0.5f;
                        float false_expectation = 0.5f;
                        if (c != null) {
                            if (c.desires.size() > 0 && c.beliefs.size() > 0) {
                                // Fear:
                                if (future_task.sentence.truth.getExpectation() > true_expectation && c.desires.get(0).sentence.truth.getExpectation() < false_expectation) {
                                    // n.addInput("<(*,{SELF},fear) --> ^feel>. :|:");
                                    float weight = future_task.getPriority();
                                    float fear = solutionQuality(true, c.desires.get(0), future_task.sentence, memory);
                                    float newValue = fear * weight;
                                    fear += newValue * weight;
                                    fear /= 1.0f + weight;
                                    // incrase concept priority by fear value:
                                    Concept C1 = memory.concept(future_task.getTerm());
                                    if (C1 != null) {
                                        C1.incPriority(fear);
                                    }
                                    memory.emit(Answer.class, "Fear value=" + fear);
                                    System.out.println("Fear value=" + fear);
                                }
                            }
                        }
                    }
                }
            };
        }
        memory.event.set(obs, enabled, Events.InduceSucceedingEvent.class, Events.TaskDerive.class);
    }
    return true;
}
Also used : EventObserver(nars.io.events.EventEmitter.EventObserver) Concept(nars.entity.Concept) Answer(nars.io.events.Events.Answer) Task(nars.entity.Task) Events(nars.io.events.Events) Memory(nars.storage.Memory)

Example 3 with EventObserver

use of nars.io.events.EventEmitter.EventObserver 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)3 EventObserver (nars.io.events.EventEmitter.EventObserver)3 Memory (nars.storage.Memory)3 Events (nars.io.events.Events)2 BudgetValue (nars.entity.BudgetValue)1 Concept (nars.entity.Concept)1 Sentence (nars.entity.Sentence)1 Stamp (nars.entity.Stamp)1 TruthValue (nars.entity.TruthValue)1 Answer (nars.io.events.Events.Answer)1 TaskDerive (nars.io.events.Events.TaskDerive)1 Inheritance (nars.language.Inheritance)1 Product (nars.language.Product)1 SetExt (nars.language.SetExt)1 Term (nars.language.Term)1 Operation (nars.operator.Operation)1 Operator (nars.operator.Operator)1