use of nars.derive.match.EllipsisMatch 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;
}
use of nars.derive.match.EllipsisMatch 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;
}
Aggregations