Skip to content
Snippets Groups Projects
Commit f8280c3b authored by Chrissi's avatar Chrissi
Browse files

remove errors for view creation from model join query

+ toString method change from join
+ ViewGeneration changes
parent 8a197bbc
No related branches found
No related tags found
No related merge requests found
Showing
with 128 additions and 89 deletions
natural join imdbdatabase.Film with eclipselibrary.VideoCassette as joins.JoinMovie {
natural join imdbdatabase.Film with elib.VideoCassette as joins.JoinMovie {
keep attributes imdbdatabase.Film.year
keep attributes eclipselibrary.AudioVisualItem.minutesLength
keep outgoing imdbdatabase.Film.votes as type jointarget.Vote {
keep attributes imdbdatabase.Vote.score
}
keep outgoing elib.VideoCassette.cast as type jointarget.Person {
keep attributes elib.Person.lastName
}
}
......@@ -3,14 +3,13 @@ package org.rosi_project.model_sync.generator.acr_model
import org.rosi_project.model_management.sum.join.RsumJoinType
class SJoinClass(_name: String,
_sPackage: String = "",
_isAbstract: Boolean = false,
_isInterface: Boolean = false,
_sPackage: String,
val base: SClass,
val other: SClass,
val joinType: RsumJoinType.Value,
val joinAttributes: Set[SStructuralFeature],
val innerAttributes: Set[SStructuralFeature]) extends SClass(_name, _sPackage, _isAbstract, _isInterface) {
val innerAttributes: Set[SStructuralFeature],
val joinObject: SClass) extends SClass(_name, _sPackage) {
override def getRootClassWithNameAndPackage(n: String, p: String): SClass = {
val bparent = base.getRootClassWithNameAndPackage(n, p)
......
package org.rosi_project.model_sync.generator.acr_model
class SViewClass(_name: String,
_sPackage: String) extends SClass(_name, _sPackage) {
private var joinObjects: Set[SJoinClass] = Set.empty
def getJoinObject(): Set[SJoinClass] = joinObjects
def addJoinObject(v: SJoinClass): Unit = {
require(v != null)
joinObjects += v
}
}
\ No newline at end of file
......@@ -7,20 +7,34 @@ import org.rosi_project.model_sync.generator.acr_model.types.GenericSequence
import org.rosi_project.model_sync.generator.sync.HelperFunctions
import org.rosi_project.model_sync.generator.PackageNames
/** The `Writer` generates the source code of a [[SClass]] and provides it as a `String`.
/**
* The `Writer` generates the source code of a [[SClass]] and provides it as a `String`.
*
* @author Rico Bergmann
*/
class SClassWriter(val modelClass: SClass, val isView: Boolean, val isObject: Boolean) {
class SClassWriter(val modelClass: SClass, val isObject: Boolean) {
private val isView: Boolean = modelClass.isInstanceOf[SViewClass]
private val pckg: String = if (modelClass.isDefaultPackage) "" else s"package ${modelClass.sPackage}"
private val imports: Seq[String] = modelClass.collectImports.toSeq
private val viewImports: String = {
if (isView) {
s"""
| import ${PredefRsumTypes.VIEWTYPE_INFO_STYPE.getPackage}.${PredefRsumTypes.VIEWTYPE_INFO_STYPE.getName}
| import ${PredefRsumTypes.JOIN_INFO_STYPE.getPackage}.${PredefRsumTypes.JOIN_INFO_STYPE.getName}
| ${modelClass.asInstanceOf[SViewClass].getJoinObject().map(j => s"import ${j.joinObject.getPackage}.${j.joinObject.getName}").mkString(" \n")}
""".stripMargin
} else {
""
}
}
private val clazzFixture: String = generateClassFixture
private val internalClazzFixture: String = generateInternalClazzFixture
private val companionObject: String = generateCompanionFixture
/** Provides a source code representation of the `modelClass` as a `String`.
/**
* Provides a source code representation of the `modelClass` as a `String`.
*
* When writing it to a file it will be able to be compiled with the `scalac` (assuming the
* ''classpath'' is set-up correctly).
......@@ -29,8 +43,7 @@ class SClassWriter(val modelClass: SClass, val isView: Boolean, val isObject: Bo
s"""$pckg
|
|${imports.map(i => s"import $i").mkString("\n")}
|${if(isView) s"import ${PredefRsumTypes.VIEWTYPE_INFO_STYPE.getPackage}.${PredefRsumTypes.VIEWTYPE_INFO_STYPE.getName}" else ""}
|${if(isView) s"import ${PredefRsumTypes.JOIN_INFO_STYPE.getPackage}.${PredefRsumTypes.JOIN_INFO_STYPE.getName}" else ""}
|${viewImports}
|
|${clazzFixture} {
|
......@@ -48,6 +61,9 @@ class SClassWriter(val modelClass: SClass, val isView: Boolean, val isObject: Bo
""".stripMargin
}
/**
* Provides a source code representation of an internal class used as role in SCROLL.
*/
def internalStringify: String = {
s"""
|$clazzFixture {
......@@ -60,7 +76,8 @@ class SClassWriter(val modelClass: SClass, val isView: Boolean, val isObject: Bo
""".stripMargin
}
/** Writes a method as source code.
/**
* Writes a method as source code.
*
* @param m the method to write
* @return the `String` representation of `m`
......@@ -72,28 +89,35 @@ class SClassWriter(val modelClass: SClass, val isView: Boolean, val isObject: Bo
""".stripMargin
}
/** Writes the "''companion fixture''" for views.
/**
* Writes the "''companion fixture''" for views.
*/
protected def generateCompanionFixture: String = {
if (isView) {
val viewClass = modelClass.asInstanceOf[SViewClass]
s"""object ${modelClass.getName} extends ${PredefRsumTypes.VIEWTYPE_INFO_STYPE.getName} {
|
| override def getViewName(): String = "${modelClass.getName}"
|
| def getJoinInfos(): Set[${PredefRsumTypes.JOIN_INFO_STYPE.getName}] = Set.empty
| def getJoinInfos(): Set[${PredefRsumTypes.JOIN_INFO_STYPE.getName}] = ${if (viewClass.getJoinObject.isEmpty) "Set.empty" else s"Set(${viewClass.getJoinObject.map(_.joinObject.getName).mkString(", ")})"}
|
| protected def getNewInstance(): ${PredefRsumTypes.IVIEW_COMPARTMENT_STYPE.getName} = new ${modelClass.getName}()
|
| def getNewView(): ${modelClass.getName} = getNewViewTypeInstance().asInstanceOf[${modelClass.getName}]
|}
""".stripMargin
} else {
""
}
}
/** Writes the internal classes.
/**
* Writes the internal classes.
*/
protected def generateInternalClazzFixture: String = {
var result = "";
modelClass.getInternalClasses.foreach(intCls => {
var sw: SClassWriter = new SClassWriter(intCls, false, false)
var sw: SClassWriter = new SClassWriter(intCls, false)
var s = sw.internalStringify;
result = result + s + "\n";
})
......@@ -125,7 +149,8 @@ class SClassWriter(val modelClass: SClass, val isView: Boolean, val isObject: Bo
}).mkString(" \n")
}
/** Writes the "''class fixture''", i.e. the `class` identifier followed by the constructor and
/**
* Writes the "''class fixture''", i.e. the `class` identifier followed by the constructor and
* optionally a parent class. The parent constructor will be called correctly.
*/
protected def generateClassFixture: String = {
......@@ -172,10 +197,9 @@ class SClassWriter(val modelClass: SClass, val isView: Boolean, val isObject: Bo
if (modelClass.isRootClass) {
if (allInterfaces != "") {
if (isView) {
baseFixture = s"$baseFixture private extends $allInterfaces"
} else {
baseFixture = s"$baseFixture extends $allInterfaces"
baseFixture = s"$baseFixture private"
}
baseFixture = s"$baseFixture extends $allInterfaces"
}
} else {
baseFixture = s"$baseFixture extends ${parent.getName}"
......@@ -186,17 +210,11 @@ class SClassWriter(val modelClass: SClass, val isView: Boolean, val isObject: Bo
baseFixture = s"$baseFixture with $allInterfaces"
}
}
/*if (isView) {
return s"class CompleteView private extends IViewCompartment"
}*/
//println("baseFixture: " + baseFixture)
//println("**************************************************")
baseFixture
}
/** Generates a ''class parameter's'' name that will be used to initialize a field of the super class.
/**
* Generates a ''class parameter's'' name that will be used to initialize a field of the super class.
*
* This is purely to prevent naming conflicts and shadowing when trying to access a parent's field
* from the subclass.
......@@ -221,7 +239,8 @@ class SClassWriter(val modelClass: SClass, val isView: Boolean, val isObject: Bo
parName
}
/** Returns the name of the interface.
/**
* Returns the name of the interface.
*/
private def namesOfInterfaces(interface: STypedElement): String = {
interface.getName
......
......@@ -59,12 +59,12 @@ class SModelFSWriter(
override def visit(sModel: SModel): Unit = {
sModel.getModelEnums.foreach(writeEnum(_))
sModel.getModelClasses.foreach(writeClass(_, false, false))
sModel.getJoinClasses.foreach(writeClass(_, false, false))
sModel.getJoinObjects.foreach(writeClass(_, false, true)) //TODO
sModel.getRelationalCompartments.foreach(writeClass(_, false, false))
sModel.getViewCompartments.foreach(writeClass(_, true, false))
sModel.getProviderClasses.foreach(writeClass(_, false, false))
sModel.getModelClasses.foreach(writeClass(_, false))
sModel.getJoinClasses.foreach(writeClass(_, false))
sModel.getJoinObjects.foreach(writeClass(_, true)) //TODO
sModel.getRelationalCompartments.foreach(writeClass(_, false))
sModel.getViewCompartments.foreach(writeClass(_, false))
sModel.getProviderClasses.foreach(writeClass(_, false))
//println(s"... Wrote files (sources) $sFilesToCompile")
println("... Starting compilation")
......@@ -102,11 +102,11 @@ class SModelFSWriter(
}
}
private def writeClass(sClass: SClass, isView: Boolean, isObject: Boolean): Unit = {
private def writeClass(sClass: SClass, isObject: Boolean): Unit = {
try {
println(s"Writing class $sClass")
val classNameWithPath = workingDir.toAbsolute.toString() + File.separator + pckg2Path(sClass.getPackage) + File.separator + s"${sClass.getName}.scala"
val writer = new SClassWriter(sClass, isView, isObject)
val writer = new SClassWriter(sClass, isObject)
val classFile = File(classNameWithPath)
......
......@@ -71,10 +71,10 @@ class JoinGeneratingVisitor(joinExpression: ModelJoinExpression) extends SModelV
println("B: " + otherAttsBase)
println("O: " + otherAttsOther)
val joinObject = new SClass(j.getTarget().getResourceName + "Object", j.getTarget().getResourcePath)
val joinClass = new SJoinClass(j.getTarget().getResourceName, j.getTarget().getResourcePath,
base = baseClass, other = otherClass, joinType = rsumJoinType,
joinAttributes = joinAtts, innerAttributes = joinAtts ++ otherAttsBase ++ otherAttsOther)
val joinObject = new SClass(j.getTarget().getResourceName + "Object", j.getTarget().getResourcePath)
joinAttributes = joinAtts, innerAttributes = joinAtts ++ otherAttsBase ++ otherAttsOther, joinObject)
//add parents
joinClass.addParent(PredefRsumTypes.IJOIN_COMPARTMENT_STYPE)
......
......@@ -10,7 +10,7 @@ import org.rosi_project.model_sync.generator.PackageNames
class ModelJoinViewGeneratingVisitor(joinExpression: ModelJoinExpression) extends SModelVisitor {
var viewCompartment: SClass = null
var viewCompartment: SViewClass = null
var refClasses: Seq[SClass] = Seq.empty
var refRelationalClasses: Seq[SClass] = Seq.empty
var newInternalRoles: Seq[SInnerViewNaturalClass] = Seq.empty
......@@ -25,12 +25,13 @@ class ModelJoinViewGeneratingVisitor(joinExpression: ModelJoinExpression) extend
newInternalRoles = Seq.empty
newInternalRelationalRoles = Seq.empty
allImportClasses = Set.empty
viewCompartment = new SClass(joinExpression.getName() + counter, PackageNames.viewPkgName)
viewCompartment = new SViewClass(joinExpression.getName() + counter, PackageNames.viewPkgName)
counter += 1
viewCompartment.addParent(PredefRsumTypes.IVIEW_COMPARTMENT_STYPE)
val joinType = STypeRegistry.query(j.getTarget.getResourceName, j.getTarget.getResourcePath)
if (!joinType.isEmpty) {
viewCompartment.addJoinObject(joinType.get.asInstanceOf[SJoinClass])
createClasses(asScalaBuffer(j.getKeepsList).toSet, joinType.get.asInstanceOf[SClass], sModel)
}
......
......@@ -22,7 +22,7 @@ object ToStringMethods {
name = "toString",
result = PredefTypes.String,
params = Seq.empty,
implementation = Seq(SMethodStatement(s""" "JOIN ${s}: " + base + " " + other """)),
implementation = Seq(SMethodStatement(s""" "JOIN ${s}: " + baseObj + " " + otherObj """)),
overrides = true
)
}
......
......@@ -8,7 +8,7 @@ class ViewGeneratingVisitor extends SModelVisitor {
override def visit(sModel: SModel): Unit = {
val viewCompartment = new SClass(sModel.getName.capitalize + PackageNames.viewPostName, PackageNames.viewPkgName)
val viewCompartment = new SViewClass(sModel.getName.capitalize + PackageNames.viewPostName, PackageNames.viewPkgName)
var newInternalRoles: Seq[SInnerViewNaturalClass] = Seq.empty
var newInternalRelationalRoles: Seq[SInnerViewRelationalClass] = Seq.empty
viewCompartment.addParent(PredefRsumTypes.IVIEW_COMPARTMENT_STYPE)
......
......@@ -61,7 +61,7 @@ object ApplicationTest extends App {
}
def runCombinedTest(cre: Creation.Value): Unit = {
var config: GeneratorConfig = GeneratorConfig(Seq("assets/models/IMDBDatabase.ecore", "assets/models/ModelJoinLibrary.ecore"), false, new File("assets/models"), modelJoin = "assets/model_join/complex.modeljoin");
var config: GeneratorConfig = GeneratorConfig(Seq("assets/models/IMDBDatabase.ecore", "assets/models/ModelJoinLibrary.ecore"), false, new File("assets/models"), modelJoin = "assets/model_join/simple.modeljoin");
config.setCreate(cre)
new Generator(config).run()
}
......
......@@ -23,7 +23,7 @@ object SClassWriterTest/* extends App */{
val syncNotificationVisitor = new SyncEnhancingVisitor
modelClass.accept(syncNotificationVisitor)
val writer = new SClassWriter(modelClass, false, false)
val writer = new SClassWriter(modelClass, false)
println(writer.stringify)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment