use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.
the class AbstractTheoryTestingSupport method getTypesForTesting.
@Override
public Collection<Type> getTypesForTesting() {
if (cachedTypesForTesting == null) {
Set<Type> topTypesForTesting = new LinkedHashSet<>(getVariableNamesAndTypesForTesting().values());
topTypesForTesting.addAll(getTheory().getNativeTypes());
// Ensure we include embedded types (e.g. in a function or tuple type) as well
Set<Type> embeddedTypes = new LinkedHashSet<>();
for (Type topType : topTypesForTesting) {
embeddedTypes.addAll(topType.getEmbeddedTypes());
}
topTypesForTesting.addAll(embeddedTypes);
cachedTypesForTesting = Collections.unmodifiableCollection(topTypesForTesting);
}
return cachedTypesForTesting;
}
use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.
the class AbstractTheoryTestingSupport method extendWithTestingInformation.
@Override
public Context extendWithTestingInformation(Context context) {
// we only need to provide the variables types, and not the known constant types, because the latter will be extracted from the already registered types.
Map<String, String> mapFromSymbolNamesToTypeNames = new LinkedHashMap<String, String>();
for (Map.Entry<String, Type> symbolAndType : getVariableNamesAndTypesForTesting().entrySet()) {
mapFromSymbolNamesToTypeNames.put(symbolAndType.getKey(), symbolAndType.getValue().toString());
}
for (Map.Entry<String, Type> symbolAndType : getExtendedVariableNamesAndTypesForTesting().entrySet()) {
mapFromSymbolNamesToTypeNames.put(symbolAndType.getKey(), symbolAndType.getValue().toString());
}
Context result = (Context) GrinderUtil.extendRegistryWith(mapFromSymbolNamesToTypeNames, getTypesForTesting(), context);
return result;
}
use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.
the class CompoundTheoryTestingSupport method setVariableNamesAndTypesForTesting.
/**
* This is overridden so that given variables and types for testing are distributed to their
* respective theories according to {@link #isSuitableFor(Expression, Type)}.
*/
@Override
public void setVariableNamesAndTypesForTesting(Map<String, Type> variableNamesAndTypesForTesting) {
// First ensure the compound set of variables names and type information is setup correctly
super.setVariableNamesAndTypesForTesting(variableNamesAndTypesForTesting);
// Then update the sub-theories so that they share appropriate subsets of this information
Map<Theory, Map<String, Type>> mapForSubTheory = map();
for (Theory subTheory : getTheory().getSubTheories()) {
mapForSubTheory.put(subTheory, map());
}
for (Map.Entry<String, Type> variableNameAndType : getVariableNamesAndTypesForTesting().entrySet()) {
String variableName = variableNameAndType.getKey();
Expression variable = Expressions.parse(variableName);
Type type = variableNameAndType.getValue();
for (Theory subTheory : getTheory().getSubTheories()) {
if (subTheory.isSuitableFor(variable, type) || (type instanceof FunctionType && subTheory.isSuitableFor(variable, ((FunctionType) type).getCodomain()))) {
mapForSubTheory.get(subTheory).put(variableName, type);
}
}
}
for (Map.Entry<Theory, TheoryTestingSupport> entry : getTheoryToTestingSupport().entrySet()) {
Map<String, Type> forThisSubTheory = mapForSubTheory.get(entry.getKey());
entry.getValue().setVariableNamesAndTypesForTesting(forThisSubTheory);
}
}
use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.
the class CompoundTheoryTestingSupport method getTheoryTestingSupport.
private TheoryTestingSupport getTheoryTestingSupport(String variable) {
Type variableType = getTestingVariableType(variable);
Theory subConstraintTheory = getTheory().getTheory(parse(variable), variableType);
check(() -> subConstraintTheory != null, () -> "There is no sub-theory suitable for " + variable + ", which has type " + variableType);
TheoryTestingSupport result = getTheoryToTestingSupport().get(subConstraintTheory);
return result;
}
use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method isSatisfiableByBruteForce.
/**
* Determines whether a formula is satisfiable by adding existential quantifiers for each of its variables
* (according to the theory provided) and evaluating it.
* @param formula
* @param theoryTestingSupport
* @param context
* @return whether the formula is satisfiable.
*/
public static boolean isSatisfiableByBruteForce(Expression formula, TheoryTestingSupport theoryTestingSupport, Context context) {
Map<String, Type> variableNamesAndTypesForTesting = theoryTestingSupport.getVariableNamesAndTypesForTesting();
Expression quantifiedFormula = formula;
Collection<Expression> variables = theoryTestingSupport.getTheory().getVariablesIn(formula, context);
for (Expression variable : variables) {
Expression typeNameExpression = parse(variableNamesAndTypesForTesting.get(variable).toString());
quantifiedFormula = ThereExists.make(IndexExpressions.makeIndexExpression(variable, typeNameExpression), quantifiedFormula);
}
Expression evaluation = new BruteForceCommonInterpreter().apply(quantifiedFormula, context);
boolean result = evaluation.equals(TRUE);
return result;
}
Aggregations