use of jcog.TODO in project narchy by automenta.
the class MatchConstraint method combineConstraints.
public static PrediTerm<Derivation> combineConstraints(AndCondition a) {
RoaringBitmap constraints = new RoaringBitmap();
@NotNull PrediTerm[] cond1 = a.cond;
for (int i = 0, cl = cond1.length; i < cl; i++) {
Term x = cond1[i];
if (x instanceof MatchConstraint) {
constraints.add(i);
}
}
if (constraints.getCardinality() < 2) {
return a;
} else {
// identify contiguous runs of constraints
// inclusive
List<IntIntPair> ranges = new FasterList<>(1);
int start = -1, end = -1;
PeekableIntIterator ii = constraints.getIntIterator();
while (ii.hasNext()) {
int next = ii.next();
if (start == -1) {
start = end = next;
} else {
if (next == end + 1) {
end++;
} else {
if (end - start >= 1) {
// compile that range
ranges.add(pair(start, end));
}
// broken
start = -1;
}
}
}
if (end - start >= 1)
ranges.add(pair(start, end));
if (ranges.size() > 1)
throw new TODO();
IntIntPair rr = ranges.get(0);
List<PrediTerm<Derivation>> l = new FasterList();
int i;
for (i = 0; i < start; i++) {
l.add(a.cond[i]);
}
CompoundConstraint.the(Util.map(MatchConstraint.class::cast, MatchConstraint[]::new, ArrayUtils.subarray(a.cond, rr.getOne(), rr.getTwo() + 1))).forEach(l::add);
i = end + 1;
for (; i < a.cond.length; i++) {
l.add(a.cond[i]);
}
return AndCondition.the((List) l);
}
}
use of jcog.TODO in project narchy by automenta.
the class PrologToNAL method N.
private static nars.term.Term N(alice.tuprolog.Term t) {
if (t instanceof alice.tuprolog.Term) {
Struct s = (Struct) t;
String name = s.name();
switch(name) {
case ":-":
assert (s.subs() == 2);
// reverse, prolog is backwards
nars.term.Term pre = N(s.sub(1));
nars.term.Term post = N(s.sub(0));
// convert to implication first, then promote variables on the resulting pre/post
Term impl = $.impl(pre, post);
pre = impl.sub(0);
post = impl.sub(1);
if (pre.varQuery() > 0 && post.varQuery() > 0) {
MutableSet<nars.term.var.Variable> prev = new UnifiedSet();
pre.recurseTerms(Termlike::hasVarQuery, (a) -> {
if (a.op() == Op.VAR_QUERY)
prev.add((Variable) a);
return true;
}, null);
MutableSet<nars.term.var.Variable> posv = new UnifiedSet();
post.recurseTerms(Termlike::hasVarQuery, (a) -> {
if (a.op() == Op.VAR_QUERY)
posv.add((Variable) a);
return true;
}, null);
MutableSet<nars.term.var.Variable> common = prev.intersect(posv);
int cs = common.size();
if (cs > 0) {
Map<nars.term.Term, nars.term.Term> x = new UnifiedMap(cs);
for (nars.term.var.Variable c : common) {
x.put(c, $.varIndep(c.toString().substring(1)));
}
impl = impl.replace(x);
}
}
return impl;
case ",":
return CONJ.the(N(s.sub(0)), N(s.sub(1)));
default:
nars.term.Term atom = $.the(name);
int arity = s.subs();
if (arity == 0) {
return atom;
} else {
return $.inh($.p((nars.term.Term[]) Util.map(0, arity, i -> N(s.sub(i)), nars.term.Term[]::new)), atom);
}
}
} else if (t instanceof Var) {
return $.varQuery(((Var) t).name());
// throw new RuntimeException(t + " untranslated");
} else if (t instanceof NumberTerm.Int) {
return $.the(((NumberTerm.Int) t).intValue());
} else {
throw new TODO(t + " (" + t.getClass() + ") untranslatable");
}
}
use of jcog.TODO in project narchy by automenta.
the class TasksView method layoutTimeline.
public void layoutTimeline() {
final MapNodeGraph<Surface, String> graph = new MapNodeGraph();
float tScale = 100;
float tMin = tScale;
for (Surface cc : children()) {
// TODO make window content iteration method
TaskIcon c = (TaskIcon) cc;
Task t = c.task;
NodeGraph.MutableNode<Surface, String> tn = graph.addNode(c);
for (long e : t.stamp()) {
NodeGraph.Node en = graph.node(evidences.getIfAbsentPutWithKey(e, (ee) -> {
Surface s = new PushButton("_" + ee);
// TODO make evidence buttons visibility toggleable
// children.add(s);
graph.addNode(s);
return s;
}));
graph.addEdge((NodeGraph.MutableNode) en, "stamp", tn);
}
float minH = 30;
float maxH = 200;
float h = t.isQuestOrQuestion() ? Util.lerp(t.originality() / 2f, minH, maxH) : Util.lerp(t.originality() * t.conf(), minH, maxH);
long start, end;
if (!t.isEternal()) {
start = t.start();
end = t.end();
} else {
start = t.creation();
// TODO max time
end = 10;
}
float x1 = start * tScale;
float x2 = end * tScale;
if (x2 - x1 < tMin) {
float x = (x1 + x2) / 2f;
x1 = x - tMin / 2;
x2 = x + tMin / 2;
}
float y = (float) (Math.random() * 500);
c.pos(x1, y, x2, y + h);
}
graph.print();
new Thread(() -> {
int iterations = 300;
for (int i = 0; i < iterations; i++) {
layoutForceDirect(graph);
layout();
Util.sleep(5);
}
}).start();
}
use of jcog.TODO in project narchy by automenta.
the class TimeGraph method solveDT.
boolean solveDT(Term x, Predicate<Event> each) {
assert (x.dt() == XTERNAL);
Subterms xx = x.subterms();
// FasterList<Event> events = new FasterList<>(byTerm.get(x.root()));
// for (int i = 0, eventsSize = events.size(); i < eventsSize; i++) {
// Event r = events.get(i);
// if (r instanceof Absolute) {
// if (r.id.subterms().equals(xx)) {
// if (!each.test(r))
// return false; //done
// }
// }
//
// }
int subs = xx.subs();
if (subs == 2) {
Term a = xx.sub(0);
Term b = xx.sub(1);
boolean aEqB = a.equals(b);
if (!a.hasXternal() && !b.hasXternal() && (aEqB || !commonSubEventsWithMultipleOccurrences(a, b))) {
UnifiedSet<Event> ae = new UnifiedSet(2);
solveOccurrence(event(a, TIMELESS), ax -> {
if (ax instanceof Absolute)
ae.add(ax);
return true;
});
int aes = ae.size();
if (aes > 0) {
if (aEqB) {
// same term, must have >1 absolute timepoints
if (aes > 1) {
Event[] ab = ae.toArray(new Event[aes]);
// Arrays.sort(ab, Comparator.comparingLong(Event::when));
for (int i = 0; i < ab.length; i++) {
Event abi = ab[i];
for (int j = 0; j < ab.length; j++) {
if (i == j)
continue;
if (!solveDT(x, abi.when(), dt(x, abi, ab[j]), each))
return false;
}
}
}
} else {
UnifiedSet<Event> be = new UnifiedSet(2);
solveOccurrence(event(b, TIMELESS), bx -> {
if (bx instanceof Absolute)
be.add(bx);
return true;
});
int bes = be.size();
if (bes > 0) {
// if (aes == 1 || bes == 1) {
if (!ae.allSatisfy(ax -> be.allSatisfyWith((bx, axx) -> solveDT(x, axx.when(), dt(x, axx, bx), each), ax)))
return false;
// }
}
}
}
}
// UnifiedSet<Event>[] abs = new UnifiedSet[2]; //exact occurrences of each subterm
FasterList<Event> rels = new FasterList<>(4);
// int[] phase = new int[]{0};
// int p = phase[0];
// if (z instanceof Absolute) {
// if (abs[p] == null) abs[p] = new UnifiedSet(2);
// abs[p].add(z);
// //}
// }
Consumer<Event> collect = rels::add;
byTerm.get(a).forEach(collect);
if (aEqB) {
// abs[1] = abs[0];
} else {
// phase[0] = 1;
byTerm.get(b).forEach(collect);
// if (abs[1] == null)
// byTerm.get(b.neg()).forEach(collect); //if nothing, look for negations
}
// if (abs[0] != null && abs[1] != null) {
// known exact occurrences for both subterms
// iterate all possibilities
// TODO order in some way
// TODO other simple cases: 1 -> N
// if (abs[0].size() == 1 && abs[1].size() == 1) {
// //simple case:
// Event aa = abs[0].iterator().next();
// Event bb = abs[1].iterator().next();
// if (!solveDT(x, each, aa, bb))
// return false;
// } else {
// if (!abs[0].allSatisfy(ae ->
// abs[1].allSatisfyWith((be, aaee) ->
// solveDT(x, each, aaee, be), ae)))
// return false;
// }
// }
int ns = rels.size();
if (ns > 0) {
if (ns > 1) {
// sort by volume
rels.sortThisByInt(s -> s.id.volume());
}
return bfs(rels, new CrossTimeSolver() {
@Override
protected boolean next(BooleanObjectPair<ImmutableDirectedEdge<Event, TimeSpan>> move, Node<nars.derive.time.TimeGraph.Event, nars.derive.time.TimeGraph.TimeSpan> next) {
// System.out.println(path);
long[] startDT = pathDT(next, a, b, path);
if (startDT == null)
// nothing at this step
return true;
long start = startDT[0];
long ddt = startDT[1];
return TimeGraph.this.solveDT(x, start, ddt, each);
}
});
}
// } else {
// assert (x.op() == CONJ);
// List<LongObjectPair<Term>> when = $.newArrayList();
// for (int ix = 0; ix < subs; ix++) {
// //assert(!z.hasXternal());
// solveOccurrence(event(xx.sub(ix), TIMELESS), (ze) -> {
// if (ze.when() == TIMELESS)
// return true; //keep trying
// when.add(pair(ze.when(), ze.id));
// return false; //just one, for now //TODO see if there are any others
// });
// }
// if (when.size() == subs) {
// when.sort(Comparator.comparingLong(LongObjectPair::getOne));
// long base = when.get(0).getOne();
// Term zz = when.get(0).getTwo();
// for (int i = 1; i < subs; i++) {
// LongObjectPair<Term> wgi = when.get(i);
// zz = Op.conjMerge(zz, 0, wgi.getTwo(), wgi.getOne() - base);
// if (zz instanceof Bool)
// return true; //failure
// }
// return each.test(event(zz, base));
// }
}
// last resort
return each.test(event(x, TIMELESS));
}
use of jcog.TODO 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