Search in sources :

Example 1 with SyntaxTree

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

the class IntersectionIntensionalSetsSimplifier method standardizeApartIntensionalSets.

@SuppressWarnings("deprecation")
public static IntensionalSet standardizeApartIntensionalSets(IntensionalSet intensionalSet, IntensionalSet fromOtherIntensionalSet, Context context) {
    IntensionalSet result = intensionalSet;
    IndexExpressionsSet intensionalSetIndexes = intensionalSet.getIndexExpressions();
    IndexExpressionsSet fromOtherIntensionalSetIn = fromOtherIntensionalSet.getIndexExpressions();
    List<Expression> overlappingIndexNames = new ArrayList<>();
    for (Expression intensionalSetIndex : IndexExpressions.getIndices(intensionalSetIndexes)) {
        if (IndexExpressions.indexExpressionsContainIndex(fromOtherIntensionalSetIn, intensionalSetIndex)) {
            overlappingIndexNames.add(intensionalSetIndex);
        }
    }
    if (overlappingIndexNames.size() > 0) {
        Expression combinedExpression = And.make(intensionalSet, fromOtherIntensionalSet);
        List<Expression> newIndexNames = new ArrayList<>();
        for (Expression overlappingIndex : overlappingIndexNames) {
            Expression newIndexName = Expressions.makeUniqueVariable(overlappingIndex.toString(), combinedExpression, context);
            newIndexNames.add(newIndexName);
        }
        SyntaxTree resultSyntaxTree = result.getSyntaxTree();
        for (int i = 0; i < newIndexNames.size(); i++) {
            Expression replaced = overlappingIndexNames.get(i);
            Expression replacement = newIndexNames.get(i);
            resultSyntaxTree = resultSyntaxTree.replaceSubTreesAllOccurrences(replaced.getSyntaxTree(), replacement.getSyntaxTree());
        }
        result = (IntensionalSet) Expressions.makeFromSyntaxTree(resultSyntaxTree);
    }
    return result;
}
Also used : IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 2 with SyntaxTree

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

the class AbstractExpression method equals.

@Override
public boolean equals(Object another) {
    if (this == another) {
        return true;
    }
    SyntaxTree anotherSyntaxTree;
    if (another instanceof SyntaxTree) {
        anotherSyntaxTree = (SyntaxTree) another;
    } else if (another instanceof Expression) {
        anotherSyntaxTree = ((Expression) another).getSyntaxTree();
    } else {
        anotherSyntaxTree = SyntaxTrees.makeSyntaxLeaf(another);
    }
    boolean result = getSyntaxTree().equals(anotherSyntaxTree);
    return result;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) Expression(com.sri.ai.expresso.api.Expression)

Example 3 with SyntaxTree

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

the class AbstractIntensionalSet method makeSyntaxTree.

private SyntaxTree makeSyntaxTree() {
    IndexExpressionsSet indexExpressions = getIndexExpressions();
    SyntaxTree parametersSyntaxTree = indexExpressions.getSubSyntaxTree();
    SyntaxTree scopingSyntaxTree = new DefaultCompoundSyntaxTree(IntensionalSet.SCOPED_VARIABLES_LABEL, parametersSyntaxTree);
    SyntaxTree headSyntaxTree = getHead().getSyntaxTree();
    SyntaxTree conditionSyntaxTree = (getCondition() == null || getCondition().equals("true")) ? null : SyntaxTrees.makeCompoundSyntaxTree(IntensionalSet.CONDITION_LABEL, condition.getSyntaxTree());
    cachedSyntaxTree = makeCompoundSyntaxTree(getSyntaxTreeLabel(), scopingSyntaxTree, headSyntaxTree, conditionSyntaxTree);
    return cachedSyntaxTree;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) SyntaxTrees.makeCompoundSyntaxTree(com.sri.ai.expresso.helper.SyntaxTrees.makeCompoundSyntaxTree) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 4 with SyntaxTree

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

the class AbstractQuantifiedExpressionWithABody method makeSyntaxTree.

private SyntaxTree makeSyntaxTree() {
    IndexExpressionsSet indexExpressions = getIndexExpressions();
    List<Expression> indexExpressionsList = ((ExtensionalIndexExpressionsSet) indexExpressions).getList();
    List<SyntaxTree> indexExpressionsSyntaxTrees = mapIntoArrayList(indexExpressionsList, Expression::getSyntaxTree);
    SyntaxTree parameterList = SyntaxTrees.makeKleeneListIfNeeded(indexExpressionsSyntaxTrees);
    SyntaxTree result = makeCompoundSyntaxTree(getSyntaxTreeLabel(), parameterList, getBody().getSyntaxTree());
    return result;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) SyntaxTrees.makeCompoundSyntaxTree(com.sri.ai.expresso.helper.SyntaxTrees.makeCompoundSyntaxTree) Expression(com.sri.ai.expresso.api.Expression) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 5 with SyntaxTree

use of com.sri.ai.expresso.api.SyntaxTree 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;
}
Also used : SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree)

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