aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/boundary/InputConnector.java
blob: c044850f5e9b893d575682f13b4722d501aa9720 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*******************************************************************************
 * Copyright (c) 2010-2014, Bergmann Gabor, 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.boundary;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Stream;

import tools.refinery.viatra.runtime.matchers.context.IInputKey;
import tools.refinery.viatra.runtime.matchers.tuple.Tuple;
import tools.refinery.viatra.runtime.matchers.tuple.Tuples;
import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory;
import tools.refinery.viatra.runtime.rete.network.Network;
import tools.refinery.viatra.runtime.rete.network.Node;
import tools.refinery.viatra.runtime.rete.recipes.InputFilterRecipe;
import tools.refinery.viatra.runtime.rete.recipes.InputRecipe;
import tools.refinery.viatra.runtime.rete.remote.Address;

/**
 * A class responsible for connecting input nodes to the runtime context.
 * 
 * @author Bergmann Gabor
 *
 */
public final class InputConnector {
    Network network;
    
    private Map<IInputKey, Map<Tuple, Address<ExternalInputEnumeratorNode>>> externalInputRoots = CollectionsFactory.createMap();
    
//    /*
//     * arity:1 used as simple entity constraints label is the object representing the type null label means all entities
//     * regardless of type (global supertype), if allowed
//     */
//    protected Map<Object, Address<? extends Tunnel>> unaryRoots = CollectionsFactory.getMap();
//    /*
//     * arity:3 (rel, from, to) used as VPM relation constraints null label means all relations regardless of type
//     * (global supertype)
//     */
//    protected Map<Object, Address<? extends Tunnel>> ternaryEdgeRoots = CollectionsFactory.getMap();
//    /*
//     * arity:2 (from, to) not used over VPM; can be used as EMF references for instance label is the object representing
//     * the type null label means all entities regardless of type if allowed (global supertype), if allowed
//     */
//    protected Map<Object, Address<? extends Tunnel>> binaryEdgeRoots = CollectionsFactory.getMap();
//	
//    protected Address<? extends Tunnel> containmentRoot = null;
//    protected Address<? extends Supplier> containmentTransitiveRoot = null;
//    protected Address<? extends Tunnel> instantiationRoot = null;
//    protected Address<? extends Supplier> instantiationTransitiveRoot = null;
//    protected Address<? extends Tunnel> generalizationRoot = null;
//    protected Address<? extends Supplier> generalizationTransitiveRoot = null;
    

    public InputConnector(Network network) {
        super();
        this.network = network;
    }
    

    public Network getNetwork() {
        return network;
    }


    /**
     * Connects a given input filter node to the external input source.
     */
    public void connectInputFilter(InputFilterRecipe recipe, Node freshNode) {
        final ExternalInputStatelessFilterNode inputNode = (ExternalInputStatelessFilterNode)freshNode;
        
        IInputKey inputKey = (IInputKey) recipe.getInputKey();
        inputNode.connectThroughContext(network.getEngine(), inputKey);
    }


