]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.structural2/src/org/simantics/structural2/utils/StructuralMetadataUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / utils / StructuralMetadataUtils.java
diff --git a/bundles/org.simantics.structural2/src/org/simantics/structural2/utils/StructuralMetadataUtils.java b/bundles/org.simantics.structural2/src/org/simantics/structural2/utils/StructuralMetadataUtils.java
new file mode 100644 (file)
index 0000000..c5401a9
--- /dev/null
@@ -0,0 +1,127 @@
+package org.simantics.structural2.utils;\r
+\r
+import gnu.trove.set.hash.THashSet;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.changeset.ChangeVisitor;\r
+import org.simantics.db.layer0.changeset.MetadataUtils;\r
+import org.simantics.db.layer0.genericrelation.DependencyChanges;\r
+import org.simantics.db.layer0.genericrelation.DependencyChanges.Change;\r
+import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentAddition;\r
+import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentModification;\r
+import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentRemoval;\r
+import org.simantics.db.layer0.request.ModelInstances;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+\r
+public class StructuralMetadataUtils {\r
+\r
+       private static ChangeVisitor getChangeVisitor(final ReadGraph graph, final Resource model, final StructuralChangeVisitor visitor) throws DatabaseException {\r
+               \r
+               return new ChangeVisitor() {\r
+            \r
+            Layer0 L0 = Layer0.getInstance(graph);\r
+            StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
+            THashSet<Resource> visited = new THashSet<Resource>();\r
+            Resource IsLiftedAs = graph.getResource("http://www.simantics.org/Diagram-2.2/IsLiftedAs");\r
+            \r
+            @Override\r
+            public void visit(ReadGraph graph, Change change, boolean inverted)\r
+                    throws DatabaseException {\r
+                \r
+                switch(change.getType()) {\r
+                case COMPONENT_ADDITION: {\r
+                    ComponentAddition addition = (ComponentAddition)change;\r
+                    if(inverted)\r
+                        visitor.visitComponentChange(graph, addition.parent, false);\r
+                    else\r
+                        visitor.visitComponentChange(graph, addition.component, true);\r
+                } break;\r
+                case COMPONENT_MODIFICATION: {\r
+                    ComponentModification modification = (ComponentModification)change;\r
+                    handleModification(graph, modification.component);\r
+                } break;\r
+                case COMPONENT_REMOVAL: {\r
+                    ComponentRemoval removal = (ComponentRemoval)change;\r
+                    if(inverted)\r
+                        visitor.visitComponentChange(graph, removal.component, true);\r
+                    else\r
+                        visitor.visitComponentChange(graph, removal.parent, false);\r
+                } break;\r
+                case LINK_CHANGE:\r
+                    // Link changes should not affect the structural configuration \r
+                    break;\r
+                }\r
+            }\r
+            \r
+            void handleModification(ReadGraph graph, Resource resource) throws DatabaseException {\r
+                if(!visited.add(resource))\r
+                    return;\r
+                if(!graph.hasStatement(resource))\r
+                    return;\r
+                //System.out.println("handleModification: " + graph.getPossibleURI(resource));\r
+                if(graph.isInstanceOf(resource, STR.Component))\r
+                    visitor.visitComponentChange(graph, resource, true);\r
+                else if(graph.isInstanceOf(resource, STR.Connection))\r
+                    for(Resource connection : StructuralUtils.getRelatedConnections(graph, resource))\r
+                        handleConnectionChange(graph, connection);\r
+                else if(graph.isInstanceOf(resource, STR.ConnectionJoin))\r
+                    for(Resource connection : StructuralUtils.getRelatedConnectionsOfConnectionJoin(graph, resource))\r
+                        handleConnectionChange(graph, connection);\r
+                else if(graph.isInstanceOf(resource, STR.ComponentType))\r
+                    for(Resource instance : graph.syncRequest(new ModelInstances(model, resource)).values())\r
+                        visitor.visitComponentChange(graph, instance, true);\r
+                else {\r
+                    Resource connectionRelation = graph.getPossibleObject(resource, IsLiftedAs);\r
+                    if(connectionRelation != null)\r
+                        handleConnectionRelation(graph, connectionRelation);\r
+                    else\r
+                        for(Resource parent : graph.getObjects(resource, L0.IsDependencyOf))\r
+                            handleModification(graph, parent);\r
+                }\r
+            }\r
+\r
+            void handleConnectionChange(ReadGraph graph, Resource connection) throws DatabaseException {\r
+                for(Resource component : graph.getObjects(connection, STR.Connects))\r
+                    visitor.visitComponentChange(graph, component, true);\r
+                for(Resource connectionRelation : graph.getObjects(connection, STR.Binds))\r
+                    handleConnectionRelation(graph, connectionRelation);\r
+            }\r
+            \r
+            void handleConnectionRelation(ReadGraph graph, Resource connectionRelation) throws DatabaseException {\r
+                Resource componentType = graph.getPossibleObject(connectionRelation, L0.HasDomain);\r
+                if(componentType == null)\r
+                    return;\r
+                for(Resource instance : graph.syncRequest(new ModelInstances(model, componentType)).values())\r
+                    for(Resource connection2 : graph.getObjects(instance, connectionRelation))\r
+                        handleConnectionChange(graph, connection2);\r
+            }\r
+            \r
+        };\r
+       }\r
+       \r
+    /**\r
+     * Finds all structural changes made to the given {@code model} from the given revision and calls \r
+     * the {@code visitor} for them.\r
+     * The function processes raw metadata about modifications to resources and produces changes\r
+     * to components. It implements the following rules:\r
+     * <ul>\r
+     *     <li>Component removal implies a change to the parent of the component</li>\r
+     *     <li>Component addition implies a change to the added component.</li>\r
+     *     <li>A modification in the connection or join implies a change to all reachable components of the connection</li>\r
+     *     <li>A modification in a component type implies a change to all its instances</li>  \r
+     * </ul>\r
+     */\r
+    public static void visitStructuralChangesFrom(final ReadGraph graph, final Resource model, long fromRevision,\r
+            final StructuralChangeVisitor visitor) throws DatabaseException {\r
+        MetadataUtils.visitDependencyChangesFrom(graph, model, fromRevision, getChangeVisitor(graph, model, visitor));\r
+    }\r
+\r
+    public static void visitStructuralChangesFrom(final ReadGraph graph, final Resource model, DependencyChanges.Change[] changes,\r
+            final StructuralChangeVisitor visitor) throws DatabaseException {\r
+        MetadataUtils.visitDependencyChangesFrom2(graph, model, changes, getChangeVisitor(graph, model, visitor));\r
+    }\r
+    \r
+}\r