Search in sources :

Example 6 with Variable

use of nars.language.Variable in project opennars by opennars.

the class DerivationContext method derivedTask.

public boolean derivedTask(final Task task, final boolean revised, final boolean single, boolean overlapAllowed, boolean addToMemory) {
    if (task.sentence.isGoal() && (task.sentence.term instanceof Implication || task.sentence.term instanceof Equivalence)) {
        // implication and equivalence goals are not supported anymore
        return false;
    }
    if (!task.budget.aboveThreshold()) {
        memory.removeTask(task, "Insufficient Budget");
        return false;
    }
    if (task.sentence != null && task.sentence.truth != null) {
        float conf = task.sentence.truth.getConfidence();
        if (conf == 0) {
            // no confidence - we can delete the wrongs out that way.
            memory.removeTask(task, "Ignored (zero confidence)");
            return false;
        }
    }
    if (task.sentence.term instanceof Operation) {
        Operation op = (Operation) task.sentence.term;
        if (op.getSubject() instanceof Variable || op.getPredicate() instanceof Variable) {
            memory.removeTask(task, "Operation with variable as subject or predicate");
            return false;
        }
    }
    final Stamp stamp = task.sentence.stamp;
    // its revision, of course its cyclic, apply evidental base policy
    if (!overlapAllowed) {
        // todo reconsider
        final int stampLength = stamp.baseLength;
        for (int i = 0; i < stampLength; i++) {
            final long baseI = stamp.evidentialBase[i];
            for (int j = 0; j < stampLength; j++) {
                // !single since the derivation shouldn't depend on whether there is a current belief or not!!
                if ((!single && this.evidentalOverlap) || ((i != j) && (baseI == stamp.evidentialBase[j]))) {
                    memory.removeTask(task, "Overlapping Evidenctal Base");
                    // "(i=" + i + ",j=" + j +')' /* + " in " + stamp.toString()*/
                    return false;
                }
            }
        }
    }
    // deactivated, new anticipation handling is attempted instead
    /*if(task.sentence.getOccurenceTime()>memory.time() && ((this.getCurrentTask()!=null && (this.getCurrentTask().isInput() || this.getCurrentTask().sentence.producedByTemporalInduction)) || (this.getCurrentBelief()!=null && this.getCurrentBelief().producedByTemporalInduction))) {
            Anticipate ret = ((Anticipate)memory.getOperator("^anticipate"));
            if(ret!=null) {
                ret.anticipate(task.sentence.term, memory, task.sentence.getOccurenceTime(),task);
            }
        }*/
    task.setElemOfSequenceBuffer(false);
    if (!revised) {
        task.getBudget().setDurability(task.getBudget().getDurability() * Parameters.DERIVATION_DURABILITY_LEAK);
        task.getBudget().setPriority(task.getBudget().getPriority() * Parameters.DERIVATION_PRIORITY_LEAK);
    }
    memory.event.emit(Events.TaskDerive.class, task, revised, single);
    if (addToMemory) {
        addTask(task, "Derived");
    }
    return true;
}
Also used : Variable(nars.language.Variable) Equivalence(nars.language.Equivalence) Stamp(nars.entity.Stamp) Events(nars.io.events.Events) Operation(nars.operator.Operation) Implication(nars.language.Implication)

Example 7 with Variable

use of nars.language.Variable in project opennars by opennars.

the class CompositionalRules method IntroVarSameSubjectOrPredicate.

static void IntroVarSameSubjectOrPredicate(final Sentence originalMainSentence, final Sentence subSentence, final Term component, final Term content, final int index, final DerivationContext nal) {
    Term T1 = originalMainSentence.term;
    if (!(T1 instanceof CompoundTerm) || !(content instanceof CompoundTerm)) {
        return;
    }
    CompoundTerm T = (CompoundTerm) T1;
    CompoundTerm T2 = (CompoundTerm) content;
    if ((component instanceof Inheritance && content instanceof Inheritance) || (component instanceof Similarity && content instanceof Similarity)) {
        // CompoundTerm result = T;
        if (component.equals(content)) {
            // wouldnt make sense to create a conjunction here, would contain a statement twice
            return;
        }
        Variable depIndVar1 = new Variable("#depIndVar1");
        Variable depIndVar2 = new Variable("#depIndVar2");
        if (((Statement) component).getPredicate().equals(((Statement) content).getPredicate()) && !(((Statement) component).getPredicate() instanceof Variable)) {
            CompoundTerm zw = (CompoundTerm) T.term[index];
            zw = (CompoundTerm) zw.setComponent(1, depIndVar1, nal.mem());
            T2 = (CompoundTerm) T2.setComponent(1, depIndVar1, nal.mem());
            Conjunction res = (Conjunction) Conjunction.make(zw, T2);
            T = (CompoundTerm) T.setComponent(index, res, nal.mem());
        } else if (((Statement) component).getSubject().equals(((Statement) content).getSubject()) && !(((Statement) component).getSubject() instanceof Variable)) {
            CompoundTerm zw = (CompoundTerm) T.term[index];
            zw = (CompoundTerm) zw.setComponent(0, depIndVar2, nal.mem());
            T2 = (CompoundTerm) T2.setComponent(0, depIndVar2, nal.mem());
            Conjunction res = (Conjunction) Conjunction.make(zw, T2);
            T = (CompoundTerm) T.setComponent(index, res, nal.mem());
        }
        TruthValue truth = induction(originalMainSentence.truth, subSentence.truth);
        BudgetValue budget = BudgetFunctions.compoundForward(truth, T, nal);
        nal.doublePremiseTask(T, truth, budget, false, false);
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) BudgetValue(nars.entity.BudgetValue) Variable(nars.language.Variable) Similarity(nars.language.Similarity) Statement(nars.language.Statement) TruthValue(nars.entity.TruthValue) Inheritance(nars.language.Inheritance) TruthFunctions.reduceConjunction(nars.inference.TruthFunctions.reduceConjunction) Conjunction(nars.language.Conjunction) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term)

