Пример #1
0
class Node(object):
    """
    """

    __metaclass__ = _NodeMeta

    def __init__(self, name, parent_graph, has_default_params=True):
        """
        """
        self._parent_graph = parent_graph
        self._nodegraph = NodeGraph(self)
        self._name = name
        self._has_default_params = has_default_params

        self._input_params = OrderedDict()
        self._output_params = OrderedDict()

        self._params_created = False
        self.__init_params()
        self._params_created = True

        self._executed = False

    def __init_params(self):
        """
        Initialize node parameters.
        """
        if self._has_default_params:
            self.add_input_param("netin", NodeParamTypeRegistry.get("VariantType"))
            self.add_output_param("netout", NodeParamTypeRegistry.get("VariantType"))
        self._init_params()

    def _init_params(self):
        """
        """
        pass

    @property
    def parent(self):
        """
        Return the parent NodeGraph
        @returns NodeGraph
        """
        return self._parent_graph

    @property
    def parent_graph(self):
        """
        Return the parent NodeGraph
        @returns NodeGraph
        """
        return self._parent_graph

    @property
    def parent_node(self):
        """
        """
        return self.parent_graph._parent_node

    @property
    def nodegraph(self):
        """
        Return the node's NodeGraph
        @returns NodeGraph
        """
        return self._nodegraph

    @property
    def name(self):
        """
        Return the node's name
        @returns str
        """
        return self._name

    def rename(self, name):
        """
        Rename the node.
        @param name The new node name.
        @returns str
        """
        return self.parent_graph.rename(self, name)

    def input_param(self, name):
        """
        Get an input param by name.
        @param name. The name of the param to get.
        @returns NodeParam
        """
        return self._input_params.get(name, None)

    def output_param(self, name):
        """
        Get an output param by name.
        @param name. The name of the param to get
        @returns NodeParam
        """
        return self._output_params.get(name, None)

    def find_param(self, name):
        """
        @param name
        @returns Tuple of (param, mode)
        """
        print "Looking for", name, "on", self
        if name in self._input_params:
            print "Found Input"
            return self._input_params[name], NodeParam.INPUT
        if name in self._output_params:
            print "Found Output"
            return self._output_params[name], NodeParam.OUTPUT

        return None

    def input_connections(self):
        """
        """
        return self.parent_graph.connections.find(dst_node=self)

    def output_connections(self):
        """
        """
        return self.parent_graph.connections.find(src_node=self)

    def connections(self, src=False, dst=True):
        """
        Get the connections to this node.
        @param src If True return the connections that this node is a source to.
        @param dst If True return the connections that this node is a destination to.        
        @returns list
        """
        conns = []
        if dst:
            input_conns = self.input_connections()
            conns.extend(input_conns)
        if src:
            output_conns = self.output_connections()
            conns.extend(output_conns)

        return conns

    def _get_unique_param_name(self, name, mode):
        """
        Get a unique param name.
        @param name The base name.
        @param mode. The NodeParam mode.
        @returns str
        """
        _name = name
        inc = 1

        if mode == NodeParam.INPUT:
            existing_params = self._input_params
        else:
            existing_params = self._output_params

        while _name in existing_params:
            _name = "%s%i" % (name, inc)
            inc += 1
        return _name

    def rename_param(self, param, name):
        """
        Rename a param.
        @param param Then param to rename.
        @param name The new param name.
        @returns str
        """
        old_name = param.name
        new_name = self._get_unique_param_name(name, param.mode)

        param._name = new_name

        if param.mode == NodeParam.INPUT:
            self._input_params.pop(old_name)
            self._input_params[new_name] = param
        else:
            self._output_params.pop(old_name)
            self._output_params[new_name] = param

        return new_name

    def add_input_param(self, name, ptype, default_value=NULL_VALUE):
        """
        Add an input param to this node.
        @param name The name of the param.
        @param ptype The NodeParamType to add.
        @param default The default value to give the param.
        @returns NodeParam
        """
        param_name = self._get_unique_param_name(name, NodeParam.INPUT)
        p = NodeParam(self, param_name, ptype, NodeParam.INPUT,
                      default_value=default_value, user_param=self._params_created)
        self._input_params[param_name] = p
        return p

    def add_output_param(self, name, ptype, default_value=NULL_VALUE):
        """
        Add an output param to this node.
        @param name The name of the param.
        @param ptype The NodeParamType to add.
        @param default The default value to give the param.
        @returns NodeParm
        """
        param_name = self._get_unique_param_name(name, NodeParam.OUTPUT)
        p = NodeParam(self, param_name, ptype, NodeParam.OUTPUT,
                      default_value=default_value, user_param=self._params_created)
        self._output_params[param_name] = p
        return p

    def _evaluate(self, executor):
        """
        """
        #get all input connections
        input_connections = self.parent_graph.list_connections(dst_node=self)
        #get all input nodes
        input_nodes = [ conn.src_node for conn in input_connections ]
        #filter any nodes which have already been executed
        nodes = filter(lambda x : not in executor.executed, input_nodes)
        #filter duplicates
        nodes = list(set(nodes))

        if nodes:
            #get the first node in the list
            next_node = nodes[0]
            #if the next node is a child node or next node is that parent node, skip
            #if next_node in self.nodegraph.nodes or next_node == self.parent_node:
            #    return None
            return next_node
        return None

    def _execute(self, executor):
        """
        """
        pass

    def __str__(self):
        return self._name

    def __repr__(self):
        return self.__str__()
