Search in sources :

Example 1 with Operation

use of nars.operator.Operation 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 Operation

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

the class InternalExperience method InternalExperienceFromTaskInternal.

public static boolean InternalExperienceFromTaskInternal(Memory memory, Task task, boolean full) {
    if (!enabled) {
        return false;
    }
    // if(OLD_BELIEVE_WANT_EVALUATE_WONDER_STRATEGY ||
    // (!OLD_BELIEVE_WANT_EVALUATE_WONDER_STRATEGY && (task.sentence.punctuation==Symbols.QUESTION_MARK || task.sentence.punctuation==Symbols.QUEST_MARK))) {
    {
        if (task.sentence.punctuation == Symbols.QUESTION_MARK || task.sentence.punctuation == Symbols.QUEST_MARK) {
            if (task.getPriority() < MINIMUM_PRIORITY_TO_CREATE_WONDER_EVALUATE) {
                return false;
            }
        } else if (task.getPriority() < MINIMUM_PRIORITY_TO_CREATE_WANT_BELIEVE_ETC) {
            return false;
        }
    }
    Term content = task.getTerm();
    // to prevent infinite recursions
    if (content instanceof Operation) /* ||  Memory.randomNumber.nextDouble()>Parameters.INTERNAL_EXPERIENCE_PROBABILITY*/
    {
        return true;
    }
    Sentence sentence = task.sentence;
    TruthValue truth = new TruthValue(1.0f, Parameters.DEFAULT_JUDGMENT_CONFIDENCE);
    Stamp stamp = task.sentence.stamp.clone();
    stamp.setOccurrenceTime(memory.time());
    Term ret = toTerm(sentence, memory);
    if (ret == null) {
        return true;
    }
    Sentence j = new Sentence(ret, Symbols.JUDGMENT_MARK, truth, stamp);
    BudgetValue newbudget = new BudgetValue(Parameters.DEFAULT_JUDGMENT_CONFIDENCE * INTERNAL_EXPERIENCE_PRIORITY_MUL, Parameters.DEFAULT_JUDGMENT_PRIORITY * INTERNAL_EXPERIENCE_DURABILITY_MUL, BudgetFunctions.truthToQuality(truth));
    if (!OLD_BELIEVE_WANT_EVALUATE_WONDER_STRATEGY) {
        newbudget.setPriority(task.getPriority() * INTERNAL_EXPERIENCE_PRIORITY_MUL);
        newbudget.setDurability(task.getDurability() * INTERNAL_EXPERIENCE_DURABILITY_MUL);
    }
    Task newTask = new Task(j, (BudgetValue) newbudget, true);
    memory.addNewTask(newTask, "Reflected mental operation (Internal Experience)");
    return false;
}
Also used : BudgetValue(nars.entity.BudgetValue) Task(nars.entity.Task) Stamp(nars.entity.Stamp) TruthValue(nars.entity.TruthValue) Term(nars.language.Term) Operation(nars.operator.Operation) Sentence(nars.entity.Sentence)

Example 3 with Operation

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

the class VisualSpace method ConstructSpace.

@Override
public ImaginationSpace ConstructSpace(Conjunction program) {
    if (program.isSpatial || program.getTemporalOrder() != TemporalRules.ORDER_FORWARD) {
        // would be a strange program :)
        return null;
    }
    Term beginning = program.term[0];
    if (beginning.imagination == null) {
        return null;
    }
    ImaginationSpace cur = beginning.imagination;
    // "execute program":
    for (int i = 1; i < program.term.length; i += 1) {
        if (!(program.term[i] instanceof Operation)) {
            return null;
        }
        Operation oper = (Operation) program.term[i];
        if (!IsOperationInSpace(oper)) {
            return null;
        }
        cur = cur.ProgressSpace(oper, cur);
        i++;
    }
    return null;
}
Also used : ImaginationSpace(nars.operator.ImaginationSpace) Term(nars.language.Term) Operation(nars.operator.Operation)

Example 4 with Operation

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

the class TemporalRules method temporalInduction.

