]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/handler/RunActiveValidations.java
Merge changes I78c3a258,I7bf72f04
[simantics/platform.git] / bundles / org.simantics.issues.ui / src / org / simantics / issues / ui / handler / RunActiveValidations.java
index f44c3a8dcfc2d25086cbafd134fb2f86e7e80f41..f6121ce19ec6179d824a98ffed7a5493cf4a2a3e 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2011 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.issues.ui.handler;\r
-\r
-import java.lang.reflect.InvocationTargetException;\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Set;\r
-\r
-import org.eclipse.core.commands.AbstractHandler;\r
-import org.eclipse.core.commands.ExecutionEvent;\r
-import org.eclipse.core.commands.ExecutionException;\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.core.runtime.OperationCanceledException;\r
-import org.eclipse.core.runtime.SubMonitor;\r
-import org.eclipse.jface.operation.IRunnableWithProgress;\r
-import org.eclipse.ui.PlatformUI;\r
-import org.simantics.Simantics;\r
-import org.simantics.SleepingDatabaseJob;\r
-import org.simantics.db.Issue;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.common.request.Queries;\r
-import org.simantics.db.common.request.ResourceRead;\r
-import org.simantics.db.common.utils.ListUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.request.PossibleActiveModel;\r
-import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;\r
-import org.simantics.db.layer0.util.SessionGarbageCollection;\r
-import org.simantics.issues.common.BatchIssueSource;\r
-import org.simantics.issues.common.BatchIssueValidationContext;\r
-import org.simantics.issues.common.ManagedIssues;\r
-import org.simantics.issues.common.SelectedModelBatchIssueSources;\r
-import org.simantics.issues.ontology.IssueResource;\r
-import org.simantics.issues.preferences.IssuePreferenceUtil;\r
-import org.simantics.modeling.utils.BatchValidations;\r
-import org.simantics.utils.ui.ExceptionUtils;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class RunActiveValidations extends AbstractHandler {\r
-\r
-    @Override\r
-    public Object execute(ExecutionEvent event) throws ExecutionException {\r
-        Runnable postValidation = null;\r
-        run(postValidation);\r
-        return null;\r
-    }\r
-\r
-    public void run(Runnable postValidation) {\r
-\r
-        final Session session = Simantics.getSession();\r
-\r
-        // 1. query for which composites to run the validation\r
-        final Collection<BatchIssueSource> validations = new ArrayList<BatchIssueSource>();\r
-        final BatchIssueValidationContext context = new BatchIssueValidationContext();\r
-\r
-        try {\r
-            SleepingDatabaseJob dbLock = new SleepingDatabaseJob("Validation Preparation").scheduleAndWaitForRunning();\r
-            try {\r
-                PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {\r
-                    @Override\r
-                    public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
-                        try {\r
-                            Resource model = session.sync(new PossibleActiveModel(Simantics.getProjectResource()));\r
-                            if(model == null) return;\r
-\r
-                            Collection<Resource> activeSources = session.syncRequest(new SelectedModelBatchIssueSources(model));\r
-                            for(Resource source : activeSources) {\r
-                                BatchIssueSource bis = session.syncRequest(Queries.adapt(source, BatchIssueSource.class, true));\r
-                                if(bis != null)\r
-                                    validations.add(bis);\r
-                            }\r
-\r
-                            SubMonitor.convert(monitor, "Preparing resources for validation", 100);\r
-                            context.contexts = Collections.singletonList(model);\r
-                            context.domain = ModelTransferableGraphSourceRequest.getDomainOnly(session, monitor, model);\r
-\r
-                            if (monitor.isCanceled())\r
-                                throw new OperationCanceledException();\r
-                        } catch (DatabaseException e) {\r
-                            throw new InvocationTargetException(e);\r
-                        } finally {\r
-                            monitor.done();\r
-                        }\r
-                    }\r
-                });\r
-            } finally {\r
-                dbLock.disposeAndJoin();\r
-            }\r
-        } catch (InvocationTargetException e) {\r
-            if (e.getTargetException() instanceof OperationCanceledException)\r
-                return;\r
-            ExceptionUtils.logAndShowError(e.getTargetException());\r
-            return;\r
-        } catch (InterruptedException e) {\r
-            // Operation cancelled, ignore.\r
-            return;\r
-        }\r
-        \r
-        if(!validations.isEmpty() && !context.contexts.isEmpty())\r
-            run(postValidation, validations, context);\r
-\r
-    }\r
-\r
-    public static void run(Runnable postValidation, final Collection<BatchIssueSource> validations, final BatchIssueValidationContext context) {\r
-        // Run the validations for the selected composites\r
-        SleepingDatabaseJob dbLock = new SleepingDatabaseJob("Validation");\r
-        try {\r
-            dbLock.scheduleAndWaitForRunning();\r
-            try {\r
-                PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {\r
-                    @Override\r
-                    public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
-                        try {\r
-                            SubMonitor progress = SubMonitor.convert(monitor, "Validate Model", 100);\r
-                            int maxWrittenIssues = IssuePreferenceUtil.getPreferences().maxBatchIssuesToWrite;\r
-                            int writtenIssues = 0;\r
-                            for (BatchIssueSource source : validations) {\r
-                                Map<Resource, Set<Issue>> results = BatchValidations.validate(progress.newChild(90, SubMonitor.SUPPRESS_NONE), source, context);\r
-                                if (progress.isCanceled())\r
-                                    throw new OperationCanceledException();\r
-\r
-                                Collection<Resource> removed = Simantics.getSession().syncRequest(new ResourceRead<Collection<Resource>>(source.getResource()) {\r
-                                    @Override\r
-                                    public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {\r
-                                        IssueResource ISSUE = IssueResource.getInstance(graph);\r
-                                        ArrayList<Resource> result = new ArrayList<Resource>();\r
-                                        for (Resource issue : graph.syncRequest(new ManagedIssues(resource))) {\r
-                                            Resource list = graph.getSingleObject(issue, ISSUE.Issue_HasContexts);\r
-                                            List<Resource> l = ListUtils.toList(graph, list);\r
-                                            if (l.size() > 0) {\r
-                                                Resource mainContext = l.get(0); \r
-                                                if (!graph.hasStatement(mainContext))\r
-                                                    result.add(mainContext);\r
-                                            }\r
-                                        }\r
-                                        return result;\r
-                                    }\r
-                                });\r
-\r
-                                for(Resource r : removed) {\r
-                                    results.put(r, Collections.<Issue>emptySet());\r
-                                }\r
-                                if (progress.isCanceled())\r
-                                    throw new OperationCanceledException();\r
-\r
-                                int wroteIssues = BatchValidations.store(progress.newChild(10, SubMonitor.SUPPRESS_NONE), source.getResource(), results, Math.max(0, maxWrittenIssues - writtenIssues));\r
-                                writtenIssues += wroteIssues;\r
-\r
-                                // Try to keep resource consumption down.\r
-                                SessionGarbageCollection.gc(null, Simantics.getSession(), true, null);\r
-\r
-                            }\r
-                        } catch (OperationCanceledException e) {\r
-                               throw e;\r
-                        } catch (Exception e) {\r
-                            throw new InvocationTargetException(e);\r
-                        } finally {\r
-                            monitor.done();\r
-                        }\r
-                    }\r
-                });\r
-            } finally {\r
-                dbLock.disposeAndJoin();\r
-            }\r
-\r
-            if (postValidation != null)\r
-                postValidation.run();\r
-\r
-        } catch (OperationCanceledException e) {\r
-            // Operation cancelled, ignore.\r
-        } catch (InvocationTargetException e) {\r
-            ExceptionUtils.logAndShowError(e.getTargetException());\r
-        } catch (InterruptedException e) {\r
-            // Operation cancelled, ignore.\r
-        }\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2019 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *     Semantum Oy - Reorganization
+ *******************************************************************************/
+package org.simantics.issues.ui.handler;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.core.runtime.SubMonitor;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.ui.PlatformUI;
+import org.simantics.Simantics;
+import org.simantics.SleepingDatabaseJob;
+import org.simantics.db.Issue;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.RequestProcessor;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.common.request.Queries;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.utils.ListUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.request.PossibleActiveModel;
+import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
+import org.simantics.db.layer0.util.SessionGarbageCollection;
+import org.simantics.issues.common.BatchIssueSource;
+import org.simantics.issues.common.BatchIssueValidationContext;
+import org.simantics.issues.common.ManagedIssues;
+import org.simantics.issues.common.SelectedModelBatchIssueSources;
+import org.simantics.issues.ontology.IssueResource;
+import org.simantics.issues.preferences.IssuePreferenceUtil;
+import org.simantics.modeling.utils.BatchValidations;
+import org.simantics.utils.ui.ExceptionUtils;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class RunActiveValidations extends AbstractHandler {
+
+    @Override
+    public Object execute(ExecutionEvent event) throws ExecutionException {
+        try {
+            run();
+        } catch (DatabaseException e) {
+            throw new ExecutionException("Error while running active validations", e);
+        }
+        return null;
+    }
+
+    public static void run() throws DatabaseException {
+        Resource model = Simantics.getSession().syncRequest(new PossibleActiveModel(Simantics.getProjectResource()));
+        if(model != null)
+            run(model);
+    }
+
+    public static void run(Resource model) {
+        run(model, null);
+    }
+
+    public static void run(Resource model, Runnable postValidation) {
+
+        final Session session = Simantics.getSession();
+
+        // 1. query for which composites to run the validation
+        final Collection<BatchIssueSource> validations = new ArrayList<>();
+        final BatchIssueValidationContext context = new BatchIssueValidationContext();
+
+        try {
+            SleepingDatabaseJob dbLock = new SleepingDatabaseJob(Messages.RunActiveValidations_ValidationPreparation).scheduleAndWaitForRunning();
+            try {
+                PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
+                    @Override
+                    public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
+                        try {
+
+                            toBatchIssueSources(session,
+                                    session.syncRequest(new SelectedModelBatchIssueSources(model)),
+                                    validations);
+
+                            SubMonitor.convert(monitor, Messages.RunActiveValidations_MonitorPreparingResourcesForValidation, 100);
+                            context.contexts = Collections.singletonList(model);
+                            context.domain = ModelTransferableGraphSourceRequest.getDomainOnly(session, monitor, model);
+
+                            if (monitor.isCanceled())
+                                throw new OperationCanceledException();
+                        } catch (DatabaseException e) {
+                            throw new InvocationTargetException(e);
+                        } finally {
+                            monitor.done();
+                        }
+                    }
+                });
+            } finally {
+                dbLock.disposeAndJoin();
+            }
+        } catch (InvocationTargetException e) {
+            if (e.getTargetException() instanceof OperationCanceledException)
+                return;
+            ExceptionUtils.logAndShowError(e.getTargetException());
+            return;
+        } catch (InterruptedException e) {
+            // Operation cancelled, ignore.
+            return;
+        }
+
+        if(!validations.isEmpty() && !context.contexts.isEmpty())
+            run(postValidation, validations, context);
+
+    }
+
+    static Collection<BatchIssueSource> toBatchIssueSources(RequestProcessor processor, Collection<Resource> sources, Collection<BatchIssueSource> result) throws DatabaseException {
+        for (Resource source : sources) {
+            BatchIssueSource bis = processor.syncRequest(Queries.adapt(source, BatchIssueSource.class, true));
+            if (bis != null)
+                result.add(bis);
+        }
+        return result;
+    }
+
+    public static void run(Runnable postValidation, final Collection<BatchIssueSource> validations, final BatchIssueValidationContext context) {
+        // Run the validations for the selected composites
+        SleepingDatabaseJob dbLock = new SleepingDatabaseJob(Messages.RunActiveValidations_Validation);
+        try {
+            dbLock.scheduleAndWaitForRunning();
+            try {
+                PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
+                    @Override
+                    public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
+                        try {
+                            SubMonitor progress = SubMonitor.convert(monitor, Messages.RunActiveValidations_ValidateModel, 100);
+                            int maxWrittenIssues = IssuePreferenceUtil.getPreferences().maxBatchIssuesToWrite;
+                            int writtenIssues = 0;
+                            for (BatchIssueSource source : validations) {
+                                Map<Resource, Set<Issue>> results = BatchValidations.validate(progress.newChild(90, SubMonitor.SUPPRESS_NONE), source, context);
+                                if (progress.isCanceled())
+                                    throw new OperationCanceledException();
+
+                                Collection<Resource> removed = Simantics.getSession().syncRequest(new ResourceRead<Collection<Resource>>(source.getResource()) {
+                                    @Override
+                                    public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
+                                        IssueResource ISSUE = IssueResource.getInstance(graph);
+                                        ArrayList<Resource> result = new ArrayList<>();
+                                        for (Resource issue : graph.syncRequest(new ManagedIssues(resource))) {
+                                            Resource list = graph.getSingleObject(issue, ISSUE.Issue_HasContexts);
+                                            List<Resource> l = ListUtils.toList(graph, list);
+                                            if (l.size() > 0) {
+                                                Resource mainContext = l.get(0);
+                                                if (!BatchValidations.isLinkedToOtherThan(graph, mainContext, issue))
+                                                    result.add(mainContext);
+                                            }
+                                        }
+                                        return result;
+                                    }
+                                });
+
+                                for(Resource r : removed) {
+                                    results.put(r, Collections.<Issue>emptySet());
+                                }
+                                if (progress.isCanceled())
+                                    throw new OperationCanceledException();
+
+                                int wroteIssues = BatchValidations.store(progress.newChild(10, SubMonitor.SUPPRESS_NONE), source.getResource(), results, Math.max(0, maxWrittenIssues - writtenIssues));
+                                writtenIssues += wroteIssues;
+
+                                // Try to keep resource consumption down.
+                                SessionGarbageCollection.gc(null, Simantics.getSession(), true, null);
+
+                            }
+                        } catch (OperationCanceledException e) {
+                            throw e;
+                        } catch (Exception e) {
+                            throw new InvocationTargetException(e);
+                        } finally {
+                            monitor.done();
+                        }
+                    }
+                });
+            } finally {
+                dbLock.disposeAndJoin();
+            }
+
+            if (postValidation != null)
+                postValidation.run();
+
+        } catch (OperationCanceledException e) {
+            // Operation cancelled, ignore.
+        } catch (InvocationTargetException e) {
+            ExceptionUtils.logAndShowError(e.getTargetException());
+        } catch (InterruptedException e) {
+            // Operation cancelled, ignore.
+        }
+    }
+
+}