use of nars.term.Compound in project narchy by automenta.
the class RawPerception method perceive.
@Override
public Iterable<Task> perceive(NAR nar, double[] input, double t) {
List<Task> tasks = new ArrayList(input.length);
// simple binary +/- 0 discretization
for (int i = 0; i < input.length; i++) {
float f = getFrequency(input[i]);
Compound x;
if (states.size() > i) {
x = states.get(i);
} else {
states.add(x = newState(nar, i));
}
tasks.add(TaskSeed.make(nar.memory, x).judgment().present().truth(f, confidence));
}
return tasks;
}
use of nars.term.Compound in project narchy by automenta.
the class DeriveRuleProto method build.
/**
* compiles the conditions which are necessary to activate this rule
*/
public Pair<Set<PrediTerm<PreDerivation>>, PrediTerm<Derivation>> build(PostCondition post) {
byte puncOverride = post.puncOverride;
TruthOperator belief = BeliefFunction.get(post.beliefTruth);
if ((post.beliefTruth != null) && !post.beliefTruth.equals(TruthOperator.NONE) && (belief == null)) {
throw new RuntimeException("unknown BeliefFunction: " + post.beliefTruth);
}
TruthOperator goal = GoalFunction.get(post.goalTruth);
if ((post.goalTruth != null) && !post.goalTruth.equals(TruthOperator.NONE) && (goal == null)) {
throw new RuntimeException("unknown GoalFunction: " + post.goalTruth);
}
// //if (puncOverride==0) {
// if (belief!=null && goal!=null) {
// if (!belief.single() && !goal.single()) {
// pre.add(TaskPolarity.belief);
// } else if (belief.single() ^ goal.single()){
// throw new TODO();
// }
// } else if (belief!=null && !belief.single()) {
// pre.add(TaskPolarity.belief);
// } else if (goal!=null && !goal.single()) {
// pre.add(TaskPolarity.belief);
// }
// TODO add more specific conditions that also work
// }
String beliefLabel = belief != null ? belief.toString() : "_";
String goalLabel = goal != null ? goal.toString() : "_";
FasterList<Term> args = new FasterList();
args.add($.the(beliefLabel));
args.add($.the(goalLabel));
if (puncOverride != 0)
args.add($.quote(((char) puncOverride)));
Compound ii = (Compound) $.func("truth", args.toArrayRecycled(Term[]::new));
Solve truth = (puncOverride == 0) ? new Solve.SolvePuncFromTask(ii, belief, goal) : new Solve.SolvePuncOverride(ii, puncOverride, belief, goal);
// PREFIX
// for ensuring uniqueness / no duplicates
Set<PrediTerm<PreDerivation>> precon = newHashSet(4);
addAll(precon, PRE);
precon.addAll(this.pre);
// //-------------------
// below here are predicates which affect the derivation
// SUFFIX (order already determined for matching)
int n = 1 + this.constraints.size() + this.post.size();
PrediTerm[] suff = new PrediTerm[n];
int k = 0;
suff[k++] = truth;
for (PrediTerm p : this.constraints) {
suff[k++] = p;
}
for (PrediTerm p : this.post) {
suff[k++] = p;
}
return pair(precon, (PrediTerm<Derivation>) AndCondition.the(suff));
}
use of nars.term.Compound in project narchy by automenta.
the class PostCondition method make.
/**
* @param rule rule which contains and is constructing this postcondition
* @param pattern
* @param modifiers
* @throws RuntimeException
*/
@NotNull
public static PostCondition make(DeriveRuleProto rule, Term pattern, @NotNull Term... modifiers) throws RuntimeException {
Term beliefTruth = null, goalTruth = null;
// boolean negate = false;
byte puncOverride = 0;
for (Term m : modifiers) {
if (m.op() != Op.INH)
throw new RuntimeException("Unknown postcondition format: " + m);
Term type = m.sub(1);
Term which = m.sub(0);
switch(type.toString()) {
case "Punctuation":
switch(which.toString()) {
case "Question":
puncOverride = Op.QUESTION;
break;
case "Goal":
puncOverride = Op.GOAL;
break;
case "Belief":
puncOverride = Op.BELIEF;
break;
case "Quest":
puncOverride = Op.QUEST;
break;
default:
throw new RuntimeException("unknown punctuation: " + which);
}
break;
case "Belief":
beliefTruth = which;
break;
case "Goal":
goalTruth = which;
break;
default:
throw new RuntimeException("Unhandled postcondition: " + type + ':' + which);
}
}
PostCondition pc = new PostCondition(pattern, beliefTruth, goalTruth, puncOverride);
if (!pc.modifiesPunctuation() && pattern instanceof Compound) {
assert !rule.getTask().equals(pattern) : "punctuation not modified yet rule task equals pattern: " + rule;
assert !rule.getBelief().equals(pattern) : "punctuation not modified yet rule belief equals pattern: " + rule + "\n\t" + rule.getBelief() + "\n\t" + pattern;
}
return pc;
}
use of nars.term.Compound in project narchy by automenta.
the class SubtermsTest method testDifferReusesInstance.
@Test
public void testDifferReusesInstance() throws Narsese.NarseseException {
Compound x = $("{x}");
Compound y = $("{y}");
assertSame(Op.difference(x.op(), x, y), x);
}
use of nars.term.Compound in project narchy by automenta.
the class TermVectorTest method testSubtermsEquality.
@Test
public void testSubtermsEquality() throws Narsese.NarseseException {
Term a = $.$("(a-->b)");
// return Atom.the(Utf8.toUtf8(name));
// int olen = name.length();
// switch (olen) {
// case 0:
// throw new RuntimeException("empty atom name: " + name);
//
// // //re-use short term names
// // case 1:
// // case 2:
// // return theCached(name);
//
// default:
// if (olen > Short.MAX_VALUE/2)
// throw new RuntimeException("atom name too long");
// }
// return Atom.the(Utf8.toUtf8(name));
// int olen = name.length();
// switch (olen) {
// case 0:
// throw new RuntimeException("empty atom name: " + name);
//
// // //re-use short term names
// // case 1:
// // case 2:
// // return theCached(name);
//
// default:
// if (olen > Short.MAX_VALUE/2)
// throw new RuntimeException("atom name too long");
// }
Compound b = $.impl(Atomic.the("a"), Atomic.the("b"));
assertEquals(a.subterms(), b.subterms());
assertEquals(a.subterms().hashCode(), b.subterms().hashCode());
assertNotEquals(a, b);
assertNotEquals(a.hashCode(), b.hashCode());
assertEquals(0, Subterms.compare(a.subterms(), b.subterms()));
assertEquals(0, Subterms.compare(b.subterms(), a.subterms()));
assertNotEquals(0, a.compareTo(b));
assertNotEquals(0, b.compareTo(a));
/*assertTrue("after equality test, subterms vector determined shareable",
a.subterms() == b.subterms());*/
}
Aggregations