Skip to content

Commit

Permalink
associations
Browse files Browse the repository at this point in the history
  • Loading branch information
niklauslee committed Apr 27, 2016
1 parent 2411bcc commit 43502c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
24 changes: 23 additions & 1 deletion PythonCodeGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ define(function (require, exports, module) {
} else {
line = "self." + elem.name;
}
if (elem.defaultValue && elem.defaultValue.length > 0) {
if (elem.multiplicity && _.contains(["0..*", "1..*", "*"], elem.multiplicity.trim())) {
line += " = []";
} else if (elem.defaultValue && elem.defaultValue.length > 0) {
line += " = " + elem.defaultValue;
} else {
line += " = None";
Expand All @@ -140,6 +142,8 @@ define(function (require, exports, module) {
hasBody = false;
codeWriter.writeLine("def __init__(self):");
codeWriter.indent();

// from attributes
if (elem.attributes.length > 0) {
elem.attributes.forEach(function (attr) {
if (attr.isStatic === false) {
Expand All @@ -148,9 +152,27 @@ define(function (require, exports, module) {
}
});
}

// from associations
var associations = Repository.getRelationshipsOf(elem, function (rel) {
return (rel instanceof type.UMLAssociation);
});
for (var i = 0, len = associations.length; i < len; i++) {
var asso = associations[i];
if (asso.end1.reference === elem && asso.end2.navigable === true) {
self.writeVariable(codeWriter, asso.end2, options);
hasBody = true;
}
if (asso.end2.reference === elem && asso.end1.navigable === true) {
self.writeVariable(codeWriter, asso.end1, options);
hasBody = true;
}
}

if (!hasBody) {
codeWriter.writeLine("pass");
}

codeWriter.outdent();
codeWriter.writeLine();
};
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ Belows are the rules to convert from UML model elements to Python source codes.
* converted to a python class inherited from _Enum_ as a separated module (`.py`).
* literals converted to class variables

### UMLAttribute
### UMLAttribute, UMLAssociationEnd

* converted to an instance variable if `isStatic` property is true, or a class variable if `isStatic` property is false
* `name` property to identifier
* `documentation` property to docstring
* If `multiplicity` is one of `0..*`, `1..*`, `*`, then the variable will be initialized with `[]`.

### UMLOperation

Expand Down

0 comments on commit 43502c9

Please sign in to comment.