Search in sources :

Example 1 with Compound

use of nars.term.Compound in project narchy by automenta.

the class DefaultConceptBuilder method unroll.

@Nullable
static DynamicTruthModel unroll(Term t) {
    DynamicTruthModel dmt = null;
    final Subterms ts = t.subterms();
    switch(t.op()) {
        case INH:
            Term subj = ts.sub(0);
            Term pred = ts.sub(1);
            Op so = subj.op();
            Op po = pred.op();
            if (dmt == null) /*&& (so.atomic || so == PROD || so.isSet())*/
            {
                if ((po == Op.SECTi) || (po == Op.SECTe) || (po == DIFFi)) {
                    // (M --> P), (M --> S), notSet(S), notSet(P), neqCom(S,P) |- (M --> (P & S)), (Belief:Intersection)
                    // (M --> P), (M --> S), notSet(S), notSet(P), neqCom(S,P) |- (M --> (P | S)), (Belief:Union)
                    // (M --> P), (M --> S), notSet(S), notSet(P), neqCom(S,P) |- (M --> (P - S)), (Belief:Difference) //intensional
                    Compound cpred = (Compound) pred;
                    int s = cpred.subs();
                    Term[] x = new Term[s];
                    for (int i = 0; i < s; i++) {
                        Term y;
                        if (!validDynamicSubterm.test(y = INH.the(subj, cpred.sub(i))))
                            return null;
                        x[i] = y;
                    }
                    switch(po) {
                        case SECTi:
                            dmt = new DynamicTruthModel.Union(x);
                            break;
                        case SECTe:
                            dmt = new DynamicTruthModel.SectIntersection(x);
                            break;
                        case DIFFi:
                            dmt = new DynamicTruthModel.Difference(x[0], x[1]);
                            break;
                    }
                }
            /*else if (so.image) {
                        Compound img = (Compound) subj;
                        Term[] ee = new Term[img.size()];

                        int relation = img.dt();
                        int s = ee.length;
                        for (int j = 1, i = 0; i < s; ) {
                            if (j == relation)
                                ee[i++] = pred;
                            if (i < s)
                                ee[i++] = img.sub(j++);
                        }
                        Compound b = compoundOrNull(INH.the(DTERNAL, img.sub(0), $.p(ee)));
                        if (b != null)
                            dmt = new DynamicTruthModel.Identity(t, b);
                    }*/
            }
            if (dmt == null) /* && (po.atomic || po == PROD || po.isSet()) */
            {
                if ((so == Op.SECTi) || (so == Op.SECTe) || (so == Op.DIFFe)) // || (subj instanceof Int.IntRange) || (so == PROD && subj.OR(Int.IntRange.class::isInstance))
                {
                    // (P --> M), (S --> M), notSet(S), notSet(P), neqCom(S,P) |- ((S | P) --> M), (Belief:Intersection)
                    // (P --> M), (S --> M), notSet(S), notSet(P), neqCom(S,P) |- ((S & P) --> M), (Belief:Union)
                    // (P --> M), (S --> M), notSet(S), notSet(P), neqCom(S,P) |- ((P ~ S) --> M), (Belief:Difference) //extensional
                    Subterms subjsubs = subj.subterms();
                    int s = subjsubs.subs();
                    Term[] x = new Term[s];
                    for (int i = 0; i < s; i++) {
                        Term y;
                        // 
                        if (!validDynamicSubterm.test(y = INH.the(subjsubs.sub(i), pred)))
                            return null;
                        x[i] = y;
                    }
                    switch(so) {
                        // case PROD:
                        case SECTi:
                            dmt = new DynamicTruthModel.SectIntersection(x);
                            break;
                        case SECTe:
                            dmt = new DynamicTruthModel.Union(x);
                            break;
                        case DIFFe:
                            dmt = new DynamicTruthModel.Difference(x[0], x[1]);
                            break;
                    }
                }
            }
            break;
        case CONJ:
            // allow variables onlyif they are not themselves direct subterms of this
            if (validDynamicSubterms(ts)) {
                dmt = DynamicTruthModel.Intersection.ConjIntersection.the;
            }
            break;
        case DIFFe:
            // root DIFFe (not subj or pred of an inh)
            if (validDynamicSubterms(ts))
                dmt = new DynamicTruthModel.Difference(ts.arrayShared());
            break;
        case NEG:
            throw new RuntimeException("negation terms can not be conceptualized as something separate from that which they negate");
    }
    return dmt;
}
Also used : Subterms(nars.subterm.Subterms) Op(nars.Op) Compound(nars.term.Compound) Term(nars.term.Term) DynamicTruthModel(nars.concept.dynamic.DynamicTruthModel) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with Compound

use of nars.term.Compound in project narchy by automenta.

the class Abbreviation method accept.

@Override
public void accept(NAR nar, Task task) {
    Term taskTerm = task.term();
    if ((!(taskTerm instanceof Compound)) || taskTerm.vars() > 0)
        return;
    Prioritized b = task;
    input(b, bag.bag::put, (Compound) taskTerm, 1f, nar);
}
Also used : Prioritized(jcog.pri.Prioritized) Compound(nars.term.Compound) Term(nars.term.Term)

Example 3 with Compound

use of nars.term.Compound in project narchy by automenta.

the class Inperience method reify.

