Search in sources :

Example 21 with SyntaxTree

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

the class IntersectionIntensionalSetsSimplifier method standardizeApartIntensionalSets.

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 22 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) Util.mapIntoList(com.sri.ai.util.Util.mapIntoList) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList)

Example 23 with SyntaxTree

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

the class AntlrGrinderParserTest method makeScopingSyntaxTree.

/**
 * Makes a scoping expression out of a list of scoping variables.
 */
public static SyntaxTree makeScopingSyntaxTree(IndexExpressionsSet indexExpressions) {
    List<Expression> indexExpressionsList = ((ExtensionalIndexExpressionsSet) indexExpressions).getList();
    Expression kleeneListExpression = Expressions.makeKleeneListIfNeeded(indexExpressionsList);
    SyntaxTree kleeneListSyntaxTree = kleeneListExpression.getSyntaxTree();
    SyntaxTree result = SyntaxTrees.makeCompoundSyntaxTree(IntensionalSet.SCOPED_VARIABLES_LABEL, kleeneListSyntaxTree);
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) Expression(com.sri.ai.expresso.api.Expression)

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