Coverage for skema/skema_py/petris.py: 65%

31 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-30 17:15 +0000

1from skema.skema_py.acsets import Ob, Hom, Attr, AttrType, Schema, ACSet 

2 

3from typing import Tuple, List 

4 

5Species = Ob("S") 

6Transition = Ob("T") 

7Input = Ob("I") 

8Output = Ob("O") 

9 

10hom_it = Hom("it", Input, Transition) 

11hom_is = Hom("is", Input, Species) 

12hom_ot = Hom("ot", Output, Transition) 

13hom_os = Hom("os", Output, Species) 

14 

15Name = AttrType("Name", str) 

16 

17attr_tname = Attr("tname", Transition, Name) 

18attr_sname = Attr("sname", Species, Name) 

19 

20SchPetri = Schema( 

21 [Species, Transition, Input, Output], 

22 [hom_it, hom_is, hom_ot, hom_os], 

23 [Name], 

24 [attr_tname, attr_sname] 

25) 

26 

27class Petri(ACSet): 

28 def __init__(self, schema=SchPetri): 

29 super(Petri, self).__init__(schema) 

30 

31 def add_species(self, n: int) -> range: 

32 return self.add_parts(Species, n) 

33 

34 def add_transitions(self, transitions: List[Tuple[List[int], List[int]]]) -> range: 

35 ts = self.add_parts(Transition, len(transitions)) 

36 for (t, (ins, outs)) in zip(ts, transitions): 

37 for s in ins: 

38 arc = self.add_part(Input) 

39 self.set_subpart(arc, hom_it, t) 

40 self.set_subpart(arc, hom_is, s) 

41 for t in ins: 

42 arc = self.add_part(Output) 

43 self.set_subpart(arc, hom_ot, t) 

44 self.set_subpart(arc, hom_os, s) 

45 return ts