8000 Separate blackboard per python version · faif/python-patterns@2ce0d4e · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ce0d4e

Browse files
committed
Separate blackboard per python version
1 parent 423fe9d commit 2ce0d4e

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ __Others__:
6969

7070
| Pattern | Description |
7171
|:-------:| ----------- |
72-
| [blackboard](patterns/other/blackboard.py) | architectural model, assemble different sub-system knowledge to build a solution, AI approach - non gang of four pattern |
72+
| [blackboard](patterns/other/blackboard__py3.py) | architectural model, assemble different sub-system knowledge to build a solution, AI approach - non gang of four pattern |
7373
| [graph_search](patterns/other/graph_search.py) | graphing algorithms - non gang of four pattern |
7474
| [hsm](patterns/other/hsm/hsm.py) | hierarchical state machine - non gang of four pattern |
7575

File renamed without changes.

patterns/other/blackboard__py3.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
@author: Eugene Duboviy <eugene.dubovoy@gmail.com> | github.com/duboviy
6+
7+
In Blackboard pattern several specialised sub-systems (knowledge sources)
8+
assemble their knowledge to build a possibly partial or approximate solution.
9+
In this way, the sub-systems work together to solve the problem,
10+
where the solution is the sum of its parts.
11+
12+
https://en.wikipedia.org/wiki/Blackboard_system
13+
"""
14+
15+
import abc
16+
import random
17+
18+
19+
class Blackboard(object):
20+
def __init__(self):
21+
self.experts = []
22+
self.common_state = {
23+
'problems': 0,
24+
'suggestions': 0,
25+
'contributions': [],
26+
'progress': 0, # percentage, if 100 -> task is finished
27+
}
28+
29+
def add_expert(self, expert):
30+
self.experts.append(expert)
31+
32+
33+
class Controller(object):
34+
def __init__(self, blackboard):
35+
self.blackboard = blackboard
36+
37+
def run_loop(self):
38+
while self.blackboard.common_state['progress'] < 100:
39+
for expert in self.blackboard.experts:
40+
if expert.is_eager_to_contribute:
41+
expert.contribute()
42+
return self.blackboard.common_state['contributions']
43+
44+
45+
class AbstractExpert(metaclass=abc.ABCMeta):
46+
47+
def __init__(self, blackboard):
48+
self.blackboard = blackboard
49+
50+
@property
51+
@abc.abstractmethod
52+
def is_eager_to_contribute(self):
53+
raise NotImplementedError('Must provide implementation in subclass.')
54+
55+
@abc.abstractmethod
56+
def contribute(self):
57+
raise NotImplementedError('Must provide implementation in subclass.')
58+
59+
60+
class Student(AbstractExpert):
61+
@property
62+
def is_eager_to_contribute(self):
63+
return True
64+
65+
def contribute(self):
66+
self.blackboard.common_state['problems'] += random.randint(1, 10)
67+
self.blackboard.common_state['suggestions'] += random.randint(1, 10)
68+
self.blackboard.common_state['contributions'] += [self.__class__.__name__]
69+
self.blackboard.common_state['progress'] += random.randint(1, 2)
70+
71+
72+
class Scientist(AbstractExpert):
73+
@property
74+
def is_eager_to_contribute(self):
75+
return random.randint(0, 1)
76+
77+
def contribute(self):
78+
self.blackboard.common_state['problems'] += random.randint(10, 20)
79+
self.blackboard.common_state['suggestions'] += random.randint(10, 20)
80+
self.blackboard.common_state['contributions'] += [self.__class__.__name__]
81+
self.blackboard.common_state['progress'] += random.randint(10, 30)
82+
83+
84+
class Professor(AbstractExpert):
85+
@property
86+
def is_eager_to_contribute(self):
87+
return True if self.blackboard.common_state['problems'] > 100 else False
88+
89+
def contribute(self):
90+
self.blackboard.common_state['problems'] += random.randint(1, 2)
91+
self.blackboard.common_state['suggestions'] += random.randint(10, 20)
92+
self.blackboard.common_state['contributions'] += [self.__class__.__name__]
93+
self.blackboard.common_state['progress'] += random.randint(10, 100)
94+
95+
96+
if __name__ == '__main__':
97+
blackboard = Blackboard()
98+
99+
blackboard.add_expert(Student(blackboard))
100+
blackboard.add_expert(Scientist(blackboard))
101+
blackboard.add_expert(Professor(blackboard))
102+
103+
c = Controller(blackboard)
104+
contributions = c.run_loop()
105+
106+
from pprint import pprint
107+
108+
pprint(contributions)
109+
110+
### OUTPUT ###
111+
# ['Student',
112+
# 'Student',
113+
# 'Scientist',
114+
# 'Student',
115+
# 'Scientist',
116+
# 'Student',
117+
# 'Scientist',
118+
# 'Student',
119+
# 'Scientist',
120+
# 'Student',
121+
# 'Scientist',
122+
# 'Professor']

0 commit comments

Comments
 (0)
0