@Nullable
public static Term reify(Task s, Term self) {
    Truth tr = s.truth();
    Term x = s.term().negIf(tr != null && tr.isNegative());
    Term xx = x instanceof Compound && x.hasAny(VAR_QUERY) ? x.transform(TermTransform.queryToDepVar) : x;
    if (!xx.op().conceptualizable)
        return null;
    return $.func(reify(s.punc()), self, xx);
}
Also used : Compound(nars.term.Compound) Term(nars.term.Term) Truth(nars.truth.Truth) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with Compound

use of nars.term.Compound in project narchy by automenta.

the class Int method unroll.

/**
 * unroll IntInterval's
 */
public static Iterator<Term> unroll(Term cc) {
    // assert(!c.hasAny(Op.INT));
    // return Iterators.singletonIterator(c); //no IntInterval's early exit
    Map<ByteList, IntRange> intervals = new HashMap();
    cc.pathsTo(x -> x instanceof IntRange ? ((IntRange) x) : null, d -> d.hasAny(Op.INT), (ByteList p, IntRange x) -> {
        intervals.put(p.toImmutable(), x);
        return true;
    });
    int dim = intervals.size();
    switch(dim) {
        case 0:
            throw new RuntimeException();
        case // 1D
        1:
            {
                Map.Entry<ByteList, IntRange> e = intervals.entrySet().iterator().next();
                IntRange i1 = e.getValue();
                int max = i1.max;
                int min = i1.min;
                List<Term> t = $.newArrayList(1 + max - min);
                for (int i = min; i <= max; i++) {
                    @Nullable Term c1 = cc.transform(e.getKey(), $.the(i));
                    if (c1 != null)
                        t.add(c1);
                }
                return t.iterator();
            }
        case // 2D
        2:
            Iterator<Map.Entry<ByteList, IntRange>> ee = intervals.entrySet().iterator();
            Map.Entry<ByteList, IntRange> e1 = ee.next();
            Map.Entry<ByteList, IntRange> e2 = ee.next();
            IntRange i1 = e1.getValue();
            IntRange i2 = e2.getValue();
            int max1 = i1.max, min1 = i1.min, max2 = i2.max, min2 = i2.min;
            List<Term> t = $.newArrayList((1 + max2 - min2) * (1 + max1 - min1));
            for (int i = min1; i <= max1; i++) {
                for (int j = min2; j <= max2; j++) {
                    Term c1 = cc.transform(e1.getKey(), $.the(i));
                    Term c2 = c1.transform(e2.getKey(), $.the(j));
                    if (!(c2 instanceof Compound))
                        // throw new RuntimeException("how not transformed to compound");
                        continue;
                    t.add(c2);
                }
            }
            return t.iterator();
        default:
            // either there is none, or too many -- just use the term directly
            if (Param.DEBUG)
                throw new UnsupportedOperationException("too many embedded dimensions: " + dim);
            else
                return null;
    }
}
Also used : ImmutableByteList(org.eclipse.collections.api.list.primitive.ImmutableByteList) ByteList(org.eclipse.collections.api.list.primitive.ByteList) Compound(nars.term.Compound) Term(nars.term.Term) ImmutableByteList(org.eclipse.collections.api.list.primitive.ImmutableByteList) FasterList(jcog.list.FasterList) ByteList(org.eclipse.collections.api.list.primitive.ByteList)

Example 5 with Compound

use of nars.term.Compound in project narchy by automenta.

the class LightCompoundDT method equals.

// @Override
// public Term sub(int i, Term ifOutOfBounds) {
// return ref.sub(i, ifOutOfBounds);
// }
// @Override
// public final int hashCodeSubTerms() {
// return ref.hashCodeSubTerms();
// }
// @Override
// public Term conceptual() {
// return Compound.super.conceptual();
// //return ref.conceptual();
// }
// @Override
// @Nullable
// public final Term root() {
// return Compound.super.root();
// //return ref.root();
// }
@Override
public boolean equals(Object that) {
    if (this == that)
        return true;
    if (!(that instanceof Compound) || (hashDT != that.hashCode()))
        return false;
    if (that instanceof LightCompoundDT) {
        LightCompoundDT cthat = (LightCompoundDT) that;
        Compound thatRef = cthat.ref;
        Compound myRef = this.ref;
        // try sharing ref, even if the equality is false
        if (myRef != thatRef) {
            if (!myRef.equals(thatRef))
                return false;
        // if (myRef instanceof CachedCompound && thatRef instanceof CachedCompound) {
        // //prefer the earlier instance for sharing
        // if ((((CachedCompound) myRef).serial) < (((CachedCompound) thatRef).serial)) {
        // cthat.ref = myRef;
        // } else {
        // this.ref = thatRef;
        // }
        // }
        }
        return (dt == cthat.dt);
    } else {
        return Compound.equals(this, (Term) that);
    }
}
Also used : PatternCompound(nars.derive.PatternCompound) Compound(nars.term.Compound)

Aggregations

Compound (nars.term.Compound)58 Test (org.junit.jupiter.api.Test)38 Term (nars.term.Term)20 Unify (nars.term.subst.Unify)3 FasterList (jcog.list.FasterList)2 XorShift128PlusRandom (jcog.math.random.XorShift128PlusRandom)2 Op (nars.Op)2 PatternCompound (nars.derive.PatternCompound)2 PatternIndex (nars.index.term.PatternIndex)2 Atomic (nars.term.atom.Atomic)2 Variable (nars.term.var.Variable)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 alice.tuprolog (alice.tuprolog)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Prioritized (jcog.pri.Prioritized)1 Narsese (nars.Narsese)1