99https://en.wikipedia.org/wiki/Blackboard_system
1010"""
1111
12- from __future__ import annotations
13-
14- import abc
12+ from abc import ABC , abstractmethod
1513import random
1614
15+ class AbstractExpert (ABC ):
16+ """Abstract class for experts in the blackboard system."""
17+ @abstractmethod
18+ def __init__ (self , blackboard : object ) -> None :
19+ self .blackboard = blackboard
20+
21+ @property
22+ @abstractmethod
23+ def is_eager_to_contribute (self ):
24+ raise NotImplementedError ("Must provide implementation in subclass." )
25+
26+ @abstractmethod
27+ def contribute (self ):
28+ raise NotImplementedError ("Must provide implementation in subclass." )
29+
1730
1831class Blackboard :
32+ """The blackboard system that holds the common state."""
1933 def __init__ (self ) -> None :
2034 self .experts = []
2135 self .common_state = {
@@ -30,6 +44,7 @@ def add_expert(self, expert: AbstractExpert) -> None:
3044
3145
3246class Controller :
47+ """The controller that manages the blackboard system."""
3348 def __init__ (self , blackboard : Blackboard ) -> None :
3449 self .blackboard = blackboard
3550
@@ -45,21 +60,11 @@ def run_loop(self):
4560 return self .blackboard .common_state ["contributions" ]
4661
4762
48- class AbstractExpert (metaclass = abc .ABCMeta ):
49- def __init__ (self , blackboard : Blackboard ) -> None :
50- self .blackboard = blackboard
51-
52- @property
53- @abc .abstractmethod
54- def is_eager_to_contribute (self ):
55- raise NotImplementedError ("Must provide implementation in subclass." )
56-
57- @abc .abstractmethod
58- def contribute (self ):
59- raise NotImplementedError ("Must provide implementation in subclass." )
60-
61-
6263class Student (AbstractExpert ):
64+ """Concrete class for a student expert."""
65+ def __init__ (self , blackboard ) -> None :
66+ super ().__init__ (blackboard )
67+
6368 @property
6469 def is_eager_to_contribute (self ) -> bool :
6570 return True
@@ -72,6 +77,10 @@ def contribute(self) -> None:
7277
7378
7479class Scientist (AbstractExpert ):
80+ """Concrete class for a scientist expert."""
81+ def __init__ (self , blackboard ) -> None :
82+ super ().__init__ (blackboard )
83+
7584 @property
7685 def is_eager_to_contribute (self ) -> int :
7786 return random .randint (0 , 1 )
@@ -84,6 +93,9 @@ def contribute(self) -> None:
8493
8594
8695class Professor (AbstractExpert ):
96+ def __init__ (self , blackboard ) -> None :
97+ super ().__init__ (blackboard )
98+
8799 @property
88100 def is_eager_to_contribute (self ) -> bool :
89101 return True if self .blackboard .common_state ["problems" ] > 100 else False
0 commit comments