Ejemplo n.º 1
0
def build_range(i, ex, args):
    """A range iterator hive"""
    i.min_value = hive.variable("int")
    i.max_value = hive.variable("int")
    i.step = hive.variable("int")

    i.pull_min_value = hive.pull_in(i.min_value)
    i.pull_max_value = hive.pull_in(i.max_value)
    i.pull_step = hive.pull_in(i.step)

    ex.min_value = hive.antenna(i.pull_min_value)
    ex.max_value = hive.antenna(i.pull_max_value)
    ex.step = hive.antenna(i.pull_step)

    i.iterator = hive.variable("int")

    def get_range(self):
        self._iterator = range(self._min_value, self._max_value, self._step)

    i.get_range = hive.modifier(get_range)

    i.pull_iterator = hive.pull_out(i.iterator, "iterator")
    ex.iterator = hive.output(i.pull_iterator)

    hive.trigger(i.pull_iterator, i.pull_min_value, pretrigger=True)
    hive.trigger(i.pull_iterator, i.pull_max_value, pretrigger=True)
    hive.trigger(i.pull_iterator, i.pull_step, pretrigger=True)
    hive.trigger(i.pull_iterator, i.get_range, pretrigger=True)
Ejemplo n.º 2
0
def build_nand(i, ex, args):
    ex.a_value = hive.variable(("bool", ), False)
    ex.b_value = hive.variable(("bool", ), False)

    i.a = hive.pull_in(ex.a_value)
    i.b = hive.pull_in(ex.b_value)

    ex.a = hive.antenna(i.a)
    ex.b = hive.antenna(i.b)

    def on_nand(h):
        h._pull_inputs()

        if not (h.a_value and h.b_value):
            h.trig_out()

    i.trig_out = hive.triggerfunc()
    i.trig_in = hive.modifier(on_nand)

    # Update attributes before calling modifier
    i.pull_inputs = hive.triggerfunc()
    hive.trigger(i.pull_inputs, i.a, pretrigger=True)
    hive.trigger(i.pull_inputs, i.b, pretrigger=True)

    ex.trig_out = hive.hook(i.trig_out)
    ex.trig_in = hive.entry(i.trig_in)
Ejemplo n.º 3
0
def build_operator(i, ex, args, meta_args):
    """HIVE interface to python mathematical operators"""
    assert meta_args.operator in operators
    op = operators[meta_args.operator]

    i.a = hive.variable(meta_args.data_type)
    i.b = hive.variable(meta_args.data_type)

    i.pull_a = hive.pull_in(i.a)
    ex.a = hive.antenna(i.pull_a)

    i.pull_b = hive.pull_in(i.b)
    ex.b = hive.antenna(i.pull_b)
    hive.trigger(i.pull_a, i.pull_b)

    i.result = hive.variable(meta_args.data_type)
    i.pull_result = hive.pull_out(i.result)
    ex.result = hive.output(i.pull_result)

    def calc(self):
        self._result = op(self._a, self._b)

    i.run_operator = hive.modifier(calc)

    hive.trigger(i.pull_a, i.run_operator)
    hive.trigger(i.pull_result, i.pull_a, pretrigger=True)
Ejemplo n.º 4
0
def build_set(i, ex, args):
    """Perform set operation on two sets"""
    i.a = hive.variable('set')
    i.pull_a = hive.pull_in(i.a)
    ex.a = hive.antenna(i.pull_a)

    i.b = hive.variable('set')
    i.pull_b = hive.pull_in(i.b)
    ex.b = hive.antenna(i.pull_b)

    i.result = hive.variable('set')
    for op_name, op in SET_SET_OPERATIONS.items():
        pull_op = hive.pull_out(i.result)
        setattr(i, "pull_{}".format(op_name), pull_op)
        setattr(ex, op_name, hive.output(pull_op))

        def do_operation(self):
            self._result = op(self._a, self._b)

        mod = hive.modifier(do_operation)
        setattr(i, "do_{}".format(op_name), mod)

        hive.trigger(pull_op, i.pull_a, pretrigger=True)
        hive.trigger(pull_op, mod, pretrigger=True)

    hive.trigger(i.pull_a, i.pull_b)
