1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.issues.ui.handler;
14 import java.lang.reflect.InvocationTargetException;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.List;
22 import org.eclipse.core.commands.AbstractHandler;
23 import org.eclipse.core.commands.ExecutionEvent;
24 import org.eclipse.core.commands.ExecutionException;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.OperationCanceledException;
27 import org.eclipse.core.runtime.SubMonitor;
28 import org.eclipse.jface.operation.IRunnableWithProgress;
29 import org.eclipse.ui.PlatformUI;
30 import org.simantics.Simantics;
31 import org.simantics.SleepingDatabaseJob;
32 import org.simantics.db.Issue;
33 import org.simantics.db.ReadGraph;
34 import org.simantics.db.RequestProcessor;
35 import org.simantics.db.Resource;
36 import org.simantics.db.Session;
37 import org.simantics.db.common.request.Queries;
38 import org.simantics.db.common.request.ResourceRead;
39 import org.simantics.db.common.utils.ListUtils;
40 import org.simantics.db.exception.DatabaseException;
41 import org.simantics.db.layer0.request.PossibleActiveModel;
42 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
43 import org.simantics.db.layer0.util.SessionGarbageCollection;
44 import org.simantics.issues.common.BatchIssueSource;
45 import org.simantics.issues.common.BatchIssueValidationContext;
46 import org.simantics.issues.common.ManagedIssues;
47 import org.simantics.issues.common.SelectedModelBatchIssueSources;
48 import org.simantics.issues.ontology.IssueResource;
49 import org.simantics.issues.preferences.IssuePreferenceUtil;
50 import org.simantics.modeling.utils.BatchValidations;
51 import org.simantics.utils.ui.ExceptionUtils;
54 * @author Tuukka Lehtonen
56 public class RunActiveValidations extends AbstractHandler {
59 public Object execute(ExecutionEvent event) throws ExecutionException {
60 Runnable postValidation = null;
65 public void run(Runnable postValidation) {
67 final Session session = Simantics.getSession();
69 // 1. query for which composites to run the validation
70 final Collection<BatchIssueSource> validations = new ArrayList<>();
71 final BatchIssueValidationContext context = new BatchIssueValidationContext();
74 SleepingDatabaseJob dbLock = new SleepingDatabaseJob("Validation Preparation").scheduleAndWaitForRunning();
76 PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
78 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
80 Resource model = session.sync(new PossibleActiveModel(Simantics.getProjectResource()));
81 if(model == null) return;
83 toBatchIssueSources(session,
84 session.syncRequest(new SelectedModelBatchIssueSources(model)),
87 SubMonitor.convert(monitor, "Preparing resources for validation", 100);
88 context.contexts = Collections.singletonList(model);
89 context.domain = ModelTransferableGraphSourceRequest.getDomainOnly(session, monitor, model);
91 if (monitor.isCanceled())
92 throw new OperationCanceledException();
93 } catch (DatabaseException e) {
94 throw new InvocationTargetException(e);
101 dbLock.disposeAndJoin();
103 } catch (InvocationTargetException e) {
104 if (e.getTargetException() instanceof OperationCanceledException)
106 ExceptionUtils.logAndShowError(e.getTargetException());
108 } catch (InterruptedException e) {
109 // Operation cancelled, ignore.
113 if(!validations.isEmpty() && !context.contexts.isEmpty())
114 run(postValidation, validations, context);
118 static Collection<BatchIssueSource> toBatchIssueSources(RequestProcessor processor, Collection<Resource> sources, Collection<BatchIssueSource> result) throws DatabaseException {
119 for (Resource source : sources) {
120 BatchIssueSource bis = processor.syncRequest(Queries.adapt(source, BatchIssueSource.class, true));
127 public static void run(Runnable postValidation, final Collection<BatchIssueSource> validations, final BatchIssueValidationContext context) {
128 // Run the validations for the selected composites
129 SleepingDatabaseJob dbLock = new SleepingDatabaseJob("Validation");
131 dbLock.scheduleAndWaitForRunning();
133 PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
135 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
137 SubMonitor progress = SubMonitor.convert(monitor, "Validate Model", 100);
138 int maxWrittenIssues = IssuePreferenceUtil.getPreferences().maxBatchIssuesToWrite;
139 int writtenIssues = 0;
140 for (BatchIssueSource source : validations) {
141 Map<Resource, Set<Issue>> results = BatchValidations.validate(progress.newChild(90, SubMonitor.SUPPRESS_NONE), source, context);
142 if (progress.isCanceled())
143 throw new OperationCanceledException();
145 Collection<Resource> removed = Simantics.getSession().syncRequest(new ResourceRead<Collection<Resource>>(source.getResource()) {
147 public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
148 IssueResource ISSUE = IssueResource.getInstance(graph);
149 ArrayList<Resource> result = new ArrayList<>();
150 for (Resource issue : graph.syncRequest(new ManagedIssues(resource))) {
151 Resource list = graph.getSingleObject(issue, ISSUE.Issue_HasContexts);
152 List<Resource> l = ListUtils.toList(graph, list);
154 Resource mainContext = l.get(0);
155 if (!graph.hasStatement(mainContext))
156 result.add(mainContext);
163 for(Resource r : removed) {
164 results.put(r, Collections.<Issue>emptySet());
166 if (progress.isCanceled())
167 throw new OperationCanceledException();
169 int wroteIssues = BatchValidations.store(progress.newChild(10, SubMonitor.SUPPRESS_NONE), source.getResource(), results, Math.max(0, maxWrittenIssues - writtenIssues));
170 writtenIssues += wroteIssues;
172 // Try to keep resource consumption down.
173 SessionGarbageCollection.gc(null, Simantics.getSession(), true, null);
176 } catch (OperationCanceledException e) {
178 } catch (Exception e) {
179 throw new InvocationTargetException(e);
186 dbLock.disposeAndJoin();
189 if (postValidation != null)
190 postValidation.run();
192 } catch (OperationCanceledException e) {
193 // Operation cancelled, ignore.
194 } catch (InvocationTargetException e) {
195 ExceptionUtils.logAndShowError(e.getTargetException());
196 } catch (InterruptedException e) {
197 // Operation cancelled, ignore.