#!/usr/bin/env ruby require 'drb' require 'pry' begin begin DRb.start_service("druby://localhost:0") rescue SocketError, Errno::EADDRNOTAVAIL DRb.start_service("druby://:0") end $spec_server = DRbObject.new_with_uri("druby://127.0.0.1:8989") rescue DRb::DRbConnError err.puts "No DRb server is running. Running in local process instead ..." end def rspec(file=nil) if file $spec_server.run(["--color", "--format", "s", file], STDERR, STDOUT) else $spec_server.run(["--color", 'spec'], STDERR, STDOUT) end end $service = nil def service $service ||= com.espertech.esper.client.EPServiceProviderManager.getDefaultProvider end $administrator = nil def administrator $administrator ||= service.getEPAdministrator end def compileEPL(query) administrator.compileEPL(query.expression) end def query_string(model) writer = java.io.StringWriter.new formatter = com.espertech.esper.client.soda.EPStatementFormatter.new if model.respond_to?(:toEPLElement) model.toEPLElement(writer) writer.toString elsif model.respond_to?(:toPrecedenceFreeEPL) begin model.toPrecedenceFreeEPL(writer) rescue ArgumentError model.toPrecedenceFreeEPL(writer, formatter) end writer.toString elsif model.is_a?(Java::ComEspertechEsperClientSoda::EPStatementObjectModel) model.toEPL else model.toEPL(writer, formatter) writer.toString end end def compiledExpression(query) model = compileEPL(query) mapping = {} query.targets.each do |target| mapping[target] = target end Norikra::Query.rewrite_query(model, mapping) query_string(model) end $LOAD_PATH.push './lib' $LOAD_PATH.push './esper' require 'norikra' require 'java' esper_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'esper')) Dir.entries(esper_dir).each do |f| next unless f =~ /^esper-\d+\.\d+\.\d+\.jar$/ require f end Dir.entries(File.join(esper_dir, 'esper', 'lib')).each do |f| if f =~ /^commons-logging-\d+\.\d+\.\d+\.jar$/ || f =~ /^antlr-runtime-\d+\.\d+\.jar/ || f =~ /^cglib-nodep-\d+\.\d+\.jar$/ require File.join('esper', 'lib', f) end end puts < # execute 'spork' on other terminal > rspec 'spec/xxx_spec.rb' > rspec # for all tests > query = Norikra::Query.new(name:'test1', expression:'SELECT ...') > > query.ast.to_a # dump query AST > compiledExpression(query) # show parsed and compiled query DESC Pry.start