From 1010fa5c0d688efdc4990d7d7b576105101b487f Mon Sep 17 00:00:00 2001
From: Chrissi <christopher@hbsc-werner.de>
Date: Tue, 14 May 2019 16:26:41 +0200
Subject: [PATCH] error removing and shrinking example finish

---
 assets/model_join/shrinking.modeljoin         |  14 +++
 assets/models/ShrinkingModel.ecore            |   2 +-
 .../sync/ModelJoinViewGeneratingVisitor.scala | 114 +++++++++---------
 .../generator/test/ApplicationTest.scala      |  10 +-
 4 files changed, 81 insertions(+), 59 deletions(-)
 create mode 100644 assets/model_join/shrinking.modeljoin

diff --git a/assets/model_join/shrinking.modeljoin b/assets/model_join/shrinking.modeljoin
new file mode 100644
index 0000000..427c5b2
--- /dev/null
+++ b/assets/model_join/shrinking.modeljoin
@@ -0,0 +1,14 @@
+
+natural join shrinking.Class with shrinking.Class as shrinking.Class {
+  keep supertype shrinking.Classifier as type shrinking.Classifier {
+    keep attributes shrinking.Classifier.name
+  }
+  keep outgoing shrinking.StructuredClassifier.part as type shrinking.Property {
+    keep attributes shrinking.ConnectableElement.name
+  }
+  keep outgoing shrinking.StructuredClassifier.role as type shrinking.Property {
+    keep attributes shrinking.ConnectableElement.name
+  }
+  keep outgoing shrinking.StructuredClassifier.ownedAttribute as type shrinking.Property {
+  }
+}
diff --git a/assets/models/ShrinkingModel.ecore b/assets/models/ShrinkingModel.ecore
index b9ca19d..d44b9ff 100644
--- a/assets/models/ShrinkingModel.ecore
+++ b/assets/models/ShrinkingModel.ecore
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="ShrinkingExample" nsURI="http://www.example.org/shrinking" nsPrefix="shr">
+    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="shrinking" nsURI="http://www.example.org/shrinking" nsPrefix="shr">
   <eClassifiers xsi:type="ecore:EClass" name="Classifier" abstract="true">
     <eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
   </eClassifiers>
diff --git a/src/main/scala/org/rosi_project/model_sync/generator/sync/ModelJoinViewGeneratingVisitor.scala b/src/main/scala/org/rosi_project/model_sync/generator/sync/ModelJoinViewGeneratingVisitor.scala
index 3ec8fa7..e45b64c 100644
--- a/src/main/scala/org/rosi_project/model_sync/generator/sync/ModelJoinViewGeneratingVisitor.scala
+++ b/src/main/scala/org/rosi_project/model_sync/generator/sync/ModelJoinViewGeneratingVisitor.scala
@@ -14,6 +14,7 @@ class ModelJoinViewGeneratingVisitor(joinExpression: ModelJoinExpression) extend
   var refRelationalClasses: Seq[SClass] = Seq.empty
   var newInternalRoles: Seq[SInnerViewNaturalClass] = Seq.empty
   var newInternalRelationalRoles: Seq[SInnerViewRelationalClass] = Seq.empty
+  var allImportClasses: Set[STypedElement] = Set.empty
 
   override def visit(sModel: SModel): Unit = {
     println("#################################################################")
@@ -23,6 +24,7 @@ class ModelJoinViewGeneratingVisitor(joinExpression: ModelJoinExpression) extend
       refRelationalClasses = Seq.empty
       newInternalRoles = Seq.empty
       newInternalRelationalRoles = Seq.empty
+      allImportClasses = Set.empty
       viewCompartment = new SClass(joinExpression.getName() + counter, "view")
       counter += 1
       viewCompartment.addParent(PredefRsumTypes.IVIEW_COMPARTMENT_STYPE)
@@ -56,44 +58,6 @@ class ModelJoinViewGeneratingVisitor(joinExpression: ModelJoinExpression) extend
         })
       })
 
