1 package org.simantics.graph.compiler.internal.validation;
\r
3 import java.util.ArrayList;
\r
4 import java.util.Collection;
\r
5 import java.util.concurrent.Callable;
\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
20 public class ValidateGraph implements Runnable {
\r
21 CompositeGraph graph;
\r
22 Collection<Problem> problems;
\r
24 GraphCompilerPreferences preferences;
\r
27 public ValidateGraph(CompositeGraph graph,
\r
28 Collection<Problem> problems,
\r
30 GraphCompilerPreferences preferences) {
\r
32 this.problems = problems;
\r
34 this.preferences = preferences;
\r
35 this.paths = graph.getPaths();
\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
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
58 if(preferences.validateResourceHasType != ValidationMode.IGNORE) {
\r
59 if(!graph.rawGetObjects(resource, paths.InstanceOf).isEmpty()) {
\r
61 else if(!graph.rawGetObjects(resource, paths.Inherits).isEmpty()) {
\r
64 emit(id, preferences.validateResourceHasType, "Resource " + resource + " does not have a type, it has to instantiate or inherit some other resource.");
\r
67 /*if(preferences.validateRelationRestrictions != ValidationMode.IGNORE) {
\r
68 store.statements.forStatementsWithSubject(id, new IStatementProcedure() {
\r
70 public void execute(int s, int p, int o) {
\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
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
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
111 GraphExecutor.EXECUTOR.invokeAll(tasks);
\r
112 } catch (InterruptedException e) {
\r
113 throw new RuntimeException(e);
\r