From 403afed211488ea9a54a64207f63841d95ce6cc4 Mon Sep 17 00:00:00 2001 From: ros Date: Mon, 26 Jun 2023 15:51:03 +0200 Subject: [PATCH 001/133] Apron Prototyp und kleines Beispiel --- .../apron/ApronBooleanFormulaManager.java | 80 +++++++++ .../java_smt/solvers/apron/ApronExamples.java | 75 +++++++++ .../solvers/apron/ApronFormulaCreator.java | 86 ++++++++++ .../solvers/apron/ApronFormulaManager.java | 92 ++++++++++ .../apron/ApronIntegerFormulaManager.java | 157 ++++++++++++++++++ .../solvers/apron/ApronNativeApiTest.java | 43 +++++ .../apron/ApronNumeralFormulaManager.java | 125 ++++++++++++++ .../apron/ApronRationalFormulaManager.java | 141 ++++++++++++++++ .../solvers/apron/ApronSolverContext.java | 70 ++++++++ .../solvers/apron/ApronTheoremProver.java | 97 +++++++++++ .../solvers/apron/ApronUFManager.java | 30 ++++ 11 files changed, 996 insertions(+) create mode 100644 src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java create mode 100644 src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java create mode 100644 src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java create mode 100644 src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java create mode 100644 src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java create mode 100644 src/org/sosy_lab/java_smt/solvers/apron/ApronNativeApiTest.java create mode 100644 src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java create mode 100644 src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java create mode 100644 src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java create mode 100644 src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java create mode 100644 src/org/sosy_lab/java_smt/solvers/apron/ApronUFManager.java diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java new file mode 100644 index 0000000000..2c294e7e62 --- /dev/null +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java @@ -0,0 +1,80 @@ +/* + * JavaSMT is an API wrapper for a collection of SMT solvers. + * This file is part of JavaSMT. + * + * Copyright (C) 2007-2016 Dirk Beyer + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sosy_lab.java_smt.solvers.apron; + +import org.sosy_lab.java_smt.basicimpl.AbstractBooleanFormulaManager; +import org.sosy_lab.java_smt.basicimpl.FormulaCreator; + +public class ApronBooleanFormulaManager extends AbstractBooleanFormulaManager { + protected ApronBooleanFormulaManager(FormulaCreator pCreator) { + super(pCreator); + } + + @Override + protected Object makeVariableImpl(String pVar) { + return null; + } + + @Override + protected Object makeBooleanImpl(boolean value) { + return null; + } + + @Override + protected Object not(Object pParam1) { + return null; + } + + @Override + protected Object and(Object pParam1, Object pParam2) { + return null; + } + + @Override + protected Object or(Object pParam1, Object pParam2) { + return null; + } + + @Override + protected Object xor(Object pParam1, Object pParam2) { + return null; + } + + @Override + protected Object equivalence(Object bits1, Object bits2) { + return null; + } + + @Override + protected boolean isTrue(Object bits) { + return false; + } + + @Override + protected boolean isFalse(Object bits) { + return false; + } + + @Override + protected Object ifThenElse(Object cond, Object f1, Object f2) { + return null; + } +} diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java new file mode 100644 index 0000000000..bfece0e87b --- /dev/null +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java @@ -0,0 +1,75 @@ +/* + * JavaSMT is an API wrapper for a collection of SMT solvers. + * This file is part of JavaSMT. + * + * Copyright (C) 2007-2016 Dirk Beyer + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sosy_lab.java_smt.solvers.apron; + +import java.util.Arrays; +import apron.*; +import org.junit.AssumptionViolatedException; +import org.sosy_lab.common.NativeLibraries; + +/** + * Simple examples about the Apron Library. Inspired by + * ... + */ +public class ApronExamples +{ + private static void testBox(Manager pManager) throws ApronException { + String[] intVars = {"x"}; + String[] realVars = {}; + + Environment environment = new Environment(intVars, realVars); + //x <= 2 and x >= -3 + + //x <= 2 --> -x+2 >= 0 + Lincons1 cons1 = new Lincons1(environment); + cons1.setCoeff("x",new MpqScalar(-1)); + cons1.setCst(new MpqScalar(+2)); + cons1.setKind(Lincons1.SUPEQ); + //x >= 3 --> x-3 >= 0 + Lincons1 cons2 = new Lincons1(environment); + cons2.setCoeff("x",new MpqScalar(1)); + cons2.setCst(new MpqScalar(-3)); + cons2.setKind(Lincons1.SUPEQ); + + Lincons1[] constraints = {cons1, cons2}; + Abstract1 abstract1 = new Abstract1(pManager, constraints); + //is x = 1 satisfiable? + Lincons1 cons3 = new Lincons1(environment); + cons3.setCoeff("x",new MpqScalar(1)); + cons3.setCst(new MpqScalar(-1)); + cons3.setKind(Lincons1.EQ); + System.out.println("Constraint is satisfiable: "+abstract1.satisfy(pManager, cons3)); + + //always unsat example, 1 = 0 + Lincons1 cons4 = new Lincons1(environment); + cons4.setCoeff("x", new MpqScalar(0)); + cons4.setCst(new MpqScalar(1)); + cons4.setKind(Lincons1.EQ); + Abstract1 abstract2 = new Abstract1(pManager, new Lincons1[]{cons4}); + System.out.println("Abstract-Obj. is Bottom: "+abstract2.isBottom(pManager)); + } + + public static void main(String[] args) throws ApronException { + Manager manager = new Box(); + testBox(manager); + } +} + diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java new file mode 100644 index 0000000000..182a893218 --- /dev/null +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java @@ -0,0 +1,86 @@ +/* + * JavaSMT is an API wrapper for a collection of SMT solvers. + * This file is part of JavaSMT. + * + * Copyright (C) 2007-2016 Dirk Beyer + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sosy_lab.java_smt.solvers.apron; + +import java.util.List; +import javax.annotation.Nullable; +import org.sosy_lab.java_smt.api.Formula; +import org.sosy_lab.java_smt.api.FormulaType; +import org.sosy_lab.java_smt.api.FormulaType.FloatingPointType; +import org.sosy_lab.java_smt.api.visitors.FormulaVisitor; +import org.sosy_lab.java_smt.basicimpl.FormulaCreator; + +public class ApronFormulaCreator extends FormulaCreator { + protected ApronFormulaCreator( + Object pO, + Object boolType, + @Nullable Object pIntegerType, + @Nullable Object pRationalType, + @Nullable Object stringType, + @Nullable Object regexType) { + super(pO, boolType, pIntegerType, pRationalType, stringType, regexType); + } + + @Override + public Object getBitvectorType(int bitwidth) { + return null; + } + + @Override + public Object getFloatingPointType(FloatingPointType type) { + return null; + } + + @Override + public Object getArrayType(Object indexType, Object elementType) { + return null; + } + + @Override + public Object makeVariable(Object pO, String varName) { + return null; + } + + @Override + public FormulaType getFormulaType(Object formula) { + return null; + } + + @Override + public Object callFunctionImpl(Object declaration, List args) { + return null; + } + + @Override + public Object declareUFImpl(String pName, Object pReturnType, List pArgTypes) { + return null; + } + + @Override + protected Object getBooleanVarDeclarationImpl(Object pO) { + return null; + } + + @Override + public Object visit(FormulaVisitor visitor, Formula formula, Object f) { + return null; + } +} diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java new file mode 100644 index 0000000000..6b116a0f95 --- /dev/null +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java @@ -0,0 +1,92 @@ +/* + * JavaSMT is an API wrapper for a collection of SMT solvers. + * This file is part of JavaSMT. + * + * Copyright (C) 2007-2016 Dirk Beyer + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sosy_lab.java_smt.solvers.apron; + +import java.util.Map; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.sosy_lab.common.Appender; +import org.sosy_lab.java_smt.api.BooleanFormula; +import org.sosy_lab.java_smt.api.Formula; +import org.sosy_lab.java_smt.api.IntegerFormulaManager; +import org.sosy_lab.java_smt.api.RationalFormulaManager; +import org.sosy_lab.java_smt.basicimpl.AbstractArrayFormulaManager; +import org.sosy_lab.java_smt.basicimpl.AbstractBitvectorFormulaManager; +import org.sosy_lab.java_smt.basicimpl.AbstractBooleanFormulaManager; +import org.sosy_lab.java_smt.basicimpl.AbstractEnumerationFormulaManager; +import org.sosy_lab.java_smt.basicimpl.AbstractFloatingPointFormulaManager; +import org.sosy_lab.java_smt.basicimpl.AbstractFormulaManager; +import org.sosy_lab.java_smt.basicimpl.AbstractQuantifiedFormulaManager; +import org.sosy_lab.java_smt.basicimpl.AbstractSLFormulaManager; +import org.sosy_lab.java_smt.basicimpl.AbstractStringFormulaManager; +import org.sosy_lab.java_smt.basicimpl.AbstractUFManager; +import org.sosy_lab.java_smt.basicimpl.FormulaCreator; + +public class ApronFormulaManager extends AbstractFormulaManager { + /** + * Builds a solver from the given theory implementations. + * + * @param pFormulaCreator + * @param functionManager + * @param booleanManager + * @param pIntegerManager + * @param pRationalManager + * @param bitvectorManager + * @param floatingPointManager + * @param quantifiedManager + * @param arrayManager + * @param slManager + * @param strManager + * @param enumManager + */ + protected ApronFormulaManager( + FormulaCreator pFormulaCreator, + AbstractUFManager functionManager, + AbstractBooleanFormulaManager booleanManager, + @Nullable IntegerFormulaManager pIntegerManager, + @Nullable RationalFormulaManager pRationalManager, + @Nullable AbstractBitvectorFormulaManager bitvectorManager, + @Nullable AbstractFloatingPointFormulaManager floatingPointManager, + @Nullable AbstractQuantifiedFormulaManager quantifiedManager, + @Nullable AbstractArrayFormulaManager arrayManager, + @Nullable AbstractSLFormulaManager slManager, + @Nullable AbstractStringFormulaManager strManager, + @Nullable AbstractEnumerationFormulaManager enumManager) { + super(pFormulaCreator, functionManager, booleanManager, pIntegerManager, pRationalManager, + bitvectorManager, floatingPointManager, quantifiedManager, arrayManager, slManager, + strManager, enumManager); + } + + @Override + public BooleanFormula parse(String s) throws IllegalArgumentException { + return null; + } + + @Override + public T substitute(T f, + Map fromToMapping) { + return null; + } + + @Override + public Appender dumpFormula(Long t) { + return null; + } +} diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java new file mode 100644 index 0000000000..0d380209c6 --- /dev/null +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java @@ -0,0 +1,157 @@ +/* + * JavaSMT is an API wrapper for a collection of SMT solvers. + * This file is part of JavaSMT. + * + * Copyright (C) 2007-2016 Dirk Beyer + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sosy_lab.java_smt.solvers.apron; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.List; +import org.sosy_lab.common.rationals.Rational; +import org.sosy_lab.java_smt.api.BooleanFormula; +import org.sosy_lab.java_smt.api.IntegerFormulaManager; +import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula; +import org.sosy_lab.java_smt.basicimpl.FormulaCreator; + +public class ApronIntegerFormulaManager extends ApronNumeralFormulaManager + implements IntegerFormulaManager { + protected ApronIntegerFormulaManager( + FormulaCreator pCreator, + NonLinearArithmetic pNonLinearArithmetic) { + super(pCreator, pNonLinearArithmetic); + } + + @Override + public BooleanFormula modularCongruence( + IntegerFormula number1, + IntegerFormula number2, + BigInteger n) { + return null; + } + + @Override + public BooleanFormula modularCongruence(IntegerFormula number1, IntegerFormula number2, long n) { + return null; + } + + @Override + public IntegerFormula modulo(IntegerFormula numerator, IntegerFormula denumerator) { + return null; + } + + @Override + public IntegerFormula makeNumber(long number) { + return null; + } + + @Override + public IntegerFormula makeNumber(BigInteger number) { + return null; + } + + @Override + public IntegerFormula makeNumber(double number) { + return null; + } + + @Override + public IntegerFormula makeNumber(BigDecimal number) { + return null; + } + + @Override + public IntegerFormula makeNumber(String pI) { + return null; + } + + @Override + public IntegerFormula makeNumber(Rational pRational) { + return null; + } + + @Override + public IntegerFormula makeVariable(String pVar) { + return null; + } + + @Override + public IntegerFormula negate(IntegerFormula number) { + return null; + } + + @Override + public IntegerFormula add(IntegerFormula number1, IntegerFormula number2) { + return null; + } + + @Override + public IntegerFormula sum(List operands) { + return null; + } + + @Override + public IntegerFormula subtract(IntegerFormula number1, IntegerFormula number2) { + return null; + } + + @Override + public IntegerFormula divide(IntegerFormula numerator, IntegerFormula denumerator) { + return null; + } + + @Override + public IntegerFormula multiply(IntegerFormula number1, IntegerFormula number2) { + return null; + } + + @Override + public BooleanFormula equal(IntegerFormula number1, IntegerFormula number2) { + return null; + } + + @Override + public BooleanFormula distinct(List pNumbers) { + return null; + } + + @Override + public BooleanFormula greaterThan(IntegerFormula number1, IntegerFormula number2) { + return null; + } + + @Override + public BooleanFormula greaterOrEquals(IntegerFormula number1, IntegerFormula number2) { + return null; + } + + @Override + public BooleanFormula lessThan(IntegerFormula number1, IntegerFormula number2) { + return null; + } + + @Override + public BooleanFormula lessOrEquals(IntegerFormula number1, IntegerFormula number2) { + return null; + } + + @Override + public IntegerFormula floor(IntegerFormula formula) { + return null; + } +} diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronNativeApiTest.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronNativeApiTest.java new file mode 100644 index 0000000000..3ced6b4332 --- /dev/null +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronNativeApiTest.java @@ -0,0 +1,43 @@ +/* + * JavaSMT is an API wrapper for a collection of SMT solvers. + * This file is part of JavaSMT. + * + * Copyright (C) 2007-2016 Dirk Beyer + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sosy_lab.java_smt.solvers.apron; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.internal.AssumptionViolatedException; +import org.sosy_lab.common.NativeLibraries; + +public class ApronNativeApiTest { + + @BeforeClass + public static void load(){ + try { + NativeLibraries.loadLibrary("apron"); + System.out.println("Apron-Library is loaded."); + } catch (UnsatisfiedLinkError e){ + throw new AssumptionViolatedException("Apron not availible: ", e); + } + } + + @Test + public void solverBackendTest(){ + } +} diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java new file mode 100644 index 0000000000..a5a84cddf8 --- /dev/null +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java @@ -0,0 +1,125 @@ +/* + * JavaSMT is an API wrapper for a collection of SMT solvers. + * This file is part of JavaSMT. + * + * Copyright (C) 2007-2016 Dirk Beyer + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sosy_lab.java_smt.solvers.apron; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.List; +import org.sosy_lab.java_smt.api.FormulaType; +import org.sosy_lab.java_smt.api.NumeralFormula; +import org.sosy_lab.java_smt.basicimpl.AbstractNumeralFormulaManager; +import org.sosy_lab.java_smt.basicimpl.FormulaCreator; + +public class ApronNumeralFormulaManager< + ParamFormulaType extends NumeralFormula, ResultFormulaType extends NumeralFormula> + extends AbstractNumeralFormulaManager< + Long, Long, Long, ParamFormulaType, ResultFormulaType, Long> { + protected ApronNumeralFormulaManager( + FormulaCreator pCreator, + NonLinearArithmetic pNonLinearArithmetic) { + super(pCreator, pNonLinearArithmetic); + } + + @Override + public FormulaType getFormulaType() { + return null; + } + + @Override + protected boolean isNumeral(Long val) { + return false; + } + + @Override + protected Long makeNumberImpl(long i) { + return null; + } + + @Override + protected Long makeNumberImpl(BigInteger i) { + return null; + } + + @Override + protected Long makeNumberImpl(String i) { + return null; + } + + @Override + protected Long makeNumberImpl(double pNumber) { + return null; + } + + @Override + protected Long makeNumberImpl(BigDecimal pNumber) { + return null; + } + + @Override + protected Long makeVariableImpl(String i) { + return null; + } + + @Override + protected Long negate(Long pParam1) { + return null; + } + + @Override + protected Long add(Long pParam1, Long pParam2) { + return null; + } + + @Override + protected Long subtract(Long pParam1, Long pParam2) { + return null; + } + + @Override + protected Long equal(Long pParam1, Long pParam2) { + return null; + } + + @Override + protected Long distinctImpl(List pNumbers) { + return null; + } + + @Override + protected Long greaterThan(Long pParam1, Long pParam2) { + return null; + } + + @Override + protected Long greaterOrEquals(Long pParam1, Long pParam2) { + return null; + } + + @Override + protected Long lessThan(Long pParam1, Long pParam2) { + return null; + } + + @Override + protected Long lessOrEquals(Long pParam1, Long pParam2) { + return null; + } +} diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java new file mode 100644 index 0000000000..c342e67b3f --- /dev/null +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java @@ -0,0 +1,141 @@ +/* + * JavaSMT is an API wrapper for a collection of SMT solvers. + * This file is part of JavaSMT. + * + * Copyright (C) 2007-2016 Dirk Beyer + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sosy_lab.java_smt.solvers.apron; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.List; +import org.sosy_lab.common.rationals.Rational; +import org.sosy_lab.java_smt.api.BooleanFormula; +import org.sosy_lab.java_smt.api.NumeralFormula; +import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula; +import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula; +import org.sosy_lab.java_smt.api.RationalFormulaManager; +import org.sosy_lab.java_smt.basicimpl.FormulaCreator; + +public class ApronRationalFormulaManager extends ApronNumeralFormulaManager implements RationalFormulaManager { + protected ApronRationalFormulaManager( + FormulaCreator pCreator, + NonLinearArithmetic pNonLinearArithmetic) { + super(pCreator, pNonLinearArithmetic); + } + + @Override + public RationalFormula makeNumber(long number) { + return null; + } + + @Override + public RationalFormula makeNumber(BigInteger number) { + return null; + } + + @Override + public RationalFormula makeNumber(double number) { + return null; + } + + @Override + public RationalFormula makeNumber(BigDecimal number) { + return null; + } + + @Override + public RationalFormula makeNumber(String pI) { + return null; + } + + @Override + public RationalFormula makeNumber(Rational pRational) { + return null; + } + + @Override + public RationalFormula makeVariable(String pVar) { + return null; + } + + @Override + public RationalFormula negate(NumeralFormula number) { + return null; + } + + @Override + public RationalFormula add(NumeralFormula number1, NumeralFormula number2) { + return null; + } + + @Override + public RationalFormula sum(List operands) { + return null; + } + + @Override + public RationalFormula subtract(NumeralFormula number1, NumeralFormula number2) { + return null; + } + + @Override + public RationalFormula divide(NumeralFormula numerator, NumeralFormula denumerator) { + return null; + } + + @Override + public RationalFormula multiply(NumeralFormula number1, NumeralFormula number2) { + return null; + } + + @Override + public BooleanFormula equal(NumeralFormula number1, NumeralFormula number2) { + return null; + } + + @Override + public BooleanFormula distinct(List pNumbers) { + return null; + } + + @Override + public BooleanFormula greaterThan(NumeralFormula number1, NumeralFormula number2) { + return null; + } + + @Override + public BooleanFormula greaterOrEquals(NumeralFormula number1, NumeralFormula number2) { + return null; + } + + @Override + public BooleanFormula lessThan(NumeralFormula number1, NumeralFormula number2) { + return null; + } + + @Override + public BooleanFormula lessOrEquals(NumeralFormula number1, NumeralFormula number2) { + return null; + } + + @Override + public IntegerFormula floor(NumeralFormula formula) { + return null; + } +} diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java new file mode 100644 index 0000000000..bc2c5008d6 --- /dev/null +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java @@ -0,0 +1,70 @@ +/* + * JavaSMT is an API wrapper for a collection of SMT solvers. + * This file is part of JavaSMT. + * + * Copyright (C) 2007-2016 Dirk Beyer + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sosy_lab.java_smt.solvers.apron; + +import java.util.Set; +import org.sosy_lab.java_smt.SolverContextFactory.Solvers; +import org.sosy_lab.java_smt.api.FormulaManager; +import org.sosy_lab.java_smt.api.InterpolatingProverEnvironment; +import org.sosy_lab.java_smt.api.OptimizationProverEnvironment; +import org.sosy_lab.java_smt.api.ProverEnvironment; +import org.sosy_lab.java_smt.basicimpl.AbstractSolverContext; + +public class ApronSolverContext extends AbstractSolverContext { + protected ApronSolverContext(FormulaManager fmgr) { + super(fmgr); + } + + @Override + public String getVersion() { + return null; + } + + @Override + public Solvers getSolverName() { + return null; + } + + @Override + public void close() { + + } + + @Override + protected ProverEnvironment newProverEnvironment0(Set options) { + return null; + } + + @Override + protected InterpolatingProverEnvironment newProverEnvironmentWithInterpolation0(Set pSet) { + return null; + } + + @Override + protected OptimizationProverEnvironment newOptimizationProverEnvironment0(Set pSet) { + return null; + } + + @Override + protected boolean supportsAssumptionSolving() { + return false; + } +} diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java new file mode 100644 index 0000000000..4427755eb5 --- /dev/null +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java @@ -0,0 +1,97 @@ +/* + * JavaSMT is an API wrapper for a collection of SMT solvers. + * This file is part of JavaSMT. + * + * Copyright (C) 2007-2016 Dirk Beyer + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sosy_lab.java_smt.solvers.apron; + +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import javax.annotation.Nullable; +import org.sosy_lab.common.ShutdownNotifier; +import org.sosy_lab.java_smt.api.BooleanFormula; +import org.sosy_lab.java_smt.api.BooleanFormulaManager; +import org.sosy_lab.java_smt.api.Evaluator; +import org.sosy_lab.java_smt.api.Model; +import org.sosy_lab.java_smt.api.ProverEnvironment; +import org.sosy_lab.java_smt.api.SolverException; +import org.sosy_lab.java_smt.basicimpl.AbstractProverWithAllSat; + +public class ApronTheoremProver extends AbstractProverWithAllSat implements ProverEnvironment { + protected ApronTheoremProver( + Set pSet, + BooleanFormulaManager pBmgr, + ShutdownNotifier pShutdownNotifier) { + super(pSet, pBmgr, pShutdownNotifier); + } + + @Override + public void pop() { + + } + + @Nullable + @Override + public @org.checkerframework.checker.nullness.qual.Nullable Void addConstraint(BooleanFormula constraint) throws InterruptedException { + return null; + } + + @Override + public void push() throws InterruptedException { + + } + + @Override + public int size() { + return 0; + } + + @Override + public boolean isUnsat() throws SolverException, InterruptedException { + return false; + } + + @Override + public Model getModel() throws SolverException { + return null; + } + + @Override + public List getUnsatCore() { + return null; + } + + @Override + public Optional> unsatCoreOverAssumptions(Collection assumptions) + throws SolverException, InterruptedException { + return Optional.empty(); + } + + @Override + public boolean isUnsatWithAssumptions(Collection assumptions) + throws SolverException, InterruptedException { + return false; + } + + @Override + protected Evaluator getEvaluatorWithoutChecks() throws SolverException { + return null; + } +} diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronUFManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronUFManager.java new file mode 100644 index 0000000000..95eaff71e9 --- /dev/null +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronUFManager.java @@ -0,0 +1,30 @@ +/* + * JavaSMT is an API wrapper for a collection of SMT solvers. + * This file is part of JavaSMT. + * + * Copyright (C) 2007-2016 Dirk Beyer + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sosy_lab.java_smt.solvers.apron; + +import org.sosy_lab.java_smt.basicimpl.AbstractUFManager; +import org.sosy_lab.java_smt.basicimpl.FormulaCreator; + +public class ApronUFManager extends AbstractUFManager { + protected ApronUFManager(FormulaCreator pCreator) { + super(pCreator); + } +} From 70e0ef02ca76e7bc890be653262d30474950424b Mon Sep 17 00:00:00 2001 From: winnieros Date: Fri, 21 Jul 2023 12:00:38 +0200 Subject: [PATCH 002/133] SolverContext, Type logic, Formular Creator beginnings --- .idea/JavaSMT.iml | 228 +----------------- .idea/ant.xml | 13 +- .idea/codeStyles/Project.xml | 185 ++++++++++++++ .idea/codeStyles/codeStyleConfig.xml | 5 + .idea/modules.xml | 13 +- .../java_smt/SolverContextFactory.java | 7 +- .../apron/ApronBooleanFormulaManager.java | 5 +- .../java_smt/solvers/apron/ApronExamples.java | 12 +- .../solvers/apron/ApronFormulaCreator.java | 84 +++++-- .../solvers/apron/ApronFormulaManager.java | 20 +- .../apron/ApronIntegerFormulaManager.java | 77 ++---- .../java_smt/solvers/apron/ApronModel.java | 46 ++++ .../apron/ApronNumeralFormulaManager.java | 94 +------- .../apron/ApronRationalFormulaManager.java | 60 ++--- .../solvers/apron/ApronSolverContext.java | 67 ++++- .../solvers/apron/ApronTheoremProver.java | 26 +- .../solvers/apron/types/ApronFormulaType.java | 62 +++++ .../solvers/apron/types/ApronFormulas.java | 94 ++++++++ 18 files changed, 633 insertions(+), 465 deletions(-) create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java create mode 100644 src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulaType.java create mode 100644 src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulas.java diff --git a/.idea/JavaSMT.iml b/.idea/JavaSMT.iml index 4857c85527..9d56bfe86d 100644 --- a/.idea/JavaSMT.iml +++ b/.idea/JavaSMT.iml @@ -1,15 +1,4 @@ - - - @@ -313,210 +302,15 @@ SPDX-License-Identifier: Apache-2.0 + + + + + + + + + + - - - - - - + \ No newline at end of file diff --git a/.idea/ant.xml b/.idea/ant.xml index 3e3b7e7277..52ce37416f 100644 --- a/.idea/ant.xml +++ b/.idea/ant.xml @@ -1,19 +1,8 @@ - - - - + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000000..2e6e4da19a --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,185 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000000..79ee123c2b --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index e70e3fe729..a13a73c19b 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -1,19 +1,8 @@ - - - - + \ No newline at end of file diff --git a/src/org/sosy_lab/java_smt/SolverContextFactory.java b/src/org/sosy_lab/java_smt/SolverContextFactory.java index 500beb11cf..4edb6ff4d3 100644 --- a/src/org/sosy_lab/java_smt/SolverContextFactory.java +++ b/src/org/sosy_lab/java_smt/SolverContextFactory.java @@ -29,6 +29,7 @@ import org.sosy_lab.java_smt.delegate.logging.LoggingSolverContext; import org.sosy_lab.java_smt.delegate.statistics.StatisticsSolverContext; import org.sosy_lab.java_smt.delegate.synchronize.SynchronizedSolverContext; +import org.sosy_lab.java_smt.solvers.apron.ApronSolverContext; import org.sosy_lab.java_smt.solvers.boolector.BoolectorSolverContext; import org.sosy_lab.java_smt.solvers.cvc4.CVC4SolverContext; import org.sosy_lab.java_smt.solvers.cvc5.CVC5SolverContext; @@ -55,7 +56,8 @@ public enum Solvers { BOOLECTOR, CVC4, CVC5, - YICES2 + YICES2, + APRON } @Option(secure = true, description = "Export solver queries in SmtLib format into a file.") @@ -282,6 +284,9 @@ private SolverContext generateContext0(Solvers solverToCreate) case BOOLECTOR: return BoolectorSolverContext.create(config, shutdownNotifier, logfile, randomSeed, loader); + case APRON: + return ApronSolverContext.create(); + default: throw new AssertionError("no solver selected"); } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java index 2c294e7e62..26d4589e27 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java @@ -24,8 +24,11 @@ import org.sosy_lab.java_smt.basicimpl.FormulaCreator; public class ApronBooleanFormulaManager extends AbstractBooleanFormulaManager { - protected ApronBooleanFormulaManager(FormulaCreator pCreator) { + + private ApronFormulaCreator formulaCreator; + protected ApronBooleanFormulaManager(ApronFormulaCreator pCreator) { super(pCreator); + this.formulaCreator = pCreator; } @Override diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java index bfece0e87b..07c681eea2 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java @@ -37,20 +37,20 @@ private static void testBox(Manager pManager) throws ApronException { Environment environment = new Environment(intVars, realVars); //x <= 2 and x >= -3 - //x <= 2 --> -x+2 >= 0 Lincons1 cons1 = new Lincons1(environment); cons1.setCoeff("x",new MpqScalar(-1)); cons1.setCst(new MpqScalar(+2)); cons1.setKind(Lincons1.SUPEQ); - //x >= 3 --> x-3 >= 0 + //x >= - 3 --> x+3 >= 0 Lincons1 cons2 = new Lincons1(environment); cons2.setCoeff("x",new MpqScalar(1)); - cons2.setCst(new MpqScalar(-3)); + cons2.setCst(new MpqScalar(+3)); cons2.setKind(Lincons1.SUPEQ); + Abstract1 abstract1 = new Abstract1(pManager, new Lincons1[]{cons1,cons2}); - Lincons1[] constraints = {cons1, cons2}; - Abstract1 abstract1 = new Abstract1(pManager, constraints); + System.out.println("Variable has to be in: "+abstract1.getBound(pManager, "x")); + System.out.println(abstract1.toString(pManager)); //is x = 1 satisfiable? Lincons1 cons3 = new Lincons1(environment); cons3.setCoeff("x",new MpqScalar(1)); @@ -68,7 +68,7 @@ private static void testBox(Manager pManager) throws ApronException { } public static void main(String[] args) throws ApronException { - Manager manager = new Box(); + Manager manager = new Polka(false); testBox(manager); } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java index 182a893218..13a2a0e5f9 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java @@ -20,6 +20,9 @@ package org.sosy_lab.java_smt.solvers.apron; +import apron.Environment; +import apron.Var; +import com.google.common.base.Preconditions; import java.util.List; import javax.annotation.Nullable; import org.sosy_lab.java_smt.api.Formula; @@ -27,60 +30,105 @@ import org.sosy_lab.java_smt.api.FormulaType.FloatingPointType; import org.sosy_lab.java_smt.api.visitors.FormulaVisitor; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.Type; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronBooleanType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronIntegerType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronRationalType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas.ApronVar; -public class ApronFormulaCreator extends FormulaCreator { +public class ApronFormulaCreator extends FormulaCreator { + + private Environment environment; protected ApronFormulaCreator( - Object pO, - Object boolType, - @Nullable Object pIntegerType, - @Nullable Object pRationalType, - @Nullable Object stringType, - @Nullable Object regexType) { - super(pO, boolType, pIntegerType, pRationalType, stringType, regexType); + Environment pO, + ApronBooleanType boolType, + @Nullable ApronIntegerType pIntegerType, + @Nullable ApronRationalType pRationalType, + @Nullable Long stringType, + @Nullable Long regexType) { + super(pO, boolType, pIntegerType, pRationalType, null, null); + this.environment = pO; + + } + + public Environment getEnvironment(){ + return this.environment; } @Override - public Object getBitvectorType(int bitwidth) { + public ApronFormulaType getBitvectorType(int bitwidth) { return null; } @Override - public Object getFloatingPointType(FloatingPointType type) { + public ApronFormulaType getFloatingPointType(FloatingPointType type) { return null; } @Override - public Object getArrayType(Object indexType, Object elementType) { + public ApronFormulaType getArrayType(ApronFormulaType indexType, ApronFormulaType elementType) { return null; } @Override - public Object makeVariable(Object pO, String varName) { - return null; + public ApronFormulas makeVariable(ApronFormulaType pApronFormulaType, String varName) { + Preconditions.checkArgument(!environment.hasVar(varName),"Variablename already exists!"); + Preconditions.checkArgument( + (pApronFormulaType.getType().equals(Type.INTEGER) || pApronFormulaType.getType().equals(Type.RATIONAL)), + "Only Integer or rational variables allowed1"); + if(pApronFormulaType.getType().equals(Type.INTEGER)){ + String[] intvars = new String[]{varName}; + this.environment.add(intvars,new String[]{}); + return new ApronVar(); + }else { + String[] realvars = new String[]{varName}; + this.environment.add(new String[]{}, realvars); + return new ApronVar(); + } } @Override - public FormulaType getFormulaType(Object formula) { + public FormulaType getFormulaType(ApronFormulas formula) { + //TODO + switch (formula.getFormulaType()){ + case VAR: + //... + case TERM: + //... + case COEFF: + //... + case CONSTRAINT: + //... + case EXPRESSION: + //... + default: + //.... + } return null; } @Override - public Object callFunctionImpl(Object declaration, List args) { + public R visit(FormulaVisitor visitor, Formula formula, ApronFormulas f) { return null; } @Override - public Object declareUFImpl(String pName, Object pReturnType, List pArgTypes) { + public ApronFormulas callFunctionImpl(Long declaration, List args) { return null; } @Override - protected Object getBooleanVarDeclarationImpl(Object pO) { + public Long declareUFImpl( + String pName, + ApronFormulaType pReturnType, + List pArgTypes) { return null; } @Override - public Object visit(FormulaVisitor visitor, Formula formula, Object f) { + protected Long getBooleanVarDeclarationImpl(ApronFormulas pApronFormula) { return null; } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java index 6b116a0f95..d60fed1ea2 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java @@ -20,6 +20,7 @@ package org.sosy_lab.java_smt.solvers.apron; +import apron.Environment; import java.util.Map; import org.checkerframework.checker.nullness.qual.Nullable; import org.sosy_lab.common.Appender; @@ -39,15 +40,15 @@ import org.sosy_lab.java_smt.basicimpl.AbstractUFManager; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; -public class ApronFormulaManager extends AbstractFormulaManager { +public class ApronFormulaManager extends AbstractFormulaManager { /** * Builds a solver from the given theory implementations. * * @param pFormulaCreator * @param functionManager * @param booleanManager - * @param pIntegerManager - * @param pRationalManager + * @param pIntegerFormulaManager + * @param pRationalFormulaManager * @param bitvectorManager * @param floatingPointManager * @param quantifiedManager @@ -57,11 +58,11 @@ public class ApronFormulaManager extends AbstractFormulaManager implements IntegerFormulaManager { + protected ApronIntegerFormulaManager( - FormulaCreator pCreator, + FormulaCreator pCreator, NonLinearArithmetic pNonLinearArithmetic) { super(pCreator, pNonLinearArithmetic); } @Override - public BooleanFormula modularCongruence( - IntegerFormula number1, - IntegerFormula number2, - BigInteger n) { - return null; - } - - @Override - public BooleanFormula modularCongruence(IntegerFormula number1, IntegerFormula number2, long n) { - return null; + protected boolean isNumeral(ApronFormulas val) { + return false; } @Override - public IntegerFormula modulo(IntegerFormula numerator, IntegerFormula denumerator) { + protected ApronFormulas makeNumberImpl(long i) { return null; } @Override - public IntegerFormula makeNumber(long number) { + protected ApronFormulas makeNumberImpl(BigInteger i) { return null; } @Override - public IntegerFormula makeNumber(BigInteger number) { + protected ApronFormulas makeNumberImpl(String i) { return null; } @Override - public IntegerFormula makeNumber(double number) { + protected ApronFormulas makeNumberImpl(double pNumber) { return null; } @Override - public IntegerFormula makeNumber(BigDecimal number) { + protected ApronFormulas makeNumberImpl(BigDecimal pNumber) { return null; } @Override - public IntegerFormula makeNumber(String pI) { + protected ApronFormulas makeVariableImpl(String i) { return null; } @Override - public IntegerFormula makeNumber(Rational pRational) { + protected ApronFormulas negate(ApronFormulas pParam1) { return null; } @Override - public IntegerFormula makeVariable(String pVar) { + protected ApronFormulas add(ApronFormulas pParam1, ApronFormulas pParam2) { return null; } @Override - public IntegerFormula negate(IntegerFormula number) { + protected ApronFormulas subtract(ApronFormulas pParam1, ApronFormulas pParam2) { return null; } @Override - public IntegerFormula add(IntegerFormula number1, IntegerFormula number2) { + protected ApronFormulas equal(ApronFormulas pParam1, ApronFormulas pParam2) { return null; } @Override - public IntegerFormula sum(List operands) { + protected ApronFormulas distinctImpl(List pNumbers) { return null; } @Override - public IntegerFormula subtract(IntegerFormula number1, IntegerFormula number2) { + protected ApronFormulas greaterThan(ApronFormulas pParam1, ApronFormulas pParam2) { return null; } @Override - public IntegerFormula divide(IntegerFormula numerator, IntegerFormula denumerator) { + protected ApronFormulas greaterOrEquals(ApronFormulas pParam1, ApronFormulas pParam2) { return null; } @Override - public IntegerFormula multiply(IntegerFormula number1, IntegerFormula number2) { + protected ApronFormulas lessThan(ApronFormulas pParam1, ApronFormulas pParam2) { return null; } @Override - public BooleanFormula equal(IntegerFormula number1, IntegerFormula number2) { + protected ApronFormulas lessOrEquals(ApronFormulas pParam1, ApronFormulas pParam2) { return null; } - @Override - public BooleanFormula distinct(List pNumbers) { - return null; - } - - @Override - public BooleanFormula greaterThan(IntegerFormula number1, IntegerFormula number2) { - return null; - } - - @Override - public BooleanFormula greaterOrEquals(IntegerFormula number1, IntegerFormula number2) { - return null; - } - - @Override - public BooleanFormula lessThan(IntegerFormula number1, IntegerFormula number2) { - return null; - } - - @Override - public BooleanFormula lessOrEquals(IntegerFormula number1, IntegerFormula number2) { - return null; - } - - @Override - public IntegerFormula floor(IntegerFormula formula) { - return null; - } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java new file mode 100644 index 0000000000..96878c180d --- /dev/null +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java @@ -0,0 +1,46 @@ +/* + * JavaSMT is an API wrapper for a collection of SMT solvers. + * This file is part of JavaSMT. + * + * Copyright (C) 2007-2016 Dirk Beyer + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sosy_lab.java_smt.solvers.apron; + +import com.google.common.collect.ImmutableList; +import javax.annotation.Nullable; +import org.sosy_lab.java_smt.basicimpl.AbstractModel; +import org.sosy_lab.java_smt.basicimpl.AbstractProver; +import org.sosy_lab.java_smt.basicimpl.FormulaCreator; + +public class ApronModel extends AbstractModel { + protected ApronModel( + AbstractProver prover, + FormulaCreator creator) { + super(prover, creator); + } + + @Override + public ImmutableList asList() { + return null; + } + + @Nullable + @Override + protected Object evalImpl(Object formula) { + return null; + } +} diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java index a5a84cddf8..e1215b7107 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java @@ -20,6 +20,7 @@ package org.sosy_lab.java_smt.solvers.apron; +import apron.Environment; import java.math.BigDecimal; import java.math.BigInteger; import java.util.List; @@ -27,99 +28,16 @@ import org.sosy_lab.java_smt.api.NumeralFormula; import org.sosy_lab.java_smt.basicimpl.AbstractNumeralFormulaManager; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas; -public class ApronNumeralFormulaManager< +abstract class ApronNumeralFormulaManager < ParamFormulaType extends NumeralFormula, ResultFormulaType extends NumeralFormula> extends AbstractNumeralFormulaManager< - Long, Long, Long, ParamFormulaType, ResultFormulaType, Long> { + ApronFormulas, ApronFormulaType, Environment, ParamFormulaType, ResultFormulaType, Long>{ protected ApronNumeralFormulaManager( - FormulaCreator pCreator, + FormulaCreator pCreator, NonLinearArithmetic pNonLinearArithmetic) { super(pCreator, pNonLinearArithmetic); } - - @Override - public FormulaType getFormulaType() { - return null; - } - - @Override - protected boolean isNumeral(Long val) { - return false; - } - - @Override - protected Long makeNumberImpl(long i) { - return null; - } - - @Override - protected Long makeNumberImpl(BigInteger i) { - return null; - } - - @Override - protected Long makeNumberImpl(String i) { - return null; - } - - @Override - protected Long makeNumberImpl(double pNumber) { - return null; - } - - @Override - protected Long makeNumberImpl(BigDecimal pNumber) { - return null; - } - - @Override - protected Long makeVariableImpl(String i) { - return null; - } - - @Override - protected Long negate(Long pParam1) { - return null; - } - - @Override - protected Long add(Long pParam1, Long pParam2) { - return null; - } - - @Override - protected Long subtract(Long pParam1, Long pParam2) { - return null; - } - - @Override - protected Long equal(Long pParam1, Long pParam2) { - return null; - } - - @Override - protected Long distinctImpl(List pNumbers) { - return null; - } - - @Override - protected Long greaterThan(Long pParam1, Long pParam2) { - return null; - } - - @Override - protected Long greaterOrEquals(Long pParam1, Long pParam2) { - return null; - } - - @Override - protected Long lessThan(Long pParam1, Long pParam2) { - return null; - } - - @Override - protected Long lessOrEquals(Long pParam1, Long pParam2) { - return null; - } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java index c342e67b3f..bfc6f334f9 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java @@ -20,6 +20,7 @@ package org.sosy_lab.java_smt.solvers.apron; +import apron.Environment; import java.math.BigDecimal; import java.math.BigInteger; import java.util.List; @@ -30,112 +31,95 @@ import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula; import org.sosy_lab.java_smt.api.RationalFormulaManager; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas; public class ApronRationalFormulaManager extends ApronNumeralFormulaManager implements RationalFormulaManager { + protected ApronRationalFormulaManager( - FormulaCreator pCreator, + FormulaCreator pCreator, NonLinearArithmetic pNonLinearArithmetic) { super(pCreator, pNonLinearArithmetic); } @Override - public RationalFormula makeNumber(long number) { - return null; - } - - @Override - public RationalFormula makeNumber(BigInteger number) { - return null; - } - - @Override - public RationalFormula makeNumber(double number) { - return null; - } - - @Override - public RationalFormula makeNumber(BigDecimal number) { - return null; - } - - @Override - public RationalFormula makeNumber(String pI) { - return null; + protected boolean isNumeral(ApronFormulas val) { + return false; } @Override - public RationalFormula makeNumber(Rational pRational) { + protected ApronFormulas makeNumberImpl(long i) { return null; } @Override - public RationalFormula makeVariable(String pVar) { + protected ApronFormulas makeNumberImpl(BigInteger i) { return null; } @Override - public RationalFormula negate(NumeralFormula number) { + protected ApronFormulas makeNumberImpl(String i) { return null; } @Override - public RationalFormula add(NumeralFormula number1, NumeralFormula number2) { + protected ApronFormulas makeNumberImpl(double pNumber) { return null; } @Override - public RationalFormula sum(List operands) { + protected ApronFormulas makeNumberImpl(BigDecimal pNumber) { return null; } @Override - public RationalFormula subtract(NumeralFormula number1, NumeralFormula number2) { + protected ApronFormulas makeVariableImpl(String i) { return null; } @Override - public RationalFormula divide(NumeralFormula numerator, NumeralFormula denumerator) { + protected ApronFormulas negate(ApronFormulas pParam1) { return null; } @Override - public RationalFormula multiply(NumeralFormula number1, NumeralFormula number2) { + protected ApronFormulas add(ApronFormulas pParam1, ApronFormulas pParam2) { return null; } @Override - public BooleanFormula equal(NumeralFormula number1, NumeralFormula number2) { + protected ApronFormulas subtract(ApronFormulas pParam1, ApronFormulas pParam2) { return null; } @Override - public BooleanFormula distinct(List pNumbers) { + protected ApronFormulas equal(ApronFormulas pParam1, ApronFormulas pParam2) { return null; } @Override - public BooleanFormula greaterThan(NumeralFormula number1, NumeralFormula number2) { + protected ApronFormulas distinctImpl(List pNumbers) { return null; } @Override - public BooleanFormula greaterOrEquals(NumeralFormula number1, NumeralFormula number2) { + protected ApronFormulas greaterThan(ApronFormulas pParam1, ApronFormulas pParam2) { return null; } @Override - public BooleanFormula lessThan(NumeralFormula number1, NumeralFormula number2) { + protected ApronFormulas greaterOrEquals(ApronFormulas pParam1, ApronFormulas pParam2) { return null; } @Override - public BooleanFormula lessOrEquals(NumeralFormula number1, NumeralFormula number2) { + protected ApronFormulas lessThan(ApronFormulas pParam1, ApronFormulas pParam2) { return null; } @Override - public IntegerFormula floor(NumeralFormula formula) { + protected ApronFormulas lessOrEquals(ApronFormulas pParam1, ApronFormulas pParam2) { return null; } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java index bc2c5008d6..7e399adac9 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java @@ -20,37 +20,94 @@ package org.sosy_lab.java_smt.solvers.apron; +import apron.ApronException; +import apron.Box; +import apron.Environment; +import apron.Manager; +import com.microsoft.z3.Native; import java.util.Set; +import org.sosy_lab.common.ShutdownNotifier; import org.sosy_lab.java_smt.SolverContextFactory.Solvers; import org.sosy_lab.java_smt.api.FormulaManager; import org.sosy_lab.java_smt.api.InterpolatingProverEnvironment; import org.sosy_lab.java_smt.api.OptimizationProverEnvironment; import org.sosy_lab.java_smt.api.ProverEnvironment; +import org.sosy_lab.java_smt.basicimpl.AbstractNumeralFormulaManager.NonLinearArithmetic; import org.sosy_lab.java_smt.basicimpl.AbstractSolverContext; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronBooleanType; public class ApronSolverContext extends AbstractSolverContext { - protected ApronSolverContext(FormulaManager fmgr) { + + private Manager manager; + private final ApronFormulaCreator formulaCreator; + protected ShutdownNotifier shutdownNotifier; + protected ApronSolverContext(ApronFormulaManager fmgr, + Manager pManager, + ApronFormulaCreator pFormulaCreator, + ShutdownNotifier pShutdownNotifier) { super(fmgr); + this.manager = pManager; + this.formulaCreator = pFormulaCreator; + this.shutdownNotifier = pShutdownNotifier; + } + + public static synchronized ApronSolverContext create(NonLinearArithmetic pNonLinearArithmetic, + ShutdownNotifier pShutdownNotifier){ + + Environment env = new Environment(); + Manager manager = new Box(); + ApronBooleanType booleanType = new ApronBooleanType(); + ApronFormulaCreator formulaCreator = new ApronFormulaCreator(env, booleanType, null,null,null,null); + ApronUFManager ufManager = new ApronUFManager(formulaCreator); + ApronBooleanFormulaManager booleanFormulaManager = + new ApronBooleanFormulaManager(formulaCreator); + ApronIntegerFormulaManager integerFormulaManager = + new ApronIntegerFormulaManager(formulaCreator, pNonLinearArithmetic); + ApronRationalFormulaManager rationalFormulaManager = + new ApronRationalFormulaManager(formulaCreator, pNonLinearArithmetic); + ApronFormulaManager fmgr = new ApronFormulaManager(formulaCreator, ufManager, + booleanFormulaManager,integerFormulaManager,rationalFormulaManager,null,null,null,null, + null,null,null); + return new ApronSolverContext(fmgr, manager, formulaCreator, pShutdownNotifier); } + public Manager getManager(){ + return this.manager; + } + + public ApronFormulaCreator getFormulaCreator(){ + return this.formulaCreator; + } @Override public String getVersion() { - return null; + return this.manager.getVersion(); } @Override public Solvers getSolverName() { - return null; + return Solvers.APRON; } @Override public void close() { - + //TODO } @Override protected ProverEnvironment newProverEnvironment0(Set options) { - return null; + return newApronProverEnvironment(options); + } + + private ProverEnvironment newApronProverEnvironment(Set pProverOptions){ + try{ + ApronBooleanFormulaManager booleanFormulaManager = + new ApronBooleanFormulaManager(this.formulaCreator); + return new ApronTheoremProver(pProverOptions,booleanFormulaManager,this.shutdownNotifier,this); + } catch(ApronException pApronException){ + System.out.println(pApronException.toString()); + System.exit(0); + return null; + } } @Override diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java index 4427755eb5..ff01731aa3 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java @@ -20,11 +20,14 @@ package org.sosy_lab.java_smt.solvers.apron; +import apron.Abstract1; +import apron.ApronException; import java.util.Collection; import java.util.List; import java.util.Optional; import java.util.Set; import javax.annotation.Nullable; +import org.checkerframework.checker.units.qual.A; import org.sosy_lab.common.ShutdownNotifier; import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.BooleanFormulaManager; @@ -33,13 +36,21 @@ import org.sosy_lab.java_smt.api.ProverEnvironment; import org.sosy_lab.java_smt.api.SolverException; import org.sosy_lab.java_smt.basicimpl.AbstractProverWithAllSat; +import org.sosy_lab.java_smt.utils.SolverUtils; public class ApronTheoremProver extends AbstractProverWithAllSat implements ProverEnvironment { + + private Abstract1 abstract1; + private ApronSolverContext solverContext; protected ApronTheoremProver( Set pSet, BooleanFormulaManager pBmgr, - ShutdownNotifier pShutdownNotifier) { + ShutdownNotifier pShutdownNotifier, + ApronSolverContext pApronSolverContext) throws ApronException { super(pSet, pBmgr, pShutdownNotifier); + this.solverContext = pApronSolverContext; + this.abstract1 = new Abstract1(pApronSolverContext.getManager(), + pApronSolverContext.getFormulaCreator().getEnvironment()); } @Override @@ -64,10 +75,19 @@ public int size() { } @Override - public boolean isUnsat() throws SolverException, InterruptedException { - return false; + public boolean isUnsat() throws SolverException, InterruptedException{ + return isUnsatApron(); } + private boolean isUnsatApron(){ + try { + return abstract1.isBottom(solverContext.getManager()); + } catch (ApronException pApronException){ + System.out.println(pApronException.toString()); + System.exit(0); + return false; + } + } @Override public Model getModel() throws SolverException { return null; diff --git a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulaType.java b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulaType.java new file mode 100644 index 0000000000..dfaa98537d --- /dev/null +++ b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulaType.java @@ -0,0 +1,62 @@ +/* + * JavaSMT is an API wrapper for a collection of SMT solvers. + * This file is part of JavaSMT. + * + * Copyright (C) 2007-2016 Dirk Beyer + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sosy_lab.java_smt.solvers.apron.types; + +import org.sosy_lab.java_smt.solvers.apron.ApronIntegerFormulaManager; + +public interface ApronFormulaType { + + enum Type{ + BOOLEAN, + INTEGER, + RATIONAL + } + + Type getType(); + class ApronIntegerType implements ApronFormulaType{ + + public ApronIntegerType(){} + @Override + public Type getType(){ + return Type.INTEGER; + } + + } + + class ApronRationalType implements ApronFormulaType{ + + public ApronRationalType(){} + @Override + public Type getType(){ + return Type.RATIONAL; + } + } + + class ApronBooleanType implements ApronFormulaType{ + + public ApronBooleanType(){ + } + @Override + public Type getType(){ + return Type.BOOLEAN; + } + } +} diff --git a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulas.java b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulas.java new file mode 100644 index 0000000000..e66ce46010 --- /dev/null +++ b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulas.java @@ -0,0 +1,94 @@ +/* + * JavaSMT is an API wrapper for a collection of SMT solvers. + * This file is part of JavaSMT. + * + * Copyright (C) 2007-2016 Dirk Beyer + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sosy_lab.java_smt.solvers.apron.types; + +import apron.Coeff; +import apron.Linexpr1; +import apron.Linterm1; +import org.sosy_lab.java_smt.api.Formula; + +public interface ApronFormulas extends Formula { + + enum ApronFormulaType { + VAR, + COEFF, + TERM, + EXPRESSION, + CONSTRAINT + } + + ApronFormulaType getFormulaType(); + + class ApronVar implements ApronFormulas { + + public ApronVar(){} + @Override + public ApronFormulaType getFormulaType() { + return ApronFormulaType.VAR; + } + } + + class ApronCoeff implements ApronFormulas { + + public ApronCoeff(){} + @Override + public ApronFormulaType getFormulaType() { + return ApronFormulaType.COEFF; + } + } + + class ApronTerm implements ApronFormulas { + private Linterm1 linterm1; + + public ApronTerm(String pVar,Coeff pCoeff){ + this.linterm1 = new Linterm1(pVar,pCoeff); + } + + @Override + public ApronFormulaType getFormulaType() { + return ApronFormulaType.TERM; + } + } + + class ApronExpr implements ApronFormulas { + private Linexpr1 linexpr1; + + public ApronExpr(){ + + } + + @Override + public ApronFormulaType getFormulaType() { + return ApronFormulaType.EXPRESSION; + } + } + + class ApronCons implements ApronFormulas { + public ApronCons(){ + + } + + @Override + public ApronFormulaType getFormulaType() { + return ApronFormulaType.CONSTRAINT; + } + } +} From 642b24e248d7a73fd8701b431f46fd5e5f82c21e Mon Sep 17 00:00:00 2001 From: winnieros Date: Mon, 24 Jul 2023 15:13:54 +0200 Subject: [PATCH 003/133] adjustments to the git-lab annotations --- .gitignore | 2 +- .../java_smt/SolverContextFactory.java | 4 +- .../java_smt/solvers/apron/ApronExamples.java | 7 ++- .../solvers/apron/ApronFormulaCreator.java | 19 ++++---- .../solvers/apron/ApronSolverContext.java | 48 ++++++++++++++++--- 5 files changed, 59 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 2091b4ba6d..46e71b4181 100644 --- a/.gitignore +++ b/.gitignore @@ -56,7 +56,7 @@ solvers_maven_conf/*.asc .idea/workspace.xml .idea/uiDesigner.xml - +.idea /*.so /*.dll /*.jar diff --git a/src/org/sosy_lab/java_smt/SolverContextFactory.java b/src/org/sosy_lab/java_smt/SolverContextFactory.java index 4edb6ff4d3..b3030ccba8 100644 --- a/src/org/sosy_lab/java_smt/SolverContextFactory.java +++ b/src/org/sosy_lab/java_smt/SolverContextFactory.java @@ -285,7 +285,9 @@ private SolverContext generateContext0(Solvers solverToCreate) return BoolectorSolverContext.create(config, shutdownNotifier, logfile, randomSeed, loader); case APRON: - return ApronSolverContext.create(); + return ApronSolverContext.create(nonLinearArithmetic, config, shutdownNotifier, logfile, + logger, + randomSeed); default: throw new AssertionError("no solver selected"); diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java index 07c681eea2..36d2d38e7b 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java @@ -22,6 +22,7 @@ import java.util.Arrays; import apron.*; +import org.junit.Assert; import org.junit.AssumptionViolatedException; import org.sosy_lab.common.NativeLibraries; @@ -49,14 +50,12 @@ private static void testBox(Manager pManager) throws ApronException { cons2.setKind(Lincons1.SUPEQ); Abstract1 abstract1 = new Abstract1(pManager, new Lincons1[]{cons1,cons2}); - System.out.println("Variable has to be in: "+abstract1.getBound(pManager, "x")); - System.out.println(abstract1.toString(pManager)); //is x = 1 satisfiable? Lincons1 cons3 = new Lincons1(environment); cons3.setCoeff("x",new MpqScalar(1)); cons3.setCst(new MpqScalar(-1)); cons3.setKind(Lincons1.EQ); - System.out.println("Constraint is satisfiable: "+abstract1.satisfy(pManager, cons3)); + assert abstract1.satisfy(pManager,cons3); //always unsat example, 1 = 0 Lincons1 cons4 = new Lincons1(environment); @@ -64,7 +63,7 @@ private static void testBox(Manager pManager) throws ApronException { cons4.setCst(new MpqScalar(1)); cons4.setKind(Lincons1.EQ); Abstract1 abstract2 = new Abstract1(pManager, new Lincons1[]{cons4}); - System.out.println("Abstract-Obj. is Bottom: "+abstract2.isBottom(pManager)); + assert abstract2.isBottom(pManager); } public static void main(String[] args) throws ApronException { diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java index 13a2a0e5f9..0bb7e3c157 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java @@ -44,8 +44,8 @@ public class ApronFormulaCreator extends FormulaCreator getFormulaType(ApronFormulas formula) { } @Override - public R visit(FormulaVisitor visitor, Formula formula, ApronFormulas f) { + public R visit(FormulaVisitor visitor, Formula formula, ApronFormulas f) { //hinten + // anstellen, Frage kann man eine formel in alle kleinteile zerlegen und dann wieder + // zusammenbauen? return null; } @Override - public ApronFormulas callFunctionImpl(Long declaration, List args) { + public ApronFormulas callFunctionImpl(Long declaration, List args) { // nicht + // supported return null; } @Override - public Long declareUFImpl( + public Long declareUFImpl( //nicht supported String pName, ApronFormulaType pReturnType, List pArgTypes) { @@ -128,7 +131,7 @@ public Long declareUFImpl( } @Override - protected Long getBooleanVarDeclarationImpl(ApronFormulas pApronFormula) { + protected Long getBooleanVarDeclarationImpl(ApronFormulas pApronFormula) { //brauche ich nicht return null; } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java index 7e399adac9..3a763ce2e3 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java @@ -24,11 +24,15 @@ import apron.Box; import apron.Environment; import apron.Manager; -import com.microsoft.z3.Native; import java.util.Set; +import java.util.logging.Level; +import javax.annotation.Nullable; import org.sosy_lab.common.ShutdownNotifier; +import org.sosy_lab.common.ShutdownNotifier.ShutdownRequestListener; +import org.sosy_lab.common.configuration.Configuration; +import org.sosy_lab.common.io.PathCounterTemplate; +import org.sosy_lab.common.log.LogManager; import org.sosy_lab.java_smt.SolverContextFactory.Solvers; -import org.sosy_lab.java_smt.api.FormulaManager; import org.sosy_lab.java_smt.api.InterpolatingProverEnvironment; import org.sosy_lab.java_smt.api.OptimizationProverEnvironment; import org.sosy_lab.java_smt.api.ProverEnvironment; @@ -40,19 +44,43 @@ public class ApronSolverContext extends AbstractSolverContext { private Manager manager; private final ApronFormulaCreator formulaCreator; - protected ShutdownNotifier shutdownNotifier; + private ShutdownNotifier shutdownNotifier; + private Configuration config; + private @Nullable PathCounterTemplate logfile; + private LogManager logger; + private long randomSeed; + + private ShutdownRequestListener shutdownRequestListener; + private boolean closed = false; + protected ApronSolverContext(ApronFormulaManager fmgr, Manager pManager, ApronFormulaCreator pFormulaCreator, - ShutdownNotifier pShutdownNotifier) { + ShutdownNotifier pShutdownNotifier, + Configuration pConfig, + PathCounterTemplate pLogfile, + LogManager pLogger, + long pRandomSeed) { super(fmgr); this.manager = pManager; this.formulaCreator = pFormulaCreator; this.shutdownNotifier = pShutdownNotifier; + this.shutdownRequestListener = reason -> { + + }; + shutdownNotifier.register(shutdownRequestListener); + this.config = pConfig; + this.logfile = pLogfile; + this.randomSeed = pRandomSeed; + this.logger = pLogger; } public static synchronized ApronSolverContext create(NonLinearArithmetic pNonLinearArithmetic, - ShutdownNotifier pShutdownNotifier){ + Configuration pConfiguration, + ShutdownNotifier pShutdownNotifier, + PathCounterTemplate logfile, + LogManager pLogger, + long randomSeed){ Environment env = new Environment(); Manager manager = new Box(); @@ -68,7 +96,8 @@ public static synchronized ApronSolverContext create(NonLinearArithmetic pNonLin ApronFormulaManager fmgr = new ApronFormulaManager(formulaCreator, ufManager, booleanFormulaManager,integerFormulaManager,rationalFormulaManager,null,null,null,null, null,null,null); - return new ApronSolverContext(fmgr, manager, formulaCreator, pShutdownNotifier); + return new ApronSolverContext(fmgr, manager, formulaCreator, pShutdownNotifier,pConfiguration + ,logfile,pLogger,randomSeed); } public Manager getManager(){ @@ -90,7 +119,12 @@ public Solvers getSolverName() { @Override public void close() { - //TODO + //TODO was muss hier noch passieren? + if(!closed){ + closed = true; + logger.log(Level.FINER, "Freeing Apron Environment"); + shutdownNotifier.unregister(shutdownRequestListener); + } } @Override From 6758f9fc132ce8aca2fa21c91cab9f78d4d224be Mon Sep 17 00:00:00 2001 From: winnieros Date: Mon, 24 Jul 2023 17:49:33 +0200 Subject: [PATCH 004/133] NumeralFormularManager Beginnings --- .../solvers/apron/ApronFormulaCreator.java | 1 - .../apron/ApronIntegerFormulaManager.java | 75 ++++------------ .../apron/ApronNumeralFormulaManager.java | 84 +++++++++++++++++- .../apron/ApronRationalFormulaManager.java | 87 ++++--------------- 4 files changed, 114 insertions(+), 133 deletions(-) diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java index 0bb7e3c157..7157d01ce5 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java @@ -21,7 +21,6 @@ package org.sosy_lab.java_smt.solvers.apron; import apron.Environment; -import apron.Var; import com.google.common.base.Preconditions; import java.util.List; import javax.annotation.Nullable; diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java index 807927a7c1..e4afeb8685 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java @@ -23,45 +23,32 @@ import apron.Environment; import java.math.BigDecimal; import java.math.BigInteger; -import java.util.List; -import org.sosy_lab.common.rationals.Rational; import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.IntegerFormulaManager; import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronIntegerType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.Type; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas; -public class ApronIntegerFormulaManager extends ApronNumeralFormulaManager - implements IntegerFormulaManager { +public class ApronIntegerFormulaManager extends ApronNumeralFormulaManager implements + IntegerFormulaManager { + private ApronFormulaType integerType = new ApronIntegerType(); + private ApronFormulaCreator formulaCreator; protected ApronIntegerFormulaManager( - FormulaCreator pCreator, + ApronFormulaCreator pCreator, NonLinearArithmetic pNonLinearArithmetic) { super(pCreator, pNonLinearArithmetic); + this.formulaCreator = pCreator; } @Override - protected boolean isNumeral(ApronFormulas val) { - return false; + protected Type getNumeralType() { + return Type.INTEGER; } - @Override - protected ApronFormulas makeNumberImpl(long i) { - return null; - } - - @Override - protected ApronFormulas makeNumberImpl(BigInteger i) { - return null; - } - - @Override - protected ApronFormulas makeNumberImpl(String i) { - return null; - } - - @Override protected ApronFormulas makeNumberImpl(double pNumber) { return null; } @@ -72,53 +59,27 @@ protected ApronFormulas makeNumberImpl(BigDecimal pNumber) { } @Override - protected ApronFormulas makeVariableImpl(String i) { + public BooleanFormula modularCongruence( + IntegerFormula number1, + IntegerFormula number2, + BigInteger n) { return null; } @Override - protected ApronFormulas negate(ApronFormulas pParam1) { + public BooleanFormula modularCongruence(IntegerFormula number1, IntegerFormula number2, long n) { return null; } @Override - protected ApronFormulas add(ApronFormulas pParam1, ApronFormulas pParam2) { + public IntegerFormula modulo(IntegerFormula numerator, IntegerFormula denumerator) { return null; } - @Override - protected ApronFormulas subtract(ApronFormulas pParam1, ApronFormulas pParam2) { - return null; - } @Override - protected ApronFormulas equal(ApronFormulas pParam1, ApronFormulas pParam2) { - return null; - } - - @Override - protected ApronFormulas distinctImpl(List pNumbers) { - return null; - } - - @Override - protected ApronFormulas greaterThan(ApronFormulas pParam1, ApronFormulas pParam2) { - return null; - } - - @Override - protected ApronFormulas greaterOrEquals(ApronFormulas pParam1, ApronFormulas pParam2) { - return null; - } - - @Override - protected ApronFormulas lessThan(ApronFormulas pParam1, ApronFormulas pParam2) { - return null; - } - - @Override - protected ApronFormulas lessOrEquals(ApronFormulas pParam1, ApronFormulas pParam2) { - return null; + protected ApronFormulas makeVariableImpl(String i) { + return this.formulaCreator.makeVariable(integerType,i); } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java index e1215b7107..30f9b7e229 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java @@ -21,23 +21,101 @@ package org.sosy_lab.java_smt.solvers.apron; import apron.Environment; -import java.math.BigDecimal; import java.math.BigInteger; import java.util.List; -import org.sosy_lab.java_smt.api.FormulaType; import org.sosy_lab.java_smt.api.NumeralFormula; import org.sosy_lab.java_smt.basicimpl.AbstractNumeralFormulaManager; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.Type; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas; abstract class ApronNumeralFormulaManager < ParamFormulaType extends NumeralFormula, ResultFormulaType extends NumeralFormula> extends AbstractNumeralFormulaManager< - ApronFormulas, ApronFormulaType, Environment, ParamFormulaType, ResultFormulaType, Long>{ + ApronFormulas, ApronFormulaType, Environment, ParamFormulaType, ResultFormulaType, Long> { protected ApronNumeralFormulaManager( FormulaCreator pCreator, NonLinearArithmetic pNonLinearArithmetic) { super(pCreator, pNonLinearArithmetic); } + + + protected boolean isNumeral(ApronFormulas val){ + return false; + } + protected abstract Type getNumeralType(); + @Override + protected ApronFormulas makeNumberImpl(long i) { + return null; + } + + @Override + protected ApronFormulas makeNumberImpl(BigInteger i) { + return null; + } + + @Override + protected ApronFormulas makeNumberImpl(String i) { + return null; + } + + @Override + protected ApronFormulas negate(ApronFormulas pParam1) { + return null; + } + + @Override + protected ApronFormulas add(ApronFormulas pParam1, ApronFormulas pParam2) { + return null; + } + + @Override + protected ApronFormulas sumImpl(List operands){ + return null; + } + @Override + protected ApronFormulas subtract(ApronFormulas pParam1, ApronFormulas pParam2) { + return null; + } + + @Override + protected ApronFormulas divide(ApronFormulas pParam1, ApronFormulas pParam2) { + return null; + } + + @Override + protected ApronFormulas multiply(ApronFormulas pParam1, ApronFormulas pParam2) { + return null; + } + + @Override + protected ApronFormulas equal(ApronFormulas pParam1, ApronFormulas pParam2) { + return null; + } + + @Override + protected ApronFormulas distinctImpl(List pNumbers) { + return null; + } + + @Override + protected ApronFormulas greaterThan(ApronFormulas pParam1, ApronFormulas pParam2) { + return null; + } + + @Override + protected ApronFormulas greaterOrEquals(ApronFormulas pParam1, ApronFormulas pParam2) { + return null; + } + + @Override + protected ApronFormulas lessThan(ApronFormulas pParam1, ApronFormulas pParam2) { + return null; + } + + @Override + protected ApronFormulas lessOrEquals(ApronFormulas pParam1, ApronFormulas pParam2) { + return null; + } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java index bfc6f334f9..bf0eb8aefc 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java @@ -22,45 +22,31 @@ import apron.Environment; import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.List; -import org.sosy_lab.common.rationals.Rational; -import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.NumeralFormula; -import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula; import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula; import org.sosy_lab.java_smt.api.RationalFormulaManager; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronRationalType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.Type; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas; -public class ApronRationalFormulaManager extends ApronNumeralFormulaManager implements RationalFormulaManager { +public class ApronRationalFormulaManager extends + ApronNumeralFormulaManager + implements RationalFormulaManager { + + private ApronFormulaCreator formulaCreator; + private ApronFormulaType rationalType = new ApronRationalType(); protected ApronRationalFormulaManager( - FormulaCreator pCreator, + ApronFormulaCreator pFormulaCreator, NonLinearArithmetic pNonLinearArithmetic) { - super(pCreator, pNonLinearArithmetic); - } - - @Override - protected boolean isNumeral(ApronFormulas val) { - return false; + super(pFormulaCreator, pNonLinearArithmetic); + this.formulaCreator = pFormulaCreator; } - - @Override - protected ApronFormulas makeNumberImpl(long i) { - return null; - } - - @Override - protected ApronFormulas makeNumberImpl(BigInteger i) { - return null; - } - @Override - protected ApronFormulas makeNumberImpl(String i) { - return null; + protected Type getNumeralType() { + return Type.RATIONAL; } @Override @@ -73,53 +59,10 @@ protected ApronFormulas makeNumberImpl(BigDecimal pNumber) { return null; } - @Override - protected ApronFormulas makeVariableImpl(String i) { - return null; - } - - @Override - protected ApronFormulas negate(ApronFormulas pParam1) { - return null; - } - - @Override - protected ApronFormulas add(ApronFormulas pParam1, ApronFormulas pParam2) { - return null; - } @Override - protected ApronFormulas subtract(ApronFormulas pParam1, ApronFormulas pParam2) { - return null; - } - - @Override - protected ApronFormulas equal(ApronFormulas pParam1, ApronFormulas pParam2) { - return null; - } - - @Override - protected ApronFormulas distinctImpl(List pNumbers) { - return null; - } - - @Override - protected ApronFormulas greaterThan(ApronFormulas pParam1, ApronFormulas pParam2) { - return null; - } - - @Override - protected ApronFormulas greaterOrEquals(ApronFormulas pParam1, ApronFormulas pParam2) { - return null; - } - - @Override - protected ApronFormulas lessThan(ApronFormulas pParam1, ApronFormulas pParam2) { - return null; + protected ApronFormulas makeVariableImpl(String i) { + return formulaCreator.makeVariable(rationalType,i); } - @Override - protected ApronFormulas lessOrEquals(ApronFormulas pParam1, ApronFormulas pParam2) { - return null; - } } From 6fb8c14a159c4c1fbd86ac30c2b7264365463d3b Mon Sep 17 00:00:00 2001 From: winnieros Date: Thu, 10 Aug 2023 13:23:10 +0200 Subject: [PATCH 005/133] ApronNumeralFormulaManager add Function --- .idea/misc.xml | 14 +- .../solvers/apron/ApronFormulaCreator.java | 36 +-- .../apron/ApronIntegerFormulaManager.java | 8 +- .../apron/ApronNumeralFormulaManager.java | 210 +++++++++++++++++- .../apron/ApronRationalFormulaManager.java | 8 +- .../solvers/apron/types/ApronFormulaType.java | 18 +- .../solvers/apron/types/ApronFormulas.java | 176 +++++++++++++-- 7 files changed, 388 insertions(+), 82 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index ce7f947e76..31251a5781 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,15 +1,3 @@ - - - - @@ -54,4 +42,4 @@ SPDX-License-Identifier: Apache-2.0 - + \ No newline at end of file diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java index 7157d01ce5..2366f3fbfe 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java @@ -25,17 +25,17 @@ import java.util.List; import javax.annotation.Nullable; import org.sosy_lab.java_smt.api.Formula; -import org.sosy_lab.java_smt.api.FormulaType; import org.sosy_lab.java_smt.api.FormulaType.FloatingPointType; import org.sosy_lab.java_smt.api.visitors.FormulaVisitor; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.Type; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronBooleanType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronIntegerType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronRationalType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas.ApronVar; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas.FormulaCategory; public class ApronFormulaCreator extends FormulaCreator { @@ -75,36 +75,22 @@ public ApronFormulaType getArrayType(ApronFormulaType indexType, ApronFormulaTyp public ApronFormulas makeVariable(ApronFormulaType pApronFormulaType, String varName) { Preconditions.checkArgument(!environment.hasVar(varName),"Variablename already exists!"); Preconditions.checkArgument( - (pApronFormulaType.getType().equals(Type.INTEGER) || pApronFormulaType.getType().equals(Type.RATIONAL)), + (pApronFormulaType.getType().equals(FormulaType.INTEGER) || pApronFormulaType.getType().equals( + FormulaType.RATIONAL)), "Only Integer or rational variables allowed1"); - if(pApronFormulaType.getType().equals(Type.INTEGER)){ + if(pApronFormulaType.getType().equals(FormulaType.INTEGER)){ String[] intvars = new String[]{varName}; this.environment.add(intvars,new String[]{}); - return new ApronVar(); + return new ApronVar(varName,FormulaType.INTEGER); }else { String[] realvars = new String[]{varName}; this.environment.add(new String[]{}, realvars); - return new ApronVar(); + return new ApronVar(varName,FormulaType.RATIONAL); } } @Override - public FormulaType getFormulaType(ApronFormulas formula) { - //TODO - switch (formula.getFormulaType()){ - case VAR: - //... - case TERM: - //... - case COEFF: - //... - case CONSTRAINT: - //... - case EXPRESSION: - //... - default: - //.... - } + public org.sosy_lab.java_smt.api.FormulaType getFormulaType(ApronFormulas formula) { return null; } @@ -116,13 +102,13 @@ public R visit(FormulaVisitor visitor, Formula formula, ApronFormulas f) } @Override - public ApronFormulas callFunctionImpl(Long declaration, List args) { // nicht - // supported + public ApronFormulas callFunctionImpl(Long declaration, List args) { + // not supported return null; } @Override - public Long declareUFImpl( //nicht supported + public Long declareUFImpl( //not supported String pName, ApronFormulaType pReturnType, List pArgTypes) { diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java index e4afeb8685..d4fe193d62 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java @@ -20,16 +20,14 @@ package org.sosy_lab.java_smt.solvers.apron; -import apron.Environment; import java.math.BigDecimal; import java.math.BigInteger; import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.IntegerFormulaManager; import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula; -import org.sosy_lab.java_smt.basicimpl.FormulaCreator; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronIntegerType; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.Type; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas; public class ApronIntegerFormulaManager extends ApronNumeralFormulaManager implements @@ -45,8 +43,8 @@ protected ApronIntegerFormulaManager( } @Override - protected Type getNumeralType() { - return Type.INTEGER; + protected FormulaType getNumeralType() { + return FormulaType.INTEGER; } protected ApronFormulas makeNumberImpl(double pNumber) { diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java index 30f9b7e229..555ef3b747 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java @@ -22,54 +22,254 @@ import apron.Environment; import java.math.BigInteger; +import java.util.HashSet; import java.util.List; import org.sosy_lab.java_smt.api.NumeralFormula; import org.sosy_lab.java_smt.basicimpl.AbstractNumeralFormulaManager; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.Type; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas.ApronCoeff; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas.ApronExpr; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas.ApronTerm; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas.ApronVar; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas.FormulaCategory; abstract class ApronNumeralFormulaManager < ParamFormulaType extends NumeralFormula, ResultFormulaType extends NumeralFormula> extends AbstractNumeralFormulaManager< ApronFormulas, ApronFormulaType, Environment, ParamFormulaType, ResultFormulaType, Long> { + + private ApronFormulaCreator formulaCreator; protected ApronNumeralFormulaManager( FormulaCreator pCreator, NonLinearArithmetic pNonLinearArithmetic) { super(pCreator, pNonLinearArithmetic); + this.formulaCreator = (ApronFormulaCreator) pCreator; } protected boolean isNumeral(ApronFormulas val){ - return false; + FormulaType type = val.getType(); + return !type.equals(FormulaType.BOOLEAN); } - protected abstract Type getNumeralType(); + protected abstract FormulaType getNumeralType(); @Override protected ApronFormulas makeNumberImpl(long i) { + // return new ApronCoeff(new MpqScalar(BigInteger.valueOf(i)), i, FormulaType.INTEGER); return null; } @Override protected ApronFormulas makeNumberImpl(BigInteger i) { + //return new ApronCoeff(new MpqScalar(i),i,FormulaType.INTEGER); return null; } @Override protected ApronFormulas makeNumberImpl(String i) { + // return new ApronCoeff(new MpqScalar(Integer.parseInt(i)), Integer.parseInt(i), FormulaType + // .INTEGER); return null; } @Override protected ApronFormulas negate(ApronFormulas pParam1) { - return null; + FormulaCategory category = pParam1.getFormulaType(); + switch(category){ + case VAR: + ApronVar var = (ApronVar) pParam1; + return new ApronTerm(((ApronVar) pParam1).getVarName(),new ApronCoeff(-1,pParam1.getType()), + pParam1.getType()); + case COEFF: + ApronCoeff coeff = (ApronCoeff) pParam1; + coeff.negate(); + return coeff; + case TERM: + ApronTerm term = (ApronTerm) pParam1; + term.getApronCoeff().negate(); + return term; + case EXPRESSION: + ApronExpr expr = (ApronExpr) pParam1; + ApronTerm[] terms = expr.getApronTerms(); + ApronTerm[] negTerms = new ApronTerm[terms.length]; + for(int i=0;i newTerms = new HashSet<>(); + for(int i = 0; i operands){ return null; diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java index bf0eb8aefc..a957f2a991 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java @@ -20,15 +20,13 @@ package org.sosy_lab.java_smt.solvers.apron; -import apron.Environment; import java.math.BigDecimal; import org.sosy_lab.java_smt.api.NumeralFormula; import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula; import org.sosy_lab.java_smt.api.RationalFormulaManager; -import org.sosy_lab.java_smt.basicimpl.FormulaCreator; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronRationalType; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.Type; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas; public class ApronRationalFormulaManager extends @@ -45,8 +43,8 @@ protected ApronRationalFormulaManager( this.formulaCreator = pFormulaCreator; } @Override - protected Type getNumeralType() { - return Type.RATIONAL; + protected FormulaType getNumeralType() { + return FormulaType.RATIONAL; } @Override diff --git a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulaType.java b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulaType.java index dfaa98537d..c177360cbd 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulaType.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulaType.java @@ -20,23 +20,21 @@ package org.sosy_lab.java_smt.solvers.apron.types; -import org.sosy_lab.java_smt.solvers.apron.ApronIntegerFormulaManager; - public interface ApronFormulaType { - enum Type{ + enum FormulaType { BOOLEAN, INTEGER, RATIONAL } - Type getType(); + FormulaType getType(); class ApronIntegerType implements ApronFormulaType{ public ApronIntegerType(){} @Override - public Type getType(){ - return Type.INTEGER; + public FormulaType getType(){ + return FormulaType.INTEGER; } } @@ -45,8 +43,8 @@ class ApronRationalType implements ApronFormulaType{ public ApronRationalType(){} @Override - public Type getType(){ - return Type.RATIONAL; + public FormulaType getType(){ + return FormulaType.RATIONAL; } } @@ -55,8 +53,8 @@ class ApronBooleanType implements ApronFormulaType{ public ApronBooleanType(){ } @Override - public Type getType(){ - return Type.BOOLEAN; + public FormulaType getType(){ + return FormulaType.BOOLEAN; } } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulas.java b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulas.java index e66ce46010..1360d93749 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulas.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulas.java @@ -21,13 +21,22 @@ package org.sosy_lab.java_smt.solvers.apron.types; import apron.Coeff; +import apron.Environment; import apron.Linexpr1; import apron.Linterm1; +import apron.MpqScalar; +import apron.Scalar; +import apron.Var; import org.sosy_lab.java_smt.api.Formula; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; + +/** + * Wrapper-class for Apron Numerical Types; + */ public interface ApronFormulas extends Formula { - enum ApronFormulaType { + enum FormulaCategory { VAR, COEFF, TERM, @@ -35,60 +44,189 @@ enum ApronFormulaType { CONSTRAINT } - ApronFormulaType getFormulaType(); + FormulaCategory getFormulaType(); + FormulaType getType(); + + class ApronVar implements ApronFormulas,Var { + + private FormulaType type; + private String varName; + private Var apronVar; + + public ApronVar(String pVarName, FormulaType pType){ + this.type = pType; + this.varName = pVarName; + } + @Override + public FormulaCategory getFormulaType() { + return FormulaCategory.VAR; + } + + public String getVarName() { + return varName; + } - class ApronVar implements ApronFormulas { + @Override + public FormulaType getType() { + return type; + } - public ApronVar(){} @Override - public ApronFormulaType getFormulaType() { - return ApronFormulaType.VAR; + public Var clone() { + return null; + } + + @Override + public int compareTo(Var var) { + ApronVar apronVar1 = (ApronVar) var; + if (apronVar1.getVarName().equals(this.varName)){ + return 1; + } + return 0; } } class ApronCoeff implements ApronFormulas { - public ApronCoeff(){} + private Coeff apronCoeff; + private int value; + private FormulaType type; + + public ApronCoeff(int pValue, FormulaType pType){ + this.apronCoeff = new MpqScalar(pValue); + this.type = pType; + this.value = pValue; + } @Override - public ApronFormulaType getFormulaType() { - return ApronFormulaType.COEFF; + public FormulaCategory getFormulaType() { + return FormulaCategory.COEFF; + } + + public int getValue() { + return value; + } + + public void setValue(int pValue) { + value = pValue; + this.apronCoeff = new MpqScalar(pValue); } + + @Override + public FormulaType getType() { + return type; + } + + private Coeff getApronCoeff(){ + return apronCoeff; + } + + public void negate(){ + this.value = (-1)*value; + this.apronCoeff = new MpqScalar(value); + } + } class ApronTerm implements ApronFormulas { private Linterm1 linterm1; + private FormulaType type; - public ApronTerm(String pVar,Coeff pCoeff){ - this.linterm1 = new Linterm1(pVar,pCoeff); + private ApronVar apronVar; + private ApronCoeff apronCoeff; + + public ApronTerm(String pVar,ApronCoeff pCoeff, FormulaType pType){ + this.linterm1 = new Linterm1(pVar,new MpqScalar(pCoeff.getValue())); + this.type = pType; + this.apronVar = new ApronVar(pVar,pType); + this.apronCoeff = pCoeff; } @Override - public ApronFormulaType getFormulaType() { - return ApronFormulaType.TERM; + public FormulaCategory getFormulaType() { + return FormulaCategory.TERM; + } + + @Override + public FormulaType getType() { + return type; + } + + private Linterm1 getApronTerm(){ + return linterm1; + } + + public ApronVar getApronVar() { + return apronVar; + } + + public ApronCoeff getApronCoeff() { + return apronCoeff; } } class ApronExpr implements ApronFormulas { private Linexpr1 linexpr1; + private FormulaType type; + private ApronTerm[] apronTerms; + + private int cstValue; + + public ApronExpr(ApronTerm[] pApronTerms,int pCstValue, + Environment pEnvironment, + FormulaType pType){ + Linterm1[] terms = new Linterm1[pApronTerms.length]; + for(int i =0;i Date: Thu, 10 Aug 2023 21:10:24 +0200 Subject: [PATCH 006/133] ApronNumeralFormulaManager add-function adjusted --- .../solvers/apron/ApronFormulaManager.java | 17 +- .../apron/ApronNumeralFormulaManager.java | 225 +++++++++--------- 2 files changed, 122 insertions(+), 120 deletions(-) diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java index d60fed1ea2..2d6ea15b64 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java @@ -39,8 +39,10 @@ import org.sosy_lab.java_smt.basicimpl.AbstractStringFormulaManager; import org.sosy_lab.java_smt.basicimpl.AbstractUFManager; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas; -public class ApronFormulaManager extends AbstractFormulaManager { +public class ApronFormulaManager extends AbstractFormulaManager { /** * Builds a solver from the given theory implementations. * @@ -70,10 +72,8 @@ protected ApronFormulaManager( @Nullable AbstractSLFormulaManager slManager, @Nullable AbstractStringFormulaManager strManager, @Nullable AbstractEnumerationFormulaManager enumManager) { - super(pFormulaCreator, functionManager, booleanManager, pIntegerFormulaManager, - pRationalFormulaManager, - bitvectorManager, floatingPointManager, quantifiedManager, arrayManager, slManager, - strManager, enumManager); + super(pFormulaCreator,functionManager,booleanManager,pIntegerFormulaManager, + pRationalFormulaManager,null,null,null,null,null,null,null); } @Override @@ -82,13 +82,14 @@ public BooleanFormula parse(String s) throws IllegalArgumentException { } @Override - public T substitute(T f, - Map fromToMapping) { + public Appender dumpFormula(ApronFormulas t) { return null; } @Override - public Appender dumpFormula(Long t) { + public T substitute(T f, + Map fromToMapping) { return null; } + } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java index 555ef3b747..afec2ec7a3 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java @@ -36,12 +36,13 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas.ApronVar; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas.FormulaCategory; -abstract class ApronNumeralFormulaManager < +abstract class ApronNumeralFormulaManager< ParamFormulaType extends NumeralFormula, ResultFormulaType extends NumeralFormula> extends AbstractNumeralFormulaManager< ApronFormulas, ApronFormulaType, Environment, ParamFormulaType, ResultFormulaType, Long> { private ApronFormulaCreator formulaCreator; + protected ApronNumeralFormulaManager( FormulaCreator pCreator, NonLinearArithmetic pNonLinearArithmetic) { @@ -50,14 +51,16 @@ protected ApronNumeralFormulaManager( } - protected boolean isNumeral(ApronFormulas val){ + protected boolean isNumeral(ApronFormulas val) { FormulaType type = val.getType(); return !type.equals(FormulaType.BOOLEAN); } + protected abstract FormulaType getNumeralType(); + @Override protected ApronFormulas makeNumberImpl(long i) { - // return new ApronCoeff(new MpqScalar(BigInteger.valueOf(i)), i, FormulaType.INTEGER); + // return new ApronCoeff(new MpqScalar(BigInteger.valueOf(i)), i, FormulaType.INTEGER); return null; } @@ -69,7 +72,7 @@ protected ApronFormulas makeNumberImpl(BigInteger i) { @Override protected ApronFormulas makeNumberImpl(String i) { - // return new ApronCoeff(new MpqScalar(Integer.parseInt(i)), Integer.parseInt(i), FormulaType + // return new ApronCoeff(new MpqScalar(Integer.parseInt(i)), Integer.parseInt(i), FormulaType // .INTEGER); return null; } @@ -77,10 +80,11 @@ protected ApronFormulas makeNumberImpl(String i) { @Override protected ApronFormulas negate(ApronFormulas pParam1) { FormulaCategory category = pParam1.getFormulaType(); - switch(category){ + switch (category) { case VAR: ApronVar var = (ApronVar) pParam1; - return new ApronTerm(((ApronVar) pParam1).getVarName(),new ApronCoeff(-1,pParam1.getType()), + return new ApronTerm(((ApronVar) pParam1).getVarName(), + new ApronCoeff(-1, pParam1.getType()), pParam1.getType()); case COEFF: ApronCoeff coeff = (ApronCoeff) pParam1; @@ -94,13 +98,13 @@ protected ApronFormulas negate(ApronFormulas pParam1) { ApronExpr expr = (ApronExpr) pParam1; ApronTerm[] terms = expr.getApronTerms(); ApronTerm[] negTerms = new ApronTerm[terms.length]; - for(int i=0;i newTerms = new HashSet<>(); - for(int i = 0; i operands){ + protected ApronFormulas sumImpl(List operands) { return null; } + @Override protected ApronFormulas subtract(ApronFormulas pParam1, ApronFormulas pParam2) { return null; From 0480cc402cb698a3d9cfd260dc4b0af8c601ac87 Mon Sep 17 00:00:00 2001 From: winnieros Date: Mon, 14 Aug 2023 11:15:15 +0200 Subject: [PATCH 007/133] ApronNumeralFormulaManager add-function adjusted --- .../java_smt/solvers/apron/ApronExamples.java | 8 + .../solvers/apron/ApronFormulaCreator.java | 31 ++- .../solvers/apron/ApronFormulaManager.java | 12 +- .../apron/ApronIntegerFormulaManager.java | 8 +- .../apron/ApronNumeralFormulaManager.java | 250 +++--------------- .../apron/ApronRationalFormulaManager.java | 8 +- .../solvers/apron/types/ApronFormulas.java | 232 ---------------- .../solvers/apron/types/ApronNode.java | 143 ++++++++++ 8 files changed, 216 insertions(+), 476 deletions(-) delete mode 100644 src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulas.java create mode 100644 src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java index 36d2d38e7b..ecdfb1a3da 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java @@ -50,6 +50,14 @@ private static void testBox(Manager pManager) throws ApronException { cons2.setKind(Lincons1.SUPEQ); Abstract1 abstract1 = new Abstract1(pManager, new Lincons1[]{cons1,cons2}); + // x+x-x=0 + Linterm1 linterm1 = new Linterm1("x",new MpqScalar(1)); + Linterm1 linterm2 = new Linterm1("x",new MpqScalar(1)); + Linterm1 linterm3 = new Linterm1("x",new MpqScalar(-1)); + Linterm1[] terms = new Linterm1[]{linterm3,linterm2,linterm1}; + Linexpr1 linexpr1 = new Linexpr1(environment,terms,new MpqScalar(0)); + Lincons1 cons = new Lincons1(Lincons1.EQ,linexpr1); + //is x = 1 satisfiable? Lincons1 cons3 = new Lincons1(environment); cons3.setCoeff("x",new MpqScalar(1)); diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java index 2366f3fbfe..4111df5668 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java @@ -29,15 +29,15 @@ import org.sosy_lab.java_smt.api.visitors.FormulaVisitor; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronBooleanType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronIntegerType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronRationalType; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas.ApronVar; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas.FormulaCategory; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronVarNode; -public class ApronFormulaCreator extends FormulaCreator { +public class ApronFormulaCreator extends FormulaCreator { private Environment environment; protected ApronFormulaCreator( @@ -72,51 +72,50 @@ public ApronFormulaType getArrayType(ApronFormulaType indexType, ApronFormulaTyp } @Override - public ApronFormulas makeVariable(ApronFormulaType pApronFormulaType, String varName) { + public ApronNode makeVariable(ApronFormulaType pApronFormulaType, String varName) { Preconditions.checkArgument(!environment.hasVar(varName),"Variablename already exists!"); Preconditions.checkArgument( (pApronFormulaType.getType().equals(FormulaType.INTEGER) || pApronFormulaType.getType().equals( FormulaType.RATIONAL)), - "Only Integer or rational variables allowed1"); + "Only Integer or rational variables allowed!"); if(pApronFormulaType.getType().equals(FormulaType.INTEGER)){ String[] intvars = new String[]{varName}; this.environment.add(intvars,new String[]{}); - return new ApronVar(varName,FormulaType.INTEGER); + return new ApronVarNode(FormulaType.INTEGER, varName); }else { String[] realvars = new String[]{varName}; this.environment.add(new String[]{}, realvars); - return new ApronVar(varName,FormulaType.RATIONAL); + return new ApronVarNode(FormulaType.RATIONAL, varName); } } @Override - public org.sosy_lab.java_smt.api.FormulaType getFormulaType(ApronFormulas formula) { + public org.sosy_lab.java_smt.api.FormulaType getFormulaType(ApronNode formula) { return null; } @Override - public R visit(FormulaVisitor visitor, Formula formula, ApronFormulas f) { //hinten + public R visit(FormulaVisitor visitor, Formula formula, ApronNode f) { //hinten // anstellen, Frage kann man eine formel in alle kleinteile zerlegen und dann wieder // zusammenbauen? return null; } @Override - public ApronFormulas callFunctionImpl(Long declaration, List args) { - // not supported - return null; + public ApronNode callFunctionImpl(Long declaration, List args) { + throw new UnsupportedOperationException(); } @Override - public Long declareUFImpl( //not supported + public Long declareUFImpl( String pName, ApronFormulaType pReturnType, List pArgTypes) { - return null; + throw new UnsupportedOperationException("Apron does not support uninterpreted functions."); } @Override - protected Long getBooleanVarDeclarationImpl(ApronFormulas pApronFormula) { //brauche ich nicht + protected Long getBooleanVarDeclarationImpl(ApronNode pApronFormula) { //brauche ich nicht return null; } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java index 2d6ea15b64..c634f1665e 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java @@ -26,23 +26,19 @@ import org.sosy_lab.common.Appender; import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.Formula; -import org.sosy_lab.java_smt.api.IntegerFormulaManager; -import org.sosy_lab.java_smt.api.RationalFormulaManager; import org.sosy_lab.java_smt.basicimpl.AbstractArrayFormulaManager; import org.sosy_lab.java_smt.basicimpl.AbstractBitvectorFormulaManager; -import org.sosy_lab.java_smt.basicimpl.AbstractBooleanFormulaManager; import org.sosy_lab.java_smt.basicimpl.AbstractEnumerationFormulaManager; import org.sosy_lab.java_smt.basicimpl.AbstractFloatingPointFormulaManager; import org.sosy_lab.java_smt.basicimpl.AbstractFormulaManager; import org.sosy_lab.java_smt.basicimpl.AbstractQuantifiedFormulaManager; import org.sosy_lab.java_smt.basicimpl.AbstractSLFormulaManager; import org.sosy_lab.java_smt.basicimpl.AbstractStringFormulaManager; -import org.sosy_lab.java_smt.basicimpl.AbstractUFManager; -import org.sosy_lab.java_smt.basicimpl.FormulaCreator; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; -public class ApronFormulaManager extends AbstractFormulaManager { +public class ApronFormulaManager extends AbstractFormulaManager { /** * Builds a solver from the given theory implementations. * @@ -82,7 +78,7 @@ public BooleanFormula parse(String s) throws IllegalArgumentException { } @Override - public Appender dumpFormula(ApronFormulas t) { + public Appender dumpFormula(ApronNode t) { return null; } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java index d4fe193d62..de4ff5985a 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java @@ -28,7 +28,7 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronIntegerType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; public class ApronIntegerFormulaManager extends ApronNumeralFormulaManager implements IntegerFormulaManager { @@ -47,12 +47,12 @@ protected FormulaType getNumeralType() { return FormulaType.INTEGER; } - protected ApronFormulas makeNumberImpl(double pNumber) { + protected ApronNode makeNumberImpl(double pNumber) { return null; } @Override - protected ApronFormulas makeNumberImpl(BigDecimal pNumber) { + protected ApronNode makeNumberImpl(BigDecimal pNumber) { return null; } @@ -76,7 +76,7 @@ public IntegerFormula modulo(IntegerFormula numerator, IntegerFormula denumerato @Override - protected ApronFormulas makeVariableImpl(String i) { + protected ApronNode makeVariableImpl(String i) { return this.formulaCreator.makeVariable(integerType,i); } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java index afec2ec7a3..e4e7e5dddc 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java @@ -21,6 +21,9 @@ package org.sosy_lab.java_smt.solvers.apron; import apron.Environment; +import apron.Texpr1BinNode; +import apron.Texpr1Node; +import apron.Texpr1UnNode; import java.math.BigInteger; import java.util.HashSet; import java.util.List; @@ -29,29 +32,26 @@ import org.sosy_lab.java_smt.basicimpl.FormulaCreator; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas.ApronCoeff; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas.ApronExpr; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas.ApronTerm; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas.ApronVar; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas.FormulaCategory; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronBinaryNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronUnaryNode; abstract class ApronNumeralFormulaManager< ParamFormulaType extends NumeralFormula, ResultFormulaType extends NumeralFormula> extends AbstractNumeralFormulaManager< - ApronFormulas, ApronFormulaType, Environment, ParamFormulaType, ResultFormulaType, Long> { + ApronNode, ApronFormulaType, Environment, ParamFormulaType, ResultFormulaType, Long> { private ApronFormulaCreator formulaCreator; protected ApronNumeralFormulaManager( - FormulaCreator pCreator, + FormulaCreator pCreator, NonLinearArithmetic pNonLinearArithmetic) { super(pCreator, pNonLinearArithmetic); this.formulaCreator = (ApronFormulaCreator) pCreator; } - protected boolean isNumeral(ApronFormulas val) { + protected boolean isNumeral(ApronNode val) { FormulaType type = val.getType(); return !type.equals(FormulaType.BOOLEAN); } @@ -59,264 +59,90 @@ protected boolean isNumeral(ApronFormulas val) { protected abstract FormulaType getNumeralType(); @Override - protected ApronFormulas makeNumberImpl(long i) { + protected ApronNode makeNumberImpl(long i) { // return new ApronCoeff(new MpqScalar(BigInteger.valueOf(i)), i, FormulaType.INTEGER); return null; } @Override - protected ApronFormulas makeNumberImpl(BigInteger i) { + protected ApronNode makeNumberImpl(BigInteger i) { //return new ApronCoeff(new MpqScalar(i),i,FormulaType.INTEGER); return null; } @Override - protected ApronFormulas makeNumberImpl(String i) { + protected ApronNode makeNumberImpl(String i) { // return new ApronCoeff(new MpqScalar(Integer.parseInt(i)), Integer.parseInt(i), FormulaType // .INTEGER); return null; } @Override - protected ApronFormulas negate(ApronFormulas pParam1) { - FormulaCategory category = pParam1.getFormulaType(); - switch (category) { - case VAR: - ApronVar var = (ApronVar) pParam1; - return new ApronTerm(((ApronVar) pParam1).getVarName(), - new ApronCoeff(-1, pParam1.getType()), - pParam1.getType()); - case COEFF: - ApronCoeff coeff = (ApronCoeff) pParam1; - coeff.negate(); - return coeff; - case TERM: - ApronTerm term = (ApronTerm) pParam1; - term.getApronCoeff().negate(); - return term; - case EXPRESSION: - ApronExpr expr = (ApronExpr) pParam1; - ApronTerm[] terms = expr.getApronTerms(); - ApronTerm[] negTerms = new ApronTerm[terms.length]; - for (int i = 0; i < terms.length; i++) { - ApronTerm t = terms[i]; - t.getApronCoeff().negate(); - negTerms[i] = t; - } - int negCst = expr.getCstValue() * (-1); - return new ApronExpr(negTerms, negCst, formulaCreator.getEnvironment(), - pParam1.getType()); - default: - throw new IllegalArgumentException("This formula is not negate-able"); - } + protected ApronNode negate(ApronNode pParam1) { + ApronUnaryNode unaryNode = new ApronUnaryNode(pParam1.getType(),pParam1, Texpr1UnNode.OP_NEG); + return unaryNode; } @Override - protected ApronFormulas add(ApronFormulas pParam1, ApronFormulas pParam2) { - FormulaCategory param1cat = pParam1.getFormulaType(); - FormulaCategory param2cat = pParam2.getFormulaType(); - FormulaType formulaType = pParam1.getType(); //TODO - switch (param1cat) { - case COEFF: - switch (param2cat) { - case COEFF: - addCoeffAndCoeff((ApronCoeff) pParam1, (ApronCoeff) pParam2, formulaType); - case VAR: - addCoeffAndVar((ApronCoeff) pParam1, (ApronVar) pParam2, formulaType); - case TERM: - addCoeffAndTerm((ApronCoeff) pParam1, (ApronTerm) pParam2, formulaType); - case EXPRESSION: - addCoeffAndExpr((ApronCoeff) pParam1, (ApronExpr) pParam2, formulaType); - default: - throw new IllegalArgumentException("Coeff can not be added to the second " - + "Parameter!"); - } - case VAR: - switch (param2cat) { - case COEFF: - addCoeffAndVar((ApronCoeff) pParam2, (ApronVar) pParam1, formulaType); - case VAR: - addVarAndVar((ApronVar) pParam1, (ApronVar) pParam2, formulaType); - case TERM: - addVarAndTerm((ApronVar) pParam1, (ApronTerm) pParam2, formulaType); - case EXPRESSION: - addVarAndExpr((ApronVar) pParam1, (ApronExpr) pParam2, formulaType); - default: - throw new IllegalArgumentException("Var can not be added to the second " - + "Parameter!"); - } - case TERM: - switch (param2cat) { - case COEFF: - addCoeffAndTerm((ApronCoeff) pParam2, (ApronTerm) pParam1, formulaType); - case VAR: - addVarAndTerm((ApronVar) pParam2, (ApronTerm) pParam1, formulaType); - case TERM: - addTermAndTerm((ApronTerm) pParam1, (ApronTerm) pParam2, formulaType); - case EXPRESSION: - addTermAndExpr((ApronTerm) pParam1, (ApronExpr) pParam2, formulaType); - default: - throw new IllegalArgumentException("Term can not be added to the second " - + "Parameter!"); - } - case EXPRESSION: - switch (param2cat) { - case COEFF: - addCoeffAndExpr((ApronCoeff) pParam2, (ApronExpr) pParam1, formulaType); - case VAR: - addVarAndExpr((ApronVar) pParam2, (ApronExpr) pParam1, formulaType); - case TERM: - addTermAndExpr((ApronTerm) pParam2, (ApronExpr) pParam1, formulaType); - case EXPRESSION: - addExprAndExpr((ApronExpr) pParam1, (ApronExpr) pParam2, formulaType); - default: - throw new IllegalArgumentException("Expression can not be added to the second " - + "Parameter!"); - } - default: - throw new IllegalArgumentException("Parameter 1 can not be added to the second " - + "Parameter!"); - } + protected ApronNode add(ApronNode pParam1, ApronNode pParam2) { + ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(),pParam1,pParam2, + Texpr1BinNode.OP_ADD); + return binaryNode; } - private ApronExpr addCoeffAndVar(ApronCoeff pApronCoeff, ApronVar pVar, FormulaType pType) { - ApronTerm term = new ApronTerm(pVar.getVarName(), new ApronCoeff(1, pType), pType); - ApronTerm[] terms = new ApronTerm[]{term}; - return new ApronExpr(terms, pApronCoeff.getValue(), formulaCreator.getEnvironment(), pType); - } - - private ApronCoeff addCoeffAndCoeff(ApronCoeff coeff1, ApronCoeff coeff2, FormulaType pType) { - int newValue = coeff1.getValue() + coeff2.getValue(); - return new ApronCoeff(newValue, pType); - } - - private ApronExpr addCoeffAndTerm(ApronCoeff coeff, ApronTerm term, FormulaType pType) { - ApronTerm[] linterm1s = new ApronTerm[]{term}; - return new ApronExpr(linterm1s, coeff.getValue(), - formulaCreator.getEnvironment(), - pType); - } - - private ApronExpr addCoeffAndExpr(ApronCoeff apronCoeff, ApronExpr expr, FormulaType pType) { - int newCoeff = apronCoeff.getValue() + expr.getCstValue(); - return new ApronExpr(expr.getApronTerms(), newCoeff, formulaCreator.getEnvironment(), pType); - } - - private ApronFormulas addVarAndVar(ApronVar var1, ApronVar var2, FormulaType pType) { - ApronTerm linterm1 = new ApronTerm(var1.getVarName(), new ApronCoeff(1, pType), pType); - ApronTerm linterm2 = new ApronTerm(var2.getVarName(), new ApronCoeff(1, pType), pType); - ApronTerm[] linterms = new ApronTerm[]{linterm1, linterm2}; - return new ApronExpr(linterms, 0, formulaCreator.getEnvironment(), pType); - - } - - private ApronFormulas addVarAndTerm(ApronVar pVar, ApronTerm pTerm, FormulaType pType) { - - ApronTerm linterm1 = new ApronTerm(pVar.getVarName(), new ApronCoeff(1, pType), pType); - ApronTerm[] terms = new ApronTerm[]{linterm1, pTerm}; - return new ApronExpr(terms, 0, formulaCreator.getEnvironment(), pType); - - } - - private ApronExpr addVarAndExpr(ApronVar pVar, ApronExpr pExpr, FormulaType pType) { - ApronTerm[] terms = pExpr.getApronTerms(); - ApronTerm[] newTerms = new ApronTerm[terms.length + 1]; - int i = 0; - for (ApronTerm term : terms) { - newTerms[i] = term; - i++; - } - newTerms[terms.length] = new ApronTerm(pVar.getVarName(), new ApronCoeff(1, pType), pType); - return new ApronExpr(newTerms, pExpr.getCstValue(), formulaCreator.getEnvironment(), pType); - } - - private ApronFormulas addTermAndTerm(ApronTerm pTerm1, ApronTerm pTerm2, FormulaType pType) { - - ApronTerm[] terms = new ApronTerm[]{pTerm1, pTerm2}; - return new ApronExpr(terms, 0, formulaCreator.getEnvironment(), pType); - - } - - private ApronExpr addTermAndExpr(ApronTerm pTerm, ApronExpr pExpr, FormulaType pType) { - ApronTerm[] exprTerms = pExpr.getApronTerms(); - ApronTerm[] newTerms = new ApronTerm[exprTerms.length + 1]; - for (int i = 0; i < exprTerms.length; i++) { - newTerms[i] = exprTerms[i]; - } - newTerms[exprTerms.length] = pTerm; - return new ApronExpr(newTerms, pExpr.getCstValue(), formulaCreator.getEnvironment(), pType); - } - - private ApronExpr addExprAndExpr(ApronExpr pExpr1, ApronExpr pExpr2, FormulaType pType) { - ApronTerm[] terms1 = pExpr1.getApronTerms(); - ApronTerm[] terms2 = pExpr2.getApronTerms(); - HashSet newTerms = new HashSet<>(); - for (int i = 0; i < terms1.length; i++) { - for (int j = 0; j < terms2.length; j++) { - - newTerms.add(terms2[j]); - - } - newTerms.add(terms1[i]); - } - ApronTerm[] finalTerms = new ApronTerm[newTerms.size()]; - int i = 0; - for (ApronTerm term : newTerms) { - finalTerms[i] = term; - i++; - } - int finalCst = pExpr1.getCstValue() + pExpr2.getCstValue(); - return new ApronExpr(finalTerms, finalCst, formulaCreator.getEnvironment(), pType); - } - - @Override - protected ApronFormulas sumImpl(List operands) { + protected ApronNode sumImpl(List operands) { return null; } @Override - protected ApronFormulas subtract(ApronFormulas pParam1, ApronFormulas pParam2) { - return null; + protected ApronNode subtract(ApronNode pParam1, ApronNode pParam2) { + ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(), pParam1,pParam2, + Texpr1BinNode.OP_SUB); + return binaryNode; } @Override - protected ApronFormulas divide(ApronFormulas pParam1, ApronFormulas pParam2) { - return null; + protected ApronNode divide(ApronNode pParam1, ApronNode pParam2) { + ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(), pParam1,pParam2, + Texpr1BinNode.OP_DIV); + return binaryNode; } @Override - protected ApronFormulas multiply(ApronFormulas pParam1, ApronFormulas pParam2) { - return null; + protected ApronNode multiply(ApronNode pParam1, ApronNode pParam2) { + ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(), pParam1,pParam2, + Texpr1BinNode.OP_MUL); + return binaryNode; } @Override - protected ApronFormulas equal(ApronFormulas pParam1, ApronFormulas pParam2) { + protected ApronNode equal(ApronNode pParam1, ApronNode pParam2) { return null; } @Override - protected ApronFormulas distinctImpl(List pNumbers) { + protected ApronNode distinctImpl(List pNumbers) { return null; } @Override - protected ApronFormulas greaterThan(ApronFormulas pParam1, ApronFormulas pParam2) { + protected ApronNode greaterThan(ApronNode pParam1, ApronNode pParam2) { return null; } @Override - protected ApronFormulas greaterOrEquals(ApronFormulas pParam1, ApronFormulas pParam2) { + protected ApronNode greaterOrEquals(ApronNode pParam1, ApronNode pParam2) { return null; } @Override - protected ApronFormulas lessThan(ApronFormulas pParam1, ApronFormulas pParam2) { + protected ApronNode lessThan(ApronNode pParam1, ApronNode pParam2) { return null; } @Override - protected ApronFormulas lessOrEquals(ApronFormulas pParam1, ApronFormulas pParam2) { + protected ApronNode lessOrEquals(ApronNode pParam1, ApronNode pParam2) { return null; } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java index a957f2a991..4634b42b01 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java @@ -27,7 +27,7 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronRationalType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulas; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; public class ApronRationalFormulaManager extends ApronNumeralFormulaManager @@ -48,18 +48,18 @@ protected FormulaType getNumeralType() { } @Override - protected ApronFormulas makeNumberImpl(double pNumber) { + protected ApronNode makeNumberImpl(double pNumber) { return null; } @Override - protected ApronFormulas makeNumberImpl(BigDecimal pNumber) { + protected ApronNode makeNumberImpl(BigDecimal pNumber) { return null; } @Override - protected ApronFormulas makeVariableImpl(String i) { + protected ApronNode makeVariableImpl(String i) { return formulaCreator.makeVariable(rationalType,i); } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulas.java b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulas.java deleted file mode 100644 index 1360d93749..0000000000 --- a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulas.java +++ /dev/null @@ -1,232 +0,0 @@ -/* - * JavaSMT is an API wrapper for a collection of SMT solvers. - * This file is part of JavaSMT. - * - * Copyright (C) 2007-2016 Dirk Beyer - * All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.sosy_lab.java_smt.solvers.apron.types; - -import apron.Coeff; -import apron.Environment; -import apron.Linexpr1; -import apron.Linterm1; -import apron.MpqScalar; -import apron.Scalar; -import apron.Var; -import org.sosy_lab.java_smt.api.Formula; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; - -/** - * Wrapper-class for Apron Numerical Types; - */ - -public interface ApronFormulas extends Formula { - - enum FormulaCategory { - VAR, - COEFF, - TERM, - EXPRESSION, - CONSTRAINT - } - - FormulaCategory getFormulaType(); - FormulaType getType(); - - class ApronVar implements ApronFormulas,Var { - - private FormulaType type; - private String varName; - private Var apronVar; - - public ApronVar(String pVarName, FormulaType pType){ - this.type = pType; - this.varName = pVarName; - } - @Override - public FormulaCategory getFormulaType() { - return FormulaCategory.VAR; - } - - public String getVarName() { - return varName; - } - - @Override - public FormulaType getType() { - return type; - } - - @Override - public Var clone() { - return null; - } - - @Override - public int compareTo(Var var) { - ApronVar apronVar1 = (ApronVar) var; - if (apronVar1.getVarName().equals(this.varName)){ - return 1; - } - return 0; - } - } - - class ApronCoeff implements ApronFormulas { - - private Coeff apronCoeff; - private int value; - private FormulaType type; - - public ApronCoeff(int pValue, FormulaType pType){ - this.apronCoeff = new MpqScalar(pValue); - this.type = pType; - this.value = pValue; - } - @Override - public FormulaCategory getFormulaType() { - return FormulaCategory.COEFF; - } - - public int getValue() { - return value; - } - - public void setValue(int pValue) { - value = pValue; - this.apronCoeff = new MpqScalar(pValue); - } - - @Override - public FormulaType getType() { - return type; - } - - private Coeff getApronCoeff(){ - return apronCoeff; - } - - public void negate(){ - this.value = (-1)*value; - this.apronCoeff = new MpqScalar(value); - } - - } - - class ApronTerm implements ApronFormulas { - private Linterm1 linterm1; - private FormulaType type; - - private ApronVar apronVar; - private ApronCoeff apronCoeff; - - public ApronTerm(String pVar,ApronCoeff pCoeff, FormulaType pType){ - this.linterm1 = new Linterm1(pVar,new MpqScalar(pCoeff.getValue())); - this.type = pType; - this.apronVar = new ApronVar(pVar,pType); - this.apronCoeff = pCoeff; - } - - @Override - public FormulaCategory getFormulaType() { - return FormulaCategory.TERM; - } - - @Override - public FormulaType getType() { - return type; - } - - private Linterm1 getApronTerm(){ - return linterm1; - } - - public ApronVar getApronVar() { - return apronVar; - } - - public ApronCoeff getApronCoeff() { - return apronCoeff; - } - } - - class ApronExpr implements ApronFormulas { - private Linexpr1 linexpr1; - private FormulaType type; - private ApronTerm[] apronTerms; - - private int cstValue; - - public ApronExpr(ApronTerm[] pApronTerms,int pCstValue, - Environment pEnvironment, - FormulaType pType){ - Linterm1[] terms = new Linterm1[pApronTerms.length]; - for(int i =0;i Date: Mon, 14 Aug 2023 11:57:31 +0200 Subject: [PATCH 008/133] Numeral constraints --- .../apron/ApronNumeralFormulaManager.java | 32 ++++++++++++++++--- .../solvers/apron/types/ApronNode.java | 3 +- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java index e4e7e5dddc..7d13da7bc1 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java @@ -21,6 +21,7 @@ package org.sosy_lab.java_smt.solvers.apron; import apron.Environment; +import apron.Tcons1; import apron.Texpr1BinNode; import apron.Texpr1Node; import apron.Texpr1UnNode; @@ -34,6 +35,7 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronBinaryNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronUnaryNode; abstract class ApronNumeralFormulaManager< @@ -118,7 +120,11 @@ protected ApronNode multiply(ApronNode pParam1, ApronNode pParam2) { @Override protected ApronNode equal(ApronNode pParam1, ApronNode pParam2) { - return null; + ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(), pParam1,pParam2, + Texpr1BinNode.OP_SUB); + ApronConstraint constraint = new ApronConstraint(Tcons1.EQ,formulaCreator.getEnvironment(), + binaryNode.getNode()); + return constraint; } @Override @@ -128,21 +134,37 @@ protected ApronNode distinctImpl(List pNumbers) { @Override protected ApronNode greaterThan(ApronNode pParam1, ApronNode pParam2) { - return null; + ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(), pParam1,pParam2, + Texpr1BinNode.OP_SUB); + ApronConstraint constraint = new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(), + binaryNode.getNode()); + return constraint; } @Override protected ApronNode greaterOrEquals(ApronNode pParam1, ApronNode pParam2) { - return null; + ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(), pParam1,pParam2, + Texpr1BinNode.OP_SUB); + ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ,formulaCreator.getEnvironment(), + binaryNode.getNode()); + return constraint; } @Override protected ApronNode lessThan(ApronNode pParam1, ApronNode pParam2) { - return null; + ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(), pParam2,pParam1, + Texpr1BinNode.OP_SUB); + ApronConstraint constraint = new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(), + binaryNode.getNode()); + return constraint; } @Override protected ApronNode lessOrEquals(ApronNode pParam1, ApronNode pParam2) { - return null; + ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(), pParam2,pParam1, + Texpr1BinNode.OP_SUB); + ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ,formulaCreator.getEnvironment(), + binaryNode.getNode()); + return constraint; } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java index 06a9cb3102..8ac3d091e0 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java @@ -33,6 +33,7 @@ import gmp.Mpz; import java.math.BigDecimal; import java.math.BigInteger; +import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.Formula; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; @@ -118,7 +119,7 @@ public Texpr1Node getNode() { } } - class ApronConstraint implements ApronNode{ + class ApronConstraint implements ApronNode, BooleanFormula { private Tcons1 constraintNode; private Texpr1Node node; From b8a40bca2955f58e83ad74b0ae8563c409c14661 Mon Sep 17 00:00:00 2001 From: winnieros Date: Mon, 14 Aug 2023 15:02:52 +0200 Subject: [PATCH 009/133] New Type System for ApronFormulas --- .../solvers/apron/ApronFormulaCreator.java | 7 +- .../apron/ApronIntegerFormulaManager.java | 120 +++++++++++++++++- .../apron/ApronNumeralFormulaManager.java | 110 +--------------- .../apron/ApronRationalFormulaManager.java | 112 ++++++++++++++++ .../solvers/apron/types/ApronNode.java | 111 +++++++++++++--- 5 files changed, 326 insertions(+), 134 deletions(-) diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java index 4111df5668..ecaa7120ee 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java @@ -34,7 +34,8 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronIntegerType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronRationalType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronVarNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronIntVarNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatVarNode; public class ApronFormulaCreator extends FormulaCreator { @@ -81,11 +82,11 @@ public ApronNode makeVariable(ApronFormulaType pApronFormulaType, String varName if(pApronFormulaType.getType().equals(FormulaType.INTEGER)){ String[] intvars = new String[]{varName}; this.environment.add(intvars,new String[]{}); - return new ApronVarNode(FormulaType.INTEGER, varName); + return new ApronIntVarNode(varName); }else { String[] realvars = new String[]{varName}; this.environment.add(new String[]{}, realvars); - return new ApronVarNode(FormulaType.RATIONAL, varName); + return new ApronRatVarNode(varName); } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java index de4ff5985a..dc7d31ce27 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java @@ -20,8 +20,12 @@ package org.sosy_lab.java_smt.solvers.apron; +import apron.Tcons1; +import apron.Texpr1BinNode; +import apron.Texpr1UnNode; import java.math.BigDecimal; import java.math.BigInteger; +import java.util.List; import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.IntegerFormulaManager; import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula; @@ -29,6 +33,10 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronIntegerType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronIntBinaryNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronIntCstNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronIntUnaryNode; public class ApronIntegerFormulaManager extends ApronNumeralFormulaManager implements IntegerFormulaManager { @@ -48,12 +56,12 @@ protected FormulaType getNumeralType() { } protected ApronNode makeNumberImpl(double pNumber) { - return null; + return new ApronIntCstNode(BigInteger.valueOf((int) pNumber)); } @Override protected ApronNode makeNumberImpl(BigDecimal pNumber) { - return null; + return new ApronIntCstNode(pNumber.toBigInteger()); } @Override @@ -71,7 +79,11 @@ public BooleanFormula modularCongruence(IntegerFormula number1, IntegerFormula n @Override public IntegerFormula modulo(IntegerFormula numerator, IntegerFormula denumerator) { - return null; + ApronNode node1 = (ApronNode) numerator; + ApronNode node2 = (ApronNode) denumerator; + ApronIntBinaryNode result = new ApronIntBinaryNode(node1, node2, + Texpr1BinNode.OP_MOD); + return result; } @@ -79,5 +91,107 @@ public IntegerFormula modulo(IntegerFormula numerator, IntegerFormula denumerato protected ApronNode makeVariableImpl(String i) { return this.formulaCreator.makeVariable(integerType,i); } + protected ApronNode makeNumberImpl(long i) { + return new ApronIntCstNode(BigInteger.valueOf(i)); + } + + @Override + protected ApronNode makeNumberImpl(BigInteger i) { + return new ApronIntCstNode(i); + } + + @Override + protected ApronNode makeNumberImpl(String i) { + return null; + } + + @Override + protected ApronNode negate(ApronNode pParam1) { + ApronIntUnaryNode unaryNode = new ApronIntUnaryNode(pParam1, + Texpr1UnNode.OP_NEG); + return unaryNode; + } + + @Override + protected ApronNode add(ApronNode pParam1, ApronNode pParam2) { + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1,pParam2, + Texpr1BinNode.OP_ADD); + return binaryNode; + } + + @Override + protected ApronNode sumImpl(List operands) { + return null; + } + + @Override + protected ApronNode subtract(ApronNode pParam1, ApronNode pParam2) { + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1,pParam2, + Texpr1BinNode.OP_SUB); + return binaryNode; + } + @Override + protected ApronNode divide(ApronNode pParam1, ApronNode pParam2) { + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1,pParam2, + Texpr1BinNode.OP_DIV); + return binaryNode; + } + + @Override + protected ApronNode multiply(ApronNode pParam1, ApronNode pParam2) { + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1,pParam2, + Texpr1BinNode.OP_MUL); + return binaryNode; + } + + @Override + protected ApronNode equal(ApronNode pParam1, ApronNode pParam2) { + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1,pParam2, + Texpr1BinNode.OP_SUB); + ApronConstraint constraint = new ApronConstraint(Tcons1.EQ,formulaCreator.getEnvironment(), + binaryNode.getNode()); + return constraint; + } + + @Override + protected ApronNode distinctImpl(List pNumbers) { + return null; + } + + @Override + protected ApronNode greaterThan(ApronNode pParam1, ApronNode pParam2) { + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1,pParam2, + Texpr1BinNode.OP_SUB); + ApronConstraint constraint = new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(), + binaryNode.getNode()); + return constraint; + } + + @Override + protected ApronNode greaterOrEquals(ApronNode pParam1, ApronNode pParam2) { + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1,pParam2, + Texpr1BinNode.OP_SUB); + ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ,formulaCreator.getEnvironment(), + binaryNode.getNode()); + return constraint; + } + + @Override + protected ApronNode lessThan(ApronNode pParam1, ApronNode pParam2) { + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam2,pParam1, + Texpr1BinNode.OP_SUB); + ApronConstraint constraint = new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(), + binaryNode.getNode()); + return constraint; + } + + @Override + protected ApronNode lessOrEquals(ApronNode pParam1, ApronNode pParam2) { + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam2,pParam1, + Texpr1BinNode.OP_SUB); + ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ,formulaCreator.getEnvironment(), + binaryNode.getNode()); + return constraint; + } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java index 7d13da7bc1..9e59d58d1b 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java @@ -34,9 +34,8 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronBinaryNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronUnaryNode; + abstract class ApronNumeralFormulaManager< ParamFormulaType extends NumeralFormula, ResultFormulaType extends NumeralFormula> @@ -60,111 +59,4 @@ protected boolean isNumeral(ApronNode val) { protected abstract FormulaType getNumeralType(); - @Override - protected ApronNode makeNumberImpl(long i) { - // return new ApronCoeff(new MpqScalar(BigInteger.valueOf(i)), i, FormulaType.INTEGER); - return null; - } - - @Override - protected ApronNode makeNumberImpl(BigInteger i) { - //return new ApronCoeff(new MpqScalar(i),i,FormulaType.INTEGER); - return null; - } - - @Override - protected ApronNode makeNumberImpl(String i) { - // return new ApronCoeff(new MpqScalar(Integer.parseInt(i)), Integer.parseInt(i), FormulaType - // .INTEGER); - return null; - } - - @Override - protected ApronNode negate(ApronNode pParam1) { - ApronUnaryNode unaryNode = new ApronUnaryNode(pParam1.getType(),pParam1, Texpr1UnNode.OP_NEG); - return unaryNode; - } - - @Override - protected ApronNode add(ApronNode pParam1, ApronNode pParam2) { - ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(),pParam1,pParam2, - Texpr1BinNode.OP_ADD); - return binaryNode; - } - - @Override - protected ApronNode sumImpl(List operands) { - return null; - } - - @Override - protected ApronNode subtract(ApronNode pParam1, ApronNode pParam2) { - ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(), pParam1,pParam2, - Texpr1BinNode.OP_SUB); - return binaryNode; - } - - @Override - protected ApronNode divide(ApronNode pParam1, ApronNode pParam2) { - ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(), pParam1,pParam2, - Texpr1BinNode.OP_DIV); - return binaryNode; - } - - @Override - protected ApronNode multiply(ApronNode pParam1, ApronNode pParam2) { - ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(), pParam1,pParam2, - Texpr1BinNode.OP_MUL); - return binaryNode; - } - - @Override - protected ApronNode equal(ApronNode pParam1, ApronNode pParam2) { - ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(), pParam1,pParam2, - Texpr1BinNode.OP_SUB); - ApronConstraint constraint = new ApronConstraint(Tcons1.EQ,formulaCreator.getEnvironment(), - binaryNode.getNode()); - return constraint; - } - - @Override - protected ApronNode distinctImpl(List pNumbers) { - return null; - } - - @Override - protected ApronNode greaterThan(ApronNode pParam1, ApronNode pParam2) { - ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(), pParam1,pParam2, - Texpr1BinNode.OP_SUB); - ApronConstraint constraint = new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(), - binaryNode.getNode()); - return constraint; - } - - @Override - protected ApronNode greaterOrEquals(ApronNode pParam1, ApronNode pParam2) { - ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(), pParam1,pParam2, - Texpr1BinNode.OP_SUB); - ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ,formulaCreator.getEnvironment(), - binaryNode.getNode()); - return constraint; - } - - @Override - protected ApronNode lessThan(ApronNode pParam1, ApronNode pParam2) { - ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(), pParam2,pParam1, - Texpr1BinNode.OP_SUB); - ApronConstraint constraint = new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(), - binaryNode.getNode()); - return constraint; - } - - @Override - protected ApronNode lessOrEquals(ApronNode pParam1, ApronNode pParam2) { - ApronBinaryNode binaryNode = new ApronBinaryNode(pParam1.getType(), pParam2,pParam1, - Texpr1BinNode.OP_SUB); - ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ,formulaCreator.getEnvironment(), - binaryNode.getNode()); - return constraint; - } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java index 4634b42b01..7082780d18 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java @@ -20,7 +20,12 @@ package org.sosy_lab.java_smt.solvers.apron; +import apron.Tcons1; +import apron.Texpr1BinNode; +import apron.Texpr1UnNode; import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.List; import org.sosy_lab.java_smt.api.NumeralFormula; import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula; import org.sosy_lab.java_smt.api.RationalFormulaManager; @@ -28,6 +33,10 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronRationalType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatBinaryNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatCstNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatUnaryNode; public class ApronRationalFormulaManager extends ApronNumeralFormulaManager @@ -62,5 +71,108 @@ protected ApronNode makeNumberImpl(BigDecimal pNumber) { protected ApronNode makeVariableImpl(String i) { return formulaCreator.makeVariable(rationalType,i); } + protected ApronNode makeNumberImpl(long i) { + return new ApronRatCstNode(BigInteger.valueOf(i), BigInteger.ONE); + } + + @Override + protected ApronNode makeNumberImpl(BigInteger i) { + return new ApronRatCstNode(i,BigInteger.ONE); + } + + @Override + protected ApronNode makeNumberImpl(String i) { + return null; + } + + @Override + protected ApronNode negate(ApronNode pParam1) { + ApronRatUnaryNode unaryNode = new ApronRatUnaryNode(pParam1, + Texpr1UnNode.OP_NEG); + return unaryNode; + } + + @Override + protected ApronNode add(ApronNode pParam1, ApronNode pParam2) { + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1,pParam2, + Texpr1BinNode.OP_ADD); + return binaryNode; + } + + @Override + protected ApronNode sumImpl(List operands) { + return null; + } + + @Override + protected ApronNode subtract(ApronNode pParam1, ApronNode pParam2) { + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1,pParam2, + Texpr1BinNode.OP_SUB); + return binaryNode; + } + + @Override + protected ApronNode divide(ApronNode pParam1, ApronNode pParam2) { + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1,pParam2, + Texpr1BinNode.OP_DIV); + return binaryNode; + } + + @Override + protected ApronNode multiply(ApronNode pParam1, ApronNode pParam2) { + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1,pParam2, + Texpr1BinNode.OP_MUL); + return binaryNode; + } + + @Override + protected ApronNode equal(ApronNode pParam1, ApronNode pParam2) { + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1,pParam2, + Texpr1BinNode.OP_SUB); + ApronConstraint constraint = new ApronConstraint(Tcons1.EQ,formulaCreator.getEnvironment(), + binaryNode.getNode()); + return constraint; + } + + @Override + protected ApronNode distinctImpl(List pNumbers) { + return null; + } + + @Override + protected ApronNode greaterThan(ApronNode pParam1, ApronNode pParam2) { + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1,pParam2, + Texpr1BinNode.OP_SUB); + ApronConstraint constraint = new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(), + binaryNode.getNode()); + return constraint; + } + + @Override + protected ApronNode greaterOrEquals(ApronNode pParam1, ApronNode pParam2) { + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1,pParam2, + Texpr1BinNode.OP_SUB); + ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ,formulaCreator.getEnvironment(), + binaryNode.getNode()); + return constraint; + } + + @Override + protected ApronNode lessThan(ApronNode pParam1, ApronNode pParam2) { + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam2,pParam1, + Texpr1BinNode.OP_SUB); + ApronConstraint constraint = new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(), + binaryNode.getNode()); + return constraint; + } + + @Override + protected ApronNode lessOrEquals(ApronNode pParam1, ApronNode pParam2) { + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam2,pParam1, + Texpr1BinNode.OP_SUB); + ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ,formulaCreator.getEnvironment(), + binaryNode.getNode()); + return constraint; + } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java index 8ac3d091e0..b61cc3fa10 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java @@ -35,6 +35,9 @@ import java.math.BigInteger; import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.Formula; +import org.sosy_lab.java_smt.api.NumeralFormula; +import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula; +import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; public interface ApronNode extends Formula { @@ -42,13 +45,12 @@ public interface ApronNode extends Formula { FormulaType getType(); Texpr1Node getNode(); - class ApronCstNode implements ApronNode { + class ApronRatCstNode implements ApronNode, RationalFormula { - private FormulaType type; - private Texpr1CstNode cstNode; + private final FormulaType type = FormulaType.RATIONAL; + private final Texpr1CstNode cstNode; - public ApronCstNode(FormulaType pType, BigInteger pNumerator, BigInteger pDenominator){ - this.type = pType; + public ApronRatCstNode(BigInteger pNumerator, BigInteger pDenominator){ this.cstNode = new Texpr1CstNode(new MpqScalar(pNumerator.divide(pDenominator))); } @@ -62,13 +64,12 @@ public Texpr1CstNode getNode() { } } - class ApronVarNode implements ApronNode{ + class ApronRatVarNode implements ApronNode, RationalFormula{ - private FormulaType type; - private Texpr1VarNode varNode; + private final FormulaType type = FormulaType.RATIONAL; + private final Texpr1VarNode varNode; - public ApronVarNode(FormulaType pType, String pVarName){ - this.type = pType; + public ApronRatVarNode(String pVarName){ this.varNode = new Texpr1VarNode(pVarName); } @Override @@ -81,12 +82,11 @@ public Texpr1VarNode getNode() { } } - class ApronUnaryNode implements ApronNode{ - private FormulaType type; - private Texpr1UnNode unaryNode; + class ApronRatUnaryNode implements ApronNode, RationalFormula { + private final FormulaType type = FormulaType.RATIONAL; + private final Texpr1UnNode unaryNode; - public ApronUnaryNode(FormulaType pType, ApronNode param,int op ){ - this.type = pType; + public ApronRatUnaryNode(ApronNode param,int op ){ this.unaryNode = new Texpr1UnNode(op,param.getNode()); } @Override @@ -99,13 +99,86 @@ public Texpr1UnNode getNode(){ } } - class ApronBinaryNode implements ApronNode{ + class ApronRatBinaryNode implements ApronNode, RationalFormula{ - private FormulaType type; + private FormulaType type = FormulaType.RATIONAL; private Texpr1BinNode binaryNode; - public ApronBinaryNode(FormulaType pType, ApronNode param1, ApronNode param2, int op){ - this.type = pType; + public ApronRatBinaryNode(ApronNode param1, ApronNode param2, int op){ + this.binaryNode = new Texpr1BinNode(op,param1.getNode(),param2.getNode()); + } + @Override + public FormulaType getType() { + return this.type; + } + + @Override + public Texpr1Node getNode() { + return this.binaryNode; + } + } + + class ApronIntCstNode implements ApronNode, IntegerFormula { + + private final FormulaType type = FormulaType.INTEGER; + private final Texpr1CstNode cstNode; + + public ApronIntCstNode(BigInteger pNumerator){ + this.cstNode = new Texpr1CstNode(new MpqScalar(pNumerator)); + } + + @Override + public FormulaType getType() { + return this.type; + } + + public Texpr1CstNode getNode() { + return cstNode; + } + } + + class ApronIntVarNode implements ApronNode, IntegerFormula{ + + private final FormulaType type = FormulaType.INTEGER; + private final Texpr1VarNode varNode; + + public ApronIntVarNode(String pVarName){ + this.varNode = new Texpr1VarNode(pVarName); + } + @Override + public FormulaType getType() { + return this.type; + } + + public Texpr1VarNode getNode() { + return varNode; + } + + } + + class ApronIntUnaryNode implements ApronNode, IntegerFormula { + private final FormulaType type = FormulaType.INTEGER; + private final Texpr1UnNode unaryNode; + + public ApronIntUnaryNode(ApronNode param,int op ){ + this.unaryNode = new Texpr1UnNode(op,param.getNode()); + } + @Override + public FormulaType getType() { + return null; + } + + public Texpr1UnNode getNode(){ + return this.unaryNode; + } + } + + class ApronIntBinaryNode implements ApronNode, IntegerFormula{ + + private final FormulaType type = FormulaType.INTEGER; + private final Texpr1BinNode binaryNode; + + public ApronIntBinaryNode(ApronNode param1, ApronNode param2, int op){ this.binaryNode = new Texpr1BinNode(op,param1.getNode(),param2.getNode()); } @Override From b6d6af3123185ded94a328ee4010b4a6191655dd Mon Sep 17 00:00:00 2001 From: winnieros Date: Mon, 14 Aug 2023 16:59:38 +0200 Subject: [PATCH 010/133] Small changes here and there --- .../apron/ApronBooleanFormulaManager.java | 28 +++++++++++-------- .../solvers/apron/ApronFormulaCreator.java | 10 +++++-- .../java_smt/solvers/apron/ApronModel.java | 15 +++++----- .../solvers/apron/ApronTheoremProver.java | 28 +++++++++++++++---- .../solvers/apron/types/ApronNode.java | 4 +++ 5 files changed, 59 insertions(+), 26 deletions(-) diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java index 26d4589e27..acec5821b3 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java @@ -20,10 +20,14 @@ package org.sosy_lab.java_smt.solvers.apron; +import apron.Environment; import org.sosy_lab.java_smt.basicimpl.AbstractBooleanFormulaManager; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; -public class ApronBooleanFormulaManager extends AbstractBooleanFormulaManager { +public class ApronBooleanFormulaManager extends AbstractBooleanFormulaManager { private ApronFormulaCreator formulaCreator; protected ApronBooleanFormulaManager(ApronFormulaCreator pCreator) { @@ -32,52 +36,52 @@ protected ApronBooleanFormulaManager(ApronFormulaCreator pCreator) { } @Override - protected Object makeVariableImpl(String pVar) { - return null; + protected ApronNode makeVariableImpl(String pVar) { + throw new UnsupportedOperationException("Apron supports only numeral variables."); } @Override - protected Object makeBooleanImpl(boolean value) { + protected ApronNode makeBooleanImpl(boolean value) { return null; } @Override - protected Object not(Object pParam1) { + protected ApronNode not(ApronNode pParam1) { return null; } @Override - protected Object and(Object pParam1, Object pParam2) { + protected ApronNode and(ApronNode pParam1, ApronNode pParam2) { return null; } @Override - protected Object or(Object pParam1, Object pParam2) { + protected ApronNode or(ApronNode pParam1, ApronNode pParam2) { return null; } @Override - protected Object xor(Object pParam1, Object pParam2) { + protected ApronNode xor(ApronNode pParam1, ApronNode pParam2) { return null; } @Override - protected Object equivalence(Object bits1, Object bits2) { + protected ApronNode equivalence(ApronNode bits1, ApronNode bits2) { return null; } @Override - protected boolean isTrue(Object bits) { + protected boolean isTrue(ApronNode bits) { return false; } @Override - protected boolean isFalse(Object bits) { + protected boolean isFalse(ApronNode bits) { return false; } @Override - protected Object ifThenElse(Object cond, Object f1, Object f2) { + protected ApronNode ifThenElse(ApronNode cond, ApronNode f1, ApronNode f2) { return null; } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java index ecaa7120ee..c988b92bae 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java @@ -20,12 +20,18 @@ package org.sosy_lab.java_smt.solvers.apron; +import static org.sosy_lab.java_smt.solvers.yices2.Yices2NativeApi.yices_bvtype_size; +import static org.sosy_lab.java_smt.solvers.yices2.Yices2NativeApi.yices_type_of_term; + import apron.Environment; import com.google.common.base.Preconditions; import java.util.List; import javax.annotation.Nullable; +import org.sosy_lab.java_smt.api.BitvectorFormula; import org.sosy_lab.java_smt.api.Formula; +import org.sosy_lab.java_smt.api.FormulaType.ArrayFormulaType; import org.sosy_lab.java_smt.api.FormulaType.FloatingPointType; +import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula; import org.sosy_lab.java_smt.api.visitors.FormulaVisitor; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; @@ -64,7 +70,7 @@ public ApronFormulaType getBitvectorType(int bitwidth) { @Override public ApronFormulaType getFloatingPointType(FloatingPointType type) { - return null; + throw new UnsupportedOperationException("Apron does not support floating point operations."); } @Override @@ -91,7 +97,7 @@ public ApronNode makeVariable(ApronFormulaType pApronFormulaType, String varName } @Override - public org.sosy_lab.java_smt.api.FormulaType getFormulaType(ApronNode formula) { + public org.sosy_lab.java_smt.api.FormulaType getFormulaType(T pFormula) { return null; } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java index 96878c180d..2848185c13 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java @@ -20,16 +20,18 @@ package org.sosy_lab.java_smt.solvers.apron; +import apron.Environment; import com.google.common.collect.ImmutableList; -import javax.annotation.Nullable; +import org.checkerframework.checker.nullness.qual.Nullable; import org.sosy_lab.java_smt.basicimpl.AbstractModel; import org.sosy_lab.java_smt.basicimpl.AbstractProver; -import org.sosy_lab.java_smt.basicimpl.FormulaCreator; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; -public class ApronModel extends AbstractModel { +public class ApronModel extends AbstractModel { protected ApronModel( - AbstractProver prover, - FormulaCreator creator) { + AbstractProver prover, + ApronFormulaCreator creator) { super(prover, creator); } @@ -38,9 +40,8 @@ public ImmutableList asList() { return null; } - @Nullable @Override - protected Object evalImpl(Object formula) { + protected @Nullable ApronNode evalImpl(ApronNode formula) { return null; } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java index ff01731aa3..48eafaf222 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java @@ -22,6 +22,7 @@ import apron.Abstract1; import apron.ApronException; +import apron.Tcons1; import java.util.Collection; import java.util.List; import java.util.Optional; @@ -36,6 +37,7 @@ import org.sosy_lab.java_smt.api.ProverEnvironment; import org.sosy_lab.java_smt.api.SolverException; import org.sosy_lab.java_smt.basicimpl.AbstractProverWithAllSat; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; import org.sosy_lab.java_smt.utils.SolverUtils; public class ApronTheoremProver extends AbstractProverWithAllSat implements ProverEnvironment { @@ -58,12 +60,30 @@ public void pop() { } - @Nullable @Override - public @org.checkerframework.checker.nullness.qual.Nullable Void addConstraint(BooleanFormula constraint) throws InterruptedException { + public @Nullable Void addConstraint(BooleanFormula constraint) + throws InterruptedException{ + ApronConstraint apronConstraint = (ApronConstraint) constraint; + addConstraintException(apronConstraint); return null; } + private void addConstraintException(ApronConstraint pConstraint) { + try { + Tcons1[] consOld = abstract1.toTcons(solverContext.getManager()); + Tcons1[] newCons = new Tcons1[consOld.length+1]; + int i=0; + for(Tcons1 c : consOld){ + newCons[i] = c; + i++; + } + newCons[consOld.length] = pConstraint.getConstraintNode(); + this.abstract1 = new Abstract1(solverContext.getManager(), newCons); + } catch (ApronException e) { + throw new RuntimeException(e); + } + } + @Override public void push() throws InterruptedException { @@ -83,9 +103,7 @@ private boolean isUnsatApron(){ try { return abstract1.isBottom(solverContext.getManager()); } catch (ApronException pApronException){ - System.out.println(pApronException.toString()); - System.exit(0); - return false; + throw new RuntimeException(pApronException); } } @Override diff --git a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java index b61cc3fa10..0f3b8998c0 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java @@ -213,5 +213,9 @@ public FormulaType getType() { public Texpr1Node getNode() { return this.node; } + + public Tcons1 getConstraintNode() { + return constraintNode; + } } } From e3ac3232a36fc1b7098ca388ddaaddc3544bfbec Mon Sep 17 00:00:00 2001 From: winnieros Date: Mon, 14 Aug 2023 17:00:14 +0200 Subject: [PATCH 011/133] Small changes here and there --- .../sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java index c988b92bae..eb5d31b087 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java @@ -97,7 +97,7 @@ public ApronNode makeVariable(ApronFormulaType pApronFormulaType, String varName } @Override - public org.sosy_lab.java_smt.api.FormulaType getFormulaType(T pFormula) { + public org.sosy_lab.java_smt.api.FormulaType getFormulaType(ApronNode formula) { return null; } From 44c13b4331268a864851155653541718eefbb122 Mon Sep 17 00:00:00 2001 From: winnieros Date: Tue, 15 Aug 2023 18:53:00 +0200 Subject: [PATCH 012/133] MakeVariable adjustment --- .../java_smt/solvers/apron/ApronExamples.java | 2 +- .../solvers/apron/ApronFormulaCreator.java | 18 +++------ .../java_smt/solvers/apron/ApronModel.java | 7 +++- .../solvers/apron/types/ApronNode.java | 40 ++++++++++++++++++- 4 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java index ecdfb1a3da..54d67cee8c 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java @@ -34,7 +34,7 @@ public class ApronExamples { private static void testBox(Manager pManager) throws ApronException { String[] intVars = {"x"}; - String[] realVars = {}; + String[] realVars = {"y"}; Environment environment = new Environment(intVars, realVars); //x <= 2 and x >= -3 diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java index eb5d31b087..0c876c66cf 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java @@ -20,18 +20,12 @@ package org.sosy_lab.java_smt.solvers.apron; -import static org.sosy_lab.java_smt.solvers.yices2.Yices2NativeApi.yices_bvtype_size; -import static org.sosy_lab.java_smt.solvers.yices2.Yices2NativeApi.yices_type_of_term; - import apron.Environment; import com.google.common.base.Preconditions; import java.util.List; import javax.annotation.Nullable; -import org.sosy_lab.java_smt.api.BitvectorFormula; import org.sosy_lab.java_smt.api.Formula; -import org.sosy_lab.java_smt.api.FormulaType.ArrayFormulaType; import org.sosy_lab.java_smt.api.FormulaType.FloatingPointType; -import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula; import org.sosy_lab.java_smt.api.visitors.FormulaVisitor; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; @@ -63,6 +57,10 @@ public Environment getEnvironment(){ return this.environment; } + public void setEnvironment(Environment pEnvironment) { + environment = pEnvironment; + } + @Override public ApronFormulaType getBitvectorType(int bitwidth) { throw new UnsupportedOperationException("Apron does not support bitvector operations."); @@ -86,13 +84,9 @@ public ApronNode makeVariable(ApronFormulaType pApronFormulaType, String varName FormulaType.RATIONAL)), "Only Integer or rational variables allowed!"); if(pApronFormulaType.getType().equals(FormulaType.INTEGER)){ - String[] intvars = new String[]{varName}; - this.environment.add(intvars,new String[]{}); - return new ApronIntVarNode(varName); + return new ApronIntVarNode(varName,this); }else { - String[] realvars = new String[]{varName}; - this.environment.add(new String[]{}, realvars); - return new ApronRatVarNode(varName); + return new ApronRatVarNode(varName, this); } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java index 2848185c13..d0eb811cdb 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java @@ -29,10 +29,13 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; public class ApronModel extends AbstractModel { + + private ApronFormulaCreator formulaCreator; protected ApronModel( - AbstractProver prover, + AbstractProver pProver, ApronFormulaCreator creator) { - super(prover, creator); + super(pProver, creator); + this.formulaCreator = creator; } @Override diff --git a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java index 0f3b8998c0..010a0c4df2 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java @@ -23,12 +23,14 @@ import apron.Environment; import apron.MpfrScalar; import apron.MpqScalar; +import apron.StringVar; import apron.Tcons1; import apron.Texpr1BinNode; import apron.Texpr1CstNode; import apron.Texpr1Node; import apron.Texpr1UnNode; import apron.Texpr1VarNode; +import apron.Var; import gmp.Mpq; import gmp.Mpz; import java.math.BigDecimal; @@ -38,6 +40,7 @@ import org.sosy_lab.java_smt.api.NumeralFormula; import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula; import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula; +import org.sosy_lab.java_smt.solvers.apron.ApronFormulaCreator; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; public interface ApronNode extends Formula { @@ -68,14 +71,30 @@ class ApronRatVarNode implements ApronNode, RationalFormula{ private final FormulaType type = FormulaType.RATIONAL; private final Texpr1VarNode varNode; + private final String varName; - public ApronRatVarNode(String pVarName){ + private ApronFormulaCreator formulaCreator; + public ApronRatVarNode(String pVarName, ApronFormulaCreator pFormulaCreator){ this.varNode = new Texpr1VarNode(pVarName); + this.formulaCreator = pFormulaCreator; + this.varName = pVarName; } @Override public FormulaType getType() { return this.type; } + private void addVarToEnv(){ + Var[] intVars = formulaCreator.getEnvironment().getIntVars(); + Var[] realVars = formulaCreator.getEnvironment().getRealVars(); + Var[] newRealVars = new Var[realVars.length+1]; + int i=0; + for(Var var : realVars){ + newRealVars[i] = var; + i++; + } + newRealVars[realVars.length] = new StringVar(this.varName); + formulaCreator.setEnvironment(new Environment(intVars, newRealVars)); + } public Texpr1VarNode getNode() { return varNode; @@ -141,9 +160,13 @@ class ApronIntVarNode implements ApronNode, IntegerFormula{ private final FormulaType type = FormulaType.INTEGER; private final Texpr1VarNode varNode; + private final String varName; + private final ApronFormulaCreator formulaCreator; - public ApronIntVarNode(String pVarName){ + public ApronIntVarNode(String pVarName, ApronFormulaCreator pFormulaCreator){ this.varNode = new Texpr1VarNode(pVarName); + this.varName =pVarName; + this.formulaCreator = pFormulaCreator; } @Override public FormulaType getType() { @@ -154,6 +177,19 @@ public Texpr1VarNode getNode() { return varNode; } + private void addVarToEnv(){ + Var[] intVars = formulaCreator.getEnvironment().getIntVars(); + Var[] realVars = formulaCreator.getEnvironment().getRealVars(); + Var[] newIntVars = new Var[intVars.length+1]; + int i=0; + for(Var var : intVars){ + newIntVars[i] = var; + i++; + } + newIntVars[realVars.length] = new StringVar(this.varName); + formulaCreator.setEnvironment(new Environment(newIntVars, realVars)); + } + } class ApronIntUnaryNode implements ApronNode, IntegerFormula { From 75bdd45650a1874c4673c82a88f92bb34b6a55c9 Mon Sep 17 00:00:00 2001 From: winnieros Date: Mon, 21 Aug 2023 13:47:38 +0200 Subject: [PATCH 013/133] Assertion stack, other small changes --- .../java_smt/solvers/apron/ApronExamples.java | 19 +++++++++++++++++++ .../java_smt/solvers/apron/ApronModel.java | 11 ++++++++--- .../solvers/apron/ApronTheoremProver.java | 19 +++++++++++++++---- .../solvers/apron/types/ApronNode.java | 5 ----- 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java index 54d67cee8c..6d3d2275e2 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java @@ -72,6 +72,25 @@ private static void testBox(Manager pManager) throws ApronException { cons4.setKind(Lincons1.EQ); Abstract1 abstract2 = new Abstract1(pManager, new Lincons1[]{cons4}); assert abstract2.isBottom(pManager); + + //Expression-Tree example, 4x+5 > 0 + Texpr1VarNode varNode = new Texpr1VarNode("x"); + Texpr1CstNode four = new Texpr1CstNode(new MpqScalar(4)); + Texpr1CstNode five = new Texpr1CstNode(new MpqScalar(5)); + Texpr1BinNode term = new Texpr1BinNode(Texpr1BinNode.OP_MUL, four, varNode); + Texpr1BinNode expr = new Texpr1BinNode(Texpr1BinNode.OP_ADD,term, five); + Tcons1 constraint = new Tcons1(environment,Tcons1.SUP,expr); + Tcons1[] tcons = new Tcons1[]{constraint}; + Abstract1 abstract13 = new Abstract1(pManager,tcons); + assert abstract13.isBottom(pManager); + + //Model example + Interval interval = abstract1.getBound(pManager, "x"); + System.out.println("Whole interval: "+interval.toString()); + Scalar lowerBound = interval.inf(); + System.out.println("Lower bound: "+lowerBound.toString()); + Scalar upperBound = interval.sup(); + System.out.println("Upper bound: "+upperBound.toString()); } public static void main(String[] args) throws ApronException { diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java index d0eb811cdb..97a823ab0b 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java @@ -21,6 +21,7 @@ package org.sosy_lab.java_smt.solvers.apron; import apron.Environment; +import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import org.checkerframework.checker.nullness.qual.Nullable; import org.sosy_lab.java_smt.basicimpl.AbstractModel; @@ -32,7 +33,7 @@ public class ApronModel extends AbstractModel pProver, + ApronTheoremProver pProver, ApronFormulaCreator creator) { super(pProver, creator); this.formulaCreator = creator; @@ -40,11 +41,15 @@ protected ApronModel( @Override public ImmutableList asList() { - return null; + Preconditions.checkState(!isClosed()); + ImmutableList.Builder out = ImmutableList.builder(); + + return out.build(); } @Override protected @Nullable ApronNode evalImpl(ApronNode formula) { - return null; + Preconditions.checkState(!isClosed()); + return formula; } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java index 48eafaf222..5849d632e4 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java @@ -23,7 +23,11 @@ import apron.Abstract1; import apron.ApronException; import apron.Tcons1; +import com.google.common.base.Preconditions; +import com.google.common.collect.Iterables; +import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedHashSet; import java.util.List; import java.util.Optional; import java.util.Set; @@ -44,6 +48,8 @@ public class ApronTheoremProver extends AbstractProverWithAllSat implement private Abstract1 abstract1; private ApronSolverContext solverContext; + + private List> assertedFormulas = new ArrayList<>(); protected ApronTheoremProver( Set pSet, BooleanFormulaManager pBmgr, @@ -53,11 +59,14 @@ protected ApronTheoremProver( this.solverContext = pApronSolverContext; this.abstract1 = new Abstract1(pApronSolverContext.getManager(), pApronSolverContext.getFormulaCreator().getEnvironment()); + this.assertedFormulas.add(new LinkedHashSet<>()); } @Override public void pop() { - + Preconditions.checkState(!closed); + Preconditions.checkState(assertedFormulas.size()>1); + assertedFormulas.remove(assertedFormulas.size()-1); } @Override @@ -79,6 +88,7 @@ private void addConstraintException(ApronConstraint pConstraint) { } newCons[consOld.length] = pConstraint.getConstraintNode(); this.abstract1 = new Abstract1(solverContext.getManager(), newCons); + Iterables.getLast(assertedFormulas).add(pConstraint); } catch (ApronException e) { throw new RuntimeException(e); } @@ -86,12 +96,13 @@ private void addConstraintException(ApronConstraint pConstraint) { @Override public void push() throws InterruptedException { - + Preconditions.checkState(!closed); + assertedFormulas.add(new LinkedHashSet<>()); } @Override public int size() { - return 0; + return assertedFormulas.size()-1; } @Override @@ -108,7 +119,7 @@ private boolean isUnsatApron(){ } @Override public Model getModel() throws SolverException { - return null; + return new ApronModel(this, solverContext.getFormulaCreator()); } @Override diff --git a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java index 010a0c4df2..a876ed8927 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java @@ -21,7 +21,6 @@ package org.sosy_lab.java_smt.solvers.apron.types; import apron.Environment; -import apron.MpfrScalar; import apron.MpqScalar; import apron.StringVar; import apron.Tcons1; @@ -31,13 +30,9 @@ import apron.Texpr1UnNode; import apron.Texpr1VarNode; import apron.Var; -import gmp.Mpq; -import gmp.Mpz; -import java.math.BigDecimal; import java.math.BigInteger; import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.Formula; -import org.sosy_lab.java_smt.api.NumeralFormula; import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula; import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula; import org.sosy_lab.java_smt.solvers.apron.ApronFormulaCreator; From e33581fc759430f89eea24c4acf6eb36fea58030 Mon Sep 17 00:00:00 2001 From: winnieros Date: Mon, 21 Aug 2023 17:08:53 +0200 Subject: [PATCH 014/133] Trying to create a Model --- .../java_smt/solvers/apron/ApronExamples.java | 3 + .../solvers/apron/ApronFormulaCreator.java | 5 + .../apron/ApronIntegerFormulaManager.java | 10 +- .../java_smt/solvers/apron/ApronModel.java | 84 +++++++++++++++- .../apron/ApronRationalFormulaManager.java | 10 +- .../solvers/apron/ApronTheoremProver.java | 17 +++- .../solvers/apron/types/ApronNode.java | 98 +++++++++++++++++-- 7 files changed, 204 insertions(+), 23 deletions(-) diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java index 6d3d2275e2..6ac9fa58ce 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java @@ -90,6 +90,9 @@ private static void testBox(Manager pManager) throws ApronException { Scalar lowerBound = interval.inf(); System.out.println("Lower bound: "+lowerBound.toString()); Scalar upperBound = interval.sup(); + String castString = upperBound.toString(); + int castInt = Integer.parseInt(castString); + System.out.println(castInt); System.out.println("Upper bound: "+upperBound.toString()); } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java index 0c876c66cf..51e537ee9b 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java @@ -53,6 +53,11 @@ protected ApronFormulaCreator( } + @Override + public Object convertValue(ApronNode pF) { + return super.convertValue(pF); + } + public Environment getEnvironment(){ return this.environment; } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java index dc7d31ce27..e0b405098b 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java @@ -150,7 +150,7 @@ protected ApronNode equal(ApronNode pParam1, ApronNode pParam2) { ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1,pParam2, Texpr1BinNode.OP_SUB); ApronConstraint constraint = new ApronConstraint(Tcons1.EQ,formulaCreator.getEnvironment(), - binaryNode.getNode()); + binaryNode); return constraint; } @@ -164,7 +164,7 @@ protected ApronNode greaterThan(ApronNode pParam1, ApronNode pParam2) { ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1,pParam2, Texpr1BinNode.OP_SUB); ApronConstraint constraint = new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(), - binaryNode.getNode()); + binaryNode); return constraint; } @@ -173,7 +173,7 @@ protected ApronNode greaterOrEquals(ApronNode pParam1, ApronNode pParam2) { ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1,pParam2, Texpr1BinNode.OP_SUB); ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ,formulaCreator.getEnvironment(), - binaryNode.getNode()); + binaryNode); return constraint; } @@ -182,7 +182,7 @@ protected ApronNode lessThan(ApronNode pParam1, ApronNode pParam2) { ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam2,pParam1, Texpr1BinNode.OP_SUB); ApronConstraint constraint = new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(), - binaryNode.getNode()); + binaryNode); return constraint; } @@ -191,7 +191,7 @@ protected ApronNode lessOrEquals(ApronNode pParam1, ApronNode pParam2) { ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam2,pParam1, Texpr1BinNode.OP_SUB); ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ,formulaCreator.getEnvironment(), - binaryNode.getNode()); + binaryNode); return constraint; } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java index 97a823ab0b..f03da0cddf 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java @@ -20,31 +20,109 @@ package org.sosy_lab.java_smt.solvers.apron; +import apron.ApronException; +import apron.DoubleScalar; import apron.Environment; +import apron.Interval; +import apron.Manager; +import apron.MpqScalar; +import apron.Scalar; +import apron.Tcons1; +import apron.Texpr1BinNode; +import apron.Var; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import java.math.BigInteger; +import java.util.Collection; import org.checkerframework.checker.nullness.qual.Nullable; +import org.sosy_lab.java_smt.api.BooleanFormula; +import org.sosy_lab.java_smt.api.Formula; import org.sosy_lab.java_smt.basicimpl.AbstractModel; import org.sosy_lab.java_smt.basicimpl.AbstractProver; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronIntBinaryNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronIntCstNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronIntVarNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatBinaryNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatCstNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatVarNode; +import scala.Int; public class ApronModel extends AbstractModel { private ApronFormulaCreator formulaCreator; + private ApronTheoremProver prover; + private ImmutableList assertedExpressions; protected ApronModel( ApronTheoremProver pProver, - ApronFormulaCreator creator) { + ApronFormulaCreator creator, + Collection pAssertedExpressions) { super(pProver, creator); this.formulaCreator = creator; + this.prover = pProver; + this.assertedExpressions = ImmutableList.copyOf(pAssertedExpressions); } @Override public ImmutableList asList() { Preconditions.checkState(!isClosed()); - ImmutableList.Builder out = ImmutableList.builder(); + return generateModel(); + } + + private ImmutableList generateModel(){ + ImmutableSet.Builder builder = ImmutableSet.builder(); + for (ApronConstraint constraint : assertedExpressions) { + for(String var : constraint.getVarNames()) + builder.add(getAssignment(constraint, var)); + } + return builder.build().asList(); + } - return out.build(); + private ValueAssignment getAssignment(ApronConstraint pFormula, String pVar) { + //needed: Formula keyFormula, Formula valueFormula, BooleanFormula formula (x = 3), + // Object value, List argumentInterpretation + ImmutableList.Builder argumentInterpretationBuilder = ImmutableList.builder(); //TODO + // : empty list! + try { + ApronConstraint constraint = pFormula; + String varName = pVar; + if (pFormula.getApronNode().getType().equals(FormulaType.INTEGER)) { + ApronNode keyFormula = new ApronIntVarNode(pVar, formulaCreator); + Manager man = this.prover.getAbstract1().getCreationManager(); + Interval interval = this.prover.getAbstract1().getBound(man, pVar); + MpqScalar value = (MpqScalar) interval.sup; + int castIntValue = Integer.parseInt(value.toString()); + ApronIntCstNode valueFormula = new ApronIntCstNode(BigInteger.valueOf(castIntValue)); + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(keyFormula,valueFormula, + Texpr1BinNode.OP_SUB); + BooleanFormula formula = new ApronConstraint(Tcons1.EQ,formulaCreator.getEnvironment(), + binaryNode); + return new ValueAssignment(keyFormula,valueFormula,formula,pVar,value, + argumentInterpretationBuilder.build()); + } else { + ApronNode keyFormula = new ApronRatVarNode(pVar, formulaCreator); + Manager man = this.prover.getAbstract1().getCreationManager(); + Interval interval = this.prover.getAbstract1().getBound(man, pVar); + Scalar value = interval.sup; + //TODO: unfortunatly it is not possible to extract nominator and denominator out of an + // Scalar; So all models show Integer Values + int castRatValue = Integer.parseInt(value.toString()); + ApronRatCstNode valueFormula = new ApronRatCstNode(BigInteger.valueOf(castRatValue), + BigInteger.ONE); + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(keyFormula,valueFormula, + Texpr1BinNode.OP_SUB); + BooleanFormula formula = new ApronConstraint(Tcons1.EQ,formulaCreator.getEnvironment(), + binaryNode); + return new ValueAssignment(keyFormula,valueFormula,formula,pVar,value, + argumentInterpretationBuilder.build()); + } + } catch (ApronException pApronException){ + throw new RuntimeException(pApronException); + } } @Override diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java index 7082780d18..6a176b693e 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java @@ -130,7 +130,7 @@ protected ApronNode equal(ApronNode pParam1, ApronNode pParam2) { ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1,pParam2, Texpr1BinNode.OP_SUB); ApronConstraint constraint = new ApronConstraint(Tcons1.EQ,formulaCreator.getEnvironment(), - binaryNode.getNode()); + binaryNode); return constraint; } @@ -144,7 +144,7 @@ protected ApronNode greaterThan(ApronNode pParam1, ApronNode pParam2) { ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1,pParam2, Texpr1BinNode.OP_SUB); ApronConstraint constraint = new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(), - binaryNode.getNode()); + binaryNode); return constraint; } @@ -153,7 +153,7 @@ protected ApronNode greaterOrEquals(ApronNode pParam1, ApronNode pParam2) { ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1,pParam2, Texpr1BinNode.OP_SUB); ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ,formulaCreator.getEnvironment(), - binaryNode.getNode()); + binaryNode); return constraint; } @@ -162,7 +162,7 @@ protected ApronNode lessThan(ApronNode pParam1, ApronNode pParam2) { ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam2,pParam1, Texpr1BinNode.OP_SUB); ApronConstraint constraint = new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(), - binaryNode.getNode()); + binaryNode); return constraint; } @@ -171,7 +171,7 @@ protected ApronNode lessOrEquals(ApronNode pParam1, ApronNode pParam2) { ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam2,pParam1, Texpr1BinNode.OP_SUB); ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ,formulaCreator.getEnvironment(), - binaryNode.getNode()); + binaryNode); return constraint; } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java index 5849d632e4..f903d00a37 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java @@ -24,7 +24,9 @@ import apron.ApronException; import apron.Tcons1; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; +import io.github.cvc5.Term; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashSet; @@ -41,6 +43,7 @@ import org.sosy_lab.java_smt.api.ProverEnvironment; import org.sosy_lab.java_smt.api.SolverException; import org.sosy_lab.java_smt.basicimpl.AbstractProverWithAllSat; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; import org.sosy_lab.java_smt.utils.SolverUtils; @@ -49,7 +52,7 @@ public class ApronTheoremProver extends AbstractProverWithAllSat implement private Abstract1 abstract1; private ApronSolverContext solverContext; - private List> assertedFormulas = new ArrayList<>(); + private List> assertedFormulas = new ArrayList<>(); protected ApronTheoremProver( Set pSet, BooleanFormulaManager pBmgr, @@ -119,7 +122,13 @@ private boolean isUnsatApron(){ } @Override public Model getModel() throws SolverException { - return new ApronModel(this, solverContext.getFormulaCreator()); + return new ApronModel(this, solverContext.getFormulaCreator(), getAssertedExpressions()); + } + + private Collection getAssertedExpressions(){ + List result = new ArrayList<>(); + assertedFormulas.forEach(result::addAll); + return result; } @Override @@ -143,4 +152,8 @@ public boolean isUnsatWithAssumptions(Collection assumptions) protected Evaluator getEvaluatorWithoutChecks() throws SolverException { return null; } + + public Abstract1 getAbstract1() { + return abstract1; + } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java index a876ed8927..7f9b547442 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java @@ -42,6 +42,7 @@ public interface ApronNode extends Formula { FormulaType getType(); Texpr1Node getNode(); + String[] getVarNames(); class ApronRatCstNode implements ApronNode, RationalFormula { @@ -60,6 +61,11 @@ public FormulaType getType() { public Texpr1CstNode getNode() { return cstNode; } + + @Override + public String[] getVarNames() { + return new String[0]; + } } class ApronRatVarNode implements ApronNode, RationalFormula{ @@ -68,7 +74,7 @@ class ApronRatVarNode implements ApronNode, RationalFormula{ private final Texpr1VarNode varNode; private final String varName; - private ApronFormulaCreator formulaCreator; + private final ApronFormulaCreator formulaCreator; public ApronRatVarNode(String pVarName, ApronFormulaCreator pFormulaCreator){ this.varNode = new Texpr1VarNode(pVarName); this.formulaCreator = pFormulaCreator; @@ -94,14 +100,21 @@ private void addVarToEnv(){ public Texpr1VarNode getNode() { return varNode; } + + @Override + public String[] getVarNames() { + return new String[]{varName}; + } } class ApronRatUnaryNode implements ApronNode, RationalFormula { private final FormulaType type = FormulaType.RATIONAL; private final Texpr1UnNode unaryNode; + private final String[] varNames; public ApronRatUnaryNode(ApronNode param,int op ){ this.unaryNode = new Texpr1UnNode(op,param.getNode()); + this.varNames = param.getVarNames(); } @Override public FormulaType getType() { @@ -111,15 +124,33 @@ public FormulaType getType() { public Texpr1UnNode getNode(){ return this.unaryNode; } + + @Override + public String[] getVarNames() { + return varNames; + } } class ApronRatBinaryNode implements ApronNode, RationalFormula{ - private FormulaType type = FormulaType.RATIONAL; - private Texpr1BinNode binaryNode; + private final FormulaType type = FormulaType.RATIONAL; + private final Texpr1BinNode binaryNode; + private final String[] varNames; public ApronRatBinaryNode(ApronNode param1, ApronNode param2, int op){ this.binaryNode = new Texpr1BinNode(op,param1.getNode(),param2.getNode()); + String[] varNames1 = param1.getVarNames(); + String[] varNames2 = param2.getVarNames(); + String[] allVarNames = new String[varNames1.length+varNames2.length]; + for(int i=0; i Date: Tue, 22 Aug 2023 12:39:58 +0200 Subject: [PATCH 015/133] Code refactored --- .../apron/ApronBooleanFormulaManager.java | 4 +- .../java_smt/solvers/apron/ApronExamples.java | 55 ++++++----- .../solvers/apron/ApronFormulaCreator.java | 26 ++--- .../solvers/apron/ApronFormulaManager.java | 13 +-- .../apron/ApronIntegerFormulaManager.java | 43 ++++---- .../java_smt/solvers/apron/ApronModel.java | 33 +++---- .../solvers/apron/ApronNativeApiTest.java | 6 +- .../apron/ApronNumeralFormulaManager.java | 10 +- .../apron/ApronRationalFormulaManager.java | 38 +++---- .../solvers/apron/ApronSolverContext.java | 84 +++++++++------- .../solvers/apron/ApronTheoremProver.java | 38 ++++--- .../solvers/apron/types/ApronFormulaType.java | 26 +++-- .../solvers/apron/types/ApronNode.java | 98 ++++++++++--------- 13 files changed, 249 insertions(+), 225 deletions(-) diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java index acec5821b3..886183d6a1 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java @@ -22,14 +22,14 @@ import apron.Environment; import org.sosy_lab.java_smt.basicimpl.AbstractBooleanFormulaManager; -import org.sosy_lab.java_smt.basicimpl.FormulaCreator; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; public class ApronBooleanFormulaManager extends AbstractBooleanFormulaManager { - private ApronFormulaCreator formulaCreator; + private final ApronFormulaCreator formulaCreator; + protected ApronBooleanFormulaManager(ApronFormulaCreator pCreator) { super(pCreator); this.formulaCreator = pCreator; diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java index 6ac9fa58ce..61869428e2 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronExamples.java @@ -20,18 +20,27 @@ package org.sosy_lab.java_smt.solvers.apron; -import java.util.Arrays; -import apron.*; -import org.junit.Assert; -import org.junit.AssumptionViolatedException; -import org.sosy_lab.common.NativeLibraries; +import apron.Abstract1; +import apron.ApronException; +import apron.Environment; +import apron.Interval; +import apron.Lincons1; +import apron.Linexpr1; +import apron.Linterm1; +import apron.Manager; +import apron.MpqScalar; +import apron.Polka; +import apron.Scalar; +import apron.Tcons1; +import apron.Texpr1BinNode; +import apron.Texpr1CstNode; +import apron.Texpr1VarNode; /** * Simple examples about the Apron Library. Inspired by * ... */ -public class ApronExamples -{ +public class ApronExamples { private static void testBox(Manager pManager) throws ApronException { String[] intVars = {"x"}; String[] realVars = {"y"}; @@ -40,30 +49,30 @@ private static void testBox(Manager pManager) throws ApronException { //x <= 2 and x >= -3 //x <= 2 --> -x+2 >= 0 Lincons1 cons1 = new Lincons1(environment); - cons1.setCoeff("x",new MpqScalar(-1)); + cons1.setCoeff("x", new MpqScalar(-1)); cons1.setCst(new MpqScalar(+2)); cons1.setKind(Lincons1.SUPEQ); //x >= - 3 --> x+3 >= 0 Lincons1 cons2 = new Lincons1(environment); - cons2.setCoeff("x",new MpqScalar(1)); + cons2.setCoeff("x", new MpqScalar(1)); cons2.setCst(new MpqScalar(+3)); cons2.setKind(Lincons1.SUPEQ); - Abstract1 abstract1 = new Abstract1(pManager, new Lincons1[]{cons1,cons2}); + Abstract1 abstract1 = new Abstract1(pManager, new Lincons1[]{cons1, cons2}); // x+x-x=0 - Linterm1 linterm1 = new Linterm1("x",new MpqScalar(1)); - Linterm1 linterm2 = new Linterm1("x",new MpqScalar(1)); - Linterm1 linterm3 = new Linterm1("x",new MpqScalar(-1)); - Linterm1[] terms = new Linterm1[]{linterm3,linterm2,linterm1}; - Linexpr1 linexpr1 = new Linexpr1(environment,terms,new MpqScalar(0)); - Lincons1 cons = new Lincons1(Lincons1.EQ,linexpr1); + Linterm1 linterm1 = new Linterm1("x", new MpqScalar(1)); + Linterm1 linterm2 = new Linterm1("x", new MpqScalar(1)); + Linterm1 linterm3 = new Linterm1("x", new MpqScalar(-1)); + Linterm1[] terms = new Linterm1[]{linterm3, linterm2, linterm1}; + Linexpr1 linexpr1 = new Linexpr1(environment, terms, new MpqScalar(0)); + Lincons1 cons = new Lincons1(Lincons1.EQ, linexpr1); //is x = 1 satisfiable? Lincons1 cons3 = new Lincons1(environment); - cons3.setCoeff("x",new MpqScalar(1)); + cons3.setCoeff("x", new MpqScalar(1)); cons3.setCst(new MpqScalar(-1)); cons3.setKind(Lincons1.EQ); - assert abstract1.satisfy(pManager,cons3); + assert abstract1.satisfy(pManager, cons3); //always unsat example, 1 = 0 Lincons1 cons4 = new Lincons1(environment); @@ -78,22 +87,18 @@ private static void testBox(Manager pManager) throws ApronException { Texpr1CstNode four = new Texpr1CstNode(new MpqScalar(4)); Texpr1CstNode five = new Texpr1CstNode(new MpqScalar(5)); Texpr1BinNode term = new Texpr1BinNode(Texpr1BinNode.OP_MUL, four, varNode); - Texpr1BinNode expr = new Texpr1BinNode(Texpr1BinNode.OP_ADD,term, five); - Tcons1 constraint = new Tcons1(environment,Tcons1.SUP,expr); + Texpr1BinNode expr = new Texpr1BinNode(Texpr1BinNode.OP_ADD, term, five); + Tcons1 constraint = new Tcons1(environment, Tcons1.SUP, expr); Tcons1[] tcons = new Tcons1[]{constraint}; - Abstract1 abstract13 = new Abstract1(pManager,tcons); + Abstract1 abstract13 = new Abstract1(pManager, tcons); assert abstract13.isBottom(pManager); //Model example Interval interval = abstract1.getBound(pManager, "x"); - System.out.println("Whole interval: "+interval.toString()); Scalar lowerBound = interval.inf(); - System.out.println("Lower bound: "+lowerBound.toString()); Scalar upperBound = interval.sup(); String castString = upperBound.toString(); int castInt = Integer.parseInt(castString); - System.out.println(castInt); - System.out.println("Upper bound: "+upperBound.toString()); } public static void main(String[] args) throws ApronException { diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java index 51e537ee9b..07ffd378c7 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java @@ -28,19 +28,20 @@ import org.sosy_lab.java_smt.api.FormulaType.FloatingPointType; import org.sosy_lab.java_smt.api.visitors.FormulaVisitor; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; -import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronBooleanType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronIntegerType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronRationalType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronIntVarNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatVarNode; -public class ApronFormulaCreator extends FormulaCreator { private Environment environment; + protected ApronFormulaCreator( Environment pO, ApronBooleanType boolType, @@ -58,7 +59,7 @@ public Object convertValue(ApronNode pF) { return super.convertValue(pF); } - public Environment getEnvironment(){ + public Environment getEnvironment() { return this.environment; } @@ -83,16 +84,17 @@ public ApronFormulaType getArrayType(ApronFormulaType indexType, ApronFormulaTyp @Override public ApronNode makeVariable(ApronFormulaType pApronFormulaType, String varName) { - Preconditions.checkArgument(!environment.hasVar(varName),"Variablename already exists!"); + Preconditions.checkArgument(!environment.hasVar(varName), "Variablename already exists!"); Preconditions.checkArgument( - (pApronFormulaType.getType().equals(FormulaType.INTEGER) || pApronFormulaType.getType().equals( - FormulaType.RATIONAL)), + (pApronFormulaType.getType().equals(FormulaType.INTEGER) || pApronFormulaType.getType() + .equals( + FormulaType.RATIONAL)), "Only Integer or rational variables allowed!"); - if(pApronFormulaType.getType().equals(FormulaType.INTEGER)){ - return new ApronIntVarNode(varName,this); - }else { - return new ApronRatVarNode(varName, this); - } + if (pApronFormulaType.getType().equals(FormulaType.INTEGER)) { + return new ApronIntVarNode(varName, this); + } else { + return new ApronRatVarNode(varName, this); + } } @Override @@ -109,7 +111,7 @@ public R visit(FormulaVisitor visitor, Formula formula, ApronNode f) { // @Override public ApronNode callFunctionImpl(Long declaration, List args) { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException(); } @Override diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java index c634f1665e..54ba9573c4 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java @@ -37,8 +37,8 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; -public class ApronFormulaManager extends AbstractFormulaManager { +public class ApronFormulaManager extends AbstractFormulaManager { /** * Builds a solver from the given theory implementations. * @@ -68,8 +68,8 @@ protected ApronFormulaManager( @Nullable AbstractSLFormulaManager slManager, @Nullable AbstractStringFormulaManager strManager, @Nullable AbstractEnumerationFormulaManager enumManager) { - super(pFormulaCreator,functionManager,booleanManager,pIntegerFormulaManager, - pRationalFormulaManager,null,null,null,null,null,null,null); + super(pFormulaCreator, functionManager, booleanManager, pIntegerFormulaManager, + pRationalFormulaManager, null, null, null, null, null, null, null); } @Override @@ -83,8 +83,9 @@ public Appender dumpFormula(ApronNode t) { } @Override - public T substitute(T f, - Map fromToMapping) { + public T substitute( + T f, + Map fromToMapping) { return null; } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java index e0b405098b..a421f00d76 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java @@ -38,11 +38,13 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronIntCstNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronIntUnaryNode; -public class ApronIntegerFormulaManager extends ApronNumeralFormulaManager implements - IntegerFormulaManager { +public class ApronIntegerFormulaManager + extends ApronNumeralFormulaManager implements + IntegerFormulaManager { + + private final ApronFormulaType integerType = new ApronIntegerType(); + private final ApronFormulaCreator formulaCreator; - private ApronFormulaType integerType = new ApronIntegerType(); - private ApronFormulaCreator formulaCreator; protected ApronIntegerFormulaManager( ApronFormulaCreator pCreator, NonLinearArithmetic pNonLinearArithmetic) { @@ -81,7 +83,7 @@ public BooleanFormula modularCongruence(IntegerFormula number1, IntegerFormula n public IntegerFormula modulo(IntegerFormula numerator, IntegerFormula denumerator) { ApronNode node1 = (ApronNode) numerator; ApronNode node2 = (ApronNode) denumerator; - ApronIntBinaryNode result = new ApronIntBinaryNode(node1, node2, + ApronIntBinaryNode result = new ApronIntBinaryNode(node1, node2, Texpr1BinNode.OP_MOD); return result; } @@ -89,8 +91,9 @@ public IntegerFormula modulo(IntegerFormula numerator, IntegerFormula denumerato @Override protected ApronNode makeVariableImpl(String i) { - return this.formulaCreator.makeVariable(integerType,i); + return this.formulaCreator.makeVariable(integerType, i); } + protected ApronNode makeNumberImpl(long i) { return new ApronIntCstNode(BigInteger.valueOf(i)); } @@ -114,7 +117,7 @@ protected ApronNode negate(ApronNode pParam1) { @Override protected ApronNode add(ApronNode pParam1, ApronNode pParam2) { - ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1,pParam2, + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1, pParam2, Texpr1BinNode.OP_ADD); return binaryNode; } @@ -126,30 +129,30 @@ protected ApronNode sumImpl(List operands) { @Override protected ApronNode subtract(ApronNode pParam1, ApronNode pParam2) { - ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1,pParam2, + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1, pParam2, Texpr1BinNode.OP_SUB); return binaryNode; } @Override protected ApronNode divide(ApronNode pParam1, ApronNode pParam2) { - ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1,pParam2, + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1, pParam2, Texpr1BinNode.OP_DIV); return binaryNode; } @Override protected ApronNode multiply(ApronNode pParam1, ApronNode pParam2) { - ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1,pParam2, + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1, pParam2, Texpr1BinNode.OP_MUL); return binaryNode; } @Override protected ApronNode equal(ApronNode pParam1, ApronNode pParam2) { - ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1,pParam2, + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1, pParam2, Texpr1BinNode.OP_SUB); - ApronConstraint constraint = new ApronConstraint(Tcons1.EQ,formulaCreator.getEnvironment(), + ApronConstraint constraint = new ApronConstraint(Tcons1.EQ, formulaCreator.getEnvironment(), binaryNode); return constraint; } @@ -161,36 +164,36 @@ protected ApronNode distinctImpl(List pNumbers) { @Override protected ApronNode greaterThan(ApronNode pParam1, ApronNode pParam2) { - ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1,pParam2, + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1, pParam2, Texpr1BinNode.OP_SUB); - ApronConstraint constraint = new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(), + ApronConstraint constraint = new ApronConstraint(Tcons1.SUP, formulaCreator.getEnvironment(), binaryNode); return constraint; } @Override protected ApronNode greaterOrEquals(ApronNode pParam1, ApronNode pParam2) { - ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1,pParam2, + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam1, pParam2, Texpr1BinNode.OP_SUB); - ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ,formulaCreator.getEnvironment(), + ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ, formulaCreator.getEnvironment(), binaryNode); return constraint; } @Override protected ApronNode lessThan(ApronNode pParam1, ApronNode pParam2) { - ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam2,pParam1, + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam2, pParam1, Texpr1BinNode.OP_SUB); - ApronConstraint constraint = new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(), + ApronConstraint constraint = new ApronConstraint(Tcons1.SUP, formulaCreator.getEnvironment(), binaryNode); return constraint; } @Override protected ApronNode lessOrEquals(ApronNode pParam1, ApronNode pParam2) { - ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam2,pParam1, + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(pParam2, pParam1, Texpr1BinNode.OP_SUB); - ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ,formulaCreator.getEnvironment(), + ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ, formulaCreator.getEnvironment(), binaryNode); return constraint; } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java index f03da0cddf..d255c48739 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java @@ -21,7 +21,6 @@ package org.sosy_lab.java_smt.solvers.apron; import apron.ApronException; -import apron.DoubleScalar; import apron.Environment; import apron.Interval; import apron.Manager; @@ -29,7 +28,6 @@ import apron.Scalar; import apron.Tcons1; import apron.Texpr1BinNode; -import apron.Var; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; @@ -37,9 +35,7 @@ import java.util.Collection; import org.checkerframework.checker.nullness.qual.Nullable; import org.sosy_lab.java_smt.api.BooleanFormula; -import org.sosy_lab.java_smt.api.Formula; import org.sosy_lab.java_smt.basicimpl.AbstractModel; -import org.sosy_lab.java_smt.basicimpl.AbstractProver; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; @@ -50,13 +46,13 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatBinaryNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatCstNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatVarNode; -import scala.Int; public class ApronModel extends AbstractModel { - private ApronFormulaCreator formulaCreator; - private ApronTheoremProver prover; - private ImmutableList assertedExpressions; + private final ApronFormulaCreator formulaCreator; + private final ApronTheoremProver prover; + private final ImmutableList assertedExpressions; + protected ApronModel( ApronTheoremProver pProver, ApronFormulaCreator creator, @@ -73,11 +69,12 @@ public ImmutableList asList() { return generateModel(); } - private ImmutableList generateModel(){ + private ImmutableList generateModel() { ImmutableSet.Builder builder = ImmutableSet.builder(); for (ApronConstraint constraint : assertedExpressions) { - for(String var : constraint.getVarNames()) - builder.add(getAssignment(constraint, var)); + for (String var : constraint.getVarNames()) { + builder.add(getAssignment(constraint, var)); + } } return builder.build().asList(); } @@ -97,11 +94,11 @@ private ValueAssignment getAssignment(ApronConstraint pFormula, String pVar) { MpqScalar value = (MpqScalar) interval.sup; int castIntValue = Integer.parseInt(value.toString()); ApronIntCstNode valueFormula = new ApronIntCstNode(BigInteger.valueOf(castIntValue)); - ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(keyFormula,valueFormula, + ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(keyFormula, valueFormula, Texpr1BinNode.OP_SUB); - BooleanFormula formula = new ApronConstraint(Tcons1.EQ,formulaCreator.getEnvironment(), + BooleanFormula formula = new ApronConstraint(Tcons1.EQ, formulaCreator.getEnvironment(), binaryNode); - return new ValueAssignment(keyFormula,valueFormula,formula,pVar,value, + return new ValueAssignment(keyFormula, valueFormula, formula, pVar, value, argumentInterpretationBuilder.build()); } else { ApronNode keyFormula = new ApronRatVarNode(pVar, formulaCreator); @@ -113,14 +110,14 @@ private ValueAssignment getAssignment(ApronConstraint pFormula, String pVar) { int castRatValue = Integer.parseInt(value.toString()); ApronRatCstNode valueFormula = new ApronRatCstNode(BigInteger.valueOf(castRatValue), BigInteger.ONE); - ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(keyFormula,valueFormula, + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(keyFormula, valueFormula, Texpr1BinNode.OP_SUB); - BooleanFormula formula = new ApronConstraint(Tcons1.EQ,formulaCreator.getEnvironment(), + BooleanFormula formula = new ApronConstraint(Tcons1.EQ, formulaCreator.getEnvironment(), binaryNode); - return new ValueAssignment(keyFormula,valueFormula,formula,pVar,value, + return new ValueAssignment(keyFormula, valueFormula, formula, pVar, value, argumentInterpretationBuilder.build()); } - } catch (ApronException pApronException){ + } catch (ApronException pApronException) { throw new RuntimeException(pApronException); } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronNativeApiTest.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronNativeApiTest.java index 3ced6b4332..8924d0769b 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronNativeApiTest.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronNativeApiTest.java @@ -28,16 +28,16 @@ public class ApronNativeApiTest { @BeforeClass - public static void load(){ + public static void load() { try { NativeLibraries.loadLibrary("apron"); System.out.println("Apron-Library is loaded."); - } catch (UnsatisfiedLinkError e){ + } catch (UnsatisfiedLinkError e) { throw new AssumptionViolatedException("Apron not availible: ", e); } } @Test - public void solverBackendTest(){ + public void solverBackendTest() { } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java index 9e59d58d1b..b0150185b4 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java @@ -21,20 +21,12 @@ package org.sosy_lab.java_smt.solvers.apron; import apron.Environment; -import apron.Tcons1; -import apron.Texpr1BinNode; -import apron.Texpr1Node; -import apron.Texpr1UnNode; -import java.math.BigInteger; -import java.util.HashSet; -import java.util.List; import org.sosy_lab.java_smt.api.NumeralFormula; import org.sosy_lab.java_smt.basicimpl.AbstractNumeralFormulaManager; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; abstract class ApronNumeralFormulaManager< @@ -42,7 +34,7 @@ abstract class ApronNumeralFormulaManager< extends AbstractNumeralFormulaManager< ApronNode, ApronFormulaType, Environment, ParamFormulaType, ResultFormulaType, Long> { - private ApronFormulaCreator formulaCreator; + private final ApronFormulaCreator formulaCreator; protected ApronNumeralFormulaManager( FormulaCreator pCreator, diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java index 6a176b693e..7d83a253aa 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java @@ -42,8 +42,8 @@ public class ApronRationalFormulaManager extends ApronNumeralFormulaManager implements RationalFormulaManager { - private ApronFormulaCreator formulaCreator; - private ApronFormulaType rationalType = new ApronRationalType(); + private final ApronFormulaCreator formulaCreator; + private final ApronFormulaType rationalType = new ApronRationalType(); protected ApronRationalFormulaManager( ApronFormulaCreator pFormulaCreator, @@ -51,6 +51,7 @@ protected ApronRationalFormulaManager( super(pFormulaCreator, pNonLinearArithmetic); this.formulaCreator = pFormulaCreator; } + @Override protected FormulaType getNumeralType() { return FormulaType.RATIONAL; @@ -69,15 +70,16 @@ protected ApronNode makeNumberImpl(BigDecimal pNumber) { @Override protected ApronNode makeVariableImpl(String i) { - return formulaCreator.makeVariable(rationalType,i); + return formulaCreator.makeVariable(rationalType, i); } + protected ApronNode makeNumberImpl(long i) { return new ApronRatCstNode(BigInteger.valueOf(i), BigInteger.ONE); } @Override protected ApronNode makeNumberImpl(BigInteger i) { - return new ApronRatCstNode(i,BigInteger.ONE); + return new ApronRatCstNode(i, BigInteger.ONE); } @Override @@ -94,7 +96,7 @@ protected ApronNode negate(ApronNode pParam1) { @Override protected ApronNode add(ApronNode pParam1, ApronNode pParam2) { - ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1,pParam2, + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1, pParam2, Texpr1BinNode.OP_ADD); return binaryNode; } @@ -106,30 +108,30 @@ protected ApronNode sumImpl(List operands) { @Override protected ApronNode subtract(ApronNode pParam1, ApronNode pParam2) { - ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1,pParam2, + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1, pParam2, Texpr1BinNode.OP_SUB); return binaryNode; } @Override protected ApronNode divide(ApronNode pParam1, ApronNode pParam2) { - ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1,pParam2, + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1, pParam2, Texpr1BinNode.OP_DIV); return binaryNode; } @Override protected ApronNode multiply(ApronNode pParam1, ApronNode pParam2) { - ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1,pParam2, + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1, pParam2, Texpr1BinNode.OP_MUL); return binaryNode; } @Override protected ApronNode equal(ApronNode pParam1, ApronNode pParam2) { - ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1,pParam2, + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1, pParam2, Texpr1BinNode.OP_SUB); - ApronConstraint constraint = new ApronConstraint(Tcons1.EQ,formulaCreator.getEnvironment(), + ApronConstraint constraint = new ApronConstraint(Tcons1.EQ, formulaCreator.getEnvironment(), binaryNode); return constraint; } @@ -141,36 +143,36 @@ protected ApronNode distinctImpl(List pNumbers) { @Override protected ApronNode greaterThan(ApronNode pParam1, ApronNode pParam2) { - ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1,pParam2, + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1, pParam2, Texpr1BinNode.OP_SUB); - ApronConstraint constraint = new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(), + ApronConstraint constraint = new ApronConstraint(Tcons1.SUP, formulaCreator.getEnvironment(), binaryNode); return constraint; } @Override protected ApronNode greaterOrEquals(ApronNode pParam1, ApronNode pParam2) { - ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1,pParam2, + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam1, pParam2, Texpr1BinNode.OP_SUB); - ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ,formulaCreator.getEnvironment(), + ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ, formulaCreator.getEnvironment(), binaryNode); return constraint; } @Override protected ApronNode lessThan(ApronNode pParam1, ApronNode pParam2) { - ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam2,pParam1, + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam2, pParam1, Texpr1BinNode.OP_SUB); - ApronConstraint constraint = new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(), + ApronConstraint constraint = new ApronConstraint(Tcons1.SUP, formulaCreator.getEnvironment(), binaryNode); return constraint; } @Override protected ApronNode lessOrEquals(ApronNode pParam1, ApronNode pParam2) { - ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam2,pParam1, + ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(pParam2, pParam1, Texpr1BinNode.OP_SUB); - ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ,formulaCreator.getEnvironment(), + ApronConstraint constraint = new ApronConstraint(Tcons1.SUPEQ, formulaCreator.getEnvironment(), binaryNode); return constraint; } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java index 3a763ce2e3..6386f28655 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java @@ -39,28 +39,31 @@ import org.sosy_lab.java_smt.basicimpl.AbstractNumeralFormulaManager.NonLinearArithmetic; import org.sosy_lab.java_smt.basicimpl.AbstractSolverContext; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronBooleanType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronIntegerType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronRationalType; public class ApronSolverContext extends AbstractSolverContext { - private Manager manager; private final ApronFormulaCreator formulaCreator; - private ShutdownNotifier shutdownNotifier; - private Configuration config; - private @Nullable PathCounterTemplate logfile; - private LogManager logger; - private long randomSeed; - - private ShutdownRequestListener shutdownRequestListener; + private final Manager manager; + private final ShutdownNotifier shutdownNotifier; + private final Configuration config; + private final @Nullable PathCounterTemplate logfile; + private final LogManager logger; + private final long randomSeed; + + private final ShutdownRequestListener shutdownRequestListener; private boolean closed = false; - protected ApronSolverContext(ApronFormulaManager fmgr, - Manager pManager, - ApronFormulaCreator pFormulaCreator, - ShutdownNotifier pShutdownNotifier, - Configuration pConfig, - PathCounterTemplate pLogfile, - LogManager pLogger, - long pRandomSeed) { + protected ApronSolverContext( + ApronFormulaManager fmgr, + Manager pManager, + ApronFormulaCreator pFormulaCreator, + ShutdownNotifier pShutdownNotifier, + Configuration pConfig, + PathCounterTemplate pLogfile, + LogManager pLogger, + long pRandomSeed) { super(fmgr); this.manager = pManager; this.formulaCreator = pFormulaCreator; @@ -75,17 +78,23 @@ protected ApronSolverContext(ApronFormulaManager fmgr, this.logger = pLogger; } - public static synchronized ApronSolverContext create(NonLinearArithmetic pNonLinearArithmetic, - Configuration pConfiguration, - ShutdownNotifier pShutdownNotifier, - PathCounterTemplate logfile, - LogManager pLogger, - long randomSeed){ + public static synchronized ApronSolverContext create( + NonLinearArithmetic pNonLinearArithmetic, + Configuration pConfiguration, + ShutdownNotifier pShutdownNotifier, + PathCounterTemplate logfile, + LogManager pLogger, + long randomSeed) { Environment env = new Environment(); Manager manager = new Box(); ApronBooleanType booleanType = new ApronBooleanType(); - ApronFormulaCreator formulaCreator = new ApronFormulaCreator(env, booleanType, null,null,null,null); + ApronIntegerType integerType = new ApronIntegerType(); + ApronRationalType rationalType = new ApronRationalType(); + ApronFormulaCreator formulaCreator = + new ApronFormulaCreator(env, booleanType, integerType, rationalType, + null, + null); ApronUFManager ufManager = new ApronUFManager(formulaCreator); ApronBooleanFormulaManager booleanFormulaManager = new ApronBooleanFormulaManager(formulaCreator); @@ -94,19 +103,21 @@ public static synchronized ApronSolverContext create(NonLinearArithmetic pNonLin ApronRationalFormulaManager rationalFormulaManager = new ApronRationalFormulaManager(formulaCreator, pNonLinearArithmetic); ApronFormulaManager fmgr = new ApronFormulaManager(formulaCreator, ufManager, - booleanFormulaManager,integerFormulaManager,rationalFormulaManager,null,null,null,null, - null,null,null); - return new ApronSolverContext(fmgr, manager, formulaCreator, pShutdownNotifier,pConfiguration - ,logfile,pLogger,randomSeed); + booleanFormulaManager, integerFormulaManager, rationalFormulaManager, null, null, null, + null, + null, null, null); + return new ApronSolverContext(fmgr, manager, formulaCreator, pShutdownNotifier, pConfiguration + , logfile, pLogger, randomSeed); } - public Manager getManager(){ + public Manager getManager() { return this.manager; } - public ApronFormulaCreator getFormulaCreator(){ + public ApronFormulaCreator getFormulaCreator() { return this.formulaCreator; } + @Override public String getVersion() { return this.manager.getVersion(); @@ -120,7 +131,7 @@ public Solvers getSolverName() { @Override public void close() { //TODO was muss hier noch passieren? - if(!closed){ + if (!closed) { closed = true; logger.log(Level.FINER, "Freeing Apron Environment"); shutdownNotifier.unregister(shutdownRequestListener); @@ -132,13 +143,14 @@ protected ProverEnvironment newProverEnvironment0(Set options) { return newApronProverEnvironment(options); } - private ProverEnvironment newApronProverEnvironment(Set pProverOptions){ - try{ + private ProverEnvironment newApronProverEnvironment(Set pProverOptions) { + try { ApronBooleanFormulaManager booleanFormulaManager = new ApronBooleanFormulaManager(this.formulaCreator); - return new ApronTheoremProver(pProverOptions,booleanFormulaManager,this.shutdownNotifier,this); - } catch(ApronException pApronException){ - System.out.println(pApronException.toString()); + return new ApronTheoremProver(pProverOptions, booleanFormulaManager, this.shutdownNotifier, + this); + } catch (ApronException pApronException) { + System.out.println(pApronException); System.exit(0); return null; } @@ -146,7 +158,7 @@ private ProverEnvironment newApronProverEnvironment(Set pProverOp @Override protected InterpolatingProverEnvironment newProverEnvironmentWithInterpolation0(Set pSet) { - return null; + throw new UnsupportedOperationException(); } @Override diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java index f903d00a37..b5aead7eb7 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java @@ -24,9 +24,7 @@ import apron.ApronException; import apron.Tcons1; import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; -import io.github.cvc5.Term; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashSet; @@ -34,7 +32,6 @@ import java.util.Optional; import java.util.Set; import javax.annotation.Nullable; -import org.checkerframework.checker.units.qual.A; import org.sosy_lab.common.ShutdownNotifier; import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.BooleanFormulaManager; @@ -43,23 +40,23 @@ import org.sosy_lab.java_smt.api.ProverEnvironment; import org.sosy_lab.java_smt.api.SolverException; import org.sosy_lab.java_smt.basicimpl.AbstractProverWithAllSat; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; -import org.sosy_lab.java_smt.utils.SolverUtils; -public class ApronTheoremProver extends AbstractProverWithAllSat implements ProverEnvironment { +public class ApronTheoremProver extends AbstractProverWithAllSat + implements ProverEnvironment { private Abstract1 abstract1; - private ApronSolverContext solverContext; + private final ApronSolverContext solverContext; + + private final List> assertedFormulas = new ArrayList<>(); - private List> assertedFormulas = new ArrayList<>(); protected ApronTheoremProver( Set pSet, BooleanFormulaManager pBmgr, ShutdownNotifier pShutdownNotifier, ApronSolverContext pApronSolverContext) throws ApronException { super(pSet, pBmgr, pShutdownNotifier); - this.solverContext = pApronSolverContext; + this.solverContext = pApronSolverContext; this.abstract1 = new Abstract1(pApronSolverContext.getManager(), pApronSolverContext.getFormulaCreator().getEnvironment()); this.assertedFormulas.add(new LinkedHashSet<>()); @@ -68,13 +65,13 @@ protected ApronTheoremProver( @Override public void pop() { Preconditions.checkState(!closed); - Preconditions.checkState(assertedFormulas.size()>1); - assertedFormulas.remove(assertedFormulas.size()-1); + Preconditions.checkState(assertedFormulas.size() > 1); + assertedFormulas.remove(assertedFormulas.size() - 1); } @Override public @Nullable Void addConstraint(BooleanFormula constraint) - throws InterruptedException{ + throws InterruptedException { ApronConstraint apronConstraint = (ApronConstraint) constraint; addConstraintException(apronConstraint); return null; @@ -83,9 +80,9 @@ public void pop() { private void addConstraintException(ApronConstraint pConstraint) { try { Tcons1[] consOld = abstract1.toTcons(solverContext.getManager()); - Tcons1[] newCons = new Tcons1[consOld.length+1]; - int i=0; - for(Tcons1 c : consOld){ + Tcons1[] newCons = new Tcons1[consOld.length + 1]; + int i = 0; + for (Tcons1 c : consOld) { newCons[i] = c; i++; } @@ -105,27 +102,28 @@ public void push() throws InterruptedException { @Override public int size() { - return assertedFormulas.size()-1; + return assertedFormulas.size() - 1; } @Override - public boolean isUnsat() throws SolverException, InterruptedException{ + public boolean isUnsat() throws SolverException, InterruptedException { return isUnsatApron(); } - private boolean isUnsatApron(){ + private boolean isUnsatApron() { try { return abstract1.isBottom(solverContext.getManager()); - } catch (ApronException pApronException){ + } catch (ApronException pApronException) { throw new RuntimeException(pApronException); } } + @Override public Model getModel() throws SolverException { return new ApronModel(this, solverContext.getFormulaCreator(), getAssertedExpressions()); } - private Collection getAssertedExpressions(){ + private Collection getAssertedExpressions() { List result = new ArrayList<>(); assertedFormulas.forEach(result::addAll); return result; diff --git a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulaType.java b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulaType.java index c177360cbd..fa94998cbe 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulaType.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronFormulaType.java @@ -22,38 +22,44 @@ public interface ApronFormulaType { + FormulaType getType(); + enum FormulaType { BOOLEAN, INTEGER, RATIONAL } - FormulaType getType(); - class ApronIntegerType implements ApronFormulaType{ + class ApronIntegerType implements ApronFormulaType { + + public ApronIntegerType() { + } - public ApronIntegerType(){} @Override - public FormulaType getType(){ + public FormulaType getType() { return FormulaType.INTEGER; } } - class ApronRationalType implements ApronFormulaType{ + class ApronRationalType implements ApronFormulaType { + + public ApronRationalType() { + } - public ApronRationalType(){} @Override - public FormulaType getType(){ + public FormulaType getType() { return FormulaType.RATIONAL; } } - class ApronBooleanType implements ApronFormulaType{ + class ApronBooleanType implements ApronFormulaType { - public ApronBooleanType(){ + public ApronBooleanType() { } + @Override - public FormulaType getType(){ + public FormulaType getType() { return FormulaType.BOOLEAN; } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java index 7f9b547442..f2b01a19a5 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java @@ -41,7 +41,9 @@ public interface ApronNode extends Formula { FormulaType getType(); + Texpr1Node getNode(); + String[] getVarNames(); class ApronRatCstNode implements ApronNode, RationalFormula { @@ -49,7 +51,7 @@ class ApronRatCstNode implements ApronNode, RationalFormula { private final FormulaType type = FormulaType.RATIONAL; private final Texpr1CstNode cstNode; - public ApronRatCstNode(BigInteger pNumerator, BigInteger pDenominator){ + public ApronRatCstNode(BigInteger pNumerator, BigInteger pDenominator) { this.cstNode = new Texpr1CstNode(new MpqScalar(pNumerator.divide(pDenominator))); } @@ -68,28 +70,31 @@ public String[] getVarNames() { } } - class ApronRatVarNode implements ApronNode, RationalFormula{ + class ApronRatVarNode implements ApronNode, RationalFormula { private final FormulaType type = FormulaType.RATIONAL; private final Texpr1VarNode varNode; private final String varName; private final ApronFormulaCreator formulaCreator; - public ApronRatVarNode(String pVarName, ApronFormulaCreator pFormulaCreator){ + + public ApronRatVarNode(String pVarName, ApronFormulaCreator pFormulaCreator) { this.varNode = new Texpr1VarNode(pVarName); this.formulaCreator = pFormulaCreator; this.varName = pVarName; } + @Override public FormulaType getType() { return this.type; } - private void addVarToEnv(){ + + private void addVarToEnv() { Var[] intVars = formulaCreator.getEnvironment().getIntVars(); Var[] realVars = formulaCreator.getEnvironment().getRealVars(); - Var[] newRealVars = new Var[realVars.length+1]; - int i=0; - for(Var var : realVars){ + Var[] newRealVars = new Var[realVars.length + 1]; + int i = 0; + for (Var var : realVars) { newRealVars[i] = var; i++; } @@ -112,16 +117,17 @@ class ApronRatUnaryNode implements ApronNode, RationalFormula { private final Texpr1UnNode unaryNode; private final String[] varNames; - public ApronRatUnaryNode(ApronNode param,int op ){ - this.unaryNode = new Texpr1UnNode(op,param.getNode()); + public ApronRatUnaryNode(ApronNode param, int op) { + this.unaryNode = new Texpr1UnNode(op, param.getNode()); this.varNames = param.getVarNames(); } + @Override public FormulaType getType() { return null; } - public Texpr1UnNode getNode(){ + public Texpr1UnNode getNode() { return this.unaryNode; } @@ -131,27 +137,26 @@ public String[] getVarNames() { } } - class ApronRatBinaryNode implements ApronNode, RationalFormula{ + class ApronRatBinaryNode implements ApronNode, RationalFormula { private final FormulaType type = FormulaType.RATIONAL; private final Texpr1BinNode binaryNode; private final String[] varNames; - public ApronRatBinaryNode(ApronNode param1, ApronNode param2, int op){ - this.binaryNode = new Texpr1BinNode(op,param1.getNode(),param2.getNode()); + public ApronRatBinaryNode(ApronNode param1, ApronNode param2, int op) { + this.binaryNode = new Texpr1BinNode(op, param1.getNode(), param2.getNode()); String[] varNames1 = param1.getVarNames(); String[] varNames2 = param2.getVarNames(); - String[] allVarNames = new String[varNames1.length+varNames2.length]; - for(int i=0; i Date: Wed, 23 Aug 2023 16:24:25 +0200 Subject: [PATCH 016/133] mostly debugging --- .../apron/ApronBooleanFormulaManager.java | 32 +- .../solvers/apron/ApronFormulaCreator.java | 66 +- .../solvers/apron/ApronFormulaManager.java | 12 + .../apron/ApronIntegerFormulaManager.java | 17 +- .../java_smt/solvers/apron/ApronModel.java | 28 +- .../apron/ApronNumeralFormulaManager.java | 1 + .../apron/ApronRationalFormulaManager.java | 23 +- .../solvers/apron/ApronSolverContext.java | 3 +- .../solvers/apron/ApronTheoremProver.java | 19 +- .../solvers/apron/types/ApronNode.java | 642 ++++++++++++------ 10 files changed, 596 insertions(+), 247 deletions(-) diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java index 886183d6a1..b1bf772dd9 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java @@ -21,9 +21,13 @@ package org.sosy_lab.java_smt.solvers.apron; import apron.Environment; +import apron.Tcons1; +import java.math.BigInteger; import org.sosy_lab.java_smt.basicimpl.AbstractBooleanFormulaManager; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntCstNode; public class ApronBooleanFormulaManager extends AbstractBooleanFormulaManager { @@ -39,49 +43,57 @@ protected ApronBooleanFormulaManager(ApronFormulaCreator pCreator) { protected ApronNode makeVariableImpl(String pVar) { throw new UnsupportedOperationException("Apron supports only numeral variables."); } - +/** + * Not directly implementable, true and false is represented by a constraint that is + * always true (1!=0) or alway false (1==0) + */ @Override protected ApronNode makeBooleanImpl(boolean value) { - return null; + ApronIntCstNode cst = new ApronIntCstNode(BigInteger.valueOf(1)); + if(value){ + return new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(),cst); //1!=0 --> true + } else { + return new ApronConstraint(Tcons1.EQ,formulaCreator.getEnvironment(),cst);//1==0 --> false + } } @Override protected ApronNode not(ApronNode pParam1) { - return null; + throw new UnsupportedOperationException("Apron does not support boolean operations."); } @Override protected ApronNode and(ApronNode pParam1, ApronNode pParam2) { - return null; + throw new UnsupportedOperationException("Apron does not support boolean operations."); } @Override protected ApronNode or(ApronNode pParam1, ApronNode pParam2) { - return null; + throw new UnsupportedOperationException("Apron does not support boolean operations."); } @Override protected ApronNode xor(ApronNode pParam1, ApronNode pParam2) { - return null; + throw new UnsupportedOperationException("Apron does not support boolean operations."); } @Override protected ApronNode equivalence(ApronNode bits1, ApronNode bits2) { - return null; + throw new UnsupportedOperationException("Apron does not support boolean operations."); } @Override protected boolean isTrue(ApronNode bits) { - return false; + throw new UnsupportedOperationException("Apron does not support boolean operations."); } @Override protected boolean isFalse(ApronNode bits) { - return false; + throw new UnsupportedOperationException("Apron does not support boolean operations."); } @Override protected ApronNode ifThenElse(ApronNode cond, ApronNode f1, ApronNode f2) { - return null; + throw new UnsupportedOperationException("Apron does not support boolean operations."); } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java index 07ffd378c7..2b01f8f6d8 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java @@ -22,8 +22,11 @@ import apron.Environment; import com.google.common.base.Preconditions; +import java.util.HashMap; import java.util.List; +import java.util.Map; import javax.annotation.Nullable; +import org.sosy_lab.common.rationals.Rational; import org.sosy_lab.java_smt.api.Formula; import org.sosy_lab.java_smt.api.FormulaType.FloatingPointType; import org.sosy_lab.java_smt.api.visitors.FormulaVisitor; @@ -34,14 +37,22 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronRationalType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronIntVarNode; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatVarNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntCstNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntVarNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatCstNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatVarNode; + public class ApronFormulaCreator extends FormulaCreator { private Environment environment; + /** + * @variables is a map that stores all variable-objects with their name as key; + */ + private Map variables; + protected ApronFormulaCreator( Environment pO, ApronBooleanType boolType, @@ -51,12 +62,20 @@ protected ApronFormulaCreator( @Nullable Long regexType) { super(pO, boolType, pIntegerType, pRationalType, null, null); this.environment = pO; - + this.variables = new HashMap<>(); } @Override - public Object convertValue(ApronNode pF) { - return super.convertValue(pF); + public Object convertValue(ApronNode pf1) { + FormulaType type = pf1.getType(); + if(pf1 instanceof ApronIntCstNode){ + ApronIntCstNode cst = (ApronIntCstNode) pf1; + return cst.getValue(); + } else if (pf1 instanceof ApronRatCstNode) { + ApronRatCstNode cast = (ApronRatCstNode) pf1; + Rational rational = Rational.of(cast.getNumerator(),cast.getDenominator()); + } + return null; } public Environment getEnvironment() { @@ -82,6 +101,13 @@ public ApronFormulaType getArrayType(ApronFormulaType indexType, ApronFormulaTyp throw new UnsupportedOperationException("Apron does not support array operations."); } + /** + * For making a Formula (Type ApronNode) for a variable it is important to also update the + * @environment as it holds all variables; + * @param pApronFormulaType Integer or Rational? + * @param varName name of the variable + * @return object of either ApronIntVarNode (Type Integer) or ApronRatVarNode (Type Rational) + */ @Override public ApronNode makeVariable(ApronFormulaType pApronFormulaType, String varName) { Preconditions.checkArgument(!environment.hasVar(varName), "Variablename already exists!"); @@ -91,21 +117,35 @@ public ApronNode makeVariable(ApronFormulaType pApronFormulaType, String varName FormulaType.RATIONAL)), "Only Integer or rational variables allowed!"); if (pApronFormulaType.getType().equals(FormulaType.INTEGER)) { - return new ApronIntVarNode(varName, this); + ApronIntVarNode varNode = new ApronIntVarNode(varName, this); + variables.put(varName, varNode); + return varNode; } else { - return new ApronRatVarNode(varName, this); + ApronRatVarNode varNode = new ApronRatVarNode(varName, this); + variables.put(varName, varNode); + return varNode; } } + public Map getVariables() { + return variables; + } + @Override public org.sosy_lab.java_smt.api.FormulaType getFormulaType(ApronNode formula) { - return null; + FormulaType type = formula.getType(); + if(type.equals(FormulaType.BOOLEAN)){ + return org.sosy_lab.java_smt.api.FormulaType.BooleanType; + } else if (type.equals(FormulaType.RATIONAL)) { + return org.sosy_lab.java_smt.api.FormulaType.RationalType; + } else if (type.equals(FormulaType.INTEGER)) { + return org.sosy_lab.java_smt.api.FormulaType.IntegerType; + } + throw new IllegalArgumentException("Type %type not available!"); } @Override - public R visit(FormulaVisitor visitor, Formula formula, ApronNode f) { //hinten - // anstellen, Frage kann man eine formel in alle kleinteile zerlegen und dann wieder - // zusammenbauen? + public R visit(FormulaVisitor visitor, Formula formula, ApronNode f) { return null; } @@ -119,11 +159,11 @@ public Long declareUFImpl( String pName, ApronFormulaType pReturnType, List pArgTypes) { - throw new UnsupportedOperationException("Apron does not support uninterpreted functions."); + return null; } @Override - protected Long getBooleanVarDeclarationImpl(ApronNode pApronFormula) { //brauche ich nicht + protected Long getBooleanVarDeclarationImpl(ApronNode pApronFormula) { return null; } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java index 54ba9573c4..cda020f55c 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java @@ -21,11 +21,14 @@ package org.sosy_lab.java_smt.solvers.apron; import apron.Environment; +import apron.Tcons1; +import java.math.BigInteger; import java.util.Map; import org.checkerframework.checker.nullness.qual.Nullable; import org.sosy_lab.common.Appender; import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.Formula; +import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula; import org.sosy_lab.java_smt.basicimpl.AbstractArrayFormulaManager; import org.sosy_lab.java_smt.basicimpl.AbstractBitvectorFormulaManager; import org.sosy_lab.java_smt.basicimpl.AbstractEnumerationFormulaManager; @@ -35,7 +38,13 @@ import org.sosy_lab.java_smt.basicimpl.AbstractSLFormulaManager; import org.sosy_lab.java_smt.basicimpl.AbstractStringFormulaManager; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronIntegerType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntCstNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntVarNode; +import scala.Int; public class ApronFormulaManager extends AbstractFormulaManager { @@ -89,4 +98,7 @@ public T substitute( return null; } + public static ApronNode getTerm(Formula pFormula){ + return ((ApronNode) pFormula).getInstance(); + } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java index a421f00d76..cecbdce515 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java @@ -26,6 +26,7 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.util.List; +import org.checkerframework.checker.units.qual.A; import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.IntegerFormulaManager; import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula; @@ -34,9 +35,9 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronIntBinaryNode; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronIntCstNode; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronIntUnaryNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntBinaryNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntCstNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntUnaryNode; public class ApronIntegerFormulaManager extends ApronNumeralFormulaManager implements @@ -105,7 +106,7 @@ protected ApronNode makeNumberImpl(BigInteger i) { @Override protected ApronNode makeNumberImpl(String i) { - return null; + return new ApronIntCstNode(BigInteger.valueOf(Integer.parseInt(i))); } @Override @@ -124,6 +125,14 @@ protected ApronNode add(ApronNode pParam1, ApronNode pParam2) { @Override protected ApronNode sumImpl(List operands) { + if(!operands.isEmpty()){ + ApronNode first = operands.remove(0); + for (ApronNode operand:operands) { + first = new ApronIntBinaryNode(first, operand, + Texpr1BinNode.OP_ADD); + } + return first; + } return null; } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java index d255c48739..1015e14574 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java @@ -33,6 +33,7 @@ import com.google.common.collect.ImmutableSet; import java.math.BigInteger; import java.util.Collection; +import java.util.Objects; import org.checkerframework.checker.nullness.qual.Nullable; import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.basicimpl.AbstractModel; @@ -40,18 +41,19 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronIntBinaryNode; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronIntCstNode; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronIntVarNode; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatBinaryNode; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatCstNode; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatVarNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntBinaryNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntCstNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntVarNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatBinaryNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatCstNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatVarNode; public class ApronModel extends AbstractModel { private final ApronFormulaCreator formulaCreator; private final ApronTheoremProver prover; private final ImmutableList assertedExpressions; + private final ImmutableList model; protected ApronModel( ApronTheoremProver pProver, @@ -61,6 +63,7 @@ protected ApronModel( this.formulaCreator = creator; this.prover = pProver; this.assertedExpressions = ImmutableList.copyOf(pAssertedExpressions); + this.model = generateModel(); } @Override @@ -83,12 +86,13 @@ private ValueAssignment getAssignment(ApronConstraint pFormula, String pVar) { //needed: Formula keyFormula, Formula valueFormula, BooleanFormula formula (x = 3), // Object value, List argumentInterpretation ImmutableList.Builder argumentInterpretationBuilder = ImmutableList.builder(); //TODO + //argumentInterpretationBuilder.add(); // : empty list! try { ApronConstraint constraint = pFormula; String varName = pVar; if (pFormula.getApronNode().getType().equals(FormulaType.INTEGER)) { - ApronNode keyFormula = new ApronIntVarNode(pVar, formulaCreator); + ApronNode keyFormula = formulaCreator.getVariables().get(varName); Manager man = this.prover.getAbstract1().getCreationManager(); Interval interval = this.prover.getAbstract1().getBound(man, pVar); MpqScalar value = (MpqScalar) interval.sup; @@ -97,14 +101,14 @@ private ValueAssignment getAssignment(ApronConstraint pFormula, String pVar) { ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(keyFormula, valueFormula, Texpr1BinNode.OP_SUB); BooleanFormula formula = new ApronConstraint(Tcons1.EQ, formulaCreator.getEnvironment(), - binaryNode); - return new ValueAssignment(keyFormula, valueFormula, formula, pVar, value, + binaryNode); //is the representation x=0, if 0 is the model for x + return new ValueAssignment(keyFormula, valueFormula, formula, pVar, castIntValue, argumentInterpretationBuilder.build()); } else { - ApronNode keyFormula = new ApronRatVarNode(pVar, formulaCreator); + ApronNode keyFormula = formulaCreator.getVariables().get(varName); Manager man = this.prover.getAbstract1().getCreationManager(); Interval interval = this.prover.getAbstract1().getBound(man, pVar); - Scalar value = interval.sup; + Object value = interval.sup; //TODO: unfortunatly it is not possible to extract nominator and denominator out of an // Scalar; So all models show Integer Values int castRatValue = Integer.parseInt(value.toString()); @@ -114,7 +118,7 @@ private ValueAssignment getAssignment(ApronConstraint pFormula, String pVar) { Texpr1BinNode.OP_SUB); BooleanFormula formula = new ApronConstraint(Tcons1.EQ, formulaCreator.getEnvironment(), binaryNode); - return new ValueAssignment(keyFormula, valueFormula, formula, pVar, value, + return new ValueAssignment(keyFormula, valueFormula, formula, pVar, castRatValue, argumentInterpretationBuilder.build()); } } catch (ApronException pApronException) { diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java index b0150185b4..ef91973c91 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronNumeralFormulaManager.java @@ -21,6 +21,7 @@ package org.sosy_lab.java_smt.solvers.apron; import apron.Environment; +import java.util.Map; import org.sosy_lab.java_smt.api.NumeralFormula; import org.sosy_lab.java_smt.basicimpl.AbstractNumeralFormulaManager; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java index 7d83a253aa..9e44e84efe 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java @@ -25,7 +25,9 @@ import apron.Texpr1UnNode; import java.math.BigDecimal; import java.math.BigInteger; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.sosy_lab.java_smt.api.NumeralFormula; import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula; import org.sosy_lab.java_smt.api.RationalFormulaManager; @@ -34,9 +36,11 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatBinaryNode; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatCstNode; -import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronRatUnaryNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntBinaryNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatBinaryNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatCstNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatUnaryNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatVarNode; public class ApronRationalFormulaManager extends ApronNumeralFormulaManager @@ -84,7 +88,7 @@ protected ApronNode makeNumberImpl(BigInteger i) { @Override protected ApronNode makeNumberImpl(String i) { - return null; + return new ApronRatCstNode(BigInteger.valueOf(Integer.parseInt(i)), BigInteger.ONE); } @Override @@ -103,8 +107,15 @@ protected ApronNode add(ApronNode pParam1, ApronNode pParam2) { @Override protected ApronNode sumImpl(List operands) { - return null; - } + if(!operands.isEmpty()){ + ApronNode first = operands.remove(0); + for (ApronNode operand:operands) { + first = new ApronRatBinaryNode(first, operand, + Texpr1BinNode.OP_ADD); + } + return first; + } + return null; } @Override protected ApronNode subtract(ApronNode pParam1, ApronNode pParam2) { diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java index 6386f28655..e984f8f6e6 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java @@ -24,6 +24,7 @@ import apron.Box; import apron.Environment; import apron.Manager; +import apron.Polka; import java.util.Set; import java.util.logging.Level; import javax.annotation.Nullable; @@ -87,7 +88,7 @@ public static synchronized ApronSolverContext create( long randomSeed) { Environment env = new Environment(); - Manager manager = new Box(); + Manager manager = new Polka(true); ApronBooleanType booleanType = new ApronBooleanType(); ApronIntegerType integerType = new ApronIntegerType(); ApronRationalType rationalType = new ApronRationalType(); diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java index b5aead7eb7..1e7021f1a2 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java @@ -41,6 +41,8 @@ import org.sosy_lab.java_smt.api.SolverException; import org.sosy_lab.java_smt.basicimpl.AbstractProverWithAllSat; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; + public class ApronTheoremProver extends AbstractProverWithAllSat implements ProverEnvironment { @@ -72,8 +74,15 @@ public void pop() { @Override public @Nullable Void addConstraint(BooleanFormula constraint) throws InterruptedException { - ApronConstraint apronConstraint = (ApronConstraint) constraint; - addConstraintException(apronConstraint); + /*ApronNode node = ApronFormulaManager.getTerm(constraint, + this.solverContext.getFormulaCreator().getEnvironment(), + this.solverContext.getFormulaCreator()); + if(node instanceof ApronConstraint){ + addConstraintException((ApronConstraint) node); + } else { + throw new IllegalArgumentException("Constraint of wrong Type!"); + }*/ + return null; } @@ -131,17 +140,17 @@ private Collection getAssertedExpressions() { @Override public List getUnsatCore() { - return null; + throw new UnsupportedOperationException("Unsatcore not supported."); } @Override - public Optional> unsatCoreOverAssumptions(Collection assumptions) + public Optional> unsatCoreOverAssumptions(Collection assumptions) throws SolverException, InterruptedException { return Optional.empty(); } @Override - public boolean isUnsatWithAssumptions(Collection assumptions) + public boolean isUnsatWithAssumptions(Collection assumptions) throws SolverException, InterruptedException { return false; } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java index f2b01a19a5..df97cf8959 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java @@ -33,10 +33,12 @@ import java.math.BigInteger; import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.Formula; +import org.sosy_lab.java_smt.api.NumeralFormula; import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula; import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula; import org.sosy_lab.java_smt.solvers.apron.ApronFormulaCreator; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatCstNode; public interface ApronNode extends Formula { @@ -45,261 +47,484 @@ public interface ApronNode extends Formula { Texpr1Node getNode(); String[] getVarNames(); + ApronNode getInstance(); + interface ApronNumeralNode extends ApronNode, NumeralFormula{ + class ApronRatCstNode + implements RationalFormula, ApronNumeralNode { + + private final FormulaType type = FormulaType.RATIONAL; + private final Texpr1CstNode cstNode; + private final BigInteger numerator; + private final BigInteger denominator; + + public ApronRatCstNode(BigInteger pNumerator, BigInteger pDenominator) { + this.cstNode = new Texpr1CstNode(new MpqScalar(pNumerator.divide(pDenominator))); + this.numerator = pNumerator; + this.denominator = pDenominator; + } - class ApronRatCstNode implements ApronNode, RationalFormula { + @Override + public FormulaType getType() { + return this.type; + } - private final FormulaType type = FormulaType.RATIONAL; - private final Texpr1CstNode cstNode; + public Texpr1CstNode getNode() { + return cstNode; + } - public ApronRatCstNode(BigInteger pNumerator, BigInteger pDenominator) { - this.cstNode = new Texpr1CstNode(new MpqScalar(pNumerator.divide(pDenominator))); - } + @Override + public String[] getVarNames() { + return new String[0]; + } - @Override - public FormulaType getType() { - return this.type; - } + public BigInteger getDenominator() { + return denominator; + } - public Texpr1CstNode getNode() { - return cstNode; - } + public BigInteger getNumerator() { + return numerator; + } - @Override - public String[] getVarNames() { - return new String[0]; + @Override + public ApronNode getInstance() { + return this; + } + + @Override + public String toString() { + return cstNode.toString(); + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + if (!(other instanceof ApronRatCstNode)) { + return false; + } + return this == ((ApronRatCstNode) other).getInstance(); + } + + @Override + public int hashCode() { + return this.cstNode.hashCode(); + } } - } - class ApronRatVarNode implements ApronNode, RationalFormula { + class ApronRatVarNode implements RationalFormula, ApronNode { - private final FormulaType type = FormulaType.RATIONAL; - private final Texpr1VarNode varNode; - private final String varName; + private final FormulaType type = FormulaType.RATIONAL; + private final Texpr1VarNode varNode; + private final String varName; - private final ApronFormulaCreator formulaCreator; + private final ApronFormulaCreator formulaCreator; - public ApronRatVarNode(String pVarName, ApronFormulaCreator pFormulaCreator) { - this.varNode = new Texpr1VarNode(pVarName); - this.formulaCreator = pFormulaCreator; - this.varName = pVarName; - } + public ApronRatVarNode(String pVarName, ApronFormulaCreator pFormulaCreator) { + this.varNode = new Texpr1VarNode(pVarName); + this.formulaCreator = pFormulaCreator; + this.varName = pVarName; + addVarToEnv(); + } + @Override + public String toString() { + return varNode.toString(); + } - @Override - public FormulaType getType() { - return this.type; - } + @Override + public boolean equals(Object other) { - private void addVarToEnv() { - Var[] intVars = formulaCreator.getEnvironment().getIntVars(); - Var[] realVars = formulaCreator.getEnvironment().getRealVars(); - Var[] newRealVars = new Var[realVars.length + 1]; - int i = 0; - for (Var var : realVars) { - newRealVars[i] = var; - i++; - } - newRealVars[realVars.length] = new StringVar(this.varName); - formulaCreator.setEnvironment(new Environment(intVars, newRealVars)); - } + if (other == this) { + return true; + } + if (!(other instanceof ApronRatVarNode)) { + return false; + } + return this == ((ApronRatVarNode) other).getInstance(); + } - public Texpr1VarNode getNode() { - return varNode; - } + @Override + public int hashCode() { + return this.varNode.hashCode(); + } - @Override - public String[] getVarNames() { - return new String[]{varName}; - } - } + @Override + public FormulaType getType() { + return this.type; + } - class ApronRatUnaryNode implements ApronNode, RationalFormula { - private final FormulaType type = FormulaType.RATIONAL; - private final Texpr1UnNode unaryNode; - private final String[] varNames; + private void addVarToEnv() { + Var[] intVars = formulaCreator.getEnvironment().getIntVars(); + Var[] realVars = formulaCreator.getEnvironment().getRealVars(); + Var[] newRealVars = new Var[realVars.length + 1]; + int i = 0; + for (Var var : realVars) { + newRealVars[i] = var; + i++; + } + newRealVars[realVars.length] = new StringVar(this.varName); + formulaCreator.setEnvironment(new Environment(intVars, newRealVars)); + } - public ApronRatUnaryNode(ApronNode param, int op) { - this.unaryNode = new Texpr1UnNode(op, param.getNode()); - this.varNames = param.getVarNames(); - } + public Texpr1VarNode getNode() { + return varNode; + } - @Override - public FormulaType getType() { - return null; + @Override + public String[] getVarNames() { + return new String[]{varName}; + } + @Override + public ApronNode getInstance() { + return this; + } } - public Texpr1UnNode getNode() { - return this.unaryNode; - } + class ApronRatUnaryNode implements RationalFormula, ApronNode { + private final FormulaType type = FormulaType.RATIONAL; + private final Texpr1UnNode unaryNode; + private final String[] varNames; - @Override - public String[] getVarNames() { - return varNames; - } - } + public ApronRatUnaryNode(ApronNode param, int op) { + this.unaryNode = new Texpr1UnNode(op, param.getNode()); + this.varNames = param.getVarNames(); + } - class ApronRatBinaryNode implements ApronNode, RationalFormula { + @Override + public String toString() { + return unaryNode.toString(); + } - private final FormulaType type = FormulaType.RATIONAL; - private final Texpr1BinNode binaryNode; - private final String[] varNames; + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + if (!(other instanceof ApronRatUnaryNode)) { + return false; + } + return this == ((ApronRatUnaryNode) other).getInstance(); } + + @Override + public int hashCode() { + return this.unaryNode.hashCode(); + } - public ApronRatBinaryNode(ApronNode param1, ApronNode param2, int op) { - this.binaryNode = new Texpr1BinNode(op, param1.getNode(), param2.getNode()); - String[] varNames1 = param1.getVarNames(); - String[] varNames2 = param2.getVarNames(); - String[] allVarNames = new String[varNames1.length + varNames2.length]; - System.arraycopy(varNames1, 0, allVarNames, 0, varNames1.length); - int j = varNames1.length - 1; - for (int i = 0; i < varNames2.length; i++) { - allVarNames[j] = varNames1[i]; - j++; - } - this.varNames = allVarNames; - } + @Override + public FormulaType getType() { + return null; + } - @Override - public FormulaType getType() { - return this.type; - } + public Texpr1UnNode getNode() { + return this.unaryNode; + } - @Override - public Texpr1Node getNode() { - return this.binaryNode; + @Override + public String[] getVarNames() { + return varNames; + } + @Override + public ApronNode getInstance() { + return this; + } } - @Override - public String[] getVarNames() { - return varNames; - } - } + class ApronRatBinaryNode implements RationalFormula, ApronNode { + + private final FormulaType type = FormulaType.RATIONAL; + private final Texpr1BinNode binaryNode; + private final String[] varNames; + + public ApronRatBinaryNode(ApronNode param1, ApronNode param2, int op) { + this.binaryNode = new Texpr1BinNode(op, param1.getNode(), param2.getNode()); + String[] varNames1 = param1.getVarNames(); + String[] varNames2 = param2.getVarNames(); + String[] allVarNames = new String[varNames1.length + varNames2.length]; + System.arraycopy(varNames1, 0, allVarNames, 0, varNames1.length); + int j = varNames1.length - 1; + for (int i = 0; i < varNames2.length; i++) { + allVarNames[j] = varNames1[i]; + j++; + } + this.varNames = allVarNames; + } - class ApronIntCstNode implements ApronNode, IntegerFormula { + @Override + public String toString() { + return binaryNode.toString(); + } - private final FormulaType type = FormulaType.INTEGER; - private final Texpr1CstNode cstNode; + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + if (!(other instanceof ApronRatBinaryNode)) { + return false; + } + return this == ((ApronRatBinaryNode) other).getInstance(); } + + @Override + public int hashCode() { + return this.binaryNode.hashCode(); + } - public ApronIntCstNode(BigInteger pNumerator) { - this.cstNode = new Texpr1CstNode(new MpqScalar(pNumerator)); - } + @Override + public FormulaType getType() { + return this.type; + } - @Override - public FormulaType getType() { - return this.type; - } + @Override + public Texpr1Node getNode() { + return this.binaryNode; + } - public Texpr1CstNode getNode() { - return cstNode; + @Override + public String[] getVarNames() { + return varNames; + } + @Override + public ApronNode getInstance() { + return this; + } } - @Override - public String[] getVarNames() { - return new String[0]; - } - } + class ApronIntCstNode implements IntegerFormula, ApronNode { - class ApronIntVarNode implements ApronNode, IntegerFormula { + private final FormulaType type = FormulaType.INTEGER; + private final Texpr1CstNode cstNode; + private final BigInteger value; - private final FormulaType type = FormulaType.INTEGER; - private final Texpr1VarNode varNode; - private final String varName; - private final ApronFormulaCreator formulaCreator; + public ApronIntCstNode(BigInteger pNumerator) { + this.cstNode = new Texpr1CstNode(new MpqScalar(pNumerator)); + this.value = pNumerator; + } - public ApronIntVarNode(String pVarName, ApronFormulaCreator pFormulaCreator) { - this.varNode = new Texpr1VarNode(pVarName); - this.varName = pVarName; - this.formulaCreator = pFormulaCreator; - } + @Override + public String toString() { + return cstNode.toString(); + } - @Override - public FormulaType getType() { - return this.type; - } + @Override + public boolean equals(Object other) { - public Texpr1VarNode getNode() { - return varNode; - } + if (other == this) { + return true; + } + if (!(other instanceof ApronIntCstNode)) { + return false; + } + return this == ((ApronIntCstNode) other).getInstance(); } - private void addVarToEnv() { - Var[] intVars = formulaCreator.getEnvironment().getIntVars(); - Var[] realVars = formulaCreator.getEnvironment().getRealVars(); - Var[] newIntVars = new Var[intVars.length + 1]; - int i = 0; - for (Var var : intVars) { - newIntVars[i] = var; - i++; - } - newIntVars[realVars.length] = new StringVar(this.varName); - formulaCreator.setEnvironment(new Environment(newIntVars, realVars)); - } + @Override + public int hashCode() { + return this.cstNode.hashCode(); + } - @Override - public String[] getVarNames() { - return new String[]{varName}; - } - } + @Override + public FormulaType getType() { + return this.type; + } - class ApronIntUnaryNode implements ApronNode, IntegerFormula { - private final FormulaType type = FormulaType.INTEGER; - private final Texpr1UnNode unaryNode; - private final String[] varNames; + public Texpr1CstNode getNode() { + return cstNode; + } - public ApronIntUnaryNode(ApronNode param, int op) { - this.unaryNode = new Texpr1UnNode(op, param.getNode()); - this.varNames = param.getVarNames(); - } + @Override + public String[] getVarNames() { + return new String[0]; + } - @Override - public FormulaType getType() { - return null; + public BigInteger getValue() { + return value; + } + @Override + public ApronNode getInstance() { + return this; + } } - public Texpr1UnNode getNode() { - return this.unaryNode; - } + class ApronIntVarNode implements IntegerFormula, ApronNode { - @Override - public String[] getVarNames() { - return varNames; - } - } + private final FormulaType type = FormulaType.INTEGER; + private final Texpr1VarNode varNode; + private final String varName; + private final ApronFormulaCreator formulaCreator; - class ApronIntBinaryNode implements ApronNode, IntegerFormula { + public ApronIntVarNode(String pVarName, ApronFormulaCreator pFormulaCreator) { + this.varNode = new Texpr1VarNode(pVarName); + this.varName = pVarName; + this.formulaCreator = pFormulaCreator; + addVarToEnv(); + } - private final FormulaType type = FormulaType.INTEGER; - private final Texpr1BinNode binaryNode; - private final String[] varNames; + @Override + public String toString() { + return varNode.toString(); + } - public ApronIntBinaryNode(ApronNode param1, ApronNode param2, int op) { - this.binaryNode = new Texpr1BinNode(op, param1.getNode(), param2.getNode()); - String[] varNames1 = param1.getVarNames(); - String[] varNames2 = param2.getVarNames(); - String[] allVarNames = new String[varNames1.length + varNames2.length]; - System.arraycopy(varNames1, 0, allVarNames, 0, varNames1.length); - int j = varNames1.length - 1; - for (int i = 0; i < varNames2.length; i++) { - allVarNames[j] = varNames1[i]; - j++; - } - this.varNames = allVarNames; - } + @Override + public boolean equals(Object other) { - @Override - public FormulaType getType() { - return this.type; + if (other == this) { + return true; + } + if (!(other instanceof ApronIntVarNode)) { + return false; + } + return this == ((ApronIntVarNode) other).getInstance(); } + + @Override + public int hashCode() { + return this.varNode.hashCode(); + } + + @Override + public FormulaType getType() { + return this.type; + } + + public Texpr1VarNode getNode() { + return varNode; + } + + private void addVarToEnv() { + Var[] intVars = formulaCreator.getEnvironment().getIntVars(); + Var[] realVars = formulaCreator.getEnvironment().getRealVars(); + Var[] newIntVars = new Var[intVars.length + 1]; + int i = 0; + for (Var var : intVars) { + newIntVars[i] = var; + i++; + } + newIntVars[intVars.length] = new StringVar(this.varName); + formulaCreator.setEnvironment(new Environment(newIntVars, realVars)); + } + + @Override + public String[] getVarNames() { + return new String[]{varName}; + } + @Override + public ApronNode getInstance() { + return this; + } } - @Override - public Texpr1Node getNode() { - return this.binaryNode; + class ApronIntUnaryNode implements IntegerFormula, ApronNode { + private final FormulaType type = FormulaType.INTEGER; + private final Texpr1UnNode unaryNode; + private final String[] varNames; + + public ApronIntUnaryNode(ApronNode param, int op) { + this.unaryNode = new Texpr1UnNode(op, param.getNode()); + this.varNames = param.getVarNames(); + } + + @Override + public String toString() { + return unaryNode.toString(); + } + + @Override + public boolean equals(Object other) { + + if (other == this) { + return true; + } + if (!(other instanceof ApronIntUnaryNode)) { + return false; + } + return this == ((ApronIntUnaryNode) other).getInstance(); } + + @Override + public int hashCode() { + return this.unaryNode.hashCode(); + } + + @Override + public FormulaType getType() { + return null; + } + + public Texpr1UnNode getNode() { + return this.unaryNode; + } + + @Override + public String[] getVarNames() { + return varNames; + } + @Override + public ApronNode getInstance() { + return this; + } } - @Override - public String[] getVarNames() { - return varNames; + class ApronIntBinaryNode implements IntegerFormula, ApronNode { + + private final FormulaType type = FormulaType.INTEGER; + private final Texpr1BinNode binaryNode; + private final String[] varNames; + + public ApronIntBinaryNode(ApronNode param1, ApronNode param2, int op) { + this.binaryNode = new Texpr1BinNode(op, param1.getNode(), param2.getNode()); + String[] varNames1 = param1.getVarNames(); + String[] varNames2 = param2.getVarNames(); + String[] allVarNames = new String[varNames1.length + varNames2.length]; + System.arraycopy(varNames1, 0, allVarNames, 0, varNames1.length); + int j = varNames1.length - 1; + for (int i = 0; i < varNames2.length; i++) { + allVarNames[j] = varNames1[i]; + j++; + } + this.varNames = allVarNames; + } + + @Override + public String toString() { + return binaryNode.toString(); + } + + @Override + public boolean equals(Object other) { + + if (other == this) { + return true; + } + if (!(other instanceof ApronIntBinaryNode)) { + return false; + } + return this == ((ApronIntBinaryNode) other).getInstance(); } + + @Override + public int hashCode() { + return this.binaryNode.hashCode(); + } + + @Override + public FormulaType getType() { + return this.type; + } + + @Override + public Texpr1Node getNode() { + return this.binaryNode; + } + + @Override + public String[] getVarNames() { + return varNames; + } + @Override + public ApronNode getInstance() { + return this; + } } } - class ApronConstraint implements ApronNode, BooleanFormula { + + class ApronConstraint implements BooleanFormula, ApronNode { private final Tcons1 constraintNode; private final Texpr1Node node; @@ -314,6 +539,27 @@ public ApronConstraint(int pKind, Environment pEnvironment, ApronNode pNode) { this.apronNode = pNode; } + @Override + public String toString() { + return constraintNode.toString(); + } + + @Override + public boolean equals(Object other) { + + if (other == this) { + return true; + } + if (!(other instanceof ApronConstraint)) { + return false; + } + return this == ((ApronConstraint) other).getInstance(); } + + @Override + public int hashCode() { + return this.constraintNode.hashCode(); + } + @Override public FormulaType getType() { return FormulaType.BOOLEAN; @@ -336,5 +582,9 @@ public String[] getVarNames() { public ApronNode getApronNode() { return apronNode; } + @Override + public ApronNode getInstance() { + return this; + } } } From 1834883258aa0fa22260450fdbb9ef55c54b5c5c Mon Sep 17 00:00:00 2001 From: winnieros Date: Sat, 26 Aug 2023 11:58:17 +0200 Subject: [PATCH 017/133] Model now working --- .../solvers/apron/ApronFormulaCreator.java | 85 ++++++++++++++++--- .../java_smt/solvers/apron/ApronModel.java | 30 ++++++- .../solvers/apron/ApronTheoremProver.java | 6 +- .../solvers/apron/types/ApronNode.java | 71 +++++++++++++++- 4 files changed, 172 insertions(+), 20 deletions(-) diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java index 2b01f8f6d8..54f9717f94 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java @@ -27,6 +27,7 @@ import java.util.Map; import javax.annotation.Nullable; import org.sosy_lab.common.rationals.Rational; +import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.Formula; import org.sosy_lab.java_smt.api.FormulaType.FloatingPointType; import org.sosy_lab.java_smt.api.visitors.FormulaVisitor; @@ -37,9 +38,15 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.ApronRationalType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntBinaryNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntCstNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntUnaryNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntVarNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatBinaryNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatCstNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatUnaryNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatVarNode; @@ -66,14 +73,13 @@ protected ApronFormulaCreator( } @Override - public Object convertValue(ApronNode pf1) { - FormulaType type = pf1.getType(); - if(pf1 instanceof ApronIntCstNode){ - ApronIntCstNode cst = (ApronIntCstNode) pf1; - return cst.getValue(); - } else if (pf1 instanceof ApronRatCstNode) { - ApronRatCstNode cast = (ApronRatCstNode) pf1; - Rational rational = Rational.of(cast.getNumerator(),cast.getDenominator()); + public Object convertValue(ApronNode exprNode, ApronNode value) { + FormulaType type = exprNode.getType(); + FormulaType valueType = value.getType(); + if (valueType == FormulaType.INTEGER) { + return ((ApronIntCstNode) value).getValue(); + } else if ( valueType == FormulaType.RATIONAL) { + return ((ApronRatCstNode) value).getDenominator().divide(((ApronRatCstNode) value).getDenominator()); } return null; } @@ -103,10 +109,11 @@ public ApronFormulaType getArrayType(ApronFormulaType indexType, ApronFormulaTyp /** * For making a Formula (Type ApronNode) for a variable it is important to also update the - * @environment as it holds all variables; + * * @param pApronFormulaType Integer or Rational? - * @param varName name of the variable + * @param varName name of the variable * @return object of either ApronIntVarNode (Type Integer) or ApronRatVarNode (Type Rational) + * @environment as it holds all variables; */ @Override public ApronNode makeVariable(ApronFormulaType pApronFormulaType, String varName) { @@ -134,7 +141,7 @@ public Map getVariables() { @Override public org.sosy_lab.java_smt.api.FormulaType getFormulaType(ApronNode formula) { FormulaType type = formula.getType(); - if(type.equals(FormulaType.BOOLEAN)){ + if (type.equals(FormulaType.BOOLEAN)) { return org.sosy_lab.java_smt.api.FormulaType.BooleanType; } else if (type.equals(FormulaType.RATIONAL)) { return org.sosy_lab.java_smt.api.FormulaType.RationalType; @@ -166,4 +173,60 @@ public Long declareUFImpl( protected Long getBooleanVarDeclarationImpl(ApronNode pApronFormula) { return null; } + + @Override + public T encapsulate( + org.sosy_lab.java_smt.api.FormulaType pType, + ApronNode pTerm) { + if (pType.isBooleanType()) { + ApronConstraint constraint = (ApronConstraint) pTerm; + return (T) new ApronNode.ApronConstraint(constraint); + } else if (pType.isIntegerType()) { + if (pTerm instanceof ApronIntCstNode) { + ApronIntCstNode node = (ApronIntCstNode) pTerm; + return (T) new ApronNode.ApronNumeralNode.ApronIntCstNode(node); + } else if (pTerm instanceof ApronIntVarNode) { + ApronIntVarNode node = (ApronIntVarNode) pTerm; + return (T) new ApronNode.ApronNumeralNode.ApronIntVarNode(node); + } else if (pTerm instanceof ApronIntUnaryNode) { + ApronIntUnaryNode node = (ApronIntUnaryNode) pTerm; + return (T) new ApronNode.ApronNumeralNode.ApronIntUnaryNode(node); + } else if (pTerm instanceof ApronIntBinaryNode) { + ApronIntBinaryNode node = (ApronIntBinaryNode) pTerm; + return (T) new ApronNode.ApronNumeralNode.ApronIntBinaryNode(node); + } else { + throw new IllegalArgumentException("Term " + pTerm + " not supported in Apron."); + } + } else if (pType.isRationalType()) { + if (pTerm instanceof ApronRatCstNode) { + ApronRatCstNode node = (ApronRatCstNode) pTerm; + return (T) new ApronNode.ApronNumeralNode.ApronRatCstNode(node); + } else if (pTerm instanceof ApronRatVarNode) { + ApronRatVarNode node = (ApronRatVarNode) pTerm; + return (T) new ApronNode.ApronNumeralNode.ApronRatVarNode(node); + } else if (pTerm instanceof ApronRatUnaryNode) { + ApronRatUnaryNode node = (ApronRatUnaryNode) pTerm; + return (T) new ApronNode.ApronNumeralNode.ApronRatUnaryNode(node); + } else if (pTerm instanceof ApronRatBinaryNode) { + ApronRatBinaryNode node = (ApronRatBinaryNode) pTerm; + return (T) new ApronNode.ApronNumeralNode.ApronRatBinaryNode(node); + } else { + throw new IllegalArgumentException("Term " + pTerm + " not supported in Apron."); + } + + } else { + throw new IllegalArgumentException("Type " + pType + " not supported in Apron."); + } + } + + @Override + public ApronNode extractInfo(Formula pT){ + return ApronFormulaManager.getTerm(pT); + } + + @Override + public BooleanFormula encapsulateBoolean(ApronNode pTerm) { + ApronConstraint constraint = (ApronConstraint) pTerm; + return new ApronConstraint(constraint); + } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java index 1015e14574..4640c545fe 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java @@ -36,6 +36,7 @@ import java.util.Objects; import org.checkerframework.checker.nullness.qual.Nullable; import org.sosy_lab.java_smt.api.BooleanFormula; +import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula; import org.sosy_lab.java_smt.basicimpl.AbstractModel; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; @@ -102,7 +103,7 @@ private ValueAssignment getAssignment(ApronConstraint pFormula, String pVar) { Texpr1BinNode.OP_SUB); BooleanFormula formula = new ApronConstraint(Tcons1.EQ, formulaCreator.getEnvironment(), binaryNode); //is the representation x=0, if 0 is the model for x - return new ValueAssignment(keyFormula, valueFormula, formula, pVar, castIntValue, + return new ValueAssignment(keyFormula, valueFormula, formula, pVar, formulaCreator.convertValue(keyFormula,valueFormula), argumentInterpretationBuilder.build()); } else { ApronNode keyFormula = formulaCreator.getVariables().get(varName); @@ -118,7 +119,8 @@ private ValueAssignment getAssignment(ApronConstraint pFormula, String pVar) { Texpr1BinNode.OP_SUB); BooleanFormula formula = new ApronConstraint(Tcons1.EQ, formulaCreator.getEnvironment(), binaryNode); - return new ValueAssignment(keyFormula, valueFormula, formula, pVar, castRatValue, + return new ValueAssignment(keyFormula, valueFormula, formula, pVar, + formulaCreator.convertValue(keyFormula,valueFormula), argumentInterpretationBuilder.build()); } } catch (ApronException pApronException) { @@ -129,6 +131,28 @@ private ValueAssignment getAssignment(ApronConstraint pFormula, String pVar) { @Override protected @Nullable ApronNode evalImpl(ApronNode formula) { Preconditions.checkState(!isClosed()); - return formula; + return getValue(formula); + } + + private ApronNode getValue(ApronNode pNode) { + if (pNode instanceof ApronIntVarNode) { + ApronIntVarNode varNode = (ApronIntVarNode) pNode; + String varName = varNode.getVarName(); + for (ValueAssignment assignment : model) { + if (varName.equals(assignment.getName())) { + return (ApronNode) assignment.getValueAsFormula(); + } + } + } else if (pNode instanceof ApronRatVarNode) { + ApronRatVarNode varNode = (ApronRatVarNode) pNode; + String varName = varNode.getVarName(); + for (ValueAssignment assignment : model) { + if (varName.equals(assignment.getName())) { + return (ApronNode) assignment.getValueAsFormula(); + } + } + + + } return pNode; } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java index 1e7021f1a2..1b3381e02f 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java @@ -74,14 +74,12 @@ public void pop() { @Override public @Nullable Void addConstraint(BooleanFormula constraint) throws InterruptedException { - /*ApronNode node = ApronFormulaManager.getTerm(constraint, - this.solverContext.getFormulaCreator().getEnvironment(), - this.solverContext.getFormulaCreator()); + ApronNode node = ApronFormulaManager.getTerm(constraint); if(node instanceof ApronConstraint){ addConstraintException((ApronConstraint) node); } else { throw new IllegalArgumentException("Constraint of wrong Type!"); - }*/ + } return null; } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java index df97cf8959..94d5b475b2 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java @@ -63,6 +63,12 @@ public ApronRatCstNode(BigInteger pNumerator, BigInteger pDenominator) { this.denominator = pDenominator; } + public ApronRatCstNode(ApronRatCstNode pNode){ + this.cstNode = pNode.getNode(); + this.numerator = pNode.getNumerator(); + this.denominator = pNode.getDenominator(); + } + @Override public FormulaType getType() { return this.type; @@ -126,6 +132,21 @@ public ApronRatVarNode(String pVarName, ApronFormulaCreator pFormulaCreator) { this.varName = pVarName; addVarToEnv(); } + + public ApronRatVarNode(ApronRatVarNode pNode){ + this.varNode = pNode.getNode(); + this.varName = pNode.getVarName(); + this.formulaCreator = pNode.getFormulaCreator(); + } + + public String getVarName() { + return varName; + } + + public ApronFormulaCreator getFormulaCreator() { + return formulaCreator; + } + @Override public String toString() { return varNode.toString(); @@ -190,6 +211,11 @@ public ApronRatUnaryNode(ApronNode param, int op) { this.varNames = param.getVarNames(); } + public ApronRatUnaryNode(ApronRatUnaryNode pNode){ + this.unaryNode = pNode.getNode(); + this.varNames = pNode.getVarNames(); + } + @Override public String toString() { return unaryNode.toString(); @@ -249,6 +275,11 @@ public ApronRatBinaryNode(ApronNode param1, ApronNode param2, int op) { this.varNames = allVarNames; } + public ApronRatBinaryNode(ApronRatBinaryNode pNode){ + this.binaryNode = pNode.getNode(); + this.varNames = pNode.getVarNames(); + } + @Override public String toString() { return binaryNode.toString(); @@ -275,7 +306,7 @@ public FormulaType getType() { } @Override - public Texpr1Node getNode() { + public Texpr1BinNode getNode() { return this.binaryNode; } @@ -300,6 +331,11 @@ public ApronIntCstNode(BigInteger pNumerator) { this.value = pNumerator; } + public ApronIntCstNode(ApronIntCstNode pNode){ + this.cstNode = pNode.getNode(); + this.value = pNode.getValue(); + } + @Override public String toString() { return cstNode.toString(); @@ -358,6 +394,20 @@ public ApronIntVarNode(String pVarName, ApronFormulaCreator pFormulaCreator) { addVarToEnv(); } + public ApronIntVarNode(ApronIntVarNode pNode){ + this.varNode = pNode.getNode(); + this.formulaCreator = pNode.getFormulaCreator(); + this.varName = pNode.getVarName(); + } + + public String getVarName() { + return varName; + } + + public ApronFormulaCreator getFormulaCreator() { + return formulaCreator; + } + @Override public String toString() { return varNode.toString(); @@ -421,6 +471,11 @@ public ApronIntUnaryNode(ApronNode param, int op) { this.varNames = param.getVarNames(); } + public ApronIntUnaryNode(ApronIntUnaryNode pNode){ + this.unaryNode = pNode.getNode(); + this.varNames = pNode.getVarNames(); + } + @Override public String toString() { return unaryNode.toString(); @@ -481,6 +536,11 @@ public ApronIntBinaryNode(ApronNode param1, ApronNode param2, int op) { this.varNames = allVarNames; } + public ApronIntBinaryNode(ApronIntBinaryNode pNode){ + this.binaryNode = pNode.getNode(); + this.varNames = pNode.getVarNames(); + } + @Override public String toString() { return binaryNode.toString(); @@ -508,7 +568,7 @@ public FormulaType getType() { } @Override - public Texpr1Node getNode() { + public Texpr1BinNode getNode() { return this.binaryNode; } @@ -539,6 +599,13 @@ public ApronConstraint(int pKind, Environment pEnvironment, ApronNode pNode) { this.apronNode = pNode; } + public ApronConstraint(ApronConstraint pConstraint){ + this.constraintNode = pConstraint.getConstraintNode(); + this.node = pConstraint.getNode(); + this.apronNode = pConstraint.getApronNode(); + this.varNames = pConstraint.getVarNames(); + } + @Override public String toString() { return constraintNode.toString(); From a747a278bb5554e232f02bcfc98f80be600c0fd2 Mon Sep 17 00:00:00 2001 From: winnieros Date: Sat, 26 Aug 2023 18:11:01 +0200 Subject: [PATCH 018/133] Small implementations that were missing --- .../solvers/apron/ApronFormulaCreator.java | 2 +- .../solvers/apron/ApronFormulaManager.java | 18 ++++++++++++++--- .../apron/ApronIntegerFormulaManager.java | 10 +++++++++- .../apron/ApronRationalFormulaManager.java | 20 ++++++++++++++++--- .../solvers/apron/ApronSolverContext.java | 8 +++----- .../solvers/apron/ApronTheoremProver.java | 18 +++++++++++++++-- 6 files changed, 61 insertions(+), 15 deletions(-) diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java index 54f9717f94..d8c501e38c 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java @@ -153,7 +153,7 @@ public org.sosy_lab.java_smt.api.FormulaType getFormulaType(ApronNode formula @Override public R visit(FormulaVisitor visitor, Formula formula, ApronNode f) { - return null; + throw new UnsupportedOperationException("Visit function is not supported by Apron."); } @Override diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java index cda020f55c..93abd9c10f 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaManager.java @@ -22,9 +22,14 @@ import apron.Environment; import apron.Tcons1; +import apron.Texpr1CstNode; +import apron.Texpr1Node; +import com.google.common.base.Preconditions; +import io.github.cvc5.Term; import java.math.BigInteger; import java.util.Map; import org.checkerframework.checker.nullness.qual.Nullable; +import org.checkerframework.checker.units.qual.A; import org.sosy_lab.common.Appender; import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.Formula; @@ -44,10 +49,14 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntCstNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronIntVarNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatUnaryNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatVarNode; import scala.Int; public class ApronFormulaManager extends AbstractFormulaManager { + + private ApronFormulaCreator formulaCreator; /** * Builds a solver from the given theory implementations. * @@ -79,25 +88,28 @@ protected ApronFormulaManager( @Nullable AbstractEnumerationFormulaManager enumManager) { super(pFormulaCreator, functionManager, booleanManager, pIntegerFormulaManager, pRationalFormulaManager, null, null, null, null, null, null, null); + this.formulaCreator = pFormulaCreator; } + @Override public BooleanFormula parse(String s) throws IllegalArgumentException { - return null; + throw new UnsupportedOperationException("Apron does not support parse()."); } @Override public Appender dumpFormula(ApronNode t) { - return null; + throw new UnsupportedOperationException("Apron does not support dumpFormula()."); } @Override public T substitute( T f, Map fromToMapping) { - return null; + throw new UnsupportedOperationException("Apron does not support substitute()."); } + public static ApronNode getTerm(Formula pFormula){ return ((ApronNode) pFormula).getInstance(); } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java index cecbdce515..8a84a1b720 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronIntegerFormulaManager.java @@ -28,6 +28,7 @@ import java.util.List; import org.checkerframework.checker.units.qual.A; import org.sosy_lab.java_smt.api.BooleanFormula; +import org.sosy_lab.java_smt.api.FormulaManager; import org.sosy_lab.java_smt.api.IntegerFormulaManager; import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; @@ -77,7 +78,14 @@ public BooleanFormula modularCongruence( @Override public BooleanFormula modularCongruence(IntegerFormula number1, IntegerFormula number2, long n) { - return null; + ApronIntCstNode mod = new ApronIntCstNode(BigInteger.valueOf(n)); + ApronIntBinaryNode x = new ApronIntBinaryNode(ApronFormulaManager.getTerm(number1), ApronFormulaManager.getTerm(number2), Texpr1BinNode.OP_SUB); + + if(BigInteger.ZERO.compareTo(BigInteger.valueOf(n))<0){ + + return (BooleanFormula) equal(mod,(ApronNode) x); + } + return (BooleanFormula) equal((ApronNode) mod,mod); } @Override diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java index 9e44e84efe..3311acef03 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java @@ -41,6 +41,7 @@ import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatCstNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatUnaryNode; import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronNumeralNode.ApronRatVarNode; +import scala.Int; public class ApronRationalFormulaManager extends ApronNumeralFormulaManager @@ -63,12 +64,25 @@ protected FormulaType getNumeralType() { @Override protected ApronNode makeNumberImpl(double pNumber) { - return null; - } + String str = String.valueOf(pNumber); + String[] numbers = str.split("\\."); + int num = Integer.parseInt(numbers[0]); + if(numbers.length>1) { + int den = Integer.parseInt(numbers[1]); + return new ApronRatCstNode(BigInteger.valueOf(num),BigInteger.valueOf(den)); + } + return new ApronRatCstNode(BigInteger.valueOf(num),BigInteger.ONE); } @Override protected ApronNode makeNumberImpl(BigDecimal pNumber) { - return null; + String str = pNumber.toPlainString(); + String[] numbers = str.split("\\."); + int num = Integer.parseInt(numbers[0]); + if(numbers.length>1) { + int den = Integer.parseInt(numbers[1]); + return new ApronRatCstNode(BigInteger.valueOf(num),BigInteger.valueOf(den)); + } + return new ApronRatCstNode(BigInteger.valueOf(num),BigInteger.ONE); } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java index e984f8f6e6..97523d2552 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java @@ -151,9 +151,7 @@ private ProverEnvironment newApronProverEnvironment(Set pProverOp return new ApronTheoremProver(pProverOptions, booleanFormulaManager, this.shutdownNotifier, this); } catch (ApronException pApronException) { - System.out.println(pApronException); - System.exit(0); - return null; + throw new RuntimeException(pApronException); } } @@ -164,11 +162,11 @@ protected InterpolatingProverEnvironment newProverEnvironmentWithInterpolatio @Override protected OptimizationProverEnvironment newOptimizationProverEnvironment0(Set pSet) { - return null; + throw new UnsupportedOperationException("Optimization prover not supported by Apron."); } @Override protected boolean supportsAssumptionSolving() { - return false; + return true; } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java index 1b3381e02f..02ebbf320d 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java @@ -32,6 +32,7 @@ import java.util.Optional; import java.util.Set; import javax.annotation.Nullable; +import javax.sound.midi.Soundbank; import org.sosy_lab.common.ShutdownNotifier; import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.BooleanFormulaManager; @@ -144,13 +145,26 @@ public List getUnsatCore() { @Override public Optional> unsatCoreOverAssumptions(Collection assumptions) throws SolverException, InterruptedException { - return Optional.empty(); + throw new UnsupportedOperationException("Apron does not support unsat-core."); } @Override public boolean isUnsatWithAssumptions(Collection assumptions) throws SolverException, InterruptedException { - return false; + Tcons1[] constraints =new Tcons1[assumptions.size()]; + int i = 0; + for (BooleanFormula assumption:assumptions) { + ApronConstraint cons = (ApronConstraint) ApronFormulaManager.getTerm(assumption); + constraints[i] = cons.getConstraintNode(); + i++; + } + try { + Abstract1 absNew = new Abstract1(solverContext.getManager(), constraints); + Abstract1 result = this.abstract1.joinCopy(solverContext.getManager(), absNew); + return result.isBottom(solverContext.getManager()); + } catch (ApronException e){ + throw new RuntimeException(e.toString()); + } } @Override From 2aa423bbb87a4afa41453d2fe3bc35ba268aae18 Mon Sep 17 00:00:00 2001 From: winnieros Date: Sun, 27 Aug 2023 19:55:54 +0200 Subject: [PATCH 019/133] Testclass debugging --- .../apron/ApronBooleanFormulaManager.java | 9 ++- .../solvers/apron/ApronEvaluator.java | 58 +++++++++++++++ .../solvers/apron/ApronFormulaCreator.java | 15 +++- .../java_smt/solvers/apron/ApronModel.java | 74 ++++++++++++++++--- .../apron/ApronRationalFormulaManager.java | 8 +- .../solvers/apron/ApronSolverContext.java | 1 - .../solvers/apron/ApronTheoremProver.java | 13 ++-- .../solvers/apron/ApronUFManager.java | 4 + .../solvers/apron/types/ApronNode.java | 20 +++++ .../java_smt/test/SolverBasedTest0.java | 3 + 10 files changed, 181 insertions(+), 24 deletions(-) create mode 100644 src/org/sosy_lab/java_smt/solvers/apron/ApronEvaluator.java diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java index b1bf772dd9..91ea1b0964 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronBooleanFormulaManager.java @@ -51,9 +51,14 @@ protected ApronNode makeVariableImpl(String pVar) { protected ApronNode makeBooleanImpl(boolean value) { ApronIntCstNode cst = new ApronIntCstNode(BigInteger.valueOf(1)); if(value){ - return new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(),cst); //1!=0 --> true + ApronConstraint t = new ApronConstraint(Tcons1.SUP,formulaCreator.getEnvironment(),cst); //1 + // !=0 --> true + t.setIsTrue(true); + return t; } else { - return new ApronConstraint(Tcons1.EQ,formulaCreator.getEnvironment(),cst);//1==0 --> false + ApronConstraint f = new ApronConstraint(Tcons1.EQ,formulaCreator.getEnvironment(),cst);//1==0 --> false + f.setIsTrue(false); + return f; } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronEvaluator.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronEvaluator.java new file mode 100644 index 0000000000..dadf2b5e41 --- /dev/null +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronEvaluator.java @@ -0,0 +1,58 @@ +/* + * JavaSMT is an API wrapper for a collection of SMT solvers. + * This file is part of JavaSMT. + * + * Copyright (C) 2007-2016 Dirk Beyer + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sosy_lab.java_smt.solvers.apron; + +import apron.Environment; +import java.util.Collection; +import java.util.List; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.sosy_lab.java_smt.api.SolverException; +import org.sosy_lab.java_smt.basicimpl.AbstractEvaluator; +import org.sosy_lab.java_smt.basicimpl.AbstractProver; +import org.sosy_lab.java_smt.basicimpl.FormulaCreator; +import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode.ApronConstraint; + +public class ApronEvaluator extends AbstractEvaluator { + + private final List> assertedFormulas; + + private final ApronTheoremProver theoremProver; + protected ApronEvaluator( + AbstractProver pProver, + FormulaCreator creator) { + super(pProver, creator); + ApronTheoremProver prover = (ApronTheoremProver) pProver; + this.assertedFormulas = prover.getAssertedFormulas(); + this.theoremProver = prover; + } + + @Override + protected @Nullable ApronNode evalImpl(ApronNode formula) { + try { + ApronModel apronModel = (ApronModel) theoremProver.getModel(); + return apronModel.getValue(formula); + }catch (SolverException e){ + throw new RuntimeException(e); + } + } +} diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java index d8c501e38c..60fbe70571 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronFormulaCreator.java @@ -76,12 +76,17 @@ protected ApronFormulaCreator( public Object convertValue(ApronNode exprNode, ApronNode value) { FormulaType type = exprNode.getType(); FormulaType valueType = value.getType(); - if (valueType == FormulaType.INTEGER) { + + if (valueType == FormulaType.INTEGER && value instanceof ApronIntCstNode) { return ((ApronIntCstNode) value).getValue(); - } else if ( valueType == FormulaType.RATIONAL) { + } else if ( valueType == FormulaType.RATIONAL && value instanceof ApronRatCstNode) { return ((ApronRatCstNode) value).getDenominator().divide(((ApronRatCstNode) value).getDenominator()); + } else if (value instanceof ApronIntVarNode) { + return ((ApronIntVarNode) value).getVarName(); + }else if (value instanceof ApronRatVarNode) { + return ((ApronRatVarNode) value).getVarName(); } - return null; + else return null; } public Environment getEnvironment() { @@ -117,12 +122,14 @@ public ApronFormulaType getArrayType(ApronFormulaType indexType, ApronFormulaTyp */ @Override public ApronNode makeVariable(ApronFormulaType pApronFormulaType, String varName) { - Preconditions.checkArgument(!environment.hasVar(varName), "Variablename already exists!"); Preconditions.checkArgument( (pApronFormulaType.getType().equals(FormulaType.INTEGER) || pApronFormulaType.getType() .equals( FormulaType.RATIONAL)), "Only Integer or rational variables allowed!"); + if(environment.hasVar(varName)){ + return variables.get(varName); + } if (pApronFormulaType.getType().equals(FormulaType.INTEGER)) { ApronIntVarNode varNode = new ApronIntVarNode(varName, this); variables.put(varName, varNode); diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java index 4640c545fe..2dba8ed61d 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronModel.java @@ -20,23 +20,33 @@ package org.sosy_lab.java_smt.solvers.apron; +import apron.Abstract1; import apron.ApronException; import apron.Environment; import apron.Interval; import apron.Manager; import apron.MpqScalar; import apron.Scalar; +import apron.StringVar; import apron.Tcons1; +import apron.Texpr0Node; import apron.Texpr1BinNode; +import apron.Texpr1Node; +import apron.Texpr1VarNode; +import apron.Var; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import java.math.BigInteger; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.Objects; import org.checkerframework.checker.nullness.qual.Nullable; +import org.sosy_lab.common.rationals.Rational; import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula; +import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula; import org.sosy_lab.java_smt.basicimpl.AbstractModel; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType; import org.sosy_lab.java_smt.solvers.apron.types.ApronFormulaType.FormulaType; @@ -84,11 +94,7 @@ private ImmutableList generateModel() { } private ValueAssignment getAssignment(ApronConstraint pFormula, String pVar) { - //needed: Formula keyFormula, Formula valueFormula, BooleanFormula formula (x = 3), - // Object value, List argumentInterpretation - ImmutableList.Builder argumentInterpretationBuilder = ImmutableList.builder(); //TODO - //argumentInterpretationBuilder.add(); - // : empty list! + ImmutableList.Builder argumentInterpretationBuilder = ImmutableList.builder(); try { ApronConstraint constraint = pFormula; String varName = pVar; @@ -97,8 +103,8 @@ private ValueAssignment getAssignment(ApronConstraint pFormula, String pVar) { Manager man = this.prover.getAbstract1().getCreationManager(); Interval interval = this.prover.getAbstract1().getBound(man, pVar); MpqScalar value = (MpqScalar) interval.sup; - int castIntValue = Integer.parseInt(value.toString()); - ApronIntCstNode valueFormula = new ApronIntCstNode(BigInteger.valueOf(castIntValue)); + BigInteger big = BigInteger.valueOf(Long.parseLong(value.toString())); + ApronIntCstNode valueFormula = new ApronIntCstNode(big); ApronIntBinaryNode binaryNode = new ApronIntBinaryNode(keyFormula, valueFormula, Texpr1BinNode.OP_SUB); BooleanFormula formula = new ApronConstraint(Tcons1.EQ, formulaCreator.getEnvironment(), @@ -112,8 +118,8 @@ private ValueAssignment getAssignment(ApronConstraint pFormula, String pVar) { Object value = interval.sup; //TODO: unfortunatly it is not possible to extract nominator and denominator out of an // Scalar; So all models show Integer Values - int castRatValue = Integer.parseInt(value.toString()); - ApronRatCstNode valueFormula = new ApronRatCstNode(BigInteger.valueOf(castRatValue), + BigInteger big = BigInteger.valueOf(Long.parseLong(value.toString())); + ApronRatCstNode valueFormula = new ApronRatCstNode(big, BigInteger.ONE); ApronRatBinaryNode binaryNode = new ApronRatBinaryNode(keyFormula, valueFormula, Texpr1BinNode.OP_SUB); @@ -134,7 +140,7 @@ private ValueAssignment getAssignment(ApronConstraint pFormula, String pVar) { return getValue(formula); } - private ApronNode getValue(ApronNode pNode) { + protected ApronNode getValue(ApronNode pNode) { if (pNode instanceof ApronIntVarNode) { ApronIntVarNode varNode = (ApronIntVarNode) pNode; String varName = varNode.getVarName(); @@ -151,8 +157,54 @@ private ApronNode getValue(ApronNode pNode) { return (ApronNode) assignment.getValueAsFormula(); } } + }else { + Texpr1Node node = pNode.getNode(); + List modelVars = new ArrayList<>(); + for (ValueAssignment assignment:model) { + String modelVar = assignment.getName(); + StringVar apronVar = new StringVar(modelVar); + ApronNode toSub = (ApronNode) assignment.getValueAsFormula(); + Texpr1Node toSubT = toSub.getNode(); + //hasVar() only works for Texpr0Node + Texpr0Node zeroNode = node.toTexpr0Node(prover.getAbstract1().getEnvironment()); + boolean hasVarZero = + zeroNode.hasDim(prover.getAbstract1().getEnvironment().dimOfVar(modelVar)); + if(hasVarZero){ + try { + Texpr1Node param1 = node.substitute(modelVar,toSubT); //substitutes every occurence of + // the variable with the value stored in model + Texpr1VarNode var = new Texpr1VarNode(modelVar); + Texpr1Node equation = new Texpr1BinNode(Texpr1BinNode.OP_SUB,param1,var); // param1 + // -variable + Tcons1 cons = new Tcons1(formulaCreator.getEnvironment(),Tcons1.EQ,equation); + Tcons1[] c = new Tcons1[]{cons}; + Abstract1 abstract1 = new Abstract1(prover.getAbstract1().getCreationManager(),c); + Object bound = + abstract1.getBound(prover.getAbstract1().getCreationManager(), modelVar).sup; + int value = Integer.parseInt(bound.toString()); + return new ApronIntCstNode(BigInteger.valueOf(value)); + } catch (ApronException e){ + throw new RuntimeException(e); + } + } + } + } + return pNode; + } + @Override + public @Nullable Rational evaluate(RationalFormula f) { + if(f instanceof ApronRatCstNode){ + return ((ApronRatCstNode) f).getRational(); + } return null; + } - } return pNode; + @Override + public String toString() { + StringBuilder str = new StringBuilder(); + for (ValueAssignment assignment:model) { + str.append(assignment.toString()); + } + return str.toString(); } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java index 3311acef03..46d4a7f4b7 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronRationalFormulaManager.java @@ -102,7 +102,13 @@ protected ApronNode makeNumberImpl(BigInteger i) { @Override protected ApronNode makeNumberImpl(String i) { - return new ApronRatCstNode(BigInteger.valueOf(Integer.parseInt(i)), BigInteger.ONE); + String[] numbers = i.split("/"); + int num = Integer.parseInt(numbers[0]); + if(numbers.length>1) { + int den = Integer.parseInt(numbers[1]); + return new ApronRatCstNode(BigInteger.valueOf(num),BigInteger.valueOf(den)); + } + return new ApronRatCstNode(BigInteger.valueOf(num),BigInteger.ONE); } @Override diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java index 97523d2552..896eccb973 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronSolverContext.java @@ -131,7 +131,6 @@ public Solvers getSolverName() { @Override public void close() { - //TODO was muss hier noch passieren? if (!closed) { closed = true; logger.log(Level.FINER, "Freeing Apron Environment"); diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java index 02ebbf320d..afb0b19663 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronTheoremProver.java @@ -65,6 +65,10 @@ protected ApronTheoremProver( this.assertedFormulas.add(new LinkedHashSet<>()); } + public List> getAssertedFormulas() { + return assertedFormulas; + } + @Override public void pop() { Preconditions.checkState(!closed); @@ -167,12 +171,11 @@ public boolean isUnsatWithAssumptions(Collection assumptions) } } - @Override - protected Evaluator getEvaluatorWithoutChecks() throws SolverException { - return null; - } - public Abstract1 getAbstract1() { return abstract1; } + + @Override + protected Evaluator getEvaluatorWithoutChecks() throws SolverException { + throw new UnsupportedOperationException("Apron does not support Evaluator without checks."); } } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/ApronUFManager.java b/src/org/sosy_lab/java_smt/solvers/apron/ApronUFManager.java index 95eaff71e9..4fd07149f1 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/ApronUFManager.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/ApronUFManager.java @@ -20,11 +20,15 @@ package org.sosy_lab.java_smt.solvers.apron; +import org.sosy_lab.java_smt.api.FormulaType; +import org.sosy_lab.java_smt.api.FunctionDeclaration; import org.sosy_lab.java_smt.basicimpl.AbstractUFManager; import org.sosy_lab.java_smt.basicimpl.FormulaCreator; +import org.sosy_lab.java_smt.solvers.apron.types.ApronNode; public class ApronUFManager extends AbstractUFManager { protected ApronUFManager(FormulaCreator pCreator) { super(pCreator); } + } diff --git a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java index 94d5b475b2..67aaf3964c 100644 --- a/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java +++ b/src/org/sosy_lab/java_smt/solvers/apron/types/ApronNode.java @@ -31,6 +31,7 @@ import apron.Texpr1VarNode; import apron.Var; import java.math.BigInteger; +import org.sosy_lab.common.rationals.Rational; import org.sosy_lab.java_smt.api.BooleanFormula; import org.sosy_lab.java_smt.api.Formula; import org.sosy_lab.java_smt.api.NumeralFormula; @@ -57,16 +58,24 @@ class ApronRatCstNode private final BigInteger numerator; private final BigInteger denominator; + private final Rational rational; + public ApronRatCstNode(BigInteger pNumerator, BigInteger pDenominator) { this.cstNode = new Texpr1CstNode(new MpqScalar(pNumerator.divide(pDenominator))); this.numerator = pNumerator; this.denominator = pDenominator; + this.rational = Rational.of(numerator,denominator); } public ApronRatCstNode(ApronRatCstNode pNode){ this.cstNode = pNode.getNode(); this.numerator = pNode.getNumerator(); this.denominator = pNode.getDenominator(); + this.rational = Rational.of(numerator,denominator); + } + + public Rational getRational() { + return rational; } @Override @@ -592,6 +601,8 @@ class ApronConstraint implements BooleanFormula, ApronNode { private final ApronNode apronNode; private final String[] varNames; + private boolean isTrue; + public ApronConstraint(int pKind, Environment pEnvironment, ApronNode pNode) { this.constraintNode = new Tcons1(pEnvironment, pKind, pNode.getNode()); this.node = pNode.getNode(); @@ -604,6 +615,15 @@ public ApronConstraint(ApronConstraint pConstraint){ this.node = pConstraint.getNode(); this.apronNode = pConstraint.getApronNode(); this.varNames = pConstraint.getVarNames(); + this.isTrue = pConstraint.isTrue(); + } + + public boolean isTrue() { + return isTrue; + } + + public void setIsTrue(boolean pTrue) { + isTrue = pTrue; } @Override diff --git a/src/org/sosy_lab/java_smt/test/SolverBasedTest0.java b/src/org/sosy_lab/java_smt/test/SolverBasedTest0.java index bc03a0980b..df73d7d036 100644 --- a/src/org/sosy_lab/java_smt/test/SolverBasedTest0.java +++ b/src/org/sosy_lab/java_smt/test/SolverBasedTest0.java @@ -370,6 +370,9 @@ protected void evaluateInModel( case BOOLECTOR: // ignore, Boolector provides no useful values break; + case APRON: + //ignore, Apron does not provide useful values + break; default: Truth.assertThat(eval).isIn(possibleExpectedFormulas); } From f761bc4ffebf18ab21f41c24481de03a1c0a23f1 Mon Sep 17 00:00:00 2001 From: winnieros Date: Mon, 28 Aug 2023 11:37:40 +0200 Subject: [PATCH 020/133] Rational Model --- .idea/inspectionProfiles/Project_Default.xml | 21 +++--------- .../apron/ApronBooleanFormulaManager.java | 12 +------ .../solvers/apron/ApronFormulaCreator.java | 9 ++++- .../java_smt/solvers/apron/ApronModel.java | 33 ++++++++++--------- .../solvers/apron/types/ApronNode.java | 2 +- 5 files changed, 31 insertions(+), 46 deletions(-) diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index be8f53a4b2..37ebaf72ca 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -1,17 +1,9 @@ - -