TODO: Group的Parent混乱, 需要进一步完善

This commit is contained in:
huangsimin 2019-12-18 18:13:48 +08:00
parent 009a5e37e2
commit 4f393f6573
6 changed files with 1203 additions and 28 deletions

View File

@ -1,4 +1,5 @@
<factorypath>
<factorypathentry kind="VARJAR" id="M2_REPO/commons-codec/commons-codec/1.13/commons-codec-1.13.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/logging/log4j/log4j-core/2.12.1/log4j-core-2.12.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/logging/log4j/log4j-api/2.12.1/log4j-api-2.12.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/slf4j/slf4j-log4j12/1.7.29/slf4j-log4j12-1.7.29.jar" enabled="true" runInBatchMode="false"/>

View File

@ -25,7 +25,12 @@
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.13</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>

File diff suppressed because it is too large Load Diff

View File

@ -4,10 +4,16 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.mxgraph.io.mxCodec;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.model.mxICell;
import com.mxgraph.model.mxIGraphModel;
import com.mxgraph.util.mxUtils;
import com.mxgraph.util.mxXmlUtils;
import com.mxgraph.view.mxGraph;
@ -25,47 +31,189 @@ public class Graph {
mxGraph graph;
mxIGraphModel imodel;
mxCodec codec;
public static Graph Merge(Graph g1, Graph g2) {
private static class mxCellCloned {
Graph graph = new Graph();
mxGraph mg1 = g1.getMXGraph();
mxGraph mg2 = g2.getMXGraph();
mxCell cell;
for( Object cell: mg1.getChildCells(mg1.getDefaultParent())) {
graph.getMXGraph().addCell(cell);
String parentid;
String sourceid;
String targetid;
String newId;
boolean isGroup;
public static List<mxCellCloned> clone(final Graph graph, Boolean isInGroup) throws CloneNotSupportedException {
final String uid = Utils.GeneratorUID();
final HashMap<String, mxCellCloned> idmap = new HashMap<>();
final Object[] _cells = graph.getMXGraph().getChildCells(graph.getMXGraph().getDefaultParent());
final List<mxCellCloned> cells = new ArrayList<mxCellCloned>();
mxCellCloned groupCell = null;
if (isInGroup) {
groupCell = new mxCellCloned();
groupCell.cell = new mxCell();
groupCell.cell.setId(uid);
groupCell.cell.setStyle("group");
groupCell.cell.setConnectable(false);
groupCell.cell.setVertex(true);
groupCell.isGroup = true;
}
for (final Object _cell : _cells) {
final mxCell cell = (mxCell) _cell;
final String curid = cell.getId();
if (curid.equals("0") || curid.equals("1")) {
continue;
}
final mxCellCloned cloned = new mxCellCloned();
cloned.cell = (mxCell) cell.clone();
cloned.cell.setId(curid);
final mxICell parent = cell.getParent();
if (parent != null) {
final String pid = parent.getId();
if(!pid.equals("1")){
cloned.parentid = pid;
}
}
final mxICell source = cell.getSource();
if (source != null) {
final String sid = source.getId();
cloned.sourceid = sid;
}
final mxICell target = cell.getTarget();
if (target != null) {
final String tid = target.getId();
cloned.targetid = tid;
}
cells.add(cloned);
}
for (final mxCellCloned cloned : cells) {
final String curid = cloned.cell.getId();
final String[] ids = curid.split("-");
cloned.newId = uid + "-" + ids[ids.length - 1];
idmap.put(curid, cloned);
cloned.cell.setId(cloned.newId);
}
for (final mxCellCloned cloned : cells) {
if (cloned.sourceid != null) {
final mxCellCloned source = idmap.get(cloned.sourceid);
cloned.cell.setSource(source.cell);
}
if (cloned.targetid != null) {
final mxCellCloned target = idmap.get(cloned.targetid);
cloned.cell.setTarget(target.cell);
}
if (cloned.parentid != null) {
final mxCellCloned parent = idmap.get(cloned.parentid);
if (parent != null) {
cloned.cell.setParent(parent.cell);
}
} else {
if(isInGroup) {
cloned.cell.setParent(groupCell.cell);
mxICell parent = cloned.cell.getParent();
System.out.println(parent);
}
}
}
if (isInGroup) {
cells.add(groupCell);
}
return cells;
}
for( Object cell: mg2.getChildCells(mg2.getDefaultParent())) {
graph.getMXGraph().addCell(cell);
public static List<mxCellCloned> clone(final Graph graph) throws CloneNotSupportedException {
return clone(graph, false);
}
}
public Graph() {
imodel = new mxGraphModel();
graph = new mxGraph(imodel);
}
public static Graph merge(final Graph g1, final Graph g2, boolean isInGroup) throws CloneNotSupportedException {
final Graph graph = new Graph();
graph.imodel.beginUpdate();
final List<mxCellCloned> cellCloneds1 = mxCellCloned.clone(g1, isInGroup);
final List<mxCellCloned> cellCloneds2 = mxCellCloned.clone(g2, isInGroup);
HashMap<String, Object> groupmap = new HashMap<String, Object>();
cellCloneds1.forEach(cloned -> {
mxICell parent = cloned.cell.getParent();
if (parent != null) {
Object np = groupmap.get(parent.getId());
if(np != null) {
graph.getMXGraph().addCell(cloned.cell, np);
} else {
System.out.println("x");
}
} else {
Object groupcell = graph.getMXGraph().addCell(cloned.cell);
groupmap.put(cloned.cell.getId(), groupcell);
}
});
cellCloneds2.forEach(cloned -> {
if (cloned.cell.getParent() != null) {
graph.getMXGraph().addCell(cloned.cell, cloned.cell.getParent());
} else {
graph.getMXGraph().addCell(cloned.cell);
}
});
graph.imodel.endUpdate();
return graph;
}
private void loadString(String xmlString) {
public static Graph merge(final Graph g1, final Graph g2) throws CloneNotSupportedException {
return merge(g1, g2, false);
}
private void loadString(final String xmlString) {
codec = null;
imodel = new mxGraphModel();
graph = new mxGraph(imodel);
Document doc = mxXmlUtils.parseXml(xmlString);
codec = new mxCodec(doc);
Node dmodel = doc.getElementsByTagName("mxGraphModel").item(0);
final Document doc = mxXmlUtils.parseXml(xmlString);
final mxCodec codec = new mxCodec(doc);
final Node dmodel = doc.getElementsByTagName("mxGraphModel").item(0);
codec.decode(dmodel, imodel);
}
public void load(String pathStr) throws IOException {
public void load(final String pathStr) throws IOException {
Path path = Paths.get(pathStr);
String xmlString = Files.readString(path);
final Path path = Paths.get(pathStr);
final String xmlString = Files.readString(path);
loadString(xmlString);
}
}
public void load(byte[] xmlBytes) {
public void load(final byte[] xmlBytes) {
loadString(new String(xmlBytes));
@ -76,8 +224,6 @@ public class Graph {
}
public String dumpModel() {
return mxXmlUtils.getXml(codec.encode(imodel));
return mxXmlUtils.getXml(new mxCodec().encode(imodel));
}
}

View File

@ -0,0 +1,15 @@
package com.yame;
import java.nio.ByteBuffer;
import java.util.UUID;
public class Utils {
public static String GeneratorUID() {
final ByteBuffer buf = ByteBuffer.allocate(16);
final UUID uid = UUID.randomUUID();
buf.putLong(uid.getLeastSignificantBits());
buf.putLong(uid.getMostSignificantBits());
return com.yame.Base64.getYameEncoder().encodeToString(buf.array());
}
}

View File

@ -24,17 +24,22 @@ public class GraphTest {
assertTrue(graph.dumpModel().startsWith("<mxGraphModel>"));
graph.load(graph.dumpModel().getBytes());
assertTrue(graph.dumpModel().startsWith("<mxGraphModel>"));
}
@Test
public void TestMegre() throws IOException {
public void TestMegre() throws IOException, CloneNotSupportedException {
Graph graph = new Graph();
graph.load(AppTest.testPath + "1.xml");
Graph g = Graph.Merge(graph, graph);
Graph g = Graph.merge(graph, graph);
log.debug(g.dumpModel());
}
@Test
public void TestMegreGroup() throws IOException, CloneNotSupportedException {
Graph graph = new Graph();
graph.load(AppTest.testPath + "1.xml");
Graph g = Graph.merge(graph, graph, true);
log.debug(g.dumpModel());
}
@Test