1
|
|
|
from intelligine.core.exceptions import BestMoleculeHere, NoMolecule |
2
|
|
|
from intelligine.cst import MOLECULES_INFOS |
3
|
|
|
from intelligine.simulation.molecule.MoleculeFlavour import MoleculeFlavour |
4
|
|
|
from intelligine.simulation.molecule.Molecule import Molecule |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class MoleculesManager(): |
8
|
|
|
|
9
|
|
|
def __init__(self, context): |
10
|
|
|
self._context = context |
11
|
|
|
|
12
|
|
|
def get_flavour(self, position): |
13
|
|
|
point_molecules = self._context.metas.value.get(MOLECULES_INFOS, |
14
|
|
|
position, |
15
|
|
|
allow_empty=True, |
16
|
|
|
empty_value={}) |
17
|
|
|
return MoleculeFlavour.new_from_raw_data(point_molecules) |
18
|
|
|
|
19
|
|
|
def set_flavour(self, position, flavour): |
20
|
|
|
self._context.metas.value.set(MOLECULES_INFOS, position, flavour.get_raw_data()) |
21
|
|
|
|
22
|
|
|
def get_molecule(self, position, category, type, allow_empty=False): |
23
|
|
|
flavour = self.get_flavour(position) |
24
|
|
|
try: |
25
|
|
|
return flavour.get_molecule(category, type) |
26
|
|
|
except NoMolecule: |
27
|
|
|
if allow_empty: |
28
|
|
|
return Molecule() |
29
|
|
|
raise |
30
|
|
|
|
31
|
|
|
def increment_with_molecule(self, position, apposed_molecule, cycle_age): |
32
|
|
|
flavour = self.get_flavour(position) |
33
|
|
|
try: |
34
|
|
|
position_molecule = flavour.get_molecule(apposed_molecule.get_category(), apposed_molecule.get_type()) |
35
|
|
|
except NoMolecule: |
36
|
|
|
position_molecule = Molecule(apposed_molecule.get_category(), |
37
|
|
|
apposed_molecule.get_type(), |
38
|
|
|
distance=apposed_molecule.get_distance(), |
39
|
|
|
cycle_age=cycle_age) |
40
|
|
|
|
41
|
|
|
position_molecule.increment_intensity(apposed_molecule.get_intensity()) |
42
|
|
|
position_molecule.set_cycle_age(cycle_age) |
43
|
|
|
|
44
|
|
|
if apposed_molecule.get_distance() < position_molecule.get_distance(): |
45
|
|
|
position_molecule.set_distance(apposed_molecule.get_distance()) |
46
|
|
|
|
47
|
|
|
flavour.set_molecule(position_molecule) |
48
|
|
|
self.set_flavour(position, flavour) |
49
|
|
|
|
50
|
|
|
if apposed_molecule.get_distance() > position_molecule.get_distance(): |
51
|
|
|
raise BestMoleculeHere(position_molecule.get_distance()) |