Exemplo n.º 1
0
def project_dynamic_property_graph(graph, v_prop, e_prop, v_prop_type, e_prop_type):
    """Create project graph operation for nx graph.

    Args:
        graph (:class:`nx.Graph`): A nx graph.
        v_prop (str): The node attribute key to project.
        e_prop (str): The edge attribute key to project.
        v_prop_type (str): Type of the node attribute.
        e_prop_type (str): Type of the edge attribute.

    Returns:
        Operation to project a dynamic property graph. Results in a simple graph.
    """
    check_argument(graph.graph_type == types_pb2.DYNAMIC_PROPERTY)
    config = {
        types_pb2.GRAPH_NAME: utils.s_to_attr(graph.key),
        types_pb2.GRAPH_TYPE: utils.graph_type_to_attr(types_pb2.DYNAMIC_PROJECTED),
        types_pb2.V_PROP_KEY: utils.s_to_attr(v_prop),
        types_pb2.E_PROP_KEY: utils.s_to_attr(e_prop),
        types_pb2.V_DATA_TYPE: utils.s_to_attr(utils.data_type_to_cpp(v_prop_type)),
        types_pb2.E_DATA_TYPE: utils.s_to_attr(utils.data_type_to_cpp(e_prop_type)),
    }

    op = Operation(
        graph.session_id,
        types_pb2.PROJECT_GRAPH,
        config=config,
        output_types=types_pb2.GRAPH,
    )
    return op
Exemplo n.º 2
0
def arrow_to_dynamic(graph):
    """Transform a :class:`Graph` object to :class:`nx.Graph`.

    Args:
        graph (:class:`Graph`): Source graph, which type should be ARROW_PROPERTY.

    Returns:
        An op of transform arrow graph to dynamic graph with necessary configurations.
    """
    check_argument(graph.graph_type == graph_def_pb2.ARROW_PROPERTY)
    config = {
        types_pb2.GRAPH_NAME:
        utils.s_to_attr(graph.key),
        types_pb2.GRAPH_TYPE:
        utils.graph_type_to_attr(graph_def_pb2.ARROW_PROPERTY),
        types_pb2.DST_GRAPH_TYPE:
        utils.graph_type_to_attr(graph_def_pb2.DYNAMIC_PROPERTY),
        types_pb2.OID_TYPE:
        utils.s_to_attr(utils.data_type_to_cpp(graph.schema.oid_type)),
        types_pb2.VID_TYPE:
        utils.s_to_attr(utils.data_type_to_cpp(graph.schema.vid_type)),
        types_pb2.DEFAULT_LABEL_ID:
        utils.i_to_attr(graph._default_label_id),
    }
    op = Operation(
        graph.session_id,
        types_pb2.TRANSFORM_GRAPH,
        config=config,
        output_types=types_pb2.GRAPH,
    )
    return op
Exemplo n.º 3
0
def project_arrow_property_graph(
    graph,
    v_label_id,
    v_prop_id,
    e_label_id,
    e_prop_id,
    v_data_type,
    e_data_type,
    oid_type=None,
    vid_type=None,
):
    """Project arrow property graph to a simple graph.

    Args:
        graph (:class:`Graph`): Source graph, which type should be ARROW_PROPERTY
        dst_graph_key (str): The key of projected graph.
        v_label_id (int): Label id of vertex used to project.
        v_prop_id (int): Property id of vertex used to project.
        e_label_id (int): Label id of edge used to project.
        e_prop_id (int): Property id of edge used to project.

    Returns:
        An op to project `graph`, results in a simple ARROW_PROJECTED graph.
    """
    check_argument(graph.graph_type == types_pb2.ARROW_PROPERTY)
    config = {
        types_pb2.GRAPH_NAME:
        utils.s_to_attr(graph.key),
        types_pb2.GRAPH_TYPE:
        utils.graph_type_to_attr(types_pb2.ARROW_PROJECTED),
        types_pb2.V_LABEL_ID:
        utils.i_to_attr(v_label_id),
        types_pb2.V_PROP_ID:
        utils.i_to_attr(v_prop_id),
        types_pb2.E_LABEL_ID:
        utils.i_to_attr(e_label_id),
        types_pb2.E_PROP_ID:
        utils.i_to_attr(e_prop_id),
        types_pb2.OID_TYPE:
        utils.s_to_attr(oid_type),
        types_pb2.VID_TYPE:
        utils.s_to_attr(vid_type),
        types_pb2.V_DATA_TYPE:
        utils.s_to_attr(utils.data_type_to_cpp(v_data_type)),
        types_pb2.E_DATA_TYPE:
        utils.s_to_attr(utils.data_type_to_cpp(e_data_type)),
    }
    op = Operation(
        graph._session_id,
        types_pb2.PROJECT_GRAPH,
        config=config,
        output_types=types_pb2.GRAPH,
    )
    return op
