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
|
/*******************************************************************************
* Copyright (c) 2010-2012, Tamas Szabo, Gabor Bergmann, Istvan Rath and Daniel Varro
* Copyright (c) 2023 The Refinery Authors <https://refinery.tools/>
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-v20.html.
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package tools.refinery.viatra.runtime.rete.single;
import tools.refinery.viatra.runtime.rete.itc.alg.representative.RepresentativeElectionAlgorithm;
import tools.refinery.viatra.runtime.rete.itc.alg.representative.RepresentativeObserver;
import tools.refinery.viatra.runtime.rete.itc.graphimpl.Graph;
import tools.refinery.viatra.runtime.matchers.tuple.Tuple;
import tools.refinery.viatra.runtime.matchers.tuple.Tuples;
import tools.refinery.viatra.runtime.matchers.util.Clearable;
import tools.refinery.viatra.runtime.matchers.util.Direction;
import tools.refinery.viatra.runtime.matchers.util.timeline.Timeline;
import tools.refinery.viatra.runtime.rete.network.ReinitializedNode;
import tools.refinery.viatra.runtime.rete.network.ReteContainer;
import tools.refinery.viatra.runtime.rete.network.communication.Timestamp;
import java.util.Collection;
import java.util.Map;
public class RepresentativeElectionNode extends SingleInputNode implements Clearable, RepresentativeObserver,
ReinitializedNode {
private final RepresentativeElectionAlgorithm.Factory algorithmFactory;
private Graph<Object> graph;
private RepresentativeElectionAlgorithm algorithm;
public RepresentativeElectionNode(ReteContainer reteContainer,
RepresentativeElectionAlgorithm.Factory algorithmFactory) {
super(reteContainer);
this.algorithmFactory = algorithmFactory;
graph = new Graph<>();
algorithm = algorithmFactory.create(graph);
algorithm.setObserver(this);
reteContainer.registerClearable(this);
}
@Override
public void networkStructureChanged() {
if (reteContainer.isTimelyEvaluation() && reteContainer.getCommunicationTracker().isInRecursiveGroup(this)) {
throw new IllegalStateException(this + " cannot be used in recursive differential dataflow evaluation!");
}
super.networkStructureChanged();
}
@Override
public void reinitializeWith(Collection<Tuple> tuples) {
algorithm.dispose();
graph = new Graph<>();
for (var tuple : tuples) {
insertEdge(tuple.get(0), tuple.get(1));
}
algorithm = algorithmFactory.create(graph);
algorithm.setObserver(this);
}
@Override
public void tupleChanged(Object source, Object representative, Direction direction) {
var tuple = Tuples.staticArityFlatTupleOf(source, representative);
propagateUpdate(direction, tuple, Timestamp.ZERO);
}
@Override
public void clear() {
algorithm.dispose();
graph = new Graph<>();
algorithm = algorithmFactory.create(graph);
}
@Override
public void update(Direction direction, Tuple updateElement, Timestamp timestamp) {
var source = updateElement.get(0);
var target = updateElement.get(1);
switch (direction) {
case INSERT -> insertEdge(source, target);
case DELETE -> deleteEdge(source, target);
default -> throw new IllegalArgumentException("Unknown direction: " + direction);
}
}
private void insertEdge(Object source, Object target) {
graph.insertNode(source);
graph.insertNode(target);
graph.insertEdge(source, target);
}
private void deleteEdge(Object source, Object target) {
graph.deleteEdgeIfExists(source, target);
if (isIsolated(source)) {
graph.deleteNode(source);
}
if (!source.equals(target) && isIsolated(target)) {
graph.deleteNode(target);
}
}
private boolean isIsolated(Object node) {
return graph.getTargetNodes(node).isEmpty() && graph.getSourceNodes(node).isEmpty();
}
@Override
public void pullInto(Collection<Tuple> collector, boolean flush) {
for (var entry : algorithm.getComponents().entrySet()) {
var representative = entry.getKey();
for (var node : entry.getValue()) {
collector.add(Tuples.staticArityFlatTupleOf(node, representative));
}
}
}
@Override
public void pullIntoWithTimeline(Map<Tuple, Timeline<Timestamp>> collector, boolean flush) {
// Use all zero timestamps because this node cannot be used in recursive groups anyway.
for (var entry : algorithm.getComponents().entrySet()) {
var representative = entry.getKey();
for (var node : entry.getValue()) {
collector.put(Tuples.staticArityFlatTupleOf(node, representative), Timestamp.INSERT_AT_ZERO_TIMELINE);
}
}
}
}
|