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;
}
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));
}
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;
}
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);
}
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;
}
Aggregations