X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.graph.compiler%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fcompiler%2Finternal%2Fvalidation%2FValidateGraph.java;fp=bundles%2Forg.simantics.graph.compiler%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fcompiler%2Finternal%2Fvalidation%2FValidateGraph.java;h=f30c82a24ed2097e5e6d7cddf21d058637dc8f8f;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/validation/ValidateGraph.java b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/validation/ValidateGraph.java new file mode 100644 index 000000000..f30c82a24 --- /dev/null +++ b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/validation/ValidateGraph.java @@ -0,0 +1,117 @@ +package org.simantics.graph.compiler.internal.validation; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.concurrent.Callable; + +import org.simantics.graph.compiler.GraphCompilerPreferences; +import org.simantics.graph.compiler.ValidationMode; +import org.simantics.graph.compiler.internal.store.LocationStore; +import org.simantics.graph.query.CompositeGraph; +import org.simantics.graph.query.Path; +import org.simantics.graph.query.Paths; +import org.simantics.graph.query.Res; +import org.simantics.graph.store.GraphStore; +import org.simantics.graph.store.IdRes; +import org.simantics.graph.utils.GraphExecutor; +import org.simantics.ltk.Location; +import org.simantics.ltk.Problem; + +public class ValidateGraph implements Runnable { + CompositeGraph graph; + Collection problems; + GraphStore store; + GraphCompilerPreferences preferences; + Paths paths; + + public ValidateGraph(CompositeGraph graph, + Collection problems, + GraphStore store, + GraphCompilerPreferences preferences) { + this.graph = graph; + this.problems = problems; + this.store = store; + this.preferences = preferences; + this.paths = graph.getPaths(); + } + + private void emit(int id, ValidationMode mode, String message) { + LocationStore locations = store.getStore(LocationStore.class); + Location location = locations.getLocation(id); + Problem problem = new Problem(location, message); + synchronized(problems) { + problems.add(problem); + } + } + + public void validate(int id, Res resource) { + if(!graph.rawGetObjects(resource, paths.SubrelationOf).isEmpty()) { + for(Res res : graph.rawGetObjects(resource, paths.InverseOf)) { + if(resource instanceof IdRes && res instanceof Path) + emit(id, ValidationMode.ERROR, "Resource " + resource + " doesn't have URI but its inverse has."); + if(res instanceof IdRes && resource instanceof Path) { + emit(store.resToId(res), ValidationMode.ERROR, "Resource " + res + " doesn't have URI but its inverse has."); + } + } + } + else { + if(preferences.validateResourceHasType != ValidationMode.IGNORE) { + if(!graph.rawGetObjects(resource, paths.InstanceOf).isEmpty()) { + } + else if(!graph.rawGetObjects(resource, paths.Inherits).isEmpty()) { + } + else + emit(id, preferences.validateResourceHasType, "Resource " + resource + " does not have a type, it has to instantiate or inherit some other resource."); + } + } + /*if(preferences.validateRelationRestrictions != ValidationMode.IGNORE) { + store.statements.forStatementsWithSubject(id, new IStatementProcedure() { + @Override + public void execute(int s, int p, int o) { + + } + }); + }*/ + } + + @Override + public void run() { + int resourceCount = store.identities.getResourceCount(); + int segmentLength = Math.max(256, resourceCount / GraphExecutor.EXECUTOR_THREADS / 4 + 1); + ArrayList> tasks = new ArrayList>(); + for(int segmentStart_ = 0;segmentStart_ < resourceCount;segmentStart_ += segmentLength) { + final int segmentStart = segmentStart_; + final int segmentEnd = Math.min(segmentStart + segmentLength, resourceCount); + tasks.add(new Callable() { + @Override + public Object call() { + for(int id = segmentStart;id 1) { + emit(id, ValidationMode.ERROR, "Resource " + res + " is already defined in dependencies, but it is marked new in this graph."); + continue; + } + } + else { + if(graph.countOccurences(res) <= 1) { + emit(id, ValidationMode.ERROR, "Resource " + res + " is not defined in dependencies and it is not marked new in this graph."); + continue; + } + } + } + validate(id, res); + } + return null; + } + }); + } + try { + GraphExecutor.EXECUTOR.invokeAll(tasks); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + +}