Search in sources :

Example 1 with CONJ

use of nars.Op.CONJ 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 2 with CONJ

use of nars.Op.CONJ in project narchy by automenta.

the class TimeGraph method solveDT.

boolean solveDT(Term x, Predicate<Event> each) {
    assert (x.dt() == XTERNAL);
    Subterms xx = x.subterms();
    // FasterList<Event> events = new FasterList<>(byTerm.get(x.root()));
    // for (int i = 0, eventsSize = events.size(); i < eventsSize; i++) {
    // Event r = events.get(i);
    // if (r instanceof Absolute) {
    // if (r.id.subterms().equals(xx)) {
    // if (!each.test(r))
    // return false; //done
    // }
    // }
    // 
    // }
    int subs = xx.subs();
    if (subs == 2) {
        Term a = xx.sub(0);
        Term b = xx.sub(1);
        boolean aEqB = a.equals(b);
        if (!a.hasXternal() && !b.hasXternal() && (aEqB || !commonSubEventsWithMultipleOccurrences(a, b))) {
            UnifiedSet<Event> ae = new UnifiedSet(2);
            solveOccurrence(event(a, TIMELESS), ax -> {
                if (ax instanceof Absolute)
                    ae.add(ax);
                return true;
            });
            int aes = ae.size();
            if (aes > 0) {
                if (aEqB) {
                    // same term, must have >1 absolute timepoints
                    if (aes > 1) {
                        Event[] ab = ae.toArray(new Event[aes]);
                        // Arrays.sort(ab, Comparator.comparingLong(Event::when));
                        for (int i = 0; i < ab.length; i++) {
                            Event abi = ab[i];
                            for (int j = 0; j < ab.length; j++) {
                                if (i == j)
                                    continue;
                                if (!solveDT(x, abi.when(), dt(x, abi, ab[j]), each))
                                    return false;
                            }
                        }
                    }
                } else {
                    UnifiedSet<Event> be = new UnifiedSet(2);
                    solveOccurrence(event(b, TIMELESS), bx -> {
                        if (bx instanceof Absolute)
                            be.add(bx);
                        return true;
                    });
                    int bes = be.size();
                    if (bes > 0) {
                        // if (aes == 1 || bes == 1) {
                        if (!ae.allSatisfy(ax -> be.allSatisfyWith((bx, axx) -> solveDT(x, axx.when(), dt(x, axx, bx), each), ax)))
                            return false;
                    // }
                    }
                }
            }
        }
        // UnifiedSet<Event>[] abs = new UnifiedSet[2]; //exact occurrences of each subterm
        FasterList<Event> rels = new FasterList<>(4);
        // int[] phase = new int[]{0};
        // int p = phase[0];
        // if (z instanceof Absolute) {
        // if (abs[p] == null) abs[p] = new UnifiedSet(2);
        // abs[p].add(z);
        // //}
        // }
        Consumer<Event> collect = rels::add;
        byTerm.get(a).forEach(collect);
        if (aEqB) {
        // abs[1] = abs[0];
        } else {
            // phase[0] = 1;
            byTerm.get(b).forEach(collect);
        // if (abs[1] == null)
        // byTerm.get(b.neg()).forEach(collect);  //if nothing, look for negations
        }
        // if (abs[0] != null && abs[1] != null) {
        // known exact occurrences for both subterms
        // iterate all possibilities
        // TODO order in some way
        // TODO other simple cases: 1 -> N
        // if (abs[0].size() == 1 && abs[1].size() == 1) {
        // //simple case:
        // Event aa = abs[0].iterator().next();
        // Event bb = abs[1].iterator().next();
        // if (!solveDT(x, each, aa, bb))
        // return false;
        // } else {
        // if (!abs[0].allSatisfy(ae ->
        // abs[1].allSatisfyWith((be, aaee) ->
        // solveDT(x, each, aaee, be), ae)))
        // return false;
        // }
        // }
        int ns = rels.size();
        if (ns > 0) {
            if (ns > 1) {
                // sort by volume
                rels.sortThisByInt(s -> s.id.volume());
            }
            return bfs(rels, new CrossTimeSolver() {

                @Override
                protected boolean next(BooleanObjectPair<ImmutableDirectedEdge<Event, TimeSpan>> move, Node<nars.derive.time.TimeGraph.Event, nars.derive.time.TimeGraph.TimeSpan> next) {
                    // System.out.println(path);
                    long[] startDT = pathDT(next, a, b, path);
                    if (startDT == null)
                        // nothing at this step
                        return true;
                    long start = startDT[0];
                    long ddt = startDT[1];
                    return TimeGraph.this.solveDT(x, start, ddt, each);
                }
            });
        }
    // } else {
    // assert (x.op() == CONJ);
    // List<LongObjectPair<Term>> when = $.newArrayList();
    // for (int ix = 0; ix < subs; ix++) {
    // //assert(!z.hasXternal());
    // solveOccurrence(event(xx.sub(ix), TIMELESS), (ze) -> {
    // if (ze.when() == TIMELESS)
    // return true; //keep trying
    // when.add(pair(ze.when(), ze.id));
    // return false; //just one, for now //TODO see if there are any others
    // });
    // }
    // if (when.size() == subs) {
    // when.sort(Comparator.comparingLong(LongObjectPair::getOne));
    // long base = when.get(0).getOne();
    // Term zz = when.get(0).getTwo();
    // for (int i = 1; i < subs; i++) {
    // LongObjectPair<Term> wgi = when.get(i);
    // zz = Op.conjMerge(zz, 0, wgi.getTwo(), wgi.getOne() - base);
    // if (zz instanceof Bool)
    // return true; //failure
    // }
    // return each.test(event(zz, base));
    // }
    }
    // last resort
    return each.test(event(x, TIMELESS));
}
Also used : Subterms(nars.subterm.Subterms) 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) FasterList(jcog.list.FasterList) Term(nars.term.Term) ImmutableDirectedEdge(jcog.data.graph.ImmutableDirectedEdge) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet)

Aggregations

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 java.util (java.util)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 Consumer (java.util.function.Consumer)2 Predicate (java.util.function.Predicate)2 TODO (jcog.TODO)2 Util (jcog.Util)2 ImmutableDirectedEdge (jcog.data.graph.ImmutableDirectedEdge)2 MapNodeGraph (jcog.data.graph.MapNodeGraph)2 Search (jcog.data.graph.search.Search)2 Cons (jcog.list.Cons)2 FasterList (jcog.list.FasterList)2 Op (nars.Op)2 CONJ (nars.Op.CONJ)2 IMPL (nars.Op.IMPL)2 TS_ZERO (nars.derive.time.TimeGraph.TimeSpan.TS_ZERO)2 Subterms (nars.subterm.Subterms)2