88
99https://en.wikipedia.org/wiki/Blackboard_system
1010"""
11+ from __future__ import annotations
1112
1213import abc
1314import random
1415
16+ from typing import List
17+
1518
1619class Blackboard :
17- def __init__ (self ):
18- self .experts = []
20+ def __init__ (self ) -> None :
21+ self .experts : List = []
1922 self .common_state = {
2023 "problems" : 0 ,
2124 "suggestions" : 0 ,
2225 "contributions" : [],
2326 "progress" : 0 , # percentage, if 100 -> task is finished
2427 }
2528
26- def add_expert (self , expert ) :
29+ def add_expert (self , expert : AbstractExpert ) -> None :
2730 self .experts .append (expert )
2831
2932
3033class Controller :
31- def __init__ (self , blackboard ) :
34+ def __init__ (self , blackboard : Blackboard ) -> None :
3235 self .blackboard = blackboard
3336
34- def run_loop (self ):
37+ def run_loop (self ) -> List [ str ] :
3538 """
3639 This function is a loop that runs until the progress reaches 100.
3740 It checks if an expert is eager to contribute and then calls its contribute method.
@@ -44,7 +47,7 @@ def run_loop(self):
4447
4548
4649class AbstractExpert (metaclass = abc .ABCMeta ):
47- def __init__ (self , blackboard ) :
50+ def __init__ (self , blackboard : Blackboard ) -> None :
4851 self .blackboard = blackboard
4952
5053 @property
@@ -59,10 +62,10 @@ def contribute(self):
5962
6063class Student (AbstractExpert ):
6164 @property
62- def is_eager_to_contribute (self ):
65+ def is_eager_to_contribute (self ) -> bool :
6366 return True
6467
65- def contribute (self ):
68+ def contribute (self ) -> None :
6669 self .blackboard .common_state ["problems" ] += random .randint (1 , 10 )
6770 self .blackboard .common_state ["suggestions" ] += random .randint (1 , 10 )
6871 self .blackboard .common_state ["contributions" ] += [self .__class__ .__name__ ]
@@ -71,10 +74,10 @@ def contribute(self):
7174
7275class Scientist (AbstractExpert ):
7376 @property
74- def is_eager_to_contribute (self ):
77+ def is_eager_to_contribute (self ) -> int :
7578 return random .randint (0 , 1 )
7679
77- def contribute (self ):
80+ def contribute (self ) -> None :
7881 self .blackboard .common_state ["problems" ] += random .randint (10 , 20 )
7982 self .blackboard .common_state ["suggestions" ] += random .randint (10 , 20 )
8083 self .blackboard .common_state ["contributions" ] += [self .__class__ .__name__ ]
@@ -83,10 +86,10 @@ def contribute(self):
8386
8487class Professor (AbstractExpert ):
8588 @property
86- def is_eager_to_contribute (self ):
89+ def is_eager_to_contribute (self ) -> bool :
8790 return True if self .blackboard .common_state ["problems" ] > 100 else False
8891
89- def contribute (self ):
92+ def contribute (self ) -> None :
9093 self .blackboard .common_state ["problems" ] += random .randint (1 , 2 )
9194 self .blackboard .common_state ["suggestions" ] += random .randint (10 , 20 )
9295 self .blackboard .common_state ["contributions" ] += [self .__class__ .__name__ ]
0 commit comments