use of org.wso2.siddhi.query.api.expression.constant.Constant in project siddhi by wso2.
the class ExpressionBuilder method buildVariableExecutors.
private void buildVariableExecutors(Expression expression, ExpressionVisitor expressionVisitor) {
try {
if (expression instanceof And) {
expressionVisitor.beginVisitAnd();
expressionVisitor.beginVisitAndLeftOperand();
buildVariableExecutors(((And) expression).getLeftExpression(), expressionVisitor);
expressionVisitor.endVisitAndLeftOperand();
expressionVisitor.beginVisitAndRightOperand();
buildVariableExecutors(((And) expression).getRightExpression(), expressionVisitor);
expressionVisitor.endVisitAndRightOperand();
expressionVisitor.endVisitAnd();
} else if (expression instanceof Or) {
expressionVisitor.beginVisitOr();
expressionVisitor.beginVisitOrLeftOperand();
buildVariableExecutors(((Or) expression).getLeftExpression(), expressionVisitor);
expressionVisitor.endVisitOrLeftOperand();
expressionVisitor.beginVisitOrRightOperand();
buildVariableExecutors(((Or) expression).getRightExpression(), expressionVisitor);
expressionVisitor.endVisitOrRightOperand();
expressionVisitor.endVisitOr();
} else if (expression instanceof Not) {
expressionVisitor.beginVisitNot();
buildVariableExecutors(((Not) expression).getExpression(), expressionVisitor);
expressionVisitor.endVisitNot();
} else if (expression instanceof Compare) {
expressionVisitor.beginVisitCompare(((Compare) expression).getOperator());
expressionVisitor.beginVisitCompareLeftOperand(((Compare) expression).getOperator());
buildVariableExecutors(((Compare) expression).getLeftExpression(), expressionVisitor);
expressionVisitor.endVisitCompareLeftOperand(((Compare) expression).getOperator());
expressionVisitor.beginVisitCompareRightOperand(((Compare) expression).getOperator());
buildVariableExecutors(((Compare) expression).getRightExpression(), expressionVisitor);
expressionVisitor.endVisitCompareRightOperand(((Compare) expression).getOperator());
expressionVisitor.endVisitCompare(((Compare) expression).getOperator());
} else if (expression instanceof Add) {
expressionVisitor.beginVisitMath(ExpressionVisitor.MathOperator.ADD);
expressionVisitor.beginVisitMathLeftOperand(ExpressionVisitor.MathOperator.ADD);
buildVariableExecutors(((Add) expression).getLeftValue(), expressionVisitor);
expressionVisitor.endVisitMathLeftOperand(ExpressionVisitor.MathOperator.ADD);
expressionVisitor.beginVisitMathRightOperand(ExpressionVisitor.MathOperator.ADD);
buildVariableExecutors(((Add) expression).getRightValue(), expressionVisitor);
expressionVisitor.endVisitMathRightOperand(ExpressionVisitor.MathOperator.ADD);
expressionVisitor.endVisitMath(ExpressionVisitor.MathOperator.ADD);
} else if (expression instanceof Subtract) {
expressionVisitor.beginVisitMath(ExpressionVisitor.MathOperator.SUBTRACT);
expressionVisitor.beginVisitMathLeftOperand(ExpressionVisitor.MathOperator.SUBTRACT);
buildVariableExecutors(((Subtract) expression).getLeftValue(), expressionVisitor);
expressionVisitor.endVisitMathLeftOperand(ExpressionVisitor.MathOperator.SUBTRACT);
expressionVisitor.beginVisitMathRightOperand(ExpressionVisitor.MathOperator.SUBTRACT);
buildVariableExecutors(((Subtract) expression).getRightValue(), expressionVisitor);
expressionVisitor.endVisitMathRightOperand(ExpressionVisitor.MathOperator.SUBTRACT);
expressionVisitor.endVisitMath(ExpressionVisitor.MathOperator.SUBTRACT);
} else if (expression instanceof Divide) {
expressionVisitor.beginVisitMath(ExpressionVisitor.MathOperator.DIVIDE);
expressionVisitor.beginVisitMathLeftOperand(ExpressionVisitor.MathOperator.DIVIDE);
buildVariableExecutors(((Divide) expression).getLeftValue(), expressionVisitor);
expressionVisitor.endVisitMathLeftOperand(ExpressionVisitor.MathOperator.DIVIDE);
expressionVisitor.beginVisitMathRightOperand(ExpressionVisitor.MathOperator.DIVIDE);
buildVariableExecutors(((Divide) expression).getRightValue(), expressionVisitor);
expressionVisitor.endVisitMathRightOperand(ExpressionVisitor.MathOperator.DIVIDE);
expressionVisitor.endVisitMath(ExpressionVisitor.MathOperator.DIVIDE);
} else if (expression instanceof Multiply) {
expressionVisitor.beginVisitMath(ExpressionVisitor.MathOperator.MULTIPLY);
expressionVisitor.beginVisitMathLeftOperand(ExpressionVisitor.MathOperator.MULTIPLY);
buildVariableExecutors(((Multiply) expression).getLeftValue(), expressionVisitor);
expressionVisitor.endVisitMathLeftOperand(ExpressionVisitor.MathOperator.MULTIPLY);
expressionVisitor.beginVisitMathRightOperand(ExpressionVisitor.MathOperator.MULTIPLY);
buildVariableExecutors(((Multiply) expression).getRightValue(), expressionVisitor);
expressionVisitor.endVisitMathRightOperand(ExpressionVisitor.MathOperator.MULTIPLY);
expressionVisitor.endVisitMath(ExpressionVisitor.MathOperator.MULTIPLY);
} else if (expression instanceof Mod) {
expressionVisitor.beginVisitMath(ExpressionVisitor.MathOperator.MOD);
expressionVisitor.beginVisitMathLeftOperand(ExpressionVisitor.MathOperator.MOD);
buildVariableExecutors(((Mod) expression).getLeftValue(), expressionVisitor);
expressionVisitor.endVisitMathLeftOperand(ExpressionVisitor.MathOperator.MOD);
expressionVisitor.beginVisitMathRightOperand(ExpressionVisitor.MathOperator.MOD);
buildVariableExecutors(((Mod) expression).getRightValue(), expressionVisitor);
expressionVisitor.endVisitMathRightOperand(ExpressionVisitor.MathOperator.MOD);
expressionVisitor.endVisitMath(ExpressionVisitor.MathOperator.MOD);
} else if (expression instanceof IsNull) {
IsNull isNull = (IsNull) expression;
if (isNull.getExpression() != null) {
expressionVisitor.beginVisitIsNull(null);
buildVariableExecutors(((IsNull) expression).getExpression(), expressionVisitor);
expressionVisitor.endVisitIsNull(null);
} else {
String streamId = isNull.getStreamId();
MetaStateEvent metaStateEvent = matchingMetaInfoHolder.getMetaStateEvent();
if (streamId == null) {
throw new SiddhiAppCreationException("IsNull does not support streamId being null");
} else {
AbstractDefinition definitionOutput = null;
MetaStreamEvent[] metaStreamEvents = metaStateEvent.getMetaStreamEvents();
for (int i = 0, metaStreamEventsLength = metaStreamEvents.length; i < metaStreamEventsLength; i++) {
MetaStreamEvent metaStreamEvent = metaStreamEvents[i];
AbstractDefinition definition = metaStreamEvent.getLastInputDefinition();
if (metaStreamEvent.getInputReferenceId() == null) {
if (definition.getId().equals(streamId)) {
definitionOutput = definition;
break;
}
} else {
if (metaStreamEvent.getInputReferenceId().equals(streamId)) {
definitionOutput = definition;
break;
}
}
}
if (definitionOutput != null) {
expressionVisitor.beginVisitIsNull(definitionOutput.getId());
expressionVisitor.endVisitIsNull(definitionOutput.getId());
} else {
expressionVisitor.beginVisitIsNull(null);
expressionVisitor.endVisitIsNull(null);
}
}
}
} else if (expression instanceof In) {
expressionVisitor.beginVisitIn(((In) expression).getSourceId());
buildVariableExecutors(((In) expression).getExpression(), expressionVisitor);
expressionVisitor.endVisitIn(((In) expression).getSourceId());
} else if (expression instanceof Constant) {
if (expression instanceof DoubleConstant) {
expressionVisitor.beginVisitConstant(((DoubleConstant) expression).getValue(), Attribute.Type.DOUBLE);
expressionVisitor.endVisitConstant(((DoubleConstant) expression).getValue(), Attribute.Type.DOUBLE);
} else if (expression instanceof StringConstant) {
expressionVisitor.beginVisitConstant(((StringConstant) expression).getValue(), Attribute.Type.STRING);
expressionVisitor.endVisitConstant(((StringConstant) expression).getValue(), Attribute.Type.STRING);
} else if (expression instanceof IntConstant) {
expressionVisitor.beginVisitConstant(((IntConstant) expression).getValue(), Attribute.Type.INT);
expressionVisitor.endVisitConstant(((IntConstant) expression).getValue(), Attribute.Type.INT);
} else if (expression instanceof BoolConstant) {
expressionVisitor.beginVisitConstant(((BoolConstant) expression).getValue(), Attribute.Type.BOOL);
expressionVisitor.endVisitConstant(((BoolConstant) expression).getValue(), Attribute.Type.BOOL);
} else if (expression instanceof FloatConstant) {
expressionVisitor.beginVisitConstant(((FloatConstant) expression).getValue(), Attribute.Type.FLOAT);
expressionVisitor.endVisitConstant(((FloatConstant) expression).getValue(), Attribute.Type.FLOAT);
} else if (expression instanceof LongConstant) {
expressionVisitor.beginVisitConstant(((LongConstant) expression).getValue(), Attribute.Type.LONG);
expressionVisitor.endVisitConstant(((LongConstant) expression).getValue(), Attribute.Type.LONG);
} else {
throw new OperationNotSupportedException("No constant exist with type " + expression.getClass().getName());
}
} else if (expression instanceof AttributeFunction) {
expressionVisitor.beginVisitAttributeFunction(((AttributeFunction) expression).getNamespace(), ((AttributeFunction) expression).getName());
Expression[] expressions = ((AttributeFunction) expression).getParameters();
for (int i = 0; i < expressions.length; i++) {
expressionVisitor.beginVisitParameterAttributeFunction(i);
buildVariableExecutors(expressions[i], expressionVisitor);
expressionVisitor.endVisitParameterAttributeFunction(i);
}
expressionVisitor.endVisitAttributeFunction(((AttributeFunction) expression).getNamespace(), ((AttributeFunction) expression).getName());
} else if (expression instanceof Variable) {
Variable variable = ((Variable) expression);
String attributeName = variable.getAttributeName();
AbstractDefinition definition;
Attribute.Type type = null;
int streamEventChainIndex = matchingMetaInfoHolder.getCurrentState();
if (variable.getStreamId() == null) {
MetaStreamEvent[] metaStreamEvents = matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvents();
if (streamEventChainIndex == UNKNOWN_STATE) {
String firstInput = null;
for (int i = 0; i < metaStreamEvents.length; i++) {
MetaStreamEvent metaStreamEvent = metaStreamEvents[i];
definition = metaStreamEvent.getLastInputDefinition();
if (type == null) {
try {
type = definition.getAttributeType(attributeName);
firstInput = "Input Stream: " + definition.getId() + " with " + "reference: " + metaStreamEvent.getInputReferenceId();
streamEventChainIndex = i;
} catch (AttributeNotExistException e) {
// do nothing
}
} else {
try {
definition.getAttributeType(attributeName);
throw new SiddhiAppValidationException(firstInput + " and Input Stream: " + definition.getId() + " with " + "reference: " + metaStreamEvent.getInputReferenceId() + " contains attribute " + "with same" + " name '" + attributeName + "'");
} catch (AttributeNotExistException e) {
// do nothing as its expected
}
}
}
if (streamEventChainIndex != UNKNOWN_STATE) {
if (matchingMetaInfoHolder.getMatchingStreamEventIndex() == streamEventChainIndex) {
buildStreamVariableExecutor(variable, streamEventChainIndex, expressionVisitor, type);
} else {
buildStoreVariableExecutor(variable, expressionVisitor, type, matchingMetaInfoHolder.getStoreDefinition());
}
}
} else {
MetaStreamEvent metaStreamEvent = matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvent(matchingMetaInfoHolder.getCurrentState());
definition = metaStreamEvent.getLastInputDefinition();
try {
type = definition.getAttributeType(attributeName);
} catch (AttributeNotExistException e) {
throw new SiddhiAppValidationException(e.getMessageWithOutContext() + " Input Stream: " + definition.getId() + " with " + "reference: " + metaStreamEvent.getInputReferenceId(), e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext.getName(), siddhiAppContext.getSiddhiAppString());
}
if (matchingMetaInfoHolder.getCurrentState() == matchingMetaInfoHolder.getMatchingStreamEventIndex()) {
buildStreamVariableExecutor(variable, streamEventChainIndex, expressionVisitor, type);
} else {
buildStoreVariableExecutor(variable, expressionVisitor, type, matchingMetaInfoHolder.getStoreDefinition());
}
}
} else {
MetaStreamEvent[] metaStreamEvents = matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvents();
for (int i = 0, metaStreamEventsLength = metaStreamEvents.length; i < metaStreamEventsLength; i++) {
MetaStreamEvent metaStreamEvent = metaStreamEvents[i];
definition = metaStreamEvent.getLastInputDefinition();
if (metaStreamEvent.getInputReferenceId() == null) {
if (definition.getId().equals(variable.getStreamId())) {
type = definition.getAttributeType(attributeName);
streamEventChainIndex = i;
break;
}
} else {
if (metaStreamEvent.getInputReferenceId().equals(variable.getStreamId())) {
type = definition.getAttributeType(attributeName);
streamEventChainIndex = i;
break;
}
}
}
if (matchingMetaInfoHolder.getMatchingStreamEventIndex() == streamEventChainIndex) {
buildStreamVariableExecutor(variable, streamEventChainIndex, expressionVisitor, type);
} else {
buildStoreVariableExecutor(variable, expressionVisitor, type, matchingMetaInfoHolder.getStoreDefinition());
}
}
}
} catch (Throwable t) {
ExceptionUtil.populateQueryContext(t, expression, siddhiAppContext);
throw t;
}
}
use of org.wso2.siddhi.query.api.expression.constant.Constant in project ballerina by ballerina-lang.
the class CodeGenerator method createResourceInfoEntry.
private void createResourceInfoEntry(BLangResource resourceNode, ServiceInfo serviceInfo) {
BInvokableType resourceType = (BInvokableType) resourceNode.symbol.type;
// Add resource name as an UTFCPEntry to the constant pool
int serviceNameCPIndex = addUTF8CPEntry(currentPkgInfo, resourceNode.name.value);
ResourceInfo resourceInfo = new ResourceInfo(currentPackageRefCPIndex, serviceNameCPIndex);
resourceInfo.paramTypes = resourceType.paramTypes.toArray(new BType[0]);
setParameterNames(resourceNode, resourceInfo);
resourceInfo.retParamTypes = new BType[0];
resourceInfo.signatureCPIndex = addUTF8CPEntry(currentPkgInfo, generateFunctionSig(resourceInfo.paramTypes, resourceInfo.retParamTypes));
// Add worker info
int workerNameCPIndex = addUTF8CPEntry(currentPkgInfo, "default");
resourceInfo.defaultWorkerInfo = new WorkerInfo(workerNameCPIndex, "default");
resourceNode.workers.forEach(worker -> addWorkerInfoEntry(worker, resourceInfo));
// Add resource info to the service info
serviceInfo.resourceInfoMap.put(resourceNode.name.getValue(), resourceInfo);
}
use of org.wso2.siddhi.query.api.expression.constant.Constant in project ballerina by ballerina-lang.
the class CodeGenerator method getTypeCPIndex.
/**
* Get the constant pool entry index of a given type.
*
* @param type Type to get the constant pool entry index
* @return constant pool entry index of the type
*/
private Operand getTypeCPIndex(BType type) {
int typeSigCPIndex = addUTF8CPEntry(currentPkgInfo, type.getDesc());
TypeRefCPEntry typeRefCPEntry = new TypeRefCPEntry(typeSigCPIndex);
return getOperand(currentPkgInfo.addCPEntry(typeRefCPEntry));
}
use of org.wso2.siddhi.query.api.expression.constant.Constant in project ballerina by ballerina-lang.
the class CodeGenerator method createStructInfoEntry.
private void createStructInfoEntry(BLangStruct structNode) {
BStructSymbol structSymbol = (BStructSymbol) structNode.symbol;
// Add Struct name as an UTFCPEntry to the constant pool
int structNameCPIndex = addUTF8CPEntry(currentPkgInfo, structSymbol.name.value);
StructInfo structInfo = new StructInfo(currentPackageRefCPIndex, structNameCPIndex, structSymbol.flags);
currentPkgInfo.addStructInfo(structSymbol.name.value, structInfo);
structInfo.structType = (BStructType) structSymbol.type;
List<BLangVariable> structFields = structNode.fields;
for (BLangVariable structField : structFields) {
// Create StructFieldInfo Entry
int fieldNameCPIndex = addUTF8CPEntry(currentPkgInfo, structField.name.value);
int sigCPIndex = addUTF8CPEntry(currentPkgInfo, structField.type.getDesc());
StructFieldInfo structFieldInfo = new StructFieldInfo(fieldNameCPIndex, sigCPIndex, structField.symbol.flags);
structFieldInfo.fieldType = structField.type;
// Populate default values
if (structField.expr != null && structField.expr.getKind() == NodeKind.LITERAL) {
DefaultValueAttributeInfo defaultVal = getDefaultValueAttributeInfo((BLangLiteral) structField.expr);
structFieldInfo.addAttributeInfo(AttributeInfo.Kind.DEFAULT_VALUE_ATTRIBUTE, defaultVal);
}
structInfo.fieldInfoEntries.add(structFieldInfo);
structField.symbol.varIndex = getFieldIndex(structField.symbol.type.tag);
}
// Create variable count attribute info
prepareIndexes(fieldIndexes);
int[] fieldCount = new int[] { fieldIndexes.tInt, fieldIndexes.tFloat, fieldIndexes.tString, fieldIndexes.tBoolean, fieldIndexes.tBlob, fieldIndexes.tRef };
addVariableCountAttributeInfo(currentPkgInfo, structInfo, fieldCount);
fieldIndexes = new VariableIndex(FIELD);
// Create attached function info entries
for (BAttachedFunction attachedFunc : structSymbol.attachedFuncs) {
int funcNameCPIndex = addUTF8CPEntry(currentPkgInfo, attachedFunc.funcName.value);
// Remove the first type. The first type is always the type to which the function is attached to
BType[] paramTypes = attachedFunc.type.paramTypes.toArray(new BType[0]);
if (paramTypes.length == 1) {
paramTypes = new BType[0];
} else {
paramTypes = attachedFunc.type.paramTypes.toArray(new BType[0]);
paramTypes = Arrays.copyOfRange(paramTypes, 1, paramTypes.length);
}
int sigCPIndex = addUTF8CPEntry(currentPkgInfo, generateFunctionSig(paramTypes, attachedFunc.type.retTypes.toArray(new BType[0])));
int flags = attachedFunc.symbol.flags;
structInfo.attachedFuncInfoEntries.add(new AttachedFunctionInfo(funcNameCPIndex, sigCPIndex, flags));
}
}
use of org.wso2.siddhi.query.api.expression.constant.Constant in project ballerina by ballerina-lang.
the class CodeGenerator method createConnectorInfoEntry.
private void createConnectorInfoEntry(BLangConnector connectorNode) {
BConnectorType connectorType = (BConnectorType) connectorNode.symbol.type;
// Add connector name as an UTFCPEntry to the constant pool
int connectorNameCPIndex = addUTF8CPEntry(currentPkgInfo, connectorNode.name.value);
// Create connector info
ConnectorInfo connectorInfo = new ConnectorInfo(currentPackageRefCPIndex, connectorNameCPIndex, connectorNode.symbol.flags);
connectorInfo.paramTypes = connectorType.paramTypes.toArray(new BType[0]);
connectorInfo.signatureCPIndex = addUTF8CPEntry(this.currentPkgInfo, generateConnectorSig(connectorInfo));
// Add connector level variables
int localVarAttNameIndex = addUTF8CPEntry(currentPkgInfo, AttributeInfo.Kind.LOCAL_VARIABLES_ATTRIBUTE.value());
LocalVariableAttributeInfo localVarAttributeInfo = new LocalVariableAttributeInfo(localVarAttNameIndex);
connectorNode.params.forEach(var -> visitVarSymbol(var.symbol, fieldIndexes, localVarAttributeInfo));
connectorNode.varDefs.forEach(var -> visitVarSymbol(var.var.symbol, fieldIndexes, localVarAttributeInfo));
connectorInfo.addAttributeInfo(AttributeInfo.Kind.LOCAL_VARIABLES_ATTRIBUTE, localVarAttributeInfo);
// Create variable count attribute info
prepareIndexes(fieldIndexes);
int[] fieldCount = new int[] { fieldIndexes.tInt, fieldIndexes.tFloat, fieldIndexes.tString, fieldIndexes.tBoolean, fieldIndexes.tBlob, fieldIndexes.tRef };
addVariableCountAttributeInfo(currentPkgInfo, connectorInfo, fieldCount);
// Create the init function info
BLangFunction connectorInitFunction = (BLangFunction) connectorNode.getInitFunction();
createFunctionInfoEntry(connectorInitFunction);
this.currentPkgInfo.connectorInfoMap.put(connectorNode.name.value, connectorInfo);
// Create action info entries for all actions
connectorNode.actions.forEach(res -> createActionInfoEntry(res, connectorInfo));
createActionInfoEntry(connectorNode.initAction, connectorInfo);
fieldIndexes = new VariableIndex(FIELD);
}
Aggregations