Search in sources :

Example 31 with CompoundTerm

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

the class DefaultGraphizer method onFinish.

@Override
public void onFinish(NARGraph g) {
    if (includeSyntax > 0) {
        for (final Term a : terms.keySet()) {
            if (a instanceof CompoundTerm) {
                CompoundTerm c = (CompoundTerm) a;
                g.addVertex(c.operator());
                g.addEdge(c.operator(), c, new NARGraph.TermType());
                if (includeSyntax - 1 > 0) {
                    recurseTermComponents(g, c, includeSyntax - 1);
                }
            }
        }
    }
    if (includeTermContent) {
        for (final Term a : terms.keySet()) {
            for (final Term b : terms.keySet()) {
                if (a == b) {
                    continue;
                }
                if (a.containsTerm(b)) {
                    g.addVertex(a);
                    g.addVertex(b);
                    g.addEdge(a, b, new NARGraph.TermContent());
                }
                if (b.containsTerm(a)) {
                    g.addVertex(a);
                    g.addVertex(b);
                    g.addEdge(b, a, new NARGraph.TermContent());
                }
            }
        }
    }
    if (includeDerivations && includeBeliefs) {
        for (final Map.Entry<Sentence, Concept> s : sentenceTerms.entrySet()) {
            final Sentence derivedSentence = s.getKey();
            final Concept derived = s.getValue();
            for (final Map.Entry<Sentence, Concept> t : sentenceTerms.entrySet()) {
                if (s == t) {
                    continue;
                }
                final Sentence deriverSentence = t.getKey();
                final Concept deriver = t.getValue();
                if (derived == deriver) {
                    continue;
                }
            }
        }
    }
    if (includeTermLinks) {
        for (Map.Entry<TermLink, Concept> et : termLinks.entrySet()) {
            TermLink t = et.getKey();
            Concept from = et.getValue();
            Concept to = terms.get(t.target);
            if (to != null) {
                g.addEdge(from, to, new NARGraph.TermLinkEdge(t));
            }
        }
    }
    if (includeTaskLinks) {
        for (Map.Entry<TaskLink, Concept> et : taskLinks.entrySet()) {
            TaskLink t = et.getKey();
            if (this.taskPriorityThreshold != null && t.getPriority() > this.taskPriorityThreshold.get()) {
                Concept from = et.getValue();
                if (t.targetTask != null) {
                    Task theTask = t.targetTask;
                    if (!g.containsVertex(theTask)) {
                        g.addVertex(theTask);
                        Term term = theTask.getTerm();
                        if (term != null) {
                            Concept c = terms.get(term);
                            if (c != null) {
                                if (g.containsVertex(c)) {
                                    g.addVertex(c);
                                }
                                g.addEdge(c, theTask, new NARGraph.TermContent());
                            }
                        }
                        onTask(theTask);
                    }
                    g.addEdge(from, t.targetTask, new NARGraph.TaskLinkEdge(t));
                }
            }
        }
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) Concept(nars.entity.Concept) TermLink(nars.entity.TermLink) Task(nars.entity.Task) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) TaskLink(nars.entity.TaskLink) Map(java.util.Map) HashMap(java.util.HashMap) Sentence(nars.entity.Sentence)

Example 32 with CompoundTerm

use of nars.language.CompoundTerm 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 33 with CompoundTerm

use of nars.language.CompoundTerm 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 34 with CompoundTerm

use of nars.language.CompoundTerm 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 35 with CompoundTerm

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

the class RuleTables method transformTask.

/* ----- inference with one TaskLink only ----- */
/**
 * The TaskLink is of type TRANSFORM, and the conclusion is an equivalent
 * transformation
 *
 * @param tLink The task link
 * @param nal Reference to the memory
 */
public static void transformTask(TaskLink tLink, DerivationContext nal) {
    CompoundTerm content = (CompoundTerm) nal.getCurrentTask().getTerm();
    short[] indices = tLink.index;
    Term inh = null;
    if ((indices.length == 2) || (content instanceof Inheritance)) {
        // <(*, term, #) --> #>
        inh = content;
    } else if (indices.length == 3) {
        // <<(*, term, #) --> #> ==> #>
        inh = content.term[indices[0]];
    } else if (indices.length == 4) {
        // <(&&, <(*, term, #) --> #>, #) ==> #>
        Term component = content.term[indices[0]];
        if ((component instanceof Conjunction) && (((content instanceof Implication) && (indices[0] == 0)) || (content instanceof Equivalence))) {
            Term[] cterms = ((CompoundTerm) component).term;
            if (indices[1] < cterms.length - 1) {
                inh = cterms[indices[1]];
            } else {
                return;
            }
        } else {
            return;
        }
    }
    if (inh instanceof Inheritance) {
        StructuralRules.transformProductImage((Inheritance) inh, content, indices, nal);
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) Equivalence(nars.language.Equivalence) Inheritance(nars.language.Inheritance) Conjunction(nars.language.Conjunction) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Implication(nars.language.Implication)

Aggregations

CompoundTerm (nars.language.CompoundTerm)46 Term (nars.language.Term)43 TruthValue (nars.entity.TruthValue)30 BudgetValue (nars.entity.BudgetValue)27 Sentence (nars.entity.Sentence)20 Statement (nars.language.Statement)19 Conjunction (nars.language.Conjunction)18 Task (nars.entity.Task)17 Inheritance (nars.language.Inheritance)13 Implication (nars.language.Implication)8 HashMap (java.util.HashMap)7 Variable (nars.language.Variable)7 Interval (nars.language.Interval)6 SetExt (nars.language.SetExt)6 SetInt (nars.language.SetInt)6 Product (nars.language.Product)5 TruthFunctions.reduceConjunction (nars.inference.TruthFunctions.reduceConjunction)4 Equivalence (nars.language.Equivalence)4 ImageExt (nars.language.ImageExt)4 ImageInt (nars.language.ImageInt)4