blob: 79d202d3afdd54ed136b33b79db5625fcccc8a0a [file] [log] [blame]
Zakir Meer9f251f52018-06-17 16:21:31 +02001package templates
2
3import com.google.inject.Inject
4import com.inchron.realtime.root.RootFactory
5import com.inchron.realtime.root.model.Model
6import com.inchron.realtime.root.model.ModelFactory
7import java.io.File
8import org.apache.log4j.ConsoleAppender
9import org.apache.log4j.PatternLayout
10import org.eclipse.app4mc.amalthea.model.Amalthea
11import org.eclipse.app4mc.transformation.extensions.base.templates.Model2ModelRootTransformer
12import org.eclipse.emf.common.util.URI
13import org.eclipse.emf.ecore.EObject
14import org.eclipse.emf.ecore.resource.ResourceSet
15import templates.utils.AmltCacheModel
16
17class AmaltheaTransformer extends Model2ModelRootTransformer{
18
19 /*- Factory initiaization */
20 val inchronRootFactory = RootFactory.eINSTANCE
21
22 val inchronModelFactory = ModelFactory.eINSTANCE
23
24 /*- Transformer classes initiaization */
25 @Inject extension SWTransformer swTransformer
26
27 @Inject extension HWTransformer hwTransformer
28
29 @Inject extension OSTransformer osTransformer
30
31 @Inject extension StimuliTransformer stimuliTransformer
32
33 /**
34 * This method performs the transformation of AMALTHEA model to INCHRON model and saves the transformed model in the output directory.
35 */
36 override m2mTransformation(ResourceSet inputResourceSet, ResourceSet outputResourceSet) {
37
38 /*- Associating CacheModel to the transformation.
39 * Note: This is a cummulative cache of all the elements from various input AMALTHEA model files.
40 */
41 var AmltCacheModel cacheModel = new AmltCacheModel
42
43 customObjsStore.injectMembers(AmltCacheModel, cacheModel)
44
45 var int fileIndex = 1
46
47 for (resource : inputResourceSet.resources) {
48 for (content : resource.contents) {
49
50 logger.info("Processing file : " + resource.URI)
51
52 /*- Building INCHRON model from AMALTHEA input model */
53 val inchronRoot = transform(content as Amalthea)
54
55 val out_uri = URI.createFileURI(
56 getProperty("m2m_output_folder") + File.separator + fileIndex++ + ".root")
57
58 val out_resource = outputResourceSet.createResource(out_uri)
59
60 /*-Attaching a resource to the INCHRON model element */
61 out_resource.contents.add(inchronRoot)
62
63 }
64 }
65
66 /*- Saving all the root INCHRON model files*/
67 for (resource : outputResourceSet.resources) {
68
69 resource.save(null)
70
71 logger.info("Transformed model file generated at : " + resource.URI)
72 }
73
74 logger.info("*********************** Completed : Model to Model transformation **************************")
75 }
76
77
78 /**
79 * This method is used to transform AMALTHEA model to INCHRON model
80 */
81 def create inchronRootFactory.createRoot transform(Amalthea amalthea){
82
83 /*-Step 1: Injecting all the required transformation objects into the CustomObjsStore. Advantage with this approach is, it provides flexibility to access these elements across various transformers */
84
85 customObjsStore.injectMembers(SWTransformer , swTransformer)
86
87 customObjsStore.injectMembers(HWTransformer , hwTransformer)
88
89 customObjsStore.injectMembers(OSTransformer , osTransformer)
90
91 customObjsStore.injectMembers(StimuliTransformer , stimuliTransformer)
92
93 /* Step 2: Building INCHRON model by invoking various transformers */
94 model = createInchronModel(amalthea)
95
96
97
98 }
99
100 /**
101 * This method creates the object of INCHRON Model element, and fills it by invoking various transformations
102 */
103 def create inchronModelFactory.createModel createInchronModel(Amalthea amalthea) {
104
105 setRequiredAttributes(amalthea, it)
106
107 hwTransformer.transfromHWModel(amalthea.hwModel,it)
108
109 }
110
111 /**
112 * Each transformation object should be set with the required attributes.
113 * At present the required attributes are : amaltheaModel and inchronModel elements. In future this list can grow
114 */
115 protected def EObject setRequiredAttributes(Amalthea amalthea, Model inchronModel) {
116
117 swTransformer.amaltheaModel=amalthea
118 swTransformer.inchronModel=inchronModel
119
120 hwTransformer.amaltheaModel=amalthea
121 hwTransformer.inchronModel=inchronModel
122
123 osTransformer.amaltheaModel=amalthea
124 osTransformer.inchronModel=inchronModel
125
126 stimuliTransformer.amaltheaModel=amalthea
127 stimuliTransformer.inchronModel=inchronModel
128
129 }
130
131 /**
132 * Overriding the logger creation method and attaching "ConsoleAppender" to it.
133 */
134 override protected getLogger() {
135 var logger=super.getLogger()
136 return logger
137
138 }
139
140
141}