// TODO maybe split &/ case into own function
public static List<Task> temporalInduction(final Sentence s1, final Sentence s2, final nars.control.DerivationContext nal, boolean SucceedingEventsInduction, boolean addToMemory, boolean allowSequence) {
    if ((s1.truth == null) || (s2.truth == null) || s1.punctuation != Symbols.JUDGMENT_MARK || s2.punctuation != Symbols.JUDGMENT_MARK || s1.isEternal() || s2.isEternal())
        return Collections.EMPTY_LIST;
    Term t1 = s1.term;
    Term t2 = s2.term;
    boolean deriveSequenceOnly = (!addToMemory) || Statement.invalidStatement(t1, t2, true);
    if (Statement.invalidStatement(t1, t2, false))
        return Collections.EMPTY_LIST;
    Term t11 = null;
    Term t22 = null;
    if (!deriveSequenceOnly && termForTemporalInduction(t1) && termForTemporalInduction(t2)) {
        Statement ss1 = (Statement) t1;
        Statement ss2 = (Statement) t2;
        Variable var1 = new Variable("$0");
        Variable var2 = new Variable("$1");
        if (ss2.containsTermRecursively(ss1.getSubject())) {
            HashMap<Term, Term> subs = new HashMap();
            subs.put(ss1.getSubject(), var1);
            if (ss2.containsTermRecursively(ss1.getPredicate())) {
                subs.put(ss1.getPredicate(), var2);
            }
            t11 = ss1.applySubstitute(subs);
            t22 = ss2.applySubstitute(subs);
        }
        if (ss1.containsTermRecursively(ss2.getSubject())) {
            HashMap<Term, Term> subs = new HashMap();
            subs.put(ss2.getSubject(), var1);
            if (ss1.containsTermRecursively(ss2.getPredicate())) {
                subs.put(ss2.getPredicate(), var2);
            }
            t11 = ss1.applySubstitute(subs);
            t22 = ss2.applySubstitute(subs);
        }
        // allow also temporal induction on operator arguments:
        if (ss2 instanceof Operation ^ ss1 instanceof Operation) {
            if (ss2 instanceof Operation && !(ss2.getSubject() instanceof Variable)) {
                // it is an operation, let's look if one of the arguments is same as the subject of the other term
                Term comp = ss1.getSubject();
                Term ss2_term = ((Operation) ss2).getSubject();
                boolean applicableVariableType = !(comp instanceof Variable && ((Variable) comp).hasVarIndep());
                if (ss2_term instanceof Product) {
                    Product ss2_prod = (Product) ss2_term;
                    if (applicableVariableType && Terms.contains(ss2_prod.term, comp)) {
                        // only if there is one and it isnt a variable already
                        Term[] ars = ss2_prod.cloneTermsReplacing(comp, var1);
                        t11 = Statement.make(ss1, var1, ss1.getPredicate());
                        Operation op = (Operation) Operation.make(new Product(ars), ss2.getPredicate());
                        t22 = op;
                    }
                }
            }
            if (ss1 instanceof Operation && !(ss1.getSubject() instanceof Variable)) {
                // it is an operation, let's look if one of the arguments is same as the subject of the other term
                Term comp = ss2.getSubject();
                Term ss1_term = ((Operation) ss1).getSubject();
                boolean applicableVariableType = !(comp instanceof Variable && ((Variable) comp).hasVarIndep());
                if (ss1_term instanceof Product) {
                    Product ss1_prod = (Product) ss1_term;
                    if (applicableVariableType && Terms.contains(ss1_prod.term, comp)) {
                        // only if there is one and it isnt a variable already
                        Term[] ars = ss1_prod.cloneTermsReplacing(comp, var1);
                        t22 = Statement.make(ss2, var1, ss2.getPredicate());
                        Operation op = (Operation) Operation.make(new Product(ars), ss1.getPredicate());
                        t11 = op;
                    }
                }
            }
        }
    }
    int durationCycles = Parameters.DURATION;
    long time1 = s1.getOccurenceTime();
    long time2 = s2.getOccurenceTime();
    long timeDiff = time2 - time1;
    Interval interval = null;
    if (!concurrent(time1, time2, durationCycles)) {
        interval = new Interval(Math.abs(timeDiff));
        if (timeDiff > 0) {
            t1 = Conjunction.make(t1, interval, ORDER_FORWARD);
            if (t11 != null) {
                t11 = Conjunction.make(t11, interval, ORDER_FORWARD);
            }
        } else {
            t2 = Conjunction.make(t2, interval, ORDER_FORWARD);
            if (t22 != null) {
                t22 = Conjunction.make(t22, interval, ORDER_FORWARD);
            }
        }
    }
    int order = order(timeDiff, durationCycles);
    TruthValue givenTruth1 = s1.truth;
    TruthValue givenTruth2 = s2.truth;
    // This code adds a penalty for large time distance (TODO probably revise)
    Sentence s3 = s2.projection(s1.getOccurenceTime(), nal.memory.time());
    givenTruth2 = s3.truth;
    // Truth and priority calculations
    TruthValue truth1 = TruthFunctions.induction(givenTruth1, givenTruth2);
    TruthValue truth2 = TruthFunctions.induction(givenTruth2, givenTruth1);
    TruthValue truth3 = TruthFunctions.comparison(givenTruth1, givenTruth2);
    TruthValue truth4 = TruthFunctions.intersection(givenTruth1, givenTruth2);
    BudgetValue budget1 = BudgetFunctions.forward(truth1, nal);
    budget1.setPriority(budget1.getPriority() * Parameters.TEMPORAL_INDUCTION_PRIORITY_PENALTY);
    BudgetValue budget2 = BudgetFunctions.forward(truth2, nal);
    budget2.setPriority(budget2.getPriority() * Parameters.TEMPORAL_INDUCTION_PRIORITY_PENALTY);
    BudgetValue budget3 = BudgetFunctions.forward(truth3, nal);
    budget3.setPriority(budget3.getPriority() * Parameters.TEMPORAL_INDUCTION_PRIORITY_PENALTY);
    // this one is sequence in sequenceBag, no need to reduce here
    BudgetValue budget4 = BudgetFunctions.forward(truth4, nal);
    Statement statement1 = Implication.make(t1, t2, order);
    Statement statement2 = Implication.make(t2, t1, reverseOrder(order));
    Statement statement3 = Equivalence.make(t1, t2, order);
    Term statement4 = null;
    switch(order) {
        case TemporalRules.ORDER_FORWARD:
            statement4 = Conjunction.make(t1, interval, s2.term, order);
            break;
        case TemporalRules.ORDER_BACKWARD:
            statement4 = Conjunction.make(s2.term, interval, t1, reverseOrder(order));
            break;
        default:
            statement4 = Conjunction.make(t1, s2.term, order);
            break;
    }
    // "Perception Variable Introduction Rule" - https://groups.google.com/forum/#!topic/open-nars/uoJBa8j7ryE
    if (!deriveSequenceOnly && statement2 != null) {
        // there is no general form
        // ok then it may be the (&/ =/> case which
        // is discussed here: https://groups.google.com/forum/#!topic/open-nars/uoJBa8j7ryE
        Statement st = statement2;
        if (st.getPredicate() instanceof Inheritance && (st.getSubject() instanceof Conjunction || st.getSubject() instanceof Operation)) {
            Term precon = (Term) st.getSubject();
            Inheritance consequence = (Inheritance) st.getPredicate();
            Term pred = consequence.getPredicate();
            Term sub = consequence.getSubject();
            // look if subject is contained in precon:
            boolean SubsSub = precon.containsTermRecursively(sub);
            boolean SubsPred = precon.containsTermRecursively(pred);
            Variable v1 = new Variable("$91");
            Variable v2 = new Variable("$92");
            HashMap<Term, Term> app = new HashMap<Term, Term>();
            if (SubsSub || SubsPred) {
                if (SubsSub)
                    app.put(sub, v1);
                if (SubsPred)
                    app.put(pred, v2);
                Term res = ((CompoundTerm) statement2).applySubstitute(app);
                if (res != null) {
                    // ok we applied it, all we have to do now is to use it
                    t22 = ((Statement) res).getSubject();
                    t11 = ((Statement) res).getPredicate();
                }
            }
        }
    }
    List<Task> success = new ArrayList<Task>();
    if (!deriveSequenceOnly && t11 != null && t22 != null) {
        Statement statement11 = Implication.make(t11, t22, order);
        Statement statement22 = Implication.make(t22, t11, reverseOrder(order));
        Statement statement33 = Equivalence.make(t11, t22, order);
        if (!tooMuchTemporalStatements(statement11)) {
            List<Task> t = nal.doublePremiseTask(statement11, truth1, budget1, true, false);
            if (t != null) {
                success.addAll(t);
            }
        }
        if (!tooMuchTemporalStatements(statement22)) {
            List<Task> t = nal.doublePremiseTask(statement22, truth2, budget2, true, false);
            if (t != null) {
                success.addAll(t);
            }
        }
        if (!tooMuchTemporalStatements(statement33)) {
            List<Task> t = nal.doublePremiseTask(statement33, truth3, budget3, true, false);
            if (t != null) {
                success.addAll(t);
            }
        }
    }
    if (!deriveSequenceOnly && !tooMuchTemporalStatements(statement1)) {
        List<Task> t = nal.doublePremiseTask(statement1, truth1, budget1, true, false);
        if (t != null) {
            success.addAll(t);
            for (Task task : t) {
                // we assume here that this function is used for observable events currently
                task.setObservablePrediction(true);
            }
        }
    }
    if (!deriveSequenceOnly && !tooMuchTemporalStatements(statement2)) {
        List<Task> t = nal.doublePremiseTask(statement2, truth2, budget2, true, false);
        if (t != null) {
            success.addAll(t);
            for (Task task : t) {
                // we assume here that this function is used for observable events currently
                task.setObservablePrediction(true);
            }
        }
    }
    if (!deriveSequenceOnly && !tooMuchTemporalStatements(statement3)) {
        List<Task> t = nal.doublePremiseTask(statement3, truth3, budget3, true, false);
        if (t != null) {
            for (Task task : t) {
                // we assume here that this function is used for observable events currently
                task.setObservablePrediction(true);
            }
            success.addAll(t);
        }
    }
    if (!tooMuchTemporalStatements(statement4)) {
        if (!allowSequence) {
            return success;
        }
        List<Task> tl = nal.doublePremiseTask(statement4, truth4, budget4, true, false, addToMemory);
        if (tl != null) {
            for (Task t : tl) {
                // fill sequenceTask buffer due to the new derived sequence
                if (addToMemory && t.sentence.isJudgment() && !t.sentence.isEternal() && t.sentence.term instanceof Conjunction && ((Conjunction) t.sentence.term).getTemporalOrder() != TemporalRules.ORDER_NONE && ((Conjunction) t.sentence.term).getTemporalOrder() != TemporalRules.ORDER_INVALID) {
                    TemporalInferenceControl.addToSequenceTasks(nal, t);
                }
                success.add(t);
            }
        }
    }
    return success;
}
Also used : BudgetValue(nars.entity.BudgetValue) CompoundTerm(nars.language.CompoundTerm) Task(nars.entity.Task) Variable(nars.language.Variable) HashMap(java.util.HashMap) Statement(nars.language.Statement) Inheritance(nars.language.Inheritance) ArrayList(java.util.ArrayList) Product(nars.language.Product) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Operation(nars.operator.Operation) TruthValue(nars.entity.TruthValue) Conjunction(nars.language.Conjunction) Sentence(nars.entity.Sentence) Interval(nars.language.Interval)

