blob: 6b0b791b0f8106449ec37653f3d2f3e0df24c4f3 [file] [log] [blame]
package templates
import com.google.inject.Inject
import com.inchron.realtime.root.RootFactory
import com.inchron.realtime.root.model.Model
import com.inchron.realtime.root.model.ModelFactory
import java.io.File
import java.lang.String
import java.text.DateFormat;
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import org.apache.log4j.ConsoleAppender
import org.apache.log4j.PatternLayout
import org.eclipse.app4mc.amalthea.model.Amalthea
import org.eclipse.app4mc.amalthea.model.Value
import org.eclipse.app4mc.amalthea.model.StringObject
import org.eclipse.app4mc.transformation.extensions.base.templates.Model2ModelRootTransformer
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.resource.ResourceSet
import templates.utils.AmltCacheModel
class AmaltheaTransformer extends Model2ModelRootTransformer{
/*- Factory initiaization */
val inchronRootFactory = RootFactory.eINSTANCE
val inchronModelFactory = ModelFactory.eINSTANCE
/*- Transformer classes initiaization */
@Inject extension SWTransformer swTransformer
@Inject extension HWTransformer hwTransformer
@Inject extension OSTransformer osTransformer
@Inject extension StimuliTransformer stimuliTransformer
DateFormat dateFormat = new SimpleDateFormat("dd.MMMM.yyyy", Locale.ENGLISH)
/**
* This method performs the transformation of AMALTHEA model to INCHRON model and saves the transformed model in the output directory.
*/
override m2mTransformation(ResourceSet inputResourceSet, ResourceSet outputResourceSet) {
/*- Associating CacheModel to the transformation.
* Note: This is a cummulative cache of all the elements from various input AMALTHEA model files.
*/
var AmltCacheModel cacheModel = new AmltCacheModel
customObjsStore.injectMembers(AmltCacheModel, cacheModel)
var int fileIndex = 1
for (resource : inputResourceSet.resources) {
for (content : resource.contents) {
logger.info("Processing file : " + resource.URI)
/*- Building INCHRON model from AMALTHEA input model */
val inchronRoot = transform(content as Amalthea)
val out_uri = URI.createFileURI(
getProperty("m2m_output_folder") + File.separator + fileIndex++ + ".root")
val out_resource = outputResourceSet.createResource(out_uri)
/*-Attaching a resource to the INCHRON model element */
out_resource.contents.add(inchronRoot)
}
}
/*- Saving all the root INCHRON model files*/
for (resource : outputResourceSet.resources) {
resource.save(null)
logger.info("Transformed model file generated at : " + resource.URI)
}
logger.info("*********************** Completed : Model to Model transformation **************************")
}
/**
* This method is used to transform AMALTHEA model to INCHRON model
*/
def create inchronRootFactory.createRoot transform(Amalthea amalthea){
/*-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 */
customObjsStore.injectMembers(SWTransformer , swTransformer)
customObjsStore.injectMembers(HWTransformer , hwTransformer)
customObjsStore.injectMembers(OSTransformer , osTransformer)
customObjsStore.injectMembers(StimuliTransformer , stimuliTransformer)
/* Step 2: Building INCHRON model by invoking various transformers */
model = createInchronModel(amalthea)
}
/**
* This method creates the object of INCHRON Model element, and fills it by invoking various transformations
*/
def create inchronModelFactory.createModel createInchronModel(Amalthea amalthea) {
setRequiredAttributes(amalthea, it)
transformMetaInfo(amalthea, it)
hwTransformer.transfromHWModel(amalthea.hwModel,it)
}
/**
* Create and fill the INCHRON model's GeneralInfo with meta information,
* mainly from the Amalthea customProperties map.
* @Note: customProperties keys used here are currently informal/not specified.
*/
public def transformMetaInfo(Amalthea amalthea, Model inchronModel) {
var info = inchronModelFactory.createGeneralInfo()
info.setCreator(("Amlt2Inchron " + amalthea.getVersion() + " " + new Date()).toString())
info.setVersion("1")
val value = amalthea.getCustomProperties().get("Date_Last_Modified")
if (value instanceof StringObject) {
val date = (value as StringObject).getValue()
if (date != null)
info.setModifiedDate(dateFormat.parse(date))
}
val author = amalthea.getCustomProperties().get("Author")
if (author instanceof StringObject)
info.setAuthor((author as StringObject).getValue())
val descr = amalthea.getCustomProperties().get("Description")
if (descr instanceof StringObject)
info.setDescription((descr as StringObject).getValue())
inchronModel.setGeneralInfo(info)
}
/**
* Each transformation object should be set with the required attributes.
* At present the required attributes are : amaltheaModel and inchronModel elements. In future this list can grow
*/
protected def EObject setRequiredAttributes(Amalthea amalthea, Model inchronModel) {
swTransformer.amaltheaModel=amalthea
swTransformer.inchronModel=inchronModel
hwTransformer.amaltheaModel=amalthea
hwTransformer.inchronModel=inchronModel
osTransformer.amaltheaModel=amalthea
osTransformer.inchronModel=inchronModel
stimuliTransformer.amaltheaModel=amalthea
stimuliTransformer.inchronModel=inchronModel
}
/**
* Overriding the logger creation method and attaching "ConsoleAppender" to it.
*/
override protected getLogger() {
var logger=super.getLogger()
return logger
}
}