def test_token_passing_01(self): # Tests pattern with a single node m = Matcher() a1 = A(id='a1') p1 = Pattern('p1').add_node(a1) m.add_pattern(p1) # currently empty n = m.get_pattern('p1') self.assertEqual(len(n),0) a2 = A() tok = token_add_node(a2) m.send_token(tok) self.assertEqual(len(n),1) tok = token_remove_node(a2) m.send_token(tok) self.assertEqual(len(n),0) tok = token_add_node(a2) m.send_token(tok) self.assertEqual(len(n),1)
def test_token_passing_02(self): # Test single-node pattern with non-matching attributes m = Matcher() x1 = X(id='x1',ph=True) p1 = Pattern('p1').add_node(x1) m.add_pattern(p1) # currently empty n = m.get_pattern('p1') self.assertEqual(len(n),0) x2 = X(ph=True,v=0) tok = token_add_node(x2) m.send_token(tok) self.assertEqual(len(n),1) tok = token_remove_node(x2) m.send_token(tok) self.assertEqual(len(n),0) tok = token_edit_attrs(x2,['ph']) m.send_token(tok) self.assertEqual(len(n),1) tok = token_edit_attrs(x2,['v']) m.send_token(tok) self.assertEqual(len(n),1) tok = token_add_node(X(ph=False,v=0)) m.send_token(tok) self.assertEqual(len(n),1)
def test_token_passing_10(self): # testing loops formed by A(x!0,x!1) n=10 A_list = [] x_right_list = [] x_left_list = [] for i in range(n): a1,xL,xR = A(),X(),X() a1.add_sites(xL,xR) A_list.append(a1) x_left_list.append(xL) x_right_list.append(xR) for i in range(n): bnd = Bond() x1 = x_left_list[i] x2 = x_right_list[i-1] bnd.add_sites(x1,x2) #pattern p = Pattern('loop').add_node(A_list[0]) self.assertEqual(len(p),4*n) m = Matcher() m.add_pattern(p) # tokens tokens = [] left = [] right = [] for i in range(n): a,xL,xR = A(),X(),X() left.append(xL) right.append(xR) tokens = [token_add_node(x) for x in [a,xL,xR]] m.send_tokens(tokens) a.add_sites(xL,xR) tokens = [token_add_edge(a,'sites','molecule',x) for x in [xL,xR]] m.send_tokens(tokens) for i in range(n): bnd = Bond() tokens = [token_add_node(bnd)] m.send_tokens(tokens) bnd.add_sites(left[i],right[i-1]) tokens = [token_add_edge(bnd,'sites','bond',x) for x in [left[i],right[i-1]] ] m.send_tokens(tokens) self.assertEqual(m.count('loop'),2*n)
def test_complex_bookkeeping(self): # n1 molecules of A-X # n2 molecules of A-X2 # n3 molecules of A-X3 # n4 molecules of A-X-bnd-X-A # number of complexes = n1 + n2 + n3 + n4 # number matches to pattern A-X = n1 + 2*n2 + 3*n3 + 2*n4 pAx = Pattern('pAx').add_node(A(id='a').add_sites(X(id='x'))) m = Matcher() m.add_pattern(pAx) n1,n2,n3,n4 = 0,0,0,0 # n1 molecules of A-X n1 = 100 for i in range(n1): a,x = A(),X() m.send_tokens([token_add_node(a),token_add_node(x)]) x.molecule = a m.send_tokens([token_add_edge(a,'sites','molecule',x)]) # n2 molecules of A-X2 n2 = 50 for i in range(n2): a,x1,x2 = A(), X(), X() m.send_tokens([token_add_node(a),token_add_node(x1),token_add_node(x2)]) x1.molecule = a x2.molecule = a m.send_tokens([token_add_edge(a,'sites','molecule',x1),token_add_edge(a,'sites','molecule',x2)]) # n3 molecules of A-X3 n3 = 25 for i in range(n3): a,x1,x2,x3 = A(), X(), X(), X() m.send_tokens([token_add_node(a),token_add_node(x1),token_add_node(x2),token_add_node(x3)]) x1.molecule = a x2.molecule = a x3.molecule = a m.send_tokens([token_add_edge(a,'sites','molecule',x1),token_add_edge(a,'sites','molecule',x2),token_add_edge(a,'sites','molecule',x3)]) # n4 molecules of A-X-bnd-X-A n4 = 10 for i in range(n4): a1,a2,x1,x2 = A(), A(), X(), X() m.send_tokens([token_add_node(a1),token_add_node(x1)]) m.send_tokens([token_add_node(a2),token_add_node(x2)]) x1.molecule = a1 x2.molecule = a2 m.send_tokens([token_add_edge(a1,'sites','molecule',x1)]) m.send_tokens([token_add_edge(a2,'sites','molecule',x2)]) bnd = Bond() m.send_tokens([token_add_node(bnd)]) bnd.add_sites(x1,x2) m.send_tokens([token_add_edge(bnd,'sites','bond',x1)]) m.send_tokens([token_add_edge(bnd,'sites','bond',x2)]) c1 = m.count_complexes() c2 = m.count('pAx') self.assertEqual(c1,n1 + n2 + n3 + n4) self.assertEqual(c2,n1 + 2*n2 + 3*n3 + 2*n4)
def test_token_passing_08(self): # Test A(x) # One-edge p_Ax = Pattern('Ax').add_node( A('a').add_sites(X('x') ) ) m = Matcher() for p in [p_Ax]: m.add_pattern(p) a001 = A() tokens = [token_add_node(a001)] m.send_tokens(tokens) n = 100 for i in range(n): x = X() tokens = [token_add_node(x)] m.send_tokens(tokens) x.set_molecule(a001) tokens = [token_add_edge(x,'molecule','sites',a001)] m.send_tokens(tokens) self.assertEqual(m.count('Ax'),n) # Test A(x,x) # Two edges p_Axx = Pattern('Axx').add_node( A('a').add_sites( X('x1'),X('x2') ) ) m = Matcher() for p in [p_Axx]: m.add_pattern(p) a001 = A() tokens = [token_add_node(a001)] m.send_tokens(tokens) n = 25 for i in range(n): x = X() tokens = [token_add_node(x)] m.send_tokens(tokens) x.set_molecule(a001) tokens = [token_add_edge(x,'molecule','sites',a001)] m.send_tokens(tokens) self.assertEqual(m.count('Axx'),n*(n-1)) # Three edges # Test A(x,x,x) p_Axxx = Pattern('Axxx').add_node( A('a').add_sites( X('x1'),X('x2'),X('x3') ) ) m = Matcher() for p in [p_Axxx]: m.add_pattern(p) a001 = A() tokens = [token_add_node(a001)] m.send_tokens(tokens) n = 25 for i in range(n): x = X() tokens = [token_add_node(x)] m.send_tokens(tokens) x.set_molecule(a001) tokens = [token_add_edge(x,'molecule','sites',a001)] m.send_tokens(tokens) self.assertEqual(m.count('Axxx'),n*(n-1)*(n-2))
def test_token_passing_09(self): # multiple var matching # Test AxT:A[id=a](X[id=X,ph=True]) # Ax:A[id=a](X[id=x]) # with joint condition Ax.[a,x] in AxT.[a,x] p_AxT = Pattern('AxT').add_node( A('a').add_sites( X('x',ph=True) ) ) p_Ax = Pattern('Ax').add_node( A('a').add_sites(X('x')) ) \ .add_expression('[a,x] in AxT.[a,x]') m = Matcher() for p in [p_AxT,p_Ax]: m.add_pattern(p) n=25 for i in range(n): a001,x001 = A(),X(ph=True) tokens = [token_add_node(a001),token_add_node(x001)] m.send_tokens(tokens) x001.set_molecule(a001) tokens = [token_add_edge(a001,'sites','molecule',x001)] m.send_tokens(tokens) self.assertEqual(m.count('Ax'),n) n=25 for i in range(n): a001,x001 = A(),X(ph=False) tokens = [token_add_node(a001),token_add_node(x001)] m.send_tokens(tokens) x001.set_molecule(a001) tokens = [token_add_edge(a001,'sites','molecule',x001)] m.send_tokens(tokens) self.assertEqual(m.count('Ax'),n) n=25 for i in range(n): x001 = X(ph=True) tokens = [token_add_node(x001)] m.send_tokens(tokens) self.assertEqual(m.count('Ax'),n)
def test_token_passing_07(self): # Test A(<empty>) p1 = Pattern('p1').add_node( A('a') ) p1.add_expression('a.sites empty') p2 = Pattern('p2').add_node( A('a') ) m = Matcher() for p in [p1,p2]: m.add_pattern(p) self.assertEqual(m.count('p1'),0) self.assertEqual(m.count('p2'),0) # Add new A: a001 = A() tok = token_add_node(a001) m.send_token(tok) self.assertEqual(m.count('p1'),1) self.assertEqual(m.count('p2'),1) # Add new X x001 = X() tok = token_add_node(x001) m.send_token(tok) self.assertEqual(m.count('p1'),1) self.assertEqual(m.count('p2'),1) # X.set_molecule(A) x001.set_molecule(a001) tok = token_add_edge(a001,'sites','molecule',x001) m.send_token(tok) self.assertEqual(m.count('p1'),0) self.assertEqual(m.count('p2'),1) # X.unset_molecule() x001.unset_molecule() tok = token_remove_edge(a001,'sites','molecule',x001) m.send_token(tok) self.assertEqual(m.count('p1'),1) self.assertEqual(m.count('p2'),1) # Remove A tok = token_remove_node(a001) m.send_token(tok) self.assertEqual(m.count('p1'),0) self.assertEqual(m.count('p2'),0)
def test_token_passing_06(self): # Test Ax:A(X[id=x]), X:X[id=x,ph=True] with condition A.x not in X.x p_X = Pattern('X').add_node( X('x',ph=True) ) p_Ax = Pattern('Ax').add_node( A('a').add_sites(X('x') ) ) \ .add_expression('x not in X.x') m = Matcher() for p in [p_X, p_Ax]: m.add_pattern(p) # a001 = A() x001 = X(ph=True) tokens = [token_add_node(a001),token_add_node(x001)] m.send_tokens(tokens) x001.set_molecule(a001) tokens = [token_add_edge(a001,'sites','molecule',x001),] m.send_tokens(tokens) self.assertEqual(m.count('X'),1) self.assertEqual(m.count('Ax'),0) # x001.ph = False tokens = [token_edit_attrs(x001,['ph'])] m.send_tokens(tokens) self.assertEqual(m.count('X'),0) self.assertEqual(m.count('Ax'),1) # x001.ph = True tokens = [token_edit_attrs(x001,['ph'])] m.send_tokens(tokens) self.assertEqual(m.count('X'),1) self.assertEqual(m.count('Ax'),0)
def test_token_passing_05(self): # Test pattern Ax:A(x[id=x]), X:X[id=x] with condition Ax.x in X.x p_X = Pattern('X').add_node( X('x',ph=True) ) p_Ax = Pattern('Ax').add_node( A('a').add_sites(X('x') ) ) \ .add_expression('x in X.x') m = Matcher() with self.assertRaises(BuildError): for p in reversed([p_X, p_Ax]): m.add_pattern(p) m = Matcher() for p in [p_X, p_Ax]: m.add_pattern(p) # x001 = X(ph=True) a001 = A() tokens = [token_add_node(a001),token_add_node(x001)] m.send_tokens(tokens) x001.set_molecule(a001) tokens = [token_add_edge(a001,'sites','molecule',x001)] m.send_tokens(tokens) self.assertEqual(m.count('X'),1) self.assertEqual(m.count('Ax'),1) # x001.ph = False tokens = [ token_edit_attrs(x001,['ph']) ] m.send_tokens(tokens) self.assertEqual(m.count('X'),0) self.assertEqual(m.count('Ax'),0)
def test_token_passing_04(self): # Test pattern A-X[ph=True,v=0] a1 = A(id='a1') x1 = X(id='x1',ph=True,v=0).set_molecule(a1) p1 = Pattern('p1').add_node(a1) m = Matcher() m.add_pattern(p1) n = m.get_pattern('p1') self.assertEqual(len(n),0) a2 = A() x2 = X(ph=True,v=0) tok = token_add_node(a2) m.send_token(tok) self.assertEqual(len(n),0) tok = token_add_node(x2) m.send_token(tok) self.assertEqual(len(n),0) x2.set_molecule(a2) tok = token_add_edge(a2,'sites','molecule',x2) m.send_token(tok) self.assertEqual(len(n),1) tok = token_add_edge(a2,'sites','molecule',x2) m.send_token(tok) self.assertEqual(len(n),1) x2.unset_molecule() tok = token_remove_edge(a2,'sites','molecule',x2) m.send_token(tok) self.assertEqual(len(n),0) tok = token_remove_edge(a2,'sites','molecule',x2) m.send_token(tok) self.assertEqual(len(n),0)
def test_rete_construction(self): bnd = Bond(id='bnd') a1 = A(id='A1').add_sites(X(id='x1',ph=True,v=0).set_bond(bnd)) a2 = A(id='A2').add_sites(X(id='x2',ph=True,v=1).set_bond(bnd)) p4 = Pattern('p4').add_node(a1) p5 = Pattern('p5').add_node( A(id='a3') ) m = Matcher() for p in [p4,p5]: m.add_pattern(p) textgen = m.rete_net.draw_as_gml(as_string=True) text = '''graph [ directed 1 node [id 0 graphics [ hasOutline 0 fill "#FBB4AE" ] LabelGraphics [text "(0)root" ] ] node [id 1 graphics [ hasOutline 0 fill "#DECBE4" ] LabelGraphics [text "(1)*0.bond--*1.sites" ] ] node [id 10 graphics [ hasOutline 0 fill "#B3CDE3" ] LabelGraphics [text "(10)p4:x2,p4:bnd" ] ] node [id 11 graphics [ hasOutline 0 fill "#DECBE4" ] LabelGraphics [text "(11)*0.molecule--*1.sites" ] ] node [id 12 graphics [ hasOutline 0 fill "#E5D8BD" ] LabelGraphics [text "(12)store" ] ] node [id 13 graphics [ hasOutline 0 fill "#B3CDE3" ] LabelGraphics [text "(13)p4:x1,p4:A1" ] ] node [id 14 graphics [ hasOutline 0 fill "#FFFFCC" ] LabelGraphics [text "(14)p4:A1,p4:x1" ] ] node [id 15 graphics [ hasOutline 0 fill "#FFFFCC" ] LabelGraphics [text "(15)p4:A1,p4:x1" ] ] node [id 16 graphics [ hasOutline 0 fill "#B3CDE3" ] LabelGraphics [text "(16)p4:x2,p4:A2" ] ] node [id 17 graphics [ hasOutline 0 fill "#FED9A6" ] LabelGraphics [text "(17)isinstance(*,Entity)" ] ] node [id 18 graphics [ hasOutline 0 fill "#FED9A6" ] LabelGraphics [text "(18)isinstance(*,Bond)" ] ] node [id 19 graphics [ hasOutline 0 fill "#E5D8BD" ] LabelGraphics [text "(19)store" ] ] node [id 2 graphics [ hasOutline 0 fill "#E5D8BD" ] LabelGraphics [text "(2)store" ] ] node [id 20 graphics [ hasOutline 0 fill "#B3CDE3" ] LabelGraphics [text "(20)p4:bnd" ] ] node [id 21 graphics [ hasOutline 0 fill "#FED9A6" ] LabelGraphics [text "(21)isinstance(*,Molecule)" ] ] node [id 22 graphics [ hasOutline 0 fill "#FED9A6" ] LabelGraphics [text "(22)isinstance(*,A)" ] ] node [id 23 graphics [ hasOutline 0 fill "#E5D8BD" ] LabelGraphics [text "(23)store" ] ] node [id 24 graphics [ hasOutline 0 fill "#B3CDE3" ] LabelGraphics [text "(24)p4:A1" ] ] node [id 25 graphics [ hasOutline 0 fill "#B3CDE3" ] LabelGraphics [text "(25)p4:A2" ] ] node [id 26 graphics [ hasOutline 0 fill "#B3CDE3" ] LabelGraphics [text "(26)p5:a3" ] ] node [id 27 graphics [ hasOutline 0 fill "#FED9A6" ] LabelGraphics [text "(27)isinstance(*,Site)" ] ] node [id 28 graphics [ hasOutline 0 fill "#FED9A6" ] LabelGraphics [text "(28)isinstance(*,X)" ] ] node [id 29 graphics [ hasOutline 0 fill "#CCEBC5" ] LabelGraphics [text "(29)*.ph==True *.v==0" ] ] node [id 3 graphics [ hasOutline 0 fill "#B3CDE3" ] LabelGraphics [text "(3)p4:x1,p4:bnd" ] ] node [id 30 graphics [ hasOutline 0 fill "#E5D8BD" ] LabelGraphics [text "(30)store" ] ] node [id 31 graphics [ hasOutline 0 fill "#B3CDE3" ] LabelGraphics [text "(31)p4:x1" ] ] node [id 32 graphics [ hasOutline 0 fill "#CCEBC5" ] LabelGraphics [text "(32)*.ph==True *.v==1" ] ] node [id 33 graphics [ hasOutline 0 fill "#E5D8BD" ] LabelGraphics [text "(33)store" ] ] node [id 34 graphics [ hasOutline 0 fill "#B3CDE3" ] LabelGraphics [text "(34)p4:x2" ] ] node [id 4 graphics [ hasOutline 0 fill "#FFFFCC" ] LabelGraphics [text "(4)p4:A1,p4:bnd,p4:x1" ] ] node [id 5 graphics [ hasOutline 0 fill "#FFFFCC" ] LabelGraphics [text "(5)p4:A1,p4:bnd,p4:x1" ] ] node [id 6 graphics [ hasOutline 0 fill "#FFFFCC" ] LabelGraphics [text "(6)p4:A1,p4:bnd,p4:x1,p4:x2" ] ] node [id 7 graphics [ hasOutline 0 fill "#FFFFCC" ] LabelGraphics [text "(7)p4:A1,p4:bnd,p4:x1,p4:x2" ] ] node [id 8 graphics [ hasOutline 0 fill "#FFFFCC" ] LabelGraphics [text "(8)p4:A1,p4:A2,p4:bnd,p4:x1,p4:x2" ] ] node [id 9 graphics [ hasOutline 0 fill "#FFFFCC" ] LabelGraphics [text "(9)p4:A1,p4:A2,p4:bnd,p4:x1,p4:x2" ] ] edge [ source 0 target 1 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 0 target 11 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 0 target 17 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 1 target 2 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 10 target 6 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 11 target 12 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 12 target 13 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 12 target 16 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 13 target 14 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 14 target 15 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 15 target 4 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 16 target 8 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 17 target 18 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 17 target 21 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 17 target 27 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 18 target 19 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 19 target 20 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 2 target 10 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 2 target 3 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 20 target 5 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 21 target 22 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 22 target 23 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 23 target 24 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 23 target 25 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 23 target 26 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 24 target 14 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 25 target 9 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 27 target 28 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 28 target 29 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 28 target 32 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 29 target 30 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 3 target 4 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 30 target 31 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 31 target 15 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 32 target 33 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 33 target 34 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 34 target 7 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 4 target 5 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 5 target 6 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 6 target 7 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 7 target 8 graphics [ fill "#999999" targetArrow "standard" ] ] edge [ source 8 target 9 graphics [ fill "#999999" targetArrow "standard" ] ] ] ''' textgen_split = [x.strip() for x in textgen.split('\n')] text_split = [x.strip() for x in text.split('\n')] self.assertEqual(len(textgen_split),len(text_split)) for line1,line2 in zip(textgen_split,text_split): self.assertEqual(line1,line2) return