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.event.view.contribution;
14 import gnu.trove.map.hash.TObjectDoubleHashMap;
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;
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;
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
41 * @author Tuukka Lehtonen
43 public enum ProjectEventsRule implements ChildRule {
47 public static ProjectEventsRule get() {
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;
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;
73 public static Prefs read(ReadGraph graph, Resource project) throws DatabaseException {
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));
84 public Collection<?> getChildrenImpl(ReadGraph graph, Resource project, Prefs prefs, List<Resource> eventLogs) throws DatabaseException {
86 Layer0 L0 = Layer0.getInstance(graph);
88 EventResource EVENT = EventResource.getInstance(graph);
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;
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);
105 List<Object> result = new ArrayList<Object>();
106 final TObjectDoubleHashMap<Object> times = new TObjectDoubleHashMap<Object>();
107 final Double defaultTime = 0.0;
109 for (Resource event : allEvents) {
110 if (showOnlyMilestones) {
111 boolean isMilestone = graph.hasStatement(event, EVENT.Milestone);
116 if (!showHiddenEvents && graph.hasStatement(event, EVENT.Hidden))
119 boolean isReturnEvent = hideReturnEvents || showOnlyActiveEvents
120 ? graph.hasStatement(event, EVENT.ReturnEvent) : false;
122 // Skip all return events if thus preferred.
123 if (hideReturnEvents && isReturnEvent) {
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
131 || graph.hasStatement(event, EVENT.Returns)
132 || graph.hasStatement(event, EVENT.ReturnedBy)
133 || graph.hasStatement(event, EVENT.NoReturn)))
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))
146 if (hideWarningEvents && severity >= Constants.EVENT_SEVERITY_WARNING
147 && severity < (Constants.EVENT_SEVERITY_WARNING + Constants.EVENT_SEVERITY_STEP))
152 double timeNumeric = EventLabelRule.getTimestamp(graph, event, defaultTime);
153 times.put(event, timeNumeric);
157 Collections.sort(result, new Comparator<Object>() {
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);
171 public Collection<?> getChildren(ReadGraph graph, Object parent) throws DatabaseException {
173 Layer0X L0X = Layer0X.getInstance(graph);
174 SimulationResource SIMU = SimulationResource.getInstance(graph);
175 EventResource EVENT = EventResource.getInstance(graph);
177 Resource project = (Resource) parent;
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);
189 QueryControl qc = graph.getService(QueryControl.class);
191 return getChildrenImpl(qc.getIndependentGraph(graph), project, Prefs.read(graph, project), eventLogs);
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);
204 public boolean isCompatible(Class<?> contentType) {
205 return contentType.equals(Resource.class);