Search in sources :

Example 6 with Term

use of nars.language.Term 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]);
}
Also used : Narsese(nars.io.Narsese) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) NAR(nars.main.NAR) Test(org.junit.Test)

Example 7 with Term

use of nars.language.Term 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);
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) Statement(nars.language.Statement) Inheritance(nars.language.Inheritance) Narsese(nars.io.Narsese) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) NAR(nars.main.NAR) Test(org.junit.Test)

Example 8 with Term

use of nars.language.Term 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);
    }
}
Also used : CompoundTerm(nars.language.CompoundTerm) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) NAR(nars.main.NAR)

Example 9 with Term

use of nars.language.Term 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);
}
Also used : TreeSet(java.util.TreeSet) Inheritance(nars.language.Inheritance) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) NAR(nars.main.NAR) Test(org.junit.Test)

Example 10 with Term

use of nars.language.Term in project opennars by opennars.

the class ConceptMonitor method strongestPrecondition.

public static Sentence strongestPrecondition(NAR nar, String conc, String statement) {
    Concept c = ConceptMonitor.concept(nar, conc);
    Term st = stringToTerm(nar, statement);
    if (c != null && st != null) {
        for (Task t : c.executable_preconditions) {
            if (CompoundTerm.replaceIntervals(t.getTerm()).equals(CompoundTerm.replaceIntervals(st))) {
                return t.sentence;
            }
        }
    }
    return null;
}
Also used : Concept(nars.entity.Concept) Task(nars.entity.Task) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term)

Aggregations

Term (nars.language.Term)109 CompoundTerm (nars.language.CompoundTerm)66 BudgetValue (nars.entity.BudgetValue)48 TruthValue (nars.entity.TruthValue)46 Sentence (nars.entity.Sentence)40 Task (nars.entity.Task)37 Statement (nars.language.Statement)28 Conjunction (nars.language.Conjunction)20 Inheritance (nars.language.Inheritance)19 Stamp (nars.entity.Stamp)17 Concept (nars.entity.Concept)14 Implication (nars.language.Implication)13 Product (nars.language.Product)11 NAR (nars.main.NAR)9 Interval (nars.language.Interval)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)7 SetExt (nars.language.SetExt)7 SetInt (nars.language.SetInt)7 Variable (nars.language.Variable)7