diff options
Diffstat (limited to 'subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/misc')
5 files changed, 356 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/misc/Bag.java b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/misc/Bag.java new file mode 100644 index 00000000..4aef0f96 --- /dev/null +++ b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/misc/Bag.java | |||
@@ -0,0 +1,43 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2004-2008 Gabor Bergmann 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 | |||
10 | package tools.refinery.viatra.runtime.rete.misc; | ||
11 | |||
12 | import java.util.Collection; | ||
13 | import java.util.LinkedList; | ||
14 | |||
15 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
16 | import tools.refinery.viatra.runtime.matchers.util.Direction; | ||
17 | import tools.refinery.viatra.runtime.rete.network.ReteContainer; | ||
18 | import tools.refinery.viatra.runtime.rete.network.communication.Timestamp; | ||
19 | |||
20 | /** | ||
21 | * @author Gabor Bergmann | ||
22 | * | ||
23 | * A bag is a container that tuples can be dumped into. Does NOT propagate updates! Optimized for small contents | ||
24 | * size OR positive updates only. | ||
25 | */ | ||
26 | public class Bag extends SimpleReceiver { | ||
27 | |||
28 | public Collection<Tuple> contents; | ||
29 | |||
30 | public Bag(ReteContainer reteContainer) { | ||
31 | super(reteContainer); | ||
32 | contents = new LinkedList<Tuple>(); | ||
33 | } | ||
34 | |||
35 | @Override | ||
36 | public void update(Direction direction, Tuple updateElement, Timestamp timestamp) { | ||
37 | if (direction == Direction.INSERT) | ||
38 | contents.add(updateElement); | ||
39 | else | ||
40 | contents.remove(updateElement); | ||
41 | } | ||
42 | |||
43 | } | ||
diff --git a/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/misc/ConstantNode.java b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/misc/ConstantNode.java new file mode 100644 index 00000000..980d3eee --- /dev/null +++ b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/misc/ConstantNode.java | |||
@@ -0,0 +1,50 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2004-2008 Gabor Bergmann 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 | |||
10 | package tools.refinery.viatra.runtime.rete.misc; | ||
11 | |||
12 | import java.util.Collection; | ||
13 | import java.util.Map; | ||
14 | |||
15 | import tools.refinery.viatra.runtime.matchers.context.IQueryRuntimeContext; | ||
16 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
17 | import tools.refinery.viatra.runtime.matchers.util.timeline.Timeline; | ||
18 | import tools.refinery.viatra.runtime.rete.network.ReteContainer; | ||
19 | import tools.refinery.viatra.runtime.rete.network.StandardNode; | ||
20 | import tools.refinery.viatra.runtime.rete.network.communication.Timestamp; | ||
21 | |||
22 | /** | ||
23 | * Node that always contains a single constant Tuple | ||
24 | * | ||
25 | * @author Gabor Bergmann | ||
26 | */ | ||
27 | public class ConstantNode extends StandardNode { | ||
28 | |||
29 | protected Tuple constant; | ||
30 | |||
31 | /** | ||
32 | * @param constant | ||
33 | * will be wrapped using {@link IQueryRuntimeContext#wrapTuple(Tuple)} | ||
34 | */ | ||
35 | public ConstantNode(ReteContainer reteContainer, Tuple constant) { | ||
36 | super(reteContainer); | ||
37 | this.constant = reteContainer.getNetwork().getEngine().getRuntimeContext().wrapTuple(constant); | ||
38 | } | ||
39 | |||
40 | @Override | ||
41 | public void pullInto(Collection<Tuple> collector, boolean flush) { | ||
42 | collector.add(constant); | ||
43 | } | ||
44 | |||
45 | @Override | ||
46 | public void pullIntoWithTimeline(final Map<Tuple, Timeline<Timestamp>> collector, final boolean flush) { | ||
47 | collector.put(constant, Timestamp.INSERT_AT_ZERO_TIMELINE); | ||
48 | } | ||
49 | |||
50 | } | ||
diff --git a/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/misc/DefaultDeltaMonitor.java b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/misc/DefaultDeltaMonitor.java new file mode 100644 index 00000000..efba3117 --- /dev/null +++ b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/misc/DefaultDeltaMonitor.java | |||
@@ -0,0 +1,43 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2004-2010 Gabor Bergmann 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 | |||
10 | package tools.refinery.viatra.runtime.rete.misc; | ||
11 | |||
12 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
13 | import tools.refinery.viatra.runtime.rete.network.Network; | ||
14 | import tools.refinery.viatra.runtime.rete.network.ReteContainer; | ||
15 | |||
16 | /** | ||
17 | * Default configuration for DeltaMonitor. | ||
18 | * | ||
19 | * @author Gabor Bergmann | ||
20 | * | ||
21 | */ | ||
22 | public class DefaultDeltaMonitor extends DeltaMonitor<Tuple> { | ||
23 | |||
24 | /** | ||
25 | * @param reteContainer | ||
26 | */ | ||
27 | public DefaultDeltaMonitor(ReteContainer reteContainer) { | ||
28 | super(reteContainer); | ||
29 | } | ||
30 | |||
31 | /** | ||
32 | * @param network | ||
33 | */ | ||
34 | public DefaultDeltaMonitor(Network network) { | ||
35 | super(network.getHeadContainer()); | ||
36 | } | ||
37 | |||
38 | @Override | ||
39 | public Tuple statelessConvert(Tuple tuple) { | ||
40 | return tuple; | ||
41 | } | ||
42 | |||
43 | } | ||
diff --git a/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/misc/DeltaMonitor.java b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/misc/DeltaMonitor.java new file mode 100644 index 00000000..82b6ecda --- /dev/null +++ b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/misc/DeltaMonitor.java | |||
@@ -0,0 +1,111 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2004-2008 Gabor Bergmann 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 | |||
10 | package tools.refinery.viatra.runtime.rete.misc; | ||
11 | |||
12 | import java.util.Collection; | ||
13 | import java.util.LinkedHashSet; | ||
14 | |||
15 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
16 | import tools.refinery.viatra.runtime.matchers.util.Clearable; | ||
17 | import tools.refinery.viatra.runtime.matchers.util.Direction; | ||
18 | import tools.refinery.viatra.runtime.rete.network.ReteContainer; | ||
19 | import tools.refinery.viatra.runtime.rete.network.communication.Timestamp; | ||
20 | |||
21 | /** | ||
22 | * A monitoring object that connects to the rete network as a receiver to reflect changes since an arbitrary state | ||
23 | * acknowledged by the client. Match tuples are represented by a type MatchType. | ||
24 | * | ||
25 | * <p> | ||
26 | * <b>Usage</b>. If a new matching is found, it appears in the matchFoundEvents collection, and disappears when that | ||
27 | * particular matching cannot be found anymore. If the event of finding a match has been processed by the client, it can | ||
28 | * be removed manually. In this case, when a previously found matching is lost, the Tuple will appear in the | ||
29 | * matchLostEvents collection, and disappear upon finding the same matching again. "Matching lost" events can also be | ||
30 | * acknowledged by removing a Tuple from the collection. If the matching is found once again, it will return to | ||
31 | * matchFoundEvents. | ||
32 | * | ||
33 | * <p> | ||
34 | * <b>Technical notes</b>. Does NOT propagate updates! | ||
35 | * | ||
36 | * By overriding statelessConvert(), results can be stored to a MatchType. MatchType must provide equals() and | ||
37 | * hashCode() reflecting its contents. The default implementation (DefaultDeltaMonitor) uses Tuple as MatchType. | ||
38 | * | ||
39 | * By overriding statelessFilter(), some tuples can be filtered. | ||
40 | * | ||
41 | * @author Gabor Bergmann | ||
42 | * | ||
43 | */ | ||
44 | public abstract class DeltaMonitor<MatchType> extends SimpleReceiver implements Clearable { | ||
45 | |||
46 | /** | ||
47 | * matches that are newly found | ||
48 | */ | ||
49 | public Collection<MatchType> matchFoundEvents; | ||
50 | /** | ||
51 | * matches that are newly lost | ||
52 | */ | ||
53 | public Collection<MatchType> matchLostEvents; | ||
54 | |||
55 | /** | ||
56 | * @param reteContainer | ||
57 | */ | ||
58 | public DeltaMonitor(ReteContainer reteContainer) { | ||
59 | super(reteContainer); | ||
60 | matchFoundEvents = new LinkedHashSet<MatchType>(); | ||
61 | matchLostEvents = new LinkedHashSet<MatchType>(); | ||
62 | reteContainer.registerClearable(this); | ||
63 | } | ||
64 | |||
65 | // /** | ||
66 | // * Build a delta monitor into the head container of the network. | ||
67 | // * | ||
68 | // * @param network | ||
69 | // */ | ||
70 | // public DeltaMonitor(Network network) { | ||
71 | // this(network.getHeadContainer()); | ||
72 | // } | ||
73 | |||
74 | /** | ||
75 | * Override this method to provide a lightweight, stateless filter on the tuples | ||
76 | * | ||
77 | * @param tuple | ||
78 | * the occurrence that is to be filtered | ||
79 | * @return true if this tuple should be monitored, false if ignored | ||
80 | */ | ||
81 | public boolean statelessFilter(Tuple tuple) { | ||
82 | return true; | ||
83 | } | ||
84 | |||
85 | public abstract MatchType statelessConvert(Tuple tuple); | ||
86 | |||
87 | @Override | ||
88 | public void update(Direction direction, Tuple updateElement, Timestamp timestamp) { | ||
89 | if (statelessFilter(updateElement)) { | ||
90 | MatchType match = statelessConvert(updateElement); | ||
91 | if (direction == Direction.INSERT) { | ||
92 | if (!matchLostEvents.remove(match)) // either had before but | ||
93 | // lost | ||
94 | matchFoundEvents.add(match); // or brand-new | ||
95 | } else // revoke | ||
96 | { | ||
97 | if (!matchFoundEvents.remove(match)) // either never found | ||
98 | // in the first | ||
99 | // place | ||
100 | matchLostEvents.add(match); // or newly lost | ||
101 | } | ||
102 | } | ||
103 | } | ||
104 | |||
105 | @Override | ||
106 | public void clear() { | ||
107 | matchFoundEvents.clear(); | ||
108 | matchLostEvents.clear(); | ||
109 | } | ||
110 | |||
111 | } | ||
diff --git a/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/misc/SimpleReceiver.java b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/misc/SimpleReceiver.java new file mode 100644 index 00000000..dcf9ae78 --- /dev/null +++ b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/misc/SimpleReceiver.java | |||
@@ -0,0 +1,109 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2012, Bergmann Gabor, 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.misc; | ||
10 | |||
11 | import java.util.Collection; | ||
12 | import java.util.Collections; | ||
13 | |||
14 | import tools.refinery.viatra.runtime.rete.network.BaseNode; | ||
15 | import tools.refinery.viatra.runtime.rete.network.Receiver; | ||
16 | import tools.refinery.viatra.runtime.rete.network.ReteContainer; | ||
17 | import tools.refinery.viatra.runtime.rete.network.Supplier; | ||
18 | import tools.refinery.viatra.runtime.rete.network.mailbox.Mailbox; | ||
19 | import tools.refinery.viatra.runtime.rete.network.mailbox.timeless.BehaviorChangingMailbox; | ||
20 | import tools.refinery.viatra.runtime.rete.network.mailbox.timely.TimelyMailbox; | ||
21 | import tools.refinery.viatra.runtime.rete.traceability.TraceInfo; | ||
22 | |||
23 | /** | ||
24 | * @author Bergmann Gabor | ||
25 | * | ||
26 | */ | ||
27 | public abstract class SimpleReceiver extends BaseNode implements Receiver { | ||
28 | |||
29 | protected Supplier parent = null; | ||
30 | /** | ||
31 | * @since 1.6 | ||
32 | */ | ||
33 | protected final Mailbox mailbox; | ||
34 | |||
35 | /** | ||
36 | * @param reteContainer | ||
37 | */ | ||
38 | public SimpleReceiver(ReteContainer reteContainer) { | ||
39 | super(reteContainer); | ||
40 | mailbox = instantiateMailbox(); | ||
41 | reteContainer.registerClearable(mailbox); | ||
42 | } | ||
43 | |||
44 | /** | ||
45 | * Instantiates the {@link Mailbox} of this receiver. | ||
46 | * Subclasses may override this method to provide their own mailbox implementation. | ||
47 | * | ||
48 | * @return the mailbox | ||
49 | * @since 2.0 | ||
50 | */ | ||
51 | protected Mailbox instantiateMailbox() { | ||
52 | if (this.reteContainer.isTimelyEvaluation()) { | ||
53 | return new TimelyMailbox(this, this.reteContainer); | ||
54 | } else { | ||
55 | return new BehaviorChangingMailbox(this, this.reteContainer); | ||
56 | } | ||
57 | } | ||
58 | |||
59 | @Override | ||
60 | public Mailbox getMailbox() { | ||
61 | return this.mailbox; | ||
62 | } | ||
63 | |||
64 | @Override | ||
65 | public void appendParent(Supplier supplier) { | ||
66 | if (parent == null) | ||
67 | parent = supplier; | ||
68 | else | ||
69 | throw new UnsupportedOperationException("Illegal RETE edge: " + this + " already has a parent (" + parent | ||
70 | + ") and cannot connect to additional parent (" + supplier | ||
71 | + ") as it is not a Uniqueness Enforcer Node. "); | ||
72 | } | ||
73 | |||
74 | @Override | ||
75 | public void removeParent(Supplier supplier) { | ||
76 | if (parent == supplier) | ||
77 | parent = null; | ||
78 | else | ||
79 | throw new IllegalArgumentException("Illegal RETE edge removal: the parent of " + this + " is not " | ||
80 | + supplier); | ||
81 | } | ||
82 | |||
83 | @Override | ||
84 | public Collection<Supplier> getParents() { | ||
85 | if (parent == null) | ||
86 | return Collections.emptySet(); | ||
87 | else | ||
88 | return Collections.singleton(parent); | ||
89 | } | ||
90 | |||
91 | /** | ||
92 | * Disconnects this node from the network. Can be called publicly. | ||
93 | * | ||
94 | * @pre: child nodes, if any, must already be disconnected. | ||
95 | */ | ||
96 | public void disconnectFromNetwork() { | ||
97 | if (parent != null) | ||
98 | reteContainer.disconnect(parent, this); | ||
99 | } | ||
100 | |||
101 | @Override | ||
102 | public void assignTraceInfo(TraceInfo traceInfo) { | ||
103 | super.assignTraceInfo(traceInfo); | ||
104 | if (traceInfo.propagateFromStandardNodeToSupplierParent()) | ||
105 | if (parent != null) | ||
106 | parent.acceptPropagatedTraceInfo(traceInfo); | ||
107 | } | ||
108 | |||
109 | } \ No newline at end of file | ||