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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
|
/*
* SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
*
* SPDX-License-Identifier: EPL-2.0
*/
package tools.refinery.language.semantics.model;
import com.google.inject.Inject;
import org.eclipse.collections.api.factory.primitive.ObjectIntMaps;
import org.eclipse.collections.api.map.primitive.MutableObjectIntMap;
import tools.refinery.language.model.problem.*;
import tools.refinery.language.semantics.model.internal.MutableSeed;
import tools.refinery.language.utils.BuiltinSymbols;
import tools.refinery.language.utils.ProblemDesugarer;
import tools.refinery.language.utils.ProblemUtil;
import tools.refinery.store.dse.propagation.PropagationBuilder;
import tools.refinery.store.model.ModelStoreBuilder;
import tools.refinery.store.query.Constraint;
import tools.refinery.store.query.dnf.InvalidClauseException;
import tools.refinery.store.query.dnf.Query;
import tools.refinery.store.query.dnf.RelationalQuery;
import tools.refinery.store.query.literal.*;
import tools.refinery.store.query.term.NodeVariable;
import tools.refinery.store.query.term.Variable;
import tools.refinery.store.reasoning.ReasoningAdapter;
import tools.refinery.store.reasoning.representation.AnyPartialSymbol;
import tools.refinery.store.reasoning.representation.PartialRelation;
import tools.refinery.store.reasoning.scope.ScopePropagator;
import tools.refinery.store.reasoning.seed.ModelSeed;
import tools.refinery.store.reasoning.seed.Seed;
import tools.refinery.store.reasoning.translator.containment.ContainmentHierarchyTranslator;
import tools.refinery.store.reasoning.translator.metamodel.Metamodel;
import tools.refinery.store.reasoning.translator.metamodel.MetamodelBuilder;
import tools.refinery.store.reasoning.translator.metamodel.MetamodelTranslator;
import tools.refinery.store.reasoning.translator.multiobject.MultiObjectTranslator;
import tools.refinery.store.reasoning.translator.multiplicity.ConstrainedMultiplicity;
import tools.refinery.store.reasoning.translator.multiplicity.Multiplicity;
import tools.refinery.store.reasoning.translator.multiplicity.UnconstrainedMultiplicity;
import tools.refinery.store.reasoning.translator.predicate.PredicateTranslator;
import tools.refinery.store.representation.TruthValue;
import tools.refinery.store.representation.cardinality.CardinalityInterval;
import tools.refinery.store.representation.cardinality.CardinalityIntervals;
import tools.refinery.store.representation.cardinality.UpperCardinalities;
import tools.refinery.store.tuple.Tuple;
import java.util.*;
public class ModelInitializer {
@Inject
private ProblemDesugarer desugarer;
@Inject
private SemanticsUtils semanticsUtils;
private Problem problem;
private ModelStoreBuilder storeBuilder;
private BuiltinSymbols builtinSymbols;
private PartialRelation nodeRelation;
private final MutableObjectIntMap<Node> nodeTrace = ObjectIntMaps.mutable.empty();
private final Map<Relation, RelationInfo> relationInfoMap = new LinkedHashMap<>();
private final Map<PartialRelation, RelationInfo> partialRelationInfoMap = new HashMap<>();
private final Map<AnyPartialSymbol, Relation> inverseTrace = new HashMap<>();
private Map<Relation, PartialRelation> relationTrace;
private final MetamodelBuilder metamodelBuilder = Metamodel.builder();
private Metamodel metamodel;
private final Map<Tuple, CardinalityInterval> countSeed = new LinkedHashMap<>();
private ScopePropagator scopePropagator;
private ModelSeed modelSeed;
public Problem getProblem() {
return problem;
}
public int getNodeCount() {
return nodeTrace.size();
}
public MutableObjectIntMap<Node> getNodeTrace() {
return nodeTrace;
}
public Map<Relation, PartialRelation> getRelationTrace() {
return relationTrace;
}
public Relation getInverseTrace(AnyPartialSymbol partialRelation) {
return inverseTrace.get(partialRelation);
}
public Metamodel getMetamodel() {
return metamodel;
}
public void readProblem(Problem problem) {
this.problem = problem;
builtinSymbols = desugarer.getBuiltinSymbols(problem).orElseThrow(() -> new IllegalArgumentException(
"Problem has no builtin library"));
var nodeInfo = collectPartialRelation(builtinSymbols.node(), 1, TruthValue.TRUE, TruthValue.TRUE);
nodeRelation = nodeInfo.partialRelation();
metamodelBuilder.type(nodeRelation);
putRelationInfo(builtinSymbols.exists(), new RelationInfo(ReasoningAdapter.EXISTS_SYMBOL, null,
TruthValue.TRUE));
putRelationInfo(builtinSymbols.equals(), new RelationInfo(ReasoningAdapter.EQUALS_SYMBOL,
(TruthValue) null,
null));
putRelationInfo(builtinSymbols.contained(), new RelationInfo(ContainmentHierarchyTranslator.CONTAINED_SYMBOL,
null, TruthValue.UNKNOWN));
putRelationInfo(builtinSymbols.contains(), new RelationInfo(ContainmentHierarchyTranslator.CONTAINS_SYMBOL,
null, TruthValue.UNKNOWN));
putRelationInfo(builtinSymbols.invalidContainer(),
new RelationInfo(ContainmentHierarchyTranslator.INVALID_CONTAINER, TruthValue.FALSE,
TruthValue.FALSE));
collectNodes();
collectPartialSymbols();
collectMetamodel();
metamodel = metamodelBuilder.build();
collectAssertions();
relationTrace = new LinkedHashMap<>(relationInfoMap.size());
int nodeCount = getNodeCount();
var modelSeedBuilder = ModelSeed.builder(nodeCount);
for (var entry : relationInfoMap.entrySet()) {
var relation = entry.getKey();
var info = entry.getValue();
var partialRelation = info.partialRelation();
relationTrace.put(relation, partialRelation);
modelSeedBuilder.seed(partialRelation, info.toSeed(nodeCount));
}
collectScopes();
modelSeedBuilder.seed(MultiObjectTranslator.COUNT_SYMBOL, builder -> builder
.reducedValue(CardinalityIntervals.SET)
.putAll(countSeed));
modelSeed = modelSeedBuilder.build();
}
public void configureStoreBuilder(ModelStoreBuilder storeBuilder) {
this.storeBuilder = storeBuilder;
storeBuilder.with(new MultiObjectTranslator());
storeBuilder.with(new MetamodelTranslator(metamodel));
if (scopePropagator != null) {
if (storeBuilder.tryGetAdapter(PropagationBuilder.class).isEmpty()) {
throw new TracedException(problem, "Type scopes require a PropagationBuilder");
}
storeBuilder.with(scopePropagator);
}
collectPredicates();
}
public ModelSeed createModel(Problem problem, ModelStoreBuilder storeBuilder) {
readProblem(problem);
configureStoreBuilder(storeBuilder);
return getModelSeed();
}
public ModelSeed getModelSeed() {
return modelSeed;
}
private void collectNodes() {
for (var statement : problem.getStatements()) {
if (statement instanceof IndividualDeclaration individualDeclaration) {
for (var individual : individualDeclaration.getNodes()) {
collectNode(individual);
}
} else if (statement instanceof ClassDeclaration classDeclaration) {
var newNode = classDeclaration.getNewNode();
if (newNode != null) {
collectNode(newNode);
}
} else if (statement instanceof EnumDeclaration enumDeclaration) {
for (var literal : enumDeclaration.getLiterals()) {
collectNode(literal);
}
}
}
for (var node : problem.getNodes()) {
collectNode(node);
}
}
private void collectNode(Node node) {
nodeTrace.getIfAbsentPut(node, this::getNodeCount);
}
private void collectPartialSymbols() {
for (var statement : problem.getStatements()) {
if (statement instanceof ClassDeclaration classDeclaration) {
collectClassDeclarationSymbols(classDeclaration);
} else if (statement instanceof EnumDeclaration enumDeclaration) {
collectPartialRelation(enumDeclaration, 1, TruthValue.FALSE, TruthValue.FALSE);
} else if (statement instanceof PredicateDefinition predicateDefinition) {
collectPredicateDefinitionSymbol(predicateDefinition);
}
}
}
private void collectClassDeclarationSymbols(ClassDeclaration classDeclaration) {
collectPartialRelation(classDeclaration, 1, null, TruthValue.UNKNOWN);
for (var featureDeclaration : classDeclaration.getFeatureDeclarations()) {
if (featureDeclaration instanceof ReferenceDeclaration referenceDeclaration) {
collectPartialRelation(referenceDeclaration, 2, null, TruthValue.UNKNOWN);
var invalidMultiplicityConstraint = referenceDeclaration.getInvalidMultiplicity();
if (invalidMultiplicityConstraint != null) {
collectPartialRelation(invalidMultiplicityConstraint, 1, TruthValue.FALSE, TruthValue.FALSE);
}
} else {
throw new TracedException(featureDeclaration, "Unknown feature declaration");
}
}
}
private void collectPredicateDefinitionSymbol(PredicateDefinition predicateDefinition) {
int arity = predicateDefinition.getParameters().size();
if (predicateDefinition.isError()) {
collectPartialRelation(predicateDefinition, arity, TruthValue.FALSE, TruthValue.FALSE);
} else {
collectPartialRelation(predicateDefinition, arity, null, TruthValue.UNKNOWN);
}
}
private void putRelationInfo(Relation relation, RelationInfo info) {
relationInfoMap.put(relation, info);
partialRelationInfoMap.put(info.partialRelation(), info);
inverseTrace.put(info.partialRelation(), relation);
}
private RelationInfo collectPartialRelation(Relation relation, int arity, TruthValue value,
TruthValue defaultValue) {
return relationInfoMap.computeIfAbsent(relation, key -> {
var name = getName(relation);
var info = new RelationInfo(name, arity, value, defaultValue);
partialRelationInfoMap.put(info.partialRelation(), info);
inverseTrace.put(info.partialRelation(), relation);
return info;
});
}
private String getName(Relation relation) {
return semanticsUtils.getName(relation).orElseGet(() -> "::" + relationInfoMap.size());
}
private void collectMetamodel() {
for (var statement : problem.getStatements()) {
if (statement instanceof ClassDeclaration classDeclaration) {
collectClassDeclarationMetamodel(classDeclaration);
} else if (statement instanceof EnumDeclaration enumDeclaration) {
collectEnumMetamodel(enumDeclaration);
}
}
}
private void collectEnumMetamodel(EnumDeclaration enumDeclaration) {
try {
metamodelBuilder.type(getPartialRelation(enumDeclaration), nodeRelation);
} catch (RuntimeException e) {
throw TracedException.addTrace(enumDeclaration, e);
}
}
private void collectClassDeclarationMetamodel(ClassDeclaration classDeclaration) {
var superTypes = classDeclaration.getSuperTypes();
var partialSuperTypes = new ArrayList<PartialRelation>(superTypes.size() + 1);
partialSuperTypes.add(nodeRelation);
for (var superType : superTypes) {
partialSuperTypes.add(getPartialRelation(superType));
}
try {
metamodelBuilder.type(getPartialRelation(classDeclaration), classDeclaration.isAbstract(),
partialSuperTypes);
} catch (RuntimeException e) {
throw TracedException.addTrace(classDeclaration, e);
}
for (var featureDeclaration : classDeclaration.getFeatureDeclarations()) {
if (featureDeclaration instanceof ReferenceDeclaration referenceDeclaration) {
collectReferenceDeclarationMetamodel(classDeclaration, referenceDeclaration);
}
}
}
private void collectReferenceDeclarationMetamodel(ClassDeclaration classDeclaration,
ReferenceDeclaration referenceDeclaration) {
var relation = getPartialRelation(referenceDeclaration);
var source = getPartialRelation(classDeclaration);
var target = getPartialRelation(referenceDeclaration.getReferenceType());
boolean containment = referenceDeclaration.getKind() == ReferenceKind.CONTAINMENT;
var opposite = referenceDeclaration.getOpposite();
PartialRelation oppositeRelation = null;
if (opposite != null) {
oppositeRelation = getPartialRelation(opposite);
}
var multiplicity = getMultiplicityConstraint(referenceDeclaration);
try {
metamodelBuilder.reference(relation, source, containment, multiplicity, target, oppositeRelation);
} catch (RuntimeException e) {
throw TracedException.addTrace(classDeclaration, e);
}
}
private Multiplicity getMultiplicityConstraint(ReferenceDeclaration referenceDeclaration) {
if (!ProblemUtil.hasMultiplicityConstraint(referenceDeclaration)) {
return UnconstrainedMultiplicity.INSTANCE;
}
var problemMultiplicity = referenceDeclaration.getMultiplicity();
CardinalityInterval interval;
if (problemMultiplicity == null) {
interval = CardinalityIntervals.LONE;
} else {
interval = getCardinalityInterval(problemMultiplicity);
}
var constraint = getRelationInfo(referenceDeclaration.getInvalidMultiplicity()).partialRelation();
return ConstrainedMultiplicity.of(interval, constraint);
}
private static CardinalityInterval getCardinalityInterval(
tools.refinery.language.model.problem.Multiplicity problemMultiplicity) {
if (problemMultiplicity instanceof ExactMultiplicity exactMultiplicity) {
return CardinalityIntervals.exactly(exactMultiplicity.getExactValue());
} else if (problemMultiplicity instanceof RangeMultiplicity rangeMultiplicity) {
var upperBound = rangeMultiplicity.getUpperBound();
return CardinalityIntervals.between(rangeMultiplicity.getLowerBound(),
upperBound < 0 ? UpperCardinalities.UNBOUNDED : UpperCardinalities.atMost(upperBound));
} else {
throw new TracedException(problemMultiplicity, "Unknown multiplicity");
}
}
private void collectAssertions() {
for (var statement : problem.getStatements()) {
if (statement instanceof ClassDeclaration classDeclaration) {
collectClassDeclarationAssertions(classDeclaration);
} else if (statement instanceof EnumDeclaration enumDeclaration) {
collectEnumAssertions(enumDeclaration);
} else if (statement instanceof IndividualDeclaration individualDeclaration) {
for (var individual : individualDeclaration.getNodes()) {
collectIndividualAssertions(individual);
}
} else if (statement instanceof Assertion assertion) {
collectAssertion(assertion);
}
}
}
private void collectClassDeclarationAssertions(ClassDeclaration classDeclaration) {
var newNode = classDeclaration.getNewNode();
if (newNode == null) {
return;
}
var newNodeId = getNodeId(newNode);
collectCardinalityAssertions(newNodeId, TruthValue.UNKNOWN);
var tuple = Tuple.of(newNodeId);
mergeValue(classDeclaration, tuple, TruthValue.TRUE);
var typeInfo = metamodel.typeHierarchy().getAnalysisResult(getPartialRelation(classDeclaration));
for (var subType : typeInfo.getDirectSubtypes()) {
partialRelationInfoMap.get(subType).assertions().mergeValue(tuple, TruthValue.FALSE);
}
}
private void collectEnumAssertions(EnumDeclaration enumDeclaration) {
var overlay = MutableSeed.of(1, null);
for (var literal : enumDeclaration.getLiterals()) {
collectIndividualAssertions(literal);
var nodeId = getNodeId(literal);
overlay.mergeValue(Tuple.of(nodeId), TruthValue.TRUE);
}
var info = getRelationInfo(enumDeclaration);
info.assertions().overwriteValues(overlay);
}
private void collectIndividualAssertions(Node node) {
var nodeId = getNodeId(node);
collectCardinalityAssertions(nodeId, TruthValue.TRUE);
}
private void collectCardinalityAssertions(int nodeId, TruthValue value) {
mergeValue(builtinSymbols.exists(), Tuple.of(nodeId), value);
mergeValue(builtinSymbols.equals(), Tuple.of(nodeId, nodeId), value);
}
private void collectAssertion(Assertion assertion) {
var tuple = getTuple(assertion);
var value = getTruthValue(assertion.getValue());
var relation = assertion.getRelation();
var info = getRelationInfo(relation);
var partialRelation = info.partialRelation();
if (partialRelation.arity() != tuple.getSize()) {
throw new TracedException(assertion, "Expected %d arguments for %s, got %d instead"
.formatted(partialRelation.arity(), partialRelation, tuple.getSize()));
}
if (assertion.isDefault()) {
info.defaultAssertions().mergeValue(tuple, value);
} else {
info.assertions().mergeValue(tuple, value);
}
}
private void mergeValue(Relation relation, Tuple key, TruthValue value) {
getRelationInfo(relation).assertions().mergeValue(key, value);
}
private RelationInfo getRelationInfo(Relation relation) {
var info = relationInfoMap.get(relation);
if (info == null) {
throw new IllegalArgumentException("Unknown relation: " + relation);
}
return info;
}
private PartialRelation getPartialRelation(Relation relation) {
return getRelationInfo(relation).partialRelation();
}
private int getNodeId(Node node) {
return nodeTrace.getOrThrow(node);
}
private Tuple getTuple(Assertion assertion) {
var arguments = assertion.getArguments();
int arity = arguments.size();
var nodes = new int[arity];
for (int i = 0; i < arity; i++) {
var argument = arguments.get(i);
if (argument instanceof NodeAssertionArgument nodeArgument) {
nodes[i] = getNodeId(nodeArgument.getNode());
} else if (argument instanceof WildcardAssertionArgument) {
nodes[i] = -1;
} else {
throw new TracedException(argument, "Unsupported assertion argument");
}
}
return Tuple.of(nodes);
}
private static TruthValue getTruthValue(Expr expr) {
if (!(expr instanceof LogicConstant logicAssertionValue)) {
return TruthValue.ERROR;
}
return switch (logicAssertionValue.getLogicValue()) {
case TRUE -> TruthValue.TRUE;
case FALSE -> TruthValue.FALSE;
case UNKNOWN -> TruthValue.UNKNOWN;
case ERROR -> TruthValue.ERROR;
};
}
private void collectPredicates() {
for (var statement : problem.getStatements()) {
if (statement instanceof PredicateDefinition predicateDefinition) {
collectPredicateDefinitionTraced(predicateDefinition);
}
}
}
private void collectPredicateDefinitionTraced(PredicateDefinition predicateDefinition) {
try {
collectPredicateDefinition(predicateDefinition);
} catch (InvalidClauseException e) {
int clauseIndex = e.getClauseIndex();
var bodies = predicateDefinition.getBodies();
if (clauseIndex < bodies.size()) {
throw new TracedException(bodies.get(clauseIndex), e);
} else {
throw new TracedException(predicateDefinition, e);
}
} catch (RuntimeException e) {
throw TracedException.addTrace(predicateDefinition, e);
}
}
private void collectPredicateDefinition(PredicateDefinition predicateDefinition) {
var partialRelation = getPartialRelation(predicateDefinition);
var query = toQuery(partialRelation.name(), predicateDefinition);
boolean mutable;
TruthValue defaultValue;
if (predicateDefinition.isError()) {
mutable = false;
defaultValue = TruthValue.FALSE;
} else {
var seed = modelSeed.getSeed(partialRelation);
defaultValue = seed.reducedValue() == TruthValue.FALSE ? TruthValue.FALSE : TruthValue.UNKNOWN;
var cursor = seed.getCursor(defaultValue, getNodeCount());
// The symbol should be mutable if there is at least one non-default entry in the seed.
mutable = cursor.move();
}
var translator = new PredicateTranslator(partialRelation, query, mutable, defaultValue);
storeBuilder.with(translator);
}
private RelationalQuery toQuery(String name, PredicateDefinition predicateDefinition) {
var problemParameters = predicateDefinition.getParameters();
int arity = problemParameters.size();
var parameters = new NodeVariable[arity];
var parameterMap = new HashMap<tools.refinery.language.model.problem.Variable, Variable>(arity);
var commonLiterals = new ArrayList<Literal>();
for (int i = 0; i < arity; i++) {
var problemParameter = problemParameters.get(i);
var parameter = Variable.of(problemParameter.getName());
parameters[i] = parameter;
parameterMap.put(problemParameter, parameter);
var parameterType = problemParameter.getParameterType();
if (parameterType != null) {
var partialType = getPartialRelation(parameterType);
commonLiterals.add(partialType.call(parameter));
}
}
var builder = Query.builder(name).parameters(parameters);
for (var body : predicateDefinition.getBodies()) {
try {
var localScope = extendScope(parameterMap, body.getImplicitVariables());
var problemLiterals = body.getLiterals();
var literals = new ArrayList<>(commonLiterals);
for (var problemLiteral : problemLiterals) {
toLiteralsTraced(problemLiteral, localScope, literals);
}
builder.clause(literals);
} catch (RuntimeException e) {
throw TracedException.addTrace(body, e);
}
}
return builder.build();
}
private Map<tools.refinery.language.model.problem.Variable, Variable> extendScope(
Map<tools.refinery.language.model.problem.Variable, Variable> existing,
Collection<? extends tools.refinery.language.model.problem.Variable> newVariables) {
if (newVariables.isEmpty()) {
return existing;
}
int localScopeSize = existing.size() + newVariables.size();
var localScope = new HashMap<tools.refinery.language.model.problem.Variable, Variable>(localScopeSize);
localScope.putAll(existing);
for (var newVariable : newVariables) {
localScope.put(newVariable, Variable.of(newVariable.getName()));
}
return localScope;
}
private void toLiteralsTraced(Expr expr, Map<tools.refinery.language.model.problem.Variable, Variable> localScope,
List<Literal> literals) {
try {
toLiterals(expr, localScope, literals);
} catch (RuntimeException e) {
throw TracedException.addTrace(expr, e);
}
}
private void toLiterals(Expr expr, Map<tools.refinery.language.model.problem.Variable, Variable> localScope,
List<Literal> literals) {
if (expr instanceof LogicConstant logicConstant) {
switch (logicConstant.getLogicValue()) {
case TRUE -> literals.add(BooleanLiteral.TRUE);
case FALSE -> literals.add(BooleanLiteral.FALSE);
default -> throw new TracedException(logicConstant, "Unsupported literal");
}
} else if (expr instanceof Atom atom) {
var target = getPartialRelation(atom.getRelation());
var polarity = atom.isTransitiveClosure() ? CallPolarity.TRANSITIVE : CallPolarity.POSITIVE;
var argumentList = toArgumentList(atom.getArguments(), localScope, literals);
literals.add(target.call(polarity, argumentList));
} else if (expr instanceof NegationExpr negationExpr) {
var body = negationExpr.getBody();
if (!(body instanceof Atom atom)) {
throw new TracedException(body, "Cannot negate literal");
}
var target = getPartialRelation(atom.getRelation());
Constraint constraint;
if (atom.isTransitiveClosure()) {
constraint = Query.of(target.name() + "#transitive", (builder, p1, p2) -> builder.clause(
target.callTransitive(p1, p2)
)).getDnf();
} else {
constraint = target;
}
var negatedScope = extendScope(localScope, negationExpr.getImplicitVariables());
var argumentList = toArgumentList(atom.getArguments(), negatedScope, literals);
literals.add(constraint.call(CallPolarity.NEGATIVE, argumentList));
} else if (expr instanceof ComparisonExpr comparisonExpr) {
var argumentList = toArgumentList(List.of(comparisonExpr.getLeft(), comparisonExpr.getRight()),
localScope, literals);
boolean positive = switch (comparisonExpr.getOp()) {
case EQ -> true;
case NOT_EQ -> false;
default -> throw new TracedException(
comparisonExpr, "Unsupported operator");
};
literals.add(new EquivalenceLiteral(positive, argumentList.get(0), argumentList.get(1)));
} else {
throw new TracedException(expr, "Unsupported literal");
}
}
private List<Variable> toArgumentList(
List<Expr> expressions, Map<tools.refinery.language.model.problem.Variable, Variable> localScope,
List<Literal> literals) {
var argumentList = new ArrayList<Variable>(expressions.size());
for (var expr : expressions) {
if (!(expr instanceof VariableOrNodeExpr variableOrNodeExpr)) {
throw new TracedException(expr, "Unsupported argument");
}
var variableOrNode = variableOrNodeExpr.getVariableOrNode();
if (variableOrNode instanceof Node node) {
int nodeId = getNodeId(node);
var tempVariable = Variable.of(semanticsUtils.getName(node).orElse("_" + nodeId));
literals.add(new ConstantLiteral(tempVariable, nodeId));
argumentList.add(tempVariable);
} else if (variableOrNode instanceof tools.refinery.language.model.problem.Variable problemVariable) {
if (variableOrNodeExpr.getSingletonVariable() == problemVariable) {
argumentList.add(Variable.of(problemVariable.getName()));
} else {
var variable = localScope.get(problemVariable);
if (variable == null) {
throw new TracedException(variableOrNode, "Unknown variable: " + problemVariable.getName());
}
argumentList.add(variable);
}
} else {
throw new TracedException(variableOrNode, "Unknown argument");
}
}
return argumentList;
}
private void collectScopes() {
for (var statement : problem.getStatements()) {
if (statement instanceof ScopeDeclaration scopeDeclaration) {
for (var typeScope : scopeDeclaration.getTypeScopes()) {
if (typeScope.isIncrement()) {
collectTypeScopeIncrement(typeScope);
} else {
collectTypeScope(typeScope);
}
}
}
}
}
private void collectTypeScopeIncrement(TypeScope typeScope) {
if (!(typeScope.getTargetType() instanceof ClassDeclaration classDeclaration)) {
throw new TracedException(typeScope, "Target of incremental type scope must be a class declaration");
}
var newNode = classDeclaration.getNewNode();
if (newNode == null) {
throw new TracedException(typeScope, "Target of incremental type scope must be concrete class");
}
int newNodeId = nodeTrace.get(newNode);
var type = relationTrace.get(classDeclaration);
var typeInfo = metamodel.typeHierarchy().getAnalysisResult(type);
if (!typeInfo.getDirectSubtypes().isEmpty()) {
throw new TracedException(typeScope, "Target of incremental type scope cannot have any subclasses");
}
var interval = getCardinalityInterval(typeScope.getMultiplicity());
countSeed.compute(Tuple.of(newNodeId), (key, oldValue) ->
oldValue == null ? interval : oldValue.meet(interval));
}
private void collectTypeScope(TypeScope typeScope) {
var type = relationTrace.get(typeScope.getTargetType());
if (type == null) {
throw new TracedException(typeScope, "Unknown target type");
}
var interval = getCardinalityInterval(typeScope.getMultiplicity());
if (scopePropagator == null) {
scopePropagator = new ScopePropagator();
}
scopePropagator.scope(type, interval);
}
private record RelationInfo(PartialRelation partialRelation, MutableSeed<TruthValue> assertions,
MutableSeed<TruthValue> defaultAssertions) {
public RelationInfo(String name, int arity, TruthValue value, TruthValue defaultValue) {
this(new PartialRelation(name, arity), value, defaultValue);
}
public RelationInfo(PartialRelation partialRelation, TruthValue value, TruthValue defaultValue) {
this(partialRelation, MutableSeed.of(partialRelation.arity(), value),
MutableSeed.of(partialRelation.arity(), defaultValue));
}
public Seed<TruthValue> toSeed(int nodeCount) {
defaultAssertions.overwriteValues(assertions);
if (partialRelation.equals(ReasoningAdapter.EQUALS_SYMBOL)) {
for (int i = 0; i < nodeCount; i++) {
defaultAssertions.setIfMissing(Tuple.of(i, i), TruthValue.TRUE);
}
defaultAssertions.setAllMissing(TruthValue.FALSE);
}
return defaultAssertions;
}
}
}
|