]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.issues.ui/src/org/simantics/issues/ui/handler/RunActiveValidations.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.issues.ui / src / org / simantics / issues / ui / handler / RunActiveValidations.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.issues.ui.handler;
13
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;
19 import java.util.Map;
20 import java.util.Set;
21
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;
51
52 /**
53  * @author Tuukka Lehtonen
54  */
55 public class RunActiveValidations extends AbstractHandler {
56
57     @Override
58     public Object execute(ExecutionEvent event) throws ExecutionException {
59         Runnable postValidation = null;
60         run(postValidation);
61         return null;
62     }
63
64     public void run(Runnable postValidation) {
65
66         final Session session = Simantics.getSession();
67
68         // 1. query for which composites to run the validation
69         final Collection<BatchIssueSource> validations = new ArrayList<BatchIssueSource>();
70         final BatchIssueValidationContext context = new BatchIssueValidationContext();
71
72         try {
73             SleepingDatabaseJob dbLock = new SleepingDatabaseJob("Validation Preparation").scheduleAndWaitForRunning();
74             try {
75                 PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
76                     @Override
77                     public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
78                         try {
79                             Resource model = session.sync(new PossibleActiveModel(Simantics.getProjectResource()));
80                             if(model == null) return;
81
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));
85                                 if(bis != null)
86                                     validations.add(bis);
87                             }
88
89                             SubMonitor.convert(monitor, "Preparing resources for validation", 100);
90                             context.contexts = Collections.singletonList(model);
91                             context.domain = ModelTransferableGraphSourceRequest.getDomainOnly(session, monitor, model);
92
93                             if (monitor.isCanceled())
94                                 throw new OperationCanceledException();
95                         } catch (DatabaseException e) {
96                             throw new InvocationTargetException(e);
97                         } finally {
98                             monitor.done();
99                         }
100                     }
101                 });
102             } finally {
103                 dbLock.disposeAndJoin();
104             }
105         } catch (InvocationTargetException e) {
106             if (e.getTargetException() instanceof OperationCanceledException)
107                 return;
108             ExceptionUtils.logAndShowError(e.getTargetException());
109             return;
110         } catch (InterruptedException e) {
111             // Operation cancelled, ignore.
112             return;
113         }
114         
115         if(!validations.isEmpty() && !context.contexts.isEmpty())
116             run(postValidation, validations, context);
117
118     }
119
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");
123         try {
124             dbLock.scheduleAndWaitForRunning();
125             try {
126                 PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() {
127                     @Override
128                     public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
129                         try {
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();
137
138                                 Collection<Resource> removed = Simantics.getSession().syncRequest(new ResourceRead<Collection<Resource>>(source.getResource()) {
139                                     @Override
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);
146                                             if (l.size() > 0) {
147                                                 Resource mainContext = l.get(0); 
148                                                 if (!graph.hasStatement(mainContext))
149                                                     result.add(mainContext);
150                                             }
151                                         }
152                                         return result;
153                                     }
154                                 });
155
156                                 for(Resource r : removed) {
157                                     results.put(r, Collections.<Issue>emptySet());
158                                 }
159                                 if (progress.isCanceled())
160                                     throw new OperationCanceledException();
161
162                                 int wroteIssues = BatchValidations.store(progress.newChild(10, SubMonitor.SUPPRESS_NONE), source.getResource(), results, Math.max(0, maxWrittenIssues - writtenIssues));
163                                 writtenIssues += wroteIssues;
164
165                                 // Try to keep resource consumption down.
166                                 SessionGarbageCollection.gc(null, Simantics.getSession(), true, null);
167
168                             }
169                         } catch (OperationCanceledException e) {
170                                 throw e;
171                         } catch (Exception e) {
172                             throw new InvocationTargetException(e);
173                         } finally {
174                             monitor.done();
175                         }
176                     }
177                 });
178             } finally {
179                 dbLock.disposeAndJoin();
180             }
181
182             if (postValidation != null)
183                 postValidation.run();
184
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.
191         }
192     }
193
194 }