use of org.sosy_lab.java_smt.api.Model.ValueAssignment in project java-smt by sosy-lab.
the class AllSatExample method allSatBitvectors.
/**
* For bitvector formulas, we can implement the allsat-loop and collect all models when iterating.
*/
private List<List<ValueAssignment>> allSatBitvectors() throws InterruptedException, SolverException {
bfmgr = context.getFormulaManager().getBooleanFormulaManager();
bvfmgr = context.getFormulaManager().getBitvectorFormulaManager();
// formula ((1 <= a <= 3) && (0 == b) && (p == q)) with 6 models
final int bitsize = 4;
BitvectorFormula a = bvfmgr.makeVariable(bitsize, "c");
BitvectorFormula b = bvfmgr.makeVariable(bitsize, "d");
BooleanFormula p = bfmgr.makeVariable("r");
BooleanFormula q = bfmgr.makeVariable("s");
prover.addConstraint(bvfmgr.lessOrEquals(bv(bitsize, 1), a, true));
prover.addConstraint(bvfmgr.equal(bv(bitsize, 0), b));
prover.addConstraint(bvfmgr.lessOrEquals(a, bv(bitsize, 3), true));
prover.addConstraint(bfmgr.equivalence(p, q));
List<List<ValueAssignment>> models = new ArrayList<>();
// loop over all possible models for "1<=a<=3 AND p=q"
while (!prover.isUnsat()) {
final ImmutableList<ValueAssignment> modelAssignments = prover.getModelAssignments();
models.add(modelAssignments);
final List<BooleanFormula> modelAssignmentsAsFormulas = new ArrayList<>();
for (ValueAssignment va : modelAssignments) {
modelAssignmentsAsFormulas.add(va.getAssignmentAsFormula());
}
// prevent next model from using the same assignment as a previous model
prover.addConstraint(bfmgr.not(bfmgr.and(modelAssignmentsAsFormulas)));
}
return models;
}
use of org.sosy_lab.java_smt.api.Model.ValueAssignment in project java-smt by sosy-lab.
the class AllSatExample method allSatIntegers2.
/**
* For integer formulas, we can implement the allsat-loop and collect all models when iterating.
*/
private List<List<ValueAssignment>> allSatIntegers2() throws InterruptedException, SolverException {
bfmgr = context.getFormulaManager().getBooleanFormulaManager();
ifmgr = context.getFormulaManager().getIntegerFormulaManager();
// formula ((1 <= a <= 3) && (0 == b) && (p == q)) with 6 models
IntegerFormula a = ifmgr.makeVariable("a");
IntegerFormula b = ifmgr.makeVariable("b");
BooleanFormula p = bfmgr.makeVariable("p");
BooleanFormula q = bfmgr.makeVariable("q");
prover.addConstraint(ifmgr.lessOrEquals(num(1), a));
prover.addConstraint(ifmgr.equal(num(0), b));
prover.addConstraint(ifmgr.lessOrEquals(a, num(3)));
prover.addConstraint(bfmgr.equivalence(p, q));
List<List<ValueAssignment>> models = new ArrayList<>();
// loop over all possible models for "1<=a<=3 AND p=q"
while (!prover.isUnsat()) {
final ImmutableList<ValueAssignment> modelAssignments = prover.getModelAssignments();
models.add(modelAssignments);
final List<BooleanFormula> modelAssignmentsAsFormulas = new ArrayList<>();
for (ValueAssignment va : modelAssignments) {
modelAssignmentsAsFormulas.add(va.getAssignmentAsFormula());
}
// prevent next model from using the same assignment as a previous model
prover.addConstraint(bfmgr.not(bfmgr.and(modelAssignmentsAsFormulas)));
}
return models;
}
use of org.sosy_lab.java_smt.api.Model.ValueAssignment in project java-smt by sosy-lab.
the class ModelTest method testGetMultipleUFsWithBvs.
@Test
public void testGetMultipleUFsWithBvs() throws Exception {
requireBitvectors();
BitvectorFormula arg1 = bvmgr.makeVariable(8, "arg1");
BitvectorFormula arg2 = bvmgr.makeVariable(8, "arg2");
FunctionDeclaration<BitvectorFormula> declaration = fmgr.declareUF("UF", FormulaType.getBitvectorTypeWithSize(8), FormulaType.getBitvectorTypeWithSize(8));
BitvectorFormula app1 = fmgr.callUF(declaration, arg1);
BitvectorFormula app2 = fmgr.callUF(declaration, arg2);
BitvectorFormula one = bvmgr.makeBitvector(8, 1);
BitvectorFormula two = bvmgr.makeBitvector(8, 2);
BitvectorFormula three = bvmgr.makeBitvector(8, 3);
BitvectorFormula four = bvmgr.makeBitvector(8, 4);
ImmutableList<ValueAssignment> expectedModel = ImmutableList.of(new ValueAssignment(arg1, three, bvmgr.equal(arg1, three), "arg1", BigInteger.valueOf(3), ImmutableList.of()), new ValueAssignment(arg2, four, bvmgr.equal(arg2, four), "arg2", BigInteger.valueOf(4), ImmutableList.of()), new ValueAssignment(fmgr.callUF(declaration, three), one, bvmgr.equal(fmgr.callUF(declaration, three), one), "UF", BigInteger.valueOf(1), ImmutableList.of(BigInteger.valueOf(3))), new ValueAssignment(fmgr.callUF(declaration, four), bvmgr.makeBitvector(8, 2), bvmgr.equal(fmgr.callUF(declaration, four), two), "UF", BigInteger.valueOf(2), ImmutableList.of(BigInteger.valueOf(4))));
try (ProverEnvironment prover = context.newProverEnvironment(ProverOptions.GENERATE_MODELS)) {
prover.push(bmgr.and(bvmgr.equal(app1, bvmgr.makeBitvector(8, 1)), bvmgr.equal(app2, bvmgr.makeBitvector(8, 2))));
prover.push(bvmgr.equal(arg1, bvmgr.makeBitvector(8, 3)));
prover.push(bvmgr.equal(arg2, bvmgr.makeBitvector(8, 4)));
assertThat(prover).isSatisfiable();
try (Model m = prover.getModel()) {
assertThat(m.evaluate(app1)).isEqualTo(BigInteger.ONE);
assertThat(m.evaluate(app2)).isEqualTo(BigInteger.valueOf(2));
assertThat(m).containsExactlyElementsIn(expectedModel);
}
assertThat(prover.getModelAssignments()).containsExactlyElementsIn(expectedModel);
}
}
use of org.sosy_lab.java_smt.api.Model.ValueAssignment in project java-smt by sosy-lab.
the class ModelTest method testGetArrays7.
@Test
public void testGetArrays7() throws SolverException, InterruptedException {
requireArrays();
requireIntegers();
ArrayFormula<IntegerFormula, IntegerFormula> array1 = amgr.makeArray("array", IntegerType, IntegerType);
IntegerFormula selected = amgr.select(array1, imgr.makeNumber(1));
BooleanFormula selectEq0 = imgr.equal(selected, imgr.makeNumber(0));
// Note that store is not an assignment! This is just so that the implication fails and arr[1] =
// 0
BooleanFormula selectStore = imgr.equal(amgr.select(amgr.store(array1, imgr.makeNumber(1), imgr.makeNumber(7)), imgr.makeNumber(1)), imgr.makeNumber(0));
BooleanFormula assert1 = bmgr.implication(bmgr.not(selectEq0), selectStore);
try (ProverEnvironment prover = context.newProverEnvironment(ProverOptions.GENERATE_MODELS)) {
prover.push(assert1);
assertThat(prover).isSatisfiable();
try (Model m = prover.getModel()) {
for (@SuppressWarnings("unused") ValueAssignment assignment : m) {
// Check that we can iterate through with no crashes.
}
assertThat(m.evaluate(selected)).isEqualTo(BigInteger.ZERO);
}
}
}
use of org.sosy_lab.java_smt.api.Model.ValueAssignment in project java-smt by sosy-lab.
the class ModelTest method testGetMultipleUFsWithInts.
@Test
public void testGetMultipleUFsWithInts() throws Exception {
requireIntegers();
IntegerFormula arg1 = imgr.makeVariable("arg1");
IntegerFormula arg2 = imgr.makeVariable("arg2");
FunctionDeclaration<IntegerFormula> declaration = fmgr.declareUF("UF", IntegerType, IntegerType);
IntegerFormula app1 = fmgr.callUF(declaration, arg1);
IntegerFormula app2 = fmgr.callUF(declaration, arg2);
IntegerFormula one = imgr.makeNumber(1);
IntegerFormula two = imgr.makeNumber(2);
IntegerFormula three = imgr.makeNumber(3);
IntegerFormula four = imgr.makeNumber(4);
ImmutableList<ValueAssignment> expectedModel = ImmutableList.of(new ValueAssignment(arg1, three, imgr.equal(arg1, three), "arg1", BigInteger.valueOf(3), ImmutableList.of()), new ValueAssignment(arg2, four, imgr.equal(arg2, four), "arg2", BigInteger.valueOf(4), ImmutableList.of()), new ValueAssignment(fmgr.callUF(declaration, three), one, imgr.equal(fmgr.callUF(declaration, three), one), "UF", BigInteger.valueOf(1), ImmutableList.of(BigInteger.valueOf(3))), new ValueAssignment(fmgr.callUF(declaration, four), imgr.makeNumber(2), imgr.equal(fmgr.callUF(declaration, four), two), "UF", BigInteger.valueOf(2), ImmutableList.of(BigInteger.valueOf(4))));
try (ProverEnvironment prover = context.newProverEnvironment(ProverOptions.GENERATE_MODELS)) {
prover.push(bmgr.and(imgr.equal(app1, imgr.makeNumber(1)), imgr.equal(app2, imgr.makeNumber(2))));
prover.push(imgr.equal(arg1, imgr.makeNumber(3)));
prover.push(imgr.equal(arg2, imgr.makeNumber(4)));
assertThat(prover).isSatisfiable();
try (Model m = prover.getModel()) {
assertThat(m.evaluate(app1)).isEqualTo(BigInteger.ONE);
assertThat(m.evaluate(app2)).isEqualTo(BigInteger.valueOf(2));
assertThat(m).containsExactlyElementsIn(expectedModel);
}
assertThat(prover.getModelAssignments()).containsExactlyElementsIn(expectedModel);
}
}
Aggregations