Exemplo n.º 4
0
def flatten_arrow_property_graph(graph,
                                 v_prop,
                                 e_prop,
                                 v_prop_type,
                                 e_prop_type,
                                 oid_type=None,
                                 vid_type=None):
    """Flatten arrow property graph.

    Args:
        graph (:class:`nx.Graph`): A nx graph hosts an arrow property graph.
        v_prop (str): The vertex property id.
        e_prop (str): The edge property id.
        v_prop_type (str): Type of the node attribute.
        e_prop_type (str): Type of the edge attribute.
        oid_type (str): Type of oid.
        vid_type (str): Type of vid.

    Returns:
        Operation to flatten an arrow property graph. Results in a arrow flattened graph.
    """
    config = {
        types_pb2.GRAPH_NAME:
        utils.s_to_attr(graph.key),
        types_pb2.GRAPH_TYPE:
        utils.graph_type_to_attr(graph_def_pb2.ARROW_FLATTENED),
        types_pb2.DST_GRAPH_TYPE:
        utils.graph_type_to_attr(graph.graph_type),
        types_pb2.V_DATA_TYPE:
        utils.s_to_attr(utils.data_type_to_cpp(v_prop_type)),
        types_pb2.E_DATA_TYPE:
        utils.s_to_attr(utils.data_type_to_cpp(e_prop_type)),
    }
    if graph.graph_type == graph_def_pb2.ARROW_PROPERTY:
        config[types_pb2.V_PROP_KEY] = utils.s_to_attr(str(v_prop))
        config[types_pb2.E_PROP_KEY] = utils.s_to_attr(str(e_prop))
        config[types_pb2.OID_TYPE] = utils.s_to_attr(
            utils.data_type_to_cpp(oid_type))
        config[types_pb2.VID_TYPE] = utils.s_to_attr(
            utils.data_type_to_cpp(vid_type))
    else:
        config[types_pb2.V_PROP_KEY] = utils.s_to_attr(v_prop)
        config[types_pb2.E_PROP_KEY] = utils.s_to_attr(e_prop)

    op = Operation(
        graph.session_id,
        types_pb2.PROJECT_TO_SIMPLE,
        config=config,
        output_types=types_pb2.GRAPH,
    )
    return op
Exemplo n.º 5
0
    def update_from_graph_def(self, graph_def):
        if graph_def.graph_type == graph_def_pb2.ARROW_FLATTENED:
            self._graph_node._graph_type = graph_def_pb2.ARROW_FLATTENED
        check_argument(
            self._graph_node.graph_type == graph_def.graph_type,
            "Graph type doesn't match {} versus {}".format(
                self._graph_node.graph_type, graph_def.graph_type
            ),
        )
        self._key = graph_def.key
        self._directed = graph_def.directed
        self._is_multigraph = graph_def.is_multigraph
        vy_info = graph_def_pb2.VineyardInfoPb()
        graph_def.extension.Unpack(vy_info)
        self._vineyard_id = vy_info.vineyard_id
        self._oid_type = data_type_to_cpp(vy_info.oid_type)
        self._generate_eid = vy_info.generate_eid

        self._schema_path = vy_info.schema_path
        self._schema.from_graph_def(graph_def)
        self._v_labels = self._schema.vertex_labels
        self._e_labels = self._schema.edge_labels
        self._e_relationships = self._schema.edge_relationships
        # init saved_signature (must be after init schema)
        self._saved_signature = self.signature
        # create gremlin server pod asynchronously
        if self._session.eager() and gs_config.initializing_interactive_engine:
            self._interactive_instance_launching_thread = threading.Thread(
                target=self._launch_interactive_instance_impl, args=()
            )
            self._interactive_instance_launching_thread.start()
Exemplo n.º 6
0
    def template_str(self):
        self._ensure_loaded()

        # transform str/string to std::string
        oid_type = utils.normalize_data_type_str(self._oid_type)
        vid_type = self._schema.vid_type
        vdata_type = utils.data_type_to_cpp(self._schema.vdata_type)
        edata_type = utils.data_type_to_cpp(self._schema.edata_type)
        if self._graph_type == types_pb2.ARROW_PROPERTY:
            template = f"vineyard::ArrowFragment<{oid_type},{vid_type}>"
        elif self._graph_type == types_pb2.ARROW_PROJECTED:
            template = f"gs::ArrowProjectedFragment<{oid_type},{vid_type},{vdata_type},{edata_type}>"
        elif self._graph_type == types_pb2.DYNAMIC_PROJECTED:
            template = f"gs::DynamicProjectedFragment<{vdata_type},{edata_type}>"
        else:
            raise ValueError(f"Unsupported graph type: {self._graph_type}")
        return template
