Skip to content

Commit 798ab3a

Browse files
committed
readers: implement jgf_shorthand reader
Problem: the `jgf_shorthand` reader is the same as the regular JGF reader, meaning it cannot properly handle the `jgf_shorthand` format. Add a virtual method to the JGF reader that, when updating a graph using JGF, can fetch additional vertices and treat them as if they were included in the JGF. When the `jgf_shorthand` reader encounters an exclusive vertex, make it recursively fetch all child vertices of that vertex.
1 parent 467b22f commit 798ab3a

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

resource/readers/resource_reader_jgf.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,20 +967,42 @@ int resource_reader_jgf_t::update_vertices (resource_graph_t &g,
967967
int rc = -1;
968968
unsigned int i = 0;
969969
fetch_helper_t fetcher;
970+
std::vector<fetch_helper_t> additional_vertices;
971+
std::map<std::string, vmap_val_t> empty_vmap{};
970972

971973
for (i = 0; i < json_array_size (nodes); i++) {
972974
fetcher.scrub ();
975+
additional_vertices.clear ();
973976
if ((rc = unpack_vtx (json_array_get (nodes, i), fetcher)) != 0)
974977
goto done;
975978
if ((rc = update_vtx (g, m, vmap, fetcher, update_data)) != 0)
976979
goto done;
980+
if (fetch_additional_vertices (g, m, empty_vmap, fetcher, additional_vertices) != 0)
981+
goto done;
982+
for (auto it = additional_vertices.begin (); it != additional_vertices.end (); ++it) {
983+
std::string vertex_id = std::to_string (it->uniq_id);
984+
it->vertex_id = vertex_id.c_str ();
985+
if ((rc = update_vtx (g, m, vmap, *it, update_data)) != 0) {
986+
goto done;
987+
}
988+
}
977989
}
978990
rc = 0;
979991

980992
done:
981993
return rc;
982994
}
983995

996+
int resource_reader_jgf_t::fetch_additional_vertices (
997+
resource_graph_t &g,
998+
resource_graph_metadata_t &m,
999+
std::map<std::string, vmap_val_t> &vmap,
1000+
fetch_helper_t &fetcher,
1001+
std::vector<fetch_helper_t> &additional_vertices)
1002+
{
1003+
return 0;
1004+
}
1005+
9841006
int resource_reader_jgf_t::unpack_edge (json_t *element,
9851007
std::map<std::string, vmap_val_t> &vmap,
9861008
std::string &source,

resource/readers/resource_reader_jgf.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ class resource_reader_jgf_t : public resource_reader_base_t {
240240
std::map<std::string, vmap_val_t> &vmap,
241241
json_t *nodes,
242242
jgf_updater_data &updater_data);
243+
virtual int fetch_additional_vertices (resource_graph_t &g,
244+
resource_graph_metadata_t &m,
245+
std::map<std::string, vmap_val_t> &vmap,
246+
fetch_helper_t &fetcher,
247+
std::vector<fetch_helper_t> &additional_vertices);
243248
int unpack_edge (json_t *element,
244249
std::map<std::string, vmap_val_t> &vmap,
245250
std::string &source,

resource/readers/resource_reader_jgf_shorthand.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,62 @@ using namespace Flux::resource_model;
2020
resource_reader_jgf_shorthand_t::~resource_reader_jgf_shorthand_t ()
2121
{
2222
}
23+
24+
int resource_reader_jgf_shorthand_t::fetch_additional_vertices (
25+
resource_graph_t &g,
26+
resource_graph_metadata_t &m,
27+
std::map<std::string, vmap_val_t> &vmap,
28+
fetch_helper_t &fetcher,
29+
std::vector<fetch_helper_t> &additional_vertices)
30+
{
31+
int rc = -1;
32+
vtx_t v = boost::graph_traits<resource_graph_t>::null_vertex ();
33+
if (!fetcher.exclusive) // vertex isn't exclusive, nothing to do
34+
return 0;
35+
36+
if ((rc = resource_reader_jgf_t::find_vtx (g, m, vmap, fetcher, v)) != 0)
37+
return rc;
38+
39+
return recursively_collect_vertices (g, v, additional_vertices);
40+
}
41+
42+
int resource_reader_jgf_shorthand_t::recursively_collect_vertices (
43+
resource_graph_t &g,
44+
vtx_t v,
45+
std::vector<fetch_helper_t> &additional_vertices)
46+
{
47+
static const subsystem_t containment_sub{"containment"};
48+
f_out_edg_iterator_t ei, ei_end;
49+
vtx_t target;
50+
51+
if (v == boost::graph_traits<resource_graph_t>::null_vertex ()) {
52+
return -1;
53+
}
54+
55+
for (boost::tie (ei, ei_end) = boost::out_edges (v, g); ei != ei_end; ++ei) {
56+
if (g[*ei].subsystem != containment_sub)
57+
continue;
58+
target = boost::target (*ei, g);
59+
60+
fetch_helper_t vertex_copy;
61+
vertex_copy.type = g[target].type.c_str ();
62+
vertex_copy.basename = g[target].basename.c_str ();
63+
vertex_copy.size = g[target].size;
64+
vertex_copy.uniq_id = g[target].uniq_id;
65+
vertex_copy.rank = g[target].rank;
66+
vertex_copy.status = g[target].status;
67+
vertex_copy.id = g[target].id;
68+
vertex_copy.name = g[target].name;
69+
vertex_copy.properties = g[target].properties;
70+
vertex_copy.paths = g[target].paths;
71+
vertex_copy.unit = g[target].unit.c_str ();
72+
if (resource_reader_jgf_t::apply_defaults (vertex_copy, g[target].name.c_str ()) < 0)
73+
return -1;
74+
75+
additional_vertices.push_back (vertex_copy);
76+
if (recursively_collect_vertices (g, target, additional_vertices) < 0) {
77+
return -1;
78+
}
79+
}
80+
return 0;
81+
}

resource/readers/resource_reader_jgf_shorthand.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ namespace resource_model {
2525
class resource_reader_jgf_shorthand_t : public resource_reader_jgf_t {
2626
public:
2727
virtual ~resource_reader_jgf_shorthand_t ();
28+
29+
protected:
30+
int fetch_additional_vertices (resource_graph_t &g,
31+
resource_graph_metadata_t &m,
32+
std::map<std::string, vmap_val_t> &vmap,
33+
fetch_helper_t &fetcher,
34+
std::vector<fetch_helper_t> &additional_vertices) override;
35+
36+
int recursively_collect_vertices (resource_graph_t &g,
37+
vtx_t v,
38+
std::vector<fetch_helper_t> &additional_vertices);
2839
};
2940

3041
} // namespace resource_model

0 commit comments

Comments
 (0)