Search in sources :

Example 1 with Symbol

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

the class Context method extendWith.

/**
	 * Extends context with index expressions, taking into account that new contextual variables may collide with existing ones.
	 * In this case, it renames the incoming variables to unique identifiers and replaces them in the types of remaining
	 * index expressions. It also renames the variables in a given expressions supposed to be in their scope (for example,
	 * the head and condition of an intensionally defined set).
	 * Returns the new context and the index expressions and expression in scope after the renaming.
	 * @param indexExpressions
	 * @param expressionInScope
	 * @return the new context and the index expressions and expression in scope after the renaming
	 */
default default Triple<Context, ExtensionalIndexExpressionsSet, Expression> extendWith(ExtensionalIndexExpressionsSet indexExpressions, Expression expressionInScope) {
    Triple<Context, ExtensionalIndexExpressionsSet, Expression> result;
    if (thereExists(getIndices(indexExpressions), index -> this.containsSymbol(index))) {
        // OPTIMIZATION: only kick in this entire procedure when extending with symbol in the context (previous ones could have been dealt with normally).
        // the objects to be returned in the triple:
        Context newContext = this;
        ArrayList<Expression> newIndexExpressionsList = new ArrayList<>(indexExpressions.getList());
        Expression newExpressionInScope = expressionInScope;
        // Collects all existing symbols to be able to create unique symbols
        Set<Expression> alreadyDefined = Util.set();
        alreadyDefined.addAll(this.getSymbols());
        alreadyDefined.addAll(Expressions.freeSymbols(new DefaultTuple(newIndexExpressionsList), this));
        alreadyDefined.addAll(Expressions.freeSymbols(expressionInScope, this));
        Predicate<Expression> isAlreadyDefined = e -> alreadyDefined.contains(e);
        for (int i = 0; i != newIndexExpressionsList.size(); i++) {
            Expression indexExpression = newIndexExpressionsList.get(i);
            Symbol index = (Symbol) indexExpression.get(0);
            Expression type = indexExpression.get(1);
            PairOf<Expression> newIndexAndNewExpressionInScope = Expressions.standardizeApart(index, isAlreadyDefined, newExpressionInScope);
            Expression newIndex = newIndexAndNewExpressionInScope.first;
            newExpressionInScope = newIndexAndNewExpressionInScope.second;
            // type should not contain the index
            Expression newIndexExpression = apply(IN, newIndex, type);
            newIndexExpressionsList.set(i, newIndexExpression);
            alreadyDefined.add(newIndex);
            for (int j = i + 1; j != newIndexExpressionsList.size(); j++) {
                Expression anotherIndexExpression = newIndexExpressionsList.get(j);
                Expression anotherIndex = anotherIndexExpression.get(0);
                Expression anotherType = anotherIndexExpression.get(1);
                Expression newAnotherType = anotherType.replaceSymbol(index, newIndex, this);
                // anotherIndex is a symbols and does not contain index
                Expression newAnotherIndexExpression = apply(IN, anotherIndex, newAnotherType);
                newIndexExpressionsList.set(j, newAnotherIndexExpression);
            }
        }
        ExtensionalIndexExpressionsSet newIndexExpressions = new ExtensionalIndexExpressionsSet(newIndexExpressionsList);
        newContext = newContext.extendWith(newIndexExpressions);
        result = triple(newContext, newIndexExpressions, newExpressionInScope);
    } else {
        // no collision; usual extension and the expressions do not change.
        result = triple(extendWith(indexExpressions), indexExpressions, expressionInScope);
    }
    return result;
}
Also used : IN(com.sri.ai.grinder.sgdpllt.library.FunctorConstants.IN) Type(com.sri.ai.expresso.api.Type) Triple(com.sri.ai.util.base.Triple) Collection(java.util.Collection) Expressions(com.sri.ai.expresso.helper.Expressions) Set(java.util.Set) PairOf(com.sri.ai.util.base.PairOf) Expression(com.sri.ai.expresso.api.Expression) Triple.triple(com.sri.ai.util.base.Triple.triple) DefaultTuple(com.sri.ai.expresso.core.DefaultTuple) ArrayList(java.util.ArrayList) Beta(com.google.common.annotations.Beta) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Symbol(com.sri.ai.expresso.api.Symbol) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) Predicate(com.google.common.base.Predicate) Map(java.util.Map) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) Registry(com.sri.ai.grinder.api.Registry) Util(com.sri.ai.util.Util) IndexExpressions.getIndices(com.sri.ai.grinder.sgdpllt.library.indexexpression.IndexExpressions.getIndices) Util.thereExists(com.sri.ai.util.Util.thereExists) Symbol(com.sri.ai.expresso.api.Symbol) ArrayList(java.util.ArrayList) DefaultTuple(com.sri.ai.expresso.core.DefaultTuple) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression)

