]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.event/src/org/simantics/event/EventSupport.java
Merge "Databoard and SCL enchancements."
[simantics/platform.git] / bundles / org.simantics.event / src / org / simantics / event / EventSupport.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012 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  *     Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.event;\r
13 \r
14 import java.util.UUID;\r
15 \r
16 import org.simantics.databoard.Bindings;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.WriteGraph;\r
19 import org.simantics.db.common.request.WriteRequest;\r
20 import org.simantics.db.exception.DatabaseException;\r
21 import org.simantics.db.service.VirtualGraphSupport;\r
22 import org.simantics.event.ontology.EventResource;\r
23 import org.simantics.event.writer.EventSourceResolver;\r
24 import org.simantics.event.writer.EventWriteTask;\r
25 import org.simantics.event.writer.EventWriterJob;\r
26 import org.simantics.layer0.Layer0;\r
27 \r
28 /**\r
29  * @author Tuukka Lehtonen\r
30  */\r
31 public class EventSupport {\r
32 \r
33     private Resource model;\r
34     private Resource run;\r
35     private EventSourceResolver.Filter filter;\r
36 \r
37     @SuppressWarnings("unused")\r
38     private Resource eventLog;\r
39 \r
40     private EventWriterJob writer;\r
41     private EventSourceResolver resolver;\r
42 \r
43     public EventSupport(Resource model, Resource run, EventSourceResolver.Filter filter) {\r
44         this.model = model;\r
45         this.run = run;\r
46         this.filter = filter;\r
47     }\r
48 \r
49     public Resource initialize(WriteGraph graph) throws DatabaseException {\r
50         Layer0 L0 = Layer0.getInstance(graph);\r
51         final EventResource EVENT = EventResource.getInstance(graph);\r
52 \r
53         Resource log = graph.newResource();\r
54         this.eventLog = log;\r
55         graph.claim(log, L0.InstanceOf, null, EVENT.EventLog);\r
56         graph.claimLiteral(log, L0.HasName, UUID.randomUUID().toString(), Bindings.STRING);\r
57         graph.claim(model, EVENT.HasEventLog, log);\r
58         graph.claim(model, L0.ConsistsOf, log);\r
59 \r
60         // Switch write to graph of run resource\r
61         final Resource _log = log;\r
62         VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);\r
63         graph.syncRequest(new WriteRequest( support.getGraph(graph, run) ) {\r
64             @Override\r
65             public void perform(WriteGraph graph) throws DatabaseException {\r
66                 graph.claim(_log, EVENT.HasEventProducer, run);\r
67             }\r
68         });\r
69 \r
70         // Start event writer & resolver services\r
71         this.resolver = new EventSourceResolver(graph.getProvider(), log, filter);\r
72         this.writer = new EventWriterJob(graph.getProvider(), log, this.resolver);\r
73         this.resolver.schedule();\r
74 \r
75         return log;\r
76     }\r
77 \r
78     public void queue(EventWriteTask task) {\r
79         EventWriterJob w = writer;\r
80         if (w != null)\r
81             w.queue(task);\r
82     }\r
83 \r
84     public void dispose() {\r
85         if (writer != null) {\r
86             writer.dispose();\r
87             writer = null;\r
88         }\r
89         if (resolver != null) {\r
90             resolver.dispose();\r
91             resolver = null;\r
92         }\r
93     }\r
94 \r
95 }\r