Search in sources :

Example 11 with SyntaxTree

use of com.sri.ai.expresso.api.SyntaxTree in project aic-expresso by aic-sri-international.

the class AbstractExtensionalSet method makeSyntaxTree.

protected DefaultCompoundSyntaxTree makeSyntaxTree() {
    DefaultCompoundSyntaxTree result;
    ArrayList<SyntaxTree> elementsSyntaxTrees = Util.mapIntoArrayList(elementsDefinitions, e -> e == null ? null : e.getSyntaxTree());
    SyntaxTree kleeneList = SyntaxTrees.makeKleeneListIfNeeded(elementsSyntaxTrees);
    result = new DefaultCompoundSyntaxTree(getLabel(), kleeneList);
    return result;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree)

Example 12 with SyntaxTree

use of com.sri.ai.expresso.api.SyntaxTree in project aic-expresso by aic-sri-international.

the class IndexExpressions method addSubTreeWithIndexAndBasePathPlusArgumentIndex.

public static void addSubTreeWithIndexAndBasePathPlusArgumentIndex(Expression syntaxTree, int subTreeIndex, List<Integer> basePath, List<Pair<SyntaxTree, List<Integer>>> result) {
    SyntaxTree subTree = syntaxTree.getSyntaxTree().getSubTree(subTreeIndex);
    List<Integer> subExpressionPath = new LinkedList<Integer>(basePath);
    subExpressionPath.add(subTreeIndex);
    result.add(new Pair<SyntaxTree, List<Integer>>(subTree, subExpressionPath));
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList)

Example 13 with SyntaxTree

use of com.sri.ai.expresso.api.SyntaxTree in project aic-expresso by aic-sri-international.

the class AbstractExpression method compareTo.

// //// OBJECT
@Override
public /**
 * Expressions are compared in the same way their respective syntax trees are.
 */
int compareTo(Object anotherObject) {
    if (this == anotherObject) {
        return 0;
    }
    SyntaxTree anotherSyntaxTree;
    if (anotherObject instanceof Expression) {
        anotherSyntaxTree = ((Expression) anotherObject).getSyntaxTree();
    } else {
        anotherSyntaxTree = SyntaxTrees.wrap(anotherObject);
    }
    int result = getSyntaxTree().compareTo(anotherSyntaxTree);
    return result;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) Expression(com.sri.ai.expresso.api.Expression)

Example 14 with SyntaxTree

use of com.sri.ai.expresso.api.SyntaxTree in project aic-expresso by aic-sri-international.

the class SyntaxTreeTest method testComparison.

