Search in sources :

Example 11 with Operation

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

the class RuleTables method goalFromWantBelief.

public static void goalFromWantBelief(final Task task, final short tIndex, short bIndex, final Term taskTerm, final DerivationContext nal, Term beliefTerm) {
    if (task.sentence.isJudgment() && tIndex == 0 && bIndex == 1 && taskTerm instanceof Operation) {
        Operation op = (Operation) taskTerm;
        if (op.getPredicate() == nal.memory.getOperator("^want")) {
            TruthValue newTruth = TruthFunctions.deduction(task.sentence.truth, Parameters.reliance);
            nal.singlePremiseTask(beliefTerm, Symbols.GOAL_MARK, newTruth, BudgetFunctions.forward(newTruth, nal));
        }
    }
}
Also used : TruthValue(nars.entity.TruthValue) Operation(nars.operator.Operation)

Example 12 with Operation

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

the class RuleTables method goalFromQuestion.

private static void goalFromQuestion(final Task task, final Term taskTerm, final DerivationContext nal) {
    if (task.sentence.punctuation == Symbols.QUESTION_MARK && (taskTerm instanceof Implication || taskTerm instanceof Equivalence)) {
        // <a =/> b>? |- a!
        Term goalterm = null;
        Term goalterm2 = null;
        if (taskTerm instanceof Implication) {
            Implication imp = (Implication) taskTerm;
            if (imp.getTemporalOrder() != TemporalRules.ORDER_BACKWARD || imp.getTemporalOrder() == TemporalRules.ORDER_CONCURRENT) {
                if (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || imp.getSubject() instanceof Operation) {
                    goalterm = imp.getSubject();
                }
                if (goalterm instanceof Variable && goalterm.hasVarQuery() && (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || imp.getPredicate() instanceof Operation)) {
                    // overwrite, it is a how question, in case of <?how =/> b> it is b! which is desired
                    goalterm = imp.getPredicate();
                }
            } else if (imp.getTemporalOrder() == TemporalRules.ORDER_BACKWARD) {
                if (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || imp.getPredicate() instanceof Operation) {
                    goalterm = imp.getPredicate();
                }
                if (goalterm instanceof Variable && goalterm.hasVarQuery() && (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || imp.getSubject() instanceof Operation)) {
                    // overwrite, it is a how question, in case of <?how =/> b> it is b! which is desired
                    goalterm = imp.getSubject();
                }
            }
        } else if (taskTerm instanceof Equivalence) {
            Equivalence qu = (Equivalence) taskTerm;
            if (qu.getTemporalOrder() == TemporalRules.ORDER_FORWARD || qu.getTemporalOrder() == TemporalRules.ORDER_CONCURRENT) {
                if (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || qu.getSubject() instanceof Operation) {
                    goalterm = qu.getSubject();
                }
                if (!Parameters.CURIOSITY_FOR_OPERATOR_ONLY || qu.getPredicate() instanceof Operation) {
                    goalterm2 = qu.getPredicate();
                }
            }
        }
        TruthValue truth = new TruthValue(1.0f, Parameters.DEFAULT_GOAL_CONFIDENCE * Parameters.CURIOSITY_DESIRE_CONFIDENCE_MUL);
        if (goalterm != null && !(goalterm instanceof Variable) && goalterm instanceof CompoundTerm) {
            goalterm = ((CompoundTerm) goalterm).transformIndependentVariableToDependentVar((CompoundTerm) goalterm);
            Sentence sent = new Sentence(goalterm, Symbols.GOAL_MARK, truth, new Stamp(task.sentence.stamp, nal.memory.time()));
            nal.singlePremiseTask(sent, new BudgetValue(task.getPriority() * Parameters.CURIOSITY_DESIRE_PRIORITY_MUL, task.getDurability() * Parameters.CURIOSITY_DESIRE_DURABILITY_MUL, BudgetFunctions.truthToQuality(truth)));
        }
        if (goalterm2 != null && !(goalterm2 instanceof Variable) && goalterm2 instanceof CompoundTerm) {
            goalterm2 = ((CompoundTerm) goalterm).transformIndependentVariableToDependentVar((CompoundTerm) goalterm2);
            Sentence sent = new Sentence(goalterm2, Symbols.GOAL_MARK, truth.clone(), new Stamp(task.sentence.stamp, nal.memory.time()));
            nal.singlePremiseTask(sent, new BudgetValue(task.getPriority() * Parameters.CURIOSITY_DESIRE_PRIORITY_MUL, task.getDurability() * Parameters.CURIOSITY_DESIRE_DURABILITY_MUL, BudgetFunctions.truthToQuality(truth)));
        }
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) BudgetValue(nars.entity.BudgetValue) Variable(nars.language.Variable) Equivalence(nars.language.Equivalence) Stamp(nars.entity.Stamp) TruthValue(nars.entity.TruthValue) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Operation(nars.operator.Operation) Implication(nars.language.Implication) Sentence(nars.entity.Sentence)

Example 13 with Operation

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

the class Narsese method parseTerm.

/* ---------- react String into term ---------- */
/**
 * Top-level method that react a Term in general, which may recursively call
 * itself.
 * <p>
 * There are 5 valid cases: 1. (Op, A1, ..., An) is a CompoundTerm if Op is
 * a built-in getOperator 2. {A1, ..., An} is an SetExt; 3. [A1, ..., An] is an
 * SetInt; 4. <T1 Re T2> is a Statement (including higher-order Statement);
 * 5. otherwise it is a simple term.
 *
 * @param s0 the String to be parsed
 * @param memory Reference to the memory
 * @return the Term generated from the String
 */
public Term parseTerm(String s) throws InvalidInputException {
    s = s.trim();
    if (s.length() == 0)
        return null;
    int index = s.length() - 1;
    char first = s.charAt(0);
    char last = s.charAt(index);
    NativeOperator opener = getOpener(first);
    if (opener != null) {
        switch(opener) {
            case COMPOUND_TERM_OPENER:
                if (last == COMPOUND_TERM_CLOSER.ch) {
                    return parseCompoundTerm(s.substring(1, index));
                } else {
                    throw new InvalidInputException("missing CompoundTerm closer");
                }
            case SET_EXT_OPENER:
                if (last == SET_EXT_CLOSER.ch) {
                    return SetExt.make(parseArguments(s.substring(1, index) + ARGUMENT_SEPARATOR));
                } else {
                    throw new InvalidInputException("missing ExtensionSet closer");
                }
            case SET_INT_OPENER:
                if (last == SET_INT_CLOSER.ch) {
                    return SetInt.make(parseArguments(s.substring(1, index) + ARGUMENT_SEPARATOR));
                } else {
                    throw new InvalidInputException("missing IntensionSet closer");
                }
            case STATEMENT_OPENER:
                if (last == STATEMENT_CLOSER.ch) {
                    return parseStatement(s.substring(1, index));
                } else {
                    throw new InvalidInputException("missing Statement closer");
                }
        }
    } else if (Parameters.FUNCTIONAL_OPERATIONAL_FORMAT) {
        // parse functional operation:
        // function()
        // function(a)
        // function(a,b)
        // test for existence of matching parentheses at beginning at index!=0
        int pOpen = s.indexOf('(');
        int pClose = s.lastIndexOf(')');
        if ((pOpen != -1) && (pClose != -1) && (pClose == s.length() - 1)) {
            String operatorString = Operator.addPrefixIfMissing(s.substring(0, pOpen));
            Operator operator = memory.getOperator(operatorString);
            if (operator == null) {
                // ???
                throw new InvalidInputException("Unknown operator: " + operatorString);
            }
            String argString = s.substring(pOpen + 1, pClose + 1);
            Term[] a;
            if (argString.length() > 1) {
                ArrayList<Term> args = parseArguments(argString);
                a = args.toArray(new Term[args.size()]);
            } else {
                // void "()" arguments, default to (SELF)
                a = Operation.SELF_TERM_ARRAY;
            }
            Operation o = Operation.make(operator, a, true);
            return o;
        }
    }
    // if no opener, parse the term
    return parseAtomicTerm(s);
}
Also used : Operator(nars.operator.Operator) NativeOperator(nars.io.Symbols.NativeOperator) Symbols.getOperator(nars.io.Symbols.getOperator) NativeOperator(nars.io.Symbols.NativeOperator) ArrayList(java.util.ArrayList) Operation(nars.operator.Operation)

Example 14 with Operation

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

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