Search in sources :

Example 1 with Conj

use of nars.term.compound.util.Conj in project narchy by automenta.

the class TaskLinkCurveBag method compress.

/**
 * the state of the tasklink bag can be seen as one big virtual conjunction.
 * the goal here in compressing is to combine and replace each "cluster" of
 * related links (temporally, or otherwise) with one actual conjunction.
 *
 * attempt to compress clusters of nearby time ranges into one link.
 * conceptualizes any new terms to ensure a dynamic concept can be ready.
 *
 * TODO eternal links can be combined && with any other links
 */
public void compress(NAR nar) {
    if (size() < capacity() * TEMPORAL_COMPRESSION_THRESHOLD)
        return;
    final Conj[] beliefs = { new Conj() };
    final Conj[] questions = { new Conj() };
    final Conj[] goals = { new Conj() };
    forEach(x -> {
        if (!(x instanceof TaskLink.GeneralTaskLink))
            return;
        TaskLink.GeneralTaskLink g = (TaskLink.GeneralTaskLink) x;
        long when = Tense.dither(g.when(), nar);
        // must be exact, not root
        Term what = g.term();
        Conj[] table = null;
        switch(g.punc()) {
            // maybe include a negation flag for beliefs
            case BELIEF:
                table = beliefs;
                break;
            case QUESTION:
                table = questions;
                break;
            case GOAL:
                table = goals;
                break;
            default:
                return;
        }
        if (!table[0].add(what, when))
            table[0] = new Conj();
    /* HACK just clear when it becomes contradicting */
    });
    compress(beliefs[0], BELIEF, nar);
    compress(questions[0], QUESTION, nar);
    compress(goals[0], GOAL, nar);
}
Also used : Conj(nars.term.compound.util.Conj) Term(nars.term.Term)

Example 2 with Conj

use of nars.term.compound.util.Conj 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)

Example 3 with Conj

use of nars.term.compound.util.Conj in project narchy by automenta.

the class ConjTest method testEventContradiction.

@Test
public void testEventContradiction() {
    Conj c = new Conj();
    c.add($.the("x"), 1);
    assertFalse(c.add($.the("x").neg(), 1));
    assertEquals(False, c.term());
}
Also used : Conj(nars.term.compound.util.Conj) Test(org.junit.jupiter.api.Test)

Example 4 with Conj

use of nars.term.compound.util.Conj in project narchy by automenta.

the class ConjTest method testConjComplexAddRemove.

@Test
public void testConjComplexAddRemove() {
    Term x = $$("(( ( ((_1-->_2),_3) &| (--,_4)) &| (_5 &| _6)) &&+8 ( (((_1-->_2),_3) &| (--,_4)) &| (_5 &|_6))))");
    Conj c = Conj.from(x);
    assertEquals(x, c.term());
    boolean removedLast = c.remove($$("((_1-->_2),_3)"), c.event.keysView().max());
    assertTrue(removedLast);
    assertEquals("((&|,((_1-->_2),_3),(--,_4),_5,_6) &&+8 (&|,(--,_4),_5,_6))", c.term().toString());
    boolean removedFirst = c.remove($$("((_1-->_2),_3)"), c.event.keysView().min());
    assertTrue(removedFirst);
    assertEquals("((&|,(--,_4),_5,_6) &&+8 (&|,(--,_4),_5,_6))", c.term().toString());
}
Also used : Conj(nars.term.compound.util.Conj) Test(org.junit.jupiter.api.Test)

Example 5 with Conj

use of nars.term.compound.util.Conj in project narchy by automenta.

the class ConjTest method testSimpleEvents.

@Test
public void testSimpleEvents() {
    Conj c = new Conj();
    c.add($.the("x"), 1);
    c.add($.the("y"), 2);
    assertEquals("(x &&+1 y)", c.term().toString());
    assertEquals(1, c.shift());
    assertEquals(2, c.event.size());
}
Also used : Conj(nars.term.compound.util.Conj) Test(org.junit.jupiter.api.Test)

Aggregations

Conj (nars.term.compound.util.Conj)13 Test (org.junit.jupiter.api.Test)10 Op (nars.Op)2 Subterms (nars.subterm.Subterms)2 Term (nars.term.Term)2 java.io (java.io)1 java.util (java.util)1 Predicate (java.util.function.Predicate)1 ScriptException (javax.script.ScriptException)1 TODO (jcog.TODO)1 Texts (jcog.Texts)1 User (jcog.User)1 FasterList (jcog.list.FasterList)1 PriReference (jcog.pri.PriReference)1 Concept (nars.concept.Concept)1 Operator (nars.concept.Operator)1 DepIndepVarIntroduction (nars.op.DepIndepVarIntroduction)1 ListFunc (nars.op.ListFunc)1 MathFunc (nars.op.MathFunc)1 Subst (nars.op.Subst)1