use of nars.language.CompoundTerm in project opennars by opennars.
the class ApplySubstituteTest method testApplySubstitute.
@Test
public void testApplySubstitute() throws Narsese.InvalidInputException {
String abS = "<a --> b>";
CompoundTerm ab = (CompoundTerm) np.parseTerm(abS);
int originalComplexity = ab.getComplexity();
String xyS = "<x --> y>";
Term xy = np.parseTerm(xyS);
Map<Term, Term> h = new HashMap();
h.put(np.parseTerm("b"), xy);
CompoundTerm c = ab.applySubstituteToCompound(h);
assertTrue(c.getComplexity() > originalComplexity);
// ab unmodified
assertTrue(ab.name().toString().equals(abS));
// c is actually different
assertTrue(!c.name().equals(abS));
assertTrue(!c.equals(ab));
}
use of nars.language.CompoundTerm in project opennars by opennars.
the class ApplySubstituteTest method test2.
@Test
public void test2() throws Narsese.InvalidInputException {
// substituting: <(*,$1) --> num>. with $1 ==> 0
NAR n = new NAR();
Map<Term, Term> h = new HashMap();
h.put(np.parseTerm("$1"), np.parseTerm("0"));
CompoundTerm c = ((CompoundTerm) np.parseTerm("<(*,$1) --> num>")).applySubstituteToCompound(h);
assertTrue(c != null);
}
use of nars.language.CompoundTerm in project opennars by opennars.
the class TermTest method invalidTermIndep.
@Test
public void invalidTermIndep() {
String t = "<$1 --> (~,{place4},$1)>";
NAR n = new NAR();
Narsese p = new Narsese(n);
try {
p.parseNarsese(new StringBuilder(t + "."));
assertTrue(false);
} catch (Narsese.InvalidInputException ex) {
assertTrue(true);
}
Term subj = null, pred = null;
try {
subj = p.parseTerm("$1");
pred = p.parseTerm("(~,{place4},$1)");
assertTrue(true);
} catch (Narsese.InvalidInputException ex) {
assertTrue(false);
}
Statement s = Statement.make(NativeOperator.INHERITANCE, subj, pred, false, 0);
assertEquals(null, s);
Inheritance i = Inheritance.make(subj, pred);
assertEquals(null, i);
try {
CompoundTerm forced = (CompoundTerm) p.parseTerm("<a --> b>");
assertTrue(true);
forced.term[0] = subj;
forced.term[1] = pred;
forced.invalidateName();
assertEquals(t, forced.toString());
CompoundTerm cloned = forced.clone();
assertEquals(null, cloned);
} catch (Narsese.InvalidInputException ex) {
assertTrue(false);
}
}
use of nars.language.CompoundTerm in project opennars by opennars.
the class TermTest method assertEquivalent.
protected void assertEquivalent(String term1String, String term2String) {
try {
NAR n = new NAR();
Term term1 = np.parseTerm(term1String);
Term term2 = np.parseTerm(term2String);
assertTrue(term1 instanceof CompoundTerm);
assertTrue(term2 instanceof CompoundTerm);
assert (!term1String.equals(term2String));
assert (term1.hashCode() == term2.hashCode());
assert (term1.equals(term2));
assert (term1.compareTo(term2) == 0);
} catch (Exception e) {
assertTrue(e.toString(), false);
}
}
use of nars.language.CompoundTerm in project opennars by opennars.
the class Count method function.
@Override
protected Term function(Memory memory, Term[] x) {
if (x.length != 1) {
throw new RuntimeException(requireMessage);
}
Term content = x[0];
if (!(content instanceof SetExt) && !(content instanceof SetInt)) {
throw new RuntimeException(requireMessage);
}
int n = ((CompoundTerm) content).size();
return Term.get(n);
}
Aggregations