123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768 |
- /*
- * generated by Xtext 2.10.0
- */
- package be.uantwerpen.ansymo.semanticadaptation.generator
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Adaptation
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.BoolLiteral
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.BuiltinFunction
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Close
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Connection
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.DeclaredParameter
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Expression
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.InnerFMUDeclarationFull
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.IntLiteral
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.IsSet
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Port
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.RealLiteral
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.SemanticAdaptation
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.SemanticAdaptationFactory
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.SingleParamDeclaration
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.SingleVarDeclaration
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.StringLiteral
- import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Variable
- import java.io.ByteArrayOutputStream
- import java.util.HashMap
- import org.eclipse.emf.ecore.EObject
- import org.eclipse.emf.ecore.resource.Resource
- import org.eclipse.xtext.EcoreUtil2
- import org.eclipse.xtext.generator.AbstractGenerator
- import org.eclipse.xtext.generator.IFileSystemAccess2
- import org.eclipse.xtext.generator.IGeneratorContext
- /**
- * Generates code from your model files on save.
- *
- * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#code-generation
- */
- class SemanticAdaptationCanonicalGenerator extends AbstractGenerator {
-
- String CANONICAL_EXT = ".BASE.sa"
- String NAME_SUFFIX = "_BASE"
-
- override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
- println("Generating canonical semantic adaptation for file " + resource.URI.toFileString() + "...")
-
- println("Resource URI information:")
- println("\t resource.URI.lastSegment = " + resource.URI.lastSegment())
- println("\t resource.URI.trimFileExtension = " + resource.URI.trimFileExtension())
-
- println("______________________________File Read______________________________")
- var outputByteArray = new ByteArrayOutputStream()
- resource.save(outputByteArray, null)
- println(outputByteArray.toString())
- outputByteArray.close()
- println("__________________________________________________________________________")
-
- // Create file name for the canonical sa file
- var fileNameWithoutExt = resource.URI.trimFileExtension().lastSegment()
- var canonicalFileName = fileNameWithoutExt + CANONICAL_EXT
- println("canonicalFileName = " + canonicalFileName)
-
- // Create in memory representation of canonical SA file
- var adaptations = resource.allContents.toIterable.filter(SemanticAdaptation).last.elements.filter(Adaptation);
- if (adaptations.size > 1){
- throw new Exception("Only one semantic adaptation is supported per .sa file")
- }
- var adaptation = adaptations.head
-
- println("Checking if file is already a canonical version...")
- if (adaptation.name.indexOf(NAME_SUFFIX) == -1){
- println("It is not.")
-
- adaptation.name = adaptation.name + NAME_SUFFIX
-
- adaptation.canonicalize
-
- outputByteArray = new ByteArrayOutputStream()
- adaptation.eResource.save(outputByteArray,null)
-
- println("______________________________Generated file______________________________")
- println(outputByteArray.toString())
- println("__________________________________________________________________________")
-
- fsa.generateFile(canonicalFileName, outputByteArray.toString())
- println("File " + canonicalFileName + " written.")
- outputByteArray.close()
-
- println("Generating canonical semantic adaptation for file " + resource.URI + "... DONE.")
-
- } else {
- println("It is already a canonical version.")
- println("Nothing to do.")
- }
- }
-
- def canonicalize(Adaptation sa){
-
- //inferUnits(sa)
-
-
- // Type inference
- genericDeclarationInferenceAlgorithm(sa ,
- [// getField
- element | {
- if (element instanceof SingleParamDeclaration) {
- return element.type
- } else if (element instanceof Port){
- return element.type
- } else if (element instanceof SingleVarDeclaration){
- return element.type
- } else {
- throw new Exception("Unexpected element type: " + element)
- }
- }
- ],
- [// setField
- element, value | {
- if (element instanceof SingleParamDeclaration) {
- element.type = value as String
- } else if (element instanceof Port){
- element.type = value as String
- } else if (element instanceof SingleVarDeclaration){
- element.type = value as String
- } else {
- throw new Exception("Unexpected element type: " + element)
- }
- }
- ],
- [// inferField
- element | {
- if (element instanceof SingleParamDeclaration) {
- return extractTypeFromExpression(element.expr, element.name)
- } else if (element instanceof Port){
- return getPortType(element)
- } else if (element instanceof SingleVarDeclaration){
- return extractTypeFromExpression(element.expr, element.name)
- } else {
- throw new Exception("Unexpected element type: " + element)
- }
- }
- ]
- )
-
- // TODO Add input ports
-
- // Add in params
- addInParams(sa)
-
- }
-
- def genericDeclarationInferenceAlgorithm(Adaptation sa,
- (EObject)=>Object getField,
- (EObject, Object)=>void setField,
- (EObject)=>Object inferField
- ){
- println("Running generic inference algorithm...")
-
- /*
- * Dumbest (and simplest) algorithm for this is a fixed point computation:
- * 1. Look for every var/port declaration
- * 2. If that var has a XXX already, nothing else to be done.
- * 3. If that var has no XXX declared, then
- * 3.1 If var/port has an initial value or connection, then
- * 3.1.1 If the initial_value/connection has a XXX declared, then var gets that XXX.
- * 3.1.2 Otherwise, nothing else to be done.
- * 3.2 If var/port has no initial value or connection then this either is a missing feature, or an error.
- * 3.3 If something has changed, go to 1. Otherwise, end.
- */
- var fixedPoint = false
- var untypedElementsCounter = 0
- while (! fixedPoint){
- fixedPoint = true
- untypedElementsCounter = 0
-
- println("Inferring parameter fields...")
-
- for (paramDeclarations : sa.params) {
- for (paramDeclaration : paramDeclarations.declarations) {
- println("Computing field for param " + paramDeclaration.name)
- if(getField.apply(paramDeclaration) !== null){
- println("Already has been inferred: " + getField.apply(paramDeclaration))
- } else {
- println("Has not been inferred yet.")
- //var inferredTypeAttempt = extractTypeFromExpression(paramDeclaration.expr, paramDeclaration.name)
- var inferredTypeAttempt = inferField.apply(paramDeclaration)
- if (inferredTypeAttempt !== null){
- //paramDeclaration.type = inferredTypeAttempt
- setField.apply(paramDeclaration, inferredTypeAttempt)
- fixedPoint = false
- println("Got new field: " + inferredTypeAttempt)
- } else {
- untypedElementsCounter++
- println("Cannot infer field now.")
- }
- }
- }
- }
-
- if(sa.inner !== null){
- if(sa.inner instanceof InnerFMUDeclarationFull){
- var innerFMUFull = sa.inner as InnerFMUDeclarationFull
- for(fmu : innerFMUFull.fmus){
- println("Inferring port fields of FMU " + fmu.name)
- for (port : EcoreUtil2.getAllContentsOfType(fmu, Port)) {
- if(getField.apply(port) !== null){
- println("Already has a type: " + port.type)
- //} else if(inferPortType(port)) {
- } else {
- // TODO Refactor this with the above code, to check and infer field of generic type.
- var inferredTypeAttempt = inferField.apply(port)
- if (inferredTypeAttempt !== null){
- setField.apply(port, inferredTypeAttempt)
- fixedPoint = false
- println("Got new field: " + inferredTypeAttempt)
- } else {
- untypedElementsCounter++
- println("Cannot infer field now.")
- }
- }
- }
- }
-
- if (innerFMUFull.connection.size > 0){
- println("Inferring port fields using internal scenario bindings.")
- for (binding : innerFMUFull.connection){
- if (getField.apply(binding.src.port) !== null && getField.apply(binding.tgt.port) !== null){
- println("Both ports have fields already.")
- //} else if (inferPortTypeViaConnection(binding)){
- } else {
- var inferredTypeAttempt = inferPortFieldViaConnection(binding, getField, setField, inferField)
- if (inferredTypeAttempt !== null){
- setField.apply(binding, inferredTypeAttempt)
- fixedPoint = false
- untypedElementsCounter--
- println("Got new field: " + inferredTypeAttempt)
- } else {
- println("Cannot infer field from binding now.")
- }
- }
- }
- }
- } else {
- throw new Exception("Field inference only supported for InnerFMUDeclarationFull.")
- }
- }
-
- println("Inferring external port fields...")
-
- for (port : sa.inports) {
- //if (port.type !== null){
- if (getField.apply(port) !== null){
- println("Already has a field: " + getField.apply(port))
- //if (pushPortType(port)){
- if (pushPortField(port, getField, setField, inferField)){
- fixedPoint = false
- untypedElementsCounter--
- }
- //} else if (inferPortType(port)){
- } else {
- // TODO Refactor this with the above code, to check and infer field of generic type.
- var inferredTypeAttempt = inferField.apply(port)
- if (inferredTypeAttempt !== null){
- setField.apply(port, inferredTypeAttempt)
- fixedPoint = false
- println("Got new field: " + inferredTypeAttempt)
- } else {
- untypedElementsCounter++
- println("Cannot infer field now.")
- }
- }
- }
- for (port : sa.outports) {
- //if (port.type !== null){
- // TODO Refactor this with the above code, the treatment to external output ports is the exact same as for external input ports.
- if (getField.apply(port) !== null){
- println("Already has a field: " + getField.apply(port))
- //if (pushPortType(port)){
- if (pushPortField(port, getField, setField, inferField)){
- fixedPoint = false
- untypedElementsCounter--
- }
- //} else if (inferPortType(port)){
- } else {
- // TODO Refactor this with the above code, to check and infer field of generic type.
- var inferredTypeAttempt = inferField.apply(port)
- if (inferredTypeAttempt !== null){
- setField.apply(port, inferredTypeAttempt)
- fixedPoint = false
- println("Got new field: " + inferredTypeAttempt)
- } else {
- untypedElementsCounter++
- println("Cannot infer field now.")
- }
- }
- }
-
- println("Inferring all other declaration fields...")
-
- for (varDeclaration : EcoreUtil2.getAllContentsOfType(sa, SingleVarDeclaration)) {
- println("Computing type for declaration " + varDeclaration.name)
- //if(varDeclaration.type !== null){
- if(getField.apply(varDeclaration) !== null){
- println("Already has a field: " + getField.apply(varDeclaration))
- } else {
- //var inferredTypeAttempt = extractTypeFromExpression(varDeclaration.expr, varDeclaration.name)
- var inferredTypeAttempt = inferField.apply(varDeclaration)
- if (inferredTypeAttempt !== null){
- //varDeclaration.type = inferredTypeAttempt
- setField.apply(varDeclaration, inferredTypeAttempt)
- fixedPoint = false
- println("Got new type: " + inferredTypeAttempt)
- } else {
- untypedElementsCounter++
- println("Cannot infer field now.")
- }
- }
- }
-
- println("Ended iteration with unfielded elements remaining: " + untypedElementsCounter)
- } // while (! fixedPoint)
-
- if (untypedElementsCounter > 0){
- throw new Exception("Could not infer all fields. There are " + untypedElementsCounter + " unfielded elements.")
- }
-
- println("Running generic inference algorithm... DONE")
- }
-
- def inferTypes(Adaptation sa){
- println("Inferring types...")
-
- /*
- * Dumbest (and simplest) algorithm for this is a fixed point computation:
- * 1. Look for every var/port declaration
- * 2. If that var has a type already, nothing else to be done.
- * 3. If that var has no type declared, then
- * 3.1 If var/port has an initial value or connection, then
- * 3.1.1 If the initial_value/connection has a type declared, then var gets that type.
- * 3.1.2 Otherwise, nothing else to be done.
- * 3.2 If var/port has no initial value or connection then this either is a missing feature, or an error.
- * 3.3 If something has changed, go to 1. Otherwise, end.
- */
- var fixedPoint = false
- var untypedElementsCounter = 0
- while (! fixedPoint){
- fixedPoint = true
- untypedElementsCounter = 0
-
- println("Inferring parameter types...")
-
- for (paramDeclarations : sa.params) {
- for (paramDeclaration : paramDeclarations.declarations) {
- println("Computing type for param " + paramDeclaration.name)
- if(paramDeclaration.type !== null){
- println("Already has a type: " + paramDeclaration.type)
- } else {
- println("Has no type.")
- var inferredTypeAttempt = extractTypeFromExpression(paramDeclaration.expr, paramDeclaration.name)
- if (inferredTypeAttempt !== null){
- paramDeclaration.type = inferredTypeAttempt
- fixedPoint = false
- println("Got new type: " + paramDeclaration.type)
- } else {
- untypedElementsCounter++
- println("Cannot infer type now.")
- }
- }
- }
- }
-
- if(sa.inner !== null){
- if(sa.inner instanceof InnerFMUDeclarationFull){
- var innerFMUFull = sa.inner as InnerFMUDeclarationFull
- for(fmu : innerFMUFull.fmus){
- println("Inferring port types of FMU " + fmu.name)
- for (port : EcoreUtil2.getAllContentsOfType(fmu, Port)) {
- if(port.type !== null){
- println("Already has a type: " + port.type)
- } else if(inferPortType(port)) {
- fixedPoint = false
- } else {
- untypedElementsCounter++
- }
- }
- }
-
- if (innerFMUFull.connection.size > 0){
- println("Inferring port types using internal scenario bindings.")
- for (binding : innerFMUFull.connection){
- if (binding.src.port.type !== null && binding.tgt.port.type !== null){
- println("Both ports have types already.")
- } else if (inferPortTypeViaConnection(binding)){
- fixedPoint = false
- untypedElementsCounter--
- }
- }
- }
- } else {
- throw new Exception("Type inference only supported for InnerFMUDeclarationFull.")
- }
- }
-
- println("Inferring external port types...")
-
- for (port : sa.inports) {
- if (port.type !== null){
- println("Already has a type: " + port.type)
- if (pushPortType(port)){
- fixedPoint = false
- untypedElementsCounter--
- }
- } else if (inferPortType(port)){
- fixedPoint = false
- } else {
- untypedElementsCounter++
- }
- }
- for (port : sa.outports) {
- if (port.type !== null){
- println("Already has a type: " + port.type)
- if (pushPortType(port)){
- fixedPoint = false
- untypedElementsCounter--
- }
- } else if (inferPortType(port)){
- fixedPoint = false
- } else {
- untypedElementsCounter++
- }
- }
-
- println("Inferring all other declaration types...")
-
- for (varDeclaration : EcoreUtil2.getAllContentsOfType(sa, SingleVarDeclaration)) {
- println("Computing type for declaration " + varDeclaration.name)
- if(varDeclaration.type !== null){
- println("Already has a type: " + varDeclaration.type)
- } else {
- var inferredTypeAttempt = extractTypeFromExpression(varDeclaration.expr, varDeclaration.name)
- if (inferredTypeAttempt !== null){
- varDeclaration.type = inferredTypeAttempt
- fixedPoint = false
- println("Got new type: " + varDeclaration.type)
- } else {
- untypedElementsCounter++
- println("Cannot infer type now.")
- }
- }
- }
-
- println("Ended iteration with untyped elements remaining: " + untypedElementsCounter)
- } // while (! fixedPoint)
-
- if (untypedElementsCounter > 0){
- throw new Exception("Could not infer all types. There are " + untypedElementsCounter + " untyped elements.")
- }
-
- println("Inferring types... DONE")
- }
-
- def extractTypeFromExpression(Expression expression, String declarationName){
- if (expression instanceof IntLiteral){
- return "Integer"
- } else if (expression instanceof RealLiteral){
- return "Real"
- } else if (expression instanceof BoolLiteral){
- return "Bool"
- } else if (expression instanceof StringLiteral){
- return "String"
- } else if (expression instanceof Variable){
- var varRef = expression as Variable
- if (varRef.ref instanceof Port){
- var decl = varRef.ref as Port
- if (decl.type !== null){
- return decl.type
- }
- } else if(varRef.ref instanceof SingleParamDeclaration){
- var decl = varRef.ref as SingleParamDeclaration
- if (decl.type !== null){
- return decl.type
- }
- } else if(varRef.ref instanceof SingleVarDeclaration){
- var decl = varRef.ref as SingleVarDeclaration
- if (decl.type !== null){
- return decl.type
- }
- } else if(varRef.ref instanceof DeclaredParameter){
- throw new Exception("Type cannot be inferred for references to DeclaredParameter (for now). Please specify the explicit type of declaration " + declarationName)
- } else {
- throw new Exception("Unexpected kind of Variable expression found.")
- }
- } else if(expression instanceof BuiltinFunction){
- if (expression instanceof IsSet || expression instanceof Close){
- return "Bool"
- } else {
- return "Real"
- }
- } else {
- throw new Exception("Initial value for declaration " + declarationName + " must be literal or var ref for now. Got instead " + expression + ". If you want complex expressions, give it an explicit type.")
- }
- return null
- }
-
- def inferPortFieldViaConnection(Connection binding,
- (EObject)=>Object getField,
- (EObject, Object)=>void setField,
- (EObject)=>Object inferField
- ){
- var Object resultField = null
- if (getField.apply(binding.src.port) !== null && getField.apply(binding.tgt.port) !== null){
- throw new Exception("Wrong way of using this function. It assumes type is not inferred yet.")
- } else if (getField.apply(binding.src.port) !== null){
- //binding.tgt.port.type = binding.src.port.type
- resultField = getField.apply(binding.src.port)
- println("Target port "+ binding.tgt.port.name +" got new type: " + resultField)
- } else if (getField.apply(binding.tgt.port) !== null){
- //binding.src.port.type = binding.tgt.port.type
- resultField = getField.apply(binding.tgt.port)
- println("Target port "+ binding.src.port.name +" got new type: " + resultField)
- }
-
- return resultField
- }
-
- def inferPortTypeViaConnection(Connection binding){
- var typeInferred = false
-
- if (binding.src.port.type !== null && binding.tgt.port.type !== null){
- throw new Exception("Wrong way of using this function. It assumes type is not inferred yet.")
- } else if (binding.src.port.type !== null){
- binding.tgt.port.type = binding.src.port.type
- println("Target port "+ binding.tgt.port.name +" got new type: " + binding.tgt.port.type)
- } else if (binding.tgt.port.type !== null){
- binding.src.port.type = binding.tgt.port.type
- println("Target port "+ binding.src.port.name +" got new type: " + binding.src.port.type)
- }
-
- return typeInferred
- }
-
- def pushPortField(Port port,
- (EObject)=>Object getField,
- (EObject, Object)=>void setField,
- (EObject)=>Object inferField){
- var typeInferred = false
- println("Pushing field of port " + port.name + " to its bindings.")
-
- if(getField.apply(port) === null){
- println("Has no field to be pushed.")
- // TODO Throw exception wrong usage
- } else {
- println("Pushing field: " + getField.apply(port))
- if(port.sourcedependency !== null){
- println("Has a source dependency: " + port.sourcedependency.port.name)
- if(getField.apply(port.sourcedependency.port) === null){
- //port.sourcedependency.port.type = port.type
- setField.apply(port.sourcedependency.port, getField.apply(port))
- println("Port " + port.sourcedependency.port.name + " got new type: " + getField.apply(port.sourcedependency.port))
- typeInferred = true
- } else {
- println("Source port already has field.")
- }
- } else {
- println("Has no source dependency.")
- }
-
- if (port.targetdependency !== null) {
- println("Has a target dependency: " + port.targetdependency.port.name)
- if(getField.apply(port.targetdependency.port) === null){
- println("Dependency has no field yet.")
- //port.targetdependency.port.type = port.type
- setField.apply(port.targetdependency.port, getField.apply(port))
- println("Port " + port.targetdependency.port.name + " got new type: " + getField.apply(port.targetdependency.port))
- typeInferred = true
- } else {
- println("Target port already has field.")
- }
- } else {
- println("Has no target dependency.")
- }
- }
-
- return typeInferred
- }
-
- def pushPortType(Port port){
- var typeInferred = false
- println("Pushing type of port " + port.name + " to its bindings.")
-
- if(port.type === null){
- println("Has no type to be pushed.")
- } else {
- println("Pushing type: " + port.type)
- if(port.sourcedependency !== null){
- println("Has a source dependency: " + port.sourcedependency.port.name)
- if(port.sourcedependency.port.type === null){
- port.sourcedependency.port.type = port.type
- println("Port " + port.sourcedependency.port.name + " got new type: " + port.sourcedependency.port.type)
- typeInferred = true
- } else {
- println("Source port already has type.")
- }
- } else {
- println("Has no source dependency.")
- }
-
- if (port.targetdependency !== null) {
- println("Has a target dependency: " + port.targetdependency.port.name)
- if(port.targetdependency.port.type === null){
- println("Dependency has no type yet.")
- port.targetdependency.port.type = port.type
- println("Port " + port.targetdependency.port.name + " got new type: " + port.targetdependency.port.type)
- typeInferred = true
- } else {
- println("Target port already has type.")
- }
- } else {
- println("Has no target dependency, or type has already been inferred from source dependency.")
- }
- }
-
- return typeInferred
- }
-
- def getPortType(Port port){
- var typeInferred = false
-
- println("Computing type for port " + port.name)
- //println("Object: " + port)
-
- var String returnType = null
-
- if(port.type !== null){
- throw new Exception("Wrong way of using this function. It assumes type is not inferred yet.")
- } else {
- println("Has no type.")
-
- println("Attempting to infer type from units.")
- if (port.unity !== null){
- returnType = "Real"
- println("Got new type: " + returnType)
- typeInferred = true
- } else {
- println("Attempting to infer type from bindings.")
- if(port.sourcedependency !== null){
- println("Has a source dependency: " + port.sourcedependency.port.name)
- if(port.sourcedependency.port.type === null){
- println("Dependency has no type yet.")
- } else {
- returnType = port.sourcedependency.port.type
- println("Got new type: " + returnType)
- typeInferred = true
- }
- } else {
- println("Has no source dependency.")
- }
-
- if (port.targetdependency !== null && !typeInferred) {
- println("Has a target dependency: " + port.targetdependency.owner.name + "." + port.targetdependency.port.name)
- if(port.targetdependency.port.type === null){
- //println("Port object: " + port.targetdependency.port)
- println("Dependency has no type yet.")
- } else {
- returnType = port.targetdependency.port.type
- println("Got new type: " + port.type)
- typeInferred = true
- }
- } else {
- println("Has no target dependency, or type has already been inferred from source dependency.")
- }
- }
- }
-
- return returnType
- }
-
- def inferPortType(Port port){
- var typeInferred = false
- println("Computing type for port " + port.name)
- //println("Object: " + port)
-
- if(port.type !== null){
- throw new Exception("Wrong way of using this function. It assumes type is not inferred yet.")
- } else {
- println("Has no type.")
-
- println("Attempting to infer type from units.")
- if (port.unity !== null){
- port.type = "Real"
- println("Got new type: " + port.type)
- typeInferred = true
- } else {
- println("Attempting to infer type from bindings.")
- if(port.sourcedependency !== null){
- println("Has a source dependency: " + port.sourcedependency.port.name)
- if(port.sourcedependency.port.type === null){
- println("Dependency has no type yet.")
- } else {
- port.type = port.sourcedependency.port.type
- println("Got new type: " + port.type)
- typeInferred = true
- }
- } else {
- println("Has no source dependency.")
- }
-
- if (port.targetdependency !== null && !typeInferred) {
- println("Has a target dependency: " + port.targetdependency.owner.name + "." + port.targetdependency.port.name)
- if(port.targetdependency.port.type === null){
- //println("Port object: " + port.targetdependency.port)
- println("Dependency has no type yet.")
- } else {
- port.type = port.targetdependency.port.type
- println("Got new type: " + port.type)
- typeInferred = true
- }
- } else {
- println("Has no target dependency, or type has already been inferred from source dependency.")
- }
-
- }
- }
-
- return typeInferred
- }
-
- def addInParams(Adaptation adaptation) {
- println("Adding input parameters...")
-
- val PARAM_PREFIX = "INIT_"
-
- var inputPort_to_parameterDeclaration_Map = new HashMap<Port, SingleParamDeclaration>(adaptation.inports.size)
-
- for (inputPortDeclaration : adaptation.inports) {
- println("Generating parameter for port " + inputPortDeclaration.name)
- var paramname = PARAM_PREFIX + inputPortDeclaration.name.toUpperCase()
- var paramAlreadyDeclared = false
- for(paramDeclarations : adaptation.params){
- for(paramDeclaration : paramDeclarations.declarations){
- if(paramDeclaration.name == paramname){
- paramAlreadyDeclared = true
- }
- }
- }
- if (paramAlreadyDeclared){
- println("Parameter " + paramname + " already declared for port " + inputPortDeclaration.name)
- } else {
- println("Declaring new parameter " + paramname + " for port " + inputPortDeclaration.name)
- var factory = SemanticAdaptationFactory.eINSTANCE
- if (adaptation.params.size == 0){
- adaptation.params.add(factory.createParamDeclarations())
- }
- var paramDeclaration = factory.createSingleParamDeclaration()
-
- // TODO Continue here after solving problem with ports.
-
- //adaptation.params.head.declarations.add()
- }
- }
-
- println("Adding input parameters... DONE")
- }
-
- }
|