-      //Iterate over all Relational compartments and create relational internal role classes
-      /*sModel.getRelationalCompartments.foreach(r => {
-        var sourceAtt: SAttribute = null
-        var targetAtt: SAttribute = null
-        //set as attributes the other classes
-        var sourceRole: SInnerViewNaturalClass = null
-        var targetRole: SInnerViewNaturalClass = null
-        r.getAttributes.foreach(attr => {
-          newInternalRoles.foreach(inte => {
-            if (inte.getName == attr.getTypeElement.getName + "Role") {
-              if (attr.getName == "sInstance") {
-                sourceRole = inte
-                sourceAtt = new SAttribute("source", inte)
-                sourceAtt.isFinal = true
-                sourceAtt.setVisibility(MethodVisibility.privateVis)
-              } else {
-                targetRole = inte
-                targetAtt = new SAttribute("target", inte)
-                targetAtt.isFinal = true
-                targetAtt.setVisibility(MethodVisibility.privateVis)
-              }
-            }
-          })
-        })
-        val internalClass = new SInnerViewRelationalClass(r.getName + "Role", _externalClass = viewCompartment,
-          sumSource = r.asInstanceOf[SRelationalCompartmentClass], viewSource = sourceRole, viewTarget = targetRole)
-        internalClass.setAttributes(sourceAtt ++ targetAtt)
-        internalClass.addMethod(ViewMethods.getIsRelationalMethod(true))
-        internalClass.addMethod(ViewMethods.getCreationObjectMethod(internalClass))
-        internalClass.addMethod(ViewMethods.getDeleteElementRelationMethod(internalClass))
-        internalClass.addMethod(ToStringMethods.onlyStringToStringMethod("VRR: " + internalClass.getName))
-        ViewMethods.getSourceAndTargetGetter(internalClass).foreach(internalClass.addMethod(_))
-        internalClass.addParent(PredefRsumTypes.AVIEW_ROLE_STYPE)
-        createAllReferenceLinks(internalClass)
-        newInternalRelationalRoles = newInternalRelationalRoles :+ internalClass
-        viewCompartment.addInternalClass(internalClass)
-      })*/
-
       //add methods that must be override from the view trait
       viewCompartment.addMethod(ViewMethods.getIsViewableMethod((refClasses ++ refRelationalClasses).toSet))
       viewCompartment.addMethod(ViewMethods.getViewNameMethod(viewCompartment))
@@ -103,6 +67,9 @@ class ModelJoinViewGeneratingVisitor(joinExpression: ModelJoinExpression) extend
       //Create methods to create elements from the view
       ViewMethods.getCreateNaturalRoleMethods(newInternalRoles.toSet).foreach(viewCompartment.addMethod(_))
 
+      println("Alllllllllllll: " + allImportClasses)
+      viewCompartment.augmentConstructor(new SMethodStatement("", allImportClasses))
+
       //add the view to the list for generating views
       sModel.addViewCompartment(viewCompartment)
     })
