Search in sources :

Example 11 with Op

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;
}
Also used : Op(nars.Op) Term(nars.term.Term)

Example 12 with Op

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);
    }
}
Also used : Op(nars.Op) ImmutableByteSet(org.eclipse.collections.api.set.primitive.ImmutableByteSet)

Example 13 with Op

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());
}
Also used : Op(nars.Op)

Example 14 with Op

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);
}
Also used : Subterms(nars.subterm.Subterms) Op(nars.Op)

Example 15 with Op

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());
    }
}
Also used : Op(nars.Op) Test(org.junit.jupiter.api.Test)

Aggregations

Op (nars.Op)36 Term (nars.term.Term)13 Subterms (nars.subterm.Subterms)11 Nullable (org.jetbrains.annotations.Nullable)8 Atomic (nars.term.atom.Atomic)4 Test (org.junit.jupiter.api.Test)4 FasterList (jcog.list.FasterList)3 NAR (nars.NAR)3 Termed (nars.term.Termed)3 Atom (nars.term.atom.Atom)3 Bool (nars.term.atom.Bool)3 Pair (org.eclipse.collections.api.tuple.Pair)3 NotNull (org.jetbrains.annotations.NotNull)3 java.io (java.io)2 java.util (java.util)2 List (java.util.List)2 Map (java.util.Map)2 Stream (java.util.stream.Stream)2 nars.$ (nars.$)2 Concept (nars.concept.Concept)2