Пример #1
0
 def _browseCreatedNetwork(self, rete):
     self._gWrapper = NetworkXGraphWrapper.i()
     from icse.rete.ReteNetwork import ReteNetwork
     from icse.rete.Nodes import ReteNode
     from collections import deque
     assert isinstance(rete, ReteNetwork)
     nodeQueue = deque([(rete.get_root(), None, 0)])
     readed_saw = set()
     readed_exploded = set()
     while len(nodeQueue) > 0:
         node, parent, linkType = nodeQueue.popleft()
         if node not in readed_saw:
             readed_saw.add(node)
             EventManager.trigger(EventManager.E_NODE_ADDED, node)
         if parent != None:
             EventManager.trigger(EventManager.E_NODE_LINKED, node, parent, linkType)
             
         if node not in readed_exploded:
             readed_exploded.add(node)
             # per AlphaRoot, ConstantTestNode, LengthTestNode e ReteNode
             if hasattr(node, 'get_children'):
                 linkType = -1 if isinstance(node, ReteNode) else 0
                 for succ in node.get_children():
                     nodeQueue.append( (succ, node, linkType ) )
             # per ConstantTestNode, JoinNode e NegativeNode
             if hasattr(node, 'get_alphamemory'):
                 if not isinstance(node, ReteNode):
                     if node.get_alphamemory() != None:
                         nodeQueue.append( (node.get_alphamemory(), node, 0 ) )
             # per AlphaMemory
             if hasattr(node, 'get_successors'):
                 for succ in node.get_successors():
                     linkType = 1 if isinstance(succ, ReteNode) else 0
                     nodeQueue.append( (succ, node, linkType ) )
             if hasattr(node, 'get_partner'):
                 nodeQueue.append( (node.get_partner(), node, 0 ) )
Пример #2
0
def draw_network_fragment(pnodes, rete, g = None):
    
    from icse.rete.Nodes import AlphaNode, AlphaRootNode, \
                             JoinNode, NegativeNode, NccNode, \
                             DummyJoinNode, DummyNegativeNode
    
    if g == None:
        g = NetworkXGraphWrapper.i()
        g.clear()
        
    
    visited = set()
    links = set()
    nodeStack = pnodes 
    
    while len(nodeStack) > 0:
        child = nodeStack.pop()
        # navigo prima verso il padre da sinistra
        if not isinstance(child, AlphaRootNode):
            parent_left = child.get_parent()
        else:
            parent_left = None
            
        if isinstance(child, (AlphaNode)):
            left_linkType = 0
        else:
            left_linkType = -1
        # devo prendere la memory
        # per join
        if isinstance(child, (JoinNode, NegativeNode, DummyJoinNode, DummyNegativeNode)):
            parent_right = child.get_alphamemory()
            right_linkType = 1
        elif isinstance(child, (NccNode)):
            parent_right = child.get_partner()
            right_linkType = 0
        else:
            parent_right = None
            right_linkType = 0
        
        
        if child not in visited:
            g.add_node(child)
            visited.add(child)
        if parent_left != None:
            if parent_left not in visited:
                g.add_node(parent_left)
                visited.add(parent_left)
            if (child, parent_left) not in links:
                g.add_edge(parent_left, child, left_linkType )
                links.add((child, parent_left))
        if parent_right != None:
            if parent_right not in visited:
                g.add_node(parent_right)
                visited.add(parent_right)
            if (child, parent_right) not in links:
                g.add_edge(parent_right, child, right_linkType )
                links.add((child, parent_right))
                
        # inserisco prima il padre destro e poi il sinistro
        # perche voglio che la navigazione proceda prima
        # risalendo a sinistra
        if parent_right != None and child != rete.get_root():
            nodeStack.append(parent_right)
            
        if parent_left != None and child != rete.get_root():
            nodeStack.append(parent_left)
            
    g.draw()