Search in sources :

Example 6 with Atomic

use of nars.term.atom.Atomic in project narchy by automenta.

the class PrologCore method pterm.

// NARS term -> Prolog term
public static alice.tuprolog.Term pterm(final Term term) {
    if (term instanceof Compound) {
        Op op = term.op();
        alice.tuprolog.Term[] st = psubterms(((Compound) term));
        switch(op) {
            case IMPL:
                return new Struct(":-", st[1], st[0]);
            case CONJ:
                return new Struct(",", st);
            case NEG:
                return new Struct(/*"\\="*/
                "not", st);
            case PROD:
                // list
                return new Struct(st);
            case INH:
                Term pred = term.sub(1);
                if (pred.op() == ATOM) {
                    Term subj = term.sub(0);
                    if (subj.op() == PROD) {
                        alice.tuprolog.Term args = st[0];
                        return new Struct(wrapAtom(pred.toString()), args instanceof alice.tuprolog.Term ? Iterators.toArray(((Struct) st[0]).listIterator(), alice.tuprolog.Term.class) : new alice.tuprolog.Term[] { args });
                    }
                }
                break;
        }
        return new Struct(op.str, st);
    } else if (term instanceof NormalizedVariable) {
        switch(term.op()) {
            case VAR_QUERY:
            case VAR_PATTERN:
            // ?? as if atomic
            case VAR_DEP:
            case // ??
            VAR_INDEP:
                return new Var("_" + (((NormalizedVariable) term).anonNum()));
        }
    } else if (term instanceof Atomic) {
        return new Struct(wrapAtom(term.toString()));
    }
    throw new UnsupportedOperationException();
// //CharSequence s = termString(term);
// if (term instanceof Statement) {
// Statement i = (Statement)term;
// String predicate = classPredicate(i.getClass());
// alice.tuprolog.Term  subj = alice.tuprolog.Term (i.getSubject());
// alice.tuprolog.Term  obj = alice.tuprolog.Term (i.getPredicate());
// if ((subj!=null) && (obj!=null))
// return new Struct(predicate, subj, obj);
// }
// else if ((term instanceof SetTensional) || (term instanceof Product) /* conjunction */) {
// Compound s = (Compound)term;
// String predicate = classPredicate(s.getClass());
// alice.tuprolog.Term [] args = alice.tuprolog.Term s(s.term);
// if (args!=null)
// return new Struct(predicate, args);
// }
// //Image...
// //Conjunction...
// else if (term instanceof Negation) {
// alice.tuprolog.Term  np = alice.tuprolog.Term (((Negation)term).term[0]);
// if (np == null) return null;
// return new Struct("negation", np);
// }
// else if (term.getClass().equals(Variable.class)) {
// return getVariable((Variable)term);
// }
// else if (term.getClass().equals(Atom.class)) {
// return new Struct(pescape(term.toString()));
// }
// else if (term instanceof Compound) {
// //unhandled type of compound term, store as an atomic string
// //NOT ready yet
// if (allTerms) {
// return new Struct('_' + pescape(term.toString()));
// }
// }
// 
// return null;
}
Also used : Op(nars.Op) alice.tuprolog(alice.tuprolog) Compound(nars.term.Compound) NormalizedVariable(nars.term.var.NormalizedVariable) Atomic(nars.term.atom.Atomic) Term(nars.term.Term)

Example 7 with Atomic

use of nars.term.atom.Atomic in project narchy by automenta.

the class IO method termToBytes.

public static byte[] termToBytes(Term t) {
    if (t instanceof Atomic) {
        return ((Atomic) t).bytes();
    }
    // bb = ArrayPool.bytes().
    DynBytes d = new DynBytes(t.volume() * 4);
    // ByteArrayOutputStream bs = new ByteArrayOutputStream();
    t.append((ByteArrayDataOutput) d);
    // bs.toByteArray();
    return d.array();
}
Also used : DynBytes(jcog.data.byt.DynBytes) Atomic(nars.term.atom.Atomic)

Example 8 with Atomic

use of nars.term.atom.Atomic in project narchy by automenta.

the class IO method readAtomic.

/**
 * direct method of reading Atomic from a byte[]
 */
public static Atomic readAtomic(byte[] b) {
    byte oo = b[0];
    if (oo == SPECIAL_OP)
        return (Atomic) termFromBytes(b);
    Op o = ov[oo];
    switch(o) {
        case ATOM:
            return new Atom(b);
        case VAR_PATTERN:
        case VAR_DEP:
        case VAR_QUERY:
        case VAR_INDEP:
            return $.v(o, b[1]);
        case INT:
            // }
            return (Atomic) termFromBytes(b);
        default:
            // throw new TODO();
            return (Atomic) termFromBytes(b);
    }
}
Also used : Op(nars.Op) Atomic(nars.term.atom.Atomic) Atom(nars.term.atom.Atom)

Example 9 with Atomic

use of nars.term.atom.Atomic in project narchy by automenta.

the class UnitCompoundTest method testUnitCompound_viaProd.

@Test
public void testUnitCompound_viaProd() {
    Atomic x = Atomic.the("x");
    assertEqual(PROD, x, new CachedUnitCompound(PROD, x));
}
Also used : Atomic(nars.term.atom.Atomic) Test(org.junit.jupiter.api.Test)

Example 10 with Atomic

use of nars.term.atom.Atomic in project narchy by automenta.

the class UnitCompoundTest method testCachedUnitCompound1.

@Test
public void testCachedUnitCompound1() {
    Atomic x = Atomic.the("x");
    assertEqual(PROD, x, new CachedUnitCompound(PROD, x));
}
Also used : Atomic(nars.term.atom.Atomic) Test(org.junit.jupiter.api.Test)

Aggregations

Atomic (nars.term.atom.Atomic)12 Test (org.junit.jupiter.api.Test)7 Op (nars.Op)3 Term (nars.term.Term)3 Compound (nars.term.Compound)2 alice.tuprolog (alice.tuprolog)1 Field (java.lang.reflect.Field)1 DynBytes (jcog.data.byt.DynBytes)1 NAR (nars.NAR)1 NARS (nars.NARS)1 INT (nars.Op.INT)1 Ellipsis (nars.derive.match.Ellipsis)1 Ellipsis.firstEllipsis (nars.derive.match.Ellipsis.firstEllipsis)1 EllipsisOneOrMore (nars.derive.match.EllipsisOneOrMore)1 MaplikeConceptIndex (nars.index.term.map.MaplikeConceptIndex)1 Atom (nars.term.atom.Atom)1 NormalizedVariable (nars.term.var.NormalizedVariable)1 NotNull (org.jetbrains.annotations.NotNull)1