Search in sources :

Example 1 with CompoundTerm

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));
}
Also used : CompoundTerm(nars.language.CompoundTerm) HashMap(java.util.HashMap) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term) Test(org.junit.Test)

Example 2 with CompoundTerm

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

Example 3 with CompoundTerm

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);
    }
}
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 4 with CompoundTerm

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

Example 5 with CompoundTerm

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);
}
Also used : CompoundTerm(nars.language.CompoundTerm) SetExt(nars.language.SetExt) SetInt(nars.language.SetInt) CompoundTerm(nars.language.CompoundTerm) Term(nars.language.Term)

Aggregations

CompoundTerm (nars.language.CompoundTerm)46 Term (nars.language.Term)43 TruthValue (nars.entity.TruthValue)30 BudgetValue (nars.entity.BudgetValue)27 Sentence (nars.entity.Sentence)20 Statement (nars.language.Statement)19 Conjunction (nars.language.Conjunction)18 Task (nars.entity.Task)17 Inheritance (nars.language.Inheritance)13 Implication (nars.language.Implication)8 HashMap (java.util.HashMap)7 Variable (nars.language.Variable)7 Interval (nars.language.Interval)6 SetExt (nars.language.SetExt)6 SetInt (nars.language.SetInt)6 Product (nars.language.Product)5 TruthFunctions.reduceConjunction (nars.inference.TruthFunctions.reduceConjunction)4 Equivalence (nars.language.Equivalence)4 ImageExt (nars.language.ImageExt)4 ImageInt (nars.language.ImageInt)4