@Test
public void testComparison() {
    SyntaxTree a;
    SyntaxTree b;
    SyntaxTree c;
    List<SyntaxTree> list;
    // testing comparison of symbols of different types
    // String
    a = SyntaxTrees.parse("a");
    // Boolean
    b = SyntaxTrees.parse("true");
    // Number
    c = SyntaxTrees.parse("1.0");
    list = Util.list(a, b, c);
    Collections.sort(list);
    assertEquals(Util.list(b, c, a), list);
    // testing comparison of symbols of different types
    // String
    a = SyntaxTrees.parse("a");
    // String
    b = SyntaxTrees.parse("b");
    // Number
    c = SyntaxTrees.parse("1.0");
    list = Util.list(a, b, c);
    Collections.sort(list);
    assertEquals(Util.list(c, a, b), list);
    // testing comparison of numeric symbols of different classes (Integer and Double)
    // numeric values should be compared
    a = SyntaxTrees.parse("3");
    b = SyntaxTrees.parse("2.0");
    c = SyntaxTrees.parse("1.0");
    list = Util.list(a, b, c);
    Collections.sort(list);
    assertEquals(Util.list(c, b, a), list);
    a = SyntaxTrees.parse("a");
    b = SyntaxTrees.parse("b");
    c = SyntaxTrees.parse("c");
    list = Util.list(a, b, c);
    Collections.sort(list);
    assertEquals(Util.list(a, b, c), list);
    a = SyntaxTrees.parse("f()");
    b = SyntaxTrees.parse("f");
    c = SyntaxTrees.parse("g()");
    list = Util.list(a, b, c);
    Collections.sort(list);
    assertEquals(Util.list(b, a, c), list);
    a = SyntaxTrees.parse("f(a, b, c)");
    b = SyntaxTrees.parse("f(c, b, a)");
    c = SyntaxTrees.parse("g()");
    list = Util.list(a, b, c);
    Collections.sort(list);
    assertEquals(Util.list(a, b, c), list);
    a = SyntaxTrees.parse("f(a, b, c)");
    b = SyntaxTrees.parse("f(a)");
    c = SyntaxTrees.parse("g()");
    list = Util.list(a, b, c);
    Collections.sort(list);
    assertEquals(Util.list(b, a, c), list);
    a = SyntaxTrees.parse("f(a, b, c)");
    b = SyntaxTrees.parse("f(c)");
    c = SyntaxTrees.parse("g()");
    list = Util.list(a, b, c);
    Collections.sort(list);
    assertEquals(Util.list(a, b, c), list);
    a = SyntaxTrees.parse("f(a(c,b), b, c)");
    b = SyntaxTrees.parse("f(a(b,c), b, c)");
    c = SyntaxTrees.parse("g()");
    list = Util.list(a, b, c);
    Collections.sort(list);
    assertEquals(Util.list(b, a, c), list);
    a = SyntaxTrees.parse("f(a(b,c), f(a(b,a)), c)");
    b = SyntaxTrees.parse("f(a(b,c), f(a(a,b)), c)");
    c = SyntaxTrees.parse("g()");
    list = Util.list(a, b, c);
    Collections.sort(list);
    assertEquals(Util.list(b, a, c), list);
    // Symbol labels f and g come first
    a = SyntaxTrees.parse("f   (a(b,c), f(a(a,b)), c)");
    b = SyntaxTrees.parse("f(x)(a(a,a), f(a(a,b)), c)");
    c = SyntaxTrees.parse("g()");
    list = Util.list(a, b, c);
    Collections.sort(list);
    assertEquals(Util.list(a, c, b), list);
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) Test(org.junit.Test)

Example 15 with SyntaxTree

use of com.sri.ai.expresso.api.SyntaxTree in project aic-expresso by aic-sri-international.

the class DefaultTuple method makeSyntaxTree.

private DefaultCompoundSyntaxTree makeSyntaxTree() {
    DefaultCompoundSyntaxTree result;
    if (arguments.size() == 1) {
        result = new DefaultCompoundSyntaxTree("tuple", arguments.get(0));
    } else {
        ArrayList<SyntaxTree> argumentsSyntaxTrees = Util.mapIntoArrayList(arguments, e -> e == null ? null : e.getSyntaxTree());
        SyntaxTree kleeneList = SyntaxTrees.makeKleeneListIfNeeded(argumentsSyntaxTrees);
        result = new DefaultCompoundSyntaxTree(Tuple.TUPLE_LABEL, kleeneList);
    }
    return result;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree)

Aggregations

SyntaxTree (com.sri.ai.expresso.api.SyntaxTree)23 CompoundSyntaxTree (com.sri.ai.expresso.api.CompoundSyntaxTree)9 Expression (com.sri.ai.expresso.api.Expression)9 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)4 DefaultCompoundSyntaxTree (com.sri.ai.expresso.core.DefaultCompoundSyntaxTree)3 LinkedList (java.util.LinkedList)3 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)2 SyntaxTrees.makeCompoundSyntaxTree (com.sri.ai.expresso.helper.SyntaxTrees.makeCompoundSyntaxTree)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)1 Util.mapIntoList (com.sri.ai.util.Util.mapIntoList)1 Test (org.junit.Test)1