├── src ├── main │ ├── resources │ │ └── META-INF │ │ │ └── constraints │ │ │ └── solvers │ ├── java │ │ └── gov │ │ │ └── nasa │ │ │ └── jpf │ │ │ └── constraints │ │ │ ├── api │ │ │ ├── Simplifier.java │ │ │ ├── QuantifierEliminator.java │ │ │ ├── InterpolationSolver.java │ │ │ ├── SolverContext.java │ │ │ ├── ExpressionVisitor.java │ │ │ ├── ConstraintSolver.java │ │ │ ├── ValuationEntry.java │ │ │ └── Variable.java │ │ │ ├── types │ │ │ ├── RealType.java │ │ │ ├── FloatingPointType.java │ │ │ ├── IntegerType.java │ │ │ ├── BVIntegerType.java │ │ │ ├── NoSuchCastException.java │ │ │ ├── ConcreteRealType.java │ │ │ ├── NumericType.java │ │ │ ├── Type.java │ │ │ ├── ConcreteBVIntegerType.java │ │ │ ├── ConcreteFloatingPointType.java │ │ │ ├── ConcreteNumericType.java │ │ │ ├── ConcreteIntegerType.java │ │ │ ├── TypeForest.java │ │ │ ├── TypeContext.java │ │ │ └── ConcreteType.java │ │ │ ├── util │ │ │ ├── Printable.java │ │ │ ├── ExpressionSimplifier.java │ │ │ ├── AbstractPrintable.java │ │ │ ├── MixedParamsException.java │ │ │ ├── AddPrefixVisitor.java │ │ │ ├── ExpressionRestrictor.java │ │ │ ├── StripPrefixVisitor.java │ │ │ ├── DuplicatingVisitor.java │ │ │ ├── RenameVarVisitor.java │ │ │ ├── TransformVarVisitor.java │ │ │ ├── ContainsVarsVisitor.java │ │ │ ├── NestingDepthVisitor.java │ │ │ ├── TypeUtil.java │ │ │ └── ExpressionRestrictionVisitor.java │ │ │ ├── casts │ │ │ ├── CastOperation.java │ │ │ ├── ClassCastOperation.java │ │ │ └── NumericCastOperation.java │ │ │ ├── solvers │ │ │ ├── ConstraintSolverProvider.java │ │ │ ├── dontknow │ │ │ │ ├── DontKnowSolverProvider.java │ │ │ │ ├── DontKnowSolver.java │ │ │ │ └── DontKnowSolverContext.java │ │ │ └── ReflectionSolverProvider.java │ │ │ ├── expressions │ │ │ ├── AbstractBoolExpression.java │ │ │ ├── Quantifier.java │ │ │ ├── functions │ │ │ │ ├── math │ │ │ │ │ ├── axioms │ │ │ │ │ │ ├── FunctionProperties.java │ │ │ │ │ │ ├── IsNaNProperties.java │ │ │ │ │ │ ├── AcosProperties.java │ │ │ │ │ │ ├── PowProperties.java │ │ │ │ │ │ ├── SqrtProperties.java │ │ │ │ │ │ ├── LogProperties.java │ │ │ │ │ │ ├── ExpProperties.java │ │ │ │ │ │ └── CosProperties.java │ │ │ │ │ ├── UnaryFloatFunction.java │ │ │ │ │ ├── UnaryDoubleFunction.java │ │ │ │ │ ├── UnaryIntegerFunction.java │ │ │ │ │ ├── UnaryDoubleLongFunction.java │ │ │ │ │ ├── BinaryDoubleFunction.java │ │ │ │ │ ├── BooleanDoubleFunction.java │ │ │ │ │ └── MathFunctions.java │ │ │ │ └── Function.java │ │ │ ├── NumericOperator.java │ │ │ ├── BitvectorOperator.java │ │ │ ├── AbstractExpression.java │ │ │ ├── LogicalOperator.java │ │ │ ├── NumericComparator.java │ │ │ ├── EqualityExpression.java │ │ │ ├── Negation.java │ │ │ ├── UnaryMinus.java │ │ │ ├── BitvectorNegation.java │ │ │ ├── Constant.java │ │ │ └── AbstractExpressionVisitor.java │ │ │ ├── parser │ │ │ ├── UnknownTypeException.java │ │ │ ├── UndeclaredVariableException.java │ │ │ ├── MalformedExpressionException.java │ │ │ ├── ExpressionParseException.java │ │ │ ├── UnexpectedTokenException.java │ │ │ └── ParserUtil.java │ │ │ ├── exceptions │ │ │ └── SolverCreationException.java │ │ │ └── java │ │ │ └── ObjectConstraints.java │ └── antlr3 │ │ └── gov │ │ └── nasa │ │ └── jpf │ │ └── constraints │ │ └── parser │ │ └── Expression.g └── test │ └── java │ └── gov │ └── nasa │ └── jpf │ └── constraints │ ├── expressions │ ├── IfThenElseTest.java │ ├── functions │ │ └── math │ │ │ ├── IntegerFunctionTest.java │ │ │ ├── FunctionAxiomsTest.java │ │ │ └── MathFunctionsTest.java │ └── PrintTest.java │ └── parser │ └── ParserTest.java ├── README.md └── pom.xml /src/main/resources/META-INF/constraints/solvers: -------------------------------------------------------------------------------- 1 | gov.nasa.jpf.constraints.solvers.dontknow.DontKnowSolverProvider -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/api/Simplifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | package gov.nasa.jpf.constraints.api; 8 | 9 | /** 10 | * Interface for Simplifiers 11 | * 12 | * @author falk 13 | * 14 | * @param Expression Types 15 | */ 16 | public interface Simplifier { 17 | 18 | public Expression simplify(Expression expr); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/types/RealType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.types; 18 | 19 | public interface RealType extends NumericType { 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/api/QuantifierEliminator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.api; 17 | 18 | public interface QuantifierEliminator { 19 | public Expression eliminateQuantifiers(Expression expr); 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/types/FloatingPointType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.types; 18 | 19 | public interface FloatingPointType extends RealType { 20 | 21 | public int getSignificantBits(); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/util/Printable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.util; 18 | 19 | import java.io.IOException; 20 | 21 | public interface Printable { 22 | 23 | public void print(Appendable a) throws IOException; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jConstraints # 2 | *jConstraints* is a library for modeling expressions 3 | and for interacting with constraint solvers. 4 | 5 | ## Dependencies ## 6 | 7 | * [ANTLR v3][1] 8 | * [Guava 14.0.1][7] 9 | 10 | ANTLR is distributed under the terms of the 11 | [BSD license][3]. Guava is distributed under 12 | the terms of the [Apache License 2.0][8]. 13 | 14 | 15 | ## Building and Installing ## 16 | 17 | * In the *jConstraints* folder, run `mvn install` 18 | * If the compilation was successful, the *jConstraints* 19 | library can be found in the JAR file 20 | `target/jconstraints-[VERSION].jar` 21 | 22 | 23 | ## How To Use ## 24 | 25 | *jConstraints* does not come with constraint solvers. 26 | In order to use it, you will have to install one 27 | of the plugins that connect to constraint solvers. 28 | On the [*Psycopaths GitHub org*][9], you can find *jConstraints* 29 | plugins for, e.g. Z3. 30 | 31 | [1]: http://www.antlr3.org/ 32 | [3]: http://www.antlr3.org/license.html 33 | [7]: https://code.google.com/p/guava-libraries/ 34 | [8]: http://www.apache.org/licenses/LICENSE-2.0 35 | [9]: https://github.com/psycopaths 36 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/casts/CastOperation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.casts; 18 | 19 | public interface CastOperation { 20 | public Class getFromClass(); 21 | public Class getToClass(); 22 | 23 | public T cast(F from); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/types/IntegerType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.types; 18 | 19 | import java.math.BigInteger; 20 | 21 | 22 | public interface IntegerType extends NumericType { 23 | 24 | public BigInteger getMinInt(); 25 | public BigInteger getMaxInt(); 26 | 27 | 28 | public BigInteger integerValue(T value); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/solvers/ConstraintSolverProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.solvers; 17 | 18 | import gov.nasa.jpf.constraints.api.ConstraintSolver; 19 | 20 | import java.util.Properties; 21 | 22 | /** 23 | * A constraint solver provider. 24 | */ 25 | public interface ConstraintSolverProvider { 26 | public String[] getNames(); 27 | public ConstraintSolver createSolver(Properties config); 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/api/InterpolationSolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.api; 18 | 19 | import java.util.List; 20 | 21 | public interface InterpolationSolver { 22 | 23 | /** 24 | * computes interpolants for expressions. 25 | * 26 | * @param terms 27 | * @return 28 | */ 29 | public List> getInterpolants(List> terms); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/AbstractBoolExpression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions; 18 | 19 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 20 | import gov.nasa.jpf.constraints.types.Type; 21 | 22 | public abstract class AbstractBoolExpression extends 23 | AbstractExpression { 24 | 25 | @Override 26 | public Type getType() { 27 | return BuiltinTypes.BOOL; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/util/ExpressionSimplifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.util; 17 | 18 | import gov.nasa.jpf.constraints.api.Expression; 19 | 20 | @Deprecated 21 | public class ExpressionSimplifier { 22 | 23 | public ExpressionSimplifier() { 24 | } 25 | 26 | 27 | @SuppressWarnings({"rawtypes","unchecked"}) 28 | public Expression simplify(Expression e) { 29 | return ExpressionUtil.simplify(e); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/parser/UnknownTypeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.parser; 18 | 19 | import org.antlr.runtime.tree.Tree; 20 | 21 | public class UnknownTypeException extends ExpressionParseException { 22 | 23 | private static final long serialVersionUID = 1L; 24 | 25 | public UnknownTypeException(Tree node) { 26 | super(node.getLine(), node.getCharPositionInLine(), "Unknown type identifier " + node.getText()); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/parser/UndeclaredVariableException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.parser; 18 | 19 | import org.antlr.runtime.tree.Tree; 20 | 21 | public class UndeclaredVariableException extends ExpressionParseException { 22 | 23 | private static final long serialVersionUID = 1L; 24 | 25 | public UndeclaredVariableException(Tree n) { 26 | super(n.getLine(), n.getCharPositionInLine(), "Undeclared variable " + n.getText()); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/types/BVIntegerType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.types; 18 | 19 | 20 | public interface BVIntegerType extends IntegerType { 21 | 22 | public int getNumBits(); 23 | 24 | public T shiftLeft(T value, T shiftAmt); 25 | public T shiftRight(T value, T shiftAmt); 26 | public T shiftRightUnsigned(T value, T shiftAmt); 27 | 28 | public T not(T value); 29 | public T and(T left, T right); 30 | public T or(T left, T right); 31 | public T xor(T left, T right); 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/util/AbstractPrintable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.util; 18 | 19 | import java.io.IOException; 20 | 21 | public abstract class AbstractPrintable implements Printable { 22 | 23 | @Override 24 | public String toString() { 25 | try { 26 | StringBuilder sb = new StringBuilder(); 27 | print(sb); 28 | return sb.toString(); 29 | } 30 | catch(IOException ex) { 31 | throw new IllegalStateException("Unexpected IOException while writing to StringBuilder", ex); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/casts/ClassCastOperation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.casts; 18 | 19 | public class ClassCastOperation implements CastOperation { 20 | 21 | private final Class to; 22 | 23 | public ClassCastOperation(Class to) { 24 | this.to = to; 25 | } 26 | 27 | @Override 28 | public T cast(Object from) { 29 | return to.cast(from); 30 | } 31 | 32 | @Override 33 | public Class getFromClass() { 34 | return Object.class; 35 | } 36 | 37 | @Override 38 | public Class getToClass() { 39 | return to; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/types/NoSuchCastException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.types; 18 | 19 | public class NoSuchCastException extends IllegalArgumentException { 20 | private static final long serialVersionUID = 1L; 21 | 22 | public NoSuchCastException() { 23 | super(); 24 | } 25 | 26 | public NoSuchCastException(String message, Throwable cause) { 27 | super(message, cause); 28 | } 29 | 30 | public NoSuchCastException(String s) { 31 | super(s); 32 | } 33 | 34 | public NoSuchCastException(Throwable cause) { 35 | super(cause); 36 | } 37 | 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/exceptions/SolverCreationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.exceptions; 18 | 19 | public class SolverCreationException extends RuntimeException { 20 | private static final long serialVersionUID = 1L; 21 | 22 | public SolverCreationException() { 23 | } 24 | 25 | public SolverCreationException(String message) { 26 | super(message); 27 | } 28 | 29 | public SolverCreationException(Throwable cause) { 30 | super(cause); 31 | } 32 | 33 | public SolverCreationException(String message, Throwable cause) { 34 | super(message, cause); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/Quantifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.expressions; 17 | 18 | public enum Quantifier { 19 | FORALL("forall") { 20 | public Quantifier negate() { 21 | return EXISTS; 22 | } 23 | }, 24 | EXISTS("exists") { 25 | public Quantifier negate() { 26 | return FORALL; 27 | } 28 | }; 29 | 30 | private final String str; 31 | 32 | private Quantifier(String str) { 33 | this.str = str; 34 | } 35 | 36 | public abstract Quantifier negate(); 37 | 38 | @Override 39 | public String toString() { 40 | return str; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/types/ConcreteRealType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.types; 18 | 19 | import java.math.BigDecimal; 20 | 21 | public abstract class ConcreteRealType extends ConcreteNumericType implements 22 | RealType { 23 | 24 | public ConcreteRealType(String name, Class canonicalClass, T defaultValue, 25 | boolean signed, BigDecimal min, BigDecimal max, Type superType, 26 | String[] otherNames, Class... otherClasses) { 27 | super(name, canonicalClass, defaultValue, signed, min, max, superType, 28 | otherNames, otherClasses); 29 | } 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/parser/MalformedExpressionException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.parser; 18 | 19 | 20 | public class MalformedExpressionException extends IllegalArgumentException { 21 | 22 | private static final long serialVersionUID = 1L; 23 | 24 | public MalformedExpressionException() { 25 | } 26 | 27 | public MalformedExpressionException(String s) { 28 | super(s); 29 | } 30 | 31 | public MalformedExpressionException(Throwable cause) { 32 | super(cause); 33 | } 34 | 35 | public MalformedExpressionException(String message, Throwable cause) { 36 | super(message, cause); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/solvers/dontknow/DontKnowSolverProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.solvers.dontknow; 18 | 19 | import gov.nasa.jpf.constraints.api.ConstraintSolver; 20 | import gov.nasa.jpf.constraints.solvers.ConstraintSolverProvider; 21 | import java.util.Properties; 22 | 23 | public class DontKnowSolverProvider implements ConstraintSolverProvider { 24 | 25 | @Override 26 | public String[] getNames() { 27 | return new String[] {"dontknow"}; 28 | } 29 | 30 | @Override 31 | public ConstraintSolver createSolver(Properties config) { 32 | return new DontKnowSolver(); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/solvers/dontknow/DontKnowSolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.solvers.dontknow; 17 | 18 | import gov.nasa.jpf.constraints.api.ConstraintSolver; 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.SolverContext; 21 | import gov.nasa.jpf.constraints.api.Valuation; 22 | 23 | public class DontKnowSolver extends ConstraintSolver { 24 | 25 | @Override 26 | public Result solve(Expression f, Valuation result) { 27 | return Result.DONT_KNOW; 28 | } 29 | 30 | @Override 31 | public SolverContext createContext() { 32 | return new DontKnowSolverContext(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/types/NumericType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.types; 18 | 19 | import java.math.BigDecimal; 20 | 21 | 22 | public interface NumericType extends Type { 23 | 24 | public boolean isSigned(); 25 | 26 | public int compare(T left, T right); 27 | 28 | public BigDecimal decimalValue(T value); 29 | 30 | public BigDecimal getMin(); 31 | public BigDecimal getMax(); 32 | 33 | public T plus(T left, T right); 34 | public T minus(T left, T right); 35 | public T mul(T left, T right); 36 | public T div(T left, T right); 37 | public T mod(T left, T right); 38 | public T negate(T num); 39 | 40 | public String toPlainString(T value); 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/functions/math/axioms/FunctionProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions.functions.math.axioms; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.Variable; 21 | 22 | public interface FunctionProperties { 23 | 24 | public Expression[] getDefinition(); 25 | 26 | public Expression[] getDomainBounds(Expression ... fargs); 27 | 28 | public Expression[] getRangeBounds(); 29 | 30 | public Expression[] getRangeBounds(Variable retVale); 31 | 32 | public Expression[] getDefinition(Variable retValue, Expression ... fargs); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/types/Type.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.types; 18 | 19 | import gov.nasa.jpf.constraints.casts.CastOperation; 20 | 21 | public interface Type { 22 | 23 | public String getName(); 24 | 25 | public String[] getOtherNames(); 26 | 27 | public Class getCanonicalClass(); 28 | 29 | public Class[] getOtherClasses(); 30 | 31 | public T cast(Object other); 32 | 33 | public T getDefaultValue(); 34 | 35 | public Type getSuperType(); 36 | 37 | public CastOperation cast(Type fromType); 38 | public CastOperation requireCast(Type fromType); 39 | 40 | public abstract T parse(String string); 41 | 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/NumericOperator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions; 18 | 19 | /** 20 | * operator on numbers 21 | */ 22 | public enum NumericOperator { 23 | 24 | DIV("/"), 25 | MUL("*"), 26 | MINUS("-"), 27 | PLUS("+"), 28 | REM("%"); 29 | 30 | private final String str; 31 | 32 | NumericOperator(String str) { 33 | this.str = str; 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return str; 39 | } 40 | 41 | public static NumericOperator fromString(String str){ 42 | switch(str){ 43 | case "/": return DIV; 44 | case "*": return MUL; 45 | case "-": return MINUS; 46 | case "+": return PLUS; 47 | case "%": return REM; 48 | default: return null; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/types/ConcreteBVIntegerType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.types; 18 | 19 | import java.math.BigInteger; 20 | 21 | public abstract class ConcreteBVIntegerType extends ConcreteIntegerType implements BVIntegerType { 22 | 23 | private final int numBits; 24 | 25 | public ConcreteBVIntegerType(String name, Class canonicalClass, 26 | T defaultValue, int numBits, boolean signed, BigInteger min, BigInteger max, 27 | Type superType, String[] otherNames, Class ...otherClasses) { 28 | super(name, canonicalClass, defaultValue, signed, min, max, superType, 29 | otherNames, otherClasses); 30 | this.numBits = numBits; 31 | } 32 | 33 | 34 | @Override 35 | public int getNumBits() { 36 | return numBits; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/util/MixedParamsException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.util; 17 | 18 | public class MixedParamsException extends RuntimeException { 19 | 20 | private static final long serialVersionUID = 1L; 21 | 22 | public MixedParamsException() { 23 | } 24 | 25 | public MixedParamsException(String message) { 26 | super(message); 27 | } 28 | 29 | public MixedParamsException(Throwable cause) { 30 | super(cause); 31 | } 32 | 33 | public MixedParamsException(String message, Throwable cause) { 34 | super(message, cause); 35 | } 36 | 37 | public MixedParamsException(String message, Throwable cause, 38 | boolean enableSuppression, boolean writableStackTrace) { 39 | super(message, cause, enableSuppression, writableStackTrace); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/BitvectorOperator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions; 18 | 19 | public enum BitvectorOperator { 20 | 21 | AND("&"), 22 | OR("|"), 23 | XOR("^"), 24 | SHIFTL("<<"), 25 | SHIFTR(">>"), 26 | SHIFTUR(">>>"); 27 | 28 | 29 | private final String str; 30 | 31 | private BitvectorOperator(String str) { 32 | this.str = str; 33 | } 34 | 35 | public String toString() { 36 | return str; 37 | } 38 | 39 | public static BitvectorOperator fromString(String str){ 40 | switch(str){ 41 | case "&": return AND; 42 | case "|": return OR; 43 | case "^": return XOR; 44 | case "<<": return SHIFTL; 45 | case ">>": return SHIFTR; 46 | case ">>>": return SHIFTUR; 47 | default: return null; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/types/ConcreteFloatingPointType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.types; 18 | 19 | import java.math.BigDecimal; 20 | 21 | public abstract class ConcreteFloatingPointType extends ConcreteRealType implements 22 | FloatingPointType { 23 | 24 | private final int significantBits; 25 | 26 | public ConcreteFloatingPointType(String name, Class canonicalClass, 27 | T defaultValue, boolean signed, int significantBits, BigDecimal min, BigDecimal max, 28 | Type superType, String[] otherNames, Class... otherClasses) { 29 | super(name, canonicalClass, defaultValue, signed, min, max, superType, 30 | otherNames, otherClasses); 31 | this.significantBits = significantBits; 32 | } 33 | @Override 34 | public int getSignificantBits() { 35 | return significantBits; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/parser/ExpressionParseException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.parser; 18 | 19 | public class ExpressionParseException extends IllegalArgumentException { 20 | 21 | private static final long serialVersionUID = 1L; 22 | 23 | private static String createMessage(int line, int col, String message) { 24 | return "At line " + line + ", column " + col + ": " + message; 25 | } 26 | 27 | public ExpressionParseException(int line, int col, String message) { 28 | super(createMessage(line, col, message)); 29 | } 30 | 31 | public ExpressionParseException(int line, int col, Throwable cause) { 32 | this(line, col, cause.getMessage(), cause); 33 | } 34 | 35 | public ExpressionParseException(int line, int col, String message, Throwable cause) { 36 | super(createMessage(line, col, message), cause); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/solvers/dontknow/DontKnowSolverContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.solvers.dontknow; 17 | 18 | import gov.nasa.jpf.constraints.api.ConstraintSolver; 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.SolverContext; 21 | import gov.nasa.jpf.constraints.api.Valuation; 22 | import java.util.List; 23 | 24 | public class DontKnowSolverContext extends SolverContext { 25 | 26 | @Override 27 | public void push() { 28 | } 29 | 30 | @Override 31 | public void pop(int n) { 32 | } 33 | 34 | @Override 35 | public ConstraintSolver.Result solve(Valuation val) { 36 | return ConstraintSolver.Result.DONT_KNOW; 37 | } 38 | 39 | @Override 40 | public void add(List> expressions) { 41 | } 42 | 43 | @Override 44 | public void dispose() { 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/util/AddPrefixVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.util; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.Variable; 21 | import gov.nasa.jpf.constraints.types.Type; 22 | 23 | final class AddPrefixVisitor extends DuplicatingVisitor { 24 | 25 | public static final AddPrefixVisitor INSTANCE = new AddPrefixVisitor(); 26 | 27 | /* (non-Javadoc) 28 | * @see gov.nasa.jpf.constraints.api.AbstractExpressionVisitor#visit(gov.nasa.jpf.constraints.api.Variable, java.lang.Object) 29 | */ 30 | @Override 31 | public Expression visit(Variable v, 32 | String prefix) { 33 | return Variable.create(v.getType(), prefix + v.getName()); 34 | } 35 | 36 | public Expression apply(Expression expr, String prefix) { 37 | Type type = expr.getType(); 38 | return visit(expr, prefix).as(type); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/util/ExpressionRestrictor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.util; 17 | 18 | import gov.nasa.jpf.constraints.api.Expression; 19 | import gov.nasa.jpf.constraints.api.Variable; 20 | 21 | import java.util.Set; 22 | 23 | import com.google.common.base.Predicates; 24 | 25 | @Deprecated 26 | @SuppressWarnings({"rawtypes","unchecked"}) 27 | public class ExpressionRestrictor { 28 | 29 | private final Set variables; 30 | private boolean mixedParams = false; 31 | 32 | public ExpressionRestrictor(Set variables) { 33 | this.variables = variables; 34 | } 35 | 36 | 37 | public Expression restrict(Expression e) { 38 | Expression result = e; 39 | try { 40 | result = ExpressionUtil.restrict(result, Predicates.in(variables)); 41 | } 42 | catch(MixedParamsException ex) { 43 | mixedParams = true; 44 | } 45 | return result; 46 | } 47 | 48 | public boolean hasMixedParameters() { 49 | return mixedParams; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/util/StripPrefixVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.util; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.Variable; 21 | import gov.nasa.jpf.constraints.types.Type; 22 | 23 | final class StripPrefixVisitor extends DuplicatingVisitor { 24 | 25 | public static final StripPrefixVisitor INSTANCE = new StripPrefixVisitor(); 26 | 27 | /* (non-Javadoc) 28 | * @see gov.nasa.jpf.constraints.api.AbstractExpressionVisitor#visit(gov.nasa.jpf.constraints.api.Variable, java.lang.Object) 29 | */ 30 | @Override 31 | public Expression visit(Variable v, 32 | String prefix) { 33 | String name = v.getName(); 34 | if(!name.startsWith(prefix)) 35 | return v; 36 | return Variable.create(v.getType(), name.substring(prefix.length())); 37 | } 38 | 39 | public Expression apply(Expression expr, String prefix) { 40 | Type type = expr.getType(); 41 | return visit(expr, prefix).as(type); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/parser/UnexpectedTokenException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.parser; 18 | 19 | import org.antlr.runtime.tree.Tree; 20 | 21 | public class UnexpectedTokenException extends ExpressionParseException { 22 | 23 | private static final long serialVersionUID = 1L; 24 | 25 | private static String tokenNames(int ...expected) { 26 | StringBuilder sb = new StringBuilder(); 27 | boolean first = true; 28 | 29 | for(int i = 0; i < expected.length; i++) { 30 | int tokId = expected[i]; 31 | String tokName = ExpressionParser.tokenNames[tokId]; 32 | if(first) 33 | first = false; 34 | else 35 | sb.append(','); 36 | sb.append(tokName); 37 | } 38 | 39 | return sb.toString(); 40 | } 41 | 42 | public UnexpectedTokenException(Tree token, int ...expected) { 43 | super(token.getLine(), token.getCharPositionInLine(), "Encountered token " + ExpressionParser.tokenNames[token.getType()] 44 | + ", expected " + tokenNames(expected)); 45 | 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/util/DuplicatingVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.util; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.expressions.AbstractExpressionVisitor; 21 | 22 | public abstract class DuplicatingVisitor extends 23 | AbstractExpressionVisitor, D> { 24 | 25 | 26 | /* (non-Javadoc) 27 | * @see gov.nasa.jpf.constraints.api.AbstractExpressionVisitor#defaultVisit(gov.nasa.jpf.constraints.api.Expression, java.lang.Object) 28 | */ 29 | @Override 30 | protected Expression defaultVisit( 31 | Expression expression, D data) { 32 | Expression[] children = expression.getChildren(); 33 | boolean changed = false; 34 | for(int i = 0; i < children.length; i++) { 35 | Expression c = children[i]; 36 | Expression r = visit(c, data); 37 | if(c != r) 38 | changed = true; 39 | children[i] = r; 40 | } 41 | if(!changed) 42 | return expression; 43 | return expression.duplicate(children); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/util/RenameVarVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.util; 17 | 18 | import gov.nasa.jpf.constraints.api.Expression; 19 | import gov.nasa.jpf.constraints.api.Variable; 20 | 21 | import com.google.common.base.Function; 22 | 23 | class RenameVarVisitor extends 24 | DuplicatingVisitor> { 25 | 26 | private static final RenameVarVisitor INSTANCE = new RenameVarVisitor(); 27 | 28 | public static RenameVarVisitor getInstance() { 29 | return INSTANCE; 30 | } 31 | 32 | /* (non-Javadoc) 33 | * @see gov.nasa.jpf.constraints.expressions.AbstractExpressionVisitor#visit(gov.nasa.jpf.constraints.api.Variable, java.lang.Object) 34 | */ 35 | @Override 36 | public Expression visit(Variable v, Function data) { 37 | String newName = data.apply(v.getName()); 38 | return Variable.create(v.getType(), newName); 39 | } 40 | 41 | public Expression apply(Expression expr, Function rename) { 42 | return visit(expr, rename).requireAs(expr.getType()); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/functions/math/UnaryFloatFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.expressions.functions.math; 17 | 18 | import gov.nasa.jpf.constraints.expressions.functions.Function; 19 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 20 | 21 | public abstract class UnaryFloatFunction extends Function { 22 | 23 | public UnaryFloatFunction(String name) { 24 | super(name, BuiltinTypes.FLOAT, BuiltinTypes.FLOAT); 25 | } 26 | 27 | @Override 28 | public Float evaluate(Object... args) { 29 | if(args.length != 1) { 30 | throw new IllegalArgumentException("Error evaluating function '" + getName() 31 | + "': incorrect number of arguments: expected 1, got " + args.length); 32 | } 33 | 34 | Object arg = args[0]; 35 | if(!(arg instanceof Number)) { 36 | throw new IllegalArgumentException("Error evaluating function '" + getName() 37 | + "': expected numeric argument, got type " + arg.getClass()); 38 | } 39 | 40 | Number narg = (Number)arg; 41 | 42 | return doEvaluate(narg.floatValue()); 43 | } 44 | 45 | 46 | protected abstract float doEvaluate(float value); 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/util/TransformVarVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.util; 17 | 18 | import gov.nasa.jpf.constraints.api.Expression; 19 | import gov.nasa.jpf.constraints.api.Variable; 20 | 21 | import com.google.common.base.Function; 22 | 23 | class TransformVarVisitor extends 24 | DuplicatingVisitor, ? extends Expression>> { 25 | 26 | private static final TransformVarVisitor INSTANCE = new TransformVarVisitor(); 27 | 28 | public static TransformVarVisitor getInstance() { 29 | return INSTANCE; 30 | } 31 | 32 | /* (non-Javadoc) 33 | * @see gov.nasa.jpf.constraints.expressions.AbstractExpressionVisitor#visit(gov.nasa.jpf.constraints.api.Variable, java.lang.Object) 34 | */ 35 | @Override 36 | public Expression visit(Variable v, 37 | Function, ? extends Expression> data) { 38 | return data.apply(v); 39 | } 40 | 41 | 42 | public Expression apply(Expression expr, Function,? extends Expression> transform) { 43 | return visit(expr, transform).requireAs(expr.getType()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/functions/math/UnaryDoubleFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.expressions.functions.math; 17 | 18 | import gov.nasa.jpf.constraints.expressions.functions.Function; 19 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 20 | 21 | public abstract class UnaryDoubleFunction extends Function { 22 | 23 | public UnaryDoubleFunction(String name) { 24 | super(name, BuiltinTypes.DOUBLE, BuiltinTypes.DOUBLE); 25 | } 26 | 27 | @Override 28 | public Double evaluate(Object... args) { 29 | if(args.length != 1) { 30 | throw new IllegalArgumentException("Error evaluating function '" + getName() 31 | + "': incorrect number of arguments: expected 1, got " + args.length); 32 | } 33 | 34 | Object arg = args[0]; 35 | if(!(arg instanceof Number)) { 36 | throw new IllegalArgumentException("Error evaluating function '" + getName() 37 | + "': expected numeric argument, got type " + arg.getClass()); 38 | } 39 | 40 | Number narg = (Number)arg; 41 | 42 | return doEvaluate(narg.doubleValue()); 43 | } 44 | 45 | 46 | protected abstract double doEvaluate(double value); 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/types/ConcreteNumericType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.types; 18 | 19 | import java.math.BigDecimal; 20 | 21 | public abstract class ConcreteNumericType extends ConcreteType implements NumericType { 22 | 23 | private final boolean signed; 24 | private final BigDecimal min; 25 | private final BigDecimal max; 26 | 27 | public ConcreteNumericType(String name, Class canonicalClass, 28 | T defaultValue, boolean signed, BigDecimal min, BigDecimal max, Type superType, String[] otherNames, 29 | Class ...otherClasses) { 30 | super(name, canonicalClass, defaultValue, superType, otherNames, otherClasses); 31 | this.signed = signed; 32 | this.min = min; 33 | this.max = max; 34 | } 35 | 36 | 37 | @Override 38 | public boolean isSigned() { 39 | return signed; 40 | } 41 | 42 | 43 | @Override 44 | public BigDecimal getMin() { 45 | return min; 46 | } 47 | 48 | @Override 49 | public BigDecimal getMax() { 50 | return max; 51 | } 52 | 53 | @Override 54 | public String toPlainString(T value) { 55 | return value.toString(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/AbstractExpression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.types.Type; 21 | 22 | public abstract class AbstractExpression 23 | extends Expression { 24 | 25 | 26 | protected static boolean identical(Expression[] newChildren, Expression thisChild) { 27 | if(newChildren.length != 1) 28 | return false; 29 | return thisChild == newChildren[0]; 30 | } 31 | 32 | protected static boolean identical(Expression[] newChildren, Expression ...thisChildren) { 33 | if(newChildren.length != thisChildren.length) 34 | return false; 35 | for(int i = 0; i < thisChildren.length; i++) { 36 | if(newChildren[i] != thisChildren[i]) 37 | return false; 38 | } 39 | return true; 40 | } 41 | 42 | @SuppressWarnings("unchecked") 43 | protected static Expression[] requireAs(Expression[] expressions, Type type) { 44 | for(int i = 0; i < expressions.length; i++) 45 | expressions[i].requireAs(type); 46 | return (Expression[])expressions; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/functions/math/UnaryIntegerFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.expressions.functions.math; 17 | 18 | import gov.nasa.jpf.constraints.expressions.functions.Function; 19 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 20 | 21 | /** 22 | * 23 | */ 24 | public abstract class UnaryIntegerFunction extends Function { 25 | 26 | public UnaryIntegerFunction(String name) { 27 | super(name, BuiltinTypes.SINT32, BuiltinTypes.SINT32); 28 | } 29 | 30 | @Override 31 | public Integer evaluate(Object... args) { 32 | if(args.length != 1) { 33 | throw new IllegalArgumentException("Error evaluating function '" + getName() 34 | + "': incorrect number of arguments: expected 1, got " + args.length); 35 | } 36 | 37 | Object arg = args[0]; 38 | if(!(arg instanceof Number)) { 39 | throw new IllegalArgumentException("Error evaluating function '" + getName() 40 | + "': expected numeric argument, got type " + arg.getClass()); 41 | } 42 | 43 | Number narg = (Number)arg; 44 | 45 | return doEvaluate(narg.intValue()); 46 | } 47 | 48 | 49 | protected abstract int doEvaluate(int value); 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/types/ConcreteIntegerType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.types; 18 | 19 | import java.math.BigDecimal; 20 | import java.math.BigInteger; 21 | 22 | public abstract class ConcreteIntegerType extends ConcreteNumericType implements IntegerType { 23 | 24 | private final BigInteger min; 25 | private final BigInteger max; 26 | 27 | public ConcreteIntegerType(String name, Class canonicalClass, 28 | T defaultValue, boolean signed, BigInteger min, BigInteger max, 29 | Type superType, String[] otherNames, Class ...otherClasses) { 30 | super(name, canonicalClass, defaultValue, signed, (min != null) ? new BigDecimal(min) : null, (max != null) ? new BigDecimal(max) : null, superType, 31 | otherNames, otherClasses); 32 | this.min = min; 33 | this.max = max; 34 | } 35 | 36 | 37 | @Override 38 | public BigInteger getMinInt() { 39 | return min; 40 | } 41 | 42 | @Override 43 | public BigInteger getMaxInt() { 44 | return max; 45 | } 46 | 47 | 48 | @Override 49 | public BigDecimal decimalValue(T value) { 50 | return new BigDecimal(integerValue(value)); 51 | } 52 | 53 | 54 | 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/LogicalOperator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions; 18 | 19 | /** 20 | * operator for logic formulas 21 | */ 22 | public enum LogicalOperator { 23 | 24 | AND("&&") { public boolean eval(boolean lv, boolean rv) { return lv && rv; } }, 25 | OR("||") { public boolean eval(boolean lv, boolean rv) { return lv || rv; } }, 26 | IMPLY("->") { public boolean eval(boolean lv, boolean rv) { return !lv || rv; } }, 27 | EQUIV("<->") { public boolean eval(boolean lv, boolean rv) { return lv == rv; } }, 28 | XOR("^") { public boolean eval(boolean lv, boolean rv) { return lv ^ rv; } }; 29 | 30 | private final String str; 31 | 32 | private LogicalOperator(final String str) { 33 | this.str = str; 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return str; 39 | } 40 | 41 | public static LogicalOperator fromString(String str){ 42 | switch(str){ 43 | case "&&": return AND; 44 | case "||": return OR; 45 | case "->": return IMPLY; 46 | case "<->": return EQUIV; 47 | case "^": return XOR; 48 | default: return null; 49 | } 50 | } 51 | 52 | public abstract boolean eval(boolean lv, boolean rv); 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/gov/nasa/jpf/constraints/expressions/IfThenElseTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.expressions; 17 | 18 | import gov.nasa.jpf.constraints.api.Valuation; 19 | import gov.nasa.jpf.constraints.api.Variable; 20 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 21 | import java.math.BigInteger; 22 | import org.testng.annotations.Test; 23 | 24 | /** 25 | * 26 | */ 27 | @Test 28 | public class IfThenElseTest { 29 | 30 | @Test 31 | public void testIfThenElse() { 32 | 33 | Variable x = new Variable(BuiltinTypes.BOOL, "x"); 34 | Variable y = new Variable(BuiltinTypes.SINT32, "y"); 35 | Constant c = new Constant(BuiltinTypes.SINT32, 3); 36 | 37 | IfThenElse ite = new IfThenElse(x, 38 | new NumericCompound<>(y, NumericOperator.PLUS, c), 39 | new NumericCompound<>(y, NumericOperator.MINUS, c)); 40 | 41 | System.out.println(ite); 42 | 43 | Valuation val = new Valuation(); 44 | val.setValue(y, 10); 45 | 46 | val.setValue(x, true); 47 | System.out.println(val + ": " + ite.evaluate(val)); 48 | 49 | val.setValue(x, false); 50 | System.out.println(val + ": " + ite.evaluate(val)); 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/gov/nasa/jpf/constraints/expressions/functions/math/IntegerFunctionTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | /* 17 | * To change this license header, choose License Headers in Project Properties. 18 | * To change this template file, choose Tools | Templates 19 | * and open the template in the editor. 20 | */ 21 | 22 | package gov.nasa.jpf.constraints.expressions.functions.math; 23 | 24 | import static org.testng.Assert.*; 25 | import org.testng.annotations.AfterClass; 26 | import org.testng.annotations.AfterMethod; 27 | import org.testng.annotations.BeforeClass; 28 | import org.testng.annotations.BeforeMethod; 29 | import org.testng.annotations.Test; 30 | 31 | /** 32 | * 33 | */ 34 | public class IntegerFunctionTest { 35 | 36 | public IntegerFunctionTest() { 37 | } 38 | 39 | @Test 40 | public void testIntegerFunction() { 41 | System.out.println(MathFunctions.IABS.doEvaluate(-10)); 42 | } 43 | 44 | @BeforeClass 45 | public static void setUpClass() throws Exception { 46 | } 47 | 48 | @AfterClass 49 | public static void tearDownClass() throws Exception { 50 | } 51 | 52 | @BeforeMethod 53 | public void setUpMethod() throws Exception { 54 | } 55 | 56 | @AfterMethod 57 | public void tearDownMethod() throws Exception { 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/functions/math/UnaryDoubleLongFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.expressions.functions.math; 17 | 18 | import gov.nasa.jpf.constraints.expressions.functions.Function; 19 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 20 | 21 | public abstract class UnaryDoubleLongFunction extends Function { 22 | 23 | public UnaryDoubleLongFunction(String name) { 24 | super(name, BuiltinTypes.SINT64, BuiltinTypes.DOUBLE); 25 | } 26 | 27 | @Override 28 | public Long evaluate(Object... args) { 29 | if(args.length != 1) { 30 | throw new IllegalArgumentException("Error evaluating function '" + getName() 31 | + "': incorrect number of arguments: expected 1, got " + args.length); 32 | } 33 | 34 | Object arg = args[0]; 35 | if(!(arg instanceof Number)) { 36 | throw new IllegalArgumentException("Error evaluating function '" + getName() 37 | + "': expected numeric argument, got type " + arg.getClass()); 38 | } 39 | 40 | Number narg = (Number)arg; 41 | 42 | return doEvaluate(narg.doubleValue()); 43 | } 44 | 45 | 46 | protected abstract long doEvaluate(double value); 47 | 48 | 49 | public static UnaryDoubleLongFunction DOUBLE_TO_LONG_BITS = new UnaryDoubleLongFunction("java.lang.Double.doubleToLongBits") { 50 | @Override 51 | protected long doEvaluate(double value) { 52 | return Double.doubleToLongBits(value); 53 | } 54 | }; 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/test/java/gov/nasa/jpf/constraints/expressions/PrintTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package gov.nasa.jpf.constraints.expressions; 7 | 8 | import gov.nasa.jpf.constraints.api.Expression; 9 | import gov.nasa.jpf.constraints.api.Variable; 10 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 11 | import java.io.IOException; 12 | import java.util.logging.Level; 13 | import java.util.logging.Logger; 14 | import static org.testng.Assert.assertTrue; 15 | import org.testng.annotations.BeforeSuite; 16 | import org.testng.annotations.BeforeTest; 17 | import org.testng.annotations.Test; 18 | 19 | @Test 20 | public class PrintTest { 21 | 22 | Expression exprUnderTest; 23 | @BeforeTest 24 | public void setupExpression(){ 25 | Variable var1 = Variable.create(BuiltinTypes.SINT32, "X"); 26 | Variable var2 = Variable.create(BuiltinTypes.SINT32, "Y"); 27 | Constant c1 = Constant.create(BuiltinTypes.SINT32, 5); 28 | Constant c2 = Constant.create(BuiltinTypes.SINT32, 8); 29 | 30 | NumericCompound compound1 = 31 | new NumericCompound(var1, NumericOperator.PLUS, c1); 32 | NumericCompound compound2 = 33 | new NumericCompound(var1, NumericOperator.MINUS, c2); 34 | NumericBooleanExpression bool1 = 35 | new NumericBooleanExpression(var2, NumericComparator.EQ, null); 36 | NumericBooleanExpression bool2 = 37 | new NumericBooleanExpression(null, NumericComparator.EQ, c2); 38 | PropositionalCompound compound3 = 39 | new PropositionalCompound(bool1, LogicalOperator.OR, bool2); 40 | exprUnderTest = compound3; 41 | } 42 | @Test 43 | public void testMalformedPrint() { 44 | StringBuilder builder = new StringBuilder(); 45 | try { 46 | exprUnderTest.printMalformedExpression(builder); 47 | } catch (IOException ex) { 48 | } 49 | String result = builder.toString(); 50 | assertTrue(result.contains("null")); 51 | } 52 | 53 | @Test(expectedExceptions = {NullPointerException.class}) 54 | public void testPrint(){ 55 | StringBuilder builder = new StringBuilder(); 56 | try{ 57 | exprUnderTest.print(builder); 58 | }catch(IOException e){ 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/functions/math/BinaryDoubleFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.expressions.functions.math; 17 | 18 | import gov.nasa.jpf.constraints.expressions.functions.Function; 19 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 20 | 21 | public abstract class BinaryDoubleFunction extends Function { 22 | 23 | public BinaryDoubleFunction(String name) { 24 | super(name, BuiltinTypes.DOUBLE, BuiltinTypes.DOUBLE, BuiltinTypes.DOUBLE); 25 | } 26 | 27 | @Override 28 | public Double evaluate(Object... args) { 29 | if(args.length != 2) { 30 | throw new IllegalArgumentException("Error evaluating function '" + getName() 31 | + "': incorrect number of arguments: expected 1, got " + args.length); 32 | } 33 | 34 | Object arg1 = args[0]; 35 | if(!(arg1 instanceof Number)) { 36 | throw new IllegalArgumentException("Error evaluating function '" + getName() 37 | + "': expected numeric argument, got type " + arg1.getClass()); 38 | } 39 | 40 | Object arg2 = args[1]; 41 | if(!(arg2 instanceof Number)) { 42 | throw new IllegalArgumentException("Error evaluating function '" + getName() 43 | + "': expected numeric argument, got type " + arg2.getClass()); 44 | } 45 | 46 | Number narg1 = (Number)arg1; 47 | Number narg2 = (Number)arg2; 48 | 49 | return doEvaluate(narg1.doubleValue(), narg2.doubleValue()); 50 | } 51 | 52 | 53 | protected abstract double doEvaluate(double v1, double v2); 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/util/ContainsVarsVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.util; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.Variable; 21 | import gov.nasa.jpf.constraints.expressions.AbstractExpressionVisitor; 22 | 23 | public class ContainsVarsVisitor extends 24 | AbstractExpressionVisitor { 25 | 26 | private static ContainsVarsVisitor INSTANCE = new ContainsVarsVisitor(); 27 | 28 | public static ContainsVarsVisitor getInstance() { 29 | return INSTANCE; 30 | } 31 | 32 | 33 | protected ContainsVarsVisitor() { 34 | } 35 | 36 | /* (non-Javadoc) 37 | * @see gov.nasa.jpf.constraints.expressions.AbstractExpressionVisitor#visit(gov.nasa.jpf.constraints.api.Variable, java.lang.Object) 38 | */ 39 | @Override 40 | public Boolean visit(Variable v, Void data) { 41 | return Boolean.TRUE; 42 | } 43 | 44 | /* (non-Javadoc) 45 | * @see gov.nasa.jpf.constraints.expressions.AbstractExpressionVisitor#defaultVisit(gov.nasa.jpf.constraints.api.Expression, java.lang.Object) 46 | */ 47 | @Override 48 | protected Boolean defaultVisit(Expression expression, Void data) { 49 | Expression[] children = expression.getChildren(); 50 | 51 | for(int i = 0; i < children.length; i++) { 52 | if(visit(children[i], null)) 53 | return Boolean.TRUE; 54 | } 55 | 56 | return Boolean.FALSE; 57 | } 58 | 59 | public boolean apply(Expression exp) { 60 | return visit(exp, null).booleanValue(); 61 | } 62 | 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/functions/Function.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.expressions.functions; 17 | 18 | import gov.nasa.jpf.constraints.api.Expression; 19 | import gov.nasa.jpf.constraints.types.Type; 20 | import gov.nasa.jpf.constraints.util.AbstractPrintable; 21 | 22 | import java.io.IOException; 23 | 24 | import com.google.common.base.Joiner; 25 | 26 | public class Function extends AbstractPrintable { 27 | 28 | private final String name; 29 | private final Type returnType; 30 | private final Type[] paramTypes; 31 | 32 | @SafeVarargs 33 | public Function(String name, Type returnType, Type ...paramTypes) { 34 | this.name = name; 35 | this.returnType = returnType; 36 | this.paramTypes = paramTypes; 37 | } 38 | 39 | 40 | public String getName() { 41 | return name; 42 | } 43 | 44 | public Type getReturnType() { 45 | return returnType; 46 | } 47 | 48 | public Type[] getParamTypes() { 49 | return paramTypes; 50 | } 51 | 52 | public FunctionExpression toExpression(Expression ...args) { 53 | return new FunctionExpression<>(this, args); 54 | } 55 | 56 | public T evaluate(Object... args) { 57 | throw new UnsupportedOperationException("Semantics for function '" + name + "' undefined"); 58 | } 59 | 60 | public int getArity() { 61 | return paramTypes.length; 62 | } 63 | 64 | 65 | @Override 66 | public void print(Appendable a) throws IOException { 67 | a.append(name).append('('); 68 | Joiner.on(',').appendTo(a, paramTypes); 69 | a.append("):").append(returnType.toString()); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/api/SolverContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.api; 18 | 19 | import gov.nasa.jpf.constraints.api.ConstraintSolver.Result; 20 | 21 | import java.util.Arrays; 22 | import java.util.List; 23 | 24 | /** 25 | * Solver context to support incremental solving (i.e., with backtracking). 26 | */ 27 | public abstract class SolverContext { 28 | 29 | public abstract void push(); 30 | 31 | 32 | public abstract void pop(int n); 33 | 34 | public void pop() { 35 | pop(1); 36 | } 37 | 38 | 39 | public Result isSatisfiable() { 40 | return solve(null); 41 | } 42 | 43 | public abstract Result solve(Valuation val); 44 | 45 | @SafeVarargs 46 | public final void add(Expression ...expressions) { 47 | add(Arrays.asList(expressions)); 48 | } 49 | 50 | public abstract void add(List> expressions); 51 | 52 | 53 | public Result isSatisfiable(Expression expr) { 54 | push(); 55 | add(expr); 56 | Result res = isSatisfiable(); 57 | pop(); 58 | return res; 59 | } 60 | 61 | public Result solve(Expression expr, Valuation val) { 62 | push(); 63 | add(expr); 64 | Result res = solve(val); 65 | pop(); 66 | return res; 67 | } 68 | 69 | public Result[] whichSatisfiable(List> expressions) { 70 | Result[] results = new Result[expressions.size()]; 71 | int i = 0; 72 | for(Expression expr : expressions) { 73 | push(); 74 | add(expr); 75 | Result res = isSatisfiable(); 76 | pop(); 77 | results[i++] = res; 78 | } 79 | return results; 80 | } 81 | 82 | public abstract void dispose(); 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/NumericComparator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions; 18 | 19 | /** 20 | * numeric comparator 21 | */ 22 | public enum NumericComparator { 23 | 24 | EQ("==") { public NumericComparator not() { return NE; } public boolean eval(int cmpResult) { return (cmpResult == 0); }}, 25 | NE("!=") { public NumericComparator not() { return EQ; } public boolean eval(int cmpResult) { return (cmpResult != 0); }}, 26 | LT("<") { public NumericComparator not() { return GE; } public boolean eval(int cmpResult) { return (cmpResult < 0); }}, 27 | LE("<=") { public NumericComparator not() { return GT; } public boolean eval(int cmpResult) { return (cmpResult <= 0); }}, 28 | GT(">") { public NumericComparator not() { return LE; } public boolean eval(int cmpResult) { return (cmpResult > 0); }}, 29 | GE(">=") { public NumericComparator not() { return LT; } public boolean eval(int cmpResult) { return (cmpResult >= 0); }}; 30 | 31 | private final String str; 32 | 33 | private NumericComparator(String str){ 34 | this.str = str; 35 | } 36 | 37 | public abstract NumericComparator not(); 38 | 39 | public abstract boolean eval(int cmpResult); 40 | 41 | @Override 42 | public String toString() { 43 | return str; 44 | } 45 | 46 | public static NumericComparator fromString(String str){ 47 | switch(str){ 48 | case "==": return EQ; 49 | case "!=": return NE; 50 | case "<": return LT; 51 | case "<=": return LE; 52 | case ">": return GT; 53 | case ">=": return GE; 54 | default: return null; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/functions/math/BooleanDoubleFunction.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.expressions.functions.math; 17 | 18 | import gov.nasa.jpf.constraints.expressions.functions.Function; 19 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 20 | 21 | public abstract class BooleanDoubleFunction extends Function { 22 | 23 | public BooleanDoubleFunction(String name) { 24 | super(name, BuiltinTypes.BOOL, BuiltinTypes.DOUBLE); 25 | } 26 | 27 | @Override 28 | public Boolean evaluate(Object... args) { 29 | if(args.length != 1) { 30 | throw new IllegalArgumentException("Error evaluating function '" + getName() 31 | + "': incorrect number of arguments: expected 1, got " + args.length); 32 | } 33 | 34 | Object arg = args[0]; 35 | if(!(arg instanceof Number)) { 36 | throw new IllegalArgumentException("Error evaluating function '" + getName() 37 | + "': expected numeric argument, got type " + arg.getClass()); 38 | } 39 | 40 | Number narg = (Number)arg; 41 | 42 | return doEvaluate(narg.doubleValue()); 43 | } 44 | 45 | 46 | protected abstract boolean doEvaluate(double value); 47 | 48 | 49 | public static BooleanDoubleFunction DOUBLE_IS_INFINITE = new BooleanDoubleFunction("java.lang.Double.isInfinite") { 50 | @Override 51 | protected boolean doEvaluate(double value) { 52 | return Double.isInfinite(value); 53 | } 54 | }; 55 | 56 | public static BooleanDoubleFunction DOUBLE_IS_NAN = new BooleanDoubleFunction("java.lang.Double.isNaN") { 57 | @Override 58 | protected boolean doEvaluate(double value) { 59 | return Double.isNaN(value); 60 | } 61 | }; 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/functions/math/axioms/IsNaNProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions.functions.math.axioms; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.Variable; 21 | import gov.nasa.jpf.constraints.expressions.Negation; 22 | import gov.nasa.jpf.constraints.expressions.functions.FunctionExpression; 23 | import gov.nasa.jpf.constraints.expressions.functions.math.BooleanDoubleFunction; 24 | import static gov.nasa.jpf.constraints.expressions.functions.math.axioms.PropertyBuilder.*; 25 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 26 | import gov.nasa.jpf.constraints.util.ExpressionUtil; 27 | 28 | public class IsNaNProperties implements FunctionProperties { 29 | 30 | @Override 31 | public Expression[] getDefinition() { 32 | 33 | Variable arg = var(BuiltinTypes.DOUBLE); 34 | FunctionExpression fexpr = new FunctionExpression( 35 | BooleanDoubleFunction.DOUBLE_IS_NAN, arg); 36 | 37 | return array( forall(new Negation(fexpr), arg) ); 38 | } 39 | 40 | @Override 41 | public Expression[] getRangeBounds() { 42 | return array(); 43 | } 44 | 45 | @Override 46 | public Expression[] getDomainBounds(Expression... fargs) { 47 | return array(); 48 | } 49 | 50 | @Override 51 | public Expression[] getDefinition(Variable retValue, Expression... fargs) { 52 | return array(new Negation(retValue)); 53 | } 54 | 55 | @Override 56 | public Expression[] getRangeBounds(Variable retVal) { 57 | return array(new Negation(retVal)); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/EqualityExpression.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.expressions; 17 | 18 | import gov.nasa.jpf.constraints.api.Expression; 19 | import gov.nasa.jpf.constraints.api.ExpressionVisitor; 20 | import gov.nasa.jpf.constraints.api.Valuation; 21 | import gov.nasa.jpf.constraints.api.Variable; 22 | 23 | import java.io.IOException; 24 | import java.util.Collection; 25 | 26 | /** 27 | * This class should be implemented since equality is a more general concept than numeric comparison 28 | * (e.g., applies to boolean values also). 29 | */ 30 | public class EqualityExpression extends AbstractBoolExpression { 31 | 32 | @Override 33 | public Boolean evaluate(Valuation values) { 34 | // TODO Auto-generated method stub 35 | return null; 36 | } 37 | 38 | @Override 39 | public void collectFreeVariables(Collection> variables) { 40 | // TODO Auto-generated method stub 41 | 42 | } 43 | 44 | @Override 45 | public R accept(ExpressionVisitor visitor, D data) { 46 | // TODO Auto-generated method stub 47 | return null; 48 | } 49 | 50 | @Override 51 | public Expression[] getChildren() { 52 | // TODO Auto-generated method stub 53 | return null; 54 | } 55 | 56 | @Override 57 | public Expression duplicate(Expression[] newChildren) { 58 | // TODO Auto-generated method stub 59 | return null; 60 | } 61 | 62 | @Override 63 | public void print(Appendable a, int flags) throws IOException { 64 | // TODO Auto-generated method stub 65 | } 66 | 67 | @Override 68 | public void printMalformedExpression(Appendable a, int flags) 69 | throws IOException { 70 | // TODO Auto-generated method stub 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/api/ExpressionVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.api; 18 | 19 | import gov.nasa.jpf.constraints.expressions.BitvectorExpression; 20 | import gov.nasa.jpf.constraints.expressions.BitvectorNegation; 21 | import gov.nasa.jpf.constraints.expressions.CastExpression; 22 | import gov.nasa.jpf.constraints.expressions.Constant; 23 | import gov.nasa.jpf.constraints.expressions.IfThenElse; 24 | import gov.nasa.jpf.constraints.expressions.Negation; 25 | import gov.nasa.jpf.constraints.expressions.NumericBooleanExpression; 26 | import gov.nasa.jpf.constraints.expressions.NumericCompound; 27 | import gov.nasa.jpf.constraints.expressions.PropositionalCompound; 28 | import gov.nasa.jpf.constraints.expressions.QuantifierExpression; 29 | import gov.nasa.jpf.constraints.expressions.UnaryMinus; 30 | import gov.nasa.jpf.constraints.expressions.functions.FunctionExpression; 31 | 32 | public interface ExpressionVisitor { 33 | 34 | public R visit(Variable v, D data); 35 | 36 | public R visit(Constant c, D data); 37 | 38 | public R visit(Negation n, D data); 39 | 40 | public R visit(NumericBooleanExpression n, D data); 41 | 42 | public R visit( 43 | CastExpression cast, D data); 44 | 45 | public R visit(NumericCompound n, D data); 46 | 47 | public R visit(PropositionalCompound n, D data); 48 | 49 | public R visit(IfThenElse n, D data); 50 | 51 | public R visit( 52 | UnaryMinus n, D data); 53 | 54 | public R visit( 55 | BitvectorExpression bv, D data); 56 | 57 | public R visit( 58 | BitvectorNegation n, D data); 59 | 60 | public R visit(QuantifierExpression q, D data); 61 | 62 | public R visit(FunctionExpression f, D data); 63 | 64 | } -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/functions/math/axioms/AcosProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions.functions.math.axioms; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.Variable; 21 | import gov.nasa.jpf.constraints.expressions.functions.math.MathFunctions; 22 | import static gov.nasa.jpf.constraints.expressions.functions.math.axioms.PropertyBuilder.*; 23 | import gov.nasa.jpf.constraints.util.ExpressionUtil; 24 | 25 | public class AcosProperties implements FunctionProperties { 26 | 27 | @Override 28 | public Expression[] getDefinition() { 29 | Variable arg = doubleVar(); 30 | 31 | Expression eval = minus(constant(Math.PI * 0.5), fexpr(MathFunctions.ASIN, arg)); 32 | Expression acos = forall( eq(fexpr(MathFunctions.ACOS, arg), eval), arg); 33 | return array(acos); 34 | } 35 | 36 | 37 | @Override 38 | public Expression[] getRangeBounds() { 39 | // FIXME: the computed value for 1.0 is not within these bounds!!! 40 | return array(bounds(MathFunctions.ACOS, constant(0.0), constant(Math.PI), false)); 41 | } 42 | 43 | @Override 44 | public Expression[] getDomainBounds(Expression... fargs) { 45 | return array(bounds(fargs[0], -1.0, 1.0, false, false)); 46 | } 47 | 48 | @Override 49 | public Expression[] getDefinition(Variable retValue, Expression... fargs) { 50 | throw new UnsupportedOperationException("Not supported yet."); 51 | } 52 | 53 | @Override 54 | public Expression[] getRangeBounds(Variable retVal) { 55 | return array(bounds(retVal, 0.0, Math.PI, false, false)); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/util/NestingDepthVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.util; 17 | 18 | import gov.nasa.jpf.constraints.api.Expression; 19 | import gov.nasa.jpf.constraints.expressions.AbstractExpressionVisitor; 20 | import gov.nasa.jpf.constraints.expressions.functions.FunctionExpression; 21 | 22 | public class NestingDepthVisitor extends 23 | AbstractExpressionVisitor { 24 | 25 | private static final NestingDepthVisitor INSTANCE = new NestingDepthVisitor(); 26 | 27 | public static NestingDepthVisitor getInstance() { 28 | return INSTANCE; 29 | } 30 | 31 | 32 | 33 | @SafeVarargs 34 | private final int maxDepth(Expression ...children) { 35 | if(children.length == 0) 36 | return 0; 37 | 38 | int maxDepth = children[0].accept(this, null); 39 | for(int i = 1; i < children.length; i++) { 40 | int d = visit(children[i], null); 41 | if(d > maxDepth) 42 | maxDepth = d; 43 | } 44 | return maxDepth; 45 | } 46 | 47 | /* (non-Javadoc) 48 | * @see gov.nasa.jpf.constraints.expressions.AbstractExpressionVisitor#visit(gov.nasa.jpf.constraints.expressions.functions.FunctionExpression, java.lang.Object) 49 | */ 50 | @Override 51 | public Integer visit(FunctionExpression f, Void data) { 52 | return 1 + maxDepth(f.getChildren()); 53 | } 54 | 55 | /* (non-Javadoc) 56 | * @see gov.nasa.jpf.constraints.expressions.AbstractExpressionVisitor#defaultVisit(gov.nasa.jpf.constraints.api.Expression, java.lang.Object) 57 | */ 58 | @Override 59 | protected Integer defaultVisit(Expression expression, Void data) { 60 | return maxDepth(expression.getChildren()); 61 | } 62 | 63 | 64 | public int apply(Expression expr) { 65 | return visit(expr, null); 66 | } 67 | 68 | 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/api/ConstraintSolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.api; 18 | 19 | 20 | /** 21 | * Abstract superclass for constraint solvers. 22 | */ 23 | public abstract class ConstraintSolver { 24 | 25 | /** 26 | * result returned by a constraint solver 27 | * 28 | */ 29 | public static enum Result {SAT,UNSAT,DONT_KNOW}; 30 | 31 | /** 32 | * runs f by the constraint solver and returns if 33 | * f can be satisfied 34 | * 35 | * @param f formula to check for satisfiability 36 | * @return the satisfiability {@link Result} 37 | */ 38 | public Result isSatisfiable(Expression f) { 39 | return solve(f, new Valuation()); 40 | } 41 | 42 | 43 | /** 44 | * solves f and returns a satisfying solution 45 | * 46 | * @param f 47 | * @param result solution that satisfies f or null in case of only checking for satisfiability 48 | * @return the satisfiability {@link Result} 49 | */ 50 | public abstract Result solve(Expression f, Valuation result); 51 | 52 | /** 53 | * Create a solver context, which allows for incremental solving (i.e., via push and pop). 54 | * This is an optional operation. 55 | * @return a solver context 56 | * @throws UnsupportedOperationException if the solver engine does not incremental solving 57 | */ 58 | public SolverContext createContext() { 59 | throw new UnsupportedOperationException("Solver does not support incremental solving"); 60 | } 61 | 62 | 63 | // LEGACY API 64 | 65 | @Deprecated 66 | public Result isSatisfiable(Expression f, MinMax m) { 67 | return solve(f, m, new Valuation()); 68 | } 69 | 70 | @Deprecated 71 | public Result solve(Expression f, MinMax m, Valuation val) { 72 | //Expression mod = m.apply(f); 73 | return solve(f, val); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/functions/math/axioms/PowProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions.functions.math.axioms; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.Variable; 21 | import gov.nasa.jpf.constraints.expressions.functions.math.MathFunctions; 22 | import static gov.nasa.jpf.constraints.expressions.functions.math.axioms.PropertyBuilder.*; 23 | 24 | public class PowProperties implements FunctionProperties { 25 | 26 | private Expression pow() { 27 | Variable base = doubleVar(); 28 | Variable exp = doubleVar(); 29 | 30 | Expression thenExpr = mul(base, base); 31 | Expression elseExpr = constant(-1.0); 32 | Expression ifCond = eq(exp, constant(2.0)); 33 | 34 | elseExpr = ite(ifCond, thenExpr, elseExpr); 35 | ifCond = eq(exp, constant(1.0)); 36 | thenExpr = base; 37 | 38 | elseExpr = ite(ifCond, thenExpr, elseExpr); 39 | ifCond = eq(exp, constant(0.0)); 40 | thenExpr = constant(1.0); 41 | 42 | return forall(eq(fexpr(MathFunctions.POW, base, exp) , ite(ifCond, thenExpr, elseExpr)), base, exp); 43 | } 44 | 45 | @Override 46 | public Expression[] getDefinition() { 47 | return array(pow()); 48 | } 49 | 50 | @Override 51 | public Expression[] getRangeBounds() { 52 | return new Expression[] {}; 53 | } 54 | 55 | @Override 56 | public Expression[] getDomainBounds(Expression... fargs) { 57 | return new Expression[] {}; 58 | } 59 | 60 | @Override 61 | public Expression[] getDefinition(Variable retValue, Expression... fargs) { 62 | throw new UnsupportedOperationException("Not supported yet."); 63 | } 64 | 65 | @Override 66 | public Expression[] getRangeBounds(Variable retVale) { 67 | throw new UnsupportedOperationException("Not supported yet."); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/types/TypeForest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.types; 18 | 19 | import java.util.HashMap; 20 | import java.util.HashSet; 21 | import java.util.Map; 22 | import java.util.Set; 23 | 24 | class TypeForest { 25 | 26 | private static final Map,Node> knownTypes 27 | = new HashMap,Node>(); 28 | private static final Set> pending 29 | = new HashSet>(); 30 | 31 | public static class Node { 32 | private final Type type; 33 | private final Node parent; 34 | private final int level; 35 | 36 | public Node(Type type, Node parent) { 37 | this.type = type; 38 | this.parent = parent; 39 | this.level = (parent == null) ? 0 : parent.getLevel() + 1; 40 | } 41 | 42 | public int getLevel() { 43 | return level; 44 | } 45 | 46 | public Node getParent() { 47 | return parent; 48 | } 49 | 50 | public Type getType() { 51 | return type; 52 | } 53 | } 54 | 55 | 56 | public static Node findCommonAncestor(Node a, Node b) { 57 | if(a.getLevel() > b.getLevel()) { 58 | Node tmp = a; 59 | a = b; 60 | b = tmp; 61 | } 62 | 63 | while(a.getLevel() < b.getLevel()) 64 | b = b.getParent(); 65 | 66 | while(a != null) { 67 | if(a.equals(b)) 68 | return a; 69 | 70 | a = a.getParent(); 71 | b = b.getParent(); 72 | } 73 | 74 | return null; 75 | } 76 | 77 | private static Node findNodeDirect(Type type) { 78 | Node n = knownTypes.get(type); 79 | 80 | if(n != null) 81 | return n; 82 | 83 | if(!pending.add(type)) 84 | throw new IllegalStateException("Circular supertype dependency!"); 85 | 86 | Type superType = type.getSuperType(); 87 | 88 | Node parent = null; 89 | if(superType != null) 90 | parent = findNodeDirect(superType); 91 | 92 | knownTypes.put(type, n = new Node(type, parent)); 93 | 94 | return n; 95 | } 96 | 97 | public static Node findNode(Type type) { 98 | Node n = knownTypes.get(type); 99 | 100 | if(n != null) 101 | return n; 102 | 103 | synchronized(knownTypes) { 104 | return findNodeDirect(type); 105 | } 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/util/TypeUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.util; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 21 | import gov.nasa.jpf.constraints.types.IntegerType; 22 | import gov.nasa.jpf.constraints.types.RealType; 23 | 24 | import java.math.BigDecimal; 25 | import java.math.BigInteger; 26 | 27 | 28 | /** 29 | * static helpers for dealing with types 30 | */ 31 | public class TypeUtil { 32 | 33 | /** 34 | * real number value 35 | */ 36 | public static class Real { 37 | public final BigInteger num; 38 | public final BigInteger den; 39 | public final BigInteger pow; 40 | 41 | public Real(BigInteger num, BigInteger den, BigInteger pow) { 42 | this.num = num; 43 | this.den = den; 44 | this.pow = pow; 45 | } 46 | 47 | public Real(BigDecimal d) { 48 | BigInteger power = BigInteger.ZERO; 49 | BigInteger div = BigInteger.ONE; 50 | String dstr = d.toString(); 51 | int indexOfE = dstr.indexOf('E'); 52 | if (indexOfE != -1) { 53 | power = new BigInteger(dstr.substring(indexOfE + 1)); 54 | dstr = dstr.substring(0, indexOfE); 55 | } 56 | int indexOfDot = dstr.indexOf('.'); 57 | if (indexOfDot != -1) { 58 | int length = dstr.length(); 59 | int nZeroes = length - indexOfDot - 1; 60 | dstr = dstr.substring(0, indexOfDot) + dstr.substring(indexOfDot + 1, length); 61 | while (nZeroes > 0) { 62 | div = div.multiply(BigInteger.TEN); 63 | nZeroes--; 64 | } 65 | } 66 | this.den = new BigInteger(dstr); 67 | this.pow = power; 68 | this.num = div; 69 | } 70 | 71 | public BigDecimal getDecimalValue() { 72 | return new BigDecimal(num).divide(new BigDecimal(den)).pow(pow.intValue()); 73 | } 74 | }; 75 | 76 | 77 | public static boolean isBoolSort(Expression expression) { 78 | return expression.getType().equals(BuiltinTypes.BOOL); 79 | } 80 | 81 | public static boolean isIntSort(Expression expression) { 82 | return (expression.getType() instanceof IntegerType); 83 | } 84 | 85 | public static boolean isRealSort(Expression expression) { 86 | return (expression.getType() instanceof RealType); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/types/TypeContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.types; 18 | 19 | import java.util.HashMap; 20 | import java.util.Map; 21 | 22 | public class TypeContext { 23 | 24 | private final Map> TYPE_FOR_NAME 25 | = new HashMap>(); 26 | private final Map,Type> TYPE_FOR_CLASS 27 | = new HashMap,Type>(); 28 | 29 | public TypeContext() { 30 | this(true); 31 | } 32 | 33 | public TypeContext(boolean withBuiltin) { 34 | if(withBuiltin) 35 | addBuiltinTypes(); 36 | } 37 | 38 | protected void registerType(Type type) { 39 | String name = type.getName(); 40 | registerType(name, type); 41 | String[] otherNames = type.getOtherNames(); 42 | if(otherNames != null) { 43 | for(String nam : otherNames) 44 | registerType(nam, type); 45 | } 46 | 47 | Class canonicalClass = type.getCanonicalClass(); 48 | if(canonicalClass != null) { 49 | registerType(canonicalClass, type); 50 | Class[] otherClasses = type.getOtherClasses(); 51 | if(otherClasses != null) { 52 | for(Class other : otherClasses) 53 | registerType(other, type); 54 | } 55 | } 56 | } 57 | 58 | private void registerType(String name, Type type) { 59 | if(TYPE_FOR_NAME.get(name) == null) 60 | TYPE_FOR_NAME.put(name, type); 61 | } 62 | 63 | private void registerType(Class clazz, Type type) { 64 | if(TYPE_FOR_CLASS.get(clazz) == null) 65 | TYPE_FOR_CLASS.put(clazz, type); 66 | } 67 | 68 | 69 | public void addBuiltinTypes() { 70 | for(Type t : BuiltinTypes.BUILTIN_TYPES) 71 | registerType(t); 72 | } 73 | 74 | public Type byName(String name) { 75 | return TYPE_FOR_NAME.get(name); 76 | } 77 | 78 | public Type byClass(Class clazz) { 79 | return TYPE_FOR_CLASS.get(clazz); 80 | } 81 | 82 | 83 | public Type mostSpecificSupertype(Type typeA, Type typeB) { 84 | if(typeA.equals(typeB)) 85 | return typeA; 86 | 87 | TypeForest.Node a = TypeForest.findNode(typeA); 88 | TypeForest.Node b = TypeForest.findNode(typeB); 89 | 90 | TypeForest.Node ancestor = TypeForest.findCommonAncestor(a, b); 91 | 92 | if(ancestor == null) 93 | return null; 94 | 95 | return ancestor.getType(); 96 | } 97 | 98 | } 99 | -------------------------------------------------------------------------------- /src/test/java/gov/nasa/jpf/constraints/parser/ParserTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.parser; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.Valuation; 21 | import gov.nasa.jpf.constraints.api.Variable; 22 | import gov.nasa.jpf.constraints.types.Type; 23 | import gov.nasa.jpf.constraints.types.TypeContext; 24 | import gov.nasa.jpf.constraints.util.ExpressionUtil; 25 | import java.util.ArrayList; 26 | import java.util.Collection; 27 | import java.util.Collections; 28 | import org.antlr.runtime.ANTLRStringStream; 29 | import org.antlr.runtime.CommonTokenStream; 30 | import org.antlr.runtime.RecognitionException; 31 | import org.testng.annotations.Test; 32 | 33 | /** 34 | * 35 | */ 36 | public class ParserTest { 37 | 38 | @Test 39 | public void testParser() throws RecognitionException { 40 | 41 | TypeContext tc = new TypeContext(true); 42 | Type dType = tc.byClass(Double.class); 43 | Type iType = tc.byClass(Integer.class); 44 | 45 | Variable x1 = new Variable(dType, "x1"); 46 | Variable x2 = new Variable(dType, "x2"); 47 | Variable x3 = new Variable(iType, "x3"); 48 | 49 | Collection> vars = new ArrayList<>(); 50 | Collections.addAll(vars, x1, x2, x3); 51 | 52 | Expression expr = ParserUtil.parseLogical( 53 | "(x1 * x2 > 10.0) || x3 < 100", tc, vars); 54 | 55 | System.out.println(expr); 56 | 57 | ParserUtil.parseLogical(ExpressionUtil.toParseableString(expr)); 58 | 59 | ParserUtil.parseLogical( 60 | "declare 'i1':sint32 in (forall ('i2':sint32): (('i1' * 'i2') == 0))"); 61 | 62 | ParserUtil.parseLogical( 63 | "declare 'i':sint32, 'this.state':sint32, 'b':bool in (true && (('i' != 'this.state') && 'b'))"); 64 | 65 | Valuation val = new Valuation(); 66 | val.setValue(x1, new Double(5)); 67 | val.setValue(x2, new Double(2)); 68 | val.setValue(x3, 1); 69 | 70 | boolean res = (Boolean) expr.evaluate(val); 71 | System.out.println(res); 72 | 73 | String s = x1.toString(Variable.INCLUDE_VARIABLE_TYPE); 74 | System.out.println(s); 75 | System.out.println(ParserUtil.parseVariable(s)); 76 | 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/solvers/ReflectionSolverProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.solvers; 18 | 19 | import gov.nasa.jpf.constraints.api.ConstraintSolver; 20 | import gov.nasa.jpf.constraints.exceptions.SolverCreationException; 21 | 22 | import java.lang.reflect.Constructor; 23 | import java.lang.reflect.InvocationTargetException; 24 | import java.util.Properties; 25 | import java.util.logging.Level; 26 | import java.util.logging.Logger; 27 | 28 | 29 | final class ReflectionSolverProvider implements ConstraintSolverProvider { 30 | 31 | private static final Logger logger = Logger.getLogger("constraints"); 32 | 33 | private final Constructor ctor; 34 | private final boolean hasPropertyCtor; 35 | 36 | public ReflectionSolverProvider(Class clazz) { 37 | Constructor ctor = null; 38 | boolean hasPropertyCtor = false; 39 | 40 | try { 41 | ctor = clazz.getConstructor(Properties.class); 42 | hasPropertyCtor = true; 43 | } 44 | catch(NoSuchMethodException ex) {} 45 | catch(SecurityException ex) {} 46 | 47 | if(ctor == null) { 48 | try { 49 | ctor = clazz.getConstructor(); 50 | } 51 | catch(NoSuchMethodException ex) { 52 | throw new IllegalArgumentException("Constraint solver class " + clazz.getName() 53 | + " neither exposes Properties nor default constructor"); 54 | } 55 | catch(SecurityException ex) { 56 | throw new IllegalArgumentException("Constraint solver class " + clazz.getName() 57 | + " neither exposes Properties nor default constructor"); 58 | } 59 | } 60 | 61 | this.ctor = ctor; 62 | this.hasPropertyCtor = hasPropertyCtor; 63 | } 64 | 65 | @Override 66 | public String[] getNames() { 67 | Class declClazz = ctor.getDeclaringClass(); 68 | return new String[]{declClazz.getSimpleName(), declClazz.getName()}; 69 | } 70 | 71 | @Override 72 | public ConstraintSolver createSolver(Properties config) { 73 | try { 74 | if(hasPropertyCtor) 75 | return ctor.newInstance(config); 76 | logger.log(Level.FINE, "Note: {0} does not expose a " + 77 | "Properties constructor. No configuration is used.", ctor.getDeclaringClass().getName()); 78 | return ctor.newInstance(); 79 | } 80 | catch(IllegalAccessException ex) { 81 | throw new SolverCreationException(ex); 82 | } 83 | catch(InvocationTargetException ex) { 84 | throw new SolverCreationException(ex); 85 | } 86 | catch(InstantiationException ex) { 87 | throw new SolverCreationException(ex); 88 | } 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /src/test/java/gov/nasa/jpf/constraints/expressions/functions/math/FunctionAxiomsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions.functions.math; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.expressions.functions.math.axioms.AsinProperties; 21 | import gov.nasa.jpf.constraints.expressions.functions.math.axioms.Atan2Properties; 22 | import gov.nasa.jpf.constraints.expressions.functions.math.axioms.SqrtProperties; 23 | import org.testng.annotations.Test; 24 | 25 | /** 26 | * 27 | */ 28 | @Test 29 | public class FunctionAxiomsTest { 30 | 31 | @Test 32 | public void printAxiomsSin() { 33 | AsinProperties asin = new AsinProperties(10); 34 | for (Expression e : asin.getDefinition()) { 35 | System.out.println(e); 36 | } 37 | } 38 | 39 | @Test 40 | public void testLogExp() { 41 | System.out.println("max. " + Double.MAX_VALUE); 42 | double d = Math.log(Double.MAX_VALUE); 43 | System.out.println("log of max. " + d); 44 | double max = Math.exp(d); 45 | System.out.println("exp of log of max. " + max); 46 | 47 | for (int i=0; i<5; i++) { 48 | double x = -200.0 * (double) i; 49 | double e_x = Math.exp(x); 50 | System.out.println("exp of "+ x + ": " + e_x); 51 | } 52 | } 53 | 54 | @Test 55 | public void testSqrt() { 56 | SqrtProperties sqrt = new SqrtProperties(4); 57 | 58 | for (Expression e : sqrt.getDefinition()) { 59 | System.out.println(e); 60 | } 61 | } 62 | 63 | @Test 64 | public void testAtan2() { 65 | System.out.println("(+1.0, +1.0) " + Math.toDegrees( Math.atan2( +1.0, +1.0)) ); 66 | System.out.println("(-1.0, +1.0) " + Math.toDegrees( Math.atan2( -1.0, +1.0)) ); 67 | System.out.println("(-1.0, -1.0) " + Math.toDegrees( Math.atan2( -1.0, -1.0)) ); 68 | System.out.println("(+1.0, -1.0) " + Math.toDegrees( Math.atan2( +1.0, -1.0)) ); 69 | 70 | System.out.println("(+1.0, +1.0) " + Math.atan2( +1.0, +1.0) ); 71 | System.out.println("(-1.0, +1.0) " + Math.atan2( -1.0, +1.0) ); 72 | System.out.println("(-1.0, -1.0) " + Math.atan2( -1.0, -1.0) ); 73 | System.out.println("(+1.0, -1.0) " + Math.atan2( +1.0, -1.0) ); 74 | 75 | Atan2Properties atan2 = new Atan2Properties(11, null); 76 | 77 | for (Expression e : atan2.getDefinition()) { 78 | System.out.println(e); 79 | } 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/functions/math/axioms/SqrtProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions.functions.math.axioms; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.Variable; 21 | import gov.nasa.jpf.constraints.expressions.IfThenElse; 22 | import gov.nasa.jpf.constraints.expressions.functions.math.MathFunctions; 23 | import static gov.nasa.jpf.constraints.expressions.functions.math.axioms.PropertyBuilder.*; 24 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 25 | 26 | public class SqrtProperties implements FunctionProperties { 27 | 28 | private final int lookupSize; 29 | 30 | public SqrtProperties(int lookupSize) { 31 | this.lookupSize = lookupSize; 32 | } 33 | 34 | private Expression sqrt() { 35 | Variable x = var(BuiltinTypes.DOUBLE); 36 | IfThenElse ite = lookupTable(x); 37 | return linearInterpolation(MathFunctions.SQRT, ite, x); 38 | } 39 | 40 | private IfThenElse lookupTable(Expression x) { 41 | double[] domain = new double[lookupSize* 2 + 1]; 42 | domain[lookupSize] = 1.0; 43 | for (int i=1; i<=lookupSize; i++) { 44 | domain[lookupSize + i] = 2.0 * domain[lookupSize + i - 1]; 45 | domain[lookupSize - i] = domain[lookupSize - i + 1] / 2.0; 46 | } 47 | 48 | domain[0] = 0.0; 49 | domain[domain.length-1] = Double.MAX_VALUE; 50 | 51 | double[] values = new double[domain.length]; 52 | for (int i=0; i[] getDefinition() { 62 | return array(sqrt()); 63 | } 64 | 65 | @Override 66 | public Expression[] getRangeBounds() { 67 | return array(lbound(MathFunctions.SQRT, constant(0.0), false)); 68 | } 69 | 70 | @Override 71 | public Expression[] getDomainBounds(Expression... fargs) { 72 | return array(gte(fargs[0], constant(0.0))); 73 | } 74 | 75 | @Override 76 | public Expression[] getDefinition(Variable retValue, Expression... fargs) { 77 | IfThenElse ite = lookupTable(fargs[0]); 78 | return array(eq(retValue, ite)); 79 | } 80 | 81 | @Override 82 | public Expression[] getRangeBounds(Variable retVale) { 83 | return array(gte(retVale, constant(0.0))); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/parser/ParserUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.parser; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.Variable; 21 | import gov.nasa.jpf.constraints.types.TypeContext; 22 | 23 | import java.util.Collection; 24 | import java.util.Collections; 25 | 26 | import org.antlr.runtime.ANTLRStringStream; 27 | import org.antlr.runtime.CharStream; 28 | import org.antlr.runtime.CommonTokenStream; 29 | import org.antlr.runtime.RecognitionException; 30 | import org.antlr.runtime.TokenStream; 31 | import org.antlr.runtime.tree.Tree; 32 | 33 | public class ParserUtil { 34 | 35 | 36 | public static Expression parseLogical(String string) throws RecognitionException { 37 | return parseLogical(string, new TypeContext(true), Collections.>emptySet()); 38 | } 39 | 40 | public static Expression parseLogical(String string, TypeContext types, 41 | Collection> variables) throws RecognitionException { 42 | ExpressionParser parser = getParser(string); 43 | Tree ast = parser.start().tree; 44 | ASTTranslator translator = new ASTTranslator(types); 45 | translator.declareVariables(variables); 46 | return translator.translateRootLogical(ast); 47 | } 48 | 49 | public static Expression parseArithmetic(String string) throws RecognitionException { 50 | return parseArithmetic(string, new TypeContext(true), Collections.>emptySet()); 51 | } 52 | 53 | public static Expression parseArithmetic(String string, TypeContext types, 54 | Collection> variables) throws RecognitionException { 55 | ExpressionParser parser = getParser(string); 56 | Tree ast = parser.start_aexpression().tree; 57 | ASTTranslator translator = new ASTTranslator(types); 58 | translator.declareVariables(variables); 59 | return translator.translateRootArithmetic(ast); 60 | } 61 | 62 | public static Variable parseVariable(String string) throws RecognitionException { 63 | ExpressionParser parser = getParser(string); 64 | Tree ast = parser.start_variable().tree; 65 | ASTTranslator translator = new ASTTranslator(new TypeContext(true)); 66 | return translator.translateRootVariable(ast); 67 | } 68 | 69 | private static ExpressionParser getParser(String string) throws RecognitionException { 70 | CharStream cs = new ANTLRStringStream(string); 71 | ExpressionLexer lex = new ExpressionLexer(cs); 72 | TokenStream ts = new CommonTokenStream(lex); 73 | ExpressionParser parser = new ExpressionParser(ts); 74 | return parser; 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/functions/math/axioms/LogProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions.functions.math.axioms; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.Variable; 21 | import gov.nasa.jpf.constraints.expressions.IfThenElse; 22 | import gov.nasa.jpf.constraints.expressions.functions.math.MathFunctions; 23 | import static gov.nasa.jpf.constraints.expressions.functions.math.axioms.PropertyBuilder.*; 24 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 25 | 26 | public class LogProperties implements FunctionProperties { 27 | 28 | private final int lookupSize; 29 | 30 | public LogProperties(int lookupSize) { 31 | this.lookupSize = lookupSize; 32 | } 33 | 34 | private Expression log() { 35 | Variable x = var(BuiltinTypes.DOUBLE); 36 | IfThenElse ite = lookupTable(x); 37 | return linearInterpolation(MathFunctions.LOG, ite, x); 38 | } 39 | 40 | private IfThenElse lookupTable(Expression x) { 41 | double[] domain = new double[lookupSize* 2 + 1]; 42 | domain[lookupSize] = 1.0; 43 | for (int i=1; i<=lookupSize-1; i++) { 44 | domain[lookupSize + i] = 2.0 * domain[lookupSize + i - 1]; 45 | domain[lookupSize - i] = domain[lookupSize - i + 1] / 2.0; 46 | } 47 | 48 | domain[0] = Double.MIN_NORMAL; 49 | domain[domain.length-1] = Double.MAX_VALUE; 50 | 51 | double[] values = new double[domain.length]; 52 | for (int i=0; i[] getDefinition() { 61 | return array(log()); 62 | } 63 | 64 | @Override 65 | public Expression[] getRangeBounds() { 66 | return array(ubound(MathFunctions.LOG, constant(Math.log(Double.MAX_VALUE)), false)); 67 | } 68 | 69 | @Override 70 | public Expression[] getDomainBounds(Expression... fargs) { 71 | return array(gte(fargs[0], constant(0.0) )); 72 | } 73 | 74 | @Override 75 | public Expression[] getDefinition(Variable retValue, Expression... fargs) { 76 | IfThenElse ite = lookupTable(fargs[0]); 77 | return array(eq(retValue, ite)); 78 | } 79 | 80 | @Override 81 | public Expression[] getRangeBounds(Variable retVale) { 82 | return array(lte(retVale, constant(Math.log(Double.MAX_VALUE)))); 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/Negation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.ExpressionVisitor; 21 | import gov.nasa.jpf.constraints.api.Valuation; 22 | import gov.nasa.jpf.constraints.api.Variable; 23 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 24 | 25 | import java.io.IOException; 26 | import java.util.Collection; 27 | 28 | /** 29 | * Logical negation 30 | */ 31 | public class Negation extends AbstractBoolExpression { 32 | 33 | private final Expression negated; 34 | 35 | public Negation(Expression negated) { 36 | this.negated = negated; 37 | } 38 | 39 | public Expression getNegated() { 40 | return this.negated; 41 | } 42 | 43 | @Override 44 | public Boolean evaluate(Valuation values) { 45 | return !this.negated.evaluate(values); 46 | } 47 | 48 | @Override 49 | public void collectFreeVariables(Collection> variables) { 50 | this.negated.collectFreeVariables(variables); 51 | } 52 | 53 | 54 | @Override 55 | public int hashCode() { 56 | final int prime = 31; 57 | int result = 1; 58 | result = prime * result + negated.hashCode(); 59 | return result; 60 | } 61 | 62 | @Override 63 | public boolean equals(Object obj) { 64 | if (this == obj) 65 | return true; 66 | if (obj == null) 67 | return false; 68 | if (getClass() != obj.getClass()) 69 | return false; 70 | Negation other = (Negation) obj; 71 | 72 | return negated.equals(other.negated); 73 | } 74 | 75 | @Override 76 | @SuppressWarnings("unchecked") 77 | public Expression[] getChildren() { 78 | return new Expression[]{negated}; 79 | } 80 | 81 | @Override 82 | public Expression duplicate( 83 | Expression[] newChildren) { 84 | assert newChildren.length == 1; 85 | 86 | Expression newChild = newChildren[0]; 87 | if(newChild == negated) 88 | return this; 89 | 90 | return new Negation(newChild.requireAs(BuiltinTypes.BOOL)); 91 | } 92 | 93 | @Override 94 | public void print(Appendable a, int flags) throws IOException { 95 | a.append('!'); 96 | negated.print(a, flags); 97 | } 98 | 99 | @Override 100 | public void printMalformedExpression(Appendable a, int flags) throws IOException { 101 | a.append('!'); 102 | if(negated == null){ 103 | a.append("null"); 104 | } else { 105 | negated.printMalformedExpression(a, flags); 106 | } 107 | } 108 | 109 | @Override 110 | public R accept(ExpressionVisitor visitor, D data) { 111 | return visitor.visit(this, data); 112 | } 113 | } -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/api/ValuationEntry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.api; 18 | 19 | import gov.nasa.jpf.constraints.expressions.Constant; 20 | 21 | import java.util.Map; 22 | 23 | public class ValuationEntry implements Cloneable { 24 | 25 | @Deprecated 26 | @SuppressWarnings("rawtypes") 27 | private class EntryWrapper implements Map.Entry { 28 | @Override 29 | public Variable getKey() { 30 | return variable; 31 | } 32 | @Override 33 | public Object getValue() { 34 | return value; 35 | } 36 | @Override 37 | public Object setValue(Object value) { 38 | throw new UnsupportedOperationException(); 39 | } 40 | } 41 | 42 | 43 | public static ValuationEntry create(Variable variable, E value) { 44 | return new ValuationEntry(variable, value); 45 | } 46 | 47 | 48 | private final Variable variable; 49 | private E value; 50 | 51 | public ValuationEntry(Variable variable, E value) { 52 | this.variable = variable; 53 | this.value = value; 54 | } 55 | 56 | public Variable getVariable() { 57 | return variable; 58 | } 59 | 60 | public E getValue() { 61 | return value; 62 | } 63 | 64 | public void setValue(E value) { 65 | this.value = value; 66 | } 67 | 68 | @Override 69 | public ValuationEntry clone() { 70 | return new ValuationEntry(variable, value); 71 | } 72 | 73 | public Constant valueConstant() { 74 | return Constant.create(variable.getType(), value); 75 | } 76 | 77 | @SuppressWarnings("rawtypes") 78 | public Map.Entry toEntry() { 79 | return new EntryWrapper(); 80 | } 81 | 82 | /* (non-Javadoc) 83 | * @see java.lang.Object#hashCode() 84 | */ 85 | @Override 86 | public int hashCode() { 87 | final int prime = 31; 88 | int result = 1; 89 | result = prime * result + ((value == null) ? 0 : value.hashCode()); 90 | result = prime * result + ((variable == null) ? 0 : variable.hashCode()); 91 | return result; 92 | } 93 | 94 | /* (non-Javadoc) 95 | * @see java.lang.Object#equals(java.lang.Object) 96 | */ 97 | @Override 98 | public boolean equals(Object obj) { 99 | if (this == obj) 100 | return true; 101 | if (obj == null) 102 | return false; 103 | if (getClass() != obj.getClass()) 104 | return false; 105 | ValuationEntry other = (ValuationEntry) obj; 106 | if (value == null) { 107 | if (other.value != null) 108 | return false; 109 | } else if (!value.equals(other.value)) 110 | return false; 111 | if (variable == null) { 112 | if (other.variable != null) 113 | return false; 114 | } else if (!variable.equals(other.variable)) 115 | return false; 116 | return true; 117 | } 118 | 119 | 120 | 121 | } 122 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/functions/math/axioms/ExpProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.expressions.functions.math.axioms; 17 | 18 | import gov.nasa.jpf.constraints.api.Expression; 19 | import gov.nasa.jpf.constraints.api.Variable; 20 | import gov.nasa.jpf.constraints.expressions.IfThenElse; 21 | import gov.nasa.jpf.constraints.expressions.functions.math.MathFunctions; 22 | import static gov.nasa.jpf.constraints.expressions.functions.math.axioms.PropertyBuilder.*; 23 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 24 | 25 | public class ExpProperties implements FunctionProperties { 26 | 27 | private final int lookupSize; 28 | 29 | public ExpProperties(int lookupSize) { 30 | this.lookupSize = lookupSize; 31 | } 32 | 33 | private Expression exp() { 34 | Variable x = var(BuiltinTypes.DOUBLE); 35 | IfThenElse ite = lookupTable(x); 36 | return linearInterpolation(MathFunctions.EXP, ite, x); 37 | } 38 | 39 | private IfThenElse lookupTable(Expression x) { 40 | double[] domain = new double[lookupSize* 2 + 1]; 41 | double step = 0.1; 42 | domain[lookupSize] = 0.0; 43 | for (int i=1; i<=lookupSize-1; i++) { 44 | domain[lookupSize + i] = domain[lookupSize + i - 1] + step; 45 | domain[lookupSize - i] = domain[lookupSize - i + 1] - step; 46 | 47 | assert domain[lookupSize + i -1] < Math.log(Double.MAX_VALUE); 48 | assert domain[lookupSize - i + 1] >= -800.0; 49 | 50 | step *= 2.0; 51 | } 52 | 53 | domain[0] = Double.MIN_VALUE; 54 | domain[1] = -800.0; 55 | domain[domain.length-1] = Math.log(Double.MAX_VALUE); 56 | 57 | double[] values = new double[domain.length]; 58 | for (int i=0; i[] getDefinition() { 71 | return array(exp()); 72 | } 73 | 74 | @Override 75 | public Expression[] getRangeBounds() { 76 | return array(lbound(MathFunctions.EXP, constant(0.0), false)); 77 | } 78 | 79 | @Override 80 | public Expression[] getDomainBounds(Expression... fargs) { 81 | return array(lte( fargs[0], constant(Math.log(Double.MAX_VALUE)))); 82 | } 83 | 84 | @Override 85 | public Expression[] getDefinition(Variable retValue, Expression... fargs) { 86 | IfThenElse ite = lookupTable(fargs[0]); 87 | return array(eq(retValue, ite)); 88 | } 89 | 90 | @Override 91 | public Expression[] getRangeBounds(Variable retVale) { 92 | return array(gte(retVale, constant(0.0))); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/UnaryMinus.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.ExpressionVisitor; 21 | import gov.nasa.jpf.constraints.api.Valuation; 22 | import gov.nasa.jpf.constraints.api.Variable; 23 | import gov.nasa.jpf.constraints.types.NumericType; 24 | import gov.nasa.jpf.constraints.types.Type; 25 | 26 | import java.io.IOException; 27 | import java.util.Collection; 28 | 29 | /** 30 | * numeric unary minus 31 | */ 32 | public class UnaryMinus extends AbstractExpression { 33 | 34 | public static UnaryMinus create( 35 | Expression negated) { 36 | return new UnaryMinus(negated); 37 | } 38 | 39 | private final Expression negated; 40 | 41 | public UnaryMinus(Expression negated) { 42 | this.negated = negated; 43 | } 44 | 45 | public Expression getNegated() { 46 | return this.negated; 47 | } 48 | 49 | @Override 50 | public E evaluate(Valuation values) { 51 | E num = negated.evaluate(values); 52 | NumericType type = (NumericType)getType(); 53 | return type.negate(num); 54 | } 55 | 56 | 57 | @Override 58 | public void collectFreeVariables(Collection> variables) { 59 | this.negated.collectFreeVariables(variables); 60 | } 61 | 62 | 63 | @Override 64 | public int hashCode() { 65 | final int prime = 31; 66 | int result = 1; 67 | result = prime * result + negated.hashCode(); 68 | return result; 69 | } 70 | 71 | @Override 72 | public boolean equals(Object obj) { 73 | if (this == obj) 74 | return true; 75 | if (obj == null) 76 | return false; 77 | if (getClass() != obj.getClass()) 78 | return false; 79 | UnaryMinus other = (UnaryMinus) obj; 80 | return negated.equals(other.negated); 81 | } 82 | 83 | public Type getType() { 84 | return negated.getType(); 85 | } 86 | 87 | @Override 88 | public Expression[] getChildren() { 89 | return new Expression[]{negated}; 90 | } 91 | 92 | @Override 93 | public Expression duplicate(Expression[] newChildren) { 94 | assert newChildren.length == 1; 95 | 96 | if(negated == newChildren[0]) 97 | return this; 98 | 99 | return UnaryMinus.create(newChildren[0]); 100 | } 101 | 102 | @Override 103 | public void print(Appendable a, int flags) throws IOException { 104 | a.append("-("); 105 | negated.print(a, flags); 106 | a.append(')'); 107 | } 108 | 109 | @Override 110 | public void printMalformedExpression(Appendable a, int flags) throws IOException{ 111 | a.append("-("); 112 | if(negated == null){ 113 | a.append("null"); 114 | } else { 115 | negated.print(a, flags); 116 | } 117 | a.append(')'); 118 | } 119 | 120 | @Override 121 | public R accept(ExpressionVisitor visitor, D data) { 122 | return visitor.visit(this, data); 123 | } 124 | } -------------------------------------------------------------------------------- /src/test/java/gov/nasa/jpf/constraints/expressions/functions/math/MathFunctionsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.expressions.functions.math; 17 | 18 | import gov.nasa.jpf.constraints.api.Expression; 19 | import gov.nasa.jpf.constraints.api.Valuation; 20 | import gov.nasa.jpf.constraints.api.Variable; 21 | import gov.nasa.jpf.constraints.expressions.NumericCompound; 22 | import gov.nasa.jpf.constraints.expressions.NumericOperator; 23 | import gov.nasa.jpf.constraints.expressions.functions.Function; 24 | import gov.nasa.jpf.constraints.expressions.functions.FunctionExpression; 25 | import gov.nasa.jpf.constraints.expressions.functions.math.MathFunctions; 26 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 27 | 28 | import java.lang.reflect.Method; 29 | import java.util.Random; 30 | 31 | import org.testng.Assert; 32 | import org.testng.annotations.Test; 33 | 34 | @Test 35 | public class MathFunctionsTest { 36 | 37 | private static final Random random = new Random(); 38 | 39 | private static final Variable VAR_X = Variable.create(BuiltinTypes.DOUBLE, "x"); 40 | private static final Variable VAR_Y = Variable.create(BuiltinTypes.DOUBLE, "y"); 41 | 42 | private static final Expression EXPR = NumericCompound.create(VAR_X, NumericOperator.PLUS, VAR_Y); 43 | 44 | private static Valuation createValuation() { 45 | // Make sure result value is in interval (0, 1) 46 | double x = random.nextDouble() * 0.9 + 0.1; 47 | double y = -(random.nextDouble()*0.9 + 0.1) * x * 0.5; 48 | Valuation val = new Valuation(); 49 | val.setValue(VAR_X, x); 50 | val.setValue(VAR_Y, y); 51 | 52 | return val; 53 | } 54 | 55 | private static double directEval(Function func, double arg) throws Exception { 56 | Method m = Math.class.getMethod(func.getName(), double.class); 57 | 58 | return (Double)m.invoke(null, arg); 59 | } 60 | 61 | @Test 62 | public void testCos() throws Exception { 63 | testMathFunction(MathFunctions.COS); 64 | } 65 | 66 | @Test 67 | public void testSin() throws Exception { 68 | testMathFunction(MathFunctions.SIN); 69 | } 70 | 71 | @Test 72 | public void testTan() throws Exception { 73 | testMathFunction(MathFunctions.TAN); 74 | } 75 | 76 | @Test 77 | public void testAcos() throws Exception { 78 | testMathFunction(MathFunctions.ACOS); 79 | } 80 | 81 | @Test 82 | public void testAsin() throws Exception { 83 | testMathFunction(MathFunctions.ASIN); 84 | } 85 | 86 | @Test 87 | public void testAtan() throws Exception { 88 | testMathFunction(MathFunctions.ATAN); 89 | } 90 | 91 | private void testMathFunction(Function function) throws Exception { 92 | Expression expr = EXPR; 93 | Valuation val = createValuation(); 94 | 95 | double exprEval = expr.evaluate(val); 96 | Expression funcExpr = new FunctionExpression<>(function, expr); 97 | 98 | double funcEval = funcExpr.evaluate(val); 99 | 100 | double directEval = directEval(function, exprEval); 101 | 102 | Assert.assertEquals(directEval, funcEval); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/casts/NumericCastOperation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.casts; 18 | 19 | import java.math.BigDecimal; 20 | import java.math.BigInteger; 21 | 22 | public abstract class NumericCastOperation implements CastOperation { 23 | 24 | private final Class toClass; 25 | 26 | public NumericCastOperation(Class toClass) { 27 | this.toClass = toClass; 28 | } 29 | 30 | public static final NumericCastOperation TO_SINT8 = new NumericCastOperation(Byte.class) { 31 | @Override 32 | public Byte cast(Number from) { 33 | return from.byteValue(); 34 | } 35 | }; 36 | public static final NumericCastOperation TO_SINT16 = new NumericCastOperation(Short.class) { 37 | @Override 38 | public Short cast(Number from) { 39 | return from.shortValue(); 40 | } 41 | }; 42 | public static final NumericCastOperation TO_SINT32 = new NumericCastOperation(Integer.class) { 43 | @Override 44 | public Integer cast(Number from) { 45 | return from.intValue(); 46 | } 47 | }; 48 | public static final NumericCastOperation TO_SINT64 = new NumericCastOperation(Long.class) { 49 | @Override 50 | public Long cast(Number from) { 51 | return from.longValue(); 52 | } 53 | }; 54 | public static final NumericCastOperation TO_FLOAT = new NumericCastOperation(Float.class) { 55 | @Override 56 | public Float cast(Number from) { 57 | return from.floatValue(); 58 | } 59 | }; 60 | public static final NumericCastOperation TO_DOUBLE = new NumericCastOperation(Double.class) { 61 | @Override 62 | public Double cast(Number from) { 63 | return from.doubleValue(); 64 | } 65 | }; 66 | 67 | public static final NumericCastOperation TO_INTEGER = new NumericCastOperation(BigInteger.class) { 68 | @Override 69 | public BigInteger cast(Number from) { 70 | Class clazz = from.getClass(); 71 | if(clazz == BigInteger.class) 72 | return (BigInteger)from; 73 | if(clazz == BigDecimal.class) 74 | return ((BigDecimal)from).toBigInteger(); 75 | else if(clazz == Double.class || clazz == Float.class) 76 | return BigDecimal.valueOf(from.doubleValue()).toBigInteger(); 77 | return BigInteger.valueOf(from.longValue()); 78 | } 79 | }; 80 | 81 | public static final NumericCastOperation TO_DECIMAL = new NumericCastOperation(BigDecimal.class) { 82 | @Override 83 | public BigDecimal cast(Number from) { 84 | Class clazz = from.getClass(); 85 | if(clazz == BigDecimal.class) 86 | return (BigDecimal)from; 87 | if(clazz == BigInteger.class) 88 | return new BigDecimal((BigInteger)from); 89 | if(clazz == Long.class) 90 | return new BigDecimal(from.longValue()); 91 | return new BigDecimal(from.doubleValue()); 92 | } 93 | }; 94 | 95 | public Class getFromClass() { 96 | return Number.class; 97 | } 98 | 99 | public Class getToClass() { 100 | return toClass; 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/types/ConcreteType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.types; 18 | 19 | import gov.nasa.jpf.constraints.casts.CastOperation; 20 | 21 | public abstract class ConcreteType implements Type { 22 | 23 | private final String name; 24 | private final Type superType; 25 | private final T defaultValue; 26 | 27 | private final Class canonicalClass; 28 | private final Class[] otherClasses; 29 | 30 | private final String[] otherNames; 31 | 32 | 33 | public ConcreteType(String name, Class canonicalClass, T defaultValue, Type superType, String[] otherNames, Class ...otherClasses) { 34 | this.name = name; 35 | this.superType = superType; 36 | this.defaultValue = defaultValue; 37 | this.canonicalClass = canonicalClass; 38 | this.otherClasses = otherClasses; 39 | this.otherNames = otherNames; 40 | } 41 | 42 | /* (non-Javadoc) 43 | * @see gov.nasa.jpf.constraints.types.Type#getName() 44 | */ 45 | @Override 46 | public String getName() { 47 | return name; 48 | } 49 | 50 | @Override 51 | public String[] getOtherNames() { 52 | return otherNames.clone(); // defensive copy 53 | } 54 | 55 | 56 | /* (non-Javadoc) 57 | * @see gov.nasa.jpf.constraints.types.Type#getCanonicalClass() 58 | */ 59 | @Override 60 | public Class getCanonicalClass() { 61 | return canonicalClass; 62 | } 63 | 64 | 65 | /* (non-Javadoc) 66 | * @see gov.nasa.jpf.constraints.types.Type#getOtherClasses() 67 | */ 68 | @Override 69 | public Class[] getOtherClasses() { 70 | return otherClasses.clone(); // defensive copy 71 | } 72 | 73 | 74 | 75 | 76 | /* (non-Javadoc) 77 | * @see gov.nasa.jpf.constraints.types.Type#getDefaultValue() 78 | */ 79 | @Override 80 | public T getDefaultValue() { 81 | return defaultValue; 82 | } 83 | 84 | 85 | /* (non-Javadoc) 86 | * @see gov.nasa.jpf.constraints.types.Type#getSuperType() 87 | */ 88 | @Override 89 | public Type getSuperType() { 90 | return superType; 91 | } 92 | 93 | 94 | @Override 95 | public CastOperation cast(Type fromType) { 96 | if(fromType instanceof ConcreteType) { 97 | ConcreteType ctype = (ConcreteType)fromType; 98 | CastOperation castOp = castFrom(ctype); 99 | if(castOp == null) 100 | castOp = ctype.castTo(this); 101 | return castOp; 102 | } 103 | return null; 104 | } 105 | 106 | 107 | @Override 108 | public CastOperation requireCast(Type fromType) { 109 | CastOperation op = cast(fromType); 110 | if(op == null) 111 | throw new IllegalArgumentException("Required cast from type " + fromType + ", but none declared"); 112 | return op; 113 | } 114 | 115 | 116 | 117 | protected CastOperation castTo(Type toType) { 118 | return null; 119 | } 120 | 121 | protected CastOperation castFrom(Type fromType) { 122 | return null; 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/functions/math/MathFunctions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.expressions.functions.math; 17 | 18 | import gov.nasa.jpf.constraints.expressions.functions.Function; 19 | 20 | public abstract class MathFunctions { 21 | 22 | public static UnaryDoubleFunction COS = new UnaryDoubleFunction("cos") { 23 | @Override 24 | protected double doEvaluate(double value) { 25 | return Math.cos(value); 26 | } 27 | }; 28 | 29 | public static UnaryDoubleFunction SIN = new UnaryDoubleFunction("sin") { 30 | @Override 31 | protected double doEvaluate(double value) { 32 | return Math.sin(value); 33 | } 34 | }; 35 | 36 | public static UnaryDoubleFunction TAN = new UnaryDoubleFunction("tan") { 37 | @Override 38 | protected double doEvaluate(double value) { 39 | return Math.tan(value); 40 | } 41 | }; 42 | 43 | public static UnaryDoubleFunction ACOS = new UnaryDoubleFunction("acos") { 44 | @Override 45 | protected double doEvaluate(double value) { 46 | return Math.acos(value); 47 | } 48 | }; 49 | 50 | public static UnaryDoubleFunction ASIN = new UnaryDoubleFunction("asin") { 51 | @Override 52 | protected double doEvaluate(double value) { 53 | return Math.asin(value); 54 | } 55 | }; 56 | 57 | public static UnaryDoubleFunction ATAN = new UnaryDoubleFunction("atan") { 58 | @Override 59 | protected double doEvaluate(double value) { 60 | return Math.atan(value); 61 | } 62 | }; 63 | 64 | public static UnaryDoubleFunction LOG = new UnaryDoubleFunction("log") { 65 | @Override 66 | protected double doEvaluate(double value) { 67 | return Math.log(value); 68 | } 69 | }; 70 | 71 | public static UnaryDoubleFunction EXP = new UnaryDoubleFunction("exp") { 72 | @Override 73 | protected double doEvaluate(double value) { 74 | return Math.exp(value); 75 | } 76 | }; 77 | 78 | public static UnaryDoubleFunction SQRT = new UnaryDoubleFunction("sqrt") { 79 | @Override 80 | protected double doEvaluate(double value) { 81 | return Math.sqrt(value); 82 | } 83 | }; 84 | 85 | public static UnaryDoubleFunction LOG10 = new UnaryDoubleFunction("log10") { 86 | @Override 87 | protected double doEvaluate(double value) { 88 | return Math.log10(value); 89 | } 90 | }; 91 | 92 | public static UnaryDoubleLongFunction ROUND = new UnaryDoubleLongFunction("round") { 93 | @Override 94 | protected long doEvaluate(double value) { 95 | return Math.round(value); 96 | } 97 | }; 98 | 99 | public static BinaryDoubleFunction ATAN2 = new BinaryDoubleFunction("atan2") { 100 | @Override 101 | protected double doEvaluate(double v1, double v2) { 102 | return Math.atan2(v1, v2); 103 | } 104 | }; 105 | 106 | public static BinaryDoubleFunction POW = new BinaryDoubleFunction("pow") { 107 | @Override 108 | protected double doEvaluate(double v1, double v2) { 109 | return Math.pow(v1, v2); 110 | } 111 | }; 112 | 113 | public static UnaryIntegerFunction IABS = new UnaryIntegerFunction("abs") { 114 | @Override 115 | protected int doEvaluate(int v1) { 116 | return Math.abs(v1); 117 | } 118 | }; 119 | } 120 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/BitvectorNegation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.ExpressionVisitor; 21 | import gov.nasa.jpf.constraints.api.Valuation; 22 | import gov.nasa.jpf.constraints.api.Variable; 23 | import gov.nasa.jpf.constraints.types.BVIntegerType; 24 | import gov.nasa.jpf.constraints.types.Type; 25 | 26 | import java.io.IOException; 27 | import java.util.Collection; 28 | 29 | public class BitvectorNegation 30 | extends AbstractExpression { 31 | 32 | public static BitvectorNegation create(Expression expression) { 33 | return new BitvectorNegation(expression); 34 | } 35 | 36 | private final Expression negated; 37 | 38 | public BitvectorNegation(Expression negated) { 39 | assert negated.getType() instanceof BVIntegerType; 40 | 41 | this.negated = negated; 42 | } 43 | 44 | @Override 45 | public E evaluate(Valuation values) { 46 | BVIntegerType type = (BVIntegerType)getType(); 47 | E value = negated.evaluate(values); 48 | return type.negate(value); 49 | } 50 | 51 | public Expression getNegated() { 52 | return negated; 53 | } 54 | 55 | @Override 56 | public void collectFreeVariables(Collection> variables) { 57 | negated.collectFreeVariables(variables); 58 | } 59 | 60 | @Override 61 | public R accept(ExpressionVisitor visitor, D data) { 62 | return visitor.visit(this, data); 63 | } 64 | 65 | @Override 66 | public Type getType() { 67 | return negated.getType(); 68 | } 69 | 70 | @Override 71 | @SuppressWarnings("unchecked") 72 | public Expression[] getChildren() { 73 | return new Expression[]{negated}; 74 | } 75 | 76 | @Override 77 | public Expression duplicate(Expression[] newChildren) { 78 | assert newChildren.length == 1; 79 | if(identical(newChildren, negated)) 80 | return this; 81 | return create(newChildren[0]); 82 | } 83 | 84 | @Override 85 | public void print(Appendable a, int flags) throws IOException { 86 | a.append('~'); 87 | negated.print(a, flags); 88 | } 89 | 90 | @Override 91 | public void printMalformedExpression(Appendable a, int flags) 92 | throws IOException{ 93 | a.append('~'); 94 | if(negated != null){ 95 | negated.print(a, flags); 96 | } else { 97 | a.append("null"); 98 | } 99 | } 100 | /* (non-Javadoc) 101 | * @see java.lang.Object#hashCode() 102 | */ 103 | @Override 104 | public int hashCode() { 105 | final int prime = 31; 106 | int result = 1; 107 | result = prime * result + ((negated == null) ? 0 : negated.hashCode()); 108 | return result; 109 | } 110 | 111 | /* (non-Javadoc) 112 | * @see java.lang.Object#equals(java.lang.Object) 113 | */ 114 | @Override 115 | public boolean equals(Object obj) { 116 | if (this == obj) 117 | return true; 118 | if (obj == null) 119 | return false; 120 | if (getClass() != obj.getClass()) 121 | return false; 122 | BitvectorNegation other = (BitvectorNegation) obj; 123 | if (negated == null) { 124 | if (other.negated != null) 125 | return false; 126 | } else if (!negated.equals(other.negated)) 127 | return false; 128 | return true; 129 | } 130 | 131 | 132 | 133 | } 134 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/Constant.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.ExpressionVisitor; 21 | import gov.nasa.jpf.constraints.api.Valuation; 22 | import gov.nasa.jpf.constraints.api.Variable; 23 | import gov.nasa.jpf.constraints.java.ObjectConstraints; 24 | import gov.nasa.jpf.constraints.types.Type; 25 | 26 | import java.io.IOException; 27 | import java.util.Collection; 28 | 29 | /** 30 | * constant value of type E 31 | */ 32 | public class Constant extends AbstractExpression { 33 | 34 | public static Constant create(Type type, E value) { 35 | return new Constant(type, value); 36 | } 37 | 38 | public static Constant createParsed(Type type, String txt) { 39 | return new Constant(type, type.parse(txt)); 40 | } 41 | 42 | public static Constant createCasted(Type type, Object value) { 43 | return new Constant(type, type.cast(value)); 44 | } 45 | 46 | private final Type type; 47 | private final E value; 48 | 49 | public Constant(Type type, E value) { 50 | this.type = type; 51 | this.value = value; 52 | } 53 | 54 | 55 | @Override 56 | public E evaluate(Valuation values) { 57 | return this.value; 58 | } 59 | 60 | 61 | @Override 62 | public void collectFreeVariables(Collection> variables) { 63 | // do nothing 64 | } 65 | 66 | @Override 67 | public boolean equals(Object obj) { 68 | if (obj == null) { 69 | return false; 70 | } 71 | if (getClass() != obj.getClass()) { 72 | return false; 73 | } 74 | final Constant other = (Constant) obj; 75 | if (this.type != other.type && (this.type == null || !this.type.equals(other.type))) { 76 | return false; 77 | } 78 | if ((this.value == null) ? (other.value != null) : !this.value.equals(other.value)) { 79 | return false; 80 | } 81 | return true; 82 | } 83 | 84 | @Override 85 | public int hashCode() { 86 | int hash = 7; 87 | hash = 47 * hash + (this.type != null ? this.type.hashCode() : 0); 88 | hash = 47 * hash + (this.value != null ? this.value.hashCode() : 0); 89 | return hash; 90 | } 91 | 92 | 93 | @Override 94 | public Type getType() { 95 | return this.type; 96 | } 97 | 98 | public E getValue() { 99 | return this.value; 100 | } 101 | 102 | 103 | @Override 104 | public Expression[] getChildren() { 105 | return NO_CHILDREN; 106 | } 107 | 108 | 109 | @Override 110 | public Expression duplicate(Expression[] newChildren) { 111 | assert newChildren.length == 0; 112 | return this; 113 | } 114 | 115 | 116 | @Override 117 | public void print(Appendable a, int flags) throws IOException { 118 | a.append(String.valueOf(value)); 119 | } 120 | 121 | @Override 122 | public void printMalformedExpression(Appendable a, int flags) 123 | throws IOException { 124 | if(value == null){ 125 | a.append("null"); 126 | } else { 127 | a.append(String.valueOf(value)); 128 | } 129 | } 130 | @Override 131 | public R accept(ExpressionVisitor visitor, D data) { 132 | return visitor.visit(this, data); 133 | } 134 | 135 | 136 | // LEGACY API 137 | 138 | @Deprecated 139 | public Constant(Class clazz, E value) { 140 | this(ObjectConstraints.getPrimitiveType(clazz), value); 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 4.0.0 20 | 21 | 24 | gov.nasa 25 | jconstraints 26 | 0.9.2-SNAPSHOT 27 | jar 28 | jConstraints 29 | https://github.com/psycopaths/jconstraints 30 | A library for modeling expressions and for interacting with constraint solvers 31 | 32 | 33 | 34 | Apache License, Version 2.0 35 | https://www.apache.org/licenses/LICENSE-2.0.txt 36 | 37 | 38 | 39 | 40 | https://github.com/psycopaths/jconstraints/issues 41 | GitHub Issues 42 | 43 | 44 | 45 | scm:git:https://github.com/psycopaths/jconstraints.git 46 | HEAD 47 | 48 | 49 | 50 | 53 | 54 | UTF-8 55 | 2.5.3 56 | 3.1 57 | 14.0.1 58 | 3.5.2 59 | 0.9 60 | 6.8 61 | 62 | 63 | 66 | 67 | 68 | 69 | org.apache.maven.plugins 70 | maven-compiler-plugin 71 | ${compiler-plugin.version} 72 | 73 | true 74 | 128m 75 | 512m 76 | 1.7 77 | 1.7 78 | 79 | 80 | 81 | org.apache.maven.plugins 82 | maven-release-plugin 83 | ${maven-release-plugin.version} 84 | 85 | 86 | org.antlr 87 | antlr3-maven-plugin 88 | ${antlr.version} 89 | 90 | 91 | 92 | antlr 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 103 | 104 | 105 | com.google.guava 106 | guava 107 | ${guava.version} 108 | 109 | 110 | org.antlr 111 | antlr-runtime 112 | ${antlr.version} 113 | 114 | 115 | org.testng 116 | testng 117 | ${testng.version} 118 | test 119 | 120 | 121 | 122 | 123 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/java/ObjectConstraints.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | package gov.nasa.jpf.constraints.java; 17 | 18 | import gov.nasa.jpf.constraints.api.Valuation; 19 | import gov.nasa.jpf.constraints.api.Variable; 20 | import gov.nasa.jpf.constraints.expressions.Constant; 21 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 22 | import gov.nasa.jpf.constraints.types.Type; 23 | import gov.nasa.jpf.constraints.types.TypeContext; 24 | 25 | import java.lang.reflect.Field; 26 | import java.util.IdentityHashMap; 27 | 28 | import com.google.common.base.Predicate; 29 | import com.google.common.base.Predicates; 30 | 31 | public class ObjectConstraints { 32 | 33 | private static final TypeContext javaTypes = new TypeContext(true); 34 | 35 | 36 | public static void addToValuation(String name, Object obj, Class realClass, Valuation val) { 37 | if(realClass.isPrimitive()) { 38 | addPrimitiveToValuation(name, obj, val); 39 | } 40 | else { 41 | addInstanceToValuation(name, obj, Predicates.alwaysTrue(), val, new IdentityHashMap()); 42 | } 43 | } 44 | 45 | public static void addInstanceToValuation(String objName, Object obj, Predicate filter, Valuation val, IdentityHashMap visited) { 46 | if(obj == null) 47 | return; 48 | 49 | if(visited.put(obj, Boolean.TRUE) != null) 50 | return; 51 | 52 | Field[] fields = obj.getClass().getFields(); 53 | 54 | 55 | for(Field f : fields) { 56 | if(!filter.apply(f)) 57 | continue; 58 | 59 | Class ftype = f.getType(); 60 | String fname = objName + "." + f.getName(); 61 | 62 | try { 63 | Object value = f.get(obj); 64 | 65 | if(!ftype.isPrimitive()) { 66 | addInstanceToValuation(fname, value, filter, val, visited); 67 | } 68 | else { 69 | addPrimitiveToValuation(fname, value, val); 70 | } 71 | } 72 | catch(IllegalAccessException ex) { 73 | ex.printStackTrace(); 74 | // IGNORE FIELD 75 | } 76 | } 77 | } 78 | 79 | 80 | public static void addPrimitiveToValuation(String name, Object value, Valuation val) { 81 | Variable var = getPrimitiveVar(name, value.getClass()); 82 | val.setCastedValue(var, value); 83 | } 84 | 85 | public static Variable getPrimitiveVar(String name, Class clazz) { 86 | Type t = getPrimitiveType(clazz); 87 | return Variable.create(t, name); 88 | } 89 | 90 | @SuppressWarnings("unchecked") 91 | public static Constant getPrimitiveConst(T value) { 92 | Type t = getPrimitiveType((Class)value.getClass()); 93 | return Constant.create(t, value); 94 | } 95 | 96 | 97 | @SuppressWarnings("unchecked") 98 | public static Type getPrimitiveType(Class clazz) { 99 | if(clazz == Boolean.class) 100 | return (Type)BuiltinTypes.BOOL; 101 | if(clazz == Byte.class) 102 | return (Type)BuiltinTypes.SINT8; 103 | if(clazz == Character.class) 104 | return (Type)BuiltinTypes.UINT16; 105 | if(clazz == Short.class) 106 | return (Type)BuiltinTypes.SINT16; 107 | if(clazz == Integer.class) 108 | return (Type)BuiltinTypes.SINT32; 109 | if(clazz == Long.class) 110 | return (Type)BuiltinTypes.SINT64; 111 | if(clazz == Float.class) 112 | return (Type)BuiltinTypes.FLOAT; 113 | if(clazz == Double.class) 114 | return (Type)BuiltinTypes.DOUBLE; 115 | throw new IllegalArgumentException("Class " + clazz.getName() + " is not a wrapper class for a primitive type"); 116 | } 117 | 118 | public static TypeContext getJavaTypes() { 119 | return javaTypes; 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/functions/math/axioms/CosProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions.functions.math.axioms; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.Variable; 21 | import gov.nasa.jpf.constraints.expressions.UnaryMinus; 22 | import gov.nasa.jpf.constraints.expressions.functions.math.MathFunctions; 23 | import static gov.nasa.jpf.constraints.expressions.functions.math.axioms.PropertyBuilder.*; 24 | import gov.nasa.jpf.constraints.types.BuiltinTypes; 25 | import gov.nasa.jpf.constraints.util.ExpressionUtil; 26 | 27 | public class CosProperties implements FunctionProperties { 28 | 29 | private final SinProperties sin; 30 | 31 | public CosProperties(SinProperties sin) { 32 | this.sin = sin; 33 | } 34 | 35 | @Override 36 | public Expression[] getDefinition() { 37 | Variable arg = doubleVar(); 38 | 39 | // pi * -2.0 <= arg <= pi * 1.5 40 | Expression ifCond = bounds(arg, Math.PI * -2.0, Math.PI * 1.5, false, false); 41 | Expression thenExpr = fexpr(MathFunctions.SIN, plus(constant(Math.PI * 0.5), arg)); 42 | Expression elseExpr = constant(0.0); 43 | 44 | // pi * 1.5 <= arg <= pi * 2.0 45 | elseExpr = ite(ifCond, thenExpr, elseExpr); 46 | ifCond = bounds(arg, Math.PI * 1.5, Math.PI * 2.0, false, false); 47 | thenExpr = new UnaryMinus(fexpr(MathFunctions.SIN, minus(arg, constant(Math.PI * 0.5)))); 48 | 49 | Expression cos = forall(eq(fexpr(MathFunctions.COS, arg), 50 | ite(ifCond, thenExpr, elseExpr)), arg); 51 | return array(cos); 52 | } 53 | 54 | private Expression cos(Expression arg, Variable ret, Variable sinArg, Variable sinRet) { 55 | 56 | // pi * -2.0 <= arg <= pi * 1.5 57 | Expression ifCond = bounds(arg, Math.PI * -2.0, Math.PI * 1.5, false, false); 58 | 59 | Expression thenExprArg = plus(constant(Math.PI * 0.5), arg); 60 | Expression thenExprRet = sinRet; 61 | Expression elseExprArg = constant(0.0); 62 | Expression elseExprRet = constant(0.0); 63 | 64 | elseExprArg = ite(ifCond, thenExprArg, elseExprArg); 65 | elseExprRet = ite(ifCond, thenExprRet, elseExprRet); 66 | 67 | ifCond = bounds(arg, Math.PI * 1.5, Math.PI * 2.0, false, false); 68 | thenExprArg = minus(arg, constant(Math.PI * 0.5)); 69 | thenExprRet = new UnaryMinus(sinRet); 70 | 71 | return ExpressionUtil.and( 72 | eq(sinArg, ite(ifCond, thenExprArg, elseExprArg)), 73 | eq(ret, ite(ifCond, thenExprRet, elseExprRet)) 74 | ); 75 | } 76 | 77 | @Override 78 | public Expression[] getRangeBounds() { 79 | return array(bounds(MathFunctions.COS, constant(-1.0), constant(1.0), false)); 80 | } 81 | 82 | @Override 83 | public Expression[] getDomainBounds(Expression... fargs) { 84 | return array(bounds(fargs[0], Math.PI * -2.0, Math.PI * 2.0, false, false)); 85 | } 86 | 87 | @Override 88 | public Expression[] getDefinition(Variable retValue, Expression... fargs) { 89 | Variable sinArg = var(BuiltinTypes.DOUBLE); 90 | Variable sinRet = var(BuiltinTypes.DOUBLE); 91 | 92 | Expression[] sinDefs = sin.getDefinition(sinRet, sinArg); 93 | Expression[] defs = new Expression[sinDefs.length+1]; 94 | defs[0] = cos(fargs[0], retValue, sinArg, sinRet); 95 | System.arraycopy(sinDefs, 0, defs, 1, sinDefs.length); 96 | 97 | return defs; 98 | } 99 | 100 | @Override 101 | public Expression[] getRangeBounds(Variable retVal) { 102 | return array(bounds(retVal, -1.0, 1.0, false, false)); 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /src/main/antlr3/gov/nasa/jpf/constraints/parser/Expression.g: -------------------------------------------------------------------------------- 1 | grammar Expression; 2 | 3 | options { 4 | output=AST; 5 | ASTLabelType=CommonTree; 6 | backtrack=true; 7 | } 8 | 9 | tokens { 10 | TYPED_VAR; 11 | TYPED_VAR_LIST; 12 | UNARY_MINUS; 13 | UNARY_PLUS; 14 | TYPE_CAST; 15 | BVXOR; 16 | ROOT; 17 | } 18 | 19 | @header { 20 | package gov.nasa.jpf.constraints.parser; 21 | } 22 | 23 | 24 | 25 | @lexer::header { 26 | package gov.nasa.jpf.constraints.parser; 27 | } 28 | 29 | start 30 | : root_lexpression EOF!; 31 | 32 | start_aexpression 33 | : root_aexpression EOF!; 34 | 35 | start_variable 36 | : root_variable EOF!; 37 | 38 | root_lexpression 39 | : declare_stmt? lexpression -> ^(ROOT declare_stmt? lexpression) 40 | ; 41 | 42 | root_aexpression 43 | : declare_stmt? aexpression -> ^(ROOT declare_stmt? aexpression) 44 | ; 45 | 46 | root_variable 47 | : typed_var -> ^(ROOT typed_var) 48 | ; 49 | 50 | declare_stmt 51 | : DECLARE! typed_var_list IN! 52 | ; 53 | 54 | lexpression 55 | : lexpr_quantifier 56 | ; 57 | 58 | 59 | lexpr_quantifier 60 | : (FORALL | EXISTS)^ LPAREN! typed_var_list RPAREN! COLON! lexpr_quantifier 61 | | lexpr_cmp 62 | ; 63 | 64 | lexpr_cmp: 65 | lexpr_or ((LIMP|LEQ)^ lexpr_or)* 66 | ; 67 | 68 | lexpr_or 69 | : lexpr_and (LOR^ lexpr_and)* 70 | ; 71 | 72 | lexpr_and 73 | : lexpr_xor (LAND^ lexpr_xor)* 74 | ; 75 | 76 | lexpr_xor 77 | : lexpr_unary (LXOR^ lexpr_unary)* 78 | ; 79 | 80 | lexpr_unary 81 | : LNOT^ lexpr_unary 82 | | lexpr_atomic; 83 | 84 | lexpr_atomic 85 | : (TRUE|FALSE)^ 86 | | aexpression ((EQ|NE|LE|LT|GE|GT)^ aexpression)? 87 | ; 88 | 89 | aexpression 90 | : aexpr_bvor; 91 | 92 | aexpr_bvor 93 | : aexpr_bvxor (BVOR^ aexpr_bvxor)* 94 | ; 95 | 96 | aexpr_bvxor 97 | : aexpr_bvand (LXOR aexpr_bvand)+ -> ^(BVXOR aexpr_bvand*) 98 | | aexpr_bvand 99 | ; 100 | 101 | aexpr_bvand 102 | : aexpr_bvshift (BVAND^ aexpr_bvshift)* 103 | ; 104 | 105 | aexpr_bvshift 106 | : aexpr_add ((BVSHL|BVSHR|BVSHUR)^ aexpr_add)* 107 | ; 108 | 109 | aexpr_add 110 | : aexpr_mul ((ADD|SUB)^ aexpr_mul)* 111 | ; 112 | 113 | aexpr_mul 114 | : aexpr_unary ((MUL|DIV|REM)^ aexpr_unary)* 115 | ; 116 | 117 | aexpr_unary 118 | : SUB aexpr_unary -> ^(UNARY_MINUS aexpr_unary) 119 | | ADD aexpr_unary -> ^(UNARY_PLUS aexpr_unary) 120 | | BVNEG^ aexpr_unary 121 | | LPAREN ID RPAREN aexpr_unary -> ^(TYPE_CAST ID aexpr_unary) 122 | | aexpr_atomic 123 | ; 124 | 125 | aexpr_atomic 126 | : aexpr_literal 127 | | identifier 128 | | LPAREN! lexpression RPAREN! 129 | ; 130 | 131 | aexpr_literal 132 | : (BYTE_LITERAL|SHORT_LITERAL|INT_LITERAL|LONG_LITERAL|BIGINT_LITERAL|FLOAT_LITERAL|DOUBLE_LITERAL|BIGDECIMAL_LITERAL)^ 133 | ; 134 | 135 | 136 | identifier 137 | : ID^ 138 | | QID^; 139 | 140 | typed_var 141 | : identifier COLON ID -> ^(TYPED_VAR identifier ID) 142 | ; 143 | 144 | 145 | typed_var_list 146 | : typed_var (COMMA typed_var)* -> ^(TYPED_VAR_LIST typed_var+) 147 | ; 148 | 149 | 150 | DECLARE : 'declare'; 151 | IN : 'in'; 152 | 153 | LPAREN : '('; 154 | RPAREN : ')'; 155 | 156 | COLON : ':'; 157 | COMMA : ','; 158 | 159 | EQ : '=='; 160 | NE : '!='; 161 | GE : '>='; 162 | LE : '<='; 163 | GT : '>'; 164 | LT : '<'; 165 | 166 | 167 | LAND : '&&'; 168 | LOR : '||'; 169 | LNOT : '!'; 170 | LIMP : '->'; 171 | LEQ : '<->'; 172 | LXOR : '^'; 173 | 174 | ADD : '+'; 175 | SUB : '-'; 176 | MUL : '*'; 177 | DIV : '/'; 178 | REM : '%'; 179 | 180 | BVSHL : '<<'; 181 | BVSHR : '>>'; 182 | BVSHUR : '>>>'; 183 | 184 | BVNEG : '~'; 185 | BVAND : '&'; 186 | BVOR : '|'; 187 | 188 | TRUE : 'true' 189 | ; 190 | 191 | FALSE : 'false' 192 | ; 193 | 194 | FORALL : 'forall' 195 | ; 196 | 197 | EXISTS : 'exists' 198 | ; 199 | 200 | 201 | 202 | ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_'|'.')* 203 | ; 204 | 205 | fragment NZDIGIT 206 | : '1'..'9'; 207 | 208 | fragment DIGIT 209 | : '0'|NZDIGIT; 210 | 211 | fragment INTEGER 212 | : ('0'|NZDIGIT DIGIT*); 213 | 214 | BYTE_LITERAL 215 | : INTEGER 'b'; 216 | 217 | SHORT_LITERAL 218 | : INTEGER 's'; 219 | 220 | INT_LITERAL 221 | : INTEGER; 222 | 223 | LONG_LITERAL 224 | : INTEGER 'L'; 225 | 226 | BIGINT_LITERAL 227 | : INTEGER 'I'; 228 | 229 | 230 | fragment DECIMAL 231 | : INTEGER? '.' DIGIT+; 232 | 233 | fragment REAL : (INTEGER|DECIMAL) EXPONENT?; 234 | 235 | 236 | 237 | FLOAT_LITERAL 238 | : REAL 'f'; 239 | 240 | DOUBLE_LITERAL 241 | : REAL 'd'; 242 | 243 | BIGDECIMAL_LITERAL 244 | : REAL; 245 | 246 | 247 | 248 | 249 | fragment QUOTE 250 | : '\''; 251 | fragment SPACE 252 | : (' '|'\r'|'\t'|'\u000C'|'\n'); 253 | 254 | WS : SPACE+ {$channel=HIDDEN;}; 255 | QID : QUOTE (options {greedy=false;} : . )* QUOTE; 256 | 257 | 258 | 259 | fragment 260 | EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ; -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/api/Variable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.api; 18 | 19 | import gov.nasa.jpf.constraints.java.ObjectConstraints; 20 | import gov.nasa.jpf.constraints.types.Type; 21 | 22 | import java.io.IOException; 23 | import java.util.Collection; 24 | 25 | /** 26 | * named variable 27 | */ 28 | public class Variable extends Expression { 29 | 30 | public static Variable create(Type type, String name) { 31 | return new Variable(type, name); 32 | } 33 | 34 | public static Variable createAnonymous(Type type) { 35 | return new Variable(type); 36 | } 37 | 38 | private final Type type; 39 | 40 | private final String name; 41 | 42 | 43 | public Variable(Type type) { 44 | this(type, null); 45 | } 46 | 47 | public Variable(Type type, String name) { 48 | this.type = type; 49 | this.name = name; 50 | } 51 | 52 | @Override 53 | public E evaluate(Valuation values) { 54 | return values.getValue(this); 55 | } 56 | 57 | @Override 58 | public void collectFreeVariables(Collection> variables) { 59 | variables.add(this); 60 | } 61 | 62 | @Override 63 | public boolean equals(Object obj) { 64 | if(obj == this) { 65 | return true; 66 | } 67 | if (obj == null) { 68 | return false; 69 | } 70 | // Special case: Anonymous variables are only equal to their INSTANCE 71 | if(name == null) { 72 | return false; 73 | } 74 | 75 | if(obj.getClass() != Variable.class) { 76 | return false; 77 | } 78 | final Variable other = (Variable) obj; 79 | 80 | if(!this.type.equals(other.type)) 81 | return false; 82 | 83 | return this.name.equals(other.name); 84 | } 85 | 86 | @Override 87 | public int hashCode() { 88 | if(name == null) // Identity-based equals/hashCode semantics for anonymous variables 89 | return super.hashCode(); 90 | 91 | int hash = 7; 92 | hash = 47 * hash + (this.type != null ? this.type.hashCode() : 0); 93 | hash = 47 * hash + (this.name != null ? this.name.hashCode() : 0); 94 | return hash; 95 | } 96 | 97 | 98 | public String getName() { 99 | return this.name; 100 | } 101 | 102 | @Override 103 | public Expression[] getChildren() { 104 | return NO_CHILDREN; 105 | } 106 | 107 | @Override 108 | public Expression duplicate(Expression[] newChildren) { 109 | assert newChildren.length == 0; 110 | return this; 111 | } 112 | 113 | @Override 114 | public Type getType() { 115 | return type; 116 | } 117 | 118 | @Override 119 | public void print(Appendable a, int flags) throws IOException { 120 | boolean qid = quoteIdentifiers(flags); 121 | if(qid) 122 | a.append('\''); 123 | a.append(name); 124 | if(qid) 125 | a.append('\''); 126 | if(includeVariableType(flags)) { 127 | a.append(':'); 128 | a.append(type.getName()); 129 | } 130 | } 131 | 132 | @Override 133 | public void printMalformedExpression(Appendable a, int flags) 134 | throws IOException{ 135 | boolean qid = quoteIdentifiers(flags); 136 | if(qid) 137 | a.append('\''); 138 | if(name == null){ 139 | a.append("null"); 140 | }else { 141 | a.append(name); 142 | } 143 | if(qid) 144 | a.append('\''); 145 | if(includeVariableType(flags)) { 146 | a.append(':'); 147 | if(type == null){ 148 | a.append("null"); 149 | } else { 150 | a.append(type.getName()); 151 | } 152 | } 153 | } 154 | 155 | @Override 156 | public R accept(ExpressionVisitor visitor, D data) { 157 | return visitor.visit(this, data); 158 | } 159 | 160 | public void printTyped(Appendable a) throws IOException { 161 | print(a, DEFAULT_FLAGS | INCLUDE_VARIABLE_TYPE); 162 | } 163 | 164 | 165 | 166 | 167 | 168 | // LEGACY API 169 | 170 | @Deprecated 171 | public Variable(Class clazz, String name) { 172 | this(ObjectConstraints.getPrimitiveType(clazz), name); 173 | } 174 | 175 | } 176 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/util/ExpressionRestrictionVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.util; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.Variable; 21 | import gov.nasa.jpf.constraints.expressions.AbstractExpressionVisitor; 22 | import gov.nasa.jpf.constraints.expressions.PropositionalCompound; 23 | import gov.nasa.jpf.constraints.expressions.QuantifierExpression; 24 | 25 | import com.google.common.base.Predicate; 26 | import com.google.common.base.Predicates; 27 | 28 | class ExpressionRestrictionVisitor extends AbstractExpressionVisitor,Predicate>> { 29 | 30 | private static final ExpressionRestrictionVisitor INSTANCE = new ExpressionRestrictionVisitor(); 31 | 32 | public static ExpressionRestrictionVisitor getInstance() { 33 | return INSTANCE; 34 | } 35 | 36 | //private boolean mixedParams = false; 37 | 38 | /* (non-Javadoc) 39 | * @see gov.nasa.jpf.constraints.expressions.AbstractExpressionVisitor#visit(gov.nasa.jpf.constraints.api.Variable, java.lang.Object) 40 | */ 41 | @Override 42 | public Expression visit(Variable v, Predicate> data) { 43 | return (data.apply(v)) ? v : null; 44 | } 45 | 46 | /* (non-Javadoc) 47 | * @see gov.nasa.jpf.constraints.expressions.AbstractExpressionVisitor#visit(gov.nasa.jpf.constraints.expressions.QuantifierExpression, java.lang.Object) 48 | */ 49 | @Override 50 | public Expression visit(QuantifierExpression q, Predicate> data) { 51 | Predicate> newPred = Predicates.or(Predicates.in(q.getBoundVariables()), data); 52 | Expression exp = visit(q.getBody(), newPred); 53 | return (exp != null) ? q.duplicate(new Expression[]{exp}) : null; 54 | } 55 | 56 | /* (non-Javadoc) 57 | * @see gov.nasa.jpf.constraints.expressions.AbstractExpressionVisitor#defaultVisit(gov.nasa.jpf.constraints.api.Expression, java.lang.Object) 58 | */ 59 | @Override 60 | protected Expression defaultVisit(Expression expression, 61 | Predicate> data) { 62 | Expression[] children = expression.getChildren(); 63 | if(children.length == 0) 64 | return expression; 65 | 66 | Expression c1 = children[0]; 67 | Expression v1 = visit(c1, data); 68 | 69 | if(children.length == 1) 70 | return (v1 != null) ? expression.duplicate(new Expression[]{v1}) : null; 71 | 72 | Expression c2 = children[1]; 73 | Expression v2 = visit(c2, data); 74 | if(v1 == c1 && v2 == c2) 75 | return expression; 76 | 77 | if(v1 == null) { 78 | if(v2 != null && ExpressionUtil.containsVars(v2)) 79 | throw new MixedParamsException(); 80 | return null; 81 | } 82 | if(v2 == null) { 83 | if(ExpressionUtil.containsVars(v1)) 84 | throw new MixedParamsException(); 85 | return null; 86 | } 87 | 88 | return expression.duplicate(new Expression[]{v1, v2}); 89 | } 90 | 91 | /* (non-Javadoc) 92 | * @see gov.nasa.jpf.constraints.expressions.AbstractExpressionVisitor#visit(gov.nasa.jpf.constraints.expressions.PropositionalCompound, java.lang.Object) 93 | */ 94 | @Override 95 | @SuppressWarnings("unchecked") 96 | public Expression visit(PropositionalCompound n, Predicate> data) { 97 | Expression c1 = n.getLeft(); 98 | Expression v1 = (Expression)visit(c1, data); 99 | 100 | Expression c2 = n.getRight(); 101 | Expression v2 = (Expression)visit(c2, data); 102 | if(v1 == c1 && v2 == c2) 103 | return n; 104 | 105 | if(v1 == null) 106 | return v2; 107 | if(v2 == null) 108 | return v1; 109 | 110 | return new PropositionalCompound(v1, n.getOperator(), v2); 111 | } 112 | 113 | // public boolean hasMixedParams() { 114 | // return mixedParams; 115 | // } 116 | 117 | 118 | @SuppressWarnings("unchecked") 119 | public Expression apply(Expression expr, Predicate> pred) { 120 | return (Expression)visit(expr, pred); 121 | } 122 | 123 | } 124 | -------------------------------------------------------------------------------- /src/main/java/gov/nasa/jpf/constraints/expressions/AbstractExpressionVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015, United States Government, as represented by the 3 | * Administrator of the National Aeronautics and Space Administration. 4 | * All rights reserved. 5 | * 6 | * The PSYCO: A Predicate-based Symbolic Compositional Reasoning environment 7 | * platform is licensed under the Apache License, Version 2.0 (the "License"); you 8 | * may not use this file except in compliance with the License. You may obtain a 9 | * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | package gov.nasa.jpf.constraints.expressions; 18 | 19 | import gov.nasa.jpf.constraints.api.Expression; 20 | import gov.nasa.jpf.constraints.api.ExpressionVisitor; 21 | import gov.nasa.jpf.constraints.api.Variable; 22 | import gov.nasa.jpf.constraints.expressions.functions.FunctionExpression; 23 | 24 | /** 25 | * abstract base for visitors 26 | */ 27 | public abstract class AbstractExpressionVisitor implements ExpressionVisitor { 28 | 29 | /* (non-Javadoc) 30 | * @see gov.nasa.jpf.constraints.api.ExpressionVisitor#visit(gov.nasa.jpf.constraints.api.Variable, D) 31 | */ 32 | @Override 33 | public R visit(Variable v, D data) { return defaultVisit(v, data); } 34 | 35 | /* (non-Javadoc) 36 | * @see gov.nasa.jpf.constraints.api.ExpressionVisitor#visit(gov.nasa.jpf.constraints.expressions.Constant, D) 37 | */ 38 | @Override 39 | public R visit(Constant c, D data) { return defaultVisit(c, data); } 40 | 41 | /* (non-Javadoc) 42 | * @see gov.nasa.jpf.constraints.api.ExpressionVisitor#visit(gov.nasa.jpf.constraints.expressions.Negation, D) 43 | */ 44 | @Override 45 | public R visit(Negation n, D data) { return defaultVisit(n, data); } 46 | 47 | /* (non-Javadoc) 48 | * @see gov.nasa.jpf.constraints.api.ExpressionVisitor#visit(gov.nasa.jpf.constraints.expressions.NumericBooleanExpression, D) 49 | */ 50 | @Override 51 | public 52 | R visit(NumericBooleanExpression n, D data) { return defaultVisit(n, data); } 53 | 54 | /* (non-Javadoc) 55 | * @see gov.nasa.jpf.constraints.api.ExpressionVisitor#visit(gov.nasa.jpf.constraints.expressions.CastExpression, D) 56 | */ 57 | @Override 58 | public 59 | R visit(CastExpression cast, D data) { return defaultVisit(cast, data); } 60 | 61 | /* (non-Javadoc) 62 | * @see gov.nasa.jpf.constraints.api.ExpressionVisitor#visit(gov.nasa.jpf.constraints.expressions.NumericCompound, D) 63 | */ 64 | @Override 65 | public 66 | R visit(NumericCompound n, D data) { return defaultVisit(n, data); } 67 | 68 | /* (non-Javadoc) 69 | * @see gov.nasa.jpf.constraints.api.ExpressionVisitor#visit(gov.nasa.jpf.constraints.expressions.IfThenElse, D) 70 | */ 71 | @Override 72 | public 73 | R visit(IfThenElse n, D data) { return defaultVisit(n, data); } 74 | 75 | /* (non-Javadoc) 76 | * @see gov.nasa.jpf.constraints.api.ExpressionVisitor#visit(gov.nasa.jpf.constraints.expressions.PropositionalCompound, D) 77 | */ 78 | @Override 79 | public R visit(PropositionalCompound n, D data) { return defaultVisit(n, data); } 80 | 81 | /* (non-Javadoc) 82 | * @see gov.nasa.jpf.constraints.api.ExpressionVisitor#visit(gov.nasa.jpf.constraints.expressions.UnaryMinus, D) 83 | */ 84 | @Override 85 | public 86 | R visit(UnaryMinus n, D data) { return defaultVisit(n, data); } 87 | 88 | /* (non-Javadoc) 89 | * @see gov.nasa.jpf.constraints.api.ExpressionVisitor#visit(gov.nasa.jpf.constraints.expressions.QuantifierExpression, D) 90 | */ 91 | @Override 92 | public R visit(QuantifierExpression q, D data) { return defaultVisit(q, data); } 93 | 94 | @Override 95 | public R visit(FunctionExpression f, D data) { return defaultVisit(f, data); } 96 | 97 | 98 | /* (non-Javadoc) 99 | * @see gov.nasa.jpf.constraints.api.ExpressionVisitor#visit(gov.nasa.jpf.constraints.expressions.BitvectorExpression, java.lang.Object) 100 | */ 101 | @Override 102 | public R visit( 103 | BitvectorExpression bv, D data) { 104 | return defaultVisit(bv, data); 105 | } 106 | 107 | /* (non-Javadoc) 108 | * @see gov.nasa.jpf.constraints.api.ExpressionVisitor#visit(gov.nasa.jpf.constraints.expressions.BitvectorNegation, java.lang.Object) 109 | */ 110 | @Override 111 | public R visit( 112 | BitvectorNegation n, D data) { 113 | return defaultVisit(n, data); 114 | } 115 | 116 | 117 | protected R defaultVisit(Expression expression, D data) { 118 | //System.err.println("Visit: " + expression); 119 | throw new UnsupportedOperationException(); 120 | } 121 | 122 | protected final R visit(Expression expression, D data) { 123 | return expression.accept(this, data); 124 | } 125 | 126 | protected final R visit(Expression expression) { 127 | return visit(expression, null); 128 | } 129 | 130 | 131 | } 132 | --------------------------------------------------------------------------------