Ejemplo n.º 1
0
class VisualeaModel(Model):
    default_name = "Workflow"
    default_file_name = "workflow.wpy"
    pattern = "*.wpy"
    extension = "wpy"
    icon = ":/images/resources/openalealogo.png"
    dtype = default_name
    mimetype = "text/x-visualea"

    def __init__(self, **kwargs):
        name = kwargs.get("name", "Workflow")
        kwargs["name"] = name
        self._workflow = CompositeNodeFactory(name).instantiate()
        super(VisualeaModel, self).__init__(**kwargs)

    def get_documentation(self):
        """

        :return: docstring of current workflow
        """
        if hasattr(self._workflow, "get_tip"):
            self._doc = self._workflow.get_tip()
        return self._doc

    def repr_code(self):
        """
        :return: a string representation of model to save it on disk
        """
        name = self.name

        if name[-3:] in ".py":
            name = name[-3:]
        elif name[-4:] in ".wpy":
            name = name[-4:]
        cn = self._workflow
        cnf = CompositeNodeFactory(name)
        cn.to_factory(cnf)

        repr_wf = repr(cnf.get_writer())
        # hack to allow eval rather than exec...
        # TODO: change the writer

        repr_wf = (" = ").join(repr_wf.split(" = ")[1:])
        return repr_wf

    def eval_value(self, value):
        return value

    def _outputs(self):
        outputs = []
        for i, outobj in enumerate(self.outputs_info):
            out = self._workflow.get_output(i)
            outputs.append(out)
            self._ns[outobj.name] = out
        if len(outputs) == 0:
            return None
        elif len(outputs) == 1:
            return outputs[0]
        else:
            return outputs

    def _set_inputs(self, *args, **kwargs):
        self._ns = self.inputs_from_ns(self.inputs_info, self._ns, *args, **kwargs)
        for i, inp in enumerate(self.inputs_info):
            self._workflow.set_input(i, self._ns[inp.name])

    def run(self, *args, **kwargs):
        """
        execute entire model
        """
        self.init(*args, **kwargs)
        self._workflow.eval()
        outputs = self._outputs()
        return outputs

    def namespace(self, **kwargs):
        from openalea.core.service.run import namespace

        return namespace(self, **kwargs)

    def init(self, *args, **kwargs):
        """
        go back to initial step
        """
        user_ns = kwargs.pop("namespace", {})
        self._ns = user_ns
        self._ns.update(self.namespace())
        self._set_inputs(*args, **kwargs)
        return self._outputs()

    def step(self, *args, **kwargs):
        """
        execute only one step of the model
        """
        raise NotImplementedError
        self._set_inputs()
        self._workflow.eval_as_expression()
        return self._outputs()

    def stop(self, *args, **kwargs):
        """
        stop execution
        """
        # TODO : to implement
        pass

    def animate(self, *args, **kwargs):
        """
        run model step by step
        """
        return self._workflow.eval()

    def execute(self, code=None):
        """
        In other paradigms: Execute code (str).
        Here this method does not have signification (only for "script-like" paradigm), so, it make a **run**.
        """
        return self.run()

    def set_code(self, code):
        self._initial_code = code
        if not code:
            self._workflow = CompositeNodeFactory(self.name).instantiate()
        elif isinstance(code, CompositeNodeFactory):
            # hakishhh
            # CompositeNodeFactory.instantiate_node = monkey_patch_instantiate_node
            self._workflow = code.instantiate()
        else:
            # Access to the current project
            cnf = eval(code, globals(), locals())
            # hakishhh
            CompositeNodeFactory.instantiate_node = monkey_patch_instantiate_node
            #             raise IOError(cnf)
            self._workflow = cnf.instantiate()

    @property
    def inputs_info(self):
        inputs = []
        for inp in self._workflow.input_desc:
            inpobj = InputObj()
            inpobj.name = inp.get("name", None)
            inpobj.interface = inp.get("interface", None)
            inpobj.default = inp.get("value", None)
            inputs.append(inpobj)
        return inputs

    @inputs_info.setter
    def inputs_info(self, inputs):
        self._workflow.clear_inputs()
        for inp in inputs:
            self._workflow.add_input(name=inp.name, value=inp.default, interface=inp.interface)

    @property
    def outputs_info(self):
        outputs = []
        for out in self._workflow.output_desc:
            outobj = OutputObj()
            outobj.name = out.get("name", None)
            outobj.interface = out.get("interface", None)
            outobj.default = out.get("value", None)
            outputs.append(outobj)
        return outputs

    @outputs_info.setter
    def outputs_info(self, outputs):
        self._workflow.clear_outputs()
        for out in outputs:
            self._workflow.add_output(name=out.name, value=out.default, interface=out.interface)
