use of nars.main.NAR in project opennars by opennars.
the class TermTest method testTermSort.
@Test
public void testTermSort() throws Exception {
NAR n = new NAR();
Narsese m = new Narsese(n);
Term a = m.parseTerm("a");
Term b = m.parseTerm("b");
Term c = m.parseTerm("c");
assertEquals(3, Term.toSortedSetArray(a, b, c).length);
assertEquals(2, Term.toSortedSetArray(a, b, b).length);
assertEquals(1, Term.toSortedSetArray(a, a).length);
assertEquals(1, Term.toSortedSetArray(a).length);
assertEquals("correct natural ordering", a, Term.toSortedSetArray(a, b)[0]);
}
use of nars.main.NAR 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.main.NAR 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.main.NAR in project opennars by opennars.
the class TermTest method testConjunctionTreeSet.
@Test
public void testConjunctionTreeSet() throws Narsese.InvalidInputException {
NAR n = new NAR();
// these 2 representations are equal, after natural ordering
String term1String = "<#1 --> (&,boy,(/,taller_than,{Tom},_))>";
Term term1 = np.parseTerm(term1String);
String term1Alternate = "<#1 --> (&,(/,taller_than,{Tom},_),boy)>";
Term term1a = np.parseTerm(term1Alternate);
// <#1 --> (|,boy,(/,taller_than,{Tom},_))>
Term term2 = np.parseTerm("<#1 --> (|,boy,(/,taller_than,{Tom},_))>");
assertTrue(term1.toString().equals(term1a.toString()));
assertTrue(term1.getComplexity() > 1);
assertTrue(term1.getComplexity() == term2.getComplexity());
assertTrue(term1.getClass().equals(Inheritance.class));
assertTrue(term1.getClass().equals(Inheritance.class));
// System.out.println("t1: " + term1 + ", complexity=" + term1.getComplexity());
// System.out.println("t2: " + term2 + ", complexity=" + term2.getComplexity());
assertTrue(term1.equals(term1.clone()));
assertTrue(term1.compareTo(term1.clone()) == 0);
assertTrue(term2.equals(term2.clone()));
assertTrue(term2.compareTo(term2.clone()) == 0);
boolean t1e2 = term1.equals(term2);
int t1c2 = term1.compareTo(term2);
int t2c1 = term2.compareTo(term1);
assertTrue(!t1e2);
assertTrue("term1 and term2 inequal, so t1.compareTo(t2) should not = 0", t1c2 != 0);
assertTrue("term1 and term2 inequal, so t2.compareTo(t1) should not = 0", t2c1 != 0);
/*
System.out.println("t1 equals t2 " + t1e2);
System.out.println("t1 compareTo t2 " + t1c2);
System.out.println("t2 compareTo t1 " + t2c1);
*/
TreeSet<Term> set = new TreeSet<>();
boolean added1 = set.add((Term) term1.clone());
boolean added2 = set.add((Term) term2.clone());
assertTrue("term 1 added to set", added1);
assertTrue("term 2 added to set", added2);
assertTrue(set.size() == 2);
}
use of nars.main.NAR in project opennars by opennars.
the class TuneTuffy method main.
public static void main(String[] args) throws FileNotFoundException, Narsese.InvalidInputException {
NAR n = new NAR();
n.addInputFile("nal/use_cases/tuffy.smokes.nal");
// new TextOutput(n, System.out, 0.95f);
SolutionMonitor anna0 = new SolutionMonitor(n, "<Anna <-> [Smokes]>");
SolutionMonitor bob0 = new SolutionMonitor(n, "<Bob --> [Smokes]>");
SolutionMonitor edward0 = new SolutionMonitor(n, "<Edward --> [Smokes]>");
SolutionMonitor frank0 = new SolutionMonitor(n, "<Frank --> [Smokes]>");
SolutionMonitor anna = new SolutionMonitor(n, "<Anna <-> [Cancer]>");
SolutionMonitor bob = new SolutionMonitor(n, "<Bob --> [Cancer]>");
SolutionMonitor edward = new SolutionMonitor(n, "<Edward --> [Cancer]>");
SolutionMonitor frank = new SolutionMonitor(n, "<Frank --> [Cancer]>");
n.run();
// first number is the expected Tuffy probability result
System.out.println("0.75? " + edward);
System.out.println("0.65? " + anna);
System.out.println("0.50? " + bob);
System.out.println("0.45? " + frank);
}
Aggregations