Example 5 with Operation

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

the class Anticipate method anticipationFeedback.

public void anticipationFeedback(Term content, Task t, Memory memory) {
    if (anticipationOperator) {
        Operation op = (Operation) Operation.make(Product.make(Term.SELF, content), this);
        TruthValue truth = new TruthValue(1.0f, Parameters.DEFAULT_JUDGMENT_CONFIDENCE);
        Stamp st;
        if (t == null) {
            st = new Stamp(memory);
        } else {
            st = t.sentence.stamp.clone();
            st.setOccurrenceTime(memory.time());
        }
        Sentence s = new Sentence(op, Symbols.JUDGMENT_MARK, truth, st);
        Task newTask = new Task(s, new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY * InternalExperience.INTERNAL_EXPERIENCE_PRIORITY_MUL, Parameters.DEFAULT_JUDGMENT_DURABILITY * InternalExperience.INTERNAL_EXPERIENCE_DURABILITY_MUL, BudgetFunctions.truthToQuality(truth)), true);
        memory.addNewTask(newTask, "Perceived (Internal Experience: Anticipation)");
    }
}
Also used : BudgetValue(nars.entity.BudgetValue) Task(nars.entity.Task) Stamp(nars.entity.Stamp) TruthValue(nars.entity.TruthValue) Operation(nars.operator.Operation) Sentence(nars.entity.Sentence)

Aggregations

Operation (nars.operator.Operation)14 BudgetValue (nars.entity.BudgetValue)5 Task (nars.entity.Task)5 TruthValue (nars.entity.TruthValue)5 Sentence (nars.entity.Sentence)4 Stamp (nars.entity.Stamp)4 Term (nars.language.Term)4 Operator (nars.operator.Operator)4 Events (nars.io.events.Events)3 Variable (nars.language.Variable)3 ArrayList (java.util.ArrayList)2 CompoundTerm (nars.language.CompoundTerm)2 Equivalence (nars.language.Equivalence)2 Implication (nars.language.Implication)2 FunctionOperator (nars.operator.FunctionOperator)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 DerivationContext (nars.control.DerivationContext)1 Concept (nars.entity.Concept)1 NativeOperator (nars.io.Symbols.NativeOperator)1