Search in sources :

Example 1 with FunctionDefinition

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);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) FunctionDefinition(org.wso2.siddhi.query.api.definition.FunctionDefinition) SiddhiQLBaseVisitorImpl(org.wso2.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 2 with FunctionDefinition

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;
}
Also used : PartitionType(org.wso2.siddhi.query.api.execution.partition.PartitionType) RangePartitionType(org.wso2.siddhi.query.api.execution.partition.RangePartitionType) ValuePartitionType(org.wso2.siddhi.query.api.execution.partition.ValuePartitionType) OutputAttribute(org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute) Attribute(org.wso2.siddhi.query.api.definition.Attribute) OrderByAttribute(org.wso2.siddhi.query.api.execution.query.selection.OrderByAttribute) FunctionDefinition(org.wso2.siddhi.query.api.definition.FunctionDefinition)

Example 3 with 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);
}
Also used : SiddhiAppValidationException(org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)

Example 4 with 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;
    }
}
Also used : Extension(org.wso2.siddhi.query.api.extension.Extension) Script(org.wso2.siddhi.core.function.Script) ConfigReader(org.wso2.siddhi.core.util.config.ConfigReader)

Example 5 with FunctionDefinition

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;
}
Also used : SiddhiQLParser(org.wso2.siddhi.query.compiler.SiddhiQLParser) Partition(org.wso2.siddhi.query.api.execution.partition.Partition) SiddhiApp(org.wso2.siddhi.query.api.SiddhiApp) StoreQuery(org.wso2.siddhi.query.api.execution.query.StoreQuery) Query(org.wso2.siddhi.query.api.execution.query.Query) ExecutionElement(org.wso2.siddhi.query.api.execution.ExecutionElement)

Aggregations

FunctionDefinition (org.wso2.siddhi.query.api.definition.FunctionDefinition)2 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 Script (org.wso2.siddhi.core.function.Script)1 ConfigReader (org.wso2.siddhi.core.util.config.ConfigReader)1 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)1 Attribute (org.wso2.siddhi.query.api.definition.Attribute)1 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)1 ExecutionElement (org.wso2.siddhi.query.api.execution.ExecutionElement)1 Partition (org.wso2.siddhi.query.api.execution.partition.Partition)1 PartitionType (org.wso2.siddhi.query.api.execution.partition.PartitionType)1 RangePartitionType (org.wso2.siddhi.query.api.execution.partition.RangePartitionType)1 ValuePartitionType (org.wso2.siddhi.query.api.execution.partition.ValuePartitionType)1 Query (org.wso2.siddhi.query.api.execution.query.Query)1 StoreQuery (org.wso2.siddhi.query.api.execution.query.StoreQuery)1 OrderByAttribute (org.wso2.siddhi.query.api.execution.query.selection.OrderByAttribute)1 OutputAttribute (org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute)1 Extension (org.wso2.siddhi.query.api.extension.Extension)1 SiddhiQLParser (org.wso2.siddhi.query.compiler.SiddhiQLParser)1