1
+ /*
2
+ * JavaSMT is an API wrapper for a collection of SMT solvers.
3
+ * This file is part of JavaSMT.
4
+ *
5
+ * Copyright (C) 2007-2016 Dirk Beyer
6
+ * All rights reserved.
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ package org .sosy_lab .java_smt .solvers .dreal4 ;
22
+
23
+ import org .sosy_lab .java_smt .basicimpl .AbstractBooleanFormulaManager ;
24
+ import org .sosy_lab .java_smt .basicimpl .FormulaCreator ;
25
+ import org .sosy_lab .java_smt .solvers .dreal4 .drealjni .Context ;
26
+ import org .sosy_lab .java_smt .solvers .dreal4 .drealjni .Formula ;
27
+ import org .sosy_lab .java_smt .solvers .dreal4 .drealjni .Variable .Type ;
28
+ import org .sosy_lab .java_smt .solvers .dreal4 .drealjni .dreal ;
29
+
30
+
31
+ public class DReal4BooleanFormulaManager
32
+ extends AbstractBooleanFormulaManager <DRealTerm , Type , Context , DRealTerm > {
33
+
34
+ protected DReal4BooleanFormulaManager (FormulaCreator pCreator ) {
35
+ super (pCreator );
36
+ }
37
+
38
+ @ Override
39
+ protected DRealTerm makeVariableImpl (String pVar ) {
40
+ return formulaCreator .makeVariable (getFormulaCreator ().getBoolType (), pVar );
41
+ }
42
+
43
+ @ Override
44
+ protected DRealTerm makeBooleanImpl (boolean value ) {
45
+ if (value ) {
46
+ return new DRealTerm (null , null , Formula .True ());
47
+ } else {
48
+ return new DRealTerm (null , null , Formula .False ());
49
+ }
50
+ }
51
+
52
+ @ Override
53
+ protected DRealTerm not (DRealTerm pParam1 ) {
54
+ if (pParam1 .isFormula ()) {
55
+ return new DRealTerm (null , null , dreal .Not (pParam1 .getFormula ()));
56
+ } else if (pParam1 .isVar ()) {
57
+ return new DRealTerm (null , null , dreal .Not (pParam1 .getVariable ()));
58
+ } else {
59
+ throw new UnsupportedOperationException ("dReal does not support not on Expressions." );
60
+ }
61
+ }
62
+
63
+ @ Override
64
+ protected DRealTerm and (DRealTerm pParam1 , DRealTerm pParam2 ) {
65
+ if (pParam1 .isVar () && pParam2 .isFormula ()) {
66
+ return new DRealTerm (null , null , dreal .And (pParam1 .getVariable (), pParam2 .getFormula ()));
67
+ } else if (pParam1 .isFormula () && pParam2 .isVar ()) {
68
+ return new DRealTerm (null , null , dreal .And (pParam1 .getFormula (), pParam2 .getVariable ()));
69
+ } else if (pParam1 .isVar () && pParam2 .isVar ()) {
70
+ return new DRealTerm (null , null , dreal .And (pParam1 .getVariable (), pParam2 .getVariable ()));
71
+ } else if (pParam1 .isFormula () && pParam2 .isFormula ()) {
72
+ return new DRealTerm (null , null , dreal .And (pParam1 .getFormula (), pParam2 .getFormula ()));
73
+ } else {
74
+ throw new UnsupportedOperationException ("dReal does not support and on Expressions." );
75
+ }
76
+ }
77
+
78
+ @ Override
79
+ protected DRealTerm or (DRealTerm pParam1 , DRealTerm pParam2 ) {
80
+ if (pParam1 .isVar () && pParam2 .isFormula ()) {
81
+ return new DRealTerm (null , null , dreal .Or (pParam1 .getVariable (), pParam2 .getFormula ()));
82
+ } else if (pParam1 .isFormula () && pParam2 .isVar ()) {
83
+ return new DRealTerm (null , null , dreal .Or (pParam1 .getFormula (), pParam2 .getVariable ()));
84
+ } else if (pParam1 .isVar () && pParam2 .isVar ()) {
85
+ return new DRealTerm (null , null , dreal .Or (pParam1 .getVariable (), pParam2 .getVariable ()));
86
+ } else if (pParam1 .isFormula () && pParam2 .isFormula ()) {
87
+ return new DRealTerm (null , null , dreal .Or (pParam1 .getFormula (), pParam2 .getFormula ()));
88
+ } else {
89
+ throw new UnsupportedOperationException ("dReal does not support or on Expressions." );
90
+ }
91
+ }
92
+
93
+ // a xor b = (NOT(A AND B)) AND (NOT(NOT A AND NOT B))
94
+ @ Override
95
+ protected DRealTerm xor (DRealTerm pParam1 , DRealTerm pParam2 ) {
96
+ if (pParam1 .isVar () && pParam2 .isFormula ()) {
97
+ return new DRealTerm (null , null , dreal .And (dreal .Not (dreal .And (pParam1 .getVariable (),
98
+ pParam2 .getFormula ())),
99
+ dreal .Not (dreal .And (dreal .Not (pParam1 .getVariable ()),
100
+ dreal .Not (pParam2 .getFormula ())))));
101
+ } else if (pParam1 .isFormula () && pParam2 .isVar ()) {
102
+ return new DRealTerm (null , null , dreal .And (dreal .Not (dreal .And (pParam1 .getFormula (),
103
+ pParam2 .getVariable ())),
104
+ dreal .Not (dreal .And (dreal .Not (pParam1 .getFormula ()),
105
+ dreal .Not (pParam2 .getVariable ())))));
106
+ } else if (pParam1 .isVar () && pParam2 .isVar ()) {
107
+ return new DRealTerm (null , null , dreal .And (dreal .Not (dreal .And (pParam1 .getVariable (),
108
+ pParam2 .getVariable ())),
109
+ dreal .Not (dreal .And (dreal .Not (pParam1 .getVariable ()),
110
+ dreal .Not (pParam2 .getVariable ())))));
111
+ } else if (pParam1 .isFormula () && pParam2 .isFormula ()) {
112
+ return new DRealTerm (null , null , dreal .And (dreal .Not (dreal .And (pParam1 .getFormula (),
113
+ pParam2 .getFormula ())),
114
+ dreal .Not (dreal .And (dreal .Not (pParam1 .getFormula ()),
115
+ dreal .Not (pParam2 .getFormula ())))));
116
+ } else {
117
+ throw new UnsupportedOperationException ("dReal does not support xor on Expressions." );
118
+ }
119
+ }
120
+
121
+ @ Override
122
+ protected DRealTerm equivalence (DRealTerm bits1 , DRealTerm bits2 ) {
123
+ if (bits1 .isVar () && bits2 .isFormula ()) {
124
+ return new DRealTerm (null , null , dreal .iff (bits1 .getVariable (), bits2 .getFormula ()));
125
+ } else if (bits1 .isFormula () && bits2 .isVar ()) {
126
+ return new DRealTerm (null , null , dreal .iff (bits1 .getFormula (), bits2 .getVariable ()));
127
+ } else if (bits1 .isVar () && bits2 .isVar ()) {
128
+ return new DRealTerm (null , null , dreal .iff (bits1 .getVariable (), bits2 .getVariable ()));
129
+ } else if (bits1 .isFormula () && bits2 .isFormula ()) {
130
+ return new DRealTerm (null , null , dreal .iff (bits1 .getFormula (), bits2 .getFormula ()));
131
+ } else {
132
+ throw new UnsupportedOperationException ("dReal does not support iff on Expressions." );
133
+ }
134
+ }
135
+
136
+ @ Override
137
+ protected boolean isTrue (DRealTerm bits ) {
138
+ if (bits .isFormula ()) {
139
+ return dreal .is_true (bits .getFormula ());
140
+ } else {
141
+ throw new UnsupportedOperationException ("dReal does not support isTrue on "
142
+ + "Variables and Expressions." );
143
+ }
144
+ }
145
+
146
+ @ Override
147
+ protected boolean isFalse (DRealTerm bits ) {
148
+ if (bits .isFormula ()) {
149
+ return dreal .is_false (bits .getFormula ());
150
+ } else {
151
+ throw new UnsupportedOperationException ("dReal does not support isTrue on "
152
+ + "Variables and Expressions." );
153
+ }
154
+ }
155
+
156
+ @ Override
157
+ protected DRealTerm ifThenElse (DRealTerm cond , DRealTerm f1 , DRealTerm f2 ) {
158
+ if (cond .isFormula ()) {
159
+ if (dreal .is_true (cond .getFormula ())) {
160
+ if (f1 .isVar ()) {
161
+ return new DRealTerm (f1 .getVariable (), null , null );
162
+ } else if (f1 .isExp ()) {
163
+ return new DRealTerm (null , f1 .getExpression (), null );
164
+ } else {
165
+ return new DRealTerm (null , null , f1 .getFormula ());
166
+ }
167
+ } else {
168
+ if (f2 .isVar ()) {
169
+ return new DRealTerm (f2 .getVariable (), null , null );
170
+ } else if (f2 .isExp ()) {
171
+ return new DRealTerm (null , f2 .getExpression (), null );
172
+ } else {
173
+ return new DRealTerm (null , null , f2 .getFormula ());
174
+ }
175
+ }
176
+ } else {
177
+ throw new UnsupportedOperationException ("dReal does not suppord ifThenElse with first "
178
+ + "argument beeing not a Formula." );
179
+ }
180
+ }
181
+
182
+ }
0 commit comments