Search in sources :

Example 1 with Subterms

use of nars.subterm.Subterms 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 Subterms

use of nars.subterm.Subterms in project narchy by automenta.

the class TimeGraph method onAdd.

@Override
protected void onAdd(Node<nars.derive.time.TimeGraph.Event, nars.derive.time.TimeGraph.TimeSpan> x) {
    Event event = x.id;
    Term eventTerm = event.id;
    if (byTerm.put(eventTerm, event)) {
        if (autoNeg.test(eventTerm)) {
            // link(event, 0, know(eventTerm.neg())); //WEAK
            // WEAK
            link(know(eventTerm), 0, know(eventTerm.neg()));
        }
    }
    // Term tRoot = eventTerm.root();
    // if (!tRoot.equals(eventTerm))
    // byTerm.put(tRoot, event);
    int edt = eventTerm.dt(), eventDT;
    if (edt == DTERNAL && dternalAsZero)
        eventDT = 0;
    else
        eventDT = edt;
    switch(eventTerm.op()) {
        // break;
        case IMPL:
            Term subj = eventTerm.sub(0);
            Event se = know(subj);
            Term pred = eventTerm.sub(1);
            Event pe = know(pred);
            if (eventDT == DTERNAL) {
                link(se, ETERNAL, pe);
                subj.eventsWhile((w, y) -> {
                    link(know(y), ETERNAL, pe);
                    return true;
                }, 0, true, true, false, 0);
                pred.eventsWhile((w, y) -> {
                    link(se, ETERNAL, know(y));
                    return true;
                }, 0, true, true, false, 0);
            } else if (eventDT != XTERNAL) {
                int st = subj.dtRange();
                link(se, (eventDT + st), pe);
                subj.eventsWhile((w, y) -> {
                    link(know(y), eventDT + st - w, pe);
                    return true;
                }, 0, true, true, false, 0);
                pred.eventsWhile((w, y) -> {
                    link(se, eventDT + st + w, know(y));
                    return true;
                }, 0, true, true, false, 0);
            }
            break;
        case CONJ:
            // Subterms tt = eventTerm.subterms();
            // int s = tt.subs();
            // if (et == TIMELESS) {
            // //chain the sibling subevents
            // if (s == 2) {
            // Term se0 = tt.sub(0);
            // Event e0 = know(se0);
            // Term se1 = tt.sub(1);
            // Event e1 = know(se1);
            // int dt;
            // Event earliest;
            // if (eventDT == DTERNAL) {
            // dt = DTERNAL;
            // earliest = e0; //just use the first by default
            // } else {
            // long t0 = eventTerm.subTime(se0);
            // long t1 = eventTerm.subTime(se1);
            // long ddt = (int) (t1 - t0);
            // assert (ddt < Integer.MAX_VALUE);
            // dt = (int) ddt;
            // earliest = t0 < t1 ? e0 : e1;
            // }
            // link(e0, dt, e1);
            // link(earliest, 0, event);
            // 
            // } else {
            // throw new TODO();
            // }
            // 
            // } else
            // locate the events and sub-events absolutely
            long et = event.when();
            switch(eventDT) {
                case XTERNAL:
                    break;
                case DTERNAL:
                    Subterms es = eventTerm.subterms();
                    int esn = es.subs();
                    Term prev = es.sub(0);
                    for (int i = 1; i < esn; i++) {
                        // dternal chain
                        Term next = es.sub(i);
                        link(knowComponent(et, 0, prev), ETERNAL, knowComponent(et, 0, next));
                        prev = next;
                    }
                    break;
                case 0:
                    // eventTerm.subterms().forEach(this::know); //TODO can these be absolute if the event is?
                    boolean timed = et != ETERNAL;
                    for (Term s : eventTerm.subterms()) {
                        Event t = eventDT == 0 ? // 0
                        knowComponent(et, 0, s) : (// DTERNAL and TIMED
                        timed ? // DTERNAL and TIMED
                        know(s, et) : know(s));
                        if (t != null) {
                            link(event, (eventDT == 0 || timed) ? 0 : ETERNAL, // DTERNAL and TIMELESS
                            t);
                        } else {
                        // WHY?
                        }
                    }
                    break;
                default:
                    eventTerm.eventsWhile((w, y) -> {
                        link(event, w, knowComponent(et, w, y));
                        return true;
                    }, 0, false, false, false, 0);
                    break;
            }
            break;
    }
}
Also used : Iterables(com.google.common.collect.Iterables) java.util(java.util) Search(jcog.data.graph.search.Search) CONJ(nars.Op.CONJ) Tense(nars.time.Tense) MultimapBuilder(com.google.common.collect.MultimapBuilder) IMPL(nars.Op.IMPL) Multimap(com.google.common.collect.Multimap) Iterators(com.google.common.collect.Iterators) MathArithmeticException(org.apache.commons.math3.exception.MathArithmeticException) Op(nars.Op) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Bool(nars.term.atom.Bool) LongObjectPair(org.eclipse.collections.api.tuple.primitive.LongObjectPair) Term(nars.term.Term) Predicate(java.util.function.Predicate) TS_ZERO(nars.derive.time.TimeGraph.TimeSpan.TS_ZERO) FasterList(jcog.list.FasterList) MapNodeGraph(jcog.data.graph.MapNodeGraph) Util(jcog.Util) BooleanObjectPair(org.eclipse.collections.api.tuple.primitive.BooleanObjectPair) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) TODO(jcog.TODO) ImmutableDirectedEdge(jcog.data.graph.ImmutableDirectedEdge) Subterms(nars.subterm.Subterms) Cons(jcog.list.Cons) NotNull(org.jetbrains.annotations.NotNull) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) Subterms(nars.subterm.Subterms) Term(nars.term.Term)

