--- /dev/null
+package org.simantics.graph.compiler.internal.validation;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.concurrent.Callable;\r
+\r
+import org.simantics.graph.compiler.GraphCompilerPreferences;\r
+import org.simantics.graph.compiler.ValidationMode;\r
+import org.simantics.graph.compiler.internal.store.LocationStore;\r
+import org.simantics.graph.query.CompositeGraph;\r
+import org.simantics.graph.query.Path;\r
+import org.simantics.graph.query.Paths;\r
+import org.simantics.graph.query.Res;\r
+import org.simantics.graph.store.GraphStore;\r
+import org.simantics.graph.store.IdRes;\r
+import org.simantics.graph.utils.GraphExecutor;\r
+import org.simantics.ltk.Location;\r
+import org.simantics.ltk.Problem;\r
+\r
+public class ValidateGraph implements Runnable {\r
+ CompositeGraph graph;\r
+ Collection<Problem> problems;\r
+ GraphStore store;\r
+ GraphCompilerPreferences preferences;\r
+ Paths paths;\r
+\r
+ public ValidateGraph(CompositeGraph graph,\r
+ Collection<Problem> problems,\r
+ GraphStore store,\r
+ GraphCompilerPreferences preferences) {\r
+ this.graph = graph;\r
+ this.problems = problems;\r
+ this.store = store;\r
+ this.preferences = preferences;\r
+ this.paths = graph.getPaths();\r
+ }\r
+ \r
+ private void emit(int id, ValidationMode mode, String message) {\r
+ LocationStore locations = store.getStore(LocationStore.class);\r
+ Location location = locations.getLocation(id);\r
+ Problem problem = new Problem(location, message);\r
+ synchronized(problems) { \r
+ problems.add(problem);\r
+ }\r
+ }\r
+\r
+ public void validate(int id, Res resource) {\r
+ if(!graph.rawGetObjects(resource, paths.SubrelationOf).isEmpty()) {\r
+ for(Res res : graph.rawGetObjects(resource, paths.InverseOf)) {\r
+ if(resource instanceof IdRes && res instanceof Path)\r
+ emit(id, ValidationMode.ERROR, "Resource " + resource + " doesn't have URI but its inverse has.");\r
+ if(res instanceof IdRes && resource instanceof Path) {\r
+ emit(store.resToId(res), ValidationMode.ERROR, "Resource " + res + " doesn't have URI but its inverse has.");\r
+ }\r
+ } \r
+ }\r
+ else {\r
+ if(preferences.validateResourceHasType != ValidationMode.IGNORE) {\r
+ if(!graph.rawGetObjects(resource, paths.InstanceOf).isEmpty()) {\r
+ }\r
+ else if(!graph.rawGetObjects(resource, paths.Inherits).isEmpty()) { \r
+ }\r
+ else\r
+ emit(id, preferences.validateResourceHasType, "Resource " + resource + " does not have a type, it has to instantiate or inherit some other resource.");\r
+ }\r
+ }\r
+ /*if(preferences.validateRelationRestrictions != ValidationMode.IGNORE) {\r
+ store.statements.forStatementsWithSubject(id, new IStatementProcedure() {\r
+ @Override\r
+ public void execute(int s, int p, int o) {\r
+ \r
+ }\r
+ });\r
+ }*/\r
+ }\r
+\r
+ @Override\r
+ public void run() {\r
+ int resourceCount = store.identities.getResourceCount();\r
+ int segmentLength = Math.max(256, resourceCount / GraphExecutor.EXECUTOR_THREADS / 4 + 1);\r
+ ArrayList<Callable<Object>> tasks = new ArrayList<Callable<Object>>();\r
+ for(int segmentStart_ = 0;segmentStart_ < resourceCount;segmentStart_ += segmentLength) {\r
+ final int segmentStart = segmentStart_;\r
+ final int segmentEnd = Math.min(segmentStart + segmentLength, resourceCount);\r
+ tasks.add(new Callable<Object>() {\r
+ @Override\r
+ public Object call() {\r
+ for(int id = segmentStart;id<segmentEnd;++id) {\r
+ Res res = store.idToRes(id);\r
+ if(res instanceof Path) {\r
+ if(store.identities.isNewResource(id)) {\r
+ if(graph.countOccurences(res) > 1) {\r
+ emit(id, ValidationMode.ERROR, "Resource " + res + " is already defined in dependencies, but it is marked new in this graph.");\r
+ continue;\r
+ }\r
+ }\r
+ else {\r
+ if(graph.countOccurences(res) <= 1) {\r
+ emit(id, ValidationMode.ERROR, "Resource " + res + " is not defined in dependencies and it is not marked new in this graph.");\r
+ continue;\r
+ }\r
+ }\r
+ } \r
+ validate(id, res);\r
+ }\r
+ return null;\r
+ }\r
+ }); \r
+ }\r
+ try {\r
+ GraphExecutor.EXECUTOR.invokeAll(tasks);\r
+ } catch (InterruptedException e) {\r
+ throw new RuntimeException(e);\r
+ }\r
+ }\r
+ \r
+}\r