aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/itc/graphimpl/Graph.java
blob: 91604cb2b1e40ffb6a6104b6f2fa4927b39981a1 (plain) (blame)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*******************************************************************************
 * Copyright (c) 2010-2012, Tamas Szabo, Istvan Rath and Daniel Varro
 * 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.itc.graphimpl;

import tools.refinery.viatra.runtime.rete.itc.igraph.IGraphDataSource;
import tools.refinery.viatra.runtime.rete.itc.igraph.IGraphObserver;
import tools.refinery.viatra.runtime.rete.itc.igraph.IBiDirectionalGraphDataSource;
import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory;
import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory.MemoryType;
import tools.refinery.viatra.runtime.matchers.util.IMemoryView;
import tools.refinery.viatra.runtime.matchers.util.IMultiLookup;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Graph<V> implements IGraphDataSource<V>, IBiDirectionalGraphDataSource<V> {

    // source -> target -> count
    private IMultiLookup<V, V> outgoingEdges;
    // target -> source -> count
    private IMultiLookup<V, V> incomingEdges;

    private Set<V> nodes;

    private List<IGraphObserver<V>> observers;

    public Graph() {
        outgoingEdges = CollectionsFactory.createMultiLookup(Object.class, MemoryType.MULTISETS, Object.class);
        incomingEdges = CollectionsFactory.createMultiLookup(Object.class, MemoryType.MULTISETS, Object.class);
        nodes = CollectionsFactory.createSet();
        observers = CollectionsFactory.createObserverList();
    }

    public void insertEdge(V source, V target) {
        outgoingEdges.addPair(source, target);
        incomingEdges.addPair(target, source);

        for (IGraphObserver<V> go : observers) {
            go.edgeInserted(source, target);
        }
    }

    /**
     * No-op if trying to delete edge that does not exist
     *
     * @since 2.0
     * @see #deleteEdgeIfExists(Object, Object)
     */
    public void deleteEdgeIfExists(V source, V target) {
        boolean containedEdge = outgoingEdges.lookupOrEmpty(source).containsNonZero(target);
        if (containedEdge) {
            deleteEdgeThatExists(source, target);
        }
    }

    /**
     * @throws IllegalStateException
     *             if trying to delete edge that does not exist
     * @since 2.0
     * @see #deleteEdgeIfExists(Object, Object)
     */
    public void deleteEdgeThatExists(V source, V target) {
        outgoingEdges.removePair(source, target);
        incomingEdges.removePair(target, source);
        for (IGraphObserver<V> go : observers) {
            go.edgeDeleted(source, target);
        }
    }

    /**
     * @deprecated use explicitly {@link #deleteEdgeThatExists(Object, Object)} or
     *             {@link #deleteEdgeIfExists(Object, Object)} instead. To preserve backwards compatibility, this method
     *             delegates to the latter.
     *
     */
    @Deprecated
    public void deleteEdge(V source, V target) {
        deleteEdgeIfExists(source, target);
    }

    /**
     * Insert the given node into the graph.
     */
    public void insertNode(V node) {
        if (nodes.add(node)) {
            for (IGraphObserver<V> go : observers) {
                go.nodeInserted(node);
            }
        }
    }

    /**
     * Deletes the given node AND all of the edges going in and out from the node.
     */
    public void deleteNode(V node) {
        if (nodes.remove(node)) {
            IMemoryView<V> incomingView = incomingEdges.lookup(node);
            if (incomingView != null) {
                Map<V, Integer> incoming = CollectionsFactory.createMap(incomingView.asMap());

                for (Entry<V, Integer> entry : incoming.entrySet()) {
                    for (int i = 0; i < entry.getValue(); i++) {
                        deleteEdgeThatExists(entry.getKey(), node);
                    }
                }
            }

            IMemoryView<V> outgoingView = outgoingEdges.lookup(node);
            if (outgoingView != null) {
                Map<V, Integer> outgoing = CollectionsFactory.createMap(outgoingView.asMap());

                for (Entry<V, Integer> entry : outgoing.entrySet()) {
                    for (int i = 0; i < entry.getValue(); i++) {
                        deleteEdgeThatExists(node, entry.getKey());
                    }
                }
            }

            for (IGraphObserver<V> go : observers) {
                go.nodeDeleted(node);
            }
        }
    }

    @Override
    public void attachObserver(IGraphObserver<V> go) {
        observers.add(go);
    }

    @Override
    public void attachAsFirstObserver(IGraphObserver<V> observer) {
        observers.add(0, observer);
    }

    @Override
    public void detachObserver(IGraphObserver<V> go) {
        observers.remove(go);
    }

    @Override
    public Set<V> getAllNodes() {
        return nodes;
    }

    @Override
    public IMemoryView<V> getTargetNodes(V source) {
        return outgoingEdges.lookupOrEmpty(source);
    }

    @Override
    public IMemoryView<V> getSourceNodes(V target) {
        return incomingEdges.lookupOrEmpty(target);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("nodes = ");
        for (V n : getAllNodes()) {
            sb.append(n.toString());
            sb.append(" ");
        }
        sb.append(" edges = ");
        for (V source : outgoingEdges.distinctKeys()) {
            IMemoryView<V> targets = outgoingEdges.lookup(source);
            for (V target : targets.distinctValues()) {
                int count = targets.getCount(target);
                for (int i = 0; i < count; i++) {
                    sb.append("(" + source + "," + target + ") ");
                }
            }
        }
        return sb.toString();
    }

}