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