Skip to content

Commit

Permalink
Add a flattened wrapper on property graph to run apps defined on simp…
Browse files Browse the repository at this point in the history
…le graph (#888)

Signed-off-by: acezen <weibin.zen@gmail.com>
  • Loading branch information
acezen committed Nov 1, 2021
1 parent a29a964 commit d5821a1
Show file tree
Hide file tree
Showing 38 changed files with 2,084 additions and 800 deletions.
35 changes: 18 additions & 17 deletions analytical_engine/apps/boundary/edge_boundary.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
#include "grape/grape.h"

#include "apps/boundary/edge_boundary_context.h"
#include "apps/boundary/utils.h"
#include "core/app/app_base.h"

namespace gs {
Expand All @@ -46,36 +47,36 @@ class EdgeBoundary : public AppBase<FRAG_T, EdgeBoundaryContext<FRAG_T>>,
void PEval(const fragment_t& frag, context_t& ctx,
message_manager_t& messages) {
// parse input node array from json
folly::dynamic node_array_1 = folly::parseJson(ctx.nbunch1);
std::set<vid_t> node_gid_set, node_gid_set_2;
folly::dynamic source_array = folly::parseJson(ctx.nbunch1);
std::set<vid_t> source_gid_set, target_gid_set;
vid_t gid;
vertex_t u;
for (const auto& oid : node_array_1) {
if (frag.Oid2Gid(oid, gid)) {
node_gid_set.insert(gid);
for (const auto& node : source_array) {
if (frag.Oid2Gid(dynamic_to_oid<oid_t>(node), gid)) {
source_gid_set.insert(gid);
}
}
if (!ctx.nbunch2.empty()) {
auto node_array_2 = folly::parseJson(ctx.nbunch2);
for (const auto& oid : node_array_2) {
if (frag.Oid2Gid(oid, gid)) {
node_gid_set_2.insert(gid);
auto target_array = folly::parseJson(ctx.nbunch2);
for (const auto& node : target_array) {
if (frag.Oid2Gid(dynamic_to_oid<oid_t>(node), gid)) {
target_gid_set.insert(gid);
}
}
}

// get the boundary
for (auto& gid : node_gid_set) {
for (auto& gid : source_gid_set) {
if (frag.InnerVertexGid2Vertex(gid, u)) {
for (auto e : frag.GetOutgoingAdjList(u)) {
vid_t vgid = frag.Vertex2Gid(e.get_neighbor());
if (node_gid_set_2.empty()) {
if (node_gid_set.find(vgid) == node_gid_set.end()) {
ctx.boundary.insert(std::make_pair(gid, vgid));
for (auto& e : frag.GetOutgoingAdjList(u)) {
vid_t v_gid = frag.Vertex2Gid(e.get_neighbor());
if (target_gid_set.empty()) {
if (source_gid_set.find(v_gid) == source_gid_set.end()) {
ctx.boundary.insert(std::make_pair(gid, v_gid));
}
} else {
if (node_gid_set_2.find(vgid) != node_gid_set_2.end()) {
ctx.boundary.insert(std::make_pair(gid, vgid));
if (target_gid_set.find(v_gid) != target_gid_set.end()) {
ctx.boundary.insert(std::make_pair(gid, v_gid));
}
}
}
Expand Down
33 changes: 17 additions & 16 deletions analytical_engine/apps/boundary/node_boundary.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
#include "grape/grape.h"

#include "apps/boundary/node_boundary_context.h"
#include "apps/boundary/utils.h"
#include "core/app/app_base.h"

namespace gs {
Expand All @@ -46,33 +47,33 @@ class NodeBoundary : public AppBase<FRAG_T, NodeBoundaryContext<FRAG_T>>,
void PEval(const fragment_t& frag, context_t& ctx,
message_manager_t& messages) {
// parse input node array from json
folly::dynamic node_array_1 = folly::parseJson(ctx.nbunch1);
std::set<vid_t> node_gid_set, node_gid_set_2;
folly::dynamic source_array = folly::parseJson(ctx.nbunch1);
std::set<vid_t> source_gid_set, target_gid_set;
vid_t gid;
vertex_t v;
for (const auto& oid : node_array_1) {
if (frag.Oid2Gid(oid, gid)) {
node_gid_set.insert(gid);
for (const auto& node : source_array) {
if (frag.Oid2Gid(dynamic_to_oid<oid_t>(node), gid)) {
source_gid_set.insert(gid);
}
}
if (!ctx.nbunch2.empty()) {
auto node_array_2 = folly::parseJson(ctx.nbunch2);
for (const auto& oid : node_array_2) {
if (frag.Oid2Gid(oid, gid)) {
node_gid_set_2.insert(gid);
auto target_array = folly::parseJson(ctx.nbunch2);
for (const auto& node : target_array) {
if (frag.Oid2Gid(dynamic_to_oid<oid_t>(node), gid)) {
target_gid_set.insert(gid);
}
}
}

// get the boundary
for (auto& gid : node_gid_set) {
for (auto& gid : source_gid_set) {
if (frag.InnerVertexGid2Vertex(gid, v)) {
for (auto e : frag.GetOutgoingAdjList(v)) {
vid_t gid = frag.Vertex2Gid(e.get_neighbor());
if (node_gid_set.find(gid) == node_gid_set.end() &&
(node_gid_set_2.empty() ||
node_gid_set_2.find(gid) != node_gid_set_2.end())) {
ctx.boundary.insert(gid);
for (auto& e : frag.GetOutgoingAdjList(v)) {
vid_t v_gid = frag.Vertex2Gid(e.get_neighbor());
if (source_gid_set.find(v_gid) == source_gid_set.end() &&
(target_gid_set.empty() ||
target_gid_set.find(v_gid) != target_gid_set.end())) {
ctx.boundary.insert(v_gid);
}
}
}
Expand Down
43 changes: 43 additions & 0 deletions analytical_engine/apps/boundary/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/** Copyright 2020 Alibaba Group Holding Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef ANALYTICAL_ENGINE_APPS_BOUNDARY_UTILS_H_
#define ANALYTICAL_ENGINE_APPS_BOUNDARY_UTILS_H_

#include <string>

#include "folly/dynamic.h"

namespace gs {
template <typename oid_t>
oid_t dynamic_to_oid(const folly::dynamic& node) {}

template <>
int64_t dynamic_to_oid<int64_t>(const folly::dynamic& node) {
return node.asInt();
}

template <>
std::string dynamic_to_oid<std::string>(const folly::dynamic& node) {
return node.asString();
}

template <>
folly::dynamic dynamic_to_oid<folly::dynamic>(const folly::dynamic& node) {
return node;
}
} // namespace gs

#endif // ANALYTICAL_ENGINE_APPS_BOUNDARY_UTILS_H_
41 changes: 21 additions & 20 deletions analytical_engine/apps/centrality/degree/degree_centrality.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,27 @@ class DegreeCentrality
auto inner_vertices = frag.InnerVertices();
double max_degree = static_cast<double>(frag.GetTotalVerticesNum() - 1);

ForEach(inner_vertices, [&frag, &ctx, max_degree](int tid, vertex_t v) {
switch (ctx.degree_centrality_type) {
case DegreeCentralityType::IN: {
ctx.centrality[v] =
static_cast<double>(frag.GetLocalInDegree(v)) / max_degree;
break;
}
case DegreeCentralityType::OUT: {
ctx.centrality[v] =
static_cast<double>(frag.GetLocalOutDegree(v)) / max_degree;
break;
}
case DegreeCentralityType::BOTH: {
double degree = static_cast<double>(frag.GetLocalInDegree(v) +
frag.GetLocalOutDegree(v));
ctx.centrality[v] = degree / max_degree;
break;
}
}
});
ForEach(inner_vertices.begin(), inner_vertices.end(),
[&frag, &ctx, max_degree](int tid, vertex_t v) {
switch (ctx.degree_centrality_type) {
case DegreeCentralityType::IN: {
ctx.centrality[v] =
static_cast<double>(frag.GetLocalInDegree(v)) / max_degree;
break;
}
case DegreeCentralityType::OUT: {
ctx.centrality[v] =
static_cast<double>(frag.GetLocalOutDegree(v)) / max_degree;
break;
}
case DegreeCentralityType::BOTH: {
double degree = static_cast<double>(frag.GetLocalInDegree(v) +
frag.GetLocalOutDegree(v));
ctx.centrality[v] = degree / max_degree;
break;
}
}
});
}

void IncEval(const fragment_t& frag, context_t& ctx,
Expand Down
Loading

0 comments on commit d5821a1

Please sign in to comment.