I have a STIX pattern as below:
stix_ptn_and_or_case2 = '''[(x:x.x = 'A' OR x:x.x = 'B' ) AND ( x:x.x = 'C' OR x:x.x = 'D' )]'''
The pattern's logic is (A OR B) AND (C OR D)
The pattern can be parsed to an expression tree by dendrol (python lib) as below
The logic of (A OR B) AND (C OR D) can be separated to 4 small and AND only logic below
- A
ANDC - A
ANDD - B
ANDC - B
ANDD
I want to parse the expression tree to get the 4 AND only logic
So I written the python code as below. The python lib dendrol can convert STIX pattern to expression tree.
import os, sys, datetime, copy
from dendrol import Pattern
stix_ptn_and_or_case2 = '''[(x:x.x = 'A' OR x:x.x = 'B' ) AND ( x:x.x = 'C' OR x:x.x = 'D' )]'''
ptn = Pattern(stix_ptn_and_or_case2)
pdt = ptn.to_dict_tree()
obj = pdt['pattern']
all_ptn_element_list = []
# the recursion function for expression tree to and-only logic
def ptnct(obj, ptn_element_list):
if('observation' in obj or 'expression' in obj):
if('observation' in obj):
join = obj['observation']['join']
expressions = obj['observation']['expressions']
else:
join = obj['expression']['join']
expressions = obj['expression']['expressions']
if(join=='AND' or join==None):
for exp in expressions:
p = ptnct(exp, ptn_element_list)
if(p!=None):
ptn_element_list.append(p)
if(len(ptn_element_list)==len(expressions)):# all and
all_ptn_element_list.append(ptn_element_list)
elif(join=='OR'):
for exp in expressions:
p = ptnct(exp, ptn_element_list)
tmp = copy.deepcopy( ptn_element_list )
tmp.append(p)
all_ptn_element_list.append(tmp)
elif('comparison' in obj):
exp = obj
tag = '{0}:{1}.{2}'.format(exp['comparison']['object'], exp['comparison']['path'][0], exp['comparison']['path'][1])
value = exp['comparison']['value']
return value
x=ptnct(obj, [])
print(all_ptn_element_list)
The output of the code is
[[u'A'], [u'B'], [u'C'], [u'D']]
But the desired output is
[[u'A', u'C'], [u'A', u'D'], [u'B', u'C'], [u'B', u'D']]
Actually the code works for most of the cases, but not this one.
Any suggestion for parsing an AND-OR logic expression tree to AND-only expressions(patterns)?

