]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.event/src/org/simantics/event/view/contribution/ProjectEventsRule.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.event / src / org / simantics / event / view / contribution / ProjectEventsRule.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.event.view.contribution;
13
14 import gnu.trove.map.hash.TObjectDoubleHashMap;
15
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.Comparator;
20 import java.util.List;
21
22 import org.simantics.browsing.ui.model.children.ChildRule;
23 import org.simantics.databoard.Bindings;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.common.request.ObjectsWithType;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.service.QueryControl;
29 import org.simantics.event.ontology.EventResource;
30 import org.simantics.event.view.Constants;
31 import org.simantics.event.view.preference.EventPrefs;
32 import org.simantics.layer0.Layer0;
33 import org.simantics.operation.Layer0X;
34 import org.simantics.simulation.ontology.SimulationResource;
35
36 /**
37  * A child rule that looks for all event logs within the active models of a
38  * project. The rule takes into account the preference setting for showing hidden
39  * event logs.
40  * 
41  * @author Tuukka Lehtonen
42  */
43 public enum ProjectEventsRule implements ChildRule {
44
45     INSTANCE;
46
47     public static ProjectEventsRule get() {
48         return INSTANCE;
49     }
50
51     private static class Prefs {
52         public final boolean hideInfoEvents;
53         public final boolean hideWarningEvents;
54         public final boolean hideReturnEvents;
55         public final boolean showHiddenEvents;
56         public final boolean showOnlyMilestones;
57         public final boolean showOnlyActiveEvents;
58         public Prefs(
59                 boolean hideInfoEvents,
60                 boolean hideWarningEvents,
61                 boolean hideReturnEvents,
62                 boolean showHiddenEvents,
63                 boolean showOnlyMilestones,
64                 boolean showOnlyActiveEvents) {
65             this.hideInfoEvents = hideInfoEvents;
66             this.hideWarningEvents = hideWarningEvents;
67             this.hideReturnEvents = hideReturnEvents;
68             this.showHiddenEvents = showHiddenEvents;
69             this.showOnlyMilestones = showOnlyMilestones;
70             this.showOnlyActiveEvents = showOnlyActiveEvents;
71         }
72
73         public static Prefs read(ReadGraph graph, Resource project) throws DatabaseException {
74             return new Prefs(
75                     EventPrefs.hideInfoEvents(graph, project),
76                     EventPrefs.hideWarningEvents(graph, project),
77                     EventPrefs.hideReturnEvents(graph, project),
78                     EventPrefs.showHiddenEvents(graph, project),
79                     EventPrefs.showOnlyMilestones(graph, project),
80                     EventPrefs.showOnlyActiveEvents(graph, project));
81         }
82     }
83
84     public Collection<?> getChildrenImpl(ReadGraph graph, Resource project, Prefs prefs, List<Resource> eventLogs) throws DatabaseException {
85
86         Layer0 L0 = Layer0.getInstance(graph);
87
88         EventResource EVENT = EventResource.getInstance(graph);
89
90         final boolean hideInfoEvents = prefs.hideInfoEvents;
91         final boolean hideWarningEvents = prefs.hideWarningEvents;
92         final boolean hideReturnEvents = prefs.hideReturnEvents;
93         final boolean showHiddenEvents = prefs.showHiddenEvents;
94         final boolean showOnlyMilestones = prefs.showOnlyMilestones;
95         final boolean showOnlyActiveEvents= prefs.showOnlyActiveEvents;
96
97         ArrayList<Resource> allEvents = new ArrayList<Resource>();
98         for (Resource log : eventLogs) {
99                 for (Resource slice : graph.getObjects(log, L0.ConsistsOf)) {
100                         Collection<Resource> events = graph.getObjects(slice, L0.ConsistsOf);
101                         allEvents.addAll(events);
102                 }
103         }
104
105         List<Object> result = new ArrayList<Object>();
106         final TObjectDoubleHashMap<Object> times = new TObjectDoubleHashMap<Object>();
107         final Double defaultTime = 0.0;
108
109         for (Resource event : allEvents) {
110             if (showOnlyMilestones) {
111                 boolean isMilestone = graph.hasStatement(event, EVENT.Milestone);
112                 if (!isMilestone)
113                     continue;
114             }
115
116             if (!showHiddenEvents && graph.hasStatement(event, EVENT.Hidden))
117                 continue;
118
119             boolean isReturnEvent = hideReturnEvents || showOnlyActiveEvents
120                     ? graph.hasStatement(event, EVENT.ReturnEvent) : false;
121
122             // Skip all return events if thus preferred.
123             if (hideReturnEvents && isReturnEvent) {
124                 continue;
125             }
126
127             // Skip all return events and starting events that have been returned,
128             // if thus preferred. Also skip events that are defined non-returnable.
129             if (showOnlyActiveEvents
130                     && (isReturnEvent
131                             || graph.hasStatement(event, EVENT.Returns)
132                             || graph.hasStatement(event, EVENT.ReturnedBy)
133                             || graph.hasStatement(event, EVENT.NoReturn)))
134             {
135                 continue;
136             }
137
138             // Filter by event type severity
139             Resource eventType = graph.getPossibleObject(event, EVENT.Event_type);
140             if (eventType != null) {
141                 Integer severity = graph.getPossibleRelatedValue(eventType, EVENT.EventType_severity);
142                 if (severity != null) {
143                     if (hideInfoEvents && severity >= Constants.EVENT_SEVERITY_INFO
144                             && severity < (Constants.EVENT_SEVERITY_INFO + Constants.EVENT_SEVERITY_STEP))
145                         continue;
146                     if (hideWarningEvents && severity >= Constants.EVENT_SEVERITY_WARNING
147                             && severity < (Constants.EVENT_SEVERITY_WARNING + Constants.EVENT_SEVERITY_STEP))
148                         continue;
149                 }
150             }
151
152             double timeNumeric = EventLabelRule.getTimestamp(graph, event, defaultTime);
153             times.put(event, timeNumeric);
154             result.add(event);
155         }
156
157         Collections.sort(result, new Comparator<Object>() {
158             @Override
159             public int compare(Object o1, Object o2) {
160                 double t1 = times.get(o1);
161                 double t2 = times.get(o2);
162                 return Double.compare(t1, t2);
163             }
164         });
165
166         return result;
167
168     }
169     
170     @Override
171     public Collection<?> getChildren(ReadGraph graph, Object parent) throws DatabaseException {
172
173         Layer0X L0X = Layer0X.getInstance(graph);
174         SimulationResource SIMU = SimulationResource.getInstance(graph);
175         EventResource EVENT = EventResource.getInstance(graph);
176
177         Resource project = (Resource) parent;
178
179         List<Resource> eventLogs = new ArrayList<Resource>();
180         for (Resource activeModel : graph.syncRequest(new ObjectsWithType(project, L0X.Activates, SIMU.Model))) {
181             for (Resource eventLog : graph.syncRequest(new ObjectsWithType(activeModel, EVENT.HasEventLog, EVENT.EventLog))) {
182                 if (!graph.hasStatement(eventLog, EVENT.Hidden)) {
183                     eventLogs.add(eventLog);
184                     graph.getRelatedValue(eventLog, EVENT.HasModificationCounter, Bindings.INTEGER);
185                 }
186             }
187         }
188
189         QueryControl qc = graph.getService(QueryControl.class);
190
191         return getChildrenImpl(qc.getIndependentGraph(graph), project, Prefs.read(graph, project), eventLogs);
192
193     }
194
195     @Override
196     public Collection<?> getParents(ReadGraph graph, Object child) throws DatabaseException {
197         if (!(child instanceof Resource))
198             return Collections.emptyList();
199         Layer0 L0 = Layer0.getInstance(graph);
200         return graph.getObjects((Resource) child, L0.PartOf);
201     }
202
203     @Override
204     public boolean isCompatible(Class<?> contentType) {
205         return contentType.equals(Resource.class);
206     }
207
208 }