Example 8 with Variable

use of nars.language.Variable in project opennars by opennars.

the class RuleTables method conditionalDedIndWithVar.

/**
 * Conditional deduction or induction, with variable unification
 *
 * @param conditional The premise that is an Implication with a Conjunction
 * as condition
 * @param index The location of the shared term in the condition
 * @param statement The second premise that is a statement
 * @param side The location of the shared term in the statement
 * @param nal Reference to the memory
 */
private static void conditionalDedIndWithVar(Sentence conditionalSentence, Implication conditional, short index, Statement statement, short side, DerivationContext nal) {
    if (!(conditional.getSubject() instanceof CompoundTerm))
        return;
    CompoundTerm condition = (CompoundTerm) conditional.getSubject();
    if (condition instanceof Conjunction) {
        // conditionalDedIndWithVar
        for (Term t : condition.term) {
            // does not support the case where
            if (t instanceof Variable) {
                // (this can happen since we have # due to image transform,
                return;
            }
        // although not for other conjunctions)
        }
    }
    Term component = condition.term[index];
    Term component2 = null;
    if (statement instanceof Inheritance) {
        component2 = statement;
        side = -1;
    } else if (statement instanceof Implication) {
        component2 = statement.term[side];
    }
    if (component2 != null) {
        Term[] u = new Term[] { conditional, statement };
        if (Variables.unify(VAR_INDEPENDENT, component, component2, u)) {
            conditional = (Implication) u[0];
            statement = (Statement) u[1];
            SyllogisticRules.conditionalDedInd(conditionalSentence, conditional, index, statement, side, nal);
        }
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) Variable(nars.language.Variable) Inheritance(nars.language.Inheritance) Conjunction(nars.language.Conjunction) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Implication(nars.language.Implication)

Example 9 with Variable

use of nars.language.Variable 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 10 with Variable

use of nars.language.Variable in project opennars by opennars.

the class Narsese method parseAtomicTerm.

// private static void showWarning(String message) {
// new TemporaryFrame( message + "\n( the faulty line has been kept in the addInput window )",
// 40000, TemporaryFrame.WARNING );
// }
/**
 * Parse a Term that has no internal structure.
 * <p>
 * The Term can be a constant or a variable.
 *
 * @param s0 the String to be parsed
 * @throws nars.io.StringParser.InvalidInputException the String cannot be
 * parsed into a Term
 * @return the Term generated from the String
 */
private Term parseAtomicTerm(String s0) throws InvalidInputException {
    String s = s0.trim();
    if (s.length() == 0) {
        throw new InvalidInputException("missing term");
    }
    Operator op = memory.getOperator(s0);
    if (op != null) {
        return op;
    }
    if (s.contains(" ")) {
        // invalid characters in a name
        throw new InvalidInputException("invalid term");
    }
    char c = s.charAt(0);
    if (c == Symbols.INTERVAL_PREFIX) {
        return Interval.interval(s);
    }
    if (containVar(s) && !s.equals("#")) {
        return new Variable(s);
    } else {
        return Term.get(s);
    }
}
Also used : Operator(nars.operator.Operator) NativeOperator(nars.io.Symbols.NativeOperator) Symbols.getOperator(nars.io.Symbols.getOperator) Variable(nars.language.Variable)

Aggregations

Variable (nars.language.Variable)10 CompoundTerm (nars.language.CompoundTerm)8 TruthValue (nars.entity.TruthValue)7 Term (nars.language.Term)7 BudgetValue (nars.entity.BudgetValue)6 Sentence (nars.entity.Sentence)5 Stamp (nars.entity.Stamp)4 Task (nars.entity.Task)4 Inheritance (nars.language.Inheritance)4 HashMap (java.util.HashMap)3 Conjunction (nars.language.Conjunction)3 Implication (nars.language.Implication)3 Statement (nars.language.Statement)3 Operation (nars.operator.Operation)3 ArrayList (java.util.ArrayList)2 Equivalence (nars.language.Equivalence)2 Interval (nars.language.Interval)2 TruthFunctions.reduceConjunction (nars.inference.TruthFunctions.reduceConjunction)1 NativeOperator (nars.io.Symbols.NativeOperator)1 Symbols.getOperator (nars.io.Symbols.getOperator)1