Ejemplo n.º 5
0
def build_convert(i, ex, args, meta_args):
    i.value_in = hive.variable(meta_args.from_data_type)
    i.value_out = hive.variable(meta_args.to_data_type)

    # For push in, push out
    if meta_args.mode == "push":
        i.ppin = hive.push_in(i.value_in)
        i.ppout = hive.push_out(i.value_out)

        hive.trigger(i.ppin, i.ppout)

    else:
        i.ppin = hive.pull_in(i.value_in)
        i.ppout = hive.pull_out(i.value_out)

        hive.trigger(i.ppout, i.ppin, pretrigger=True)

    ex.value_in = hive.antenna(i.ppin)
    ex.value_out = hive.output(i.ppout)

    # For casting (explicit conversion)
    if meta_args.conversion == "cast":
        to_base_type_name = hive.get_base_data_type(meta_args.to_data_type)
        value_cls = _type_map[to_base_type_name]

        def converter(self):
            self._value_out = value_cls(self._value_in)

        i.do_conversion = hive.modifier(converter)
        hive.trigger(i.ppout, i.do_conversion, pretrigger=True)

    # For duck typing, move value through
    else:
        i.move_value = hive.modifier(move_value)
        hive.trigger(i.ppin, i.move_value)
Ejemplo n.º 6
0
def build_foreach(i, ex, args, meta_args):
    """Iterate over iterable object"""
    # Set iterable
    i.iterable = hive.variable("$iterable[int]")
    i.pull_iterable = hive.pull_in(i.iterable)
    ex.iterable = hive.antenna(i.pull_iterable)

    i.do_trig = hive.triggerfunc()
    i.trig_in = hive.triggerable(i.do_trig)
    ex.start = hive.entry(i.trig_in)

    i.break_ = hive.variable('bool', False)

    i.item = hive.variable(meta_args.data_type)
    i.push_item = hive.push_out(i.item)
    ex.item = hive.output(i.push_item)

    i.index = hive.variable('int', 0)
    i.pull_index = hive.pull_out(i.index)
    ex.index = hive.output(i.pull_index)

    i.finished = hive.triggerfunc()

    i.do_break = hive.modifier(do_break)
    ex.break_ = hive.entry(i.do_break)
    ex.finished = hive.hook(i.finished)

    i.iter = hive.modifier(do_iter)
    hive.trigger(i.do_trig, i.pull_iterable)
    hive.trigger(i.do_trig, i.iter)
Ejemplo n.º 7
0
def build_unpack_tuple(i, ex, args, meta_args):
    """Unpack a tuple from individual inputs"""
    i.tuple_ = hive.variable('tuple')
    i.push_tuple = hive.push_in(i.tuple_)
    ex.tuple_ = hive.antenna(i.push_tuple)

    for index, data_type in enumerate(meta_args.types):
        attr = hive.variable(data_type)
        setattr(i, "attr_{}".format(index), attr)

        pull_out = hive.pull_out(attr)
        setattr(i, "pull_out_{}".format(index), pull_out)

        setattr(ex, "item_{}".format(index), hive.output(pull_out))

    def do_unpack_tuple(self):
        tuple_ = self._tuple_
        data_types = meta_args.types

        assert len(tuple_) == len(data_types)

        for index, item in enumerate(tuple_):
            setattr(self, "_attr_{}".format(index), item)

    i.do_unpack_tuple = hive.modifier(do_unpack_tuple)
    hive.trigger(i.push_tuple, i.do_unpack_tuple)
Ejemplo n.º 8
0
def build_determinant(i, ex, args):
    """Calculate the determinant (length) of a vector"""
    i.vector = hive.variable("vector")
    i.pull_vector = hive.pull_in(i.vector)
    ex.vector = hive.antenna(i.pull_vector)

    i.result = hive.variable("float")
    i.pull_result = hive.pull_out(i.result)
    ex.result = hive.output(i.pull_result)

    i.calculate = hive.modifier(length_modifier)
    hive.trigger(i.pull_result, i.calculate, pretrigger=True)
