def inst(basenode = None, **kwargs): # if we're copying a node from a different graph, we need to intercept it here to # add it correctly with its dependencies instead of creating from scratch if basenode is not None and basenode.node.graph() != self: return self.add_object(basenode) return ontology.get_class(name)(basenode = basenode, graph = self, **kwargs)
def inst(basenode=None, **kwargs): # if we're copying a node from a different graph, we need to intercept it here to # add it correctly with its dependencies instead of creating from scratch if basenode is not None and basenode.node.graph() != self: return self.add_object(basenode) return ontology.get_class(name)(basenode=basenode, graph=self, **kwargs)
def _find_all(self, node_type=None, valid_node=lambda x: True, **kwargs): """Implementation of findAll that returns a generator.""" if isinstance(node_type, basestring): node_type = ontology.get_class(node_type) if node_type is None: nodes = self.nodes() elif issubclass(node_type, BaseObject): nodes = self.nodes_from_class(node_type) elif isinstance(node_type, tuple): nodes = set(set(self.nodes_from_class(cls)) for cls in node_type) else: raise TypeError('ObjectGraph.find_node: Invalid node type: %s' % node_type) for node in nodes: # TODO: should this go before or after the properties checking? Which is faster in general? try: if not valid_node(node): continue except Exception, e: log.warning('valid_node returned an exception: %s' % e) continue valid = True for prop, value in kwargs.items(): try: # FIXME: this doesn't work with lists of objects if isinstance(value, BaseObject): value = value.node if node.get_chained_properties(prop.split('_')) != value: valid = False break except AttributeError: valid = False break if not valid: continue if node_type is None: yield node else: if isinstance(node_type, tuple): for cls in node_type: if node.isinstance(cls): yield cls(node) break else: raise TypeError( 'ObjectGraph._find_all: asked for nodes of type %s but found this one: %s' % (node_type, node)) else: yield node_type(node)
def from_nodes_and_edges(self, nodes, edges, classes): """Create a graph from the given (nodes, edges, classes) tuple.""" from pygoo import ontology self.clear() idmap = {} for _id, node in nodes.items(): idmap[_id] = self.create_node(props = ((prop, value, None) for prop, value in node), _classes = (ontology.get_class(cls) for cls in classes[_id])) for node, name, other_node in edges: idmap[node].add_directed_edge(name, idmap[other_node])
def _find_all(self, node_type = None, valid_node = lambda x: True, **kwargs): """Implementation of findAll that returns a generator.""" if isinstance(node_type, basestring): node_type = ontology.get_class(node_type) if node_type is None: nodes = self.nodes() elif issubclass(node_type, BaseObject): nodes = self.nodes_from_class(node_type) elif isinstance(node_type, tuple): nodes = set(set(self.nodes_from_class(cls)) for cls in node_type) else: raise TypeError('ObjectGraph.find_node: Invalid node type: %s' % node_type) for node in nodes: # TODO: should this go before or after the properties checking? Which is faster in general? try: if not valid_node(node): continue except Exception, e: log.warning('valid_node returned an exception: %s' % e) continue valid = True for prop, value in kwargs.items(): try: # FIXME: this doesn't work with lists of objects if isinstance(value, BaseObject): value = value.node if node.get_chained_properties(prop.split('_')) != value: valid = False break except AttributeError: valid = False break if not valid: continue if node_type is None: yield node else: if isinstance(node_type, tuple): for cls in node_type: if node.isinstance(cls): yield cls(node) break else: raise TypeError('ObjectGraph._find_all: asked for nodes of type %s but found this one: %s' % (node_type, node)) else: yield node_type(node)
def from_nodes_and_edges(self, nodes, edges, classes): """Create a graph from the given (nodes, edges, classes) tuple.""" from pygoo import ontology self.clear() idmap = {} for _id, node in nodes.items(): idmap[_id] = self.create_node(props=((prop, value, None) for prop, value in node), _classes=(ontology.get_class(cls) for cls in classes[_id])) for node, name, other_node in edges: idmap[node].add_directed_edge(name, idmap[other_node])