@@ -110,16 +77,30 @@ class ModelJoinViewGeneratingVisitor(joinExpression: ModelJoinExpression) extend
   }
 
   def createClasses(keeps: Set[KeepExpression], cls: SClass, sModel: SModel): SInnerViewNaturalClass = {
-    refClasses = refClasses :+ cls
-    val internalClass = new SInnerViewNaturalClass(cls.getName + "Role",
-      _isAbstract = cls.isAbstract,
-      _isInterface = cls.isInterface,
-      _externalClass = viewCompartment,
-      sumSource = cls)
+    var internalClass: SInnerViewNaturalClass = null
+    var newAtts: Seq[SAttribute] = Seq.empty
+    if (refClasses.contains(cls)) {
+      newInternalRoles.foreach(nir => {
+        if (nir.sumSource == cls) {
+          internalClass = nir
+          newAtts = nir.getAttributes()
+        }
+      })
+    } else {
+      refClasses = refClasses :+ cls
+      internalClass = new SInnerViewNaturalClass(cls.getName + "Role",
+        _isAbstract = cls.isAbstract,
+        _isInterface = cls.isInterface,
+        _externalClass = viewCompartment,
+        sumSource = cls)
+      newInternalRoles = newInternalRoles :+ internalClass
+      allImportClasses = allImportClasses + cls
+      viewCompartment.addInternalClass(internalClass)
+    }
+
     //without keep lists
     //KeepAggregateExpression KeepAttributesExpression KeepCalculatedExpression
     //sets the attributes from the source class
-    var newAtts: Set[SAttribute] = Set.empty
     var features: Set[SStructuralFeature] = Set.empty
     if (cls.isInstanceOf[SJoinClass]) {
       val jCls = cls.asInstanceOf[SJoinClass]
@@ -130,7 +111,7 @@ class ModelJoinViewGeneratingVisitor(joinExpression: ModelJoinExpression) extend
           var newAtt = SAttribute(a.getName, a.getTypeElement)
           newAtt.setVisibility(MethodVisibility.protectedVis)
           newAtt.isFinal = true
-          newAtts += newAtt
+          newAtts = newAtts :+ newAtt
         })
       }
     } else {
@@ -143,17 +124,24 @@ class ModelJoinViewGeneratingVisitor(joinExpression: ModelJoinExpression) extend
       println("#### " + keepAtt)
       keepAtt.getAttributes.forEach(attExp => {
         println("N: " + attExp.getAttributeName + " P: " + attExp.getContainingClass.getPackage + " CN: " + attExp.getContainingClass.getClassName)
-        features.foreach(a => {
-          //Proof if attribute must be in then add it to view class   
-          if (a.getName == attExp.getAttributeName) {
+        features.filter(_.getName == attExp.getAttributeName).foreach(a => {
+          var isIn = false
+          internalClass.getAttributes().foreach(ain => {
+            if (ain.getName == a.getName && ain.getTypeElement.getName == a.getTypeElement.getName) {
+              isIn = true
+            }
+          })
+          if (!isIn) {
+            //Proof if attribute must be in then add it to view class   
             var newAtt = SAttribute(a.getName, a.getTypeElement)
             newAtt.setVisibility(MethodVisibility.protectedVis)
             newAtt.isFinal = true
-            newAtts += newAtt
+            newAtts = newAtts :+ newAtt
           }
         })
       })
     })
+    internalClass.setAttributes(newAtts)   
     println("+++ KeepAggregateExpression")
     keeps.filter(_.isInstanceOf[KeepAggregateExpression]).foreach(ja => {
       //TODO: later
@@ -200,10 +188,26 @@ class ModelJoinViewGeneratingVisitor(joinExpression: ModelJoinExpression) extend
               var sourceAtt: SAttribute = null
               var targetAtt: SAttribute = null
               if (rc.sClass.getName == keepAtt.getAttribute.getContainingClass.getClassName) {
+                //current is source
                 sourceRole = internalClass
-                targetRole = createClasses(asScalaBuffer(keepAtt.getKeeps).toSet, rc.tClass.asInstanceOf[SClass], sModel)
+                var tClass = rc.tClass.asInstanceOf[SClass]
+                sModel.getModelClasses.filter(m => m.getName == keepAtt.getTarget.getClassName && m.getPackage == keepAtt.getTarget.getPackage).foreach(cs => {
+                  val parent = cs.getRootClassWithNameAndPackage(rc.tClass.getName, rc.tClass.getPackage)
+                  if (parent != null) {
+                    tClass = cs
+                  }
+                })
+                targetRole = createClasses(asScalaBuffer(keepAtt.getKeeps).toSet, tClass, sModel)
               } else {
-                sourceRole = createClasses(asScalaBuffer(keepAtt.getKeeps).toSet, rc.sClass.asInstanceOf[SClass], sModel)
+                //current is target
+                var sClass = rc.sClass.asInstanceOf[SClass]
+                sModel.getModelClasses.filter(m => m.getName == keepAtt.getTarget.getClassName && m.getPackage == keepAtt.getTarget.getPackage).foreach(cs => {
+                  val parent = cs.getRootClassWithNameAndPackage(rc.sClass.getName, rc.sClass.getPackage)
+                  if (parent != null) {
+                    sClass = cs
+                  }
+                })
+                sourceRole = createClasses(asScalaBuffer(keepAtt.getKeeps).toSet, sClass, sModel)
                 targetRole = internalClass
               }
               sourceAtt = new SAttribute("source", sourceRole)
@@ -226,16 +230,14 @@ class ModelJoinViewGeneratingVisitor(joinExpression: ModelJoinExpression) extend
               internalRelClass.addParent(PredefRsumTypes.AVIEW_ROLE_STYPE)
               createAllReferenceLinks(internalRelClass)
               newInternalRelationalRoles = newInternalRelationalRoles :+ internalRelClass
+              allImportClasses = allImportClasses + sourceRole.sumSource + targetRole.sumSource + rc.tClass + rc.sClass
               viewCompartment.addInternalClass(internalRelClass)
             }
           }
         }
       })
     })
-    println("#######################" + internalClass + " Ps: " + internalClass.getAllParents())
-    internalClass.setAttributes(newAtts.toSeq)
-    newInternalRoles = newInternalRoles :+ internalClass
-    viewCompartment.addInternalClass(internalClass)
+    println("#######################" + internalClass + " Ps: " + internalClass.getAllParents()) 
     internalClass
   }
 
diff --git a/src/main/scala/org/rosi_project/model_sync/generator/test/ApplicationTest.scala b/src/main/scala/org/rosi_project/model_sync/generator/test/ApplicationTest.scala
index 1cffdce..de3aeda 100644
--- a/src/main/scala/org/rosi_project/model_sync/generator/test/ApplicationTest.scala
+++ b/src/main/scala/org/rosi_project/model_sync/generator/test/ApplicationTest.scala
@@ -8,8 +8,8 @@ import org.rosi_project.model_sync.generator.Creation
 object ApplicationTest extends App {
       
   runCombinedTest(Creation.rsum)
-  //runTestAML(Creation.rsum)
-  //runTestLibrary(Creation.rsum)
+  //runShrinkingModel(Creation.rsum)
+
   
   //runAllTests(Creation.rsum)
   //runAllTests(Creation.rsync)
@@ -36,6 +36,12 @@ object ApplicationTest extends App {
     println("#####################################################################################")
   }
   
+  def runShrinkingModel(cre: Creation.Value): Unit = {
+    var config: GeneratorConfig = GeneratorConfig(Seq("assets/models/ShrinkingModel.ecore"), false, new File("assets/models"), modelJoin = "assets/model_join/shrinking.modeljoin");
+    config.setCreate(cre)
+    new Generator(config).run()
+  }
+  
   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");
     config.setCreate(cre)
-- 
GitLab