Working UML relationships in the graphviz-class-diagram templates

This commit is contained in:
Iván Sánchez Ortega
2020-09-08 19:06:44 +02:00
parent 00a0497f3b
commit 896db4c891
10 changed files with 163 additions and 12 deletions

View File

@ -184,7 +184,7 @@ The syntax is:
🍂relationship type namespace [ ,cardinalityFrom [ ,cardinalityTo [ ,label ]]]
```
The following relationship `type`s are implemented:
The following relationship `type`s are implemented for display in the `graphviz-class-diagram` templates:
- `associated`
- `implements`
- `dependsOn`

View File

@ -0,0 +1,32 @@
@class ChildClass
@inherits ParentClass
@method childMethod(foo: String): this
@class ParentClass
@method parentMethod(bar: Boolean): this
@class ConcreteClass
@relationship implements AbstractClass
@class AbstractClass
@namespace Student
@relationship associated ConcreteClass ,, Meaningless connection to
@namespace Teacher
@namespace Classroom
@relationship aggregationOf Student, 0..n, 1..n
@relationship aggregationOf Teacher, 1 , 0..n

View File

@ -0,0 +1,6 @@
{
"templateDir": "./templates/graphviz-class-diagram",
"showInheritancesWhenEmpty": true,
"leadingCharacter": "@",
"outputExtension": "dot"
}

View File

@ -0,0 +1,86 @@
digraph ClassDiagram {
node [shape=plaintext]
edge [arrowhead=empty];
"ChildClass" [ label = <<TABLE CELLSPACING="0">
<TR><TD><B>ChildClass</B></TD></TR>
<TR><TD><I>Methods</I><BR/>
childMethod(&lt;String&gt; foo):this
<BR align="left" />
</TD></TR>
</TABLE>>]
"ChildClass" -> "ParentClass"
"ParentClass" [ label = <<TABLE CELLSPACING="0">
<TR><TD><B>ParentClass</B></TD></TR>
<TR><TD><I>Methods</I><BR/>
parentMethod(&lt;Boolean&gt; bar):this
<BR align="left" />
</TD></TR>
</TABLE>>]
"ConcreteClass" [ label = <<TABLE CELLSPACING="0">
<TR><TD><B>ConcreteClass</B></TD></TR>
</TABLE>>]
"ConcreteClass" -> "AbstractClass" [
arrowhead=empty, style=dashed
]
"AbstractClass" [ label = <<TABLE CELLSPACING="0">
<TR><TD><B>AbstractClass</B></TD></TR>
</TABLE>>]
"Student" [ label = <<TABLE CELLSPACING="0">
<TR><TD><B>Student</B></TD></TR>
</TABLE>>]
"Student" -> "ConcreteClass" [
arrowhead = open
, label="Meaningless connection to"
]
"Teacher" [ label = <<TABLE CELLSPACING="0">
<TR><TD><B>Teacher</B></TD></TR>
</TABLE>>]
"Classroom" [ label = <<TABLE CELLSPACING="0">
<TR><TD><B>Classroom</B></TD></TR>
</TABLE>>]
"Classroom" -> "Student" [
arrowhead=odiamond
, headlabel="0..n"
, taillabel="1..n"
]
"Classroom" -> "Teacher" [
arrowhead=odiamond
, headlabel="1"
, taillabel="0..n"
]
}

View File

@ -335,11 +335,11 @@ export default class Leafdoc {
if (directive === 'relationship') {
var split = regexps.relationshipDefinition.exec(content);
currentNamespace.relationships.push({
type: parts[1],
namespace: parts[2],
cardinalityFrom: parts[3],
cardinalityTo: parts[4],
label: parts[5],
type: split[1],
namespace: split[2],
cardinalityFrom: split[3],
cardinalityTo: split[4],
label: split[5],
});
}
}

View File

@ -71,10 +71,10 @@ export const miniclassDefinition = xRegExp('^ (?<miniclass> .+ ) \\s* \\( (?<rea
// Parses a UML-like relationship definition
export const relationshipDefinition = xRegExp(`^
(?<type> \\S+ ) \\s*
(?<namespace> \\S+ ) \\s*
(?<cardinalityFrom> [^,\\s]* )? \\s*
(, \\s* (?<cardinalityTo> [^,\\s]* )? \\s*
(, \\s* (?<label> .+ )? )?
)? \\s* $`, 'nx');
(?<namespace> [^,\\s]+ ) \\s*
(, \\s* (?<cardinalityFrom> [^,\\s]*) )? \\s*
(, \\s* (?<cardinalityTo> [^,\\s]*) )? \\s*
(, \\s* (?<label> .+ )? )?
\\s* $`, 'nx');

View File

@ -93,3 +93,9 @@ Handlebars.registerHelper('json', function jsonHelper(obj) {
});
// Comparison helper. Inspired from https://github.com/helpers/handlebars-helpers
Handlebars.registerHelper('equals', function equalityHelper(a, b) {
return a == b;
});

View File

@ -8,6 +8,26 @@
"{{../name}}" -> "{{this}}"
{{/each}}
{{#each relationships}}
"{{../name}}" -> "{{namespace}}" [
{{#if (equals type "associated")}}
arrowhead = open
{{else if (equals type "implements")}}
arrowhead=empty, style=dashed
{{else if (equals type "dependsOn")}}
arrowhead=open, style=dashed
{{else if (equals type "aggregationOf")}}
arrowhead=odiamond
{{else if (equals type "compositionOf")}}
arrowhead=diamond
{{else}}
{{! Miniclasses don't have a name, skip them }}
arrowhead=normal
{{/if}}
{{#if cardinalityFrom}}, headlabel="{{cardinalityFrom}}"{{/if}}
{{#if cardinalityTo}}, taillabel="{{cardinalityTo}}"{{/if}}
{{#if label}}, label="{{label}}"{{/if}}
]
{{/each}}
{{else}}
{{! Miniclasses don't have a name per se, skip them }}
{{/if}}