BrightSide Workbench Full Report + Source Code
EntityGraph.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.entities.graph;
20 
21 import java.io.Writer;
22 import java.util.LinkedHashMap;
23 import java.util.Map;
24 import org.jgrapht.Graphs;
25 import org.jgrapht.graph.DefaultEdge;
26 import org.jgrapht.graph.SimpleGraph;
27 import org.jgrapht.nio.Attribute;
28 import org.jgrapht.nio.DefaultAttribute;
29 import org.turro.graph.GraphUtil;
30 import org.turro.reflection.Instances;
31 import org.turro.string.Strings;
32 
37 public class EntityGraph extends SimpleGraph<EntityVertex, DefaultEdge> {
38 
39  /* Populate */
40 
41  private void addEdges() {
42  Instances.fresh().byInterface(IVertexProvider.class, IVertexProvider.class)
43  .forEach(p -> p.getEdges()
44  .forEach(e -> Graphs.addEdgeWithVertices(this, e.getSource(), e.getTarget())));
45  }
46 
47  /* Utils */
48 
49  public static void DOT(Writer writer) {
50  GraphUtil.<EntityVertex, DefaultEdge>toDOT(writer, load(),
51  fi -> Strings.identifier(fi.getIdentifier()),
52  (fi) -> {
53  Map<String, Attribute> map = new LinkedHashMap<>();
54  map.put("color", DefaultAttribute.createAttribute(fi.getColor()));
55  map.put("fontcolor", DefaultAttribute.createAttribute(fi.getFontColor()));
56  map.put("label", DefaultAttribute.createAttribute(fi.getName()));
57  return map;
58  });
59  }
60 
61  /* Factory */
62 
63  public static EntityGraph load() {
64  return new EntityGraph();
65  }
66 
67  private EntityGraph() {
68  super(DefaultEdge.class);
69  addEdges();
70  }
71 
72 }
static void DOT(Writer writer)