Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1815 from umple/refactorInnotaiveToComp
A generator to refactor annotative mixsets into compositional mixsets
- Loading branch information
Showing
7 changed files
with
215 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
cruise.umple/src/generators/Generator_CodeAnnotaiveToComposition.ump
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
|
||
Copyright: All contributers to the Umple Project | ||
|
||
This file is made available subject to the open source license found at: | ||
http://umple.org/license | ||
|
||
*/ | ||
|
||
namespace cruise.umple.compiler; | ||
|
||
class UmpleAnnotaiveToCompositionGenerator | ||
{ | ||
isA CodeGeneratorWithSubptions; | ||
depend java.io.*; | ||
depend java.util.*; | ||
depend cruise.umple.util.*; | ||
depend cruise.umple.compiler.exceptions.*; | ||
depend java.util.stream.Collectors; | ||
UmpleModel model = null; | ||
String output = ""; | ||
|
||
fileExtension= ".ump"; | ||
|
||
// CENTRAL GENERATE METHOD | ||
|
||
public void generate(){ | ||
StringBuilder code = new StringBuilder(); | ||
List<Mixset> mixsetList = model.getMixsetOrFiles().stream().filter(mixset -> mixset.getIsMixset()).map(obj -> (Mixset)obj).collect(Collectors.toList()); | ||
code.append("/* \n"); | ||
code.append("Annotaive fragments are refactored into composition fragments. \n"); | ||
code.append("Total mixsts definitions: "+mixsetList.size()); | ||
code.append("\n*/ \n"); | ||
code.append("//*** \n"); | ||
code.append("//Coarse-grained variability : \n"); | ||
|
||
for (Mixset aMixset: mixsetList) | ||
{ | ||
code.append("\n"); | ||
code.append("mixset "+aMixset.getMixsetName() + " { ") ; | ||
for (MixsetFragment mFragment : aMixset.getMixsetFragments()) | ||
{ | ||
code.append("\n// Fragment source file: "+mFragment.getOriginalUmpFile().getFileName()); | ||
code.append("\n// Line : "+mFragment.getOriginalUmpLine()); | ||
code.append("\n"+mFragment.getBody()); | ||
code.append("\n"); | ||
} | ||
code.append("\n}\n") ; | ||
} | ||
// | ||
code.append("//*** \n"); | ||
code.append("//Fine-grained variability : \n"); | ||
// | ||
int labelCount= 1; | ||
List<UmpleClass> umpleClasses = model.getUmpleClasses().stream().filter(classM -> classM.getMethods().size() > 0).collect(Collectors.toList()); | ||
for (UmpleClass aClass : umpleClasses) | ||
{ | ||
String className = aClass.getName(); | ||
String classInlineMixset =""; | ||
try | ||
{ | ||
List<Method> methodsWithInlineMixset = aClass.getMethods().stream().filter(classM -> classM.getMethodBody() != null).collect(Collectors.toList()); | ||
for (Method method : methodsWithInlineMixset) | ||
{ | ||
List<MixsetInMethod> mixsetInMethodList = method.getMethodBody().getMixsetInMethods(); | ||
for (MixsetInMethod mixsetInMethod : mixsetInMethodList) | ||
{ | ||
// prepare parameter types | ||
String injectionParameters = ""; | ||
List<String> paramList = method.getMethodParameters().stream().map(aParam -> aParam.getType()).collect(Collectors.toList()); | ||
String paramListCommaSeparated = String.join(",", paramList); | ||
if(paramList.size() > 0) | ||
injectionParameters = "("+paramListCommaSeparated+ ")"; | ||
// define a labeled aspect injection. | ||
String mixsetBody = mixsetInMethod.getMixsetFragment(); | ||
mixsetBody = mixsetBody.substring(mixsetBody.indexOf("{")); | ||
classInlineMixset+="mixset " + mixsetInMethod.getMixsetName() + " { \n"; | ||
classInlineMixset+=" class " + className + " { \n"; // start class definition | ||
classInlineMixset+=" before "+ "Lable_"+mixsetInMethod.getMixsetName()+"_"+labelCount+":"+ method.getName()+ injectionParameters+"\n "+ mixsetBody; | ||
classInlineMixset+=" } \n"; | ||
classInlineMixset+="} \n"; // end class definition. | ||
labelCount ++; | ||
// two empty line after each aspect injection. | ||
classInlineMixset+="\n\n"; | ||
} | ||
} | ||
if(classInlineMixset.trim().length() > 1) | ||
{ | ||
code.append(classInlineMixset); | ||
} | ||
} | ||
catch(NullPointerException ex) | ||
{ | ||
continue; | ||
} | ||
} | ||
code.append("\n"); | ||
model.setCode(code.toString()); | ||
writeModel(); | ||
} | ||
|
||
|
||
private String spacer(int num) | ||
{ | ||
StringBuilder space = new StringBuilder(); | ||
for (int i = 0; i < num; i++) | ||
{ | ||
space.append(" "); | ||
} | ||
return space.toString(); | ||
} | ||
|
||
private void writeModel() | ||
{ | ||
try | ||
{ | ||
String path = model.getUmpleFile().getPath(); | ||
File file = new File(path); | ||
file.mkdirs(); | ||
String modelFilename = path + File.separator + model.getUmpleFile().getSimpleFileName()+"_refactoredToComposition" + this.fileExtension ; | ||
BufferedWriter bw = new BufferedWriter(new FileWriter(modelFilename)); | ||
bw.write(model.getCode()); | ||
bw.flush(); | ||
bw.close(); | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new UmpleCompilerException("There was a problem with generating UmpleSelf code." + e, e); | ||
} | ||
} | ||
|
||
|
||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
cruise.umple/test/cruise/umple/compiler/mixset/parseRefactorInlineMixsetIntoCompMixset.ump
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Bank { | ||
1 -- * Account; | ||
mixset Multibranch | ||
{ | ||
1 -- 1..* Branch; | ||
} | ||
|
||
/* | ||
void withdraw(int amount){ | ||
if (amount > 0) | ||
//some code | ||
mixset Multibranch | ||
{ | ||
amount = amount - someFees; | ||
} | ||
} | ||
*/ | ||
} | ||
|
||
mixset Multibranch { | ||
class Branch { | ||
Integer id; String address; | ||
} | ||
} | ||
|
||
class Account { | ||
owner; Integer number; Integer balance; | ||
mixset Multibranch { * -- 1 Branch;} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters