iotcs.messaging.policy module

class iotcs.messaging.policy.Formula(formula)

Bases: object

A class to represent a formula expresion.

Create a Formula instance for formula.

Parameters:formula – a formula expression
classmethod compute(vp, node=None)

Compute a value for ValueProvider vp using the parsed formula.

Parameters:
Returns:

the computed value.

dump()

generated source for method dump

classmethod getAttributeReferences(formula)

Return the attribute referenced in formula $(<attribute>). :param formula: :return: a list of attributes or []

classmethod getLogger()

generated source for method getLogger

classmethod sqlMatches(s, regEx)

Return True if s matches regular expression regEx

It is assumed that a literal back slash in regEx is escaped appropriately according to python regular expression rules. :param s: string to match :param regEx: a regular expression using SQL wildcards % or _

tree = None
class iotcs.messaging.policy.FormulaParser

Bases: object

A Formula parser.

class Node(operation, leftHandSide, rightHandSide=None)

Bases: object

The FormulaParser AST.

Create a Node instance of operation.

Parameters:
  • operation – the operator represented by this Node
  • leftHandSide – the left hand side Node.
class Operation(precedence, op)

Bases: object

Expression operators.

Create an Operation instance.

Parameters:precedence – the operator precedence
ALTERNATIVE = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
AND = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
DIV = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
EQ = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
FUNCTION = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
GROUP = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
GT = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
GTE = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
LIKE = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
LOWER = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
LT = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
LTE = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
MINUS = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
MOD = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
MUL = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
NEQ = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
NOT = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
OR = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
PLUS = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
TERMINAL = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
TERNARY = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
UNARY_MINUS = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
UNARY_PLUS = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
UPPER = <iotcs.messaging.policy.FormulaParser.Node.Operation object>
getPrec()

Return the operator precedence.

op
prec
getLeftHandSide()

Return the left hand side Node. :return: the left hand side ‘Node`

getOperation()

Return the Operation for this Node.

getRightHandSide()

Return the right hand side Node. :return: the right hand side Node

leftHandSide
operation
rightHandSide
setLeftHandSide(leftHandSide)

Set the left hand side Node.

Parameters:leftHandSide – the left hand side Node.
setRightHandSide(rightHandSide)

Set the right hand side Node.

class Terminal(terminaltype, value)

Bases: iotcs.messaging.policy.Node

A terminal is either an attribute, number, or no-arg function.

It is a Node, but has no Operation, LHS, or RHS

Create a Terminal instance.

class Type

Bases: enum.Enum

Terminal Type

BOOL = 5
CURRENT_ATTRIBUTE = 1
IDENT = 3
IN_PROCESS_ATTRIBUTE = 0
NUMBER = 2
STRING = 4
getValue()

Return the `terminal value. :return: the terminal value.

terminaltype
value
class Token(tokentype, pos, length)

Bases: object

The Lexer.

class Type

Bases: enum.Enum

FormulaParser lexer token types.

AND = 0
ATTRIBUTE = 7
BOOL = 27
COLON = 1
COMMA = 2
DIV = 3
DOLLAR = 4
EQ = 5
FUNCTION = 6
GT = 8
GTE = 9
IDENT = 10
LIKE = 25
LPAREN = 11
LT = 12
LTE = 13
MINUS = 14
MOD = 15
MUL = 16
NEQ = 17
NOT = 18
NUMBER = 19
OR = 20
PLUS = 21
QUESTION_MARK = 22
RPAREN = 23
STRING = 24
WS = 26
getLength()
getPos()
getType()
length
pos
tokentype
classmethod comparePrec(left, right)

Return 0 if left and right are of equal precedence.

Return a value less than zero if left has lower precedence than right else greater than zero if left has higher precedence than left :return: the precedence of left relative to right

classmethod dump(node)

Return a string representation of node

Parameters:node – a Node
Returns:the string representation of node
classmethod parseAdditiveExpression(stack, tokens, formula, index)

Parse an additive expression.

AdditiveExpression::
additiveExpression
:multiplicativeExpression (PLUS multiplicativeExpression | MINUS multiplicativeExpression )* ;
Parameters:
  • stack – the intermediate tokens of formula
  • tokens – the formula tokens
  • formula – the formula being parsed
  • index – the current parse position in tokens
Returns:

the index of the next token to be processed

classmethod parseArgs(stack, tokens, formula, index)

Parse function element arguments.

Args::
args
: conditionalOrExpression | conditionalOrExpression COMMA args ;
Parameters:
  • stack – the intermediate tokens of formula
  • tokens – the formula tokens
  • formula – the formula being parsed
  • index – the current parse position in tokens
Returns:

the index of the next token to be processed

classmethod parseBrackettedExpression(stack, tokens, formula, index)

Parse a bracketted expression.

BrackettedExpression::
brackettedExpression
: LPAREN conditionalOrExpression RPAREN ;
Parameters:
  • stack – the intermediate tokens of formula
  • tokens – the formula tokens
  • formula – the formula being parsed
  • index – the current parse position in tokens
Returns:

the index of the next token to be processed

classmethod parseConditionalAndExpression(stack, tokens, formula, index)

Parse a conditional AND expression.

ConditionalAndExpression:

conditionalAndExpression
    : valueLogical ( AND valueLogical )*
    ;
Parameters:
  • stack – the intermediate tokens of formula
  • tokens – the formula tokens
  • formula – the formula being parsed
  • index – the current parse position in tokens
Returns:

the index of the next token to be processed

classmethod parseConditionalOrExpression(stack, tokens, formula, index)

Parse a conditional expression.

ConditionalExpression::
conditionalOrExpression
: conditionalAndExpression ( OR conditionalAndExpression )* ;
Parameters:
  • stack – the intermediate tokens of formula
  • tokens – the formula tokens
  • formula – the formula being parsed
  • index – the current parse position in tokens
