diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/queries/PDisjunction.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/queries/PDisjunction.java | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/queries/PDisjunction.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/queries/PDisjunction.java new file mode 100644 index 00000000..eae4eacf --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/queries/PDisjunction.java | |||
@@ -0,0 +1,104 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2014, Zoltan Ujhelyi, Istvan Rath and Daniel Varro | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.matchers.psystem.queries; | ||
10 | |||
11 | import java.util.Collections; | ||
12 | import java.util.LinkedHashSet; | ||
13 | import java.util.Set; | ||
14 | import java.util.stream.Collectors; | ||
15 | |||
16 | import tools.refinery.viatra.runtime.matchers.psystem.PBody; | ||
17 | |||
18 | /** | ||
19 | * | ||
20 | * A disjunction is a set of bodies representing separate conditions. A {@link PQuery} has a single, canonical | ||
21 | * PDisjunction, that can be replaced using rewriter | ||
22 | * | ||
23 | * @author Zoltan Ujhelyi | ||
24 | * | ||
25 | */ | ||
26 | public class PDisjunction { | ||
27 | |||
28 | private Set<PBody> bodies; | ||
29 | private PQuery query; | ||
30 | |||
31 | public PDisjunction(Set<PBody> bodies) { | ||
32 | this(bodies.iterator().next().getPattern(), bodies); | ||
33 | } | ||
34 | |||
35 | public PDisjunction(PQuery query, Set<PBody> bodies) { | ||
36 | super(); | ||
37 | this.query = query; | ||
38 | this.bodies = Collections.unmodifiableSet(new LinkedHashSet<>(bodies)); | ||
39 | this.bodies.forEach(body -> body.setContainerDisjunction(this)); | ||
40 | } | ||
41 | |||
42 | /** | ||
43 | * Returns an immutable set of bodies that consists of this disjunction | ||
44 | * | ||
45 | * @return the bodies | ||
46 | */ | ||
47 | public Set<PBody> getBodies() { | ||
48 | return bodies; | ||
49 | } | ||
50 | |||
51 | /** | ||
52 | * Returns the corresponding query specification. May be null if not set. | ||
53 | */ | ||
54 | public PQuery getQuery() { | ||
55 | return query; | ||
56 | } | ||
57 | |||
58 | /** | ||
59 | * Returns all queries directly referred in the constraints. They are all required to evaluate this query | ||
60 | * | ||
61 | * @return a non-null, but possibly empty list of query definitions | ||
62 | */ | ||
63 | public Set<PQuery> getDirectReferredQueries() { | ||
64 | return this.getBodies().stream(). | ||
65 | flatMap(PQueries.directlyReferencedQueriesFunction()). // flatten stream of streams | ||
66 | collect(Collectors.toCollection(LinkedHashSet::new)); | ||
67 | } | ||
68 | |||
69 | /** | ||
70 | * Returns all queries required to evaluate this query (transitively). | ||
71 | * | ||
72 | * @return a non-null, but possibly empty list of query definitions | ||
73 | */ | ||
74 | public Set<PQuery> getAllReferredQueries() { | ||
75 | Set<PQuery> processedQueries = new LinkedHashSet<>(); | ||
76 | processedQueries.add(this.getQuery()); | ||
77 | Set<PQuery> foundQueries = getDirectReferredQueries(); | ||
78 | Set<PQuery> newQueries = new LinkedHashSet<>(foundQueries); | ||
79 | |||
80 | while(!processedQueries.containsAll(newQueries)) { | ||
81 | PQuery query = newQueries.iterator().next(); | ||
82 | processedQueries.add(query); | ||
83 | newQueries.remove(query); | ||
84 | Set<PQuery> referred = query.getDirectReferredQueries(); | ||
85 | referred.removeAll(processedQueries); | ||
86 | foundQueries.addAll(referred); | ||
87 | newQueries.addAll(referred); | ||
88 | } | ||
89 | return foundQueries; | ||
90 | } | ||
91 | |||
92 | /** | ||
93 | * Decides whether a disjunction is mutable. A disjunction is mutable if all its contained bodies are mutable. | ||
94 | * | ||
95 | */ | ||
96 | public boolean isMutable() { | ||
97 | for (PBody body : bodies) { | ||
98 | if (!body.isMutable()) { | ||
99 | return false; | ||
100 | } | ||
101 | } | ||
102 | return true; | ||
103 | } | ||
104 | } | ||