// Path: src/main/java/org/deri/cqels/data/EnQuad.java
// public class EnQuad {
// 	long gID,sID,pID,oID;
// 	long time;
// 	public EnQuad(long gID,long sID, long pID, long oID){
// 		this.gID=gID;
// 		this.sID=sID;
// 		this.pID=pID;
// 		this.oID=oID;
// 		time=System.nanoTime();
// 	}
// 	
// 	public long getGID(){
// 		return gID;
// 	}
// 	
// 	public long getSID(){
// 		return sID;
// 	}
// 	
// 	public long getPID(){
// 		return pID;
// 	}
// 	
// 	public long getOID(){
// 		return oID;
// 	}
// 	
// 	public long time(){ return time;};
// }


import org.deri.cqels.data.EnQuad;
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.sparql.core.Quad;
import com.hp.hpl.jena.tdb.store.NodeId;
	public void send(Node graph, Node s, Node p, Node o) {
		this.provider.getEPRuntime().sendEvent(new EnQuad(encode(graph), encode(s), 
														  encode(p), encode(o)));
	}
	
	public EPStatement addWindow(Quad quad, String window) {
		return 	this.provider.getEPAdministrator().createEPL(
					"select * from org.deri.cqels.data.EnQuad" 
				    + matchPattern(quad) + window);
	}
	
	public EPStatement addSmt(String stmt) {
		return 	this.provider.getEPAdministrator().createEPL(stmt);
	}
	
	private String matchPattern(Quad quad) {
		String st = "", and = ""; 
		
		if(!quad.getGraph().isVariable()) { 
			st += "GID=" + context.dictionary().getAllocateNodeId(quad.getGraph()).getId();
			and = ", ";
		}
		
		if(!quad.getSubject().isVariable()) { 
			st += and + "SID=" + context.dictionary()
					 			.getAllocateNodeId(quad.getSubject()).getId();
			and = ", ";
		}
		
		if(!quad.getPredicate().isVariable()) { 
// Path: src/main/java/org/deri/cqels/data/EnQuad.java
// public class EnQuad {
// 	long gID,sID,pID,oID;
// 	long time;
// 	public EnQuad(long gID,long sID, long pID, long oID){
// 		this.gID=gID;
// 		this.sID=sID;
// 		this.pID=pID;
// 		this.oID=oID;
// 		time=System.nanoTime();
// 	}
// 	
// 	public long getGID(){
// 		return gID;
// 	}
// 	
// 	public long getSID(){
// 		return sID;
// 	}
// 	
// 	public long getPID(){
// 		return pID;
// 	}
// 	
// 	public long getOID(){
// 		return oID;
// 	}
// 	
// 	public long time(){ return time;};
// }


// Path: src/main/java/org/deri/cqels/engine/CQELSEngine.java
import org.deri.cqels.data.EnQuad;
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.sparql.core.Quad;
import com.hp.hpl.jena.tdb.store.NodeId;

	public void send(Node graph, Node s, Node p, Node o) {
		this.provider.getEPRuntime().sendEvent(new EnQuad(encode(graph), encode(s), 
														  encode(p), encode(o)));
	}
	
	public EPStatement addWindow(Quad quad, String window) {
		return 	this.provider.getEPAdministrator().createEPL(
					"select * from org.deri.cqels.data.EnQuad" 
				    + matchPattern(quad) + window);
	}
	
	public EPStatement addSmt(String stmt) {
		return 	this.provider.getEPAdministrator().createEPL(stmt);
	}
	
	private String matchPattern(Quad quad) {
		String st = "", and = ""; 
		
		if(!quad.getGraph().isVariable()) { 
			st += "GID=" + context.dictionary().getAllocateNodeId(quad.getGraph()).getId();
			and = ", ";
		}
		
		if(!quad.getSubject().isVariable()) { 
			st += and + "SID=" + context.dictionary()
					 			.getAllocateNodeId(quad.getSubject()).getId();
			and = ", ";
		}
		
		if(!quad.getPredicate().isVariable()) { 
			st += and + "PID=" + context.dictionary()