Returns:

the index of the next token to be processed

classmethod parseExpressionElement(stack, tokens, formula, index)

Parse an expression element.

ExpressionElement::
expressionElement
: STRING | IDENT | NUMBER | BOOL | propertyRef ;
Parameters:
  • stack – the intermediate tokens of formula
  • tokens – the formula tokens
  • formula – the formula being parsed
  • index – the current parse position in tokens
Returns:

the index of the next token to be processed

classmethod parseFormula(tokens, formula)

Parse the formula into tokens.

Formula:

formula
    : numericExpression
    | ternaryExpression
    ;
Parameters:
  • tokens – the tokens of formula
  • formula – the formula to parse.
Returns:

the root of the AST

classmethod parseFunctionElement(stack, tokens, formula, index)

Parse a function element.

FunctionElement::
functionElement
: FUNCTION (args)? RPAREN ;
Parameters:
  • stack – the intermediate tokens of formula
  • tokens – the formula tokens
  • formula – the formula being parsed
  • index – the current parse position in tokens
Returns:

the index of the next token to be processed

classmethod parseMultiplicativeExpression(stack, tokens, formula, index)

Parse a multiplicative expression.

MultiplicativeExpression::
multiplicativeExpression
: exponentiationExpression (MUL exponentiationExpression | DIV exponentiationExpression | MOD exponentiationExpression)* ;
Parameters:
  • stack – the intermediate tokens of formula
  • tokens – the formula tokens
  • formula – the formula being parsed
  • index – the current parse position in tokens
Returns:

the index of the next token to be processed

classmethod parseNumericExpression(stack, tokens, formula, index)

Parse a numerical expression.

NumericalExpression::
numericExpression
: additiveExpression ;
Parameters:
  • stack – the intermediate tokens of formula
  • tokens – the formula tokens
  • formula – the formula being parsed
  • index – the current parse position in tokens
Returns:

the index of the next token to be processed

classmethod parsePrimaryExpression(stack, tokens, formula, index)

Parse a primary expression.

PrimaryExpression::
primaryExpression
: brackettedExpression | functionElement | expressionElement ;
Parameters:
  • stack – the intermediate tokens of formula
  • tokens – the formula tokens
  • formula – the formula being parsed
  • index – the current parse position in tokens
Returns:

the index of the next token to be processed

classmethod parsePropertyRef(stack, tokens, formula, index)

Parse a property reference.

PropertyRef::
propertyRef
: DOLLAR? ATTRIBUTE IDENT RPAREN ;
Parameters:
  • stack – the intermediate tokens of formula
  • tokens – the formula tokens
  • formula – the formula being parsed
  • index – the current parse position in tokens
Returns:

the index of the next token to be processed

classmethod parseRelationalExpression(stack, tokens, formula, index)

Parse a relational expression.

RelationalExpression:

relationalExpression
    : numericExpression (EQ numericExpression | NEQ numericExpression | LT numericExpression | GT numericExpression | LTE numericExpression | GTE numericExpression )?
    ;
Parameters:
  • stack – the intermediate tokens of formula
  • tokens – the formula tokens
  • formula – the formula being parsed
  • index – the current parse position in tokens
Returns:

the index of the next token to be processed

classmethod parseTernaryExpression(stack, tokens, formula, index)

Parse a ternary expression.

TernaryExpression:

ternaryExpression
    : conditionalOrExpression QUESTION_MARK additiveExpression COLON additiveExpression
    ;
Parameters:
  • stack – the intermediate tokens of formula
  • tokens – the formula tokens
  • formula – the formula being parsed
  • index – the current parse position in tokens
Returns:

the index of the next token to be processed

classmethod parseUnaryExpression(stack, tokens, formula, index)

Parse a unary expression.

UnaryExpression::
unaryExpression
: NOT primaryExpression | PLUS primaryExpression | MINUS primaryExpression | LOWER primaryExpression | UPPER primaryExpression | primaryExpression ;
Parameters:
  • stack – the intermediate tokens of formula
  • tokens – the formula tokens
  • formula – the formula being parsed
  • index – the current parse position in tokens
Returns:

the index of the next token to be processed

classmethod parseValueLogical(stack, tokens, formula, index)

Parse a logical value.

ValueLogical:

valueLogical
    : relationalExpression
    ;
Parameters:
  • stack – the intermediate tokens of formula
  • tokens – the formula tokens
  • formula – the formula being parsed
  • index – the current parse position in tokens
Returns:

the index of the next token to be processed

classmethod peek(tokens, offset)

Return the token at offset.

Parameters:
  • tokens – a list of Token
  • offset – the integer offset into tokens
Returns:

the Token at offset

classmethod peekformula(s, offset)

Return the next character in s from offset.

Parameters:
  • s – return the charcter at offset
  • offset – the character index in s to return
classmethod prioritize(lhs, rhs)

Prioritize lhs and rhs based on precedence.

The left hand side needs to have higher precedence than right hand side so that post-fix traversal does higher precedence operations first. The swap on compare == 0 ensures the remaining operations are left-to-right. :param lhs: the left hand side Node :param rhs: the right hand side Node :return: the Node with higher precedence

classmethod tokenize(formula)

Tokenize the formula string.

class iotcs.messaging.policy.ValueProvider

Bases: abc.ABC

Abstract a value in a formula.

The value could be an “in-process” value or a current attribute value.

getCurrentValue(key)

Provides an attribute or other property for $(propRef) in a formula. :para key: the value of propRef in a formula :return: value of the given propRef or None if not found

getInProcessValue(key)

Provides an attribute or other property for $$(propRef) in a formula. :para key: the value of propRef in a formula :return: value of the given propRef or null if not found