Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2018 V12 Technology Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package com.fluxtion.ext.streaming.builder.stream;
import com.fluxtion.api.annotations.AfterEvent;
import com.fluxtion.api.annotations.Initialise;
import com.fluxtion.api.annotations.NoEventReference;
import com.fluxtion.api.annotations.OnEvent;
import com.fluxtion.api.annotations.OnParentUpdate;
import com.fluxtion.api.annotations.PushReference;
import com.fluxtion.api.partition.LambdaReflection.SerializableConsumer;
import com.fluxtion.api.partition.LambdaReflection.SerializableFunction;
import com.fluxtion.api.partition.LambdaReflection.SerializableSupplier;
import com.fluxtion.builder.generation.GenerationContext;
import static com.fluxtion.builder.generation.GenerationContext.SINGLETON;
import com.fluxtion.ext.streaming.api.Constant;
import com.fluxtion.ext.streaming.api.FilterWrapper;
import com.fluxtion.ext.streaming.api.Stateful;
import com.fluxtion.ext.streaming.api.Test;
import com.fluxtion.ext.streaming.api.Wrapper;
import com.fluxtion.ext.streaming.api.numeric.MutableNumber;
import com.fluxtion.ext.streaming.api.stream.AbstractFilterWrapper;
import com.fluxtion.ext.streaming.api.stream.StreamOperator;
import com.fluxtion.ext.streaming.api.stream.Argument;
import static com.fluxtion.ext.streaming.api.stream.Argument.arg;
import com.fluxtion.ext.streaming.builder.util.FunctionGeneratorHelper;
import com.fluxtion.ext.streaming.builder.util.FunctionInfo;
import static com.fluxtion.ext.streaming.builder.util.FunctionKeys.filter;
import static com.fluxtion.ext.streaming.builder.util.FunctionKeys.filterSubjectClass;
import static com.fluxtion.ext.streaming.builder.util.FunctionKeys.functionClass;
import static com.fluxtion.ext.streaming.builder.util.FunctionKeys.imports;
import static com.fluxtion.ext.streaming.builder.util.FunctionKeys.input;
import static com.fluxtion.ext.streaming.builder.util.FunctionKeys.isPrimitiveNumeric;
import static com.fluxtion.ext.streaming.builder.util.FunctionKeys.newFunction;
import static com.fluxtion.ext.streaming.builder.util.FunctionKeys.outputClass;
import static com.fluxtion.ext.streaming.builder.util.FunctionKeys.sourceClass;
import static com.fluxtion.ext.streaming.builder.util.FunctionKeys.sourceMappingList;
import static com.fluxtion.ext.streaming.builder.util.FunctionKeys.stateful;
import static com.fluxtion.ext.streaming.builder.util.FunctionKeys.targetClass;
import static com.fluxtion.ext.streaming.builder.util.FunctionKeys.targetMethod;
import static com.fluxtion.ext.streaming.builder.util.FunctionKeys.wrappedSubject;
import com.fluxtion.ext.streaming.builder.util.ImportMap;
import com.fluxtion.ext.streaming.builder.util.SourceInfo;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.velocity.VelocityContext;
/**
* Applies filtering logic to a node in the execution graph. The filter invokes
* a predicate that tests a node for a valid match. The outcome of the match has
* the following effect:
*
*
Successful match allows the event wave to propagate.
*
Failed match the event wave stops at the node under test.
*
*
*
* Filtering has the following characteristics:
*
*
Filter functions are either instance or static methods.
*
An instance filter is a node in the SEP and can receive inputs from any
* other nodes.
*
An instance filter is stateful, the same filter instance will be used
* across multiple event processing cycles.
*
An instance filter can attach to any SEP life-cycle method such as
* {@link AfterEvent}
*
A filter inspection target can be a method reference or a node in the
* execution graph.
*
A ,target method reference can return either a primitive or reference
* types.
*
Fluxtion will cast all supplied values to the receiving type.
*
Lambdas cannot be used as filter predicates use method references.
*
*
* Below is an example creating a filter on a primitive double property. The
* filter is an int parameter, Fluxtion manages all casts.
*
*
*{@code @SepBuilder(name="FilterTest", packageName="com.fluxtion.testfilter")}
* public void buildFilter(SEPConfig cfg) {
* MyDataHandler dh1 = cfg.addNode(new MyDataHandler("dh1"));
* filter(lt(34), dh1::getDoubleVal).build();
* filter(positive(), dh1::getIntVal).build();
* }
* ...
*
* public class NumericValidator {
*
* //method reference wraps instance test
* public static SerializableFunction lt(int test) {
* return (SerializableFunction)new NumericValidator(test)::lessThan;
* }
*
* //method reference wraps static test
* public static SerializableFunction positive() {
* return (SerializableFunction) NumericValidator::positiveInt;
* }
*
* public int limit;
*
* public NumericValidator(int limit) { this.limit = limit; }
*
* public static boolean positiveInt(int d) { return d > 0; }
*
* public boolean greaterThan(int d) { return d > limit; }
*
* public boolean lessThan(int d) { return d < limit; } }
*
*
*
* @author V12 Technology Ltd.
* @param The test function applied to the filter subject
* @param The filter subject under test
*/
public class StreamFunctionCompiler {
private static final String TEMPLATE = "template/FilterTemplate.vsl";
private static final String MAPPER_TEMPLATE = "template/MapperTemplate.vsl";
private static final String PUSH_TEMPLATE = "template/PushTemplate.vsl";
private static final String MAPPER_PRIMITIVE_TEMPLATE = "template/MapperPrimitiveTemplate.vsl";
private static final String MAPPER_BOOLEAN_TEMPLATE = "template/MapperBooleanTemplate.vsl";
private static final String MAPPER_TEST_TEMPLATE = "template/TestTemplate.vsl";
private static final String CONSUMER_TEMPLATE = "template/ConsumerTemplate.vsl";
private static final String MAPFIELD_TEMPLATE = "template/MapFieldTemplate.vsl";
private FunctionClassKey key;
private String currentTemplate = TEMPLATE;
private String genClassSuffix = "Filter_";
private final HashMap