Ejemplo n.º 9
0
def build_normalise(i, ex, args):
    """Find the unit vector for a given vector"""
    i.vector = hive.variable("vector")
    i.pull_vector = hive.pull_in(i.vector)
    ex.vector = hive.antenna(i.pull_vector)

    i.result = hive.variable("vector")
    i.pull_result = hive.pull_out(i.result)
    ex.result = hive.output(i.pull_result)

    i.calculate = hive.modifier(normalise_modifier)
    hive.trigger(i.pull_result, i.calculate, pretrigger=True)
Ejemplo n.º 10
0
def build_struct(cls, i, ex, args):
    """Interface to Struct class"""
    i.tuple = hive.variable("tuple")
    i.pack_in = hive.push_in(i.tuple)
    ex.pack = hive.antenna(i.pack_in)

    i.bytes = hive.variable("bytes")
    i.unpack_out = hive.push_out(i.bytes)
    ex.unpack = hive.output(i.unpack_out)

    i.size_out = hive.pull_out(cls.size)
    ex.size = hive.output(i.size_out)
Ejemplo n.º 11
0
def build_generator(i, ex, args):
    """Define and instantiate a new generator when pulled"""
    args.generator_body = hive.parameter(("str", "code"))

    ex.generator = hive.variable()
    ex.generator_body = hive.variable(("str", "code"), args.generator_body)

    i.create_generator = hive.modifier(on_new_generator)

    i.generator_out = hive.pull_out(ex.generator)
    ex.generator_out = hive.output(i.generator_out)

    hive.trigger(i.generator_out, i.create_generator, pretrigger=True)
Ejemplo n.º 12
0
def build_format(i, ex, args, meta_args):
    """Interface to Python string value formatting"""
    formatter = string.Formatter()
    format_string = meta_args.format_string
    fields = list(formatter.parse(format_string))

    kwarg_fields = []
    indexed_fields = []

    i.result = hive.variable('str')
    i.result_out = hive.pull_out(i.result)

    for index, field in enumerate(fields):
        literal_text = field[1]

        if literal_text is None:
            continue

        if not literal_text.isidentifier():
            field_name = "field_{}".format(index)
            indexed_fields.append(field_name)

        else:
            field_name = literal_text
            kwarg_fields.append(field_name)

        # Create IO
        attr = hive.variable()
        setattr(i, field_name, attr)

        in_attr = hive.pull_in(attr)
        setattr(i, "{}_in".format(field_name), in_attr)

        setattr(ex, field_name, hive.antenna(in_attr))
        hive.trigger(i.result_out, in_attr, pretrigger=True)

    ex.result = hive.output(i.result_out)

    def do_format(self):
        args = [
            getattr(self, "_{}".format(attr_name))
            for attr_name in indexed_fields
        ]
        kwargs = {
            attr_name: getattr(self, "_{}".format(attr_name))
            for attr_name in kwarg_fields
        }
        self._result = formatter.format(format_string, *args, **kwargs)

    i.func = hive.modifier(do_format)
    hive.trigger(i.result_out, i.func, pretrigger=True)
Ejemplo n.º 13
0
def build_func(i, ex, args, meta_args):
    """Define callable object from expression"""
    # Get AST and parse ags
    ast_node = ast.parse(meta_args.definition, mode='exec')
    assert isinstance(ast_node, ast.Module)
    assert len(ast_node.body) == 1
    func_def_ast = ast_node.body[0]
    assert isinstance(func_def_ast, ast.FunctionDef)

    visitor = FunctionDefinitionVisitor()
    visitor.visit(ast_node)

    # Build the function itself
    user_namespace = {}
    exec(meta_args.definition, user_namespace)
    user_func = user_namespace[func_def_ast.name]

    has_return = visitor.output_type_name is not function_no_return

    # Define modifier source code (here, we will lookup the "user_func" name later)
    function_call_str = "user_func({})".format(", ".join(
        ["self._{}".format(a) for a in visitor.antennae]))
    result_body = result_str if has_return else ""
    modifier_decl = modifier_str.format(function_call_str=function_call_str,
                                        result_body=result_body)

    # Build modifier function
    namespace = {'user_func': user_func}
    exec(modifier_decl, namespace)
    modifier_func = namespace['modifier']

    # Create modifier bees
    i.modifier = hive.modifier(modifier_func)
    ex.trigger = hive.entry(i.modifier)

    i.pull_all_inputs = hive.triggerfunc()

    # Create IO pins
    for arg, (type_name, default) in visitor.antennae.items():
        attr = hive.variable(type_name, start_value=default)
        setattr(i, arg, attr)
        pull_in = hive.pull_in(attr)
        setattr(ex, arg, hive.antenna(pull_in))
        hive.trigger(i.pull_all_inputs, pull_in, pretrigger=True)

    if has_return:
        result_name = 'result'
        attr = hive.variable(visitor.output_type_name)
        setattr(i, result_name, attr)
        push_out = hive.push_out(attr)
        setattr(ex, result_name, hive.output(push_out))
