1
0
Fork 0
Old engine for Continuous Time Bayesian Networks. Superseded by reCTBN. 🐍 https://github.com/madlabunimib/PyCTBN
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
PyCTBN/main_package/classes/utility/cache.py

58 lines
2.0 KiB

4 years ago
import typing
from ..structure_graph.set_of_cims import SetOfCims
4 years ago
4 years ago
class Cache:
4 years ago
"""This class acts as a cache of ``SetOfCims`` objects for a node.
4 years ago
:__list_of_sets_of_parents: a list of ``Sets`` objects of the parents to which the cim in cache at SAME
4 years ago
index is related
4 years ago
:__actual_cache: a list of setOfCims objects
"""
4 years ago
def __init__(self):
4 years ago
"""Constructor Method
"""
4 years ago
self._list_of_sets_of_parents = []
self._actual_cache = []
4 years ago
def find(self, parents_comb: typing.Set): #typing.Union[typing.Set, str]
"""
4 years ago
Tries to find in cache given the symbolic parents combination ``parents_comb`` the ``SetOfCims``
related to that ``parents_comb``.
4 years ago
:param parents_comb: the parents related to that ``SetOfCims``
:type parents_comb: Set
4 years ago
:return: A ``SetOfCims`` object if the ``parents_comb`` index is found in ``__list_of_sets_of_parents``.
4 years ago
None otherwise.
:rtype: SetOfCims
"""
try:
#print("Cache State:", self.list_of_sets_of_indxs)
#print("Look For:", parents_comb)
4 years ago
result = self._actual_cache[self._list_of_sets_of_parents.index(parents_comb)]
#print("CACHE HIT!!!!", parents_comb)
return result
except ValueError:
return None
4 years ago
def put(self, parents_comb: typing.Set, socim: SetOfCims):
4 years ago
"""Place in cache the ``SetOfCims`` object, and the related symbolic index ``parents_comb`` in
4 years ago
``__list_of_sets_of_parents``.
4 years ago
:param parents_comb: the symbolic set index
:type parents_comb: Set
:param socim: the related SetOfCims object
:type socim: SetOfCims
"""
#print("Putting in cache:", parents_comb)
4 years ago
self._list_of_sets_of_parents.append(parents_comb)
self._actual_cache.append(socim)
4 years ago
def clear(self):
4 years ago
"""Clear the contents both of ``__actual_cache`` and ``__list_of_sets_of_parents``.
"""
4 years ago
del self._list_of_sets_of_parents[:]
del self._actual_cache[:]