use of nars.Task in project narchy by automenta.
the class RTreeBeliefTable method mergeOrDelete.
private boolean mergeOrDelete(Space<TaskRegion> treeRW, Top2<Leaf<TaskRegion>> l, FloatFunction<Task> taskStrength, float inputStrength, FloatFunction<TaskRegion> weakestTasks, Consumer<Tasked> added, NAR nar) {
TaskRegion a, b;
Leaf<TaskRegion> la = l.a;
short sa = la.size;
if (sa > 2) {
Top2<TaskRegion> w = new Top2<>(weakestTasks);
la.forEach(w::add);
a = w.a;
b = w.b;
} else if (sa == 2) {
a = la.get(0);
b = la.get(1);
} else {
a = la.get(0);
Leaf<TaskRegion> lb = l.b;
if (lb != null) {
int sb = lb.size();
if (sb > 1) {
Top<TaskRegion> w = new Top<>(weakestTasks);
lb.forEach(w);
b = w.the;
} else if (sb == 1) {
b = lb.get(0);
} else {
// ??
b = null;
}
} else {
b = null;
}
}
assert (a != null);
Task at = a.task();
float aPri = at.pri();
treeRW.remove(at);
if (b != null) {
Task bt = b.task();
if (bt.isDeleted()) {
treeRW.remove(bt);
return true;
} else {
at.meta("@", bt);
}
if (// already deleted
aPri != aPri)
return true;
Task c = // HACK
(this instanceof Simple || (at.term().equals(bt.term()))) ? Revision.mergeTemporal(nar, at, bt) : // TODO remove this when the mergeTemporal fully supports CONJ and Temporal
Revision.merge(at, bt, nar.time(), c2wSafe(nar.confMin.floatValue()), nar);
if (c != null && !c.equals(a) && !c.equals(b)) {
boolean allowMerge;
if (inputStrength != inputStrength) {
allowMerge = true;
} else {
float strengthRemoved = taskStrength.floatValueOf(at) + taskStrength.floatValueOf(bt);
float strengthAdded = taskStrength.floatValueOf(c) + inputStrength;
allowMerge = strengthAdded >= strengthRemoved;
}
if (allowMerge) {
treeRW.remove(bt);
// forward
((NALTask) at).delete(c);
// forward
((NALTask) bt).delete(c);
if (treeRW.add(c))
added.accept(c);
return true;
} else {
// merge result is not strong enough
c.delete();
}
}
}
// TODO do this outside of the locked section
if (Param.ETERNALIZE_EVICTED_TEMPORAL_TASKS)
eternalize(at, added, nar);
at.delete();
return true;
}
use of nars.Task in project narchy by automenta.
the class RelationClustering method link.
@Override
protected void link(Task tx, Task ty) {
// TODO abstract
assert (tx.isBelief() && ty.isBelief());
// TODO Allen interval
String relation;
if (tx.intersects(ty.start(), ty.end())) {
relation = "simul";
} else if (ty.isAfter(tx.end(), dur / 2)) {
relation = "seq";
} else if (tx.isAfter(ty.end(), dur / 2)) {
Task z = tx;
tx = ty;
ty = z;
relation = "seq";
} else {
relation = null;
}
if (relation != null) {
Term x = tx.term();
Truth truX = tx.truth();
if (truX.isNegative()) {
x = x.neg();
truX = truX.neg();
}
Term y = ty.term();
Truth truY = ty.truth();
if (truY.isNegative()) {
y = y.neg();
truY = truY.neg();
}
if (x.volume() + y.volume() < nar.termVolumeMax.intValue() - 2) {
Truth tru = TruthFunctions.intersection(truX, truY, nar.confMin.floatValue());
if (tru == null)
return;
// TODO enum
Term t;
switch(relation) {
case "simul":
t = $.inh(SETe.the(x, y), $.the("simul"));
break;
case "seq":
t = $.func(relation, x, y);
break;
default:
throw new UnsupportedOperationException();
}
if (t instanceof Bool)
return;
t = t.normalize();
long now = nar.time();
NALTask tt = new NALTask(t, BELIEF, tru, now, Math.min(tx.start(), ty.start()), Math.max(tx.end(), ty.end()), nar.time.nextStampArray());
tt.pri(tx.priElseZero() * ty.priElseZero());
in.input(tt);
}
}
}
use of nars.Task in project narchy by automenta.
the class STMLinkage method accept.
@Override
public final void accept(NAR nar, Task t) {
if (!t.isBeliefOrGoal() || !stmLinkable(t))
return;
float strength = this.strength.floatValue();
float tPri = t.priElseZero();
// short cid = cause.id;
float p = strength * tPri;
for (Task u : stm) {
// skip null's and dummy's
if (u == null)
continue;
link(t, p * u.priElseZero(), u, cause.id, nar);
}
stm.poll();
stm.offer(t);
}
use of nars.Task in project narchy by automenta.
the class Signal method set.
public Task set(Concept c, @Nullable Truth nextTruth, LongSupplier stamper, long now, int dur, NAR nar) {
@Nullable Truth tt = // nextTruth;
nextTruth != null ? nextTruth.dither(nar) : null;
final boolean[] keep = { false };
Task cur = current.updateAndGet((last) -> {
if (last == null && nextTruth == null)
return null;
TruthletTask next;
int gapFillTolerance = dur * 2;
if (tt == null) {
// no signal
next = null;
} else {
if (last == null || last.isDeleted() || ((!tt.equals(last.truth(last.end()), nar) || (now - last.end() > gapFillTolerance)) || (Param.SIGNAL_LATCH_TIME_MAX != Integer.MAX_VALUE && now - last.start() >= dur * Param.SIGNAL_LATCH_TIME_MAX))) {
long beforeNow = (last != null && (now - last.end()) < gapFillTolerance) ? last.end() : now;
// TODO move the task construction out of this critical update section?
next = taskStart(c, last, tt, beforeNow, now + lookAheadDurs * dur, stamper.getAsLong(), dur);
} else {
// nothing, keep as-is
next = last;
}
}
if (last == next) {
if (last != null) {
last.pri(pri.asFloat());
last.updateEnd(c, now + lookAheadDurs * dur);
keep[0] = true;
}
// dont re-input the task, just stretch it where it is in the temporal belief table
return last;
} else {
// new or null input; stretch will be assigned on first insert to the belief table (if this happens)
return next;
}
// } finally {
// busy.set(false);
// }
});
return keep[0] ? null : cur;
}
use of nars.Task in project narchy by automenta.
the class AtomicExec method apply.
@Override
@Nullable
public Task apply(Task x, NAR n) {
// TODO handle CMD's
Task y = exePrefilter(x);
if (y == null)
// pass-through to reasoner
return x;
if (y != x)
// transformed
return y;
x = y;
if (x.isCommand()) {
// immediately execute
exe.accept(x.term(), n);
// absorbed
return null;
} else {
active.put(new PLink(x.term().concept(), /* incase it contains temporal, we will dynamically match task anyway on invocation */
x.priElseZero()));
enable(n);
return x;
}
}
Aggregations