Ejemplo n.º 14
0
def build_keyboard(cls, i, ex, args):
    ex.on_event = hive.socket(cls.add_single_listener,
                              identifier="event.add_handler")
    i.on_tick = hive.triggerfunc()

    ex.name = hive.variable(("str", ), "<Sensor>")
    ex.key = hive.property(cls, "key", "str")
    ex.is_positive = hive.variable(("bool", ), False)

    i.positive = hive.pull_out(ex.is_positive)
    ex.positive = hive.output(i.positive)

    i.trig_out = hive.triggerfunc()
    ex.trig_out = hive.hook(i.trig_out)
Ejemplo n.º 15
0
def build_iter(i, ex, args, meta_args):
    """Create iterator for object"""
    i.iterable = hive.variable()
    i.pull_iterable = hive.pull_in(i.iterable)
    ex.iterable = hive.antenna(i.pull_iterable)

    i.iterator = hive.variable("iterator")
    i.pull_iterator = hive.pull_out(i.iterator)
    ex.iterator = hive.output(i.pull_iterator)

    i.do_iter = hive.modifier(do_iter)

    hive.trigger(i.pull_iterator, i.pull_iterable, pretrigger=True)
    hive.trigger(i.pull_iterable, i.do_iter)
Ejemplo n.º 16
0
def build_import(i, ex, args):
    """Interface to python import mechanism"""
    i.do_import = hive.modifier(do_import_from_path)

    i.import_path = hive.variable("str")
    i.pull_import_path = hive.pull_in(i.import_path)
    ex.import_path = hive.antenna(i.pull_import_path)

    i.module = hive.variable("module")
    i.pull_module = hive.pull_out(i.module)
    ex.module = hive.output(i.pull_module)

    hive.trigger(i.pull_module, i.pull_import_path, pretrigger=True)
    hive.trigger(i.pull_module, i.do_import, pretrigger=True)
Ejemplo n.º 17
0
def build_always(i, ex, args):
    ex.name = hive.variable(("str", ), "<Sensor>")
    ex.is_positive = hive.variable(("bool", ), True)

    i.positive = hive.pull_out(ex.is_positive)
    ex.positive = hive.output(i.positive)

    def trigger(h):
        h.trig_out()

    i.trig_in = hive.modifier(trigger)
    ex.trig_in = hive.entry(i.trig_in)

    i.trig_out = hive.triggerfunc()
    ex.trig_out = hive.hook(i.trig_out)
Ejemplo n.º 18
0
def build_set(i, ex, args, meta_args):
    """Perform set operation on single sets"""

    i.set_ = hive.variable('set', set())
    i.pull_set_ = hive.pull_out(i.set_)
    ex.set_out = hive.output(i.pull_set_)

    i.push_set_ = hive.push_in(i.set_)
    ex.set_ = hive.antenna(i.push_set_)

    # Pop item
    i.popped_item = hive.variable(meta_args.data_type)
    i.pull_pop = hive.pull_out(i.popped_item)
    ex.pop = hive.output(i.pull_pop)

    def do_pop(self):
        self._popped_item = self._set_.pop()

    i.do_pop = hive.modifier(do_pop)
    hive.trigger(i.pull_pop, i.do_pop, pretrigger=True)

    # Add item
    i.to_add = hive.variable(meta_args.data_type)
    i.push_to_add = hive.push_in(i.to_add)
    ex.add = hive.antenna(i.push_to_add)

    def to_add(self):
        self._set_.add(self._to_add)

    i.do_add = hive.modifier(to_add)
    hive.trigger(i.push_to_add, i.do_add)

    # Remove item
    i.to_remove = hive.variable(meta_args.data_type)
    i.push_to_remove = hive.push_in(i.to_remove)
    ex.remove = hive.antenna(i.push_to_remove)

    def do_remove(self):
        self._set_.remove(self._to_remove)

    i.do_remove = hive.modifier(do_remove)
    hive.trigger(i.push_to_remove, i.do_remove)

    def do_clear(self):
        self._set_.clear()

    i.do_clear = hive.modifier(do_clear)
    ex.clear = hive.entry(i.do_clear)
