Commit b66a6d92 authored by Lars Hermerschmidt's avatar Lars Hermerschmidt
Browse files

simplify generator

parent ea8cbcc2
package demo;
import java.util.Map;
import mc.codegen.Constants;
import mc.codegen.TemplateCalculator;
import mc.codegen.TemplateOperator;
import mc.umlp.cd._ast.ASTCDAssociation;
import mc.umlp.cd._ast.ASTCDClass;
import mc.umlp.cd._ast.ASTCDType;
public class Association extends TemplateCalculator<ASTCDClass> {
private ASTCDType parent;
// handles association and aggregation in the same way
@Override
public boolean calc(ASTCDClass a, TemplateOperator op) {
Map<ASTCDClass, ASTCDAssociation> assotiations = (Map<ASTCDClass, ASTCDAssociation>)op.getValue("assotiations");
return assotiations.containsKey(a);
}
}
package demo;
import java.util.Map;
import mc.codegen.TemplateCalculator;
import mc.codegen.TemplateOperator;
import mc.umlp.cd._ast.ASTCDAssociation;
import mc.umlp.cd._ast.ASTCDClass;
import mc.umlp.cd._ast.ASTCDType;
import mc.umlp.common._ast.ASTCardinality;
public class N2NAssociation extends TemplateCalculator<ASTCDClass>{
@Override
public boolean calc(ASTCDClass ast, TemplateOperator op) {
for(ASTCDAssociation ass : ast.getCDAssociations()) {
if (ass == null) continue;
boolean leftStar = false;
boolean rightStar = false;
ASTCardinality card = ass.getLeftCardinality();
if (card != null)
leftStar = card.isMany() || card.getUpperBound() > 1;
card = ass.getRightCardinality();
if (card != null)
rightStar = card.isMany() || card.getUpperBound() > 1;
if (leftStar && rightStar) {
// set the name of the first match
ASTCDType other;
if(ast.equals(ass.getLeftReference())) {
other = ass.getRightReference();
} else {
other = ass.getLeftReference();
}
op.setValue("otherClass", other.getName());
return true;
}
}
return false;
}
}
package demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import mc.codegen.TemplateCalculator;
import mc.codegen.TemplateOperator;
import mc.umlp.cd._ast.ASTCDAssociation;
import mc.umlp.cd._ast.ASTCDAssociationList;
import mc.umlp.cd._ast.ASTCDClass;
import mc.umlp.cd._ast.ASTCDDefinition;
public class TransformAssociations extends TemplateCalculator<ASTCDDefinition> {
@Override
public boolean calc(ASTCDDefinition ast, TemplateOperator op) {
Map<ASTCDClass, ASTCDAssociation> assotiations = new HashMap<ASTCDClass, ASTCDAssociation>();
op.setValue("assotiations", assotiations);
try {
for (ASTCDClass c : ast.getCDClasses()) {
for(ASTCDAssociation ass : c.getCDAssociations()) {
// get all Associations out of the ast
ASTCDClass other;
// find the assotiated class
if ( c.equals(ass.getLeftReference()) ) {
other = (ASTCDClass)ass.getRightReference();
} else if ( c.equals(ass.getRightReference()) ) {
other = (ASTCDClass)ass.getLeftReference();
} else {
throw new Exception("Malformed Association: Can't find class to associate the association to");
}
// remember the assotiated class
assotiations.put(other, ass);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}
<#if op.callCalculator("demo.Constants")></#if>
<#if op.callCalculator("demo.TransformAssociations")></#if>
<#list ast.getCDClasses() as class>
create table ${class.getName()} (
<#if op.callCalculator("demo.Association", class) >
autoID INT NOT NULL PRIMARY KEY,
</#if>
<#list class.getCDAttributes() as attr>
${attr.printName()} ${op.getValue("constants").getType(attr.printType())}<#if attr_has_next>,</#if>
</#list>
);
<#if op.callCalculator("demo.N2NAssociation", class)>
create table ${class.getName()}_${op.getValue("otherClass")} (
${class.getName()} INT NOT NULL,
${op.getValue("otherClass")} INT NOT NULL,
PRIMARY KEY (${class.getName()}, ${op.getValue("otherClass")})
);
</#if>
</#list>
\ No newline at end of file
<#list ast.getCDClasses() as class>
drop table ${class.getName()};
<#if op.callCalculator("demo.N2NAssociation", class)>
drop table ${class.getName()}_${op.getValue("otherClass")};
</#if>
</#list>
\ No newline at end of file
<#-- Disable Java Comments-->
${op.setTracing(false)}
${op.callTemplates(
"cd.SQLCreate",
ast.printPackage() +".create",
"sql",
ast)}
${op.callTemplates(
"cd.SQLDelete",
ast.printPackage() +".delete",
"sql",
ast)}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment