aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/tabular/EcoreIndexHost.java
blob: e87a726ab1ff194e4343ee60feb4a9774617c870 (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
/*******************************************************************************
 * 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.tabular;

import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import tools.refinery.viatra.runtime.api.scope.QueryScope;
import tools.refinery.viatra.runtime.emf.EMFQueryMetaContext;
import tools.refinery.viatra.runtime.emf.EMFScope;
import tools.refinery.viatra.runtime.emf.types.EClassExactInstancesKey;
import tools.refinery.viatra.runtime.emf.types.EClassTransitiveInstancesKey;
import tools.refinery.viatra.runtime.emf.types.EDataTypeInSlotsKey;
import tools.refinery.viatra.runtime.emf.types.EStructuralFeatureInstancesKey;
import tools.refinery.viatra.runtime.matchers.context.IInputKey;
import tools.refinery.viatra.runtime.matchers.scopes.IStorageBackend;
import tools.refinery.viatra.runtime.matchers.scopes.SimpleRuntimeContext;
import tools.refinery.viatra.runtime.matchers.scopes.tables.DisjointUnionTable;
import tools.refinery.viatra.runtime.matchers.scopes.tables.ITableWriterBinary;
import tools.refinery.viatra.runtime.matchers.scopes.tables.ITableWriterUnary;
import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory;

/**
 * Simple EMF-specific demo tabular index host. 
 * 
 * <p> Usage: <ul>
 * <li> First, instantiate index host with given Ecore metamodel packages
 * <li> To emulate an EMF instance model, write arbitrary content into the tables using {@link #getTableDirectInstances(EClassifier)} and {@link #getTableFeatureSlots(EStructuralFeature)}.
 * <li> Initialize and evaluate regular EMF-based Viatra queries on the scope provided by {@link #getScope()}, as you would on an {@link EMFScope}. 
 * <ul> 
 * 
 * <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.1
 */
public class EcoreIndexHost extends TabularIndexHost {
	
	public EcoreIndexHost(IStorageBackend storage, EPackage... packages) {
		super(storage, new SimpleRuntimeContext(EMFQueryMetaContext.DEFAULT_SURROGATE));
		
		initTables(packages);
	}
	
	@Override
    protected boolean isQueryScopeEmulated(Class<? extends QueryScope> queryScopeClass) {
        return EMFScope.class.equals(queryScopeClass);
    }

	
	private Map<EClassifier, ITableWriterUnary.Table<Object>> tableDirectInstances = CollectionsFactory.createMap();
	private Map<EClass, DisjointUnionTable> tableTransitiveInstances = CollectionsFactory.createMap();
	private Map<EStructuralFeature, ITableWriterBinary.Table<Object, Object>> tableFeatures = CollectionsFactory.createMap();
	
	private void initTables(EPackage[] packages) {
		
		// create instance tables first
		for (EPackage ePackage : packages) {
			for (EClassifier eClassifier : ePackage.getEClassifiers()) {
				boolean unique;
				IInputKey classifierKey;
				if (eClassifier instanceof EClass) {
					EClass eClass = (EClass) eClassifier;

					// create transitive instances table
					IInputKey transitiveKey = new EClassTransitiveInstancesKey(eClass);
					DisjointUnionTable transitiveTable = registerNewTable(new DisjointUnionTable(transitiveKey, runtimeContext));
					tableTransitiveInstances.put(eClass, transitiveTable);
					
					// process feature tables 
					for (EStructuralFeature feature : eClass.getEStructuralFeatures()) {
						IInputKey featureKey = new EStructuralFeatureInstancesKey(feature);
						ITableWriterBinary.Table<Object, Object> featureTable = newBinaryInputTable(featureKey, feature.isUnique());
						tableFeatures.put(feature, featureTable);
					}
					
					// direct instance table
					unique = true;
					classifierKey = new EClassExactInstancesKey(eClass);
				} else { // datatype
					unique = false;
					classifierKey = new EDataTypeInSlotsKey((EDataType) eClassifier);
				}
				ITableWriterUnary.Table<Object> directTable = newUnaryInputTable(classifierKey, unique);
				tableDirectInstances.put(eClassifier, directTable);
			}
		}
		
		// global implicit supertype EObject is always available as a transitive table
		EClass eObjectClass = EcorePackage.eINSTANCE.getEObject();
		DisjointUnionTable eObjectAllInstancesTable = tableTransitiveInstances.get(eObjectClass);
		if (eObjectAllInstancesTable == null) { // is it already added?
			IInputKey transitiveKey = new EClassTransitiveInstancesKey(eObjectClass);
			eObjectAllInstancesTable = registerNewTable(new DisjointUnionTable(transitiveKey, runtimeContext));
			tableTransitiveInstances.put(eObjectClass, eObjectAllInstancesTable);
			
			boolean unique = true;
			IInputKey classifierKey = new EClassExactInstancesKey(eObjectClass);
			ITableWriterUnary.Table<Object> directTable = newUnaryInputTable(classifierKey, unique);
			tableDirectInstances.put(eObjectClass, directTable);
		}
		
		// set up disjoint unoin tables
		for (Entry<EClass, DisjointUnionTable> entry : tableTransitiveInstances.entrySet()) {
			EClass eClass = entry.getKey();
			ITableWriterUnary.Table<Object> directTable = tableDirectInstances.get(eClass);
			
			// the direct type itself is a child
			entry.getValue().addChildTable(directTable);
			
			// connect supertypes
			for (EClass superClass : eClass.getEAllSuperTypes()) {
				DisjointUnionTable transitiveTable = tableTransitiveInstances.get(superClass);
				if (transitiveTable == null) {
				    throw new IllegalStateException(
				            String.format("No index table found for EClass %s, supertype of %s",
                                    superClass.getName(), eClass.getName())
                    );
                }
				transitiveTable.addChildTable(directTable);
			}
			// global implicit supertype
			if (!eClass.equals(eObjectClass)) {
				eObjectAllInstancesTable.addChildTable(directTable);
			}
				
		}
		
	}
	
	public ITableWriterUnary.Table<Object> getTableDirectInstances(EClassifier classifier) {
		return tableDirectInstances.get(classifier);
	}
	public ITableWriterBinary.Table<Object, Object> getTableFeatureSlots(EStructuralFeature feature) {
		return tableFeatures.get(feature);
	}


	
    public Set<Entry<EClassifier, ITableWriterUnary.Table<Object>>> getAllCurrentTablesDirectInstances() {
        return Collections.unmodifiableSet(tableDirectInstances.entrySet());
    }
    public Set<Entry<EStructuralFeature, ITableWriterBinary.Table<Object, Object>>> getAllCurrentTablesFeatures() {
        return Collections.unmodifiableSet(tableFeatures.entrySet());
    }

	
}