コード例 #1
0
def get_molecular_condition(lst: KQMLList) -> tra.MolecularCondition:
    try:
        condition_type = lst.gets('type')
        quantity_ref_lst = lst.get('quantity')
        quantity = get_molecular_quantity_ref(quantity_ref_lst)
        if condition_type == 'exact':
            value = get_molecular_quantity(lst.get('value'))
        elif condition_type == 'multiple':
            value = lst.gets('value')
        else:
            value = None
        return tra.MolecularCondition(condition_type, quantity, value)
    except Exception as e:
        raise tra.InvalidMolecularConditionError(e)
コード例 #2
0
def get_molecular_quantity_ref(lst: KQMLList) -> tra.MolecularQuantityReference:
    try:
        quant_type = lst.gets('type')
        entity_lst = lst.get('entity')
        entity = get_molecular_entity(entity_lst)
        return tra.MolecularQuantityReference(quant_type, entity)
    except Exception as e:
        raise tra.InvalidMolecularQuantityRefError(e)
コード例 #3
0
    def receive_achieve(self, msg: KQMLPerformative, content: KQMLList):
        """Overrides the default KQMLModule receive for achieves and instead
        does basic error checking before attempting the action by calling the
        proper ask function with the arguments passed along as inputs.

        Arguments:
            msg (KQMLPerformative): predicate/ signifier of task (message
                sent to python from companions)
            content (KQMLList): action task is referring to (content of
                message)

        Returns:
            None: returns only to exit function early if conditions aren't met
        """
        if content.head() != 'task':
            error_msg = (f'Only support achieve command of task, instead got '
                         f'{content.head()}')
            LOGGER.debug(error_msg)
            self.error_reply(msg, error_msg)
            return
        action = content.get('action')
        if not action:
            error_msg = 'No action for achieve task provided'
            LOGGER.debug(error_msg)
            self.error_reply(msg, error_msg)
            return
        if action.head() not in self.achieves:
            error_msg = f'No action named {action.head()} is known'
            LOGGER.debug(error_msg)
            self.error_reply(msg, error_msg)
            return
        achieve_question = self.achieves[action.head()]
        expected_args = len(getfullargspec(achieve_question).args)
        actual_args = action.data[1:]
        if expected_args != len(actual_args):
            error_msg = (f'Expected {expected_args} input arguments to achieve'
                         f' task {action.head()}, got {len(actual_args)}')
            LOGGER.debug(error_msg)
            self.error_reply(msg, error_msg)
            return
        LOGGER.info('received achieve %s', action.head())
        try:
            results = self.achieves[action.head()](*actual_args)
        except (TypeError, ValueError) as except_msg:
            LOGGER.debug('Failed execution: %s, %s', except_msg, print_exc())
            error_msg = f'An error occurred while executing {action.head()}'
            self.error_reply(msg, error_msg)
            return
        LOGGER.debug('Acheive returned results: %s', results)
        reply = performative(f'(tell :sender {self.name} :content '
                             f'{listify(results)})')
        self.reply(msg, reply)
コード例 #4
0
def get_temporal_pattern(lst: KQMLList) -> tra.TemporalPattern:
    pattern_type = lst.gets('type')
    entities_lst = lst.get('entities')
    entities = []
    if entities_lst is None:
        entities_lst = []
    for e in entities_lst:
        entity = get_molecular_entity(e)
        entities.append(entity)
    time_limit_lst = lst.get('time-limit')
    if time_limit_lst is None:
        time_limit = None
    else:
        time_limit = get_time_interval(time_limit_lst)
    # TODO: handle more pattern-specific extra arguments
    value_lst = lst.get('value')
    if value_lst is not None:
        value = get_molecular_quantity(value_lst)
    else:
        value = None
    tp = tra.TemporalPattern(pattern_type, entities, time_limit, value=value)
    return tp