Ejemplo n.º 19
0
def build_buffer(i, ex, args, meta_args):
    """Store the input value and output saved value.

    In pull mode, the trigger is used to update the internal value.
    In push mode, the trigger is used to output the internal value.

    Can be used to cache changing values
    """
    args.start_value = hive.parameter(meta_args.data_type, None)
    i.cached_value = hive.variable(meta_args.data_type, args.start_value)

    if meta_args.mode == "push":
        i.push_value = hive.push_in(i.cached_value)
        ex.value = hive.antenna(i.push_value)

        i.push_cached_value = hive.push_out(i.cached_value)
        ex.cached_value = hive.output(i.push_cached_value)

        ex.output = hive.entry(i.push_cached_value)

    elif meta_args.mode == "pull":
        i.pull_value = hive.pull_in(i.cached_value)
        ex.value = hive.antenna(i.pull_value)

        i.pull_cached_value = hive.pull_out(i.cached_value)
        ex.cached_value = hive.output(i.pull_cached_value)

        ex.update_cache = hive.entry(i.pull_value)
Ejemplo n.º 20
0
def build_debug(i, ex, args):
    def on_call(h):
        print(h.message)

    ex.message = hive.variable(("str", ), "Triggered!")
    i.trig_in = hive.modifier(on_call)
    ex.trig_in = hive.entry(i.trig_in)
Ejemplo n.º 21
0
def build_next(i, ex, args, meta_args):
    """Iterate over generator object, output new value when pulled"""
    i.iterator = hive.variable("iterator")
    i.iterator_in = hive.pull_in(i.iterator)
    ex.iterator = hive.antenna(i.iterator_in)

    i.pull_iterator = hive.triggerfunc()
    hive.trigger(i.pull_iterator, i.iterator_in)

    i.result = hive.variable(meta_args.data_type)
    i.pull_value = hive.pull_out(i.result)
    ex.value = hive.output(i.pull_value)

    i.do_next = hive.modifier(next_modifier)

    hive.trigger(i.pull_value, i.do_next, pretrigger=True)
Ejemplo n.º 22
0
def build_decompose(i, ex, args):
    """Decompose a vector into its x, y and z components"""
    i.refresh = hive.modifier(decompose_modifier)

    for name in ['x', 'y', 'z']:
        attr = hive.variable("float")
        setattr(i, name, attr)

        pull_out = hive.pull_out(attr)
        setattr(ex, name, hive.output(pull_out))

        hive.trigger(pull_out, i.refresh, pretrigger=True)

    i.vector = hive.variable("vector")
    i.pull_vector = hive.pull_in(i.vector)
    ex.vector = hive.antenna(i.pull_vector)
Ejemplo n.º 23
0
def build_input(i, ex, args):
    """Get input from Python stdin"""
    args.message = hive.parameter("str", "")
    ex.message = hive.variable("str", args.message)

    i.message_in = hive.push_in(ex.message)
    ex.message_in = hive.antenna(i.message_in)

    ex.value = hive.variable("str")
    i.value_out = hive.pull_out(ex.value)
    ex.value_out = hive.output(i.value_out)

    def get_input(self):
        self.value = input(self.message)

    i.get_input = hive.modifier(get_input)
    hive.trigger(i.value_out, i.get_input, pretrigger=True)
