Search in sources :

Example 1 with TermList

use of nars.subterm.util.TermList in project narchy by automenta.

the class TermListTest method assertReallyEquals.

static void assertReallyEquals(String s) {
    Subterms immutable = $.$$(s).subterms();
    TermList mutable = new TermList(immutable);
    assertNotSame(immutable, mutable);
    assertNotEquals(immutable.getClass(), mutable.getClass());
    assertEquals(immutable, mutable);
    assertEquals(mutable, immutable);
    assertEquals(mutable, mutable);
    assertEquals(immutable, immutable);
    if (immutable.subs() > 1) {
        assertNotEquals(mutable.reversed(), mutable);
        assertNotEquals(immutable.reversed(), mutable);
    }
    assertEquals(mutable.reversed(), immutable.reversed());
    assertEquals(immutable.toString(), mutable.toString());
    assertEquals(0, Subterms.compare(mutable, immutable));
    assertEquals(0, Subterms.compare(mutable, mutable));
    assertEquals(0, Subterms.compare(immutable, mutable));
    assertEquals(immutable.hashCode(), mutable.hashCode());
    assertEquals(immutable.hashCodeSubterms(), mutable.hashCodeSubterms());
    assertEquals(immutable.subs(), mutable.subs());
    assertEquals(immutable.volume(), mutable.volume());
    assertEquals(immutable.complexity(), mutable.complexity());
    assertEquals(immutable.structure(), mutable.structure());
    assertEquals(immutable, mutable.the());
    Subterms[] ab = { mutable, immutable };
    for (Subterms a : ab) {
        for (Subterms b : ab) {
            TermTest.assertReallyEquals(CachedCompound.the(PROD, a), new CompoundLight(PROD, b));
            TermTest.assertReallyEquals(CachedCompound.the(SETe, a), new CompoundLight(SETe, b));
            assertNotEquals(CachedCompound.the(PROD, a), new CompoundLight(SETi, b));
        }
    }
}
Also used : CompoundLight(nars.term.compound.CompoundLight) TermList(nars.subterm.util.TermList)

Example 2 with TermList

use of nars.subterm.util.TermList in project narchy by automenta.

the class TermTransform method transformCompound.

/**
 * should not be called directly except by implementations of TermTransform
 */
@Nullable
default Term transformCompound(Compound x, Op op, int dt) {
    Subterms xx = x.subterms();
    Subterms yy = transformSubterms(xx);
    if (yy == null) {
        return null;
    } else if (yy != xx || op != x.op()) {
        Term z = the(op, op.temporal ? dt : DTERNAL, (TermList) yy);
        // //seems to happen very infrequently so probably not worth the test
        if (x != z && x.equals(z))
            // unchanged
            return x;
        return z;
    } else {
        return x.dt(dt);
    }
}
Also used : Subterms(nars.subterm.Subterms) Term(nars.term.Term) TermList(nars.subterm.util.TermList) DisposableTermList(nars.subterm.util.DisposableTermList) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with TermList

use of nars.subterm.util.TermList in project narchy by automenta.

the class TermTransform method transformSubterms.

/**
 * returns 'x' unchanged if no changes were applied,
 * returns 'y' if changes
 * returns null if untransformable
 */
default Subterms transformSubterms(Subterms x) {
    int s = x.subs();
    TermList y = null;
    for (int i = 0; i < s; i++) {
        Term xi = x.sub(i);
        Term yi = xi.transform(this);
        if (yi == null)
            return null;
        if (yi instanceof EllipsisMatch) {
            EllipsisMatch xe = (EllipsisMatch) yi;
            int xes = xe.subs();
            if (y == null) {
                // create anyway because this will signal if it was just empty
                y = new DisposableTermList(s - 1 + xes);
                if (i > 0) {
                    // add previously skipped subterms
                    y.addAll(x, 0, i);
                }
            }
            if (xes > 0) {
                for (int j = 0; j < xes; j++) {
                    @Nullable Term k = xe.sub(j).transform(this);
                    if (k == null) {
                        return null;
                    } else {
                        y.add(k);
                    }
                }
            }
        } else {
            if (xi != yi) /*&& (yi.getClass() != xi.getClass() || !xi.equals(yi))*/
            {
                if (y == null) {
                    y = new DisposableTermList(s);
                    // add previously skipped subterms
                    if (i > 0)
                        y.addAll(x, 0, i);
                }
            }
            if (y != null)
                y.add(yi);
        }
    }
    return y != null ? y : x;
}
Also used : DisposableTermList(nars.subterm.util.DisposableTermList) Term(nars.term.Term) TermList(nars.subterm.util.TermList) DisposableTermList(nars.subterm.util.DisposableTermList) Nullable(org.jetbrains.annotations.Nullable) EllipsisMatch(nars.derive.match.EllipsisMatch)

Aggregations

TermList (nars.subterm.util.TermList)3 DisposableTermList (nars.subterm.util.DisposableTermList)2 Term (nars.term.Term)2 Nullable (org.jetbrains.annotations.Nullable)2 EllipsisMatch (nars.derive.match.EllipsisMatch)1 Subterms (nars.subterm.Subterms)1 CompoundLight (nars.term.compound.CompoundLight)1