Пример #2
0
class Node(object):
    """
    """    

    __metaclass__ = _NodeMeta

    def __init__(self, name, parent, nodegraph=None, has_default_params=True):
        """
        """            
        self._parent = parent        
        self._nodegraph = nodegraph or NodeGraph(self)
        self._name = name
        self._has_default_params = has_default_params
        
        self._input_params = OrderedDict()
        self._output_params = OrderedDict()
        
        self._params_created = False                
        self.__init_params()
        self._params_created = True
        
        self._executed = False
            
    def __init_params(self):
        """
        Initialize node parameters.
        """                
        if self._has_default_params:
            self.add_input_param("netin", VariantType)
            self.add_output_param("netout", VariantType)           
        self._init_params()
    
    def _init_params(self):
        """
        """
        pass
            
    @property
    def parent(self):
        """
        Return the parent NodeGraph
        @returns NodeGraph
        """
        return self._parent

    @property
    def parent_node(self):
        """
        """
        return self.parent._parent_node
    
    @property
    def nodegraph(self):
        """
        Return the node's NodeGraph
        @returns NodeGraph
        """
        return self._nodegraph
        
    @property
    def name(self):
        """
        Return the node's name
        @returns str
        """
        return self._name
    
    def rename(self, name):
        """
        Rename the node.
        @param name The new node name.
        @returns str
        """
        return self.parent.rename(self, name)
    
    def input_param(self, name):
        """
        Get an input param by name.
        @param name. The name of the param to get.
        @returns NodeParam
        """
        return self._input_params.get(name, None)
    
    def output_param(self, name):
        """
        Get an output param by name.
        @param name. The name of the param to get
        @returns NodeParam
        """
        return self._output_params.get(name, None)
    
    def input_connections(self):
        """
        """
        edges = self.parent.graph.in_edges(self, data=True)
        input_connections = [ (edge[2]["src_param"], edge[2]["dst_param"]) for edge in edges]
        return input_connections
    
    def output_connections(self):
        """
        """                
        
        edges = self.parent.graph.out_edges(self, data=True)            
        output_connections = [ (edge[2]["src_param"], edge[2]["dst_param"]) for edge in edges]        
        return output_connections
        
    def connections(self, src=False, dst=True, params=True):
        """
        Get the connections to this node.
        @param src If True return the connections that this node is a source to.
        @param dst If True return the connections that this node is a destination to.
        @param params If True return node parameters, else return just nodes.
        @returns list        
        """        
        conns = []
        if params:
            if src:
                #grab the node params that this node is a src to
                edges = self.parent.graph.out_edges(self, data=True)            
                conns.extend([ edge[2]["dst_param"] for edge in edges ])
            if dst:
                #grab the node param that this node is a dst to
                edges = self.parent.graph.in_edges(self, data=True)                
                conns.extend([ edge[2]["src_param"] for edge in edges ])
        else:                
            if src:
                conns.extend(self.parent.graph.successors(self))
            if dst:
                conns.extend(self.parent.graph.predecessors(self))
            
        return conns
    
    def _get_unique_param_name(self, name, mode):
        """
        Get a unique param name.
        @param name The base name.
        @param mode. The NodeParam mode.
        @returns str
        """
        _name = name
        inc = 1
        
        if mode == NodeParam.INPUT:
            existing_params = self._input_params
        else:
            existing_params = self._output_params
            
        while _name in existing_params:
            _name = "%s%i" % (name, inc)        
            inc += 1            
        return _name
    
    def rename_param(self, param, name):
        """
        Rename a param.
        @param param Then param to rename.
        @param name The new param name.
        @returns str
        """
        old_name = param.name
        new_name = self._get_unique_param_name(name, param.mode)
        
        param._name = new_name
        
        if param.mode == NodeParam.INPUT:
            self._input_params.pop(old_name)
            self._input_params[new_name] = param
        else:
            self._output_params.pop(old_name)
            self._output_params[new_name] = param
        
        return new_name
    
    def add_input_param(self, name, ptype, default_value=NULL_VALUE):
        """
        Add an input param to this node.
        @param name The name of the param.
        @param ptype The NodeParamType to add.
        @param default The default value to give the param.
        @returns NodeParam
        """        
        param_name = self._get_unique_param_name(name, NodeParam.INPUT)        
        p = NodeParam(self, param_name, ptype, NodeParam.INPUT, 
                      default_value=default_value, user_param=self._params_created)            
        self._input_params[param_name] = p
        return p
                    
    def add_output_param(self, name, ptype, default_value=NULL_VALUE):
        """
        Add an output param to this node.
        @param name The name of the param.
        @param ptype The NodeParamType to add.
        @param default The default value to give the param.
        @returns NodeParm
        """        
        param_name = self._get_unique_param_name(name, NodeParam.OUTPUT)
        p = NodeParam(self, param_name, ptype, NodeParam.OUTPUT, 
                      default_value=default_value, user_param=self._params_created)            
        self._output_params[param_name] = p
        return p
    
    def _evaluate(self):
        """
        """        
        nodes = filter(lambda x : not x._executed, self.connections(params=False))        
        if nodes:
            next_node = nodes[0]
            if next_node in self.nodegraph.nodes or next_node == self.parent_node:
                return None
            return next_node            
        return None
    
    def _graph_execute(self):
        """        
        """        
        self._execute()        
        self._executed = True
        
    def _execute(self):
        """        
        """        
        pass
            
    def __str__(self):
        return self._name
    
    def __repr__(self):
        return self.__str__()