Skip to content

Commit

Permalink
Merge pull request #49 from smn-snkl/abstract_forms
Browse files Browse the repository at this point in the history
Abstract Action Detection
  • Loading branch information
tmbo authored Feb 27, 2019
2 parents fc12d54 + b4c32de commit 5da8d76
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This project adheres to `Semantic Versioning`_ starting with version 0.11.0.

Added
-----
- Abstract Actions can now be subclassed
- add warning in case of mismatched version of rasa_core and rasa_core_sdk

Changed
Expand Down
5 changes: 4 additions & 1 deletion rasa_core_sdk/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,11 @@ def register_package(self, package):
actions = utils.all_subclasses(Action)

for action in actions:
meta = action.__dict__.get('Meta', False)
abstract = getattr(meta, 'abstract', False)
if (not action.__module__.startswith("rasa_core.") and
not action.__module__.startswith("rasa_core_sdk.")):
not action.__module__.startswith("rasa_core_sdk.") and
not abstract):
self.register_action(action)

@staticmethod
Expand Down
49 changes: 49 additions & 0 deletions tests/test_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from rasa_core_sdk import Action, Tracker
from rasa_core_sdk.events import SlotSet
from rasa_core_sdk.executor import ActionExecutor, CollectingDispatcher


class CustomActionBase(Action):
@classmethod
def name(cls):
# Name method needed to test if base action was registered
return "base_action"

class Meta:
abstract = True

@staticmethod
def some_common_feature():
return "test"

def run(self, dispatcher, tracker, domain):
raise NotImplementedError


class CustomAction(CustomActionBase):

@classmethod
def name(cls):
return "custom_action"

def run(self, dispatcher, tracker, domain):
return [SlotSet('test', self.some_common_feature())]


def test_abstract_action():
executor = ActionExecutor()
executor.register_package('tests')
assert CustomAction.name() in executor.actions
assert CustomActionBase.name() not in executor.actions

dispatcher = CollectingDispatcher()
tracker = Tracker('test', {}, {}, [], False, None, {}, 'listen')
domain = {}

events = CustomAction().run(dispatcher, tracker, domain)
assert events == [SlotSet('test', "test")]

0 comments on commit 5da8d76

Please sign in to comment.