aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/scopes/TabularRuntimeContext.java
blob: e99e24d352b0c89a4e39e0d417c8962c489ee2d7 (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
/*******************************************************************************
 * Copyright (c) 2010-2018, Gabor Bergmann, IncQuery Labs Ltd.
 * 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.matchers.scopes;

import java.util.Map;
import java.util.Optional;

import tools.refinery.viatra.runtime.matchers.context.AbstractQueryRuntimeContext;
import tools.refinery.viatra.runtime.matchers.context.IInputKey;
import tools.refinery.viatra.runtime.matchers.context.IQueryRuntimeContextListener;
import tools.refinery.viatra.runtime.matchers.scopes.tables.IIndexTable;
import tools.refinery.viatra.runtime.matchers.scopes.tables.ITableContext;
import tools.refinery.viatra.runtime.matchers.tuple.ITuple;
import tools.refinery.viatra.runtime.matchers.tuple.Tuple;
import tools.refinery.viatra.runtime.matchers.tuple.TupleMask;
import tools.refinery.viatra.runtime.matchers.util.Accuracy;
import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory;

/**
 * An abstract runtime context that serves enumerable input key instances from tables.
 * 
 * <p>
 * Usage: first, instantiate {@link IIndexTable} tables with this as the 'tableContext' argument. Call
 * {@link #registerIndexTable(IIndexTable)} to register them; this may happen either during a coalesced indexing, or on
 * external initiation. Afterwards, they will be visible to the query backends.
 * <p>
 * <strong>EXPERIMENTAL</strong>. This class or interface has been added as
 * part of a work in progress. There is no guarantee that this API will
 * work or that it will remain the same.
 * 
 * @author Gabor Bergmann
 * @since 2.0
 */
public abstract class TabularRuntimeContext extends AbstractQueryRuntimeContext implements ITableContext {

    private Map<IInputKey, IIndexTable> instanceTables = CollectionsFactory.createMap();

    public void registerIndexTable(IIndexTable table) {
        IInputKey inputKey = table.getInputKey();
        instanceTables.put(inputKey, table);
    }

    /**
     * @return null if the table is not registered
     */
    public IIndexTable peekIndexTable(IInputKey key) {
        return instanceTables.get(key);
    }

    /**
     * If the table is not registered, {@link #handleUnregisteredTableRequest(IInputKey)} is invoked; it may handle it
     * by raising an error or e.g. on-demand index construction
     */
    public IIndexTable getIndexTable(IInputKey key) {
        IIndexTable table = instanceTables.get(key);
        if (table != null)
            return table;
        else
            return handleUnregisteredTableRequest(key);
    }

    /**
     * Override this to provide on-demand table registration
     */
    protected IIndexTable handleUnregisteredTableRequest(IInputKey key) {
        throw new IllegalArgumentException(key.getPrettyPrintableName());
    }

    @Override
    public int countTuples(IInputKey key, TupleMask seedMask, ITuple seed) {
        return getIndexTable(key).countTuples(seedMask, seed);
    }
    
    @Override
    public Optional<Long> estimateCardinality(IInputKey key, TupleMask groupMask, Accuracy requiredAccuracy) {
        return getIndexTable(key).estimateProjectionSize(groupMask, requiredAccuracy);
    }

    @Override
    public Iterable<Tuple> enumerateTuples(IInputKey key, TupleMask seedMask, ITuple seed) {
        return getIndexTable(key).enumerateTuples(seedMask, seed);
    }

    @Override
    public Iterable<? extends Object> enumerateValues(IInputKey key, TupleMask seedMask, ITuple seed) {
        return getIndexTable(key).enumerateValues(seedMask, seed);
    }

    @Override
    public boolean containsTuple(IInputKey key, ITuple seed) {
        if (key.isEnumerable()) {
            return getIndexTable(key).containsTuple(seed);
        } else {
            return isContainedInStatelessKey(key, seed);
        }
    }
    
    @Override
    public void addUpdateListener(IInputKey key, Tuple seed, IQueryRuntimeContextListener listener) {
        getIndexTable(key).addUpdateListener(seed, listener);
    }
    @Override
    public void removeUpdateListener(IInputKey key, Tuple seed, IQueryRuntimeContextListener listener) {
        getIndexTable(key).removeUpdateListener(seed, listener);
    }
    
    /**
     * Handles non-enumerable input keys that are not backed by a table
     */
    protected abstract boolean isContainedInStatelessKey(IInputKey key, ITuple seed);

}