]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/validation/ValidateMapping.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / validation / ValidateMapping.java
1 package org.simantics.modeling.validation;
2
3 import java.util.Collection;
4
5 import org.simantics.db.ReadGraph;
6 import org.simantics.db.Resource;
7 import org.simantics.db.Statement;
8 import org.simantics.db.common.request.ReadRequest;
9 import org.simantics.db.common.utils.NameUtils;
10 import org.simantics.db.common.utils.OrderedSetUtils;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.diagram.stubs.DiagramResource;
13 import org.simantics.layer0.Layer0;
14 import org.simantics.modeling.ModelingResources;
15 import org.simantics.structural.stubs.StructuralResource2;
16
17 public class ValidateMapping extends ReadRequest {
18
19         Resource composite;
20         Resource diagram;
21         
22         ReadGraph g;
23         Layer0 b;
24         DiagramResource dr;
25         StructuralResource2 sr;
26         ModelingResources mr;   
27         
28         public ValidateMapping(Resource composite) {
29                 this.composite = composite;
30         }
31
32         @Override
33         public void run(ReadGraph g) throws DatabaseException {
34                 this.g = g;
35                 this.b = Layer0.getInstance(g);
36                 this.dr = DiagramResource.getInstance(g);
37                 this.sr = StructuralResource2.getInstance(g);
38                 this.mr = ModelingResources.getInstance(g);
39         
40                 this.diagram = g.getPossibleObject(composite, mr.CompositeToDiagram);
41                 
42                 System.out.println("------------------------------------------------------------------");
43                 System.out.println("---- " + NameUtils.getSafeName(g, composite));
44                 if (diagram == null) {
45                         System.out.println("No diagram.");
46                 } else {
47                         additiveRule();
48                         destructiveRule();
49                 }
50         }
51
52         private void additiveRule() throws DatabaseException {
53                 for(Resource element : OrderedSetUtils.toList(g, diagram)) {
54                         Resource elementType = g.getPossibleObject(element, b.InstanceOf);
55                         if(elementType == null) {
56                                 System.out.println("Element " + NameUtils.getSafeName(g, element) + " has no type.");
57                                 continue;
58                         }
59                         Resource componentType = g.getPossibleObject(elementType, mr.SymbolToComponentType);
60                         if(componentType != null)
61                                 createComponentRule(element, componentType);
62                         else if(g.isInstanceOf(element, dr.Connection))
63                                 createNormalConnectionRule(element);
64                         else if(g.isInstanceOf(element, dr.Flag))
65                                 createFlagRule(element);
66                         /*else
67                                 System.out.println("No rule for element " + NameUtils.getSafeName(g, element));
68                                 */
69                 }
70         }               
71
72         private void createComponentRule(Resource element, Resource componentType) throws DatabaseException {
73                 Collection<Resource> components = g.getObjects(element, mr.ElementToComponent);
74                 if(components.isEmpty())
75                         System.out.println("Element " + NameUtils.getSafeName(g, element) + " is not mapped.");
76                 else if(components.size() > 1)
77                         System.out.println("Element " + NameUtils.getSafeName(g, element) + " is not multiple times.");
78                 else for(Resource component : components) {
79                         if(!g.isInstanceOf(component, componentType))
80                                 System.out.println("Component " + NameUtils.getSafeName(g, component) + 
81                                                 " is not an instance of " + NameUtils.getSafeName(g, componentType));
82                         if(!g.hasStatement(component, b.PartOf, composite))
83                                 System.out.println("Component " + NameUtils.getSafeName(g, component) + " is not a part of composite.");
84                         if(!g.hasStatement(component, mr.Mapped))
85                                 System.out.println("Component " + NameUtils.getSafeName(g, component) + " is not tagged as Mapped.");
86                 }
87         }
88
89         private void createNormalConnectionRule(Resource element) throws DatabaseException {
90                 Collection<Resource> connections = g.getObjects(element, mr.DiagramConnectionToConnection);
91                 if(connections.isEmpty())
92                         System.out.println("Diagram connection " + NameUtils.getSafeName(g, element) + " is not mapped.");
93                 else if(connections.size() > 1)
94                         System.out.println("Diagram connection " + NameUtils.getSafeName(g, element) + " is not multiple times.");
95                 else for(Resource connection : connections) {
96                         if(!g.isInstanceOf(connection, sr.Connection))
97                                 System.out.println("Resource " + NameUtils.getSafeName(g, connection) + " is not a connection.");
98                         if(!g.hasStatement(connection, mr.Mapped))
99                                 System.out.println("Connection " + NameUtils.getSafeName(g, connection) + " is not tagged as Mapped.");
100                         
101                         for(Resource connector : g.getObjects(element, sr.IsConnectedTo)) {
102                                 for(Statement stat : g.getStatements(connector, sr.Connects)) {
103                                         Resource diagramConnectionRelation = g.getInverse(stat.getPredicate());
104                                         Resource cElement = stat.getObject();
105                                         Resource connectionRelation = g.getPossibleObject(diagramConnectionRelation, mr.DiagramConnectionRelationToConnectionRelation);
106                                         if(connectionRelation == null)
107                                                 continue;
108                                         Resource component = g.getPossibleObject(cElement, mr.ElementToComponent);
109                                         if(component == null)
110                                                 continue;
111                                         
112                                         // Special connection data
113                                         Resource connectionRelation2 = g.getPossibleObject(diagramConnectionRelation, mr.DiagramConnectionRelationToConnectionRelationB);
114                                         Resource connectionRelation3 = g.getPossibleObject(diagramConnectionRelation, mr.DiagramConnectionRelationToConnectionRelationC);
115                                         Resource componentType = g.getPossibleObject(diagramConnectionRelation, mr.DiagramConnectionRelationToComponentType);
116                                         
117                                         // Normal connection
118                                         if(connectionRelation2 == null || connectionRelation3 == null || componentType == null) {
119                                                 if(!g.hasStatement(component, connectionRelation, connection))
120                                                         System.out.println("Missing connection statement " + NameUtils.getSafeName(g, component) + " . " +
121                                                                         NameUtils.getSafeName(g, connectionRelation));
122                                         }
123                                         
124                                         // Special connection
125                                         else {
126                                                 Resource component2 = g.getPossibleObject(element, mr.ElementToComponent);
127                                                 if(component2 == null)
128                                                         System.out.println("Special connection " + NameUtils.getSafeName(g, element) + " is not mapped.");
129                                                 else {
130                                                         if(!g.isInstanceOf(component2, componentType))
131                                                                 System.out.println("Component " + NameUtils.getSafeName(g, component2) + 
132                                                                                 " is not an instance of " + NameUtils.getSafeName(g, componentType));
133                                                         if(!g.hasStatement(component2, b.PartOf, composite))
134                                                                 System.out.println("Component " + NameUtils.getSafeName(g, component2) + " is not a part of composite.");
135                                                         if(!g.hasStatement(component2, mr.Mapped))
136                                                                 System.out.println("Component " + NameUtils.getSafeName(g, component2) + " is not tagged as Mapped.");
137                                                         
138                                                         boolean foundConnection2 = false;
139                                                         for(Resource connection2 : g.getObjects(component2, connectionRelation2))
140                                                                 if(g.hasStatement(component, connectionRelation, connection2)) {
141                                                                         foundConnection2 = true;
142                                                                         if(!g.isInstanceOf(connection2, sr.Connection))
143                                                                                 System.out.println(NameUtils.getSafeName(g, connection2) + " is not a connection.");
144                                                                         if(!g.hasStatement(component2, connectionRelation3, connection))
145                                                                                 System.out.println("Missing connection point " + NameUtils.getSafeName(g, component2) + " . "
146                                                                                                 + NameUtils.getSafeName(g, connectionRelation3));
147                                                                 }
148                                                         if(!foundConnection2)
149                                                                 System.out.println("Special connection "+ NameUtils.getSafeName(g, element) + " is not correctly mapped (missing configuration connection).");
150                                                 }
151                                         }
152                                 }
153                         }
154                 }
155         }
156
157         /*
158                 claim(          
159                     
160                     // Create a connection
161                     exists(
162                         and(statement_bbf(Component2, ConnectionRelation2, Connection2),
163                             statement    (Component,  ConnectionRelation,  Connection2)
164                         ),
165                         Connection2
166                     ),
167                     b(sr.Connection, Connection2),
168                     //b(mapped, Connection2),
169                     
170                     // 
171                     statement(Component2, ConnectionRelation3, Connection)
172                 ),
173 }*/
174         
175         private void createFlagRule(Resource element) throws DatabaseException {
176                 for(Resource connectionJoin : g.getObjects(element, dr.FlagIsJoinedBy))
177                         for(Resource connector : g.getObjects(element, sr.IsConnectedTo))
178                                 for(Resource diagramConnection : g.getObjects(connector, sr.Connects))
179                                         for(Resource connection : g.getObjects(diagramConnection, mr.DiagramConnectionToConnection)) {
180                                                 if(!g.hasStatement(connectionJoin, sr.Joins, connection))
181                                                         System.out.println("Joins-relation of flag " + NameUtils.getSafeName(g, element) + " is not mapped.");
182                                                 if(!g.hasStatement(composite, sr.HasConnectionJoin, connectionJoin))
183                                                         System.out.println("Connection join is not attached to the composite.");
184                                         }
185                 
186         }
187         
188         private void destructiveRule() throws DatabaseException {
189                 for(Resource component : g.getObjects(composite, b.ConsistsOf)) {
190                         if(g.hasStatement(component, mr.Mapped)) {
191                                 //Resource element = g.getPossibleObject(component, mr.ComponentToElement);
192                                 Collection<Resource> elements = g.getObjects(component, mr.ComponentToElement);
193                                 if (elements.isEmpty())
194                                         System.out.println("Component " + NameUtils.getSafeName(g, component) + 
195                                                         " does not have corresponding element (but is tagged Mapped).");
196                                 for (Resource element : elements) {
197                                         if(!OrderedSetUtils.contains(g, diagram, element))
198                                                 System.out.println("Element corresponding to component " + NameUtils.getSafeName(g, component) + 
199                                                 " is not included in the diagram.");
200                                         else {
201                                                 for(Statement stat : g.getStatements(component, sr.IsConnectedTo)) {
202                                                         //Resource connectionRelation = stat.getPredicate();
203                                                         Resource connection = stat.getObject();
204                                                         if(g.hasStatement(connection, mr.Mapped)) {
205                                                                 if (!g.hasStatement(connection, mr.ConnectionMapsTo))
206                                                                         System.out.println("Connection " + NameUtils.getSafeName(g, connection) + 
207                                                                         " does not have a correspondence in diagram (but is tagged Mapped).");
208                                                         }
209                                                 }
210                                         }
211                                 }
212                         }
213                 }
214         }
215
216 }