How to match multiple patterns on single EPL query - esper

I have two different match recognize patterns created in Esper event processing language, when I run these one by one they are working fine but I need to get result from both of these patterns on incoming events. The patterns are defined as below.
(1)
match_recognize( "
measures A as a, B as b "
pattern (A B) "
define "
A as A.scene= 'stock'and A.activity='assembly' and A.task='picking' and A.mod2='FT' and A.mod3='Scn',
B as B.scene='stock' and B.activity='assembly' and B.task='picking' and B.mod2='PG' and B.mod3='GzS')
(2)
match_recognize(
measures A as a, B as b, C as c
pattern (A B C)
define
A as A.scene= 'assembly'and A.activity='assembly' and A.task='moving' and A.mod2='GrS' and A.mod3='GzS',
B as B.scene='assembly' and B.activity='assembly' and B.task='moving' and B.mod2='GrT' and B.mod3='Follow',
C as C.scene='assembly' and C.activity='assembly' and C.task='moving' and C.mod2='GrT' and C.mod3='GzS')
I need to get result when any one of these patterns is matched with incoming events on a single query.

ok so what is the problem? You simple create them both and add a listener to each and its all done.
First example:
String epl = "#name('first') select ... from ...;\n";
epl += "#name('second') select ... from ...;\n";
epAdministrator.getDeploymentAdmin().parseDeploy(epl);
epAdministrator.getStatement('first').addListener(new MyListener());
epAdministrator.getStatement('second').addListener(new MyListener());
Another example:
EPStatement first = epAdministrator.createEPL(...);
first.addListener(new MyUpdateListener());
EPStatement second = epAdministrator.createEPL(...);
second.addListener(new MyUpdateListener());

Related

foreach loops: wildcard asterisks (*), nested macros, string variables - resolving errors when using *

In Stata, in a foreach loop, I am searching for values within string variables, using strmatch() and asterisk (*) wildcards. I need the asterisks because I'm searching for words that fall into any part of the string.
These string variables are nested into local macros. However using * in the foreach does not work with Stata IF it is part of a nested/descendant macro. Is this because:
A) wildcards within strings can never be used in foreach in Stata when using nested macros, or
B) it isn't the wildcard itself, but the * (asterisk) that is producing the error in foreach?
If B), is it possible to define a new character that means 'wildcard' instead of * so I can still use nested macros to organize my concepts before doing foreach?
Note: I'm working with a large dataset so the strmatch() function without the foreach loop is not a solution, unless there is an alternative to foreach.
Here's an example, for drug class Q (parent/ancestor macro), with individual drug lists (descendant macro):
*chem term list
local drug_list1 " "A*B" "B*A" "A" "
local drug_list2 " "C*D" "D" "
*search term list
local drugclassQ " "drug_list1" "drug_list2" "
*check macro data successfully stored
di `drugclassQ'
(successfully stored)
*Search all drug terms in descriptions
foreach searchterm in "drugclassQ" {
gen byte `searchterm' = 0
di "Making column called `searchterm'"
foreach chemterm in ``searchterm'' {
di "Checking individual search terms in: `chemterm'"
foreach indiv in ``chemterm'' {
di "Searching all columns for *`indiv'*"
foreach codeterm in lower(variable) {
di "`searchterm': Checking `codeterm' column for *`indiv'*"
replace `searchterm' = 1 if strmatch(`codeterm', "*`indiv'*")
}
}
}
}
gen keep_term = .
replace keep_term=1 if drugclassQ==1
keep if keep_term==1
Here's an example of what I would want the foreach loop for find, searching within the string variable chemical
For example searching on "A*B" within parent macro drugclassQ would find drugs with string values within the string variable chemical as the following:
Amg / Fmg /B A/B A/ B/R Amg/dose / Emg/dose / Bmg/dose
(note: mg = milligrams to illustrate my point about needing to define the variable as a string since the drugs are entered into the database in different ways)
Example Output to identify strings with A and B anywhere within values of 'Chemical':
Obs
Chemical (string variable)
drugclassQ
1
Amg / Fmg /B
1
2
A/B
1
3
A/ B/R
1
4
Amg/dose / Emg/dose / Bmg/dose
1
5
A
0
My code works when I don't use asterisks, but then that defeats the premise of how I'm using the foreach code, i.e. using the wildcard that is within nested macros.
Any solutions?

