blob: caed363f740fb856d197f2e18ff56168bbe35727 [file] [log] [blame]
package templates.m2m;
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.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import org.eclipse.app4mc.amalthea.model.Amalthea
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.resource.ResourceSet
import templates.HWTransformer
import templates.m2m.os.OSTransformer
import templates.utils.AmltCacheModel
import templates.m2m.mapping.MappingTransformer
public class Amlt2InchronTransformer extends Model2ModelRootTransformer {
/*- Factory initiaization */
val inchronRootFactory = RootFactory.eINSTANCE
val inchronModelFactory = ModelFactory.eINSTANCE
@Inject extension templates.m2m.hw.HWTransformer hwTransformer
@Inject extension OSTransformer osTransformer
@Inject extension MappingTransformer mappingTransformer
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)
osTransformer.transfromOSModel(amalthea.osModel,it)
mappingTransformer.transfromMappingModel(amalthea.mappingModel, 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 DateFormat dateFormat = new SimpleDateFormat("dd.MMMM.yyyy", Locale.ENGLISH)
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)
}
}