Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4e730e98856f4e0e757d971bee9b7bc7a3a7fa29 (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
/*******************************************************************************
 * Copyright (c) 2019 Geza Kulcsar, 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 org.eclipse.viatra.query.runtime.cps.tests

import java.io.IOException
import java.util.HashMap
import java.util.Map
import java.util.function.Function
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.viatra.examples.cps.cyberPhysicalSystem.ApplicationType
import org.eclipse.viatra.examples.cps.cyberPhysicalSystem.CyberPhysicalSystemPackage
import org.eclipse.viatra.examples.cps.cyberPhysicalSystem.Identifiable
import org.eclipse.viatra.query.patternlanguage.emf.tests.pltest.Child1
import org.eclipse.viatra.query.patternlanguage.emf.tests.pltest.PltestFactory
import org.eclipse.viatra.query.patternlanguage.emf.tests.pltest.PltestPackage
import org.eclipse.viatra.query.runtime.base.api.BaseIndexOptions
import org.eclipse.viatra.query.runtime.cps.tests.queries.util.ApplicationInstancesOfApplicationTypeQuerySpecification
import org.eclipse.viatra.query.runtime.cps.tests.queries.util.ApplicationTypesQuerySpecification
import org.eclipse.viatra.query.runtime.emf.EMFScope
import org.eclipse.viatra.query.testing.core.SnapshotHelper
import org.eclipse.viatra.query.testing.core.XmiModelUtil
import org.eclipse.viatra.query.testing.core.XmiModelUtil.XmiModelUtilRunningOptionEnum
import org.eclipse.viatra.query.testing.core.api.ViatraQueryTest
import org.eclipse.viatra.query.testing.snapshot.CustomEMFSubstitution
import org.eclipse.viatra.query.testing.snapshot.QuerySnapshot
import org.junit.Assert
import org.junit.Test

class CustomEMFObjectTest {
	
	
	val Map<EClass, Function<EObject,String>> customMap
	
	val ResourceSet rs
	
	val BackendType type = BackendType.Rete
	
	def snapshot(String snp, ResourceSet rs) {
        val modelUri = XmiModelUtil::resolvePlatformURI(XmiModelUtilRunningOptionEnum.BOTH, snp)
        val snr = rs.getResource(modelUri, true)
        return snr.allContents.filter(QuerySnapshot).head
    }
    
    def snapshotHelper(Map<EClass, Function<EObject,String>> customMap) {
        return new SnapshotHelper(new HashMap, customMap)        
    }
    
    def EMFScope makeScope(String snp, ResourceSet rSet) {
        val options = new BaseIndexOptions().withDanglingFreeAssumption(false) 
        val uri = XmiModelUtil::resolvePlatformURI(XmiModelUtilRunningOptionEnum.BOTH, snp)
        rSet.getResource(uri , true)
        return new EMFScope(rSet, options)
    }
	
	
	def QuerySnapshot loadSnapshotFromUri(ResourceSet set, URI uri) throws IOException {
        val res = set.getResource(uri, true);
        if (!res.loaded) {
            res.load(newHashMap)
        }
        val snapshot = res.getContents()?.findFirst[it instanceof QuerySnapshot]  
        if (snapshot instanceof QuerySnapshot) {
            return snapshot
        } else {
            throw new IOException(String.format("Resource at uri %S does not contain a query snapshot.", uri.toString()));
        }
    }
    
	new() {
	    
        customMap = new HashMap
        rs = new ResourceSetImpl
	    
	} 
    	
    @Test
    def void basicCustomTest() {
        
        customMap.put(CyberPhysicalSystemPackage.Literals.APPLICATION_TYPE, [at | (at as ApplicationType).identifier])
        
        ViatraQueryTest.test(ApplicationTypesQuerySpecification.instance, snapshotHelper(customMap))
                        .on(makeScope("org.eclipse.viatra.query.runtime.cps.tests/models/snapshots/customemftest_apptypes.snapshot", rs))
                        .with(snapshot("org.eclipse.viatra.query.runtime.cps.tests/models/snapshots/customemftest_apptypes.snapshot", rs))
                        .with(type.hints)
                        .assertEquals
    }
    
    @Test
    def void hybridPatternTest() {
        
        customMap.put(CyberPhysicalSystemPackage.Literals.APPLICATION_TYPE, [at | (at as ApplicationType).identifier])
        
        ViatraQueryTest.test(ApplicationInstancesOfApplicationTypeQuerySpecification.instance, snapshotHelper(customMap))
                        .on(makeScope("org.eclipse.viatra.query.runtime.cps.tests/models/snapshots/customemftest_AIwithAT.snapshot", rs))
                        .with(snapshot("org.eclipse.viatra.query.runtime.cps.tests/models/snapshots/customemftest_AIwithAT.snapshot", rs))
                        .with(type.hints)
                        .assertEquals
    }
    
    
    @Test
    def void classOnlySupertypeHasCustomTest() {
        
        customMap.put(CyberPhysicalSystemPackage.Literals.IDENTIFIABLE, [id | "ID" + (id as Identifiable).identifier])
        
        ViatraQueryTest.test(ApplicationTypesQuerySpecification.instance, snapshotHelper(customMap))
                        .on(makeScope("org.eclipse.viatra.query.runtime.cps.tests/models/snapshots/customemftest_apptypes_id.snapshot", rs))
                        .with(snapshot("org.eclipse.viatra.query.runtime.cps.tests/models/snapshots/customemftest_apptypes_id.snapshot", rs))
                        .with(type.hints)
                        .assertEquals
    }
    
    @Test
    def void classAndSupertypeHasCustomTest() {
        
        customMap.put(CyberPhysicalSystemPackage.Literals.IDENTIFIABLE, [id | "ID" + (id as Identifiable).identifier])
        customMap.put(CyberPhysicalSystemPackage.Literals.APPLICATION_TYPE, [at | (at as ApplicationType).identifier])
        
        ViatraQueryTest.test(ApplicationTypesQuerySpecification.instance, snapshotHelper(customMap))
                        .on(makeScope("org.eclipse.viatra.query.runtime.cps.tests/models/snapshots/customemftest_apptypes.snapshot", rs))
                        .with(snapshot("org.eclipse.viatra.query.runtime.cps.tests/models/snapshots/customemftest_apptypes.snapshot", rs))
                        .with(type.hints)
                        .assertEquals
    }
    
    @Test
    def void grandparentHasCustomTest() {
        
        val child = PltestFactory.eINSTANCE.createChild1()
        child.name = "Child1"
        
        customMap.put(PltestPackage.Literals.BASE, [ch | "BASE:" + (ch as Child1).name])
        
        Assert.assertEquals((snapshotHelper(customMap).createSubstitution("Child1", child) as CustomEMFSubstitution).value, "BASE:Child1")
        
    }
    
    @Test
    def void grandparentAndParentHasCustomTest() {
        
        val child = PltestFactory.eINSTANCE.createChild1()
        child.name = "Child1"
        
        customMap.put(PltestPackage.Literals.BASE, [ch | "BASE:" + (ch as Child1).name])
        customMap.put(PltestPackage.Literals.COMMON, [ch | "COMMON:" + (ch as Child1).name])
        
        Assert.assertEquals((snapshotHelper(customMap).createSubstitution("Child1", child) as CustomEMFSubstitution).value, "COMMON:Child1")
        
    }
    
    @Test
    def void grandparentAndOtherParentHasCustomTest() {
        
        val child = PltestFactory.eINSTANCE.createChild1()
        child.name = "Child1"
        
        customMap.put(PltestPackage.Literals.BASE, [ch | "BASE:" + (ch as Child1).name])
        customMap.put(PltestPackage.Literals.INTERFACE, [ch | "INTERFACE:" + (ch as Child1).name])
        
        Assert.assertEquals((snapshotHelper(customMap).createSubstitution("Child1", child) as CustomEMFSubstitution).value, "INTERFACE:Child1")
        
    }
    
}

Back to the top