Search in sources :

Example 1 with Bool

use of nars.term.atom.Bool in project narchy by automenta.

the class DynamicTruthBeliefTable method template.

@Nullable
protected Term template(long start, long end, Term template, NAR nar) {
    Op templateOp = template.op();
    if (this.term != null && templateOp != this.term.op())
        // template doesnt match this (quick op test)
        return null;
    int templateSubs = template.subs();
    assert (templateSubs > 1);
    boolean temporal = templateOp.temporal;
    if (temporal) {
        int d = template.dt();
        if (d == XTERNAL) {
            int e = matchDT(start, end, templateSubs > 2, nar);
            assert (e != XTERNAL);
            Term next = template.dt(e);
            if ((next.subs() < templateSubs || next.dt() == XTERNAL)) {
                /*if no dt can be calculated, return
                              0 or some non-zero value (ex: 1, end-start, etc) in case of repeating subterms. */
                int artificialDT;
                if (templateSubs == 2) {
                    if (start != end && end - start < Integer.MAX_VALUE) {
                        if (end != start) {
                            artificialDT = (int) (end - start);
                        } else {
                            artificialDT = (template.sub(0).unneg().equals(template.sub(1).unneg())) ? nar.dur() : // ok for simultaneous
                            0;
                        }
                    } else {
                        artificialDT = nar.dur();
                    }
                } else /*(if (templateSubs > 2)*/
                {
                    assert (templateSubs > 2);
                    // commutive conjunction
                    artificialDT = 0;
                }
                next = template.dt(artificialDT);
                if (next.subs() < templateSubs || next.dt() == XTERNAL) {
                    next = template;
                    if (next.subs() == 2) {
                        // possibly pulled an internal XTERNAL to the outside, so try artificializing this as well
                        int limit = 2;
                        int nextDT = XTERNAL;
                        do {
                            next = next.dt(artificialDT);
                            if (next instanceof Bool)
                                return null;
                        } while (limit-- > 0 && (nextDT = next.dt()) == XTERNAL);
                        if (nextDT == XTERNAL)
                            // give up
                            return null;
                    } else {
                        // create a random sequence of the terms, separated by artificial DT's
                        assert (templateOp == CONJ);
                        Term[] subs = template.subterms().arrayClone();
                        ArrayUtils.shuffle(subs, nar.random());
                        int dur = nar.dur();
                        next = subs[0];
                        for (int k = 1; k < subs.length; k++) {
                            next = Op.conjMerge(next, 0, subs[k], dur);
                            if (next instanceof Bool)
                                // it is probably possible to find another solution with a different shuffle
                                return null;
                        }
                    }
                }
            }
            template = next;
        }
    }
    return template;
}
Also used : Op(nars.Op) Bool(nars.term.atom.Bool) Term(nars.term.Term) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with Bool

use of nars.term.atom.Bool in project narchy by automenta.

the class Premise method match.

/**
 * resolve the most relevant belief of a given term/concept
 * <p>
 * patham9 project-eternalize
 * patham9 depending on 4 cases
 * patham9 https://github.com/opennars/opennars2/blob/a143162a559e55c456381a95530d00fee57037c4/src/nal/deriver/projection_eternalization.clj
 * sseehh__ ok ill add that in a bit
 * patham9 you need  project-eternalize-to
 * sseehh__ btw i disabled immediate eternalization entirely
 * patham9 so https://github.com/opennars/opennars2/blob/a143162a559e55c456381a95530d00fee57037c4/src/nal/deriver/projection_eternalization.clj#L31
 * patham9 especially try to understand the "temporal temporal" case
 * patham9 its using the result of higher confidence
 * <p>
 * returns ttl used, -1 if failed before starting
 *
 * @param matchTime - temporal focus control: determines when a matching belief or answer should be projected to
 */
