1 package org.simantics.modeling.validation;
\r
3 import java.util.Collection;
\r
5 import org.simantics.db.ReadGraph;
\r
6 import org.simantics.db.Resource;
\r
7 import org.simantics.db.Statement;
\r
8 import org.simantics.db.common.request.ReadRequest;
\r
9 import org.simantics.db.common.utils.NameUtils;
\r
10 import org.simantics.db.common.utils.OrderedSetUtils;
\r
11 import org.simantics.db.exception.DatabaseException;
\r
12 import org.simantics.diagram.stubs.DiagramResource;
\r
13 import org.simantics.layer0.Layer0;
\r
14 import org.simantics.modeling.ModelingResources;
\r
15 import org.simantics.structural.stubs.StructuralResource2;
\r
17 public class ValidateMapping extends ReadRequest {
\r
25 StructuralResource2 sr;
\r
26 ModelingResources mr;
\r
28 public ValidateMapping(Resource composite) {
\r
29 this.composite = composite;
\r
33 public void run(ReadGraph g) throws DatabaseException {
\r
35 this.b = Layer0.getInstance(g);
\r
36 this.dr = DiagramResource.getInstance(g);
\r
37 this.sr = StructuralResource2.getInstance(g);
\r
38 this.mr = ModelingResources.getInstance(g);
\r
40 this.diagram = g.getPossibleObject(composite, mr.CompositeToDiagram);
\r
42 System.out.println("------------------------------------------------------------------");
\r
43 System.out.println("---- " + NameUtils.getSafeName(g, composite));
\r
44 if (diagram == null) {
\r
45 System.out.println("No diagram.");
\r
52 private void additiveRule() throws DatabaseException {
\r
53 for(Resource element : OrderedSetUtils.toList(g, diagram)) {
\r
54 Resource elementType = g.getPossibleObject(element, b.InstanceOf);
\r
55 if(elementType == null) {
\r
56 System.out.println("Element " + NameUtils.getSafeName(g, element) + " has no type.");
\r
59 Resource componentType = g.getPossibleObject(elementType, mr.SymbolToComponentType);
\r
60 if(componentType != null)
\r
61 createComponentRule(element, componentType);
\r
62 else if(g.isInstanceOf(element, dr.Connection))
\r
63 createNormalConnectionRule(element);
\r
64 else if(g.isInstanceOf(element, dr.Flag))
\r
65 createFlagRule(element);
\r
67 System.out.println("No rule for element " + NameUtils.getSafeName(g, element));
\r
72 private void createComponentRule(Resource element, Resource componentType) throws DatabaseException {
\r
73 Collection<Resource> components = g.getObjects(element, mr.ElementToComponent);
\r
74 if(components.isEmpty())
\r
75 System.out.println("Element " + NameUtils.getSafeName(g, element) + " is not mapped.");
\r
76 else if(components.size() > 1)
\r
77 System.out.println("Element " + NameUtils.getSafeName(g, element) + " is not multiple times.");
\r
78 else for(Resource component : components) {
\r
79 if(!g.isInstanceOf(component, componentType))
\r
80 System.out.println("Component " + NameUtils.getSafeName(g, component) +
\r
81 " is not an instance of " + NameUtils.getSafeName(g, componentType));
\r
82 if(!g.hasStatement(component, b.PartOf, composite))
\r
83 System.out.println("Component " + NameUtils.getSafeName(g, component) + " is not a part of composite.");
\r
84 if(!g.hasStatement(component, mr.Mapped))
\r
85 System.out.println("Component " + NameUtils.getSafeName(g, component) + " is not tagged as Mapped.");
\r
89 private void createNormalConnectionRule(Resource element) throws DatabaseException {
\r
90 Collection<Resource> connections = g.getObjects(element, mr.DiagramConnectionToConnection);
\r
91 if(connections.isEmpty())
\r
92 System.out.println("Diagram connection " + NameUtils.getSafeName(g, element) + " is not mapped.");
\r
93 else if(connections.size() > 1)
\r
94 System.out.println("Diagram connection " + NameUtils.getSafeName(g, element) + " is not multiple times.");
\r
95 else for(Resource connection : connections) {
\r
96 if(!g.isInstanceOf(connection, sr.Connection))
\r
97 System.out.println("Resource " + NameUtils.getSafeName(g, connection) + " is not a connection.");
\r
98 if(!g.hasStatement(connection, mr.Mapped))
\r
99 System.out.println("Connection " + NameUtils.getSafeName(g, connection) + " is not tagged as Mapped.");
\r
101 for(Resource connector : g.getObjects(element, sr.IsConnectedTo)) {
\r
102 for(Statement stat : g.getStatements(connector, sr.Connects)) {
\r
103 Resource diagramConnectionRelation = g.getInverse(stat.getPredicate());
\r
104 Resource cElement = stat.getObject();
\r
105 Resource connectionRelation = g.getPossibleObject(diagramConnectionRelation, mr.DiagramConnectionRelationToConnectionRelation);
\r
106 if(connectionRelation == null)
\r
108 Resource component = g.getPossibleObject(cElement, mr.ElementToComponent);
\r
109 if(component == null)
\r
112 // Special connection data
\r
113 Resource connectionRelation2 = g.getPossibleObject(diagramConnectionRelation, mr.DiagramConnectionRelationToConnectionRelationB);
\r
114 Resource connectionRelation3 = g.getPossibleObject(diagramConnectionRelation, mr.DiagramConnectionRelationToConnectionRelationC);
\r
115 Resource componentType = g.getPossibleObject(diagramConnectionRelation, mr.DiagramConnectionRelationToComponentType);
\r
117 // Normal connection
\r
118 if(connectionRelation2 == null || connectionRelation3 == null || componentType == null) {
\r
119 if(!g.hasStatement(component, connectionRelation, connection))
\r
120 System.out.println("Missing connection statement " + NameUtils.getSafeName(g, component) + " . " +
\r
121 NameUtils.getSafeName(g, connectionRelation));
\r
124 // Special connection
\r
126 Resource component2 = g.getPossibleObject(element, mr.ElementToComponent);
\r
127 if(component2 == null)
\r
128 System.out.println("Special connection " + NameUtils.getSafeName(g, element) + " is not mapped.");
\r
130 if(!g.isInstanceOf(component2, componentType))
\r
131 System.out.println("Component " + NameUtils.getSafeName(g, component2) +
\r
132 " is not an instance of " + NameUtils.getSafeName(g, componentType));
\r
133 if(!g.hasStatement(component2, b.PartOf, composite))
\r
134 System.out.println("Component " + NameUtils.getSafeName(g, component2) + " is not a part of composite.");
\r
135 if(!g.hasStatement(component2, mr.Mapped))
\r
136 System.out.println("Component " + NameUtils.getSafeName(g, component2) + " is not tagged as Mapped.");
\r
138 boolean foundConnection2 = false;
\r
139 for(Resource connection2 : g.getObjects(component2, connectionRelation2))
\r
140 if(g.hasStatement(component, connectionRelation, connection2)) {
\r
141 foundConnection2 = true;
\r
142 if(!g.isInstanceOf(connection2, sr.Connection))
\r
143 System.out.println(NameUtils.getSafeName(g, connection2) + " is not a connection.");
\r
144 if(!g.hasStatement(component2, connectionRelation3, connection))
\r
145 System.out.println("Missing connection point " + NameUtils.getSafeName(g, component2) + " . "
\r
146 + NameUtils.getSafeName(g, connectionRelation3));
\r
148 if(!foundConnection2)
\r
149 System.out.println("Special connection "+ NameUtils.getSafeName(g, element) + " is not correctly mapped (missing configuration connection).");
\r
160 // Create a connection
\r
162 and(statement_bbf(Component2, ConnectionRelation2, Connection2),
\r
163 statement (Component, ConnectionRelation, Connection2)
\r
167 b(sr.Connection, Connection2),
\r
168 //b(mapped, Connection2),
\r
171 statement(Component2, ConnectionRelation3, Connection)
\r
175 private void createFlagRule(Resource element) throws DatabaseException {
\r
176 for(Resource connectionJoin : g.getObjects(element, dr.FlagIsJoinedBy))
\r
177 for(Resource connector : g.getObjects(element, sr.IsConnectedTo))
\r
178 for(Resource diagramConnection : g.getObjects(connector, sr.Connects))
\r
179 for(Resource connection : g.getObjects(diagramConnection, mr.DiagramConnectionToConnection)) {
\r
180 if(!g.hasStatement(connectionJoin, sr.Joins, connection))
\r
181 System.out.println("Joins-relation of flag " + NameUtils.getSafeName(g, element) + " is not mapped.");
\r
182 if(!g.hasStatement(composite, sr.HasConnectionJoin, connectionJoin))
\r
183 System.out.println("Connection join is not attached to the composite.");
\r
188 private void destructiveRule() throws DatabaseException {
\r
189 for(Resource component : g.getObjects(composite, b.ConsistsOf)) {
\r
190 if(g.hasStatement(component, mr.Mapped)) {
\r
191 //Resource element = g.getPossibleObject(component, mr.ComponentToElement);
\r
192 Collection<Resource> elements = g.getObjects(component, mr.ComponentToElement);
\r
193 if (elements.isEmpty())
\r
194 System.out.println("Component " + NameUtils.getSafeName(g, component) +
\r
195 " does not have corresponding element (but is tagged Mapped).");
\r
196 for (Resource element : elements) {
\r
197 if(!OrderedSetUtils.contains(g, diagram, element))
\r
198 System.out.println("Element corresponding to component " + NameUtils.getSafeName(g, component) +
\r
199 " is not included in the diagram.");
\r
201 for(Statement stat : g.getStatements(component, sr.IsConnectedTo)) {
\r
202 //Resource connectionRelation = stat.getPredicate();
\r
203 Resource connection = stat.getObject();
\r
204 if(g.hasStatement(connection, mr.Mapped)) {
\r
205 if (!g.hasStatement(connection, mr.ConnectionMapsTo))
\r
206 System.out.println("Connection " + NameUtils.getSafeName(g, connection) +
\r
207 " does not have a correspondence in diagram (but is tagged Mapped).");
\r