summaryrefslogtreecommitdiff
path: root/org.genivi.commonapi.core/src/org/genivi/commonapi/core/generator/FrancaGenerator.xtend
blob: 55e12e1f0bacd357ad3902f571e6d453d895e9f3 (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* Copyright (C) 2013 BMW Group
 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
 * Author: Juergen Gehring (juergen.gehring@bmw.de)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.genivi.commonapi.core.generator

import java.util.Collection
import java.util.HashSet
import java.util.LinkedList
import java.util.List
import javax.inject.Inject
import org.eclipse.core.resources.IResource
import org.eclipse.core.resources.ResourcesPlugin
import org.eclipse.core.runtime.Path
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.IGenerator
import org.franca.core.dsl.FrancaPersistenceManager
import org.franca.core.franca.FArrayType
import org.franca.core.franca.FEnumerationType
import org.franca.core.franca.FInterface
import org.franca.core.franca.FMapType
import org.franca.core.franca.FModel
import org.franca.core.franca.FStructType
import org.franca.core.franca.FType
import org.franca.core.franca.FTypeCollection
import org.franca.core.franca.FTypeDef
import org.franca.core.franca.FTypeRef
import org.franca.core.franca.FUnionType
import org.franca.deploymodel.core.FDModelExtender
import org.franca.deploymodel.core.FDeployedInterface
import org.franca.deploymodel.core.FDeployedTypeCollection
import org.franca.deploymodel.dsl.fDeploy.FDInterface
import org.franca.deploymodel.dsl.fDeploy.FDProvider
import org.franca.deploymodel.dsl.fDeploy.FDTypes
import org.genivi.commonapi.core.deployment.PropertyAccessor
import org.genivi.commonapi.core.preferences.FPreferences
import org.genivi.commonapi.core.preferences.PreferenceConstants

import static com.google.common.base.Preconditions.*
import org.franca.deploymodel.dsl.fDeploy.FDModel

class FrancaGenerator implements IGenerator
{
    @Inject private extension FTypeCollectionGenerator
    @Inject private extension FInterfaceGenerator
    @Inject private extension FInterfaceProxyGenerator
    @Inject private extension FInterfaceStubGenerator
    @Inject private extension FrancaGeneratorExtensions
    
    @Inject private FrancaPersistenceManager francaPersistenceManager
    @Inject private FDeployManager fDeployManager

    override doGenerate(Resource input, IFileSystemAccess fileSystemAccess)
    {
        var FModel fModel
        var List<FDInterface> deployedInterfaces
        var List<FDTypes> deployedTypeCollections
        var List<FDProvider> deployedProviders
        var IResource res = null

		// load the model from a fidl file
        if(input.URI.fileExtension.equals(francaPersistenceManager.fileExtension))
        {
            fModel = francaPersistenceManager.loadModel(input.URI, input.URI)
            deployedInterfaces = new LinkedList<FDInterface>()

        }
        // load the model from deployment file
        else if(input.URI.fileExtension.equals(FDeployManager.fileExtension))
        {
        	var model = fDeployManager.loadModel(input.URI, input.URI);
            if(model instanceof FDModel) {
            	val fModelExtender = new FDModelExtender(model);
            	checkArgument(fModelExtender.getFDInterfaces().size > 0, "No Interfaces were deployed, nothing to generate.")
            	fModel = fModelExtender.getFDInterfaces().get(0).target.model
            	deployedInterfaces = fModelExtender.getFDInterfaces()
            	deployedTypeCollections = fModelExtender.getFDTypesList()
            	deployedProviders = fModelExtender.getFDProviders()            	
            } else if(model instanceof FModel) {
            	fModel = model
            	deployedInterfaces = new LinkedList<FDInterface>()
            }
        }
        else
        {
            checkArgument(false, "Unknown input: " + input)
        }

        try
        {
            var pathfile = input.URI.toPlatformString(false);
            if(pathfile == null)
            {
                pathfile = FPreferences::instance.getModelPath(fModel)
            }
            if(pathfile.startsWith("platform:/"))
            {
                pathfile = pathfile.substring(pathfile.indexOf("platform") + 10)
                pathfile = pathfile.substring(pathfile.indexOf(System.getProperty("file.separator")))
            }

            res = ResourcesPlugin.workspace.root.findMember(pathfile)

            doGenerateComponents(fModel, deployedInterfaces, deployedTypeCollections, deployedProviders, fileSystemAccess, res)
        }
        catch(IllegalStateException e)
        {
            //happens only when the cli calls the francagenerator
        }
        doGenerateComponents(fModel, deployedInterfaces, deployedTypeCollections, deployedProviders, fileSystemAccess, res)
    }

    def private doGenerateComponents(FModel fModel, 
    								 List<FDInterface> deployedInterfaces, 
    								 List<FDTypes> deployedTypeCollections,
    								 List<FDProvider> deployedProviders, 
    								 IFileSystemAccess fileSystemAccess, 
    								 IResource res)
    {

        val allReferencedFTypes = fModel.allReferencedFTypes
        val allFTypeTypeCollections = allReferencedFTypes.filter[eContainer instanceof FTypeCollection].map[
            eContainer as FTypeCollection]
        val allFTypeFInterfaces = allReferencedFTypes.filter[eContainer instanceof FInterface].map[eContainer as FInterface]

        val generateTypeCollections = fModel.typeCollections.toSet
        generateTypeCollections.addAll(allFTypeTypeCollections)

        val generateInterfaces = fModel.allReferencedFInterfaces.toSet
        generateInterfaces.addAll(allFTypeFInterfaces)

		val defaultDeploymentAccessor = new PropertyAccessor() 
		generateInterfaces.forEach [
            val currentInterface = it
            var PropertyAccessor deploymentAccessor
            if (deployedInterfaces != null && deployedInterfaces.exists[it.target == currentInterface])
            {
                deploymentAccessor = new PropertyAccessor(
                    						new FDeployedInterface(deployedInterfaces.filter[it.target == currentInterface].last))
            }
            else
            {
                deploymentAccessor = defaultDeploymentAccessor
            }
            generate(it, fileSystemAccess, deploymentAccessor, res)
        ]
        
        generateTypeCollections.forEach [
            val currentTypeCollection = it
            if (!(currentTypeCollection instanceof FInterface)) {
                var PropertyAccessor deploymentAccessor
	            if (deployedTypeCollections != null && deployedTypeCollections.exists[it.target == currentTypeCollection]) {
	                deploymentAccessor = new PropertyAccessor(
	                    new FDeployedTypeCollection(deployedTypeCollections.filter[it.target == currentTypeCollection].last))
	            } else {
	                deploymentAccessor = defaultDeploymentAccessor
	            }
	            generate(it, fileSystemAccess, deploymentAccessor, res)
            }
	    ]

        fModel.interfaces.forEach [
            val currentInterface = it
            var PropertyAccessor deploymentAccessor
            if(deployedInterfaces.exists[it.target == currentInterface])
            {
                deploymentAccessor = new PropertyAccessor(
                    new FDeployedInterface(deployedInterfaces.filter[it.target == currentInterface].last))
            }
            else
            {
                deploymentAccessor = defaultDeploymentAccessor
            }
            val booleanTrue = Boolean.toString(true)
            var String finalValue = booleanTrue
            finalValue = FPreferences::instance.getPreference(PreferenceConstants::P_GENERATEPROXY, finalValue)
            if(finalValue.equals(booleanTrue))
            {
                it.generateProxy(fileSystemAccess, deploymentAccessor, res)
            }
            finalValue = booleanTrue
            finalValue = FPreferences::instance.getPreference(PreferenceConstants::P_GENERATESTUB, finalValue)
            if(finalValue.equals(booleanTrue))
            {
                it.generateStub(fileSystemAccess, res)
            }
        ]

        return;
    }

    private var String filePrefix = "file://"

    def getFilePathUrl(Resource resource)
    {
        val filePath = resource.filePath
        return filePrefix + filePath
    }

    def private getFilePath(Resource resource)
    {
        if(resource.URI.file)
        {
            return resource.URI.toFileString
        }

        val platformPath = new Path(resource.URI.toPlatformString(true))
        val file = ResourcesPlugin::getWorkspace().getRoot().getFile(platformPath);

        return file.location.toString
    }

    def private getAllReferencedFInterfaces(FModel fModel)
    {
        val referencedFInterfaces = fModel.interfaces.toSet
        fModel.interfaces.forEach[base?.addFInterfaceTree(referencedFInterfaces)]
        fModel.interfaces.forEach[managedInterfaces.forEach[addFInterfaceTree(referencedFInterfaces)]]
        return referencedFInterfaces
    }

    def private void addFInterfaceTree(FInterface fInterface, Collection<FInterface> fInterfaceReferences)
    {
        if(!fInterfaceReferences.contains(fInterface))
        {
            fInterfaceReferences.add(fInterface)
            fInterface.base?.addFInterfaceTree(fInterfaceReferences)
        }
    }

    def private getAllReferencedFTypes(FModel fModel)
    {
        val referencedFTypes = new HashSet<FType>

        fModel.typeCollections.forEach[types.forEach[addFTypeDerivedTree(referencedFTypes)]]

        fModel.interfaces.forEach [
            attributes.forEach[type.addDerivedFTypeTree(referencedFTypes)]
            types.forEach[addFTypeDerivedTree(referencedFTypes)]
            methods.forEach [
                inArgs.forEach[type.addDerivedFTypeTree(referencedFTypes)]
                outArgs.forEach[type.addDerivedFTypeTree(referencedFTypes)]
            ]
            broadcasts.forEach [
                outArgs.forEach[type.addDerivedFTypeTree(referencedFTypes)]
            ]
        ]

        return referencedFTypes
    }

    def private void addDerivedFTypeTree(FTypeRef fTypeRef, Collection<FType> fTypeReferences)
    {
        fTypeRef.derived?.addFTypeDerivedTree(fTypeReferences)
    }

    def private dispatch void addFTypeDerivedTree(FTypeDef fTypeDef, Collection<FType> fTypeReferences)
    {
        if(!fTypeReferences.contains(fTypeDef))
        {
            fTypeReferences.add(fTypeDef)
            fTypeDef.actualType.addDerivedFTypeTree(fTypeReferences)
        }
    }

    def private dispatch void addFTypeDerivedTree(FArrayType fArrayType, Collection<FType> fTypeReferences)
    {
        if(!fTypeReferences.contains(fArrayType))
        {
            fTypeReferences.add(fArrayType)
            fArrayType.elementType.addDerivedFTypeTree(fTypeReferences)
        }
    }

    def private dispatch void addFTypeDerivedTree(FMapType fMapType, Collection<FType> fTypeReferences)
    {
        if(!fTypeReferences.contains(fMapType))
        {
            fTypeReferences.add(fMapType)
            fMapType.keyType.addDerivedFTypeTree(fTypeReferences)
            fMapType.valueType.addDerivedFTypeTree(fTypeReferences)
        }
    }

    def private dispatch void addFTypeDerivedTree(FStructType fStructType, Collection<FType> fTypeReferences)
    {
        if(!fTypeReferences.contains(fStructType))
        {
            fTypeReferences.add(fStructType)
            fStructType.base?.addFTypeDerivedTree(fTypeReferences)
            fStructType.elements.forEach[type.addDerivedFTypeTree(fTypeReferences)]
        }
    }

    def private dispatch void addFTypeDerivedTree(FEnumerationType fEnumerationType, Collection<FType> fTypeReferences)
    {
        if(!fTypeReferences.contains(fEnumerationType))
        {
            fTypeReferences.add(fEnumerationType)
            fEnumerationType.base?.addFTypeDerivedTree(fTypeReferences)
        }
    }

    def private dispatch void addFTypeDerivedTree(FUnionType fUnionType, Collection<FType> fTypeReferences)
    {
        if(!fTypeReferences.contains(fUnionType))
        {
            fTypeReferences.add(fUnionType)
            fUnionType.base?.addFTypeDerivedTree(fTypeReferences)
            fUnionType.elements.forEach[type.addDerivedFTypeTree(fTypeReferences)]
        }
    }

}