# Path: pyknow/abstract.py
# class Strategy(metaclass=abc.ABCMeta):
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.resolved = dict()
# 
#     @abc.abstractmethod
#     def _update_agenda(self, agenda, added, removed):  # pragma: no cover
#         pass
# 
#     def update_agenda(self, agenda, added, removed):
#         if watchers.worth('ACTIVATIONS', 'INFO'):  # pragma: no cover
#             for act in removed:
#                 watchers.ACTIVATIONS.info(
#                     " <== %r: %s %s",
#                     getattr(act.rule, '__name__', None),
#                     ", ".join(str(f) for f in act.facts),
#                     "[EXECUTED]" if act not in agenda.activations else "")
# 
#             for act in added:
#                 watchers.ACTIVATIONS.info(
#                     " ==> %r: %s",
#                     getattr(act.rule, '__name__', None),
#                     ", ".join(str(f) for f in act.facts))
# 
#         self._update_agenda(agenda, added, removed)


from functools import lru_cache
from pyknow.abstract import Strategy
import bisect



class DepthStrategy(Strategy):
    @lru_cache()
    def get_key(self, activation):
        salience = activation.rule.salience
        facts = sorted((f['__factid__'] for f in activation.facts),
                       reverse=True)
        return (salience, facts)

    def _update_agenda(self, agenda, added, removed):
        for act in added:
            act.key = self.get_key(act)

# Path: pyknow/abstract.py
# class Strategy(metaclass=abc.ABCMeta):
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.resolved = dict()
# 
#     @abc.abstractmethod
#     def _update_agenda(self, agenda, added, removed):  # pragma: no cover
#         pass
# 
#     def update_agenda(self, agenda, added, removed):
#         if watchers.worth('ACTIVATIONS', 'INFO'):  # pragma: no cover
#             for act in removed:
#                 watchers.ACTIVATIONS.info(
#                     " <== %r: %s %s",
#                     getattr(act.rule, '__name__', None),
#                     ", ".join(str(f) for f in act.facts),
#                     "[EXECUTED]" if act not in agenda.activations else "")
# 
#             for act in added:
#                 watchers.ACTIVATIONS.info(
#                     " ==> %r: %s",
#                     getattr(act.rule, '__name__', None),
#                     ", ".join(str(f) for f in act.facts))
# 
#         self._update_agenda(agenda, added, removed)


# Path: pyknow/strategies.py
from functools import lru_cache
from pyknow.abstract import Strategy
import bisect




class DepthStrategy(Strategy):
    @lru_cache()
    def get_key(self, activation):
        salience = activation.rule.salience
        facts = sorted((f['__factid__'] for f in activation.facts),
                       reverse=True)
        return (salience, facts)

    def _update_agenda(self, agenda, added, removed):
        for act in added:
            act.key = self.get_key(act)

            bisect.insort_left(agenda.activations, act)