Example 3 with Subterms

use of nars.subterm.Subterms in project narchy by automenta.

the class PatternIndex method patternify.

/*@NotNull*/
public Term patternify(/*@NotNull*/
Compound x) {
    Subterms s = x.subterms();
    int ss = s.subs();
    Term[] bb = new Term[ss];
    // , temporal = false;
    boolean changed = false;
    for (int i = 0; i < ss; i++) {
        Term a = s.sub(i);
        Termed b = get(a, true);
        if (a != b) {
            changed = true;
        }
        bb[i] = b.term();
    }
    if (!changed && Ellipsis.firstEllipsis(s) == null)
        return x;
    Subterms v = changed ? Subterms.subtermsInterned(bb.length > 1 && x.op().commutative && (concurrent(x.dt())) ? Terms.sorted(bb) : bb) : s;
    Ellipsis e = Ellipsis.firstEllipsis(v);
    return e != null ? ellipsis(x, v, e) : // new PatternCompound.PatternCompoundSimple(x.op(), x.dt(), v);
    x.op().the(x.dt(), v.arrayShared());
}
Also used : Subterms(nars.subterm.Subterms) Termed(nars.term.Termed) Ellipsis(nars.derive.match.Ellipsis) Term(nars.term.Term)

Example 4 with Subterms

use of nars.subterm.Subterms in project narchy by automenta.

the class Revision method intermpolate.

/*@NotNull*/
static Term intermpolate(/*@NotNull*/
Term a, long bOffset, /*@NotNull*/
Term b, float aProp, float curDepth, NAR nar) {
    if (a.equals(b) && bOffset == 0) {
        return a;
    }
    Op ao = a.op();
    Op bo = b.op();
    if (ao != bo)
        // fail, why
        return Null;
    // assert (ao == bo) : a + " and " + b + " have different op";
    // 
    // if (ao == NEG) {
    // return intermpolate(a.unneg(), 0, b.unneg(),
    // aProp, curDepth, rng, mergeOrChoose).neg();
    // }
    int len = a.subs();
    if (len > 0) {
        if (ao.temporal) {
            boolean mergeOrChoose = nar.dtMergeOrChoose.get();
            if (ao == CONJ) {
                return dtMergeConjEvents(a, bOffset, b, aProp, curDepth, mergeOrChoose, nar.random(), nar.dtDitherCycles());
            } else if (ao == IMPL) {
                return dtMergeDirect(a, b, aProp, curDepth, nar, mergeOrChoose);
            } else
                throw new UnsupportedOperationException();
        } else {
            if (a.equals(b)) {
                return a;
            }
            Term[] ab = new Term[len];
            boolean change = false;
            Subterms aa = a.subterms();
            Subterms bb = b.subterms();
            for (int i = 0; i < len; i++) {
                Term ai = aa.sub(i);
                Term bi = bb.sub(i);
                if (!ai.equals(bi)) {
                    Term y = intermpolate(ai, 0, bi, aProp, curDepth / 2f, nar);
                    if (y instanceof Bool && (!(ai instanceof Bool)))
                        // failure
                        return Null;
                    if (!ai.equals(y)) {
                        change = true;
                        ai = y;
                    }
                }
                ab[i] = ai;
            }
            return !change ? a : ao.the(choose(a, b, aProp, nar.random()).dt(), /**
             * this effectively chooses between && and &| in a size >2 case
             */
            ab);
        }
    }
    return choose(a, b, aProp, nar.random());
}
Also used : Subterms(nars.subterm.Subterms) Op(nars.Op) Bool(nars.term.atom.Bool) Term(nars.term.Term)

