Search in sources :

Example 81 with Term

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

the class CompositionalRules method composeCompound.

/* -------------------- intersections and differences -------------------- */
/**
 * {<S ==> M>, <P ==> M>} |- {<(S|P) ==> M>, <(S&P) ==> M>, <(S-P) ==>
 * M>,
 * <(P-S) ==> M>}
 *
 * @param taskSentence The first premise
 * @param belief The second premise
 * @param index The location of the shared term
 * @param nal Reference to the memory
 */
static void composeCompound(final Statement taskContent, final Statement beliefContent, final int index, final DerivationContext nal) {
    if ((!nal.getCurrentTask().sentence.isJudgment()) || (taskContent.getClass() != beliefContent.getClass())) {
        return;
    }
    final Term componentT = taskContent.term[1 - index];
    final Term componentB = beliefContent.term[1 - index];
    final Term componentCommon = taskContent.term[index];
    int order1 = taskContent.getTemporalOrder();
    int order2 = beliefContent.getTemporalOrder();
    int order = TemporalRules.composeOrder(order1, order2);
    if (order == TemporalRules.ORDER_INVALID) {
        return;
    }
    if ((componentT instanceof CompoundTerm) && ((CompoundTerm) componentT).containsAllTermsOf(componentB)) {
        decomposeCompound((CompoundTerm) componentT, componentB, componentCommon, index, true, order, nal);
        return;
    } else if ((componentB instanceof CompoundTerm) && ((CompoundTerm) componentB).containsAllTermsOf(componentT)) {
        decomposeCompound((CompoundTerm) componentB, componentT, componentCommon, index, false, order, nal);
        return;
    }
    final TruthValue truthT = nal.getCurrentTask().sentence.truth;
    final TruthValue truthB = nal.getCurrentBelief().truth;
    final TruthValue truthOr = union(truthT, truthB);
    final TruthValue truthAnd = intersection(truthT, truthB);
    TruthValue truthDif = null;
    Term termOr = null;
    Term termAnd = null;
    Term termDif = null;
    if (index == 0) {
        if (taskContent instanceof Inheritance) {
            termOr = IntersectionInt.make(componentT, componentB);
            termAnd = IntersectionExt.make(componentT, componentB);
            if (truthB.isNegative()) {
                if (!truthT.isNegative()) {
                    termDif = DifferenceExt.make(componentT, componentB);
                    truthDif = intersection(truthT, negation(truthB));
                }
            } else if (truthT.isNegative()) {
                termDif = DifferenceExt.make(componentB, componentT);
                truthDif = intersection(truthB, negation(truthT));
            }
        } else if (taskContent instanceof Implication) {
            termOr = Disjunction.make(componentT, componentB);
            termAnd = Conjunction.make(componentT, componentB);
        }
        processComposed(taskContent, componentCommon, termOr, order, truthOr, nal);
        processComposed(taskContent, componentCommon, termAnd, order, truthAnd, nal);
        processComposed(taskContent, componentCommon, termDif, order, truthDif, nal);
    } else {
        // index == 1
        if (taskContent instanceof Inheritance) {
            termOr = IntersectionExt.make(componentT, componentB);
            termAnd = IntersectionInt.make(componentT, componentB);
            if (truthB.isNegative()) {
                if (!truthT.isNegative()) {
                    termDif = DifferenceInt.make(componentT, componentB);
                    truthDif = intersection(truthT, negation(truthB));
                }
            } else if (truthT.isNegative()) {
                termDif = DifferenceInt.make(componentB, componentT);
                truthDif = intersection(truthB, negation(truthT));
            }
        } else if (taskContent instanceof Implication) {
            termOr = Conjunction.make(componentT, componentB);
            termAnd = Disjunction.make(componentT, componentB);
        }
        processComposed(taskContent, termOr, componentCommon, order, truthOr, nal);
        processComposed(taskContent, termAnd, componentCommon, order, truthAnd, nal);
        processComposed(taskContent, termDif, componentCommon, order, truthDif, nal);
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) TruthValue(nars.entity.TruthValue) Inheritance(nars.language.Inheritance) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Implication(nars.language.Implication)

Example 82 with Term

use of nars.language.Term 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 83 with Term

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

the class LocalRules method convertedJudgment.

/**
 * Convert judgment into different relation
 * <p>
 * called in MatchingRules
 *
 * @param budget The budget value of the new task
 * @param truth The truth value of the new task
 * @param nal Reference to the memory
 */
private static void convertedJudgment(final TruthValue newTruth, final BudgetValue newBudget, final DerivationContext nal) {
    Statement content = (Statement) nal.getCurrentTask().getTerm();
    Statement beliefContent = (Statement) nal.getCurrentBelief().term;
    int order = TemporalRules.reverseOrder(beliefContent.getTemporalOrder());
    final Term subjT = content.getSubject();
    final Term predT = content.getPredicate();
    final Term subjB = beliefContent.getSubject();
    final Term predB = beliefContent.getPredicate();
    Term otherTerm;
    if (subjT.hasVarQuery()) {
        otherTerm = (predT.equals(subjB)) ? predB : subjB;
        content = Statement.make(content, otherTerm, predT, order);
    }
    if (predT.hasVarQuery()) {
        otherTerm = (subjT.equals(subjB)) ? predB : subjB;
        content = Statement.make(content, subjT, otherTerm, order);
    }
    if (content == null) {
        return;
    }
    nal.singlePremiseTask(content, Symbols.JUDGMENT_MARK, newTruth, newBudget);
}
Also used : Statement(nars.language.Statement) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term)

