-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathAbstractEvaluator.java
140 lines (122 loc) · 4.53 KB
/
AbstractEvaluator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// This file is part of JavaSMT,
// an API wrapper for a collection of SMT solvers:
// https://github.com/sosy-lab/java-smt
//
// SPDX-FileCopyrightText: 2024 Dirk Beyer <https://www.sosy-lab.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.sosy_lab.java_smt.basicimpl;
import com.google.common.base.Preconditions;
import java.math.BigInteger;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.sosy_lab.common.rationals.Rational;
import org.sosy_lab.java_smt.api.ArrayFormula;
import org.sosy_lab.java_smt.api.BitvectorFormula;
import org.sosy_lab.java_smt.api.BooleanFormula;
import org.sosy_lab.java_smt.api.EnumerationFormula;
import org.sosy_lab.java_smt.api.Evaluator;
import org.sosy_lab.java_smt.api.FloatingPointFormula;
import org.sosy_lab.java_smt.api.FloatingPointNumber;
import org.sosy_lab.java_smt.api.Formula;
import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula;
import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula;
import org.sosy_lab.java_smt.api.StringFormula;
@SuppressWarnings("ClassTypeParameterName")
public abstract class AbstractEvaluator<TFormulaInfo, TType, TEnv> implements Evaluator {
private final AbstractProver<?> prover;
protected final FormulaCreator<TFormulaInfo, TType, TEnv, ?> creator;
private boolean closed = false;
protected AbstractEvaluator(
AbstractProver<?> pProver, FormulaCreator<TFormulaInfo, TType, TEnv, ?> creator) {
this.prover = Preconditions.checkNotNull(pProver);
this.creator = Preconditions.checkNotNull(creator);
}
@SuppressWarnings("unchecked")
@Nullable
@Override
public final <T extends Formula> T eval(T f) {
Preconditions.checkState(!isClosed());
TFormulaInfo evaluation = evalImpl(creator.extractInfo(f));
return evaluation == null ? null : (T) creator.encapsulateWithTypeOf(evaluation);
}
@Nullable
@Override
public final BigInteger evaluate(IntegerFormula f) {
Preconditions.checkState(!isClosed());
return (BigInteger) evaluateImpl(creator.extractInfo(f));
}
@Nullable
@Override
public Rational evaluate(RationalFormula f) {
Object value = evaluateImpl(creator.extractInfo(f));
if (value instanceof BigInteger) {
// We simplified the value internally. Here, we need to convert it back to Rational.
return Rational.ofBigInteger((BigInteger) value);
} else {
return (Rational) value;
}
}
@Nullable
@Override
public final Boolean evaluate(BooleanFormula f) {
Preconditions.checkState(!isClosed());
return (Boolean) evaluateImpl(creator.extractInfo(f));
}
@Nullable
@Override
public final String evaluate(StringFormula f) {
Preconditions.checkState(!isClosed());
return (String) evaluateImpl(creator.extractInfo(f));
}
@Nullable
@Override
public final String evaluate(EnumerationFormula f) {
Preconditions.checkState(!isClosed());
return (String) evaluateImpl(creator.extractInfo(f));
}
@Override
public final @Nullable FloatingPointNumber evaluate(FloatingPointFormula f) {
Preconditions.checkState(!isClosed());
return (FloatingPointNumber) evaluateImpl(creator.extractInfo(f));
}
@Nullable
@Override
public final BigInteger evaluate(BitvectorFormula f) {
Preconditions.checkState(!isClosed());
return (BigInteger) evaluateImpl(creator.extractInfo(f));
}
@Nullable
@Override
public final Object evaluate(Formula f) {
Preconditions.checkState(!isClosed());
Preconditions.checkArgument(
!(f instanceof ArrayFormula),
"cannot compute a simple constant evaluation for an array-formula");
return evaluateImpl(creator.extractInfo(f));
}
/**
* Simplify the given formula and replace all symbols with their model values. If a symbol is not
* set in the model and evaluation aborts, return <code>null</code>.
*/
@Nullable
protected abstract TFormulaInfo evalImpl(TFormulaInfo formula);
/**
* Simplify the given formula and replace all symbols with their model values. If a symbol is not
* set in the model and evaluation aborts, return <code>null</code>. Afterwards convert the
* formula into a Java object as far as possible, i.e., try to match a primitive or simple type.
*/
@Nullable
protected final Object evaluateImpl(TFormulaInfo f) {
Preconditions.checkState(!isClosed());
TFormulaInfo evaluatedF = evalImpl(f);
return evaluatedF == null ? null : creator.convertValue(f, evaluatedF);
}
protected boolean isClosed() {
return closed;
}
@Override
public void close() {
prover.unregisterEvaluator(this);
closed = true;
}
}