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