use of com.sri.ai.expresso.api.CompoundSyntaxTree in project aic-expresso by aic-sri-international.
the class DefaultCompoundSyntaxTree method equals.
@Override
public boolean equals(Object anotherObject) {
boolean result;
if (anotherObject instanceof CompoundSyntaxTree) {
CompoundSyntaxTree anotherCompoundSyntaxTree = (CompoundSyntaxTree) anotherObject;
if (this.hashCode() == anotherCompoundSyntaxTree.hashCode()) {
List<SyntaxTree> anotherSubTrees = anotherCompoundSyntaxTree.getImmediateSubTrees();
result = this.getRootTree().equals(anotherCompoundSyntaxTree.getRootTree()) && this.getImmediateSubTrees().equals(anotherSubTrees);
} else {
result = false;
}
} else {
result = false;
}
return result;
}
use of com.sri.ai.expresso.api.CompoundSyntaxTree in project aic-expresso by aic-sri-international.
the class Expressions method makeFromSyntaxTree.
/**
* Returns an expression represented by a given syntax tree.
* Scheduled to be removed once expressions are no longer based on syntax trees.
*/
public static Expression makeFromSyntaxTree(SyntaxTree syntaxTree) {
if (syntaxTree instanceof CompoundSyntaxTree) {
Expression result = makeExpressionOnSyntaxTreeWithLabelAndSubTrees(syntaxTree.getLabel(), syntaxTree.getImmediateSubTrees().toArray());
// Expression result = new ExpressionOnCompoundSyntaxTree(syntaxTree);
return result;
}
if (syntaxTree instanceof SyntaxLeaf) {
SyntaxLeaf syntaxLeaf = (SyntaxLeaf) syntaxTree;
Expression result;
if (syntaxLeaf.isStringLiteral()) {
result = Expressions.makeStringLiteral((String) syntaxLeaf.getValue());
} else {
result = Expressions.makeSymbol(syntaxLeaf.getValue());
}
// Expression result = new ExpressionOnSyntaxLeaf(syntaxTree);
return result;
}
throw new Error("Syntax tree " + syntaxTree + " should be either a CompoundSyntaxTree or a Symbol");
}
use of com.sri.ai.expresso.api.CompoundSyntaxTree in project aic-expresso by aic-sri-international.
the class DefaultCompoundSyntaxTree method compareTo.
@Override
public /**
* Compares this CompoundSyntaxTree to other syntax trees, placing it after {@link Symbol}s and comparing
* it lexicographically to other CompoundSyntaxTree, using both the label and sub trees.
*/
int compareTo(Object anotherObject) {
int result;
if (anotherObject instanceof CompoundSyntaxTree) {
CompoundSyntaxTree normalizedAnotherCompoundSyntaxTree = (CompoundSyntaxTree) anotherObject;
Iterator<SyntaxTree> subTreesIncludingLabel = getImmediateSubTreesIncludingRootOneIterator();
Iterator<SyntaxTree> anotherSubTreesIncludingLabel = normalizedAnotherCompoundSyntaxTree.getImmediateSubTreesIncludingRootOneIterator();
result = lexicographicComparison.compare(subTreesIncludingLabel, anotherSubTreesIncludingLabel);
} else {
// Symbols "come first" in the default order
result = 1;
}
return result;
}
use of com.sri.ai.expresso.api.CompoundSyntaxTree in project aic-expresso by aic-sri-international.
the class DefaultSyntaxLeaf method compareTo.
@Override
public /**
* Compares this Symbol to other syntax trees, placing it before {@link CompoundSyntaxTree}s and comparing
* it to other Symbols by comparing their values if they are in the same type (as returned by {@link #getValueType()},
* or their types if they are of different types.
*/
int compareTo(Object another) {
if (this == another) {
return 0;
}
if (another instanceof CompoundSyntaxTree) {
// Symbols come first
return -1;
}
SyntaxLeaf anotherSymbol = null;
if (another instanceof SyntaxLeaf) {
anotherSymbol = (SyntaxLeaf) another;
} else {
anotherSymbol = createSyntaxLeaf(another, false);
// Test again, as may have had self returned from the symbol table.
if (this == anotherSymbol) {
return 0;
}
}
try {
@SuppressWarnings("unchecked") Comparable<Object> value = (Comparable<Object>) getValue();
@SuppressWarnings("unchecked") Comparable<Object> anotherValue = (Comparable<Object>) anotherSymbol.getValue();
int result;
String valueType = getValueType();
String anotherValueType = anotherSymbol.getValueType();
if (valueType.equals(anotherValueType)) {
result = value.compareTo(anotherValue);
} else {
result = valueType.compareTo(anotherValueType);
}
return result;
} catch (ClassCastException e) {
throw new Error("Using DefaultSymbol.compareTo method with non-comparable values.", e);
}
}
Aggregations