Example 2 with Symbol

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

the class Expressions method standardizeApart.

/**
 * Given a symbol, a predicate indicating what symbols are already defined in some situation,
 * and an expression possibly containing the symbol,
 * prime the symbol until it is unique,
 * and return the new symbol and the result of replacing the old symbol by the new in the expression containing it.
 * @param symbol
 * @param alreadyDefined
 * @param containingSymbol
 * @return the new symbol and the result of replacing the old symbol by the new in the expression containing it.
 */
public static PairOf<Expression> standardizeApart(Symbol symbol, Predicate<Expression> alreadyDefined, Expression containingSymbol) {
    Expression newSymbol = symbol;
    while (alreadyDefined.apply(newSymbol)) {
        newSymbol = primedUntilUnique(newSymbol, s -> !alreadyDefined.apply(s));
    }
    Expression newExpressionContainingSymbol = containingSymbol.replaceSymbol(symbol, newSymbol, null);
    PairOf<Expression> result = pairOf(newSymbol, newExpressionContainingSymbol);
    return result;
}
Also used : Arrays(java.util.Arrays) CountingFormula(com.sri.ai.expresso.api.CountingFormula) AntlrGrinderParserWrapper(com.sri.ai.grinder.parser.antlr.AntlrGrinderParserWrapper) DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression) SyntaxLeaf(com.sri.ai.expresso.api.SyntaxLeaf) And(com.sri.ai.grinder.library.boole.And) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Symbol(com.sri.ai.expresso.api.Symbol) DefaultExistentiallyQuantifiedFormula(com.sri.ai.expresso.core.DefaultExistentiallyQuantifiedFormula) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) Map(java.util.Map) DefaultFunctionApplication(com.sri.ai.expresso.core.DefaultFunctionApplication) IntegerIterator(com.sri.ai.util.collect.IntegerIterator) Util.thereExists(com.sri.ai.util.Util.thereExists) Function(com.google.common.base.Function) DefaultIntensionalMultiSet(com.sri.ai.expresso.core.DefaultIntensionalMultiSet) Collection(java.util.Collection) ExpressionAndSyntacticContext(com.sri.ai.expresso.api.ExpressionAndSyntacticContext) ZipIterator(com.sri.ai.util.collect.ZipIterator) Set(java.util.Set) Util.mapIntoList(com.sri.ai.util.Util.mapIntoList) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) IsVariable(com.sri.ai.grinder.library.IsVariable) List(java.util.List) Predicate(com.google.common.base.Predicate) DefaultIntensionalUniSet(com.sri.ai.expresso.core.DefaultIntensionalUniSet) FunctorConstants(com.sri.ai.grinder.library.FunctorConstants) TIMES(com.sri.ai.grinder.library.FunctorConstants.TIMES) UnaryMinus(com.sri.ai.grinder.library.number.UnaryMinus) Tuple(com.sri.ai.expresso.api.Tuple) PairOf(com.sri.ai.util.base.PairOf) SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) Stack(java.util.Stack) ArrayList(java.util.ArrayList) ExtensionalSets(com.sri.ai.grinder.library.set.extensional.ExtensionalSets) LinkedHashMap(java.util.LinkedHashMap) SingletonListMaker(com.sri.ai.util.base.SingletonListMaker) Lists(com.google.common.collect.Lists) NotContainedBy(com.sri.ai.util.base.NotContainedBy) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) DefaultCountingFormula(com.sri.ai.expresso.core.DefaultCountingFormula) DefaultSymbol(com.sri.ai.expresso.core.DefaultSymbol) Registry(com.sri.ai.grinder.api.Registry) ThereExists(com.sri.ai.grinder.library.boole.ThereExists) CompoundSyntaxTree(com.sri.ai.expresso.api.CompoundSyntaxTree) LinkedList(java.util.LinkedList) FunctionIterator(com.sri.ai.util.collect.FunctionIterator) LinkedHashSet(java.util.LinkedHashSet) Pair(com.sri.ai.util.base.Pair) DefaultExtensionalUniSet(com.sri.ai.expresso.core.DefaultExtensionalUniSet) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) Iterator(java.util.Iterator) Parser(com.sri.ai.expresso.api.Parser) DefaultUniversallyQuantifiedFormula(com.sri.ai.expresso.core.DefaultUniversallyQuantifiedFormula) DefaultTuple(com.sri.ai.expresso.core.DefaultTuple) Beta(com.google.common.annotations.Beta) FunctionApplication(com.sri.ai.expresso.api.FunctionApplication) FunctionSignature(com.sri.ai.grinder.helper.FunctionSignature) PruningPredicate(com.sri.ai.grinder.core.PruningPredicate) PairOf.pairOf(com.sri.ai.util.base.PairOf.pairOf) DefaultExtensionalMultiSet(com.sri.ai.expresso.core.DefaultExtensionalMultiSet) ForAll(com.sri.ai.grinder.library.boole.ForAll) Util(com.sri.ai.util.Util) Equals(com.sri.ai.util.base.Equals) Collections(java.util.Collections) DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression)

