Search in sources :

Example 1 with INT

use of nars.Op.INT in project narchy by automenta.

the class DefaultTermizer method obj2term.

@Nullable
Term obj2term(@Nullable Object o) {
    if (o == null)
        return NULL;
    if (o instanceof Term)
        return (Term) o;
    if (o instanceof String)
        return $.quote(o);
    if (o instanceof Boolean)
        return ((Boolean) o) ? Termizer.TRUE : Termizer.FALSE;
    if (o instanceof Character)
        return $.quote(String.valueOf(o));
    if (o instanceof Number)
        return number((Number) o);
    if (o instanceof Class) {
        Class oc = (Class) o;
        return classTerm(oc);
    // if (metadata) {
    // Package p = oc.getPackage();
    // if (p != null) {
    // 
    // Term cterm = termClassInPackage(oc);
    // 
    // if (reportClassInPackage(oc)) { //TODO use a method for other class exclusions
    // Term pkg = packages.get(p);
    // if (pkg == null) {
    // pkg = termPackage(p);
    // packages.put(p, pkg);
    // termClassInPackage(cterm, PACKAGE);
    // }
    // 
    // //TODO add recursive superclass ancestry?
    // }
    // 
    // return cterm;
    // }
    // }
    // return PRIMITIVE;
    }
    if (o instanceof int[]) {
        return $.p((int[]) o);
    }
    // noinspection IfStatementWithTooManyBranches
    if (o instanceof Object[]) {
        List<Term> arg = Arrays.stream((Object[]) o).map(this::term).collect(Collectors.toList());
        if (arg.isEmpty())
            return EMPTY;
        return $.p(arg);
    }
    if (o instanceof List) {
        if (((Collection) o).isEmpty())
            return EMPTY;
        // TODO can this be done with an array to avoid duplicate collection allocation
        Collection c = (Collection) o;
        List<Term> arg = $.newArrayList(c.size());
        for (Object x : c) {
            Term y = term(x);
            arg.add(y);
        }
        if (arg.isEmpty())
            return EMPTY;
        return $.p(arg);
    /*} else if (o instanceof Stream) {
            return Atom.quote(o.toString().substring(17));
        }*/
    }
    if (o instanceof Set) {
        Collection<Term> arg = (Collection<Term>) ((Collection) o).stream().map(this::term).collect(Collectors.toList());
        if (arg.isEmpty())
            return EMPTY;
        return $.sete(arg);
    } else if (o instanceof Map) {
        Map mapo = (Map) o;
        Set<Term> components = $.newHashSet(mapo.size());
        mapo.forEach((k, v) -> {
            Term tv = obj2term(v);
            Term tk = obj2term(k);
            if ((tv != null) && (tk != null)) {
                components.add($.inh(tv, tk));
            }
        });
        if (components.isEmpty())
            return EMPTY;
        return $.sete(components);
    }
    return instanceTerm(o);
// //ensure package is term'ed
// String pname = p.getName();
// int period = pname.length()-1;
// int last = period;
// Term child = cterm;
// while (( period = pname.lastIndexOf('.', period)) != -1) {
// String parname = pname.substring(0, last);
// Term parent = packages.get(parname);
// if (parent == null) {
// parent = Atom.the(parname);
// nar.believe( Inheritance.make(child, parent) );
// packages.put()
// last = period;
// child = parent;
// }
// else {
// break;
// }
// }
}
Also used : java.util(java.util) ImmutableSet(com.google.common.collect.ImmutableSet) CustomConcurrentHashMap(jcog.data.map.CustomConcurrentHashMap) nars.$(nars.$) Field(java.lang.reflect.Field) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Util(jcog.Util) Variable(nars.term.var.Variable) Nullable(org.jetbrains.annotations.Nullable) TODO(jcog.TODO) Int(nars.term.atom.Int) Modifier(java.lang.reflect.Modifier) PROD(nars.Op.PROD) Atomic(nars.term.atom.Atomic) INT(nars.Op.INT) NotNull(org.jetbrains.annotations.NotNull) Method(java.lang.reflect.Method) Term(nars.term.Term) ImmutableSet(com.google.common.collect.ImmutableSet) Term(nars.term.Term) CustomConcurrentHashMap(jcog.data.map.CustomConcurrentHashMap) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 java.util (java.util)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 TODO (jcog.TODO)1 Util (jcog.Util)1 CustomConcurrentHashMap (jcog.data.map.CustomConcurrentHashMap)1 nars.$ (nars.$)1 INT (nars.Op.INT)1 PROD (nars.Op.PROD)1 Term (nars.term.Term)1 Atomic (nars.term.atom.Atomic)1 Int (nars.term.atom.Int)1 Variable (nars.term.var.Variable)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1