]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.event/src/org/simantics/event/EventSupport.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.event / src / org / simantics / event / EventSupport.java
diff --git a/bundles/org.simantics.event/src/org/simantics/event/EventSupport.java b/bundles/org.simantics.event/src/org/simantics/event/EventSupport.java
new file mode 100644 (file)
index 0000000..a657dfc
--- /dev/null
@@ -0,0 +1,95 @@
+/*******************************************************************************\r
+ * Copyright (c) 2012 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.event;\r
+\r
+import java.util.UUID;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.service.VirtualGraphSupport;\r
+import org.simantics.event.ontology.EventResource;\r
+import org.simantics.event.writer.EventSourceResolver;\r
+import org.simantics.event.writer.EventWriteTask;\r
+import org.simantics.event.writer.EventWriterJob;\r
+import org.simantics.layer0.Layer0;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class EventSupport {\r
+\r
+    private Resource model;\r
+    private Resource run;\r
+    private EventSourceResolver.Filter filter;\r
+\r
+    @SuppressWarnings("unused")\r
+    private Resource eventLog;\r
+\r
+    private EventWriterJob writer;\r
+    private EventSourceResolver resolver;\r
+\r
+    public EventSupport(Resource model, Resource run, EventSourceResolver.Filter filter) {\r
+        this.model = model;\r
+        this.run = run;\r
+        this.filter = filter;\r
+    }\r
+\r
+    public Resource initialize(WriteGraph graph) throws DatabaseException {\r
+        Layer0 L0 = Layer0.getInstance(graph);\r
+        final EventResource EVENT = EventResource.getInstance(graph);\r
+\r
+        Resource log = graph.newResource();\r
+        this.eventLog = log;\r
+        graph.claim(log, L0.InstanceOf, null, EVENT.EventLog);\r
+        graph.claimLiteral(log, L0.HasName, UUID.randomUUID().toString(), Bindings.STRING);\r
+        graph.claim(model, EVENT.HasEventLog, log);\r
+        graph.claim(model, L0.ConsistsOf, log);\r
+\r
+        // Switch write to graph of run resource\r
+        final Resource _log = log;\r
+        VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class);\r
+        graph.syncRequest(new WriteRequest( support.getGraph(graph, run) ) {\r
+            @Override\r
+            public void perform(WriteGraph graph) throws DatabaseException {\r
+                graph.claim(_log, EVENT.HasEventProducer, run);\r
+            }\r
+        });\r
+\r
+        // Start event writer & resolver services\r
+        this.resolver = new EventSourceResolver(graph.getProvider(), log, filter);\r
+        this.writer = new EventWriterJob(graph.getProvider(), log, this.resolver);\r
+        this.resolver.schedule();\r
+\r
+        return log;\r
+    }\r
+\r
+    public void queue(EventWriteTask task) {\r
+        EventWriterJob w = writer;\r
+        if (w != null)\r
+            w.queue(task);\r
+    }\r
+\r
+    public void dispose() {\r
+        if (writer != null) {\r
+            writer.dispose();\r
+            writer = null;\r
+        }\r
+        if (resolver != null) {\r
+            resolver.dispose();\r
+            resolver = null;\r
+        }\r
+    }\r
+\r
+}\r