use of org.sosy_lab.java_smt.api.ProverEnvironment in project java-smt by sosy-lab.
the class AllSatExample method main.
public static void main(String... args) throws InvalidConfigurationException, SolverException, InterruptedException {
Configuration config = Configuration.defaultConfiguration();
LogManager logger = BasicLogManager.create(config);
ShutdownNotifier notifier = ShutdownNotifier.createDummy();
for (Solvers solver : Solvers.values()) {
try (SolverContext context = SolverContextFactory.createSolverContext(config, logger, notifier, solver);
ProverEnvironment prover = context.newProverEnvironment(ProverOptions.GENERATE_MODELS, ProverOptions.GENERATE_ALL_SAT)) {
logger.log(Level.WARNING, "Using solver " + solver + " in version " + context.getVersion());
AllSatExample ase = new AllSatExample(context, prover);
prover.push();
logger.log(Level.INFO, ase.allSatBooleans1());
prover.pop();
prover.push();
logger.log(Level.INFO, ase.allSatBooleans2());
prover.pop();
if (SOLVERS_WITH_INTEGERS.contains(solver)) {
prover.push();
logger.log(Level.INFO, ase.allSatIntegers());
prover.pop();
prover.push();
logger.log(Level.INFO, ase.allSatIntegers2());
prover.pop();
}
if (SOLVERS_WITH_BITVECTORS.contains(solver)) {
prover.push();
logger.log(Level.INFO, ase.allSatBitvectors());
prover.pop();
}
} catch (InvalidConfigurationException | UnsatisfiedLinkError e) {
// on some machines we support only some solvers,
// thus we can ignore these errors.
logger.logUserException(Level.INFO, e, "Solver " + solver + " is not available.");
} catch (UnsupportedOperationException e) {
logger.logUserException(Level.INFO, e, e.getMessage());
}
}
}
use of org.sosy_lab.java_smt.api.ProverEnvironment in project java-smt by sosy-lab.
the class BoolectorNativeApiTest method satSolverBackendTest.
/**
* For each available solver, we build a context and solver a small formula.
*
* <p>This should be sufficient to test whether the sat-solver can be loaded.
*/
@Test
public void satSolverBackendTest() throws InvalidConfigurationException, InterruptedException, SolverException {
for (SatSolver satsolver : BoolectorSolverContext.SatSolver.values()) {
ConfigurationBuilder config = Configuration.builder().setOption("solver.boolector.satSolver", satsolver.name());
try (BoolectorSolverContext context = BoolectorSolverContext.create(config.build(), ShutdownNotifier.createDummy(), null, 1, NativeLibraries::loadLibrary)) {
BooleanFormulaManager bfmgr = context.getFormulaManager().getBooleanFormulaManager();
BooleanFormula fa = bfmgr.makeVariable("a");
BooleanFormula fb = bfmgr.makeVariable("b");
BooleanFormula fc = bfmgr.makeVariable("c");
BooleanFormula f1 = bfmgr.or(fa, fb, fc);
BooleanFormula f2 = bfmgr.and(fa, fb, fc);
try (ProverEnvironment prover = context.newProverEnvironment()) {
prover.addConstraint(bfmgr.equivalence(f1, f2));
assertThat(prover.isUnsat()).isFalse();
}
}
}
}
use of org.sosy_lab.java_smt.api.ProverEnvironment in project java-smt by sosy-lab.
the class BoolectorNativeApiTest method dumpVariableWithAssertionsOnStackTest.
@Test
public void dumpVariableWithAssertionsOnStackTest() throws InvalidConfigurationException, InterruptedException {
ConfigurationBuilder config = Configuration.builder();
try (BoolectorSolverContext context = BoolectorSolverContext.create(config.build(), ShutdownNotifier.createDummy(), null, 1, NativeLibraries::loadLibrary)) {
FormulaManager mgr = context.getFormulaManager();
BooleanFormulaManager bfmgr = mgr.getBooleanFormulaManager();
try (ProverEnvironment prover = context.newProverEnvironment()) {
prover.push(bfmgr.makeVariable("x"));
for (String name : ImmutableList.of("a", "a", "b", "abc", "ABC")) {
BooleanFormula f = bfmgr.makeVariable(name);
String s = mgr.dumpFormula(f).toString();
// TODO why is there a prefix "BTOR_2@"?
// Possible reason: we are on the second level of the solver stack.
// - first level comes from the constructor of ReusableStackTheoremProver.
// - second level comes from the PUSH above.
// We do actually not want to have such names in the dump.
assertThat(s).contains(String.format("(declare-fun BTOR_2@%s () (_ BitVec 1))", name));
// assertThat(s).contains(String.format("(assert "));
}
}
}
}
use of org.sosy_lab.java_smt.api.ProverEnvironment in project java-smt by sosy-lab.
the class BooleanFormulaSubject method checkIsUnsat.
private void checkIsUnsat(final BooleanFormula subject, final Fact expected) throws SolverException, InterruptedException {
try (ProverEnvironment prover = context.newProverEnvironment(ProverOptions.GENERATE_MODELS)) {
prover.push(subject);
if (prover.isUnsat()) {
// success
return;
}
// get model for failure message
try (Model model = prover.getModel()) {
List<Fact> facts = new ArrayList<>();
facts.add(Fact.fact("but was", formulaUnderTest));
if (!subject.equals(formulaUnderTest)) {
facts.add(Fact.fact("checked formula was", subject));
}
facts.add(Fact.fact("which has model", model));
failWithoutActual(expected, facts.toArray(new Fact[0]));
}
}
}
use of org.sosy_lab.java_smt.api.ProverEnvironment in project java-smt by sosy-lab.
the class SolverTheoriesTest method multiplicationSquares.
@Test
public void multiplicationSquares() throws SolverException, InterruptedException {
requireIntegers();
IntegerFormula i2 = imgr.makeNumber(2);
IntegerFormula i3 = imgr.makeNumber(3);
IntegerFormula i4 = imgr.makeNumber(4);
IntegerFormula i5 = imgr.makeNumber(5);
IntegerFormula x = imgr.makeVariable("x");
IntegerFormula y = imgr.makeVariable("y");
IntegerFormula z = imgr.makeVariable("z");
IntegerFormula xx;
IntegerFormula yy;
IntegerFormula zz;
try {
xx = imgr.multiply(x, x);
yy = imgr.multiply(y, y);
zz = imgr.multiply(z, z);
} catch (UnsupportedOperationException e) {
// to support non-linear arithmetic, we can then skip the test completely
throw new AssumptionViolatedException("Support for non-linear arithmetic is optional", e);
}
try (ProverEnvironment env = context.newProverEnvironment()) {
// check x*x + y*y = z*z
env.push(imgr.equal(zz, imgr.add(xx, yy)));
{
// SAT with x=4 and y=3
env.push(imgr.equal(x, i3));
env.push(imgr.equal(y, i4));
assertThat(env).isSatisfiable();
env.pop();
env.pop();
}
{
// SAT with z=5
env.push(imgr.equal(z, i5));
assertThat(env).isSatisfiable();
env.pop();
}
{
// UNSAT with z=5 and x=2
env.push(imgr.equal(z, i5));
env.push(imgr.equal(x, i2));
assertThat(env).isUnsatisfiable();
env.pop();
env.pop();
}
{
// UNSAT with z=5 and x>3 and y>3
env.push(imgr.equal(z, i5));
env.push(imgr.greaterThan(x, i3));
env.push(imgr.greaterThan(y, i3));
assertThat(env).isUnsatisfiable();
env.pop();
env.pop();
env.pop();
}
}
}
Aggregations