use of org.eclipse.collections.impl.set.mutable.UnifiedSet in project narchy by automenta.
the class Subterms method toSet.
/**
* @return a Mutable Set, unless empty
*/
default /*@NotNull*/
MutableSet<Term> toSet() {
int s = subs();
UnifiedSet u = new UnifiedSet(s);
if (s > 0) {
forEach(u::add);
u.trimToSize();
}
return u;
// return new DirectArrayUnenforcedSet<Term>(Terms.sorted(toArray())) {
// @Override
// public boolean removeIf(Predicate<? super Term> filter) {
//
// return false;
// }
// };
}
use of org.eclipse.collections.impl.set.mutable.UnifiedSet 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 org.eclipse.collections.impl.set.mutable.UnifiedSet 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));
}
Aggregations