コード例 #5
0
    def receive_subscribe(self, msg: KQMLPerformative, content: KQMLList):
        """Override of KQMLModule default, expects a performative of ask-all.
        Gets the ask-all query from the message contents, then checks
        to see if the query head is in the dictionary of available asks and
        checks if the query string is in the dictionary of subscribers. If both
        of these are true we then append the message to the subscriber query,
        clean out any previous subscription data, and reply with a tell ok
        message.

        Arguments:
            msg (KQMLPerformative): performative to be passed along to reply
                and stored in the subscribers dictionary (for future replies)
            content (KQMLList): ask-all for a query

        Returns:
            None: returns only to exit function early if conditions aren't met
        """
        if content.head() != 'ask-all':
            error_msg = (f'Only supports ask-all subscription, received '
                         f'unsupported performative {content.head()}')
            LOGGER.debug(error_msg)
            self.error_reply(msg, error_msg)
            return
        query = content.get('content')
        pattern = query.to_string()
        if query.head() not in self.asks:
            error_msg = f'No ask named {query.head()} is known'
            LOGGER.debug(error_msg)
            self.error_reply(msg, error_msg)
            return
        if pattern not in self.subscriptions:
            error_msg = f'Ask ({query.head()}) is not subscribable'
            LOGGER.debug(error_msg)
            self.error_reply(msg, error_msg)
            return
        LOGGER.info('received subscription %s to %s', msg, pattern)
        self.subscriptions.subscribe(pattern, msg)
        reply_msg = f'(tell :sender {self.name} :content :ok)'
        self.reply(msg, performative(reply_msg))
コード例 #6
0
def get_molecular_entity(lst: KQMLList) -> Agent:
    description_clj = lst.get('description')
    agent = TRA_Module.get_agent(description_clj)
    return agent
コード例 #7
0
    def receive_achieve(self, msg: KQMLPerformative, content: KQMLList):
        """Overrides the default KQMLModule receive for achieves and instead
        does basic error checking before attempting the action by calling the
        proper ask function with the arguments passed along as inputs.

        Arguments:
            msg (KQMLPerformative): predicate/ signifier of task (message
                sent to python from companions)
            content (KQMLList): action task is referring to (content of
                message)

        Returns:
            None: returns only to exit function early if conditions aren't met
        """
        if content.head() != 'task':
            error_msg = (f'Only support achieve command of task, instead got '
                         f'{content.head()}')
            LOGGER.warning(error_msg)
            self.error_reply(msg, error_msg)
            return
        action = content.get('action')
        if not action:
            error_msg = 'No action for achieve task provided'
            LOGGER.warning(error_msg)
            self.error_reply(msg, error_msg)
            return
        if action.head() not in self.achieves:
            error_msg = f'No action named {action.head()} is known'
            LOGGER.warning(error_msg)
            self.error_reply(msg, error_msg)
            return
        achieve_question = self.achieves[action.head()]
        # TODO - argument structure and call won't work if the self argument
        # is needed. Easy enough to filter out the arguments but it will be
        # harder to use self if that self argument doesn't correlate to a
        # pythonian object... ie if someone passes in a function that is from
        # inside another class.
        question_args = getfullargspec(achieve_question).args
        # call_needs_self = False
        # if question_args[0] is 'self':
        #     question_args = question_args[1:]
        #     call_needs_self = True
        expected_args = len(question_args)
        actual_args = action.data[1:]
        if expected_args != len(actual_args):
            error_msg = (f'Expected {expected_args} input arguments to achieve'
                         f' task {action.head()}, got {len(actual_args)}')
            LOGGER.warning(error_msg)
            self.error_reply(msg, error_msg)
            return
        LOGGER.info('received achieve %s', action.head())
        try:
            results = self.achieves[action.head()](*actual_args)
        except (TypeError, ValueError) as except_msg:
            LOGGER.warning('Failed execution: %s, %s', except_msg, print_exc())
            error_msg = f'An error occurred while executing {action.head()}'
            self.error_reply(msg, error_msg)
            return
        LOGGER.debug('Acheive returned results: %s', results)
        reply = performative(f'(tell :sender {self.name} :content '
                             f'{listify(results)})')
        self.reply(msg, reply)