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