]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/validation/ValidateGraph.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graph.compiler / src / org / simantics / graph / compiler / internal / validation / ValidateGraph.java
1 package org.simantics.graph.compiler.internal.validation;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.concurrent.Callable;\r
6 \r
7 import org.simantics.graph.compiler.GraphCompilerPreferences;\r
8 import org.simantics.graph.compiler.ValidationMode;\r
9 import org.simantics.graph.compiler.internal.store.LocationStore;\r
10 import org.simantics.graph.query.CompositeGraph;\r
11 import org.simantics.graph.query.Path;\r
12 import org.simantics.graph.query.Paths;\r
13 import org.simantics.graph.query.Res;\r
14 import org.simantics.graph.store.GraphStore;\r
15 import org.simantics.graph.store.IdRes;\r
16 import org.simantics.graph.utils.GraphExecutor;\r
17 import org.simantics.ltk.Location;\r
18 import org.simantics.ltk.Problem;\r
19 \r
20 public class ValidateGraph implements Runnable {\r
21         CompositeGraph graph;\r
22         Collection<Problem> problems;\r
23         GraphStore store;\r
24         GraphCompilerPreferences preferences;\r
25         Paths paths;\r
26 \r
27         public ValidateGraph(CompositeGraph graph,\r
28                         Collection<Problem> problems,\r
29                         GraphStore store,\r
30                         GraphCompilerPreferences preferences) {\r
31                 this.graph = graph;\r
32                 this.problems = problems;\r
33                 this.store = store;\r
34                 this.preferences = preferences;\r
35                 this.paths = graph.getPaths();\r
36         }\r
37         \r
38         private void emit(int id, ValidationMode mode, String message) {\r
39                 LocationStore locations = store.getStore(LocationStore.class);\r
40                 Location location = locations.getLocation(id);\r
41                 Problem problem = new Problem(location, message);\r
42                 synchronized(problems) {                \r
43                         problems.add(problem);\r
44                 }\r
45         }\r
46 \r
47         public void validate(int id, Res resource) {\r
48                 if(!graph.rawGetObjects(resource, paths.SubrelationOf).isEmpty()) {\r
49                         for(Res res : graph.rawGetObjects(resource, paths.InverseOf)) {\r
50                                 if(resource instanceof IdRes && res instanceof Path)\r
51                                         emit(id, ValidationMode.ERROR, "Resource " + resource + " doesn't have URI but its inverse has.");\r
52                                 if(res instanceof IdRes && resource instanceof Path) {\r
53                                         emit(store.resToId(res), ValidationMode.ERROR, "Resource " + res + " doesn't have URI but its inverse has.");\r
54                                 }\r
55                         }       \r
56                 }\r
57                 else {\r
58                         if(preferences.validateResourceHasType != ValidationMode.IGNORE) {\r
59                                 if(!graph.rawGetObjects(resource, paths.InstanceOf).isEmpty()) {\r
60                                 }\r
61                                 else if(!graph.rawGetObjects(resource, paths.Inherits).isEmpty()) {                     \r
62                                 }\r
63                                 else\r
64                                         emit(id, preferences.validateResourceHasType, "Resource " + resource + " does not have a type, it has to instantiate or inherit some other resource.");\r
65                         }\r
66                 }\r
67                 /*if(preferences.validateRelationRestrictions != ValidationMode.IGNORE) {\r
68                         store.statements.forStatementsWithSubject(id, new IStatementProcedure() {\r
69                                 @Override\r
70                                 public void execute(int s, int p, int o) {\r
71                                         \r
72                                 }\r
73                         });\r
74                 }*/\r
75         }\r
76 \r
77         @Override\r
78         public void run() {\r
79                 int resourceCount = store.identities.getResourceCount();\r
80                 int segmentLength = Math.max(256, resourceCount / GraphExecutor.EXECUTOR_THREADS / 4 + 1);\r
81                 ArrayList<Callable<Object>> tasks = new ArrayList<Callable<Object>>();\r
82                 for(int segmentStart_ = 0;segmentStart_ < resourceCount;segmentStart_ += segmentLength) {\r
83                         final int segmentStart = segmentStart_;\r
84                         final int segmentEnd = Math.min(segmentStart + segmentLength, resourceCount);\r
85                         tasks.add(new Callable<Object>() {\r
86                                 @Override\r
87                                 public Object call() {\r
88                                         for(int id = segmentStart;id<segmentEnd;++id) {\r
89                                                 Res res = store.idToRes(id);\r
90                                                 if(res instanceof Path) {\r
91                                                         if(store.identities.isNewResource(id)) {\r
92                                                                 if(graph.countOccurences(res) > 1) {\r
93                                                                         emit(id, ValidationMode.ERROR, "Resource " + res + " is already defined in dependencies, but it is marked new in this graph.");\r
94                                                                         continue;\r
95                                                                 }\r
96                                                         }\r
97                                                         else {\r
98                                                                 if(graph.countOccurences(res) <= 1) {\r
99                                                                         emit(id, ValidationMode.ERROR, "Resource " + res + " is not defined in dependencies and it is not marked new in this graph.");\r
100                                                                         continue;\r
101                                                                 }\r
102                                                         }\r
103                                                 }                               \r
104                                                 validate(id, res);\r
105                                         }\r
106                                         return null;\r
107                                 }\r
108                         });                     \r
109                 }\r
110                 try {\r
111                         GraphExecutor.EXECUTOR.invokeAll(tasks);\r
112                 } catch (InterruptedException e) {\r
113                         throw new RuntimeException(e);\r
114                 }\r
115         }\r
116         \r
117 }\r