Esempio n. 1
0
def load_node(node_id):
    """ Return a SqNode subclass for the given id.

    Wrap a call to a Graph API that returns a GraphNode and call on
    sqfactory to parse it into a SqNode subclass.

    Required:
    id  node_id         id of node to fetch

    Returns:
    SqNode              single instance of concrete SqNode subclass

    """

    node = None

    try:
        graph_node = reader.get_node(node_id)

        if graph_node:
            factory = sqfactory.get_factory()
            node = factory.construct_node_and_edges(graph_node)

    except GraphOutputError as e:
        #logger.debug(e.reason)
        print e.reason

    return node
Esempio n. 2
0
def load_edges(node_id):
    """ Return a dict of SqEdge subclasses for the given SqNode ID.

    Wrap a call to a Graph API that returns a GraphEdge and call on
    sqfactory to parse it into a SqEdge subclass.

    Required:
    id      node_id     id of edge to fetch

    Returns:
    dict                concrete SqEdge subclasses keyed on ID

    """

    edges = None

    try:
        graph_node = reader.get_node(node_id)

        edges = {}
        factory = sqfactory.get_factory()
        for id, graph_edge in graph_node.edges().items():
            edges[id] = factory.construct_edge(graph_edge)

    except GraphOutputError as e:
        #logger.debug(e.reason)
        print e.reason

    return edges
Esempio n. 3
0
def load_edge(edge_id):
    """ Return a SqEdge subclass for the given id.

    Wrap a call to a Graph API that returns a GraphEdge and call on
    sqfactory to parse it into a SqEdge subclass.

    Required:
    id      edge_id     id of edge to fetch

    Returns:
    SqEdge              single instance of concrete SqEdge subclass

    """

    edge = None

    try:
        factory = sqfactory.get_factory()
        edge = factory.construct_edge(reader.get_edge(edge_id))

    except GraphOutputError as e:
        #logger.debug(e.reason)
        print e.reason

    return edge
Esempio n. 4
0
def load_neighbors(
        node_id,
        edge_type_pruner=None,
        node_type_return_filter=None):
    """ Load a SqNode and its specified SqEdges and neighbor SqNodes.

    Required:
    id      node_id                 SqNode id

    Optional:
    list    edge_type_pruner        list of SqEdge types to traverse
    list    node_type_return_filter list of SqNode types to return

    Returns:
    tuple                           (SqNode, dict) => (start, neighbors)

    """

    node = None
    neighbor_nodes = None

    try:
        # get node, outgoing edges, neighbor nodes
        graph_path = reader.get_path_to_neighbor_nodes(
                node_id,
                edge_type_pruner,
                node_type_return_filter)

        # load nodes and edges into SqNodes and SqEdges
        factory = sqfactory.get_factory()
        node = factory.construct_node_and_edges(graph_path.get_start_node())

        neighbor_nodes = {}
        for id, graph_node in graph_path.get_neighbor_nodes().items():
            neighbor_nodes[id] = factory.construct_node_and_edges(graph_node)

    except GraphOutputError as e:
        #logger.debug(e.reason)
        print e.reason

    # TODO: this only works for depth-1 queries because of graph fan-out, so
    # we need something different for queries of depth-2 and up.
    return (node, neighbor_nodes)
Esempio n. 5
0
def create_node_and_edges(prototype_node, prototype_edges):
    """ Create a new SqNode and its SqEdges in the database.

    Required:
    GraphProtoNode  prototype_node  unwritten version of GraphNode
    list            prototype_edges unwritten GraphEdges as GraphProtoEdges

    Return:
    SqNode                          SqNode created or None on failure

    """

    node = None

    try:
        # create and store a new GraphNode
        new_node = create_node(prototype_node)

        # point to/from id in GraphEdges-to-be at the new GraphNode
        for prototype_edge in prototype_edges:
            prototype_edge.set_node_id(new_node.id())

        # TODO: raise an error when incomplete Edge prototypes [meaning, ones
        # missing a to/from node] are passed to create_edge(). this may already
        # be happening, but it certainly isn't a specific enough error.

        # create and store new GraphEdges to/from the new GraphNode
        new_edges = create_edges(prototype_edges)

        # the new GraphNode should contain the new GraphEdges
        new_node.set_edges(new_edges)

        # load the new GraphNode and GraphEdges into SqObjects
        factory = sqfactory.get_factory()
        node = factory.construct_node_and_edges(new_node)

    except GraphInputError as e:
        # logger.debug(e.reason)
        print e.reason

    return node
Esempio n. 6
0
def load_nodes_by_property(key, value, node_type_return_filter=None):
    """ Return a list of SqNodes for a given property and node type.

    Wrap a call to a Graph API that returns a GraphNode and call on
    sqfactory to parse it into a SqNode subclass.

    Ideally, this property would be indexed in the database. In fact,
    the underlying graph or data layer may impose this restriction on
    queries and throw an error if no index exists for this property.

    Required:
    str     key                         property key to look up
    mixed   value                       property value to look up

    Optional:
    list    node_type_return_filter     list of SqNode types to return

    Returns:
    dict                                SqNodes keyed on ID (or None)

    """

    nodes = None

    try:
        graph_nodes = reader.get_nodes_by_index(
                key,
                value,
                node_type_return_filter)

        nodes = {}
        factory = sqfactory.get_factory()
        for id, graph_node in graph_nodes.items():
            nodes[id] = factory.construct_node_and_edges(graph_node)

    except GraphOutputError as e:
        #logger.debug(e.reason)
        print e.reason

    return nodes