diff options
Diffstat (limited to 'subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/scopes/TabularRuntimeContext.java')
-rw-r--r-- | subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/scopes/TabularRuntimeContext.java | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/scopes/TabularRuntimeContext.java b/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/scopes/TabularRuntimeContext.java new file mode 100644 index 00000000..e99e24d3 --- /dev/null +++ b/subprojects/viatra-runtime-matchers/src/main/java/tools/refinery/viatra/runtime/matchers/scopes/TabularRuntimeContext.java | |||
@@ -0,0 +1,119 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2018, Gabor Bergmann, IncQuery Labs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | |||
10 | package tools.refinery.viatra.runtime.matchers.scopes; | ||
11 | |||
12 | import java.util.Map; | ||
13 | import java.util.Optional; | ||
14 | |||
15 | import tools.refinery.viatra.runtime.matchers.context.AbstractQueryRuntimeContext; | ||
16 | import tools.refinery.viatra.runtime.matchers.context.IInputKey; | ||
17 | import tools.refinery.viatra.runtime.matchers.context.IQueryRuntimeContextListener; | ||
18 | import tools.refinery.viatra.runtime.matchers.scopes.tables.IIndexTable; | ||
19 | import tools.refinery.viatra.runtime.matchers.scopes.tables.ITableContext; | ||
20 | import tools.refinery.viatra.runtime.matchers.tuple.ITuple; | ||
21 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
22 | import tools.refinery.viatra.runtime.matchers.tuple.TupleMask; | ||
23 | import tools.refinery.viatra.runtime.matchers.util.Accuracy; | ||
24 | import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory; | ||
25 | |||
26 | /** | ||
27 | * An abstract runtime context that serves enumerable input key instances from tables. | ||
28 | * | ||
29 | * <p> | ||
30 | * Usage: first, instantiate {@link IIndexTable} tables with this as the 'tableContext' argument. Call | ||
31 | * {@link #registerIndexTable(IIndexTable)} to register them; this may happen either during a coalesced indexing, or on | ||
32 | * external initiation. Afterwards, they will be visible to the query backends. | ||
33 | * <p> | ||
34 | * <strong>EXPERIMENTAL</strong>. This class or interface has been added as | ||
35 | * part of a work in progress. There is no guarantee that this API will | ||
36 | * work or that it will remain the same. | ||
37 | * | ||
38 | * @author Gabor Bergmann | ||
39 | * @since 2.0 | ||
40 | */ | ||
41 | public abstract class TabularRuntimeContext extends AbstractQueryRuntimeContext implements ITableContext { | ||
42 | |||
43 | private Map<IInputKey, IIndexTable> instanceTables = CollectionsFactory.createMap(); | ||
44 | |||
45 | public void registerIndexTable(IIndexTable table) { | ||
46 | IInputKey inputKey = table.getInputKey(); | ||
47 | instanceTables.put(inputKey, table); | ||
48 | } | ||
49 | |||
50 | /** | ||
51 | * @return null if the table is not registered | ||
52 | */ | ||
53 | public IIndexTable peekIndexTable(IInputKey key) { | ||
54 | return instanceTables.get(key); | ||
55 | } | ||
56 | |||
57 | /** | ||
58 | * If the table is not registered, {@link #handleUnregisteredTableRequest(IInputKey)} is invoked; it may handle it | ||
59 | * by raising an error or e.g. on-demand index construction | ||
60 | */ | ||
61 | public IIndexTable getIndexTable(IInputKey key) { | ||
62 | IIndexTable table = instanceTables.get(key); | ||
63 | if (table != null) | ||
64 | return table; | ||
65 | else | ||
66 | return handleUnregisteredTableRequest(key); | ||
67 | } | ||
68 | |||
69 | /** | ||
70 | * Override this to provide on-demand table registration | ||
71 | */ | ||
72 | protected IIndexTable handleUnregisteredTableRequest(IInputKey key) { | ||
73 | throw new IllegalArgumentException(key.getPrettyPrintableName()); | ||
74 | } | ||
75 | |||
76 | @Override | ||
77 | public int countTuples(IInputKey key, TupleMask seedMask, ITuple seed) { | ||
78 | return getIndexTable(key).countTuples(seedMask, seed); | ||
79 | } | ||
80 | |||
81 | @Override | ||
82 | public Optional<Long> estimateCardinality(IInputKey key, TupleMask groupMask, Accuracy requiredAccuracy) { | ||
83 | return getIndexTable(key).estimateProjectionSize(groupMask, requiredAccuracy); | ||
84 | } | ||
85 | |||
86 | @Override | ||
87 | public Iterable<Tuple> enumerateTuples(IInputKey key, TupleMask seedMask, ITuple seed) { | ||
88 | return getIndexTable(key).enumerateTuples(seedMask, seed); | ||
89 | } | ||
90 | |||
91 | @Override | ||
92 | public Iterable<? extends Object> enumerateValues(IInputKey key, TupleMask seedMask, ITuple seed) { | ||
93 | return getIndexTable(key).enumerateValues(seedMask, seed); | ||
94 | } | ||
95 | |||
96 | @Override | ||
97 | public boolean containsTuple(IInputKey key, ITuple seed) { | ||
98 | if (key.isEnumerable()) { | ||
99 | return getIndexTable(key).containsTuple(seed); | ||
100 | } else { | ||
101 | return isContainedInStatelessKey(key, seed); | ||
102 | } | ||
103 | } | ||
104 | |||
105 | @Override | ||
106 | public void addUpdateListener(IInputKey key, Tuple seed, IQueryRuntimeContextListener listener) { | ||
107 | getIndexTable(key).addUpdateListener(seed, listener); | ||
108 | } | ||
109 | @Override | ||
110 | public void removeUpdateListener(IInputKey key, Tuple seed, IQueryRuntimeContextListener listener) { | ||
111 | getIndexTable(key).removeUpdateListener(seed, listener); | ||
112 | } | ||
113 | |||
114 | /** | ||
115 | * Handles non-enumerable input keys that are not backed by a table | ||
116 | */ | ||
117 | protected abstract boolean isContainedInStatelessKey(IInputKey key, ITuple seed); | ||
118 | |||
119 | } | ||