diff options
Diffstat (limited to 'subprojects/store/src/main/java/tools/refinery/store/query/QueriableModelStoreImpl.java')
-rw-r--r-- | subprojects/store/src/main/java/tools/refinery/store/query/QueriableModelStoreImpl.java | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/subprojects/store/src/main/java/tools/refinery/store/query/QueriableModelStoreImpl.java b/subprojects/store/src/main/java/tools/refinery/store/query/QueriableModelStoreImpl.java new file mode 100644 index 00000000..653783dd --- /dev/null +++ b/subprojects/store/src/main/java/tools/refinery/store/query/QueriableModelStoreImpl.java | |||
@@ -0,0 +1,127 @@ | |||
1 | package tools.refinery.store.query; | ||
2 | |||
3 | import java.util.Collections; | ||
4 | import java.util.HashMap; | ||
5 | import java.util.Map; | ||
6 | import java.util.Set; | ||
7 | |||
8 | import org.eclipse.viatra.query.runtime.api.GenericQuerySpecification; | ||
9 | |||
10 | import tools.refinery.store.model.ModelDiffCursor; | ||
11 | import tools.refinery.store.model.ModelStore; | ||
12 | import tools.refinery.store.model.ModelStoreImpl; | ||
13 | import tools.refinery.store.model.representation.DataRepresentation; | ||
14 | import tools.refinery.store.query.building.DNFAnd; | ||
15 | import tools.refinery.store.query.building.DNFAtom; | ||
16 | import tools.refinery.store.query.building.DNFPredicate; | ||
17 | import tools.refinery.store.query.building.PredicateAtom; | ||
18 | import tools.refinery.store.query.building.RelationAtom; | ||
19 | import tools.refinery.store.query.internal.DNF2PQuery; | ||
20 | import tools.refinery.store.query.internal.QueriableModelImpl; | ||
21 | import tools.refinery.store.query.internal.RawPatternMatcher; | ||
22 | import tools.refinery.store.query.internal.DNF2PQuery.SimplePQuery; | ||
23 | import tools.refinery.store.query.view.RelationView; | ||
24 | |||
25 | public class QueriableModelStoreImpl implements QueriableModelStore { | ||
26 | protected final ModelStore store; | ||
27 | protected final Set<RelationView<?>> relationViews; | ||
28 | protected final Map<DNFPredicate, GenericQuerySpecification<RawPatternMatcher>> predicates; | ||
29 | |||
30 | public QueriableModelStoreImpl(Set<DataRepresentation<?, ?>> dataRepresentations, | ||
31 | Set<RelationView<?>> relationViews, Set<DNFPredicate> predicates) { | ||
32 | this.store = new ModelStoreImpl(dataRepresentations); | ||
33 | validateViews(dataRepresentations, relationViews); | ||
34 | this.relationViews = Collections.unmodifiableSet(relationViews); | ||
35 | validatePredicates(relationViews, predicates); | ||
36 | this.predicates = initPredicates(predicates); | ||
37 | } | ||
38 | |||
39 | private void validateViews(Set<DataRepresentation<?, ?>> dataRepresentations, Set<RelationView<?>> relationViews) { | ||
40 | for (RelationView<?> relationView : relationViews) { | ||
41 | // TODO: make it work for non-relation representation? | ||
42 | if (!dataRepresentations.contains(relationView.getRepresentation())) { | ||
43 | throw new IllegalArgumentException( | ||
44 | DataRepresentation.class.getSimpleName() + " " + relationView.getStringID() + " added to " | ||
45 | + QueriableModelStore.class.getSimpleName() + " without a referred representation."); | ||
46 | } | ||
47 | } | ||
48 | } | ||
49 | |||
50 | private void validatePredicates(Set<RelationView<?>> relationViews, Set<DNFPredicate> predicates) { | ||
51 | for (DNFPredicate dnfPredicate : predicates) { | ||
52 | for (DNFAnd clause : dnfPredicate.getClauses()) { | ||
53 | for (DNFAtom atom : clause.getConstraints()) { | ||
54 | if (atom instanceof RelationAtom relationAtom) { | ||
55 | validateRelationAtom(relationViews, dnfPredicate, relationAtom); | ||
56 | } else if (atom instanceof PredicateAtom predicateAtom) { | ||
57 | validatePredicateAtom(predicates, dnfPredicate, predicateAtom); | ||
58 | } | ||
59 | } | ||
60 | } | ||
61 | } | ||
62 | } | ||
63 | |||
64 | private void validateRelationAtom(Set<RelationView<?>> relationViews, DNFPredicate dnfPredicate, | ||
65 | RelationAtom relationAtom) { | ||
66 | if (!relationViews.contains(relationAtom.getView())) { | ||
67 | throw new IllegalArgumentException(DNFPredicate.class.getSimpleName() + " " | ||
68 | + dnfPredicate.getUniqueName() + " contains reference to a view of " | ||
69 | + relationAtom.getView().getRepresentation().getName() | ||
70 | + " that is not in the model."); | ||
71 | } | ||
72 | } | ||
73 | private void validatePredicateAtom(Set<DNFPredicate> predicates, DNFPredicate dnfPredicate, | ||
74 | PredicateAtom predicateAtom) { | ||
75 | if (!predicates.contains(predicateAtom.getReferred())) { | ||
76 | throw new IllegalArgumentException( | ||
77 | DNFPredicate.class.getSimpleName() + " " + dnfPredicate.getUniqueName() | ||
78 | + " contains reference to a predicate " | ||
79 | + predicateAtom.getReferred().getName() | ||
80 | + "that is not in the model."); | ||
81 | } | ||
82 | } | ||
83 | |||
84 | private Map<DNFPredicate, GenericQuerySpecification<RawPatternMatcher>> initPredicates(Set<DNFPredicate> predicates) { | ||
85 | Map<DNFPredicate, GenericQuerySpecification<RawPatternMatcher>> result = new HashMap<>(); | ||
86 | Map<DNFPredicate, SimplePQuery> dnf2PQueryMap = new HashMap<>(); | ||
87 | for (DNFPredicate dnfPredicate : predicates) { | ||
88 | GenericQuerySpecification<RawPatternMatcher> query = DNF2PQuery.translate(dnfPredicate,dnf2PQueryMap).build(); | ||
89 | result.put(dnfPredicate, query); | ||
90 | } | ||
91 | |||
92 | return result; | ||
93 | } | ||
94 | |||
95 | @Override | ||
96 | public Set<DataRepresentation<?, ?>> getDataRepresentations() { | ||
97 | return store.getDataRepresentations(); | ||
98 | } | ||
99 | @Override | ||
100 | public Set<RelationView<?>> getViews() { | ||
101 | return this.relationViews; | ||
102 | } | ||
103 | @Override | ||
104 | public Set<DNFPredicate> getPredicates() { | ||
105 | return predicates.keySet(); | ||
106 | } | ||
107 | |||
108 | @Override | ||
109 | public QueriableModel createModel() { | ||
110 | return new QueriableModelImpl(this, this.store.createModel(), predicates); | ||
111 | } | ||
112 | |||
113 | @Override | ||
114 | public QueriableModel createModel(long state) { | ||
115 | return new QueriableModelImpl(this, this.store.createModel(state), predicates); | ||
116 | } | ||
117 | |||
118 | @Override | ||
119 | public synchronized Set<Long> getStates() { | ||
120 | return this.store.getStates(); | ||
121 | } | ||
122 | |||
123 | @Override | ||
124 | public synchronized ModelDiffCursor getDiffCursor(long from, long to) { | ||
125 | return this.store.getDiffCursor(from, to); | ||
126 | } | ||
127 | } | ||