-- @atlcompiler emftvm -- @nsURI UML2=http://www.eclipse.org/uml2/3.0.0/UML -- $Id$ query UML2ToJava = UML2!"uml::Classifier".allInstances()->iterate(e; acc : Boolean = true | if e.ignore() then acc else acc and e.toFileString().writeTo(e.pathName()) endif).debug('UML2ToJava ok'); uses "lib::UML2"; -- ====================================================================== -- plumbing helpers begin -- ====================================================================== helper def : path : String = 'path'.parameterValue(); helper context UML2!"uml::NamedElement" def : implQualifiedName : String = self.qualifiedName('.'); helper context String def : parameterValue() : String = XML!"XML::Element".allInstancesFrom('parameters') ->select(e|e.name = 'param') ->any(e|e.getAttrVal('name') = self).getAttrVal('value'); helper context XML!"XML::Element" def : getAttrVal(name : String) : String = self.children->select(n|n.oclIsKindOf(XML!"XML::Attribute")) ->any(a|a.name = name).value; helper context UML2!"uml::Classifier" def : ignore() : Boolean = true; helper context UML2!"uml::Class" def : ignore() : Boolean = self.owner.oclIsKindOf(UML2!"uml::Classifier"); helper context UML2!"uml::Interface" def : ignore() : Boolean = self.owner.oclIsKindOf(UML2!"uml::Classifier"); helper context UML2!"uml::Element" def : isInPackage : Boolean = self.owner.oclIsTypeOf(UML2!"uml::Package"); helper context UML2!"uml::Type" def : typeName : String = self.implQualifiedName; helper context UML2!"uml::DataType" def : typeName : String = self.name; helper context UML2!"uml::NamedElement" def : fileName() : String = self.qualifiedName('/'); helper context UML2!"uml::NamedElement" def : pathName() : String = thisModule.path + '/' + self.fileName() + '.java'; helper context UML2!"uml::NamedElement" def : visibilityStr : String = if self.visibility = #private then 'private ' else if self.visibility = #protected then 'protected ' else if self.visibility = #public then 'public ' else '' endif endif endif; helper context UML2!"uml::Classifier" def : abstractStr : String = if self.isAbstract then 'abstract ' else '' endif; helper context UML2!"uml::BehavioralFeature" def : abstractStr : String = if self.isAbstract then 'abstract ' else '' endif; helper context UML2!"uml::Feature" def : staticStr : String = if self.isStatic then 'static ' else '' endif; helper context UML2!"uml::Parameter" def : isFinal : Boolean = not self.getAppliedStereotype('Final::final').oclIsUndefined(); helper context UML2!"uml::StructuralFeature" def : finalStr : String = if self.isLeaf or self.isReadOnly then 'final ' else '' endif; helper context UML2!"uml::RedefinableElement" def : finalStr : String = if self.isLeaf then 'final ' else '' endif; helper context UML2!"uml::Parameter" def : finalStr : String = if self.isFinal then 'final ' else '' endif; helper context UML2!"uml::BehavioralFeature" def : isConstructor : Boolean = not self.getAppliedStereotype('Standard::Create').oclIsUndefined(); -- ====================================================================== -- plumbing helpers end -- ====================================================================== -- ====================================================================== -- code generation helpers begin -- ====================================================================== helper context UML2!"uml::Classifier" def : toFileString() : String = self.packageDecl() + self.importDecl() + '\n' + self.toString(); helper context UML2!"uml::Classifier" def : packageDecl() : String = if self.isInPackage then 'package ' + self.owner.qualifiedName('.') + ';\n\n' else '' endif; helper context UML2!"uml::Classifier" def : importDecl() : String = self.clientDependency ->select(c|c.oclIsTypeOf(UML2!"uml::Dependency") and c.getAppliedStereotypes()->isEmpty()) ->collect(d|d.supplier)->flatten() ->iterate(e; acc : String = '' | acc + e.importLine()); helper context UML2!"uml::NamedElement" def : importLine() : String = 'import ' + self.importName + ';\n'; helper context UML2!"uml::NamedElement" def : importName : String = self.implQualifiedName; helper context UML2!"uml::Package" def : importName : String = self.implQualifiedName + '.*'; helper context UML2!"uml::Class" def : toString() : String = self.visibilityStr + self.abstractStr + self.finalStr + 'class ' + self.name + self.extendsClause() + self.implementsClause() + ' {\n' + self.nestedClassifier->iterate(e; acc : String = '' | acc + e.toString()) + self.ownedAttribute->iterate(e; acc : String = '' | acc + e.toString()) + self.ownedOperation->iterate(e; acc : String = '' | acc + e.toString()) + '}\n\n'; helper context UML2!"uml::Class" def : extendsClause() : String = self.generalization->iterate(e; acc : String = '' | acc + if acc = '' then ' extends ' + e.general.implQualifiedName else ', ' + e.general.implQualifiedName endif); helper context UML2!"uml::Class" def : implementsClause() : String = self.clientDependency->select(e | e.oclIsTypeOf(UML2!"uml::InterfaceRealization"))->iterate(e; acc : String = '' | acc + if acc = '' then ' implements ' + e.contract.implQualifiedName else ', ' + e.contract.implQualifiedName endif); helper context UML2!"uml::Property" def : toString() : String = self.visibilityStr + self.staticStr + self.finalStr + self.type.typeName + ' ' + self.name + if self.defaultValue.oclIsUndefined() then '' else if self.defaultValue.stringValue() = '' then '' else ' = ' + self.defaultValue.stringValue() endif endif + ';\n\n'; helper context UML2!"uml::Operation" def : toString() : String = self.visibilityStr + self.staticStr + self.abstractStr + self.finalStr + self.returnParameter() + self.name + '(' + self.parameters() + ')' + self.throwsClause() + if self.isAbstract or self.owner.oclIsKindOf(UML2!"uml::Interface") then ';\n\n' else ' {\n' + self.body() + '\n}\n\n' endif; helper context UML2!"uml::Operation" def : throwsClause() : String = -- exception types must reside in the same model, or we get -- very strange behaviour. self.raisedException->iterate(e; acc : String = '' | acc + if acc = '' then ' throws ' else ', ' endif + e.qualifiedName('.')); helper context UML2!"uml::Operation" def : body() : String = self.method->select(b|b.oclIsKindOf(UML2!"uml::OpaqueBehavior"))->select(a| a.language->includes('java'))->iterate(e; acc : String = ''| acc + e.body->at(e.language->indexOf('java'))); helper context UML2!"uml::BehavioralFeature" def : returnParameter() : String = if self.isConstructor then '' else self.ownedParameter->select(p|p.direction = #return)->iterate(e; acc : String = '' | acc + e.type.typeName + ' ') endif; helper context UML2!"uml::BehavioralFeature" def : parameters() : String = self.ownedParameter->select(p|not (p.direction = #return))->iterate(e; acc : String = '' | acc + if acc = '' then '' else ', ' endif + e.finalStr + e.type.typeName + ' ' + e.name); helper context UML2!"uml::Interface" def : toString() : String = self.visibilityStr + self.abstractStr + 'interface ' + self.name + self.extendsClause() + ' {\n' + self.nestedClassifier->iterate(e; acc : String = '' | acc + e.toString()) + self.ownedOperation->iterate(e; acc : String = '' | acc + e.toString()) + '}\n\n'; helper context UML2!"uml::Interface" def : extendsClause() : String = self.clientDependency->select(e | e.oclIsTypeOf(UML2!"uml::InterfaceRealization"))->iterate(e; acc : String = '' | acc + if acc = '' then ' extends ' + e.contract.ImplQualifiedName else ', ' + e.contract.implQualifiedName endif); -- ====================================================================== -- code generation helpers end -- ======================================================================