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.Resource;
35 import org.simantics.db.Session;
36 import org.simantics.db.common.request.Queries;
37 import org.simantics.db.common.request.ResourceRead;
38 import org.simantics.db.common.utils.ListUtils;
39 import org.simantics.db.exception.DatabaseException;
40 import org.simantics.db.layer0.request.PossibleActiveModel;
41 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
42 import org.simantics.db.layer0.util.SessionGarbageCollection;
43 import org.simantics.issues.common.BatchIssueSource;
44 import org.simantics.issues.common.BatchIssueValidationContext;
45 import org.simantics.issues.common.ManagedIssues;
46 import org.simantics.issues.common.SelectedModelBatchIssueSources;
47 import org.simantics.issues.ontology.IssueResource;
48 import org.simantics.issues.preferences.IssuePreferenceUtil;
49 import org.simantics.modeling.utils.BatchValidations;
50 import org.simantics.utils.ui.ExceptionUtils;
53 * @author Tuukka Lehtonen
55 public class RunActiveValidations extends AbstractHandler {
58 public Object execute(ExecutionEvent event) throws ExecutionException {
59 Runnable postValidation = null;
64 public void run(Runnable postValidation) {
66 final Session session = Simantics.getSession();
68 // 1. query for which composites to run the validation
69 final Collection<BatchIssueSource> validations = new ArrayList<BatchIssueSource>();
70 final BatchIssueValidationContext context = new BatchIssueValidationContext();
73 SleepingDatabaseJob dbLock = new SleepingDatabaseJob("Validation Preparation").scheduleAndWaitForRunning();
75 PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
77 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
79 Resource model = session.sync(new PossibleActiveModel(Simantics.getProjectResource()));
80 if(model == null) return;
82 Collection<Resource> activeSources = session.syncRequest(new SelectedModelBatchIssueSources(model));
83 for(Resource source : activeSources) {
84 BatchIssueSource bis = session.syncRequest(Queries.adapt(source, BatchIssueSource.class, true));
89 SubMonitor.convert(monitor, "Preparing resources for validation", 100);
90 context.contexts = Collections.singletonList(model);
91 context.domain = ModelTransferableGraphSourceRequest.getDomainOnly(session, monitor, model);
93 if (monitor.isCanceled())
94 throw new OperationCanceledException();
95 } catch (DatabaseException e) {
96 throw new InvocationTargetException(e);
103 dbLock.disposeAndJoin();
105 } catch (InvocationTargetException e) {
106 if (e.getTargetException() instanceof OperationCanceledException)
108 ExceptionUtils.logAndShowError(e.getTargetException());
110 } catch (InterruptedException e) {
111 // Operation cancelled, ignore.
115 if(!validations.isEmpty() && !context.contexts.isEmpty())
116 run(postValidation, validations, context);
120 public static void run(Runnable postValidation, final Collection<BatchIssueSource> validations, final BatchIssueValidationContext context) {
121 // Run the validations for the selected composites
122 SleepingDatabaseJob dbLock = new SleepingDatabaseJob("Validation");
124 dbLock.scheduleAndWaitForRunning();
126 PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
128 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
130 SubMonitor progress = SubMonitor.convert(monitor, "Validate Model", 100);
131 int maxWrittenIssues = IssuePreferenceUtil.getPreferences().maxBatchIssuesToWrite;
132 int writtenIssues = 0;
133 for (BatchIssueSource source : validations) {
134 Map<Resource, Set<Issue>> results = BatchValidations.validate(progress.newChild(90, SubMonitor.SUPPRESS_NONE), source, context);
135 if (progress.isCanceled())
136 throw new OperationCanceledException();
138 Collection<Resource> removed = Simantics.getSession().syncRequest(new ResourceRead<Collection<Resource>>(source.getResource()) {
140 public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
141 IssueResource ISSUE = IssueResource.getInstance(graph);
142 ArrayList<Resource> result = new ArrayList<Resource>();
143 for (Resource issue : graph.syncRequest(new ManagedIssues(resource))) {
144 Resource list = graph.getSingleObject(issue, ISSUE.Issue_HasContexts);
145 List<Resource> l = ListUtils.toList(graph, list);
147 Resource mainContext = l.get(0);
148 if (!graph.hasStatement(mainContext))
149 result.add(mainContext);
156 for(Resource r : removed) {
157 results.put(r, Collections.<Issue>emptySet());
159 if (progress.isCanceled())
160 throw new OperationCanceledException();
162 int wroteIssues = BatchValidations.store(progress.newChild(10, SubMonitor.SUPPRESS_NONE), source.getResource(), results, Math.max(0, maxWrittenIssues - writtenIssues));
163 writtenIssues += wroteIssues;
165 // Try to keep resource consumption down.
166 SessionGarbageCollection.gc(null, Simantics.getSession(), true, null);
169 } catch (OperationCanceledException e) {
171 } catch (Exception e) {
172 throw new InvocationTargetException(e);
179 dbLock.disposeAndJoin();
182 if (postValidation != null)
183 postValidation.run();
185 } catch (OperationCanceledException e) {
186 // Operation cancelled, ignore.
187 } catch (InvocationTargetException e) {
188 ExceptionUtils.logAndShowError(e.getTargetException());
189 } catch (InterruptedException e) {
190 // Operation cancelled, ignore.