use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.
the class MultiVariableContextWithCheckedProperty method conjoinSpecializedForConstraintsIfApplicable.
/**
* Returns a pair indicating whether specialized conjoin for constraints applies to this case and,
* if so, provides the result of this conjoining.
* @param formula
* @param context
* @return
*/
private Pair<Boolean, Context> conjoinSpecializedForConstraintsIfApplicable(Expression formula, Context context) {
Pair<Boolean, Context> result;
if (formula instanceof SingleVariableConstraint) {
SingleVariableConstraint formulaAsSingleVariableConstraint = (SingleVariableConstraint) formula;
Expression variable = formulaAsSingleVariableConstraint.getVariable();
// TODO: this forces expression representation to be generated, which can be expensive. Better write a method that checks it on the constraint representation itself
boolean variableAlreadyConstrainedInThis = contains(this, variable);
if (!variableAlreadyConstrainedInThis) {
// if the variable is new to this constraint, we can simply tack on its constraint on it.
Context newContext = makeAndCheck(getTheory(), formulaAsSingleVariableConstraint, this, contextDependentProblemStepSolverMaker, context);
result = pair(true, newContext);
} else {
// otherwise we won't be able to use the single variable constraint structure in any special way
result = pair(false, null);
}
} else if (formula instanceof MultiVariableContextWithCheckedProperty) {
MultiVariableContextWithCheckedProperty formulaAsMultiVariableConstraint = (MultiVariableContextWithCheckedProperty) formula;
// if formula is itself a MultiVariableContextWithCheckedProperty,
// we conjoin its two known parts individually.
// Their own inner structure will also be efficiently exploited by these conjunctions.
Context conjunction = this;
if (formulaAsMultiVariableConstraint.tail != null) {
conjunction = conjunction.conjoin(formulaAsMultiVariableConstraint.tail, context);
}
if (formulaAsMultiVariableConstraint.head != null) {
conjunction = conjunction.conjoin(formulaAsMultiVariableConstraint.head, context);
}
result = pair(true, conjunction);
} else {
// the formula does not have a recognizable structure we can exploit
result = pair(false, null);
}
return result;
}
use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.
the class AbstractQuantifierEliminationStepSolver method step.
@Override
public Step step(Context context) {
Step result;
Context contextForBody = getContextForBody(context);
if (contextForBody.isContradiction()) {
result = new Solution(group.additiveIdentityElement());
} else {
ExpressionLiteralSplitterStepSolver bodyStepSolver = getInitialBodyStepSolver(context.getTheory());
ExpressionLiteralSplitterStepSolver.Step bodyStep = bodyStepSolver.step(contextForBody);
// Check (**) in this file to see where this happens
if (!bodyStep.itDepends()) {
ExpressionLiteralSplitterStepSolver evaluatorStepSolver = getTheory().makeEvaluatorStepSolver(bodyStep.getValue());
bodyStep = evaluatorStepSolver.step(context);
}
if (bodyStep.itDepends()) {
// "intercept" literals containing the index and split the quantifier based on it
if (isSubExpressionOf(getIndex(), bodyStep.getSplitterLiteral())) {
Expression literalOnIndex = bodyStep.getSplitterLiteral();
result = resultIfLiteralContainsIndex(literalOnIndex, bodyStep, contextForBody, context);
} else {
// not on index, just pass the expression on which we depend on, but with appropriate sub-step solvers (this, for now)
AbstractQuantifierEliminationStepSolver ifTrue = clone();
AbstractQuantifierEliminationStepSolver ifFalse = clone();
ifTrue.initialBodyEvaluationStepSolver = bodyStep.getStepSolverForWhenSplitterIsTrue();
ifFalse.initialBodyEvaluationStepSolver = bodyStep.getStepSolverForWhenSplitterIsFalse();
ifTrue.initialContextForBody = bodyStep.getContextSplittingWhenSplitterIsLiteral().getContextAndLiteral();
ifFalse.initialContextForBody = bodyStep.getContextSplittingWhenSplitterIsLiteral().getContextAndLiteralNegation();
// to compute the result's constraint splitting,
// we cannot directly re-use bodyStep.getConstraintSplitting() because it was not obtained from
// the context it is returning to,
// but from the context conjoined with the index constraint.
// In order to provide two contexts to work with the sequel step solvers,
// we calculate the splittings here.
// TODO: In the future, we expect it possible to efficiently extract the contextForBody component relative
// to the original context only, excluding the index.
ContextSplitting split = new ContextSplitting(bodyStep.getSplitterLiteral(), context);
result = new ItDependsOn(bodyStep.getSplitterLiteral(), split, ifTrue, ifFalse);
}
} else {
// body is already literal free
result = eliminateQuantifierForLiteralFreeBodyAndSingleVariableConstraint(indexConstraint, bodyStep.getValue(), context);
}
}
return result;
}
use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.
the class AbstractIterativeMultiIndexQuantifierElimination method extendAssignments.
/**
* Sets the value assignment to a given expression in the binding mechanism stored in the context.
* @param newAssignment
* @param context
* @return
*/
public static Context extendAssignments(Map<Expression, Expression> newAssignments, Context context) {
@SuppressWarnings("unchecked") Map<Expression, Expression> assignments = (Map<Expression, Expression>) context.getGlobalObject(ASSIGNMENTS_GLOBAL_OBJECTS_KEY);
Map<Expression, Expression> extendedAssignments;
if (assignments == null) {
extendedAssignments = newAssignments;
} else {
extendedAssignments = new StackedHashMap<>(newAssignments, assignments);
}
Context result = context.putGlobalObject(ASSIGNMENTS_GLOBAL_OBJECTS_KEY, extendedAssignments);
return result;
}
use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.
the class MultiVariableContextWithCheckedProperty method putGlobalObject.
@Override
public MultiVariableContextWithCheckedProperty putGlobalObject(Object key, Object value) {
MultiVariableContextWithCheckedProperty result = clone();
Context newTail = tail.putGlobalObject(key, value);
result.tail = newTail;
return result;
}
use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.
the class MultiVariableContextWithCheckedProperty method makeAndCheck.
/**
* Creates a new {@link Context} from a {@link SingleVariableConstraint} and a {@link Context},
* by either returning a contradiction if either is contradictory,
* or a new {@link MultiVariableContextWithCheckedProperty} otherwise.
* @param theory
* @param head
* @param tail
* @param context
* @return
*/
public static Context makeAndCheck(Theory theory, SingleVariableConstraint head, Context tail, ContextDependentProblemStepSolverMaker contextDependentProblemStepSolverMaker, Context context) {
Context result;
if (head.isContradiction() || tail.isContradiction()) {
result = tail.makeContradiction();
} else {
MultiVariableContextWithCheckedProperty uncheckedMultiVariableConstraintWithCheckedProperty = new MultiVariableContextWithCheckedProperty(tail.getTheory(), head, tail, contextDependentProblemStepSolverMaker);
result = uncheckedMultiVariableConstraintWithCheckedProperty.check(context);
}
return result;
}
Aggregations