Search in sources :

Example 1 with DisposableTermList

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

EllipsisMatch (nars.derive.match.EllipsisMatch)1 DisposableTermList (nars.subterm.util.DisposableTermList)1 TermList (nars.subterm.util.TermList)1 Term (nars.term.Term)1 Nullable (org.jetbrains.annotations.Nullable)1