9
9
https://en.wikipedia.org/wiki/Blackboard_system
10
10
"""
11
11
12
- from __future__ import annotations
13
-
14
- import abc
12
+ from abc import ABC , abstractmethod
15
13
import random
16
14
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
+
17
30
18
31
class Blackboard :
32
+ """The blackboard system that holds the common state."""
19
33
def __init__ (self ) -> None :
20
34
self .experts = []
21
35
self .common_state = {
@@ -30,6 +44,7 @@ def add_expert(self, expert: AbstractExpert) -> None:
30
44
31
45
32
46
class Controller :
47
+ """The controller that manages the blackboard system."""
33
48
def __init__ (self , blackboard : Blackboard ) -> None :
34
49
self .blackboard = blackboard
35
50
@@ -45,21 +60,11 @@ def run_loop(self):
45
60
return self .blackboard .common_state ["contributions" ]
46
61
47
62
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
-
62
63
class Student (AbstractExpert ):
64
+ """Concrete class for a student expert."""
65
+ def __init__ (self , blackboard ) -> None :
66
+ super ().__init__ (blackboard )
67
+
63
68
@property
64
69
def is_eager_to_contribute (self ) -> bool :
65
70
return True
@@ -72,6 +77,10 @@ def contribute(self) -> None:
72
77
73
78
74
79
class Scientist (AbstractExpert ):
80
+ """Concrete class for a scientist expert."""
81
+ def __init__ (self , blackboard ) -> None :
82
+ super ().__init__ (blackboard )
83
+
75
84
@property
76
85
def is_eager_to_contribute (self ) -> int :
77
86
return random .randint (0 , 1 )
@@ -84,6 +93,9 @@ def contribute(self) -> None:
84
93
85
94
86
95
class Professor (AbstractExpert ):
96
+ def __init__ (self , blackboard ) -> None :
97
+ super ().__init__ (blackboard )
98
+
87
99
@property
88
100
def is_eager_to_contribute (self ) -> bool :
89
101
return True if self .blackboard .common_state ["problems" ] > 100 else False
0 commit comments