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 = []
1922self .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 :
2730self .experts .append (expert )
2831
2932
3033class Controller :
31- def __init__ (self , blackboard ) :
34+ def __init__ (self , blackboard : Blackboard ) -> None :
3235self .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 :
4851self .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 :
6366return True
6467
65- def contribute (self ):
68+ def contribute (self )-> None :
6669self .blackboard .common_state ["problems" ] += random .randint (1 , 10 )
6770self .blackboard .common_state ["suggestions" ] += random .randint (1 , 10 )
6871self .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 :
7578return random .randint (0 , 1 )
7679
77- def contribute (self ):
80+ def contribute (self )-> None :
7881self .blackboard .common_state ["problems" ] += random .randint (10 , 20 )
7982self .blackboard .common_state ["suggestions" ] += random .randint (10 , 20 )
8083self .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 :
8790return True if self .blackboard .common_state ["problems" ] > 100 else False
8891
89- def contribute (self ):
92+ def contribute (self )-> None :
9093self .blackboard .common_state ["problems" ] += random .randint (1 , 2 )
9194self .blackboard .common_state ["suggestions" ] += random .randint (10 , 20 )
9295self .blackboard .common_state ["contributions" ] += [self .__class__ .__name__ ]
0 commit comments