Lua | Count Occurences of Each Item in a Table

hi I'm trying to convert this python code to Lua
names=['Deepak','Reema','John','Deepak','Munna','Reema','Deepak','Amit','John','Reema']
d={}
for i in range(len(names)-1):
x=names[i]
c=0
for j in range(i,len(names)):
if names[j]==names[i]:
c=c+1
count=dict({x:c})
if x not in d.keys():
d.update(count)
print (d)
I got all the other parts working from top but I couldn't figure out how to convert this part into Lua
if x not in d.keys():
d.update(count)
would be really great if someone can help me make sense of this conversion
if x not in d.keys():
d.update(count)
x is the name currently indexed in that loop cycle
d is a dictionary that is used to store the count of each name
c is a dictionary with a single entry, the count c of x in names
So that line basically says:
if the current name x has not been counted yet (is not in our dictionary), add its count c to d using the name in x as key.
This code is not very efficient as it counts the names every time, even if that name has already been counted. The order should be changed so you only count if there is no count in d, yet.
There is also no need to iterate over the whole array for each entry. That nested loop is nonsense. You can count that in one go.
You shouldn't learn from whatever resource that is.
In Lua the snippet above would look something like:
if not d[x] then d[x] = c end
or simply
d[x] = d[x] or c
This is how you could implement it in Lua efficiently.
local names= {'Deepak','Reema','John','Deepak','Munna','Reema','Deepak',
'Amit','John','Reema'}
local counts = {}
for _, v in ipairs(names) do
counts[v] = counts[v] and counts[v] + 1 or 1
end

Backward compatibility on parsing by means of FParsec

Imagine that I have simple string (in reality this string can contains much complex items like digit combination, Guid etc...):
"a, b, c"
and appropriate parser which works great with it.
Then after some changes string becomes:
"a, b, c, d, e"
Parser has been rewritten in manner to aim successful result.
But now I have to be sure that rewritten parser doesn't fail on parsing old format string.
Is it possible to provide backward compatibility with FParsec?
The backward compatibility should be implemented within your data model, and make the parser only to support this data model.
Say, if your Result datatype is a List<> (which is intended to be 3-elemens long), only a minimal intrusion makes the parser return a 4-element data. You then check data validity with your application logic, and you're all set.
However, if your Result is a fixed tuple/triple like T1 * T2 * T3, there is no straightforward way to add the fourth element. You would probably need an extra layer — usually implemented as a Discriminated Union (DU), like this:
type MyData =
| OldFormat of T1 * T2 * T3 // this is your old way
| NewFormat of T1 * T2 * T3 * T4 // this is your new data format
This way, you will have to implement a wrapper DU and also duplicate the parsing logic to support the 4-element data.
Check this, this, and this answers for some simple ways of parsing the "list of either-either" data.
Unless you provide with the existing code, it is hard to tell anything further.

Extending Query Expressions

