
eu.larkc.csparql.cep.esper.EsperEngine Maven / Gradle / Ivy
The newest version!
/*
* @(#)CSPARQLEngine.java 1.0 Sep 10, 2009
*
* Copyright 2009-2009 Politecnico di Milano. All Rights Reserved.
*
* This software is the proprietary information of Politecnico di Milano.
* Use is subject to license terms.
*
* @(#) $Id$
*/
package eu.larkc.csparql.cep.esper;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import com.espertech.esper.client.Configuration;
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;
import com.espertech.esper.client.EPStatementState;
import eu.larkc.csparql.cep.api.CepEngine;
import eu.larkc.csparql.cep.api.CepQuery;
import eu.larkc.csparql.cep.api.RdfQuadruple;
import eu.larkc.csparql.cep.api.RdfSnapshot;
import eu.larkc.csparql.cep.api.RdfStream;
import eu.larkc.csparql.common.streams.format.GenericObservable;
public class EsperEngine implements CepEngine {
private EPServiceProvider epService = null;
private Map queries = null;
private Collection streams = null;
private Map statements = null;
private final Configuration configuration = new Configuration();
public Collection getAllQueries() {
return this.queries.values();
}
public Collection getAllRegisteredStreams() {
return streams;
}
public void initialize() {
// Obtain an engine instance
this.epService = EPServiceProviderManager.getDefaultProvider(this.configuration);
// ...and initialize it
this.epService.initialize();
// initialize collections
this.queries = new HashMap();
this.streams = new ArrayList();
this.statements = new HashMap();
}
public void registerStream(final RdfStream p) {
String un = p.uniqueName();
this.epService.getEPAdministrator().getConfiguration().addImport(RdfQuadruple.class);
this.epService.getEPAdministrator().getConfiguration().addEventType(un,
RdfQuadruple.class);
p.addObserver(this);
this.streams.add(p);
}
public void unregisterQuery(final String id) {
this.queries.remove(id);
}
@Override
public RdfSnapshot registerQuery(final String query, final String id) {
final EsperQuery qry = new EsperQuery(query);
this.queries.put(id, qry);
final EPStatement stmt = this.epService.getEPAdministrator().createEPL(query);
this.statements.put(id, stmt);
final QueryListener l = new QueryListener(id);
stmt.addListener(l);
return l;
}
public void destroy() {
this.epService.destroy();
}
@Override
public void update(final GenericObservable observed, final RdfQuadruple q) {
RdfStream s = (RdfStream) observed;
q.setStreamName(s.getIRI());
this.epService.getEPRuntime().sendEvent(q);
}
@Override
public void startQuery(final String id) {
final EPStatement s = this.getStatementById(id);
if (s != null) {
s.start();
}
}
@Override
public void stopQuery(final String id) {
final EPStatement s = this.getStatementById(id);
if (s != null) {
EPStatementState state = s.getState();
if (state.compareTo(EPStatementState.STOPPED) != 0)
s.stop();
}
}
private EPStatement getStatementById(final String id) {
if (this.statements.containsKey(id)) {
return this.statements.get(id);
}
return null;
}
@Override
public void unregisterStream(final RdfStream stream) {
this.streams.remove(stream);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy