Turi Create  4.0
unity_sgraph_lazy_ops.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef UNITY_SGRAPH_LAZY_OPS_HPP
7 #define UNITY_SGRAPH_LAZY_OPS_HPP
8 #include <core/storage/sgraph_data/sgraph.hpp>
9 #include<core/storage/sframe_interface/unity_sgraph.hpp>
10 
11 /**************************************************************************/
12 /* */
13 /* Lazy operators on unity_sgraph */
14 /* */
15 /**************************************************************************/
16 
17 namespace turi {
18 
19  typedef lazy_eval_operation_base<sgraph> operator_type;
20 
21  template<typename DataType>
22  struct add_vertices_op: operator_type {
23  add_vertices_op(std::shared_ptr<DataType> data,
24  const std::string& id_field_name,
25  size_t group=0):
26  data(data), id_field_name(id_field_name), group(group) { }
27 
28  virtual size_t num_arguments() { return 1; }
29 
30  virtual void execute(sgraph& output,
31  const std::vector<sgraph*>& parents) {
32  output.add_vertices(*data, id_field_name);
33  }
34 
35  std::shared_ptr<DataType> data;
36  const std::string id_field_name;
37  const size_t group;
38  };
39 
40  template<typename DataType>
41  struct add_edges_op: operator_type {
42  add_edges_op(std::shared_ptr<DataType> data,
43  const std::string& source_field_name,
44  const std::string& target_field_name,
45  size_t groupa = 0, size_t groupb = 0):
46  data(data),
47  source_field_name(source_field_name),
48  target_field_name(target_field_name),
49  groupa(groupa), groupb(groupb) { }
50 
51  virtual size_t num_arguments() { return 1; }
52 
53  virtual void execute(sgraph& output,
54  const std::vector<sgraph*>& parents) {
55  output.add_edges(*data, source_field_name, target_field_name, groupa, groupb);
56  }
57  std::shared_ptr<DataType> data;
58  const std::string source_field_name;
59  const std::string target_field_name;
60  const size_t groupa, groupb;
61  };
62 
63  struct copy_vertex_field_op: operator_type {
64  copy_vertex_field_op(const std::string& field,
65  const std::string& new_field,
66  size_t group = 0):
67  field(field), new_field(new_field), group(group){ }
68 
69  virtual size_t num_arguments() { return 1; }
70 
71  virtual void execute(sgraph& output,
72  const std::vector<sgraph*>& parents) {
73  output.copy_vertex_field(field, new_field, group);
74  }
75  const std::string field;
76  const std::string new_field;
77  const size_t group;
78  };
79 
80  struct copy_edge_field_op: operator_type {
81  copy_edge_field_op(const std::string& field,
82  const std::string& new_field,
83  size_t groupa = 0, size_t groupb = 0):
84  field(field), new_field(new_field), groupa(groupa), groupb(groupb){ }
85 
86  virtual size_t num_arguments() { return 1; }
87 
88  virtual void execute(sgraph& output,
89  const std::vector<sgraph*>& parents) {
90  output.copy_edge_field(field, new_field, groupa, groupb);
91  }
92  const std::string field;
93  const std::string new_field;
94  const size_t groupa, groupb;
95  };
96 
97  struct delete_vertex_field_op: operator_type {
98  delete_vertex_field_op(const std::string& field, size_t group = 0):
99  field(field), group(group) { }
100 
101  virtual size_t num_arguments() { return 1; }
102 
103  virtual void execute(sgraph& output,
104  const std::vector<sgraph*>& parents) {
105  output.remove_vertex_field(field, group);
106  }
107  const std::string field;
108  const size_t group;
109  };
110 
111  struct delete_edge_field_op: operator_type {
112  delete_edge_field_op(const std::string& field, size_t groupa = 0, size_t groupb = 0):
113  field(field), groupa(groupa), groupb(groupb) { }
114 
115  virtual size_t num_arguments() { return 1; }
116 
117  virtual void execute(sgraph& output,
118  const std::vector<sgraph*>& parents) {
119  output.remove_edge_field(field, groupa, groupb);
120  }
121  const std::string field;
122  const size_t groupa, groupb;
123  };
124 
125  struct select_vertex_fields_op: operator_type {
126  select_vertex_fields_op(const std::vector<std::string>& _fields, size_t group) :
127  group(group) {
128  std::set<std::string> unique_fields;
129  for (const auto& f: _fields) {
130  if (!unique_fields.count(f)) {
131  fields.push_back(f);
132  unique_fields.insert(unique_fields.end(), f);
133  }
134  }
135  DASSERT_TRUE(unique_fields.count(sgraph::VID_COLUMN_NAME) > 0);
136  }
137 
138  virtual size_t num_arguments() { return 1; }
139 
140  virtual void execute(sgraph& output,
141  const std::vector<sgraph*>& parents) {
142  output.select_vertex_fields(fields, group);
143  }
144  std::vector<std::string> fields;
145  const size_t group;
146  };
147 
148  struct select_edge_fields_op: operator_type {
149  select_edge_fields_op(const std::vector<std::string>& _fields, size_t groupa, size_t groupb) :
150  groupa(groupa), groupb(groupb) {
151  std::set<std::string> unique_fields;
152  for (const auto& f: _fields) {
153  if (!unique_fields.count(f)) {
154  fields.push_back(f);
155  unique_fields.insert(unique_fields.end(), f);
156  }
157  }
158  DASSERT_TRUE(unique_fields.count(sgraph::SRC_COLUMN_NAME) > 0);
159  DASSERT_TRUE(unique_fields.count(sgraph::DST_COLUMN_NAME) > 0);
160  }
161 
162  virtual size_t num_arguments() { return 1; }
163 
164  virtual void execute(sgraph& output,
165  const std::vector<sgraph*>& parents) {
166  output.select_edge_fields(fields, groupa, groupb);
167  }
168  std::vector<std::string> fields;
169  const size_t groupa, groupb;
170  };
171 }
172 
173 #endif
virtual void execute(T &output, const std::vector< T *> &parents)=0
sframe group(sframe sframe_in, std::string key_column)
#define DASSERT_TRUE(cond)
Definition: assertions.hpp:364