8
8
9
9
https://en.wikipedia.org/wiki/Blackboard_system
10
10
"""
11
+ from __future__ import annotations
11
12
12
13
import abc
13
14
import random
14
15
16
+ from typing import List
17
+
15
18
16
19
class Blackboard :
17
- def __init__ (self ):
18
- self .experts = []
20
+ def __init__ (self ) -> None :
21
+ self .experts : List = []
19
22
self .common_state = {
20
23
"problems" : 0 ,
21
24
"suggestions" : 0 ,
22
25
"contributions" : [],
23
26
"progress" : 0 , # percentage, if 100 -> task is finished
24
27
}
25
28
26
- def add_expert (self , expert ) :
29
+ def add_expert (self , expert : AbstractExpert ) -> None :
27
30
self .experts .append (expert )
28
31
29
32
30
33
class Controller :
31
- def __init__ (self , blackboard ) :
34
+ def __init__ (self , blackboard : Blackboard ) -> None :
32
35
self .blackboard = blackboard
33
36
34
- def run_loop (self ):
37
+ def run_loop (self ) -> List [ str ] :
35
38
"""
36
39
This function is a loop that runs until the progress reaches 100.
37
40
It checks if an expert is eager to contribute and then calls its contribute method.
@@ -44,7 +47,7 @@ def run_loop(self):
44
47
45
48
46
49
class AbstractExpert (metaclass = abc .ABCMeta ):
47
- def __init__ (self , blackboard ) :
50
+ def __init__ (self , blackboard : Blackboard ) -> None :
48
51
self .blackboard = blackboard
49
52
50
53
@property
@@ -59,10 +62,10 @@ def contribute(self):
59
62
60
63
class Student (AbstractExpert ):
61
64
@property
62
- def is_eager_to_contribute (self ):
65
+ def is_eager_to_contribute (self ) -> bool :
63
66
return True
64
67
65
- def contribute (self ):
68
+ def contribute (self ) -> None :
66
69
self .blackboard .common_state ["problems" ] += random .randint (1 , 10 )
67
70
self .blackboard .common_state ["suggestions" ] += random .randint (1 , 10 )
68
71
self .blackboard .common_state ["contributions" ] += [self .__class__ .__name__ ]
@@ -71,10 +74,10 @@ def contribute(self):
71
74
72
75
class Scientist (AbstractExpert ):
73
76
@property
74
- def is_eager_to_contribute (self ):
77
+ def is_eager_to_contribute (self ) -> int :
75
78
return random .randint (0 , 1 )
76
79
77
- def contribute (self ):
80
+ def contribute (self ) -> None :
78
81
self .blackboard .common_state ["problems" ] += random .randint (10 , 20 )
79
82
self .blackboard .common_state ["suggestions" ] += random .randint (10 , 20 )
80
83
self .blackboard .common_state ["contributions" ] += [self .__class__ .__name__ ]
@@ -83,10 +86,10 @@ def contribute(self):
83
86
84
87
class Professor (AbstractExpert ):
85
88
@property
86
- def is_eager_to_contribute (self ):
89
+ def is_eager_to_contribute (self ) -> bool :
87
90
return True if self .blackboard .common_state ["problems" ] > 100 else False
88
91
89
- def contribute (self ):
92
+ def contribute (self ) -> None :
90
93
self .blackboard .common_state ["problems" ] += random .randint (1 , 2 )
91
94
self .blackboard .common_state ["suggestions" ] += random .randint (10 , 20 )
92
95
self .blackboard .common_state ["contributions" ] += [self .__class__ .__name__ ]
0 commit comments