X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.event%2Fsrc%2Forg%2Fsimantics%2Fevent%2FEventSupport.java;fp=bundles%2Forg.simantics.event%2Fsrc%2Forg%2Fsimantics%2Fevent%2FEventSupport.java;h=a657dfc196e7eb78ec55261a9279e966e0bbc01e;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git 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 index 000000000..a657dfc19 --- /dev/null +++ b/bundles/org.simantics.event/src/org/simantics/event/EventSupport.java @@ -0,0 +1,95 @@ +/******************************************************************************* + * Copyright (c) 2012 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.event; + +import java.util.UUID; + +import org.simantics.databoard.Bindings; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.service.VirtualGraphSupport; +import org.simantics.event.ontology.EventResource; +import org.simantics.event.writer.EventSourceResolver; +import org.simantics.event.writer.EventWriteTask; +import org.simantics.event.writer.EventWriterJob; +import org.simantics.layer0.Layer0; + +/** + * @author Tuukka Lehtonen + */ +public class EventSupport { + + private Resource model; + private Resource run; + private EventSourceResolver.Filter filter; + + @SuppressWarnings("unused") + private Resource eventLog; + + private EventWriterJob writer; + private EventSourceResolver resolver; + + public EventSupport(Resource model, Resource run, EventSourceResolver.Filter filter) { + this.model = model; + this.run = run; + this.filter = filter; + } + + public Resource initialize(WriteGraph graph) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + final EventResource EVENT = EventResource.getInstance(graph); + + Resource log = graph.newResource(); + this.eventLog = log; + graph.claim(log, L0.InstanceOf, null, EVENT.EventLog); + graph.claimLiteral(log, L0.HasName, UUID.randomUUID().toString(), Bindings.STRING); + graph.claim(model, EVENT.HasEventLog, log); + graph.claim(model, L0.ConsistsOf, log); + + // Switch write to graph of run resource + final Resource _log = log; + VirtualGraphSupport support = graph.getService(VirtualGraphSupport.class); + graph.syncRequest(new WriteRequest( support.getGraph(graph, run) ) { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + graph.claim(_log, EVENT.HasEventProducer, run); + } + }); + + // Start event writer & resolver services + this.resolver = new EventSourceResolver(graph.getProvider(), log, filter); + this.writer = new EventWriterJob(graph.getProvider(), log, this.resolver); + this.resolver.schedule(); + + return log; + } + + public void queue(EventWriteTask task) { + EventWriterJob w = writer; + if (w != null) + w.queue(task); + } + + public void dispose() { + if (writer != null) { + writer.dispose(); + writer = null; + } + if (resolver != null) { + resolver.dispose(); + resolver = null; + } + } + +}