public class EventLogGenerator extends Object
The expression is composed by characters and strings for the definition of elements, the operator + to define the sum of two set of elements, the operator | to define the interleaving between two set of elements and spaces between all of these to define elements composed by multiple characters.
Here an example of a possible expression: a b c + ( ef g | 123 4 5). The element "ef" and the element "123" are a single block, while for example "4" and "5" are two different element of the same set, because they are divided by a space. Note that operators define also the division between different sets, as they are operation between them.
Modifier and Type | Field and Description |
---|---|
private int |
leftAssociative
This variable defines a constant that symbolises
the left associativity of an operator.
|
private Map<String,int[]> |
operators
This is the
Map of the possible operators,
together with their precedence and left/right associativity. |
private int |
rightAssociative
This variable defines a constant that symbolises
the right associativity of an operator.
|
Constructor and Description |
---|
EventLogGenerator()
Creates an
EventLogGenerator , defining the value of the
leftAssociative and rightAssociative constants and
setting the possible operators and their precedence and left/right
associativity. |
Modifier and Type | Method and Description |
---|---|
private void |
appendAll(String s,
ArrayList<ArrayList<String>> output)
Adds on top of every list that is inside the passed
ArrayList
the string elements passed as parameter. |
private String[] |
calculateExpression(String[] postfixExpression)
Given an expression in postfix notation that defines a set of event traces,
the method returns an array of all possible traces that can be generated
from this expression.
|
String[] |
generateTraces(String exp)
Given an expression that defines a set of event traces, the method first checks if it is a valid
expression (not empty, equal number of left and right parentheses and the use of only these symbols
[ A-Z a-z 0-9 ( ) + | ]), then transforms it from infix notation to postfix notation, and at the end
generates from the postfix expression the result containing all the possible strings defined by the expression.
|
private int |
getPrecedence(String token1,
String token2)
Compares the precedence of the two passed operators (if one of them or both are not
operators, an unchecked exception will be thrown), to understand which one has the
precedence during the calculation of the expression.
|
private String[] |
infixToPostfix(String[] infixExpression)
Converts the passed expression from infix notation to Reverse Polish Notation
(also known as postfix notation) using the shunting-yard algorithm, invented
by Edsger Dijkstra.
|
private ArrayList<ArrayList<String>> |
interleaveElements(ArrayList<String> array1,
ArrayList<String> array2)
This method implements the interleaving operation between two string sets
where single elements are contained in two arrays.
|
private StringBuffer |
interleaveSets(String string1,
String string2)
This method implements the interleaving operation between two string sets
where single elements are divided inside the string by spaces.
|
private String |
interleaving(String string1,
String string2)
This method implements the interleaving between two parts of the expression, that
are composed by sets of strings connected together by the + sign and that contain
single elements separated by spaces.
|
private boolean |
isAssociative(String token,
int associativity)
Returns
true if the passed String is an operator and
its associativity value is the same as the one passed has parameter, false
if it is an operator but its associativity is different from the one passed has parameter. |
private boolean |
isOperator(String token)
Returns
true if the passed String is an operator
(defined in the operators map), false otherwise. |
private String |
plus(String string1,
String string2)
This method implements the sum of two parts of the expression.
|
private int leftAssociative
private int rightAssociative
public EventLogGenerator()
EventLogGenerator
, defining the value of the
leftAssociative
and rightAssociative
constants and
setting the possible operators and their precedence and left/right
associativity.private boolean isOperator(String token)
true
if the passed String
is an operator
(defined in the operators
map), false
otherwise.token
- the String
to testtrue
if the passed String
is an operator, false
otherwise.private boolean isAssociative(String token, int associativity)
true
if the passed String
is an operator and
its associativity value is the same as the one passed has parameter, false
if it is an operator but its associativity is different from the one passed has parameter.token
- the String
representing the operator to checkassociativity
- the constant value that defines left/right associativitytrue
if the operator has the specified associativity, false
otherwise.private int getPrecedence(String token1, String token2)
token1
- the String
representing the first operator to comparetoken2
- the String
representing the second operator to compareprivate String[] infixToPostfix(String[] infixExpression)
infixExpression
- an array containing the elements of the expression in infix notation order (divided in symbols, operators and parentheses)private String plus(String string1, String string2)
The two passed string are a representation of two parts of the expression, in which there are sets of strings divided by a + sign and that contain single elements divided by spaces.
The method simply returns a String
generated by the sum of the
two passed string with a "+" sign between them. This shows to the calculateExpression(String[])
method that this is a set of elements composed by the two passed as parameter, and not just one
single element. The method will process this string accordingly to this information.
string1
- the first String
of the new set that represents the left part of the expression to sumstring2
- the second String
of the new set that represents the righ part of the expression to sumString
composed by the two passed String
merged together with a "+" sign between themprivate String interleaving(String string1, String string2)
The method returns a String
composed by the sum ("+" between strings)
of all the generated strings by the interleaving operation.
The two passed strings can be simple strings, set of strings (with spaces between elements), sum of strings ("+" between strings) or sum of string sets. The method manage all the cases. In particular, when a + is present, the method splits the strings in groups over the + sign, then it processes the interleaving operation between all the possible combination of these groups.
string1
- the first String
for the interleaving operation. It can be a set of strings or a sum of strings or string sets ("+" between strings)string2
- the second String
for the interleaving operation. It can be a set of strings or a sum of strings or string sets ("+" between strings)String
composed by the sum ("+" between strings) of all the generated strings by the interleaving operation.private StringBuffer interleaveSets(String string1, String string2)
The output is composed by all the possible interleaved strings between these two sets, separated by a "+" sign.
string1
- the first String
set to interleavestring2
- the second String
set to interleaveStringBuffer
representing a string of all possible interleaved solutions between the two passed sets.private ArrayList<ArrayList<String>> interleaveElements(ArrayList<String> array1, ArrayList<String> array2)
The output is composed by all the possible interleaved strings between these two sets, separated by a "+" sign.
array1
- the ArrayList
containing the elements of the first setarray2
- the ArrayList
containing the elements of the second setArrayList
of ArrayList
representing the list of all the generated traces (i.e. list of interleaved elements).private void appendAll(String s, ArrayList<ArrayList<String>> output)
ArrayList
the string elements passed as parameter.s
- the String
element to add on top of all the passed listsoutput
- the ArrayList
containing the ArrayList
that represent list of string elementsprivate String[] calculateExpression(String[] postfixExpression) throws Exception
postfixExpression
- a String
array containing an expression in postfix notation, divided in single elements to be processedString
array of all possible strings that can be generated from the passed expression.Exception
- if an error occurred during the process, an exception will be thrown to inform the main method and stop the processpublic String[] generateTraces(String exp) throws Exception
In the expression sequences of events are expressed using a character for every event from the set [ A-Z a-z 0-9 , possible operators are "+" (plus) and "|" (interleaving) and the use of left and right parentheses for defining operation precedence is admitted.
exp
- the String
representing the expression that defines a set of event traces to generateException
- when an error occurred during the expression check or the generation of the result, an exception is thrown, containing as message the description of the error