Ejemplo n.º 2
0
class VisualeaModel(Model):
    default_name = "Workflow"
    default_file_name = "workflow.wpy"
    pattern = "*.wpy"
    extension = "wpy"
    icon = ":/images/resources/openalealogo.png"
    dtype = default_name
    mimetype = "text/x-visualea"

    def __init__(self, **kwargs):
        name = kwargs.get('name', 'Workflow')
        kwargs['name'] = name
        self._workflow = CompositeNodeFactory(name).instantiate()
        super(VisualeaModel, self).__init__(**kwargs)

    def get_documentation(self):
        """

        :return: docstring of current workflow
        """
        if hasattr(self._workflow, "get_tip"):
            self._doc = self._workflow.get_tip()
        return self._doc

    def repr_code(self):
        """
        :return: a string representation of model to save it on disk
        """
        name = self.name

        if name[-3:] in '.py':
            name = name[-3:]
        elif name[-4:] in '.wpy':
            name = name[-4:]
        cn = self._workflow
        cnf = CompositeNodeFactory(name)
        cn.to_factory(cnf)

        repr_wf = repr(cnf.get_writer())
        # hack to allow eval rather than exec...
        # TODO: change the writer

        repr_wf = (' = ').join(repr_wf.split(' = ')[1:])
        return repr_wf

    def eval_value(self, value):
        return value

    def _outputs(self):
        outputs = []
        for i, outobj in enumerate(self.outputs_info):
            out = self._workflow.get_output(i)
            outputs.append(out)
            self._ns[outobj.name] = out
        if len(outputs) == 0:
            return None
        elif len(outputs) == 1:
            return outputs[0]
        else:
            return outputs

    def _set_inputs(self, *args, **kwargs):
        self._ns = self.inputs_from_ns(self.inputs_info, self._ns, *args, **kwargs)
        for i, inp in enumerate(self.inputs_info):
            self._workflow.set_input(i, self._ns[inp.name])

    def run(self, *args, **kwargs):
        """
        execute entire model
        """
        self.init(*args, **kwargs)
        self._workflow.eval()
        outputs = self._outputs()
        return outputs

    def namespace(self, **kwargs):
        from openalea.core.service.run import namespace
        return namespace(self, **kwargs)

    def init(self, *args, **kwargs):
        """
        go back to initial step
        """
        user_ns = kwargs.pop('namespace', {})
        self._ns = user_ns
        self._ns.update(self.namespace())
        self._set_inputs(*args, **kwargs)
        return self._outputs()

    def step(self, *args, **kwargs):
        """
        execute only one step of the model
        """
        raise NotImplementedError
        self._set_inputs()
        self._workflow.eval_as_expression()
        return self._outputs()

    def stop(self, *args, **kwargs):
        """
        stop execution
        """
        # TODO : to implement
        pass

    def animate(self, *args, **kwargs):
        """
        run model step by step
        """
        return self._workflow.eval()

    def execute(self, code=None):
        """
        In other paradigms: Execute code (str).
        Here this method does not have signification (only for "script-like" paradigm), so, it make a **run**.
        """
        return self.run()

    def set_code(self, code):
        self._initial_code = code
        if not code:
            self._workflow = CompositeNodeFactory(self.name).instantiate()
        elif isinstance(code, CompositeNodeFactory):
            # hakishhh
            # CompositeNodeFactory.instantiate_node = monkey_patch_instantiate_node
            self._workflow = code.instantiate()
        else:
            # Access to the current project
            cnf = eval(code, globals(), locals())
            # hakishhh
            CompositeNodeFactory.instantiate_node = monkey_patch_instantiate_node
#             raise IOError(cnf)
            self._workflow = cnf.instantiate()

    @property
    def inputs_info(self):
        inputs = []
        for inp in self._workflow.input_desc:
            inpobj = InputObj()
            inpobj.name = inp.get('name', None)
            inpobj.interface = inp.get('interface', None)
            inpobj.default = inp.get('value', None)
            inputs.append(inpobj)
        return inputs

    @inputs_info.setter
    def inputs_info(self, inputs):
        self._workflow.clear_inputs()
        for inp in inputs:
            self._workflow.add_input(name=inp.name, value=inp.default, interface=inp.interface)

    @property
    def outputs_info(self):
        outputs = []
        for out in self._workflow.output_desc:
            outobj = OutputObj()
            outobj.name = out.get('name', None)
            outobj.interface = out.get('interface', None)
            outobj.default = out.get('value', None)
            outputs.append(outobj)
        return outputs

    @outputs_info.setter
    def outputs_info(self, outputs):
        self._workflow.clear_outputs()
        for out in outputs:
            self._workflow.add_output(name=out.name, value=out.default, interface=out.interface)