aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/itc/igraph/IBiDirectionalWrapper.java
blob: c4315ca233741ec758de5d73e6bbaf45804f5e1c (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
/*******************************************************************************
 * 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.igraph;

import java.util.Set;

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;

/**
 * This class can be used to wrap an {@link IGraphDataSource} into an {@link IBiDirectionalGraphDataSource}. This class
 * provides support for the retrieval of source nodes for a given target which is not supported by standard
 * {@link IGraphDataSource} implementations.
 *
 * @author Tamas Szabo
 *
 * @param <V>
 *            the type parameter of the nodes in the graph data source
 */
public class IBiDirectionalWrapper<V> implements IBiDirectionalGraphDataSource<V>, IGraphObserver<V> {

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

    public IBiDirectionalWrapper(IGraphDataSource<V> gds) {
        this.wrappedDataSource = gds;

        this.incomingEdges = CollectionsFactory.createMultiLookup(
                Object.class, MemoryType.MULTISETS, Object.class);

        if (gds.getAllNodes() != null) {
            for (V source : gds.getAllNodes()) {
                IMemoryView<V> targets = gds.getTargetNodes(source);
                for (V target : targets.distinctValues()) {
                    int count = targets.getCount(target);
                    for (int i = 0; i < count; i++) {
                        edgeInserted(source, target);
                    }
                }
            }
        }

        gds.attachAsFirstObserver(this);
    }

    @Override
    public void attachObserver(IGraphObserver<V> observer) {
        wrappedDataSource.attachObserver(observer);
    }

    @Override
    public void attachAsFirstObserver(IGraphObserver<V> observer) {
        wrappedDataSource.attachAsFirstObserver(observer);
    }

    @Override
    public void detachObserver(IGraphObserver<V> observer) {
        wrappedDataSource.detachObserver(observer);
    }

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

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

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

    @Override
    public void edgeInserted(V source, V target) {
        incomingEdges.addPair(target, source);
    }

    @Override
    public void edgeDeleted(V source, V target) {
        incomingEdges.removePair(target, source);
    }

    @Override
    public void nodeInserted(V n) {

    }

    @Override
    public void nodeDeleted(V node) {

    }

    @Override
    public String toString() {
        return wrappedDataSource.toString();
    }
}