    /**
     * Connects a given input enumerator node to the external input source.
     */
    public void connectInput(InputRecipe recipe, Node freshNode) {
        final ExternalInputEnumeratorNode inputNode = (ExternalInputEnumeratorNode)freshNode;
        
        IInputKey inputKey = (IInputKey) recipe.getInputKey();
        Tuple seed = nopSeed(inputKey); // no preseeding as of now
        final Address<ExternalInputEnumeratorNode> freshAddress = Address.of(inputNode);
        externalInputRoots.computeIfAbsent(inputKey, k -> CollectionsFactory.createMap()).put(seed, freshAddress);
        inputNode.connectThroughContext(network.getEngine(), inputKey, seed);
        
//		final Address<Tunnel> freshAddress = Address.of((Tunnel)freshNode);
//		if (recipe instanceof TypeInputRecipe) {
//			final Object typeKey = ((TypeInputRecipe) recipe).getTypeKey();
//			
//			if (recipe instanceof UnaryInputRecipe) {
//				unaryRoots.put(typeKey, freshAddress);
//				new EntityFeeder(freshAddress, this, typeKey).feed();
////		        if (typeObject != null && generalizationQueryDirection == GeneralizationQueryDirection.BOTH) {
////		            Collection<? extends Object> subTypes = context.enumerateDirectUnarySubtypes(typeObject);
////		
////		            for (Object subType : subTypes) {
////		                Address<? extends Tunnel> subRoot = accessUnaryRoot(subType);
////		                network.connectRemoteNodes(subRoot, tn, true);
////		            }
////		        }
//			} else if (recipe instanceof BinaryInputRecipe) {
//				binaryEdgeRoots.put(typeKey, freshAddress);
//				externalInputRoots.put(rowKey, columnKey, freshAddress);
//				new ReferenceFeeder(freshAddress, this, typeKey).feed();
//				//        if (typeObject != null && generalizationQueryDirection == GeneralizationQueryDirection.BOTH) {
//				//            Collection<? extends Object> subTypes = context.enumerateDirectTernaryEdgeSubtypes(typeObject);
//				//
//				//            for (Object subType : subTypes) {
//				//                Address<? extends Tunnel> subRoot = accessTernaryEdgeRoot(subType);
//				//                network.connectRemoteNodes(subRoot, tn, true);
//				//            }
//				//        }
//			}
//			
//			
//		}
        
    }
        
//    /**
//     * fetches the entity Root node under specified label; returns null if it doesn't exist yet
//     */
//    public Address<? extends Tunnel> getUnaryRoot(Object label) {
//        return unaryRoots.get(label);
//    }
//
//    public Collection<Address<? extends Tunnel>> getAllUnaryRoots() {
//        return unaryRoots.values();
//    }
//
//    /**
//     * fetches the relation Root node under specified label; returns null if it doesn't exist yet
//     */
//    public Address<? extends Tunnel> getTernaryEdgeRoot(Object label) {
//        return ternaryEdgeRoots.get(label);
//    }
//
//    public Collection<Address<? extends Tunnel>> getAllTernaryEdgeRoots() {
//        return ternaryEdgeRoots.values();
//    }
//    
//    /**
//     * fetches the reference Root node under specified label; returns null if it doesn't exist yet
//     */
//    public Address<? extends Tunnel> getBinaryEdgeRoot(Object label) {
//        return binaryEdgeRoots.get(label);
//    }
//
//    public Collection<Address<? extends Tunnel>> getAllBinaryEdgeRoots() {
//        return binaryEdgeRoots.values();
//    }
//
//
//	public Address<? extends Tunnel> getContainmentRoot() {
//		return containmentRoot;
//	}
//
//
//	public Address<? extends Supplier> getContainmentTransitiveRoot() {
//		return containmentTransitiveRoot;
//	}
//
//
//	public Address<? extends Tunnel> getInstantiationRoot() {
//		return instantiationRoot;
//	}
//
//
//	public Address<? extends Supplier> getInstantiationTransitiveRoot() {
//		return instantiationTransitiveRoot;
//	}
//
//
//	public Address<? extends Tunnel> getGeneralizationRoot() {
//		return generalizationRoot;
//	}

    
    public Stream<Address<ExternalInputEnumeratorNode>> getAllExternalInputNodes() {
        return externalInputRoots.values().stream().flatMap(map -> map.values().stream());
    }
    public Collection<Address<ExternalInputEnumeratorNode>> getAllExternalInputNodesForKey(IInputKey inputKey) {
        return externalInputRoots.getOrDefault(inputKey, Collections.emptyMap()).values();
    }
    public Address<ExternalInputEnumeratorNode> getExternalInputNodeForKeyUnseeded(IInputKey inputKey) {
        return externalInputRoots.getOrDefault(inputKey, Collections.emptyMap()).get(nopSeed(inputKey));
    }
    public Address<ExternalInputEnumeratorNode> getExternalInputNode(IInputKey inputKey, Tuple seed) {
        if (seed == null) seed = nopSeed(inputKey);
        return externalInputRoots.getOrDefault(inputKey, Collections.emptyMap()).get(seed);
    }


    Tuple nopSeed(IInputKey inputKey) {
        return Tuples.flatTupleOf(new Object[inputKey.getArity()]);
    }
    
    
}