Are there any documents or examples out there on how one can extend/add new keywords to query expressions? Is this even possible?
For example, I'd like to add a lead/lag operator.
In addition to the query builder for the Rx Framework mentioned by #pad, there is also a talk by Wonseok Chae from the F# team about Computation Expressions that includes query expressions. I'm not sure if the meeting was recorded, but there are very detailed slides with a cool example on query syntax for generating .NET IL code.
The source code of the standard F# query builder is probably the best resource for finding out what types of operations are supported and how to annotate them with attributes.
The key attributes that you'll probably need are demonstrated by the where clause:
[<CustomOperation("where",MaintainsVariableSpace=true,AllowIntoPattern=true)>]
member Where :
: source:QuerySource<'T,'Q> *
[<ProjectionParameter>] predicate:('T -> bool) -> QuerySource<'T,'Q>
The CustomOperation attribute defines the name of the operation. The (quite important) parameter MaintainsVariableSpace allows you to say that the operation returns the same type of values as it takes as the input. In that case, the variables defined earlier are still available after the operation. For example:
query { for p in db.Products do
let name = p.ProductName
where (p.UnitPrice.Value > 100.0M)
select name }
Here, the variables p and name are still accessible after where because where only filters the input, but it does not transform the values in the list.
Finally, the ProjectionParameter allows you to say that p.UnitValue > 100.0M should actually be turned into a function that takes the context (available variables) and evaluates this expression. If you do not specify this attribute, then the operation just gets the value of the argument as in:
query { for p in .. do
take 10 }
Here, the argument 10 is just a simple expression that cannot use values in p.
Pretty cool feature for the language. Just implemented the reverse to query QuerySource.
Simple example, but just a demonstration.
module QueryExtensions
type ExtendedQueryBuilder() =
inherit Linq.QueryBuilder()
/// Defines an operation 'reverse' that reverses the sequence
[<CustomOperation("reverse", MaintainsVariableSpace = true)>]
member __.Reverse (source : Linq.QuerySource<'T,System.Collections.IEnumerable>) =
let reversed = source.Source |> List.ofSeq |> List.rev
new Linq.QuerySource<'T,System.Collections.IEnumerable>(reversed)
let query = ExtendedQueryBuilder()
And now it being used.
let a = [1 .. 100]
let specialReverse =
query {
for i in a do
select i
reverse
}

Find all possible pairs between the subsets of N sets with Erlang

I have a set S. It contains N subsets (which in turn contain some sub-subsets of various lengths):
1. [[a,b],[c,d],[*]]
2. [[c],[d],[e,f],[*]]
3. [[d,e],[f],[f,*]]
N. ...
I also have a list L of 'unique' elements that are contained in the set S:
a, b, c, d, e, f, *
I need to find all possible combinations between each sub-subset from each subset so, that each resulting combination has exactly one element from the list L, but any number of occurrences of the element [*] (it is a wildcard element).
So, the result of the needed function working with the above mentioned set S should be (not 100% accurate):
- [a,b],[c],[d,e],[f];
- [a,b],[c],[*],[d,e],[f];
- [a,b],[c],[d,e],[f],[*];
- [a,b],[c],[d,e],[f,*],[*];
So, basically I need an algorithm that does the following:
take a sub-subset from the subset 1,
add one more sub-subset from the subset 2 maintaining the list of 'unique' elements acquired so far (the check on the 'unique' list is skipped if the sub-subset contains the * element);
Repeat 2 until N is reached.
In other words, I need to generate all possible 'chains' (it is pairs, if N == 2, and triples if N==3), but each 'chain' should contain exactly one element from the list L except the wildcard element * that can occur many times in each generated chain.
I know how to do this with N == 2 (it is a simple pair generation), but I do not know how to enhance the algorithm to work with arbitrary values for N.
Maybe Stirling numbers of the second kind could help here, but I do not know how to apply them to get the desired result.
Note: The type of data structure to be used here is not important for me.
Note: This question has grown out from my previous similar question.
These are some pointers (not a complete code) that can take you to right direction probably:
I don't think you will need some advanced data structures here (make use of erlang list comprehensions). You must also explore erlang sets and lists module. Since you are dealing with sets and list of sub-sets, they seems like an ideal fit.
Here is how things with list comprehensions will get solved easily for you: [{X,Y} || X <- [[c],[d],[e,f]], Y <- [[a,b],[c,d]]]. Here i am simply generating a list of {X,Y} 2-tuples but for your use case you will have to put real logic here (including your star case)
Further note that with list comprehensions, you can use output of one generator as input of a later generator e.g. [{X,Y} || X1 <- [[c],[d],[e,f]], X <- X1, Y1 <- [[a,b],[c,d]], Y <- Y1].
Also for removing duplicates from a list of things L = ["a", "b", "a"]., you can anytime simply do sets:to_list(sets:from_list(L)).
With above tools you can easily generate all possible chains and also enforce your logic as these chains get generated.

Resources