diff options
Diffstat (limited to 'subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/index/TransitiveClosureNodeIndexer.java')
-rw-r--r-- | subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/index/TransitiveClosureNodeIndexer.java | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/index/TransitiveClosureNodeIndexer.java b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/index/TransitiveClosureNodeIndexer.java new file mode 100644 index 00000000..77202004 --- /dev/null +++ b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/index/TransitiveClosureNodeIndexer.java | |||
@@ -0,0 +1,121 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2012, Tamas Szabo, Gabor Bergmann, 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.rete.index; | ||
10 | |||
11 | import java.util.Collection; | ||
12 | import java.util.Collections; | ||
13 | import java.util.Iterator; | ||
14 | import java.util.Set; | ||
15 | |||
16 | import tools.refinery.viatra.runtime.rete.itc.alg.incscc.IncSCCAlg; | ||
17 | import tools.refinery.viatra.runtime.matchers.tuple.MaskedTuple; | ||
18 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
19 | import tools.refinery.viatra.runtime.matchers.tuple.TupleMask; | ||
20 | import tools.refinery.viatra.runtime.matchers.tuple.Tuples; | ||
21 | import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory; | ||
22 | import tools.refinery.viatra.runtime.matchers.util.Direction; | ||
23 | import tools.refinery.viatra.runtime.rete.network.Receiver; | ||
24 | import tools.refinery.viatra.runtime.rete.single.TransitiveClosureNode; | ||
25 | |||
26 | // UNFINISHED, not used yet | ||
27 | public class TransitiveClosureNodeIndexer extends StandardIndexer implements IterableIndexer { | ||
28 | private TransitiveClosureNode tcNode; | ||
29 | private IncSCCAlg<Object> tcAlg; | ||
30 | private Collection<Tuple> emptySet; | ||
31 | |||
32 | public TransitiveClosureNodeIndexer(TupleMask mask, IncSCCAlg<Object> tcAlg, TransitiveClosureNode tcNode) { | ||
33 | super(tcNode.getContainer(), mask); | ||
34 | this.tcAlg = tcAlg; | ||
35 | this.tcNode = tcNode; | ||
36 | this.emptySet = Collections.emptySet(); | ||
37 | this.parent = tcNode; | ||
38 | } | ||
39 | |||
40 | @Override | ||
41 | public Collection<Tuple> get(Tuple signature) { | ||
42 | if (signature.getSize() == mask.sourceWidth) { | ||
43 | if (mask.indices.length == 0) { | ||
44 | // mask ()/2 | ||
45 | return getSignatures(); | ||
46 | } else if (mask.indices.length == 1) { | ||
47 | Set<Tuple> retSet = CollectionsFactory.createSet(); | ||
48 | |||
49 | // mask (0)/2 | ||
50 | if (mask.indices[0] == 0) { | ||
51 | Object source = signature.get(0); | ||
52 | for (Object target : tcAlg.getAllReachableTargets(source)) { | ||
53 | retSet.add(Tuples.staticArityFlatTupleOf(source, target)); | ||
54 | } | ||
55 | return retSet; | ||
56 | } | ||
57 | // mask (1)/2 | ||
58 | if (mask.indices[0] == 1) { | ||
59 | Object target = signature.get(1); | ||
60 | for (Object source : tcAlg.getAllReachableSources(target)) { | ||
61 | retSet.add(Tuples.staticArityFlatTupleOf(source, target)); | ||
62 | } | ||
63 | return retSet; | ||
64 | } | ||
65 | } else { | ||
66 | // mask (0,1)/2 | ||
67 | if (mask.indices[0] == 0 && mask.indices[1] == 1) { | ||
68 | Object source = signature.get(0); | ||
69 | Object target = signature.get(1); | ||
70 | Tuple singleton = Tuples.staticArityFlatTupleOf(source, target); | ||
71 | return (tcAlg.isReachable(source, target) ? Collections.singleton(singleton) : emptySet); | ||
72 | } | ||
73 | // mask (1,0)/2 | ||
74 | if (mask.indices[0] == 1 && mask.indices[1] == 0) { | ||
75 | Object source = signature.get(1); | ||
76 | Object target = signature.get(0); | ||
77 | Tuple singleton = Tuples.staticArityFlatTupleOf(source, target); | ||
78 | return (tcAlg.isReachable(source, target) ? Collections.singleton(singleton) : emptySet); | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | return null; | ||
83 | } | ||
84 | |||
85 | @Override | ||
86 | public int getBucketCount() { | ||
87 | throw new UnsupportedOperationException(); | ||
88 | } | ||
89 | |||
90 | @Override | ||
91 | public Collection<Tuple> getSignatures() { | ||
92 | return asTupleCollection(tcAlg.getTcRelation()); | ||
93 | } | ||
94 | |||
95 | @Override | ||
96 | public Iterator<Tuple> iterator() { | ||
97 | return asTupleCollection(tcAlg.getTcRelation()).iterator(); | ||
98 | } | ||
99 | |||
100 | private Collection<Tuple> asTupleCollection( | ||
101 | Collection<tools.refinery.viatra.runtime.rete.itc.alg.misc.Tuple<Object>> tuples) { | ||
102 | Set<Tuple> retSet = CollectionsFactory.createSet(); | ||
103 | for (tools.refinery.viatra.runtime.rete.itc.alg.misc.Tuple<Object> tuple : tuples) { | ||
104 | retSet.add(Tuples.staticArityFlatTupleOf(tuple.getSource(), tuple.getTarget())); | ||
105 | } | ||
106 | return retSet; | ||
107 | } | ||
108 | |||
109 | /** | ||
110 | * @since 2.4 | ||
111 | */ | ||
112 | public void propagate(Direction direction, Tuple updateElement, boolean change) { | ||
113 | propagate(direction, updateElement, new MaskedTuple(updateElement, mask), change, null); | ||
114 | } | ||
115 | |||
116 | @Override | ||
117 | public Receiver getActiveNode() { | ||
118 | return tcNode; | ||
119 | } | ||
120 | |||
121 | } | ||