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;
// }
// }
}
Aggregations