--- /dev/null
+package org.simantics.issues.common;\r
+\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.SubMonitor;\r
+import org.simantics.db.Issue;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.QueryMemoryWatcher;\r
+import org.simantics.db.common.primitiverequest.IsInheritedFrom;\r
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;\r
+import org.simantics.db.common.utils.Functions;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest.DomainOnlyProcessor;\r
+import org.simantics.db.service.QueryControl;\r
+import org.simantics.issues.ontology.IssueResource;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public class DependencyTrackerBatchIssueSource implements BatchIssueSource {\r
+\r
+ final Resource source;\r
+ \r
+ public DependencyTrackerBatchIssueSource(Resource source) {\r
+ this.source = source;\r
+ }\r
+ \r
+ @Override\r
+ public Map<Resource, Set<Issue>> run(IProgressMonitor monitor, ReadGraph graph, BatchIssueValidationContext context) throws DatabaseException {\r
+ \r
+ SubMonitor progress = SubMonitor.convert(monitor);\r
+\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ IssueResource IR = IssueResource.getInstance(graph);\r
+ \r
+ Resource constraint = graph.getSingleObject(source, IR.Sources_DependencyTracker_HasConstraint);\r
+ Resource type = graph.getSingleObject(constraint, L0.HasConstraint_Inverse);\r
+ Resource function = graph.getSingleObject(constraint, L0.Constraint_Validator);\r
+ \r
+ String sourceName = NameUtils.getSafeName(graph, source);\r
+\r
+ QueryControl qc = graph.getService(QueryControl.class);\r
+ qc.flush(graph);\r
+\r
+ progress.subTask(sourceName);\r
+ progress.worked(1);\r
+ \r
+ HashMap<Resource, Set<Issue>> result = new HashMap<Resource, Set<Issue>>();\r
+ \r
+ // Allow this process to make 50k queries\r
+ QueryMemoryWatcher memory = new QueryMemoryWatcher(graph, 50000, 0.5, 300);\r
+\r
+ int number = 0;\r
+ \r
+ DomainOnlyProcessor domain = context.domain;\r
+\r
+ for(int i=0;i<domain.internals.size();i++) {\r
+\r
+ Resource r = domain.internals.get(i);\r
+ Resource singleType = domain.internalTypes.get(i);\r
+\r
+ if (number % 1000 == 0) {\r
+ if(progress.isCanceled()) return Collections.emptyMap();\r
+ memory.maintain();\r
+ progress.subTask(sourceName + " " + (100*number)/domain.internals.size() + "% done.");\r
+ }\r
+ ++number;\r
+\r
+\r
+ if(singleType != null) {\r
+ if(!graph.syncRequest(new IsInheritedFrom(singleType, type), TransientCacheAsyncListener.<Boolean>instance())) continue;\r
+ } else {\r
+ if(!graph.isInstanceOf(r, type)) continue;\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ List<Issue> is = (List<Issue>)Functions.exec(graph, function, graph, r);\r
+ if(is.isEmpty())\r
+ result.put(r, Collections.<Issue>emptySet());\r
+ else\r
+ result.put(r, new HashSet<Issue>(is));\r
+\r
+ }\r
+\r
+ return result;\r
+\r
+ }\r
+\r
+ @Override\r
+ public Resource getResource() {\r
+ return source;\r
+ }\r
+\r
+}\r