aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/boundary/ReteBoundary.java
blob: fe9c795e00a7d84c3e49bb14006ecdbc1f69897d (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*******************************************************************************
 * Copyright (c) 2004-2008 Gabor Bergmann 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.Map;
import java.util.Set;

import tools.refinery.viatra.runtime.matchers.ViatraQueryRuntimeException;
import tools.refinery.viatra.runtime.matchers.planning.SubPlan;
import tools.refinery.viatra.runtime.matchers.psystem.queries.PQuery;
import tools.refinery.viatra.runtime.matchers.tuple.Tuple;
import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory;
import tools.refinery.viatra.runtime.matchers.util.Direction;
import tools.refinery.viatra.runtime.rete.matcher.ReteEngine;
import tools.refinery.viatra.runtime.rete.network.Network;
import tools.refinery.viatra.runtime.rete.network.ProductionNode;
import tools.refinery.viatra.runtime.rete.network.Receiver;
import tools.refinery.viatra.runtime.rete.network.ReteContainer;
import tools.refinery.viatra.runtime.rete.network.Supplier;
import tools.refinery.viatra.runtime.rete.remote.Address;
import tools.refinery.viatra.runtime.rete.traceability.CompiledQuery;
import tools.refinery.viatra.runtime.rete.traceability.RecipeTraceInfo;

/**
 * Responsible for the storage, maintenance and communication of the nodes of the network that are accessible form the
 * outside for various reasons.
 *
 * @author Gabor Bergmann
 * 
 * <p> TODO: should eventually be merged into {@link InputConnector} and deleted
 *
 */
public class ReteBoundary /*implements IPatternMatcherRuntimeContextListener*/ {

    protected ReteEngine engine;
    protected Network network;
    protected ReteContainer headContainer;

    public ReteContainer getHeadContainer() {
        return headContainer;
    }

    protected final InputConnector inputConnector;


    protected Map<SubPlan, Address<? extends Supplier>> subplanToAddressMapping;


    /**
     * SubPlans of parent nodes that have the key node as their child. For RETE --> SubPlan traceability, mainly at production
     * nodes.
     */
    protected Map<Address<? extends Receiver>, Set<SubPlan>> parentPlansOfReceiver;

    /**
     * Prerequisite: engine has its network and framework fields initialized
     */
    public ReteBoundary(ReteEngine engine) {
        super();
        this.engine = engine;
        this.network = engine.getReteNet();
        this.headContainer = network.getHeadContainer();
        inputConnector = network.getInputConnector();

        this.parentPlansOfReceiver = CollectionsFactory.createMap();

        // productionsScoped = new HashMap<GTPattern, Map<Map<Integer,Scope>,Address<? extends Production>>>();
        subplanToAddressMapping = CollectionsFactory.createMap();

    }

    public Collection<? extends RecipeTraceInfo> getAllProductionNodes() {
        return engine.getCompiler().getCachedCompiledQueries().values();
    }

//    /**
//     * accesses the entity Root node under specified label; creates the node if it doesn't exist yet
//     */
//    public Address<? extends Tunnel> accessUnaryRoot(Object typeObject) {
//        Address<? extends Tunnel> tn;
//        tn = unaryRoots.get(typeObject);
//        if (tn == null) {
//            tn = headContainer.getProvisioner().newUniquenessEnforcerNode(1, typeObject);
//            unaryRoots.put(typeObject, tn);
//
//            new EntityFeeder(tn, context, network, this, typeObject).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);
//                }
//            }
//
//        }
//        return tn;
//    }
//
//    /**
//     * accesses the relation Root node under specified label; creates the node if it doesn't exist yet
//     */
//    public Address<? extends Tunnel> accessTernaryEdgeRoot(Object typeObject) {
//        Address<? extends Tunnel> tn;
//        tn = ternaryEdgeRoots.get(typeObject);
//        if (tn == null) {
//            tn = headContainer.getProvisioner().newUniquenessEnforcerNode(3, typeObject);
//            ternaryEdgeRoots.put(typeObject, tn);
//
//            new RelationFeeder(tn, context, network, this, typeObject).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);
//                }
//            }
//        }
//        return tn;
//    }
//
//    /**
//     * accesses the reference Root node under specified label; creates the node if it doesn't exist yet
//     */
//    public Address<? extends Tunnel> accessBinaryEdgeRoot(Object typeObject) {
//        Address<? extends Tunnel> tn;
//        tn = binaryEdgeRoots.get(typeObject);
//        if (tn == null) {
//            tn = headContainer.getProvisioner().newUniquenessEnforcerNode(2, typeObject);
//            binaryEdgeRoots.put(typeObject, tn);
//
//            new ReferenceFeeder(tn, context, network, this, typeObject).feed();
//
//            if (typeObject != null && generalizationQueryDirection == GeneralizationQueryDirection.BOTH) {
//                Collection<? extends Object> subTypes = context.enumerateDirectBinaryEdgeSubtypes(typeObject);
//
//                for (Object subType : subTypes) {
//                    Address<? extends Tunnel> subRoot = accessBinaryEdgeRoot(subType);
//                    network.connectRemoteNodes(subRoot, tn, true);
//                }
//            }
//        }
//        return tn;
//    }
//
//    /**
//     * accesses the special direct containment relation Root node; creates the node if it doesn't exist yet
//     */
//    public Address<? extends Tunnel> accessContainmentRoot() {
//        if (containmentRoot == null) {
//            // containment: relation quasi-type
//            containmentRoot = headContainer.getProvisioner().newUniquenessEnforcerNode(2, "$containment");
//
//            new ContainmentFeeder(containmentRoot, context, network, this).feed();
//        }
//        return containmentRoot;
//    }
//
//    /**
//     * accesses the special transitive containment relation Root node; creates the node if it doesn't exist yet
//     */
//    public Address<? extends Supplier> accessContainmentTransitiveRoot() {
//        if (containmentTransitiveRoot == null) {
//            // transitive containment: derived
//            Address<? extends Tunnel> containmentTransitiveRoot = headContainer.getProvisioner().newUniquenessEnforcerNode(
//                    2, "$containmentTransitive");
//            network.connectRemoteNodes(accessContainmentRoot(), containmentTransitiveRoot, true);
//
//            final int[] actLI = { 1 };
//            final int arcLIw = 2;
//            final int[] actRI = { 0 };
//            final int arcRIw = 2;
//            Address<? extends IterableIndexer> jPrimarySlot = headContainer.getProvisioner().accessProjectionIndexer(
//                    accessContainmentRoot(), new TupleMask(actLI, arcLIw));
//            Address<? extends IterableIndexer> jSecondarySlot = headContainer.getProvisioner().accessProjectionIndexer(
//                    containmentTransitiveRoot, new TupleMask(actRI, arcRIw));
//
//            final int[] actRIcomp = { 1 };
//            final int arcRIwcomp = 2;
//            TupleMask complementerMask = new TupleMask(actRIcomp, arcRIwcomp);
//
//            Address<? extends Supplier> andCT = headContainer.getProvisioner().accessJoinNode(jPrimarySlot, jSecondarySlot,
//                    complementerMask);
//
//            final int[] mask = { 0, 2 };
//            final int maskw = 3;
//            Address<? extends Supplier> tr = headContainer.getProvisioner().accessTrimmerNode(andCT, new TupleMask(mask, maskw));
//            network.connectRemoteNodes(tr, containmentTransitiveRoot, true);
//
//            this.containmentTransitiveRoot = containmentTransitiveRoot; // cast
//                                                                        // back
//                                                                        // to
//                                                                        // Supplier
//        }
//        return containmentTransitiveRoot;
//    }
//
//    /**
//     * accesses the special instantiation relation Root node; creates the node if it doesn't exist yet
//     */
//    public Address<? extends Tunnel> accessInstantiationRoot() {
//        if (instantiationRoot == null) {
//            // instantiation: relation quasi-type
//            instantiationRoot = headContainer.getProvisioner().newUniquenessEnforcerNode(2, "$instantiation");
//
//            new InstantiationFeeder(instantiationRoot, context, network, this).feed();
//        }
//        return instantiationRoot;
//    }
//
//    /**
//     * accesses the special transitive instantiation relation Root node; creates the node if it doesn't exist yet
//     * InstantiationTransitive = Instantiation o (Generalization)^*
//     */
//    public Address<? extends Supplier> accessInstantiationTransitiveRoot() {
//        if (instantiationTransitiveRoot == null) {
//            // transitive instantiation: derived
//            Address<? extends Tunnel> instantiationTransitiveRoot = headContainer.getProvisioner()
//                    .newUniquenessEnforcerNode(2, "$instantiationTransitive");
//            network.connectRemoteNodes(accessInstantiationRoot(), instantiationTransitiveRoot, true);
//
//            final int[] actLI = { 1 };
//            final int arcLIw = 2;
//            final int[] actRI = { 0 };
//            final int arcRIw = 2;
//            Address<? extends IterableIndexer> jPrimarySlot = headContainer.getProvisioner().accessProjectionIndexer(
//                    accessGeneralizationRoot(), new TupleMask(actLI, arcLIw));
//            Address<? extends Indexer> jSecondarySlot = headContainer.getProvisioner().accessProjectionIndexer(
//                    instantiationTransitiveRoot, new TupleMask(actRI, arcRIw));
//
//            final int[] actRIcomp = { 1 };
//            final int arcRIwcomp = 2;
//            TupleMask complementerMask = new TupleMask(actRIcomp, arcRIwcomp);
//
//            Address<? extends Supplier> andCT = headContainer.getProvisioner().accessJoinNode(jPrimarySlot, jSecondarySlot,
//                    complementerMask);
//
//            final int[] mask = { 0, 2 };
//            final int maskw = 3;
//            Address<? extends Supplier> tr = headContainer.getProvisioner().accessTrimmerNode(andCT,
//                    new TupleMask(mask, maskw));
//            network.connectRemoteNodes(tr, instantiationTransitiveRoot, true);
//
//            this.instantiationTransitiveRoot = instantiationTransitiveRoot; // cast
//                                                                            // back
//                                                                            // to
//                                                                            // Supplier
//        }
//        return instantiationTransitiveRoot;
//    }
//
//    /**
//     * accesses the special generalization relation Root node; creates the node if it doesn't exist yet
//     */
//    public Address<? extends Tunnel> accessGeneralizationRoot() {
//        if (generalizationRoot == null) {
//            // generalization: relation quasi-type
//            generalizationRoot = headContainer.getProvisioner().newUniquenessEnforcerNode(2, "$generalization");
//
//            new GeneralizationFeeder(generalizationRoot, context, network, this).feed();
//        }
//        return generalizationRoot;
//    }
//
//    /**
//     * accesses the special transitive containment relation Root node; creates the node if it doesn't exist yet
//     */
//    public Address<? extends Supplier> accessGeneralizationTransitiveRoot() {
//        if (generalizationTransitiveRoot == null) {
//            // transitive generalization: derived
//            Address<? extends Tunnel> generalizationTransitiveRoot = headContainer.getProvisioner()
//                    .newUniquenessEnforcerNode(2, "$generalizationTransitive");
//            network.connectRemoteNodes(accessGeneralizationRoot(), generalizationTransitiveRoot, true);
//
//            final int[] actLI = { 1 };
//            final int arcLIw = 2;
//            final int[] actRI = { 0 };
//            final int arcRIw = 2;
//            Address<? extends IterableIndexer> jPrimarySlot = headContainer.getProvisioner().accessProjectionIndexer(
//                    accessGeneralizationRoot(), new TupleMask(actLI, arcLIw));
//            Address<? extends Indexer> jSecondarySlot = headContainer.getProvisioner().accessProjectionIndexer(
//                    generalizationTransitiveRoot, new TupleMask(actRI, arcRIw));
//
//            final int[] actRIcomp = { 1 };
//            final int arcRIwcomp = 2;
//            TupleMask complementerMask = new TupleMask(actRIcomp, arcRIwcomp);
//
//            Address<? extends Supplier> andCT = headContainer.getProvisioner().accessJoinNode(jPrimarySlot, jSecondarySlot,
//                    complementerMask);
//
//            final int[] mask = { 0, 2 };
//            final int maskw = 3;
//            Address<? extends Supplier> tr = headContainer.getProvisioner().accessTrimmerNode(andCT, new TupleMask(mask, maskw));
//            network.connectRemoteNodes(tr, generalizationTransitiveRoot, true);
//
//            this.generalizationTransitiveRoot = generalizationTransitiveRoot; // cast
//                                                                              // back
//                                                                              // to
//                                                                              // Supplier
//        }
//        return generalizationTransitiveRoot;
//    }

    // /**
    // * Registers and publishes a supplier under specified label.
    // */
    // public void publishSupplier(Supplier s, Object label)
    // {
    // publishedSuppliers.put(label, s);
    // }
    //
    // /**
    // * fetches the production node under specified label;
    // * returns null if it doesn't exist yet
    // */
    // public Production getProductionNode(Object label)
    // {
    // return productions.get(label);
    // }
    //
    // /**
    // * fetches the published supplier under specified label;
    // * returns null if it doesn't exist yet
    // */
    // public Supplier getPublishedSupplier(Object label)
    // {
    // return publishedSuppliers.get(label);
    // }

    /**
     * accesses the production node for specified pattern; builds pattern matcher if it doesn't exist yet
     * @throws ViatraQueryRuntimeException
     */
    public synchronized RecipeTraceInfo accessProductionTrace(PQuery query) 
    {
        final CompiledQuery compiled = engine.getCompiler().getCompiledForm(query);
        return compiled;
//    	RecipeTraceInfo pn;
//        pn = queryPlans.get(query);
//        if (pn == null) {
//            pn = construct(query);
//            TODO handle recursion by reinterpret-RecipeTrace
//            queryPlans.put(query, pn);
//            if (pn == null) {
//                String[] args = { query.toString() };
//                throw new RetePatternBuildException("Unsuccessful planning of RETE construction recipe for query {1}",
//                        args, "Could not create RETE recipe plan.", query);
//            }
//        }
//        return pn;
    }
    /**
     * accesses the production node for specified pattern; builds pattern matcher if it doesn't exist yet
     * @throws ViatraQueryRuntimeException
     */
    public synchronized Address<? extends ProductionNode> accessProductionNode(PQuery query) {
        final RecipeTraceInfo productionTrace = accessProductionTrace(query);
        return (Address<? extends ProductionNode>) headContainer.getProvisioner().getOrCreateNodeByRecipe(productionTrace);
    }

//    /**
//     * creates the production node for the specified pattern Contract: only call from the builder (through Buildable)
//     * responsible for building this pattern
//     *
//     * @throws PatternMatcherCompileTimeException
//     *             if production node is already created
//     */
//    public synchronized Address<? extends Production> createProductionInternal(PQuery gtPattern)
//            throws QueryPlannerException {
//        if (queryPlans.containsKey(gtPattern)) {
//            String[] args = { gtPattern.toString() };
//            throw new RetePatternBuildException("Multiple creation attempts of production node for {1}", args,
//                    "Duplicate RETE production node.", gtPattern);
//        }
//
//        Map<String, Integer> posMapping = engine.getBuilder().getPosMapping(gtPattern);
//        Address<? extends Production> pn = headContainer.getProvisioner().newProductionNode(posMapping, gtPattern);
//        queryPlans.put(gtPattern, pn);
//        context.reportPatternDependency(gtPattern);
//
//        return pn;
//    }

    // /**
    // * accesses the production node for specified pattern and scope map; creates the node if
    // * it doesn't exist yet
    // */
    // public synchronized Address<? extends Production> accessProductionScoped(
    // GTPattern gtPattern, Map<Integer, Scope> additionalScopeMap) throws PatternMatcherCompileTimeException {
    // if (additionalScopeMap.isEmpty()) return accessProduction(gtPattern);
    //
    // Address<? extends Production> pn;
    //
    // Map<Map<Integer, Scope>, Address<? extends Production>> scopes = productionsScoped.get(gtPattern);
    // if (scopes == null) {
    // scopes = new HashMap<Map<Integer, Scope>, Address<? extends Production>>();
    // productionsScoped.put(gtPattern, scopes);
    // }
    //
    // pn = scopes.get(additionalScopeMap);
    // if (pn == null) {
    // Address<? extends Production> unscopedProduction = accessProduction(gtPattern);
    //
    // HashMap<Object, Integer> posMapping = headContainer.resolveLocal(unscopedProduction).getPosMapping();
    // pn = headContainer.getLibrary().newProductionNode(posMapping);
    // scopes.put(additionalScopeMap, pn);
    //
    // constructScoper(unscopedProduction, additionalScopeMap, pn);
    // }
    // return pn;
    // }

    // protected void constructScoper(
    // Address<? extends Production> unscopedProduction,
    // Map<Integer, Scope> additionalScopeMap,
    // Address<? extends Production> production)
    // throws PatternMatcherCompileTimeException {
    // engine.reteNet.waitForReteTermination();
    // engine.builder.constructScoper(unscopedProduction, additionalScopeMap, production);
    // }

    // /**
    // * Invalidates the subnet constructed for the recognition of a given
    // pattern.
    // * The pattern matcher will have to be rebuilt.
    // * @param gtPattern the pattern whose matcher subnet should be invalidated
    // */
    // public void invalidatePattern(GTPattern gtPattern) {
    // Production production = null;
    // try {
    // production = accessProduction(gtPattern);
    // } catch (PatternMatcherCompileTimeException e) {
    // // this should not occur here, since we already have a production node
    // e.printStackTrace();
    // }
    //
    // production.tearOff();
    // //production.setDirty(true);
    // }

    // updaters for change notification
    // if the corresponding rete input isn't created yet, call is ignored
    
    private static Direction direction(boolean isInsertion) {
        return isInsertion ? Direction.INSERT : Direction.DELETE;
    }
    
//    @Override
//	public void updateUnary(boolean isInsertion, Object entity, Object typeObject) {
//        Address<? extends Tunnel> root = inputConnector.getUnaryRoot(typeObject);
//        if (root != null) {
//            network.sendExternalUpdate(root, direction(isInsertion), new FlatTuple(inputConnector.wrapElement(entity)));
//            if (!engine.isParallelExecutionEnabled())
//                network.waitForReteTermination();
//        }
//        if (typeObject != null && generalizationQueryDirection == GeneralizationQueryDirection.SUPERTYPE_ONLY) {
//            for (Object superType : context.enumerateDirectUnarySupertypes(typeObject)) {
//                updateUnary(isInsertion, entity, superType);
//            }
//        }
//    }
//
//    @Override
//	public void updateTernaryEdge(boolean isInsertion, Object relation, Object from, Object to, Object typeObject) {
//        Address<? extends Tunnel> root = inputConnector.getTernaryEdgeRoot(typeObject);
//        if (root != null) {
//            network.sendExternalUpdate(root, direction(isInsertion), new FlatTuple(inputConnector.wrapElement(relation), inputConnector.wrapElement(from),
//                    inputConnector.wrapElement(to)));
//            if (!engine.isParallelExecutionEnabled())
//                network.waitForReteTermination();
//        }
//        if (typeObject != null && generalizationQueryDirection == GeneralizationQueryDirection.SUPERTYPE_ONLY) {
//            for (Object superType : context.enumerateDirectTernaryEdgeSupertypes(typeObject)) {
//                updateTernaryEdge(isInsertion, relation, from, to, superType);
//            }
//        }
//    }
//    @Override
//	public void updateBinaryEdge(boolean isInsertion, Object from, Object to, Object typeObject) {
//        Address<? extends Tunnel> root = inputConnector.getBinaryEdgeRoot(typeObject);
//        if (root != null) {
//            network.sendExternalUpdate(root, direction(isInsertion), new FlatTuple(inputConnector.wrapElement(from), inputConnector.wrapElement(to)));
//            if (!engine.isParallelExecutionEnabled())
//                network.waitForReteTermination();
//        }
//        if (typeObject != null && generalizationQueryDirection == GeneralizationQueryDirection.SUPERTYPE_ONLY) {
//            for (Object superType : context.enumerateDirectBinaryEdgeSupertypes(typeObject)) {
//                updateBinaryEdge(isInsertion, from, to, superType);
//            }
//        }
//    }
//
//    @Override
//	public void updateContainment(boolean isInsertion, Object container, Object element) {
//        final Address<? extends Tunnel> containmentRoot = inputConnector.getContainmentRoot();
//		if (containmentRoot != null) {
//            network.sendExternalUpdate(containmentRoot, direction(isInsertion), new FlatTuple(inputConnector.wrapElement(container),
//                    inputConnector.wrapElement(element)));
//            if (!engine.isParallelExecutionEnabled())
//                network.waitForReteTermination();
//        }
//    }
//
//    @Override
//	public void updateInstantiation(boolean isInsertion, Object parent, Object child) {
//        final Address<? extends Tunnel> instantiationRoot = inputConnector.getInstantiationRoot();
//       if (instantiationRoot != null) {
//            network.sendExternalUpdate(instantiationRoot, direction(isInsertion), new FlatTuple(inputConnector.wrapElement(parent),
//                    inputConnector.wrapElement(child)));
//            if (!engine.isParallelExecutionEnabled())
//                network.waitForReteTermination();
//        }
//    }
//
//    @Override
//	public void updateGeneralization(boolean isInsertion, Object parent, Object child) {
//       final Address<? extends Tunnel> generalizationRoot = inputConnector.getGeneralizationRoot();
//       if (generalizationRoot != null) {
//            network.sendExternalUpdate(generalizationRoot, direction(isInsertion), new FlatTuple(inputConnector.wrapElement(parent),
//                    inputConnector.wrapElement(child)));
//            if (!engine.isParallelExecutionEnabled())
//                network.waitForReteTermination();
//        }
//    }

    // no wrapping needed!
    public void notifyEvaluator(Address<? extends Receiver> receiver, Tuple tuple) {
        network.sendExternalUpdate(receiver, Direction.INSERT, tuple);
        if (!engine.isParallelExecutionEnabled())
            network.waitForReteTermination();
    }

    public void mapPlanToAddress(SubPlan plan, Address<? extends Supplier> handle) {
        subplanToAddressMapping.put(plan, handle);
    }

    public Address<? extends Supplier> getAddress(SubPlan plan) {
        return subplanToAddressMapping.get(plan);
    }
}