use of nars.term.Termed in project narchy by automenta.
the class NARTest method testConceptInstancing.
//
// @Test public void testFork() throws Exception {
// NAR a = new Default();
// a.input("b:a.");
// a.input("c:b.");
// a.frame(8);
//
// NAR b = a.fork();
//
// assertEquals(a, b);
//
//
// }
@Test
public void testConceptInstancing() throws Narsese.NarseseException {
NAR n = NARS.tmp();
String statement1 = "<a --> b>.";
Termed a = $.$("a");
assertNotNull(a);
Termed a1 = $.$("a");
assertEquals(a, a1);
n.input(statement1);
n.run(4);
n.input(" <a --> b>. ");
n.run(1);
n.input(" <a--> b>. ");
n.run(1);
String statement2 = "<a --> c>.";
n.input(statement2);
n.run(4);
Termed a2 = $.$("a");
assertNotNull(a2);
Concept ca = n.conceptualize(a2);
assertNotNull(ca);
}
use of nars.term.Termed in project narchy by automenta.
the class CaffeineIndex2 method get.
@Override
public Termed get(Term x, boolean createIfMissing) {
if (x.volume() > nar.termVolumeMax.intValue())
// quick check to avoid creating a vector for a term that will be invalid anyway
return null;
assert (!(x instanceof Variable)) : "variables should not be stored in index";
Op op = x.op();
TermContainerToOpMap<Termed> v;
if (createIfMissing) {
v = vectors.get(vector(x), k -> {
TermContainerToOpMap<Termed> t = new TermContainerToOpMap<>(k);
Termed p = nar.conceptBuilder.apply(x, null);
if (p != null)
t.compareAndSet(op.id, null, p);
return t;
});
} else {
v = vectors.getIfPresent(vector(x));
}
return v != null ? v.get(op.id) : null;
}
use of nars.term.Termed in project narchy by automenta.
the class ConceptBuilder method apply.
@Nullable
default Termed apply(Term x) {
x = x.the();
Termed y = build(x);
if (y == null) {
return null;
}
Concept c = (Concept) y;
ConceptState s = c.state();
if (s == ConceptState.New || s == ConceptState.Deleted) {
c.state(init());
}
return c;
}
use of nars.term.Termed in project narchy by automenta.
the class Conclude method match.
public static void match(final DeriveRuleProto rule, List<PrediTerm<PreDerivation>> pre, List<PrediTerm<Derivation>> post, PatternIndex index, NAR nar) {
Term pattern = rule.conclusion().sub(0);
// TODO may interfere with constraints, functors, etc or other features, ie.
// if the pattern is a product for example?
// pattern = pattern.replace(ta, Derivation._taskTerm);
// determine if any cases where a shortcut like this can work (ie. no constraints, not a product etc)
// //substitute compound occurrences of the exact task and belief terms with the short-cut
// Term ta = rule.getTask();
// if (!ta.op().var) {
// if (pattern.equals(ta))
// pattern = Derivation.TaskTerm;
// }
// Term tb = rule.getBelief();
// if (!tb.op().var) {
// //pattern = pattern.replace(tb, Derivation._beliefTerm);
// if (pattern.equals(tb))
// pattern = Derivation.BeliefTerm;
// }
// HACK unwrap varIntro so we can apply it at the end of the derivation process, not before like other functors
boolean introVars1;
Pair<Termed, Term> outerFunctor = Op.functor(pattern, (i) -> i.equals(VAR_INTRO) ? VAR_INTRO : null);
if (outerFunctor != null) {
introVars1 = true;
pattern = outerFunctor.getTwo().sub(0);
} else {
introVars1 = false;
}
pattern = index.get(pattern, true).term();
Taskify taskify = new Taskify(nar.newCause((s) -> new RuleCause(rule, s)));
PrediTerm<Derivation> conc = AndCondition.the(new Termify($.func("derive", pattern), pattern, rule), introVars1 ? AndCondition.the(introVars, taskify) : taskify);
final Term taskPattern = rule.getTask();
final Term beliefPattern = rule.getBelief();
Op to = taskPattern.op();
boolean taskIsPatVar = to == Op.VAR_PATTERN;
Op bo = beliefPattern.op();
boolean belIsPatVar = bo == Op.VAR_PATTERN;
if (!taskIsPatVar) {
pre.add(new TaskBeliefOp(to, true, false));
pre.addAll(SubTermStructure.get(0, taskPattern.structure()));
}
if (!belIsPatVar) {
if (to == bo) {
pre.add(new AbstractPatternOp.TaskBeliefOpEqual());
} else {
pre.add(new TaskBeliefOp(bo, false, true));
pre.addAll(SubTermStructure.get(1, beliefPattern.structure()));
}
}
if (taskPattern.equals(beliefPattern)) {
post.add(new UnifyTerm.UnifySubtermThenConclude(0, taskPattern, conc));
}
if (taskFirst(taskPattern, beliefPattern)) {
// task first
post.add(new UnifyTerm.UnifySubterm(0, taskPattern));
post.add(new UnifyTerm.UnifySubtermThenConclude(1, beliefPattern, conc));
} else {
// belief first
post.add(new UnifyTerm.UnifySubterm(1, beliefPattern));
post.add(new UnifyTerm.UnifySubtermThenConclude(0, taskPattern, conc));
}
// Term beliefPattern = pattern.term(1);
// if (Global.DEBUG) {
// if (beliefPattern.structure() == 0) {
// if nothing else in the rule involves this term
// which will be a singular VAR_PATTERN variable
// then allow null
// if (beliefPattern.op() != Op.VAR_PATTERN)
// throw new RuntimeException("not what was expected");
// }
// }
/*System.out.println( Long.toBinaryString(
pStructure) + " " + pattern
);*/
}
use of nars.term.Termed in project narchy by automenta.
the class PatternIndex method get.
// /**
// * installs static and NAR-specific functors
// */
// public PatternIndex(NAR nar) {
// this();
// // for (Termed t : Derivation.ruleFunctors(nar)) {
// // set(t);
// // }
// }
@SuppressWarnings("Java8MapApi")
@Override
public Termed get(/*@NotNull*/
Term x, boolean createIfMissing) {
if (!x.op().conceptualizable)
return x;
// avoid recursion-caused concurrent modifiation exception
Termed y = concepts.get(x);
if (y == null) {
Term yy = x instanceof Compound ? patternify((Compound) x) : x;
concepts.put(yy, yy);
return yy;
} else {
return y;
}
}
Aggregations