Example 5 with Subterms

use of nars.subterm.Subterms in project narchy by automenta.

the class Revision method dtDiff.

static float dtDiff(Term a, Term b, int depth) {
    if (a.equals(b))
        return 0f;
    // if (!a.isTemporal() || !b.isTemporal()) {
    // return 0f;
    // }
    Op ao = a.op();
    Op bo = b.op();
    if (ao != bo)
        // why?
        return Float.POSITIVE_INFINITY;
    Subterms aa = a.subterms();
    int len = aa.subs();
    Subterms bb = b.subterms();
    float d = 0;
    // if (len!=blen) {
    // if (!aa.equals(bb)) {
    // return (a.dtRange() + b.dtRange()) / depth; //estimate
    // }
    // int blen = bb.subs();
    boolean aSubsEqualsBSubs = aa.equals(bb);
    if (a.op() == CONJ && !aSubsEqualsBSubs) {
        // HACK :)
        Conj c = new Conj();
        String as = Conj.sequenceString(a, c).toString();
        String bs = Conj.sequenceString(b, c).toString();
        int levDist = Texts.levenshteinDistance(as, bs);
        float seqDiff = (((float) levDist) / (Math.min(as.length(), bs.length())));
        // HACK estimate
        float rangeDiff = Math.max(1f, Math.abs(a.dtRange() - b.dtRange()));
        d += (1f + rangeDiff) * (1f + seqDiff);
    } else {
        if (!aSubsEqualsBSubs) {
            if (aa.subs() != bb.subs())
                return Float.POSITIVE_INFINITY;
            for (int i = 0; i < len; i++) d += dtDiff(aa.sub(i), bb.sub(i), depth + 1);
        }
        int adt = a.dt();
        int bdt = b.dt();
        // ockham temoral razor - prefer temporally shorter explanations
        if (adt == DTERNAL)
            adt = // 0; //dternal prefer match with immediate dt=0
            bdt;
        if (bdt == DTERNAL)
            bdt = // bdt = 0; //dternal prefer match with immediate dt=0
            adt;
        if (adt == XTERNAL)
            adt = bdt;
        if (bdt == XTERNAL)
            bdt = adt;
        if (adt != bdt) /* && adt != DTERNAL && bdt != DTERNAL*/
        {
            // if (adt == DTERNAL) {
            // adt = 0;
            // dLocal += 0.5f;
            // }
            // if (bdt == DTERNAL) {
            // bdt = 0;
            // dLocal += 0.5f;
            // }
            d += Math.abs(adt - bdt);
        }
    }
    return d / depth;
}
Also used : Subterms(nars.subterm.Subterms) Op(nars.Op) Conj(nars.term.compound.util.Conj)

Aggregations

Subterms (nars.subterm.Subterms)21 Op (nars.Op)11 Term (nars.term.Term)9 Nullable (org.jetbrains.annotations.Nullable)7 java.util (java.util)3 Predicate (java.util.function.Predicate)3 TODO (jcog.TODO)3 FasterList (jcog.list.FasterList)3 Bool (nars.term.atom.Bool)3 LongObjectPair (org.eclipse.collections.api.tuple.primitive.LongObjectPair)3 Iterables (com.google.common.collect.Iterables)2 Iterators (com.google.common.collect.Iterators)2 Multimap (com.google.common.collect.Multimap)2 MultimapBuilder (com.google.common.collect.MultimapBuilder)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 Consumer (java.util.function.Consumer)2 Util (jcog.Util)2 ImmutableDirectedEdge (jcog.data.graph.ImmutableDirectedEdge)2 MapNodeGraph (jcog.data.graph.MapNodeGraph)2 Search (jcog.data.graph.search.Search)2