Search in sources :

Example 1 with AbstractPred

use of nars.term.pred.AbstractPred in project narchy by automenta.

the class Compound method evalSafe.

/*@NotNull*/
@Override
default Term evalSafe(TermContext context, Op supertermOp, int subterm, int remain) {
    if (remain-- <= 0)
        return Null;
    // Termed ff = context.applyIfPossible(this);
    // if (!ff.equals(this))
    // return ff.term();
    /*if (subterms().hasAll(opBits))*/
    Subterms uu = subterms();
    Term[] xy = null;
    // any contained evaluables
    Op o = op();
    // int possiblyFunctional = o == INH ? Op.funcInnerBits : Op.funcBits;
    // boolean recurseIfChanged = false;
    int ellipsisAdds = 0, ellipsisRemoves = 0;
    for (int i = 0, n = uu.subs(); i < n; i++) {
        Term xi = xy != null ? xy[i] : uu.sub(i);
        Term yi = xi.evalSafe(context, o, i, remain);
        if (yi == null) {
            return Null;
        } else {
            if (yi instanceof EllipsisMatch) {
                int ys = yi.subs();
                ellipsisAdds += ys;
                ellipsisRemoves++;
            }
            if (xi != yi && (xi.getClass() != yi.getClass() || !xi.equals(yi))) {
                if (xy == null) {
                    // begin clone copy
                    xy = arrayClone();
                }
                xy[i] = yi;
            }
        }
    }
    if (ellipsisAdds > 0) {
        // flatten ellipsis
        xy = EllipsisMatch.flatten(xy, ellipsisAdds, ellipsisRemoves);
    }
    Term u;
    if (/*changed*/
    xy != null) {
        u = o.a(dt(), xy);
        // refresh root operator in case it has changed
        o = u.op();
        // refresh subterms
        uu = u.subterms();
    } else {
        u = this;
    }
    // compute this without necessarily constructing the superterm, which happens after this if it doesnt recurse
    if (o == INH && u.hasAll(Op.funcBits)) {
        Term pred, subj;
        if ((pred = uu.sub(1)) instanceof Functor && (subj = uu.sub(0)).op() == PROD) {
            Term v = ((Functor) pred).apply(subj.subterms());
            if (v instanceof AbstractPred) {
                u = $.the(((AbstractPred) v).test(null));
            } else if (v == null) {
            // null means to keep 'u' unchanged same
            } else {
                // continue with the evaluation result
                u = v;
            }
        }
    }
    if (u != this && (u.equals(this) && u.getClass() == getClass()))
        // return to this instance, undoing any substitutions necessary to reach this eval
        u = this;
    return u;
}
Also used : Subterms(nars.subterm.Subterms) Op(nars.Op) AbstractPred(nars.term.pred.AbstractPred) EllipsisMatch(nars.derive.match.EllipsisMatch)

Aggregations

Op (nars.Op)1 EllipsisMatch (nars.derive.match.EllipsisMatch)1 Subterms (nars.subterm.Subterms)1 AbstractPred (nars.term.pred.AbstractPred)1