timer ( cron:* * 10-18 * * ? ) rule consequences output duplicated - jboss

I want to limit the rule run in a specfic time span ,the rule i wrote is as below
rule "Event3"
timer ( cron:* * 10-18 * * ? )
no-loop
when
$m : EventTest( originNumber == "123", originNumber : originNumber ) from entry-point "ATM Stream"
or
$m : EventTest( originNumber == "456",originNumber : originNumber ) from entry-point "ATM Stream"
then
System.out.println( $m.getOriginNumber() );
end
when i insert a new fact into engine,every inserted fact before will duplicated triger the rule,how can i forbiden the have existed facts triger the rule?
the output log is below
==>[ObjectInsertedEventImpl: getFactHandle()=5:1:1210939243:1210939243:1:ATM Stream:NON_TRAIT:com.neo.drools.entity.EventTest, getObject()=com.neo.drools.entity.EventTest#482d776b, getKnowledgeRuntime()=KieSession[0], getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::ATM Stream, factHandle=5:1:1210939243:1210939243:1:ATM Stream:NON_TRAIT:com.neo.drools.entity.EventTest, originOffset=-1, propagationNumber=2, rule=null, type=INSERTION]]
==>[ObjectInsertedEventImpl: getFactHandle()=5:2:703555670:703555670:2:ATM Stream:NON_TRAIT:com.neo.drools.entity.EventTest, getObject()=com.neo.drools.entity.EventTest#29ef6856, getKnowledgeRuntime()=KieSession[0], getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::ATM Stream, factHandle=5:2:703555670:703555670:2:ATM Stream:NON_TRAIT:com.neo.drools.entity.EventTest, originOffset=-1, propagationNumber=3, rule=null, type=INSERTION]]
456
123
==>[ObjectInsertedEventImpl: getFactHandle()=5:3:179808568:179808568:3:ATM Stream:NON_TRAIT:com.neo.drools.entity.EventTest, getObject()=com.neo.drools.entity.EventTest#ab7a938, getKnowledgeRuntime()=KieSession[0], getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::ATM Stream, factHandle=5:3:179808568:179808568:3:ATM Stream:NON_TRAIT:com.neo.drools.entity.EventTest, originOffset=-1, propagationNumber=4, rule=null, type=INSERTION]]
123
456
456
==>[ObjectInsertedEventImpl: getFactHandle()=5:4:1068445309:1068445309:4:ATM Stream:NON_TRAIT:com.neo.drools.entity.EventTest, getObject()=com.neo.drools.entity.EventTest#3faf2e7d, getKnowledgeRuntime()=KieSession[0], getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::ATM Stream, factHandle=5:4:1068445309:1068445309:4:ATM Stream:NON_TRAIT:com.neo.drools.entity.EventTest, originOffset=-1, propagationNumber=5, rule=null, type=INSERTION]]
456
123
456
456
==>[ObjectInsertedEventImpl: getFactHandle()=5:5:73698537:73698537:5:ATM Stream:NON_TRAIT:com.neo.drools.entity.EventTest, getObject()=com.neo.drools.entity.EventTest#4648ce9, getKnowledgeRuntime()=KieSession[0], getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::ATM Stream, factHandle=5:5:73698537:73698537:5:ATM Stream:NON_TRAIT:com.neo.drools.entity.EventTest, originOffset=-1, propagationNumber=6, rule=null, type=INSERTION]]
456
123
456
456
456
==>[ObjectInsertedEventImpl: getFactHandle()=5:6:1453062635:1453062635:6:ATM Stream:NON_TRAIT:com.neo.drools.entity.EventTest, getObject()=com.neo.drools.entity.EventTest#569bf9eb, getKnowledgeRuntime()=KieSession[0], getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::ATM Stream, factHandle=5:6:1453062635:1453062635:6:ATM Stream:NON_TRAIT:com.neo.drools.entity.EventTest, originOffset=-1, propagationNumber=7, rule=null, type=INSERTION]]
456
456
456
456
456
123
==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:7:1632789609:-866637727:7:ATM Stream:NON_TRAIT:java.lang.String:test string aaaa], getObject()=test string aaaa, getKnowledgeRuntime()=KieSession[0], getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::ATM Stream, factHandle=[fact 0:7:1632789609:-866637727:7:ATM Stream:NON_TRAIT:java.lang.String:test string aaaa], originOffset=-1, propagationNumber=8, rule=null, type=INSERTION]]
and i expected log is
123
==========
456
==========
456
==========
456
==========
456

Could you try over window:length(1)?
rule "Event3"
timer ( cron:* * 10-18 * * ? )
no-loop
when
$m : EventTest( originNumber == "123", originNumber : originNumber ) over window:length(1) from entry-point "ATM Stream"
or
$m : EventTest( originNumber == "456",originNumber : originNumber ) over window:length(1) from entry-point "ATM Stream"
then
System.out.println( $m.getOriginNumber() );
end

Related

Why PySpark partitionBy isn't working properly?

I have a table as:
COL1
COL2
COL3
COMP
0005
2008-08-04
COMP
0009
2002-01-01
COMP
01.0
2002-01-01
COMP
0005
2008-01-01
COMP
0005
2001-10-20
CTEC
0009
2001-10-20
COMP
0005
2009-10-01
COMP
01.0
2003-07-01
COMP
02.0
2004-01-01
CTEC
0009
2021-09-24
At first I want to partition the table on COL1, then do another partition on COL2, then sort the COL3 in descending order. Then I'm trying to add row number.
I write:
windowSpec = Window.partitionBy(col("COL1")).partitionBy(col("COl2")).orderBy(desc("COL3"))
TBL = TBL.withColumn(f"RANK", F.row_number().over(windowSpec))
My expected output is this:
COL1
COL2
COL3
RANK
COMP
0005
2009-10-01
1
COMP
0005
2008-08-04
2
COMP
0005
2008-01-01
3
COMP
0005
2001-10-20
4
COMP
0009
2002-01-01
1
COMP
01.0
2003-07-01
1
COMP
01.0
2002-01-01
2
COMP
02.0
2004-01-01
1
CTEC
0009
2021-09-24
1
CTEC
0009
2001-10-20
2
But the output I'm getting is like this:
COL1
COL2
COL3
RANK
COMP
0005
2009-10-01
1
COMP
0005
2008-08-04
2
COMP
0005
2008-01-01
3
COMP
0005
2001-10-20
4
COMP
0009
2002-01-01
2
COMP
01.0
2003-07-01
1
COMP
01.0
2002-01-01
2
COMP
02.0
2004-01-01
1
CTEC
0009
2021-09-24
1
CTEC
0009
2001-10-20
3
Can anyone please help me to figure out where I'm doing the mistake??

Create a column in pyspark dataframe based on the columns from other dataframe

I have 3 pyspark data frames (df_main, df_xyz, df_cvb) where df_main is the driver data frame where a column(new_col) needs to be created based on the conditions
l = [(1,’XYZ', '324 NW', ‘VA’), (2,’XYZ, '323 NW', ‘VA’), (3,‘CVB’, '314 NW', ‘VA’)]
df_main = spark.createDataFrame(l, (‘ID’, ’Name', 'Address', 'State'))
ID Name Address State
1 XYZ 324 NW VA
2 XYZ 323 NW VA
3 CVB 314 NW VA
l = [(1 ,10, ‘A’), (2,20, ‘B’), (4, 120, ‘C’)]
df_xyz = spark.createDataFrame(l, (‘ID’, 'col1', 'col2'))
ID col1 col2
1 10 A
2 20 B
4 120 C
l = [(1 ,56), (2,45), (3,12)]
df_cvb = spark.createDataFrame(l, (‘ID’, ‘col3’))
ID col3
1 56
2 45
3 12
Create a column “new_col” in df_main data frame as.
If Name = ‘XYZ’, get col1 value from df_xyz on ID col
If Name = ‘CVB’, get col3 value from df_cvb on ID col
So my expected output df_main data frame should look like below
ID Name Address State new_col
1 XYZ 324 NW VA 10
2 XYZ 323 NW VA 20
3 CVB 314 NW VA 12
l = [(1,'XYZ', '324 NW', 'VA'), (2,'XYZ', '323 NW', 'VA'), (3,'CVB', '314 NW', 'VA')]
df_main = spark.createDataFrame(l, ('ID', 'Name', 'Address', 'State'))
l = [(1 ,10, 'A'), (2,20, 'B'), (4, 120, 'C')]
df_xyz = spark.createDataFrame(l, ('ID', 'col1', 'col2'))
df_xyz = df_xyz.select( df_xyz['ID'],df_xyz['col1'].alias('new_col'), lit("XYZ").alias("table") )
l = [(1 ,56), (2,45), (3,12)]
df_cvb = spark.createDataFrame(l, ('ID', 'col3'))
df_cvb = df_cvb.select( df_cvb['ID'],df_cvb['col3'].alias('new_col') , lit("CVB").alias( "table"))
cond = [ df_cvb['ID'] == df_main['ID'], df_cvb['table'] == df_main['Name']]
condxyz = [ df_xyz['ID'] == df_main['ID'], df_xyz['table'] == df_main['Name']]
df_result = df_main.join( df_cvb, cond, "inner").union(df_main.join( df_xyz,condxyz,"inner"))

I have input as 123 and output I am looking is 123 123 123 123 123. How to achieve it in Datastage?

I have input as 123 and output I am looking is 123 123 123 123 123. How to achieve it in Datastage?
input: 123
output :123
123
123
123
123
The Str() function returns multiples of the input string.
Str(InLink.MyString, 5)

Problem with using two different sum()'s on same column

I'm trying to get two different counts on the same column. The first count works fine with the constraints given, but the second count is not counting correctly. I have two tables, which are DailyFieldRecord and AB953. DailyFieldRecord contains: DailyFieldRecordID and ActivityCodeID. The AB953 table contains:DailyFieldRecordID, ItemID, and GroupID. Count1 will return the count of the DailyfieldrecordID's that contain ActivityCodeID=387 and GroupID=260 and that DON'T have ItemID in (1302,1303,1305,1306). Count2 will return the count of the DailyfieldrecordID's that contain ActivityCodeID=387 and GroupID=260 and that HAVE ItemID in (1302,1303,1305,1306). I'm trying to only get the count of the GroupID =260 for each DailyFieldRecordID that corresponds with the above constraints.
DailyFieldRecord: AB953:
DailyFieldRecordID ActivityCodeID DailyFieldRecordID: ItemID: GroupID:
657 387 657 1305 210
888 420 657 1333 260
672 387 657 1335 260
657 1302 210
657 1334 260
657 1111 111
888 1302 210
888 1336 260
672 1327 260
672 1334 260
672 1335 260
672 1322 260
672 1222 420
Expected Output:
Count1: Count2:
4 3
Count1 is supposed to count: Count2 is supposed to count:
672 1327 260 657 1333 260
672 1334 260 657 1335 260
672 1335 260 657 1334 260
672 1322 260
Current Count:
Count1: Count2:
4 6
SELECT sum(CASE WHEN ex=0 THEN 1 ELSE 0 END) AS COUNT1,sum(EX) AS COUNT2
FROM AB953 ab
JOIN DailyFieldRecord dfr
ON dfr.DailyFieldRecordID = ab.DailyFieldRecordID
JOIN ( SELECT AB1.DailyFieldRecordID,sum(CASE WHEN AB1.ItemID IN
(1302,1303,1305,1306) THEN 1 ELSE 0 END) AS EX
FROM AB953 AB1
GROUP BY AB1.DailyFieldRecordID) T
ON dfr.DailyFieldRecordID = T.DailyFieldRecordID
WHERE dfr.ActivityCodeID = 387
AND ab.GroupID = 260
First need to identify all of the DailyFieldRecordIDs that have any of the ItemIDs specified, which is what the sub-query here is doing. Then you can determine if the record in the outer query belongs to Count1 or Count2 based on where or not it exists in the result set of the subquery.
select sum(case when i.DailyFieldRecordID is null then 1 else 0 end) as Count1
, sum(case when i.DailyFieldRecordID is null then 0 else 1 end) as Count2
from AB953 as ab
inner join DailyFieldRecord as dfr on ab.DailyFieldRecordID = dfr.DailyFieldRecordID
left join (
select distinct a.DailyFieldRecordID
from AB953 as a
where a.ItemID in (1302, 1303, 1305, 1306)
) as i on ab.DailyFieldRecordID = i.DailyFieldRecordID
where dfr.ActivityCodeID = 387
and ab.GroupID = 260
Final Output:
+--------+--------+
| Count1 | Count2 |
+--------+--------+
| 4 | 3 |
+--------+--------+

SQL Server: FAILING extra records

I have a tableA (ID int, Match varchar, code char, status = char)
ID Match code Status
101 123 A
102 123 B
103 123 C
104 234 A
105 234 B
106 234 C
107 234 B
108 456 A
109 456 B
110 456 C
I want to populate status with 'FAIL' when:
For same match, there exists code different than (A,B or C)
or the code exists multiple times.
In other words, code can be only (A,B,C) and it should exists only one for same match, else fail. So, expected result would be:
ID Match code Status
101 123 A NULL
102 123 B NULL
103 123 C NULL
104 234 A NULL
105 234 B NULL
106 234 C NULL
107 234 B FAIL
108 456 A NULL
109 456 B NULL
110 456 C NULL
Thanks
No guarantees on efficiency here...
update tableA
set status = 'FAIL'
where ID not in (
select min(ID)
from tableA
group by Match, code)