use of org.wso2.siddhi.query.api.definition.FunctionDefinition in project siddhi by wso2.
the class SiddhiCompiler method parseFunctionDefinition.
public static FunctionDefinition parseFunctionDefinition(String source) throws SiddhiParserException {
ANTLRInputStream input = new ANTLRInputStream(source);
SiddhiQLLexer lexer = new SiddhiQLLexer(input);
lexer.removeErrorListeners();
lexer.addErrorListener(SiddhiErrorListener.INSTANCE);
CommonTokenStream tokens = new CommonTokenStream(lexer);
SiddhiQLParser parser = new SiddhiQLParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(SiddhiErrorListener.INSTANCE);
ParseTree tree = parser.definition_function_final();
SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
return (FunctionDefinition) eval.visit(tree);
}
use of org.wso2.siddhi.query.api.definition.FunctionDefinition in project siddhi by wso2.
the class SiddhiQLBaseVisitorImpl method visitDefinition_function.
@Override
public FunctionDefinition visitDefinition_function(@NotNull SiddhiQLParser.Definition_functionContext ctx) {
String functionName = (String) visitFunction_name(ctx.function_name());
String languageName = (String) visitLanguage_name(ctx.language_name());
Attribute.Type attributeType = (Attribute.Type) visit(ctx.attribute_type());
String functionBody = (String) visitFunction_body(ctx.function_body());
FunctionDefinition functionDefinition = new FunctionDefinition();
functionDefinition.id(functionName).language(languageName).type(attributeType).body(functionBody);
populateQueryContext(functionDefinition, ctx);
return functionDefinition;
}
use of org.wso2.siddhi.query.api.definition.FunctionDefinition in project siddhi by wso2.
the class SiddhiApp method defineFunction.
public void defineFunction(FunctionDefinition functionDefinition) {
if (functionDefinition == null) {
throw new SiddhiAppValidationException("Function Definition should not be null");
} else if (functionDefinition.getId() == null) {
throw new SiddhiAppValidationException("Function Id should not be null for Function Definition", functionDefinition.getQueryContextStartIndex(), functionDefinition.getQueryContextEndIndex());
} else if (functionDefinition.getReturnType() == null) {
throw new SiddhiAppValidationException("Return type should not be null for Function Definition", functionDefinition.getQueryContextStartIndex(), functionDefinition.getQueryContextEndIndex());
} else if (functionDefinition.getBody() == null) {
throw new SiddhiAppValidationException("Body should not be null for Function Definition", functionDefinition.getQueryContextStartIndex(), functionDefinition.getQueryContextEndIndex());
} else if (functionDefinition.getLanguage() == null) {
throw new SiddhiAppValidationException("Language should not be null for Function Definition", functionDefinition.getQueryContextStartIndex(), functionDefinition.getQueryContextEndIndex());
}
checkDuplicateFunctionExist(functionDefinition);
this.functionDefinitionMap.put(functionDefinition.getId(), functionDefinition);
}
use of org.wso2.siddhi.query.api.definition.FunctionDefinition in project siddhi by wso2.
the class DefinitionParserHelper method addFunction.
public static void addFunction(SiddhiAppContext siddhiAppContext, final FunctionDefinition functionDefinition) {
Extension extension = new Extension() {
@Override
public String getNamespace() {
return "script";
}
@Override
public String getName() {
return functionDefinition.getLanguage().toLowerCase();
}
};
try {
Script script = (Script) SiddhiClassLoader.loadExtensionImplementation(extension, ScriptExtensionHolder.getInstance(siddhiAppContext));
ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(extension.getNamespace(), extension.getName());
script.setReturnType(functionDefinition.getReturnType());
script.init(functionDefinition.getId(), functionDefinition.getBody(), configReader);
siddhiAppContext.getScriptFunctionMap().put(functionDefinition.getId(), script);
} catch (Throwable t) {
ExceptionUtil.populateQueryContext(t, functionDefinition, siddhiAppContext);
throw t;
}
}
use of org.wso2.siddhi.query.api.definition.FunctionDefinition in project siddhi by wso2.
the class SiddhiQLBaseVisitorImpl method visitSiddhi_app.
/**
* {@inheritDoc}
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*
* @param ctx
*/
@Override
public SiddhiApp visitSiddhi_app(@NotNull SiddhiQLParser.Siddhi_appContext ctx) {
SiddhiApp siddhiApp = SiddhiApp.siddhiApp();
for (SiddhiQLParser.App_annotationContext annotationContext : ctx.app_annotation()) {
siddhiApp.annotation((Annotation) visit(annotationContext));
}
for (SiddhiQLParser.Definition_streamContext streamContext : ctx.definition_stream()) {
siddhiApp.defineStream((StreamDefinition) visit(streamContext));
}
for (SiddhiQLParser.Definition_tableContext tableContext : ctx.definition_table()) {
siddhiApp.defineTable((TableDefinition) visit(tableContext));
}
for (SiddhiQLParser.Definition_functionContext functionContext : ctx.definition_function()) {
siddhiApp.defineFunction((FunctionDefinition) visit(functionContext));
}
for (SiddhiQLParser.Definition_windowContext windowContext : ctx.definition_window()) {
siddhiApp.defineWindow((WindowDefinition) visit(windowContext));
}
for (SiddhiQLParser.Definition_aggregationContext aggregationContext : ctx.definition_aggregation()) {
siddhiApp.defineAggregation((AggregationDefinition) visit(aggregationContext));
}
for (SiddhiQLParser.Execution_elementContext executionElementContext : ctx.execution_element()) {
ExecutionElement executionElement = (ExecutionElement) visit(executionElementContext);
if (executionElement instanceof Partition) {
siddhiApp.addPartition((Partition) executionElement);
} else if (executionElement instanceof Query) {
siddhiApp.addQuery((Query) executionElement);
} else {
throw newSiddhiParserException(ctx);
}
}
for (SiddhiQLParser.Definition_triggerContext triggerContext : ctx.definition_trigger()) {
siddhiApp.defineTrigger((TriggerDefinition) visit(triggerContext));
}
populateQueryContext(siddhiApp, ctx);
return siddhiApp;
}
Aggregations