aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/interpreter-rete/src/main/java/tools/refinery/interpreter/rete/network/ConnectionFactory.java
blob: c69757b6071e533b0aeabdf879df787dd0c10bfa (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
/*******************************************************************************
 * Copyright (c) 2010-2014, Bergmann Gabor, Istvan Rath and Daniel Varro
 * Copyright (c) 2023 The Refinery Authors <https://refinery.tools>
 * 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.interpreter.rete.network;

import tools.refinery.interpreter.matchers.tuple.Tuple;
import tools.refinery.interpreter.rete.aggregation.IndexerBasedAggregatorNode;
import tools.refinery.interpreter.rete.boundary.InputConnector;
import tools.refinery.interpreter.rete.eval.RelationEvaluatorNode;
import tools.refinery.interpreter.rete.index.DualInputNode;
import tools.refinery.interpreter.rete.index.Indexer;
import tools.refinery.interpreter.rete.index.IterableIndexer;
import tools.refinery.interpreter.rete.index.ProjectionIndexer;
import tools.refinery.interpreter.rete.recipes.*;
import tools.refinery.interpreter.rete.remote.Address;
import tools.refinery.interpreter.rete.traceability.RecipeTraceInfo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Class responsible for connecting freshly instantiating Rete nodes to their parents.
 *
 * @author Bergmann Gabor
 *
 */
class ConnectionFactory {
    ReteContainer reteContainer;

    public ConnectionFactory(ReteContainer reteContainer) {
        super();
        this.reteContainer = reteContainer;
    }

    // TODO move to node implementation instead?
    private boolean isStateful(ReteNodeRecipe recipe) {
        return recipe instanceof ProjectionIndexerRecipe || recipe instanceof IndexerBasedAggregatorRecipe
                || recipe instanceof SingleColumnAggregatorRecipe || recipe instanceof ExpressionEnforcerRecipe
                || recipe instanceof TransitiveClosureRecipe || recipe instanceof ProductionRecipe
                || recipe instanceof UniquenessEnforcerRecipe || recipe instanceof RelationEvaluationRecipe;

    }

    /**
     * PRE: nodes for parent recipes must already be created and registered
     * <p>
     * PRE: must not be an input node (for which {@link InputConnector} is responsible)
     */
    public void connectToParents(RecipeTraceInfo recipeTrace, Node freshNode) {
        final ReteNodeRecipe recipe = recipeTrace.getRecipe();
        if (recipe instanceof ConstantRecipe) {
            // NO-OP
        } else if (recipe instanceof InputRecipe) {
            throw new IllegalArgumentException(
                    ConnectionFactory.class.getSimpleName() + " not intended for input connection: " + recipe);
        } else if (recipe instanceof SingleParentNodeRecipe) {
            final Receiver receiver = (Receiver) freshNode;
            ReteNodeRecipe parentRecipe = ((SingleParentNodeRecipe) recipe).getParent();
            connectToParent(recipe, receiver, parentRecipe);
        } else if (recipe instanceof RelationEvaluationRecipe) {
            List<ReteNodeRecipe> parentRecipes = ((MultiParentNodeRecipe) recipe).getParents();
            List<Supplier> parentSuppliers = new ArrayList<Supplier>();
            for (final ReteNodeRecipe parentRecipe : parentRecipes) {
                parentSuppliers.add(getSupplierForRecipe(parentRecipe));
            }
            ((RelationEvaluatorNode) freshNode).connectToParents(parentSuppliers);
        } else if (recipe instanceof BetaRecipe) {
            final DualInputNode beta = (DualInputNode) freshNode;
            final ArrayList<RecipeTraceInfo> parentTraces = new ArrayList<RecipeTraceInfo>(
                    recipeTrace.getParentRecipeTraces());
            Slots slots = avoidActiveNodeConflict(parentTraces.get(0), parentTraces.get(1));
            beta.connectToIndexers(slots.primary, slots.secondary);
        } else if (recipe instanceof IndexerBasedAggregatorRecipe) {
            final IndexerBasedAggregatorNode aggregator = (IndexerBasedAggregatorNode) freshNode;
            final IndexerBasedAggregatorRecipe aggregatorRecipe = (IndexerBasedAggregatorRecipe) recipe;
            aggregator.initializeWith((ProjectionIndexer) resolveIndexer(aggregatorRecipe.getParent()));
        } else if (recipe instanceof MultiParentNodeRecipe) {
            final Receiver receiver = (Receiver) freshNode;
            List<ReteNodeRecipe> parentRecipes = ((MultiParentNodeRecipe) recipe).getParents();
            for (ReteNodeRecipe parentRecipe : parentRecipes) {
                connectToParent(recipe, receiver, parentRecipe);
            }
        }
    }

    private Indexer resolveIndexer(final IndexerRecipe indexerRecipe) {
        final Address<? extends Node> address = reteContainer.getNetwork().getExistingNodeByRecipe(indexerRecipe);
        return (Indexer) reteContainer.resolveLocal(address);
    }

    private void connectToParent(ReteNodeRecipe recipe, Receiver freshNode, ReteNodeRecipe parentRecipe) {
        final Supplier parentSupplier = getSupplierForRecipe(parentRecipe);

        // special synch
        if (freshNode instanceof ReinitializedNode) {
            Collection<Tuple> tuples = new ArrayList<Tuple>();
            parentSupplier.pullInto(tuples, true);
            ((ReinitializedNode) freshNode).reinitializeWith(tuples);
            reteContainer.connect(parentSupplier, freshNode);
        } else { // default case
            // stateless nodes do not have to be synced with contents UNLESS they already have children (recursive
            // corner case)
            if (isStateful(recipe)
                    || ((freshNode instanceof Supplier) && !((Supplier) freshNode).getReceivers().isEmpty())) {
                reteContainer.connectAndSynchronize(parentSupplier, freshNode);
            } else {
                // stateless node, no synch
                reteContainer.connect(parentSupplier, freshNode);
            }
        }
    }

    private Supplier getSupplierForRecipe(ReteNodeRecipe recipe) {
        @SuppressWarnings("unchecked")
        final Address<? extends Supplier> parentAddress = (Address<? extends Supplier>) reteContainer.getNetwork()
                .getExistingNodeByRecipe(recipe);
        final Supplier supplier = reteContainer.getProvisioner().asSupplier(parentAddress);
        return supplier;
    }

    /**
     * If two indexers share their active node, joining them via DualInputNode is error-prone. Exception: coincidence of
     * the two indexers is supported.
     *
     * @return a replacement for the secondary Indexers, if needed
     */
    private Slots avoidActiveNodeConflict(final RecipeTraceInfo primarySlot, final RecipeTraceInfo secondarySlot) {
        Slots result = new Slots();
        result.primary = (IterableIndexer) resolveIndexer((ProjectionIndexerRecipe) primarySlot.getRecipe());
        result.secondary = resolveIndexer((IndexerRecipe) secondarySlot.getRecipe());
        if (activeNodeConflict(result.primary, result.secondary)) {
			if (result.secondary instanceof IterableIndexer) {
				result.secondary = resolveActiveIndexer(secondarySlot);
			} else {
				result.primary = (IterableIndexer) resolveActiveIndexer(primarySlot);
			}
		}
        return result;
    }

    private Indexer resolveActiveIndexer(final RecipeTraceInfo inactiveIndexerTrace) {
        final RecipeTraceInfo activeIndexerTrace = reteContainer.getProvisioner()
                .accessActiveIndexer(inactiveIndexerTrace);
        reteContainer.getProvisioner().getOrCreateNodeByRecipe(activeIndexerTrace);
        return resolveIndexer((ProjectionIndexerRecipe) activeIndexerTrace.getRecipe());
    }

    private static class Slots {
        IterableIndexer primary;
        Indexer secondary;
    }

    /**
     * If two indexers share their active node, joining them via DualInputNode is error-prone. Exception: coincidence of
     * the two indexers is supported.
     *
     * @return true if there is a conflict of active nodes.
     */
    private boolean activeNodeConflict(Indexer primarySlot, Indexer secondarySlot) {
        return !primarySlot.equals(secondarySlot) && primarySlot.getActiveNode().equals(secondarySlot.getActiveNode());
    }

}