Ejemplo n.º 24
0
def build_cross(i, ex, args):
    """Calculate the cross product between two vectors"""
    i.a = hive.variable("vector")
    i.b = hive.variable("vector")

    pull_a = hive.pull_in(i.a)
    pull_b = hive.pull_in(i.b)

    ex.a = hive.antenna(pull_a)
    ex.b = hive.antenna(pull_b)

    i.result = hive.variable("vector")
    i.pull_result = hive.pull_out(i.result)
    ex.result = hive.output(i.pull_result)

    calculate = hive.modifier(cross_modifier)
    hive.trigger(i.pull_result, calculate, pretrigger=True)
Ejemplo n.º 25
0
def build_io(i, ex, args, meta_args):
    if meta_args.mode == 'pull':
        i.value = hive.variable()
        i.in_ = hive.pull_in(i.value)
        ex.in_ = hive.antenna(i.in_)

        hive.trigger(i.in_, )

        i.out = meta_args.out(i.value)
        ex.out = hive.output(i.out)
    else:
        i.value = hive.variable()
        i.in_ = meta_args.in_(i.value)
        ex.in_ = hive.antenna(i.in_)

        i.out = meta_args.out(i.value)
        ex.out = hive.output(i.out)
Ejemplo n.º 26
0
def build_print(i, ex, args):
    """Output object to Python stdout"""
    ex.value = hive.variable()
    i.value_in = hive.push_in(ex.value)
    ex.value_in = hive.antenna(i.value_in)

    i.func = hive.modifier(lambda self: print(self.value))

    hive.trigger(i.value_in, i.func)
Ejemplo n.º 27
0
def build_compose(i, ex, args):
    """Compose a vector from its x, y and z components"""
    i.compose_vector = hive.modifier(compose_modifier)

    i.result = hive.variable("vector")
    i.pull_result = hive.pull_out(i.result)

    for name in ('x', 'y', 'z'):
        attr = hive.variable("float")
        setattr(i, name, attr)

        pull_in = hive.pull_in(attr)
        setattr(ex, "{}".format(name), hive.antenna(pull_in))

        hive.trigger(i.pull_result, pull_in, pretrigger=True)

    hive.trigger(i.pull_result, i.compose_vector, pretrigger=True)
    ex.result = hive.output(i.pull_result)
Ejemplo n.º 28
0
def build_conjugate(i, ex, args):
    """Calculate the complex conjugate of a complex number"""
    i.value = hive.variable('complex')
    i.conjugate = hive.variable('complex')

    i.pull_conjugate = hive.pull_out(i.conjugate)
    ex.conjugate = hive.output(i.pull_conjugate)

    i.pull_value = hive.pull_in(i.value)
    ex.value = hive.antenna(i.pull_value)

    def build_conjugate(self):
        self._conjugate = self._value.conjugate()

    i.build_conjugate = hive.modifier(build_conjugate)

    hive.trigger(i.pull_conjugate, i.pull_value, pretrigger=True)
    hive.trigger(i.pull_value, i.build_conjugate)
Ejemplo n.º 29
0
def build_loads(i, ex, args):
    """Interface to JSON loads function"""
    def do_loads(self):
        self._result = loads(self._object_)

    i.result = hive.variable('str')
    i.object_ = hive.variable()

    i.pull_result = hive.pull_out(i.result)
    ex.result = hive.output(i.pull_result)

    i.pull_object = hive.pull_in(i.object_)
    ex.object_ = hive.antenna(i.pull_object)

    i.do_loads = hive.modifier(do_loads)

    hive.trigger(i.pull_result, i.pull_object, pretrigger=True)
    hive.trigger(i.pull_object, i.do_loads)
Ejemplo n.º 30
0
def build_abs(i, ex, args, meta_args):
    """Calculate the absolute abs() of a value"""
    i.value = hive.variable(meta_args.data_type)
    i.pull_value = hive.pull_in(i.value)

    i.result = hive.variable(meta_args.data_type)
    i.pull_result = hive.pull_out(i.result)

    ex.value = hive.antenna(i.pull_value)
    ex.result = hive.output(i.pull_result)

    def do_abs(self):
        self._result = abs(self._value)

    i.do_abs = hive.modifier(do_abs)

    hive.trigger(i.pull_result, i.pull_value, pretrigger=True)
    hive.trigger(i.pull_value, i.do_abs)