Example 3 with Symbol

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

the class ProceduralAttachmentExpressionFactor method computeInnerExpression.

@Override
protected Expression computeInnerExpression() {
    Variable variable = proceduralAttachment.getVariable();
    Object value = proceduralAttachment.getProcedureValue();
    Symbol valueExpression = DefaultSymbol.createSymbol(value);
    Type type = getVariableType(variable, getContext());
    Expression condition;
    if (value instanceof Double) {
        assertVariableIsOfRealType(variable, type, getContext());
        condition = makeIntervalFactorCondition(valueExpression);
    } else if (value instanceof Integer) {
        assertVariableIsOfIntegerType(variable, type, getContext());
        condition = makeEquality(valueExpression);
    } else if (value instanceof Boolean) {
        assertVariableIsOfBooleanType(variable, type, getContext());
        condition = makeBooleanCondition(valueExpression);
    } else {
        throw new Error("Procedural attachments must provide only Double or Integer objects, but procedure for " + proceduralAttachment.getVariable() + " provided " + proceduralAttachment.getProcedureValue() + " of type " + proceduralAttachment.getProcedureValue().getClass());
    }
    Expression result = IfThenElse.make(condition, Expressions.ONE, Expressions.ZERO);
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) Variable(com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) Expression(com.sri.ai.expresso.api.Expression) Symbol(com.sri.ai.expresso.api.Symbol) DefaultSymbol(com.sri.ai.expresso.core.DefaultSymbol)

Example 4 with Symbol

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

the class FromTableToExpressionFactorConverter method makeComparisonToAssignedValue.

private Expression makeComparisonToAssignedValue(Integer variableIndex, List<TableVariable> tableVariables, ArrayList<? extends Integer> assignment) {
    Integer assignedValue = assignment.get(variableIndex);
    Symbol assignedValueExpression = createSymbol(assignedValue);
    TableVariable tableVariable = tableVariables.get(variableIndex);
    Symbol variableExpression = makeVariableExpression(tableVariable);
    Expression result = Equality.make(variableExpression, assignedValueExpression);
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) DefaultSymbol.createSymbol(com.sri.ai.expresso.core.DefaultSymbol.createSymbol) Symbol(com.sri.ai.expresso.api.Symbol) TableVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.table.TableVariable)

Example 5 with Symbol

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

the class IntegerInterval method sampleUniquelyNamedConstant.

@Override
public Expression sampleUniquelyNamedConstant(Random random) {
    myAssert(() -> boundsAreConstants(), () -> "Cannot sample uniquely named constant from integer interval that is infinite and/or defined by variables: " + getName());
    Symbol result = makeSymbol(new Rational(nonStrictLowerBound.intValue() + random.nextInt(nonStrictUpperBound.intValue() - nonStrictLowerBound.intValue() + 1)));
    return result;
}
Also used : Rational(com.sri.ai.util.math.Rational) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Symbol(com.sri.ai.expresso.api.Symbol)

Aggregations

Symbol (com.sri.ai.expresso.api.Symbol)22 Expression (com.sri.ai.expresso.api.Expression)15 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)13 Type (com.sri.ai.expresso.api.Type)6 DefaultSymbol (com.sri.ai.expresso.core.DefaultSymbol)6 Registry (com.sri.ai.grinder.api.Registry)6 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)5 DefaultRegistry (com.sri.ai.grinder.core.DefaultRegistry)5 ArrayList (java.util.ArrayList)5 Predicate (com.google.common.base.Predicate)4 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)4 Util (com.sri.ai.util.Util)4 PairOf (com.sri.ai.util.base.PairOf)4 Rational (com.sri.ai.util.math.Rational)4 Set (java.util.Set)4 Beta (com.google.common.annotations.Beta)3 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)3 DefaultExtensionalUniSet (com.sri.ai.expresso.core.DefaultExtensionalUniSet)3 DefaultTuple (com.sri.ai.expresso.core.DefaultTuple)3 TIMES (com.sri.ai.grinder.library.FunctorConstants.TIMES)3