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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
/*
* SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
*
* SPDX-License-Identifier: EPL-2.0
*/
package tools.refinery.store.reasoning.interpretation;
import tools.refinery.store.map.Cursor;
import tools.refinery.store.query.ModelQueryAdapter;
import tools.refinery.store.query.dnf.Query;
import tools.refinery.store.query.resultset.ResultSet;
import tools.refinery.store.reasoning.ReasoningAdapter;
import tools.refinery.store.reasoning.literal.Concreteness;
import tools.refinery.store.reasoning.representation.PartialSymbol;
import tools.refinery.store.representation.TruthValue;
import tools.refinery.store.tuple.Tuple;
public class QueryBasedRelationInterpretationFactory implements PartialInterpretation.Factory<TruthValue, Boolean> {
private final Query<Boolean> may;
private final Query<Boolean> must;
private final Query<Boolean> candidateMay;
private final Query<Boolean> candidateMust;
public QueryBasedRelationInterpretationFactory(
Query<Boolean> may, Query<Boolean> must, Query<Boolean> candidateMay, Query<Boolean> candidateMust) {
this.may = may;
this.must = must;
this.candidateMay = candidateMay;
this.candidateMust = candidateMust;
}
@Override
public PartialInterpretation<TruthValue, Boolean> create(
ReasoningAdapter adapter, Concreteness concreteness, PartialSymbol<TruthValue, Boolean> partialSymbol) {
var queryEngine = adapter.getModel().getAdapter(ModelQueryAdapter.class);
ResultSet<Boolean> mayResultSet;
ResultSet<Boolean> mustResultSet;
switch (concreteness) {
case PARTIAL -> {
mayResultSet = queryEngine.getResultSet(may);
mustResultSet = queryEngine.getResultSet(must);
}
case CANDIDATE -> {
mayResultSet = queryEngine.getResultSet(candidateMay);
mustResultSet = queryEngine.getResultSet(candidateMust);
}
default -> throw new IllegalArgumentException("Unknown concreteness: " + concreteness);
}
if (mayResultSet.equals(mustResultSet)) {
return new TwoValuedInterpretation(adapter, concreteness, partialSymbol, mustResultSet);
} else {
return new FourValuedInterpretation(
adapter, concreteness, partialSymbol, mayResultSet, mustResultSet);
}
}
private static class TwoValuedInterpretation extends AbstractPartialInterpretation<TruthValue, Boolean> {
private final ResultSet<Boolean> resultSet;
protected TwoValuedInterpretation(
ReasoningAdapter adapter, Concreteness concreteness, PartialSymbol<TruthValue, Boolean> partialSymbol,
ResultSet<Boolean> resultSet) {
super(adapter, concreteness, partialSymbol);
this.resultSet = resultSet;
}
@Override
public TruthValue get(Tuple key) {
return TruthValue.toTruthValue(resultSet.get(key));
}
@Override
public Cursor<Tuple, TruthValue> getAll() {
return new TwoValuedCursor(resultSet.getAll());
}
private record TwoValuedCursor(Cursor<Tuple, Boolean> cursor) implements Cursor<Tuple, TruthValue> {
@Override
public Tuple getKey() {
return cursor.getKey();
}
@Override
public TruthValue getValue() {
return TruthValue.toTruthValue(cursor.getValue());
}
@Override
public boolean isTerminated() {
return cursor.isTerminated();
}
@Override
public boolean move() {
return cursor.move();
}
}
}
private static class FourValuedInterpretation extends AbstractPartialInterpretation<TruthValue, Boolean> {
private final ResultSet<Boolean> mayResultSet;
private final ResultSet<Boolean> mustResultSet;
public FourValuedInterpretation(
ReasoningAdapter adapter, Concreteness concreteness, PartialSymbol<TruthValue, Boolean> partialSymbol,
ResultSet<Boolean> mayResultSet, ResultSet<Boolean> mustResultSet) {
super(adapter, concreteness, partialSymbol);
this.mayResultSet = mayResultSet;
this.mustResultSet = mustResultSet;
}
@Override
public TruthValue get(Tuple key) {
boolean isMay = mayResultSet.get(key);
boolean isMust = mustResultSet.get(key);
if (isMust) {
return isMay ? TruthValue.TRUE : TruthValue.ERROR;
} else {
return isMay ? TruthValue.UNKNOWN : TruthValue.FALSE;
}
}
@Override
public Cursor<Tuple, TruthValue> getAll() {
return new FourValuedCursor();
}
private final class FourValuedCursor implements Cursor<Tuple, TruthValue> {
private final Cursor<Tuple, Boolean> mayCursor;
private Cursor<Tuple, Boolean> mustCursor;
private FourValuedCursor() {
this.mayCursor = mayResultSet.getAll();
}
@Override
public Tuple getKey() {
return mustCursor == null ? mayCursor.getKey() : mustCursor.getKey();
}
@Override
public TruthValue getValue() {
if (mustCursor != null) {
return TruthValue.ERROR;
}
if (Boolean.TRUE.equals(mustResultSet.get(mayCursor.getKey()))) {
return TruthValue.TRUE;
}
return TruthValue.UNKNOWN;
}
@Override
public boolean isTerminated() {
return mustCursor != null && mustCursor.isTerminated();
}
@Override
public boolean move() {
if (mayCursor.isTerminated()) {
return moveMust();
}
if (mayCursor.move()) {
return true;
}
mustCursor = mustResultSet.getAll();
return moveMust();
}
private boolean moveMust() {
while (mustCursor.move()) {
// We already iterated over {@code TRUE} truth values with {@code mayCursor}.
if (!Boolean.TRUE.equals(mayResultSet.get(mustCursor.getKey()))) {
return true;
}
}
return false;
}
}
}
}
|