Example 84 with Term

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

the class LocalRules method revision.

/**
 * Belief revision
 * <p>
 * called from Concept.reviseTable and match
 *
 * @param newBelief The new belief in task
 * @param oldBelief The previous belief with the same content
 * @param feedbackToLinks Whether to send feedback to the links
 * @param memory Reference to the memory
 */
public static boolean revision(final Sentence newBelief, final Sentence oldBelief, final boolean feedbackToLinks, final DerivationContext nal) {
    if (newBelief.term == null) {
        return false;
    }
    newBelief.stamp.alreadyAnticipatedNegConfirmation = oldBelief.stamp.alreadyAnticipatedNegConfirmation;
    TruthValue newTruth = newBelief.truth.clone();
    TruthValue oldTruth = oldBelief.truth;
    boolean useNewBeliefTerm = false;
    if (newBelief.getTerm().hasInterval()) {
        Term cterm = replaceIntervals(newBelief.getTerm());
        Concept c = nal.memory.concept(cterm);
        ArrayList<Long> ivalOld = extractIntervals(nal.memory, oldBelief.getTerm());
        if (c.recent_intervals.size() == 0) {
            for (Long l : ivalOld) {
                c.recent_intervals.add((float) l);
            }
        }
        ArrayList<Long> ivalNew = extractIntervals(nal.memory, newBelief.getTerm());
        for (int i = 0; i < ivalNew.size(); i++) {
            // vote as one new entry, turtle style
            float Inbetween = (c.recent_intervals.get(i) + ivalNew.get(i)) / 2.0f;
            // less truth expectation, slower
            float speed = 1.0f / (float) (Parameters.INTERVAL_ADAPT_SPEED * (1.0f - newBelief.getTruth().getExpectation()));
            c.recent_intervals.set(i, c.recent_intervals.get(i) + speed * (Inbetween - c.recent_intervals.get(i)));
        }
        long AbsDiffSumNew = 0;
        for (int i = 0; i < ivalNew.size(); i++) {
            AbsDiffSumNew += Math.abs(ivalNew.get(i) - c.recent_intervals.get(i));
        }
        long AbsDiffSumOld = 0;
        for (int i = 0; i < ivalNew.size(); i++) {
            AbsDiffSumOld += Math.abs(ivalOld.get(i) - c.recent_intervals.get(i));
        }
        long AbsDiffSum = 0;
        for (int i = 0; i < ivalNew.size(); i++) {
            AbsDiffSum += Math.abs(ivalNew.get(i) - ivalOld.get(i));
        }
        // re-project, and it's safe:
        float a = temporalProjection(0, AbsDiffSum, 0);
        // we won't count more confidence than
        // when the second premise would have been shifted
        // to the necessary time in the first place
        // to build the hypothesis newBelief encodes
        newTruth.setConfidence(newTruth.getConfidence() * a);
        useNewBeliefTerm = AbsDiffSumNew < AbsDiffSumOld;
    }
    TruthValue truth = TruthFunctions.revision(newTruth, oldTruth);
    BudgetValue budget = BudgetFunctions.revise(newTruth, oldTruth, truth, feedbackToLinks, nal);
    if (budget.aboveThreshold()) {
        if (nal.doublePremiseTaskRevised(useNewBeliefTerm ? newBelief.term : oldBelief.term, truth, budget)) {
            // nal.mem().logic.BELIEF_REVISION.commit();
            return true;
        }
    }
    return false;
}
Also used : Concept(nars.entity.Concept) BudgetValue(nars.entity.BudgetValue) TruthValue(nars.entity.TruthValue) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term)

Example 85 with Term

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

the class LocalRules method inferToAsym.

/**
 * {<S <-> P>, <P --> S>} |- <S --> P> Produce an Inheritance/Implication
 * from a Similarity/Equivalence and a reversed Inheritance/Implication
 *
 * @param asym The asymmetric premise
 * @param sym The symmetric premise
 * @param nal Reference to the memory
 */
private static void inferToAsym(Sentence asym, Sentence sym, DerivationContext nal) {
    Statement statement = (Statement) asym.term;
    Term sub = statement.getPredicate();
    Term pre = statement.getSubject();
    Statement content = Statement.make(statement, sub, pre, statement.getTemporalOrder());
    if (content == null)
        return;
    TruthValue truth = TruthFunctions.reduceConjunction(sym.truth, asym.truth);
    BudgetValue budget = BudgetFunctions.forward(truth, nal);
    nal.doublePremiseTask(content, truth, budget, false, false);
}
Also used : BudgetValue(nars.entity.BudgetValue) Statement(nars.language.Statement) TruthValue(nars.entity.TruthValue) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term)

Aggregations

Term (nars.language.Term)109 CompoundTerm (nars.language.CompoundTerm)66 BudgetValue (nars.entity.BudgetValue)48 TruthValue (nars.entity.TruthValue)46 Sentence (nars.entity.Sentence)40 Task (nars.entity.Task)37 Statement (nars.language.Statement)28 Conjunction (nars.language.Conjunction)20 Inheritance (nars.language.Inheritance)19 Stamp (nars.entity.Stamp)17 Concept (nars.entity.Concept)14 Implication (nars.language.Implication)13 Product (nars.language.Product)11 NAR (nars.main.NAR)9 Interval (nars.language.Interval)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)7 SetExt (nars.language.SetExt)7 SetInt (nars.language.SetInt)7 Variable (nars.language.Variable)7