1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-11-11 16:15:50 +00:00

Add associations between entities

This commit is contained in:
Benjamin Davies 2023-08-27 00:03:40 +12:00
parent 07b25de38a
commit d4110d5ceb
No known key found for this signature in database
GPG Key ID: 9FFDE0674336C460
2 changed files with 48 additions and 11 deletions

View File

@ -35,10 +35,17 @@
*/ */
package net.sourceforge.plantuml.cheneer.command; package net.sourceforge.plantuml.cheneer.command;
import net.sourceforge.plantuml.abel.Entity;
import net.sourceforge.plantuml.abel.Link;
import net.sourceforge.plantuml.abel.LinkArg;
import net.sourceforge.plantuml.cheneer.ChenEerDiagram; import net.sourceforge.plantuml.cheneer.ChenEerDiagram;
import net.sourceforge.plantuml.command.CommandExecutionResult; import net.sourceforge.plantuml.command.CommandExecutionResult;
import net.sourceforge.plantuml.command.SingleLineCommand2; import net.sourceforge.plantuml.command.SingleLineCommand2;
import net.sourceforge.plantuml.decoration.LinkDecor;
import net.sourceforge.plantuml.decoration.LinkType;
import net.sourceforge.plantuml.klimt.color.NoSuchColorException; import net.sourceforge.plantuml.klimt.color.NoSuchColorException;
import net.sourceforge.plantuml.klimt.creole.Display;
import net.sourceforge.plantuml.plasma.Quark;
import net.sourceforge.plantuml.regex.IRegex; import net.sourceforge.plantuml.regex.IRegex;
import net.sourceforge.plantuml.regex.RegexConcat; import net.sourceforge.plantuml.regex.RegexConcat;
import net.sourceforge.plantuml.regex.RegexLeaf; import net.sourceforge.plantuml.regex.RegexLeaf;
@ -64,11 +71,27 @@ public class CommandAssociateRelationship extends SingleLineCommand2<ChenEerDiag
} }
@Override @Override
protected CommandExecutionResult executeArg(ChenEerDiagram system, LineLocation location, RegexResult arg) protected CommandExecutionResult executeArg(ChenEerDiagram diagram, LineLocation location, RegexResult arg)
throws NoSuchColorException { throws NoSuchColorException {
final String name = arg.get("NAME", 0); final Entity relationship = diagram.peekOwner();
if (relationship == null) {
return CommandExecutionResult.error("Can only associate from a relationship");
}
System.out.println("- " + name); final String entityName = diagram.cleanId(arg.get("NAME", 0));
final Quark<Entity> entityQuark = diagram.quarkInContext(true, entityName);
final Entity entity = entityQuark.getData();
if (entity == null) {
return CommandExecutionResult.error("No such entity: " + entityName);
}
final LinkType linkType = new LinkType(LinkDecor.NONE, LinkDecor.NONE);
final Link link = new Link(diagram.getEntityFactory(), diagram.getCurrentStyleBuilder(), relationship, entity,
linkType,
// TODO: Cardinality
LinkArg.build(Display.NULL, 1));
diagram.addLink(link);
return CommandExecutionResult.ok(); return CommandExecutionResult.ok();
} }

View File

@ -37,9 +37,13 @@ package net.sourceforge.plantuml.cheneer.command;
import net.sourceforge.plantuml.abel.Entity; import net.sourceforge.plantuml.abel.Entity;
import net.sourceforge.plantuml.abel.LeafType; import net.sourceforge.plantuml.abel.LeafType;
import net.sourceforge.plantuml.abel.Link;
import net.sourceforge.plantuml.abel.LinkArg;
import net.sourceforge.plantuml.cheneer.ChenEerDiagram; import net.sourceforge.plantuml.cheneer.ChenEerDiagram;
import net.sourceforge.plantuml.command.CommandExecutionResult; import net.sourceforge.plantuml.command.CommandExecutionResult;
import net.sourceforge.plantuml.command.SingleLineCommand2; import net.sourceforge.plantuml.command.SingleLineCommand2;
import net.sourceforge.plantuml.decoration.LinkDecor;
import net.sourceforge.plantuml.decoration.LinkType;
import net.sourceforge.plantuml.klimt.color.NoSuchColorException; import net.sourceforge.plantuml.klimt.color.NoSuchColorException;
import net.sourceforge.plantuml.klimt.creole.Display; import net.sourceforge.plantuml.klimt.creole.Display;
import net.sourceforge.plantuml.plasma.Quark; import net.sourceforge.plantuml.plasma.Quark;
@ -83,28 +87,38 @@ public class CommandCreateAttribute extends SingleLineCommand2<ChenEerDiagram> {
new RegexOptional(// new RegexOptional(//
new RegexConcat( // new RegexConcat( //
RegexLeaf.spaceZeroOrMore(), // RegexLeaf.spaceZeroOrMore(), //
new RegexLeaf("COMPOSITE", "\\{"))), // new RegexLeaf("COMPOSITE", "(\\{)"))), //
RegexLeaf.end()); RegexLeaf.end());
} }
@Override @Override
protected CommandExecutionResult executeArg(ChenEerDiagram diagram, LineLocation location, RegexResult arg) protected CommandExecutionResult executeArg(ChenEerDiagram diagram, LineLocation location, RegexResult arg)
throws NoSuchColorException { throws NoSuchColorException {
final LeafType type = LeafType.OBJECT; final Entity owner = diagram.peekOwner();
if (owner == null) {
return CommandExecutionResult.error("Attribute must be inside an entity, relationship or another attribute");
}
final LeafType type = LeafType.USECASE;
final String name = diagram.cleanId(arg.get("NAME", 0)); final String name = diagram.cleanId(arg.get("NAME", 0));
final boolean composite = arg.get("COMPOSITE") != null; final String id = owner.getName() + "/" + name;
final boolean composite = arg.get("COMPOSITE", 0) != null;
final Quark<Entity> quark = diagram.quarkInContext(true, id);
final Quark<Entity> quark = diagram.quarkInContext(true, name);
Entity entity = quark.getData(); Entity entity = quark.getData();
if (entity == null) { if (entity == null) {
Display display = Display.getWithNewlines(name); final Display display = Display.getWithNewlines(name);
entity = diagram.reallyCreateLeaf(quark, display, type, null); entity = diagram.reallyCreateLeaf(quark, display, type, null);
} else { } else {
if (entity.muteToType(type, null) == false) return CommandExecutionResult.error("Attribute already exists");
return CommandExecutionResult.error("Bad name");
} }
final LinkType linkType = new LinkType(LinkDecor.NONE, LinkDecor.NONE);
final Link link = new Link(diagram.getEntityFactory(), diagram.getCurrentStyleBuilder(), entity, owner, linkType,
LinkArg.build(Display.NULL, 1));
diagram.addLink(link);
if (composite) { if (composite) {
diagram.pushOwner(entity); diagram.pushOwner(entity);
} }