Exemplo n.º 7
0
 def template_str(self):
     if self._key is None:
         raise RuntimeError("graph should be registered in remote.")
     graph_type = self._graph_type
     # transform str/string to std::string
     oid_type = utils.normalize_data_type_str(self._schema.oid_type)
     vid_type = self._schema.vid_type
     vdata_type = utils.data_type_to_cpp(self._schema.vdata_type)
     edata_type = utils.data_type_to_cpp(self._schema.edata_type)
     if graph_type == types_pb2.ARROW_PROPERTY:
         template = f"vineyard::ArrowFragment<{oid_type},{vid_type}>"
     elif graph_type == types_pb2.ARROW_PROJECTED:
         template = f"gs::ArrowProjectedFragment<{oid_type},{vid_type},{vdata_type},{edata_type}>"
     elif graph_type == types_pb2.DYNAMIC_PROJECTED:
         template = f"gs::DynamicProjectedFragment<{vdata_type},{edata_type}>"
     else:
         raise ValueError(f"Unsupported graph type: {graph_type}")
     return template
Exemplo n.º 8
0
def dynamic_to_arrow(graph):
    """Create an op to transform a :class:`nx.Graph` object to :class:`Graph`.

    Args:
        graph (:class:`Graph`): Source graph, which type should be DYNAMIC_PROPERTY

    Returns: An op of transform dynamic graph to arrow graph with necessary configurations.
    """
    check_argument(graph.graph_type == graph_def_pb2.DYNAMIC_PROPERTY)
    oid_type = None
    for node in graph:
        if oid_type is None:
            oid_type = type(node)
        elif oid_type != type(node):
            raise RuntimeError(
                "The vertex type is not consistent {} vs {}, can not convert it to arrow graph"
                .format(str(oid_type), str(type(node))))
    if oid_type == int or oid_type is None:
        oid_type = utils.data_type_to_cpp(graph_def_pb2.LONG)
    elif oid_type == str:
        oid_type = utils.data_type_to_cpp(graph_def_pb2.STRING)
    else:
        raise RuntimeError("Unsupported oid type: " + str(oid_type))
    vid_type = utils.data_type_to_cpp(graph_def_pb2.ULONG)
    config = {
        types_pb2.GRAPH_NAME:
        utils.s_to_attr(graph.key),
        types_pb2.GRAPH_TYPE:
        utils.graph_type_to_attr(graph_def_pb2.ARROW_PROPERTY),
        types_pb2.DST_GRAPH_TYPE:
        utils.graph_type_to_attr(graph_def_pb2.ARROW_PROPERTY),
        types_pb2.OID_TYPE:
        utils.s_to_attr(oid_type),
        types_pb2.VID_TYPE:
        utils.s_to_attr(vid_type),
    }

    op = Operation(
        graph.session_id,
        types_pb2.TRANSFORM_GRAPH,
        config=config,
        output_types=types_pb2.GRAPH,
    )
    return op
Exemplo n.º 9
0
 def to_dict(self) -> Dict:
     options = {}
     options["delimiter"] = self.delimiter
     options["header_row"] = self.header_row
     if self.include_columns:
         options["schema"] = ",".join(self.include_columns)
     if self.column_types:
         cpp_types = [utils.data_type_to_cpp(dt) for dt in self.column_types]
         options["column_types"] = ",".join(cpp_types)
     if self.force_include_all:
         options["include_all_columns"] = self.force_include_all
     return options
Exemplo n.º 10
0
def create_app(graph, app):
    """Wrapper for create an `CREATE_APP` Operation with configuration.
    Compile and load an application after evaluated.

    Args:
        graph (:class:`Graph`): A :class:`Graph` instance
        app (:class:`App`): A :class:`App` instance.

    Returns:
        An :class:`Operation` with configuration that instruct
        analytical engine how to build the app.
    """
    config = {
        types_pb2.APP_ALGO:
        utils.s_to_attr(app.algo),
        types_pb2.GRAPH_TYPE:
        utils.graph_type_to_attr(graph.graph_type),
        types_pb2.OID_TYPE:
        utils.s_to_attr(graph.schema.oid_type),
        types_pb2.VID_TYPE:
        utils.s_to_attr(graph.schema.vid_type),
        types_pb2.V_DATA_TYPE:
        utils.s_to_attr(utils.data_type_to_cpp(graph.schema.vdata_type)),
        types_pb2.E_DATA_TYPE:
        utils.s_to_attr(utils.data_type_to_cpp(graph.schema.edata_type)),
        types_pb2.APP_SIGNATURE:
        utils.s_to_attr(app.signature),
        types_pb2.GRAPH_SIGNATURE:
        utils.s_to_attr(graph.template_sigature),
    }
    if app.gar is not None:
        config[types_pb2.GAR] = utils.bytes_to_attr(app.gar)

    opr = Operation(
        graph.session_id,
        types_pb2.CREATE_APP,
        config=config,
        output_types=types_pb2.APP,
    )
    return opr
Exemplo n.º 11
0
 def __str__(self) -> str:
     options = []
     options.append("delimiter={}".format(self.delimiter))
     options.append("header_row={}".format(self.header_row))
     if self.include_columns:
         options.append("schema={}".format(",".join(self.include_columns)))
     if self.column_types:
         cpp_types = [
             utils.data_type_to_cpp(dt) for dt in self.column_types
         ]
         options.append("column_types={}".format(",".join(cpp_types)))
     if self.force_include_all:
         options.append("include_all_columns={}".format(
             self.force_include_all))
     return "&".join(options)