use of nars.Op in project narchy by automenta.
the class Variable method unify.
@Override
default boolean unify(Term y, Unify u) {
if (this.equals(y))
return true;
// var pattern will unify anything (below)
// see: https://github.com/opennars/opennars/blob/4515f1d8e191a1f097859decc65153287d5979c5/nars_core/nars/language/Variables.java#L18
Op xOp = op();
if (y instanceof Variable) {
Op yOp = y.op();
if (xOp == yOp) {
if (u.varCommonalize && commonalizableVariable(xOp) && commonalizableVariable(yOp)) {
// TODO check if this is already a common variable containing y
Term common = CommonVariable.common(this, (Variable) y);
if (common == this || common == y)
// no change
return true;
Term xBound = u.xy.get(this);
Term yBound = u.xy.get(y);
if (xBound != null && yBound != null) {
return xBound.unify(yBound, u);
}
Term target;
if (xBound != null)
target = xBound;
else if (yBound != null)
target = yBound;
else
target = null;
if (u.replaceXY(this, common) && u.replaceXY(y, common)) {
if (target != null)
return u.putXY(common, target);
else
return true;
}
}
}
if (xOp.id < yOp.id) {
// only allows indep to subsume dep but not vice versa
if (u.varSymmetric)
return y.unify(this, u);
else
return false;
}
}
return u.matchType(xOp) && u.putXY(this, y);
// if (y instanceof Variable) {
// return subst.putXY(this, y);
// }
//
// if (subst.matchType(this)
// //&& !subst.matchType(y) //note: the !subst.matchType(y) subcondition is an attempt at preventing infinite cycles of variable references
// ) {
// return subst.putXY(this, y);
// }
//
// return false;
}
use of nars.Op in project narchy by automenta.
the class CommonVariable method common.
public static Variable common(Variable A, Variable B) {
// 1. sort
if (A.compareTo(B) < 0) {
Variable c = B;
B = A;
A = c;
}
Op Aop = A.op();
assert (B.op() == Aop);
boolean aa = A instanceof NormalizedVariable;
boolean bb = B instanceof NormalizedVariable;
if (aa && bb) {
byte ai = ((NormalizedVariable) A).anonNum();
byte bi = ((NormalizedVariable) B).anonNum();
return new CommonVariable(Aop, ai, bi);
}
if (!aa && bb) {
ImmutableByteSet ai = ((CommonVariable) A).vars;
byte bi = ((NormalizedVariable) B).anonNum();
if (ai.contains(bi))
return A;
return new CommonVariable(Aop, ai.newWith(bi));
}
if (aa && !bb) {
byte ai = ((NormalizedVariable) A).anonNum();
ImmutableByteSet bi = ((CommonVariable) B).vars;
if (bi.contains(ai))
return B;
return new CommonVariable(Aop, bi.newWith(ai));
}
/*if (!aa && !bb)*/
{
ImmutableByteSet ai = ((CommonVariable) A).vars;
ImmutableByteSet bi = ((CommonVariable) B).vars;
ImmutableByteSet combined = ai.newWithAll(bi);
if (combined.equals(ai))
return A;
return new CommonVariable(Aop, combined);
}
}
use of nars.Op in project narchy by automenta.
the class UnitCompound method append.
@Override
public void append(ByteArrayDataOutput out) {
Op o = op();
out.writeByte(o.id);
// avoids creating temporary Subterms instance:
// one subterm
out.writeByte(1);
sub().append(out);
if (o.temporal)
// can happen if ellipsis term
out.writeInt(dt());
}
use of nars.Op in project narchy by automenta.
the class Retemporalize method transformTemporal.
default Term transformTemporal(Compound x, int dtNext) {
Subterms xx = x.subterms();
if (x.dt() == dtNext && !xx.hasAny(Temporal))
// no change
return x;
Op op = x.op();
return TermTransform.NegObliviousTermTransform.super.transformCompound(x, op, dtNext);
}
use of nars.Op in project narchy by automenta.
the class TermTest method testFromEllipsisMatch.
@Test
public void testFromEllipsisMatch() {
Term xy = EllipsisMatch.match($.the("x"), $.the("y"));
for (Op o : new Op[] { Op.SECTi, SECTe, DIFFe, DIFFi, CONJ }) {
Term z = o.the(DTERNAL, xy);
assertEquals("(x" + o.str + "y)", z.toString());
assertEquals(3, z.volume());
assertEquals(Op.ATOM, z.sub(0).op());
assertEquals(Op.ATOM, z.sub(1).op());
}
}
Aggregations