@Nullable
public Derivation match(Derivation d, long[] focus, int matchTTL) {
    NAR n = d.nar;
    if (task == null || task.isDeleted()) {
        // }
        return null;
    }
    // n.conceptualize(task.term(), (c)->{});
    int dur = d.dur;
    Term beliefTerm = term();
    Term taskTerm = task.term();
    Term taskConcept = task.term().concept();
    final boolean[] beliefConceptCanAnswerTaskConcept = { false };
    boolean unifiedBelief = false;
    Op to = taskTerm.op();
    Op bo = beliefTerm.op();
    if (to == bo) {
        if (taskConcept.equals(beliefTerm.concept())) {
            beliefConceptCanAnswerTaskConcept[0] = true;
        } else {
            if ((bo.conceptualizable) && (beliefTerm.hasAny(var) || taskTerm.hasAny(var))) {
                Term _beliefTerm = beliefTerm;
                final Term[] unifiedBeliefTerm = new Term[] { null };
                UnifySubst u = new UnifySubst(/*null*/
                VAR_QUERY, n, (y) -> {
                    if (y.op().conceptualizable) {
                        y = y.normalize();
                        beliefConceptCanAnswerTaskConcept[0] = true;
                        if (!y.equals(_beliefTerm)) {
                            unifiedBeliefTerm[0] = y;
                            // stop
                            return false;
                        }
                    }
                    // keep going
                    return true;
                }, matchTTL);
                u.varSymmetric = true;
                u.varCommonalize = true;
                u.unify(taskTerm, beliefTerm, true);
                if (unifiedBeliefTerm[0] != null) {
                    beliefTerm = unifiedBeliefTerm[0];
                    unifiedBelief = true;
                }
            }
        }
    }
    // HACK ?? assert(beliefTerm.op()!=NEG);
    beliefTerm = beliefTerm.unneg();
    // QUESTION ANSWERING and TERMLINK -> TEMPORALIZED BELIEF TERM projection
    Task belief = null;
    // float timeFocus = n.timeFocus.floatValue();
    // int fRad = Math.round(Math.max(1,dur * timeFocus));
    final Concept beliefConcept = beliefTerm.op().conceptualizable ? // conceptualize in case of dynamic concepts
    n.conceptualize(beliefTerm) : null;
    if (beliefConcept != null) {
        if (!beliefTerm.hasVarQuery()) {
            // doesnt make sense to look for a belief in a term with query var, it will have none
            final BeliefTable bb = beliefConcept.beliefs();
            if (task.isQuestOrQuestion()) {
                if (beliefConceptCanAnswerTaskConcept[0]) {
                    final BeliefTable answerTable = (task.isGoal() || task.isQuest()) ? beliefConcept.goals() : bb;
                    // //see if belief unifies with task (in reverse of previous unify)
                    // if (questionTerm.varQuery() == 0 || (unify((Compound)beliefConcept.term(), questionTerm, nar) == null)) {
                    // 
                    // } else {
                    // 
                    // }
                    Task match = answerTable.answer(task.start(), task.end(), dur, task, beliefTerm, n, d::add);
                    if (match != null) {
                        assert (task.isQuest() || match.punc() == BELIEF) : "quest answered with a belief but should be a goal";
                        @Nullable Task answered = task.onAnswered(match, n);
                        if (answered != null) {
                            n.emotion.onAnswer(task, answered);
                        }
                        if (match.isBelief()) {
                            belief = match;
                        }
                    }
                }
            }
            if ((belief == null) && !bb.isEmpty()) {
                belief = bb.match(focus[0], focus[1], beliefTerm, n, taskConcept.equals(beliefConcept.term()) ? x -> !x.equals(task) : null);
            }
        }
        if (unifiedBelief) {
            Concept originalBeliefConcept = n.conceptualize(term());
            if (originalBeliefConcept != null)
                linkVariable(originalBeliefConcept, beliefConcept);
        }
    }
    if (belief != null) {
        // use the belief's actual possibly-temporalized term
        beliefTerm = belief.term().unneg();
    // if (belief.equals(task)) { //do not repeat the same task for belief
    // belief = null; //force structural transform; also prevents potential inductive feedback loop
    // }
    }
    if (beliefTerm instanceof Bool) {
        // logger.warn("{} produced Bool beliefTerm", this);
        return null;
    }
    if (!d.reset().proto(task, belief, beliefTerm))
        return null;
    return d;
}
Also used : Concept(nars.concept.Concept) Op(nars.Op) Task(nars.Task) Term(nars.term.Term) Bool(nars.term.atom.Bool) BeliefTable(nars.table.BeliefTable) UnifySubst(nars.term.subst.UnifySubst) NAR(nars.NAR) Nullable(org.jetbrains.annotations.Nullable) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with Bool

use of nars.term.atom.Bool in project narchy by automenta.

the class DeriveTime method dt.

@Override
protected Term dt(Term x, int dt) {
    int ddt = Tense.dither(dt, d.ditherTime);
    Term y = super.dt(x, ddt);
    if (y instanceof Bool && ddt != dt) {
        // the dithered dt has destroyed it, so try the non-dithered (more precise) dt
        y = super.dt(x, dt);
    }
    return y;
}
Also used : Bool(nars.term.atom.Bool) Term(nars.term.Term)

Example 4 with Bool

use of nars.term.atom.Bool in project narchy by automenta.

the class TimeGraph method solveDT.

private boolean solveDT(Term x, long start, long ddt, Predicate<Event> each) {
    assert (ddt != TIMELESS && ddt != XTERNAL);
    int dt;
    if (ddt == ETERNAL) {
        dt = DTERNAL;
    } else {
        assert (ddt < Integer.MAX_VALUE) : ddt + " dt calculated";
        dt = (int) ddt;
    }
    Term y = dt(x, dt);
    if (y instanceof Bool)
        return true;
    if (start != ETERNAL && start != TIMELESS && dt != DTERNAL && dt < 0 && y.op() == CONJ) {
        // shift to left align
        start += dt;
    }
    return start != TIMELESS ? each.test(event(y, start, false)) : solveOccurrence(event(y, TIMELESS), each);
}
Also used : Bool(nars.term.atom.Bool) Term(nars.term.Term)

Example 5 with Bool

use of nars.term.atom.Bool 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)

Aggregations

Term (nars.term.Term)14 Bool (nars.term.atom.Bool)14 Op (nars.Op)4 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 FasterList (jcog.list.FasterList)2 Task (nars.Task)2 Subterms (nars.subterm.Subterms)2 Variable (nars.term.var.Variable)2 LongObjectPair (org.eclipse.collections.api.tuple.primitive.LongObjectPair)2 Iterables (com.google.common.collect.Iterables)1 Iterators (com.google.common.collect.Iterators)1 Multimap (com.google.common.collect.Multimap)1 MultimapBuilder (com.google.common.collect.MultimapBuilder)1 FileNotFoundException (java.io.FileNotFoundException)1 java.util (java.util)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 Consumer (java.util.function.Consumer)1 IntPredicate (java.util.function.IntPredicate)1 Predicate (java.util.function.Predicate)1