]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Validate plant structure on DB write to prevent corruption 06/3406/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Thu, 24 Oct 2019 11:29:46 +0000 (14:29 +0300)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Thu, 24 Oct 2019 11:29:46 +0000 (14:29 +0300)
gitlab #35

Change-Id: Ibea2a0fa90fcc4c3a39ac2ced7b85e69f5bceaeb

org.simantics.plant3d/src/org/simantics/plant3d/editor/P3DNodeMap.java

index 10e914de4e142868d099815b958af4cebfd661c3..f7bb42306aad6690a9088d35e8eac79d5ccac054 100644 (file)
@@ -7,7 +7,9 @@ import java.util.List;
 import java.util.Set;
 
 import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
 import org.simantics.db.Session;
+import org.simantics.db.WriteGraph;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.g3d.ontology.G3D;
 import org.simantics.g3d.scenegraph.base.INode;
@@ -38,7 +40,7 @@ public class P3DNodeMap extends AbstractVTKNodeMap<INode> {
        }
        @Override
        protected void updateActor(INode n, Set<String> ids) {
-               if (DEBUG) System.out.println("P3DNodeMap update " + n);
+               if (DEBUG) System.out.println("P3DNodeMap update " + debugString(n));
                if (!(n instanceof IP3DVisualNode)) {
                        if (n instanceof PipeControlPoint) {
                                n = ((PipeControlPoint)n).getPipelineComponent();
@@ -52,7 +54,7 @@ public class P3DNodeMap extends AbstractVTKNodeMap<INode> {
                IP3DVisualNode node = (IP3DVisualNode)n;
                
                if (DEBUG) {
-                       System.out.print("P3DNodeMap update " + node);
+                       System.out.print("P3DNodeMap update " + debugString(node));
                        for (String s : ids)
                                System.out.print(" " + s);
                        System.out.println();
@@ -92,7 +94,7 @@ public class P3DNodeMap extends AbstractVTKNodeMap<INode> {
        }
        
        private void updateTransform(IP3DNode node) {
-               if (DEBUG) System.out.println("P3DNodeMap update Transform " + node);
+               if (DEBUG) System.out.println("P3DNodeMap update Transform " + debugString(node));
 
                node.update(view.getRenderer());
                
@@ -117,7 +119,7 @@ public class P3DNodeMap extends AbstractVTKNodeMap<INode> {
        
        @Override
        protected void removeActor(INode n) {
-               if (DEBUG) System.out.println("P3DNodeMap.removeActor " + n);
+               if (DEBUG) System.out.println("P3DNodeMap.removeActor " + debugString(n));
                if (!(n instanceof IP3DVisualNode))
                        return;
                IP3DVisualNode node = (IP3DVisualNode)n;
@@ -132,13 +134,15 @@ public class P3DNodeMap extends AbstractVTKNodeMap<INode> {
        
        @Override
        protected void addActor(INode n) {
-               if (DEBUG) System.out.println("P3DNodeMap.addActor " + n);
+               if (DEBUG) System.out.println("P3DNodeMap.addActor " + debugString(n));
                if (!(n instanceof IP3DVisualNode))
                        return;
                IP3DVisualNode node = (IP3DVisualNode)n;
                
-               if (hasActor(node))
+               if (hasActor(node)) {
+                   if (DEBUG) System.out.println("Node already has an actor");
                        return;
+               }
                if (Thread.currentThread() != view.getThreadQueue().getThread())
                        throw new RuntimeException("Illegal thread.");
                
@@ -150,6 +154,7 @@ public class P3DNodeMap extends AbstractVTKNodeMap<INode> {
                        nodeToActor.add(node, act);
                        actorToNode.put(act, node);
                }
+               if (DEBUG) System.out.println("Added " + node.getActors().size() + " actors");
                
                if (node instanceof P3DParentNode<?>) {
                        for (IP3DNode n2 : ((P3DParentNode<?>)node).getNodes())
@@ -205,6 +210,12 @@ public class P3DNodeMap extends AbstractVTKNodeMap<INode> {
                super.commit(commitMessage);
                
        }
+       
+       @Override
+       protected void commit(WriteGraph graph) throws DatabaseException {
+           super.commit(graph);
+           validateGraph(graph);
+       }
        @Override
        protected void doCommit() {
 //             System.out.println("Do commit");
@@ -240,5 +251,56 @@ public class P3DNodeMap extends AbstractVTKNodeMap<INode> {
                }
                
        }
+       
+       protected void validateGraph(ReadGraph graph) throws DatabaseException {
+           Plant3D P3D = Plant3D.getInstance(graph);
+           Resource root = (Resource)mapping.inverseGet(rootNode);
+           if (!graph.isInstanceOf(root, P3D.Plant))
+               throw new DatabaseException("Root is not a Plant");
+           Set<Resource> equipment = new HashSet<>();
+           Set<Resource> pipeRuns = new HashSet<>();
+           for (Resource r : graph.getObjects(root, P3D.children)) {
+               if (graph.isInstanceOf(r, P3D.Equipment)) {
+                   equipment.add(r);
+               } else if (graph.isInstanceOf(r, P3D.PipeRun)) {
+                   pipeRuns.add(r);
+               } else {
+                   throw new DatabaseException("Unknown resource " + r);
+               }
+           }
+           Set<Resource> nozzles = new HashSet<>();
+           for (Resource e : equipment) {
+               for (Resource n : graph.getObjects(e, P3D.HasNozzle)) {
+                   if (graph.isInstanceOf(n, P3D.Nozzle)) {
+                       nozzles.add(n);
+                   } else {
+                       throw new DatabaseException("Unknown nozzle resource " + n);
+                   }
+               }
+           }
+           
+           Set<Resource> components = new HashSet<>();
+        for (Resource run : pipeRuns) {
+            for (Resource c : graph.getObjects(run, P3D.children)) {
+                if (graph.isInstanceOf(c, P3D.PipelineComponent)) {
+                    components.add(c);
+                } else {
+                    throw new DatabaseException("Unknown component resource " + c);
+                }
+            }
+        }
+        
+        for (Resource c : components) {
+            for (Resource connected : graph.getObjects(c, P3D.Connects)) {
+                if (!components.contains(connected) && !nozzles.contains(connected)) {
+                    throw new DatabaseException("Unbrowsable component resource " + connected + " connected to " + c);
+                }
+                Collection<Resource> connectedConnected = graph.getObjects(connected, P3D.Connects);
+                if (!connectedConnected.contains(c)) {
+                    throw new DatabaseException("Component resource " + c + " is connected to " + connected +", but its has no connection back.");
+                }
+            }
+        }
+       }
 
 }