package org.simantics.interop.update.model; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Set; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.interop.test.GraphChanges; public class UpdateNode3 { UpdateNode un1; UpdateNode un2; UpdateNode un3; private Collection children = new ArrayList(); public Collection getChildren() { return children; } public UpdateNode getUn1() { return un1; } public UpdateNode getUn2() { return un2; } public UpdateNode getUn3() { return un3; } public UpdateNode3(UpdateNode un1, UpdateNode un2, UpdateNode un3) { this.un1 = un1; this.un2 = un2; this.un3 = un3; } public boolean isVisible() { if (un1 != null) return un1.isVisible(); if (un2 != null) return un2.isVisible(); if (un3 != null) return un3.isVisible(); return false; } public void setVisible(boolean visible) { if (un1 != null) un1.setVisible(visible); if (un2 != null) un2.setVisible(visible); if (un3 != null) un3.setVisible(visible); } public static UpdateNode3 getCombinedTree(ModelUpdate update) throws DatabaseException { UpdateTree updateTree1 = update.getUpdateTree(); UpdateTree updateTree2 = update.getUpdateTree2(); UpdateTree updateTree3 = update.getUpdateTree3(); UpdateNode3 n3 = new UpdateNode3(updateTree1.getRootNode(), updateTree2.getRootNode(), updateTree3.getRootNode()); populate(n3, update.getChanges(), update.getChanges2(), update.getChanges3()); return n3; } private static void getComparable(UpdateNode n, GraphChanges gc1, GraphChanges gc2, GraphChanges gc3, Set nodeR) { nodeR.clear(); Resource r = n.getResource(); nodeR.add(r); addNotNull(nodeR,gc1.getComparable().getLeft(r)); addNotNull(nodeR,gc1.getComparable().getRight(r)); addNotNull(nodeR,gc2.getComparable().getLeft(r)); addNotNull(nodeR,gc2.getComparable().getRight(r)); addNotNull(nodeR,gc3.getComparable().getLeft(r)); addNotNull(nodeR,gc3.getComparable().getRight(r)); } public static void addNotNull(Set set, T t) { if (t != null) set.add(t); } private static void populate(UpdateNode3 un, GraphChanges gc1, GraphChanges gc2, GraphChanges gc3) { Set p1 = new HashSet<>(); Set p2 = new HashSet<>(); Set p3 = new HashSet<>(); Set nodeR = new HashSet<>(); if (un.getUn1() != null) { for (UpdateNode n1 : un.getUn1().getChildren()) { getComparable(n1, gc1, gc2, gc3, nodeR); p1.add(n1); UpdateNode n2 = null; if (un.getUn2() != null) { n2 = getMatching(n1, un.getUn2().getChildren(), nodeR); } UpdateNode n3 = null; if (un.getUn3() != null) { n3 = getMatching(n1, un.getUn3().getChildren(), nodeR); } UpdateNode3 cn = new UpdateNode3(n1, n2, n3); un.children.add(cn); populate(cn, gc1, gc2, gc3); if (n2 != null) p2.add(n2); if (n3 != null) p3.add(n3); } } if (un.getUn2() != null) { for (UpdateNode n2 : un.getUn2().getChildren()) { if (p2.contains(n2)) continue; p2.add(n2); getComparable(n2, gc1, gc2, gc3, nodeR); UpdateNode n3 = null; if (un.getUn3() != null) { n3 = getMatching(n2, un.getUn3().getChildren(), nodeR); } UpdateNode3 cn = new UpdateNode3(null, n2, n3); un.children.add(cn); populate(cn, gc1, gc2, gc3); if (n3 != null) p3.add(n3); } } if (un.getUn3() != null) { for (UpdateNode n3 : un.getUn3().getChildren()) { if (p3.contains(n3)) continue; p3.add(n3); UpdateNode3 cn = new UpdateNode3(null, null, n3); un.children.add(cn); populate(cn, gc1, gc2, gc3); } } } private static UpdateNode getMatching(UpdateNode n , Collection coll, Set set) { if (n.getResource() == null) { if (coll.size() != 1) return null; UpdateNode n2 = coll.iterator().next(); if (n2.getClass() != n.getClass()) return null; return n2; } for (UpdateNode c : coll) { if (set.contains(c.getResource())) return c; } return null; } }