Menu

[r12]: / trunk / libtop_engine / rdf_rule_cback.h  Maximize  Restore  History

Download this file

233 lines (181 with data), 7.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#ifndef RDF_RULE_CBACK_H_
#define RDF_RULE_CBACK_H_
#include "rdf_rule_core.h"
namespace rule {
class knowledge_rule;
class rule_session;
namespace internal {
class knowledge_term;
class beta_relation;
class rule_term_base;
typedef std::tr1::shared_ptr<rule_term_base> rule_term_ptr_type;
};
};
namespace rdf {
class index_triple_cback {
typedef rule::internal::rule_vertex vertex_t;
public:
index_triple_cback(rule::knowledge_rule const* rule_p, rule::internal::knowledge_term const* kterm_p, vertex_t const u, vertex_t const v):
m_krule_p(rule_p),
m_kterm_p(kterm_p),
m_u(u), m_v(v) {};
index_triple_cback():
m_krule_p(NULL),
m_kterm_p(NULL),
m_u(0), m_v(0) {};
index_triple_cback(index_triple_cback const& rhs):
m_krule_p(rhs.m_krule_p),
m_kterm_p(rhs.m_kterm_p),
m_u(rhs.m_u), m_v(rhs.m_v) {};
void triple_inserted(rule::rule_session *session, index_type s, index_type p, index_type o)const;
void triple_deleted (rule::rule_session *session, index_type s, index_type p, index_type o)const;
protected:
/* Called by triple_inserted / triple_deleted */
void perform_merge_row (
rule::rule_session *session_p,
rule::internal::beta_relation& relation,
rule::internal::rule_term_ptr_type const& rule_term,
index_type const s, index_type const p, index_type const o)const;
void perform_remove_row(
rule::rule_session *session_p,
rule::internal::beta_relation& relation,
rule::internal::rule_term_ptr_type const& rule_term,
index_type const s, index_type const p, index_type const o)const;
private:
rule::knowledge_rule const* m_krule_p;
rule::internal::knowledge_term const* m_kterm_p;
vertex_t m_u, m_v;
};
typedef std::tr1::shared_ptr<index_triple_cback> index_triple_cback_ptr_type;
// class to manage the callbacks, callbacks are dependent on schema graph, not user session graph
// therefore the lookups are factored out and constructed by knowledge_base
typedef index_triple_cback_ptr_type cback_t;
typedef std::vector<cback_t> cback_map_t;
typedef std::tr1::unordered_multimap<index_type , cback_t> cback_u_map_t;
typedef std::tr1::unordered_multimap<internal::uv_t, cback_t, internal::hash_uv, internal::eq_uv> cback_uv_map_t;
class index_triple_cback_mgr {
public:
inline index_triple_cback_mgr(char const lookup):
m_lookup(lookup),
m_cback_map(),
m_cback_u_map(),
m_cback_uv_map() {};
void register_call_back(index_triple_cback_ptr_type const& callback)
{
m_cback_map.push_back(callback);
};
void register_call_back(index_type u, index_triple_cback_ptr_type const& callback)
{
m_cback_u_map.insert(cback_u_map_t::value_type(u, callback));
};
void register_call_back(index_type u, index_type v, index_triple_cback_ptr_type const& callback)
{
m_cback_uv_map.insert(cback_uv_map_t::value_type(internal::uv_t(u, v), callback));
};
inline void triple_inserted(rule::rule_session *session, index_type u, index_type v, index_type w)const
{
index_type s;
index_type p;
index_type o;
lookup_spo(m_lookup, s, p, o, u, v, w);
cback_map_t::const_iterator itor = m_cback_map.begin();
cback_map_t::const_iterator end = m_cback_map.end();
for(; itor!=end; ++itor) (*itor)->triple_inserted(session, s, p, o);
if(!m_cback_u_map.empty()) {
cback_u_map_t::const_iterator itor;
cback_u_map_t::const_iterator end;
boost::tie(itor, end) = m_cback_u_map.equal_range(u);
for(; itor!=end; ++itor) itor->second->triple_inserted(session, s, p, o);
};
if(!m_cback_uv_map.empty()) {
cback_uv_map_t::const_iterator itor;
cback_uv_map_t::const_iterator end;
boost::tie(itor, end) = m_cback_uv_map.equal_range(internal::uv_t(u, v));
for(; itor!=end; ++itor) itor->second->triple_inserted(session, s, p, o);
};
};
inline void triple_deleted(rule::rule_session *session, index_type u, index_type v, index_type w)const
{
index_type s;
index_type p;
index_type o;
lookup_spo(m_lookup, s, p, o, u, v, w);
cback_map_t::const_iterator itor = m_cback_map.begin();
cback_map_t::const_iterator end = m_cback_map.end();
for(; itor!=end; ++itor) (*itor)->triple_deleted(session, s, p, o);
if(!m_cback_u_map.empty()) {
cback_u_map_t::const_iterator itor;
cback_u_map_t::const_iterator end;
boost::tie(itor, end) = m_cback_u_map.equal_range(u);
for(; itor!=end; ++itor) itor->second->triple_deleted(session, s, p, o);
};
if(!m_cback_uv_map.empty()) {
cback_uv_map_t::const_iterator itor;
cback_uv_map_t::const_iterator end;
boost::tie(itor, end) = m_cback_uv_map.equal_range(internal::uv_t(u, v));
for(; itor!=end; ++itor) itor->second->triple_deleted(session, s, p, o);
};
};
inline unsigned int
size()const
{
return m_cback_map.size() + m_cback_u_map.size() + m_cback_uv_map.size();
};
private:
char const m_lookup;
cback_map_t m_cback_map;
cback_u_map_t m_cback_u_map;
cback_uv_map_t m_cback_uv_map;
};
class rdf_cback_mgr {
public:
inline rdf_cback_mgr():
m_spo_cback_mgr('s'),
m_pos_cback_mgr('p'),
m_osp_cback_mgr('o') {};
inline void register_call_back(all_subjects, all_predicates, all_objects, index_triple_cback_ptr_type const& callback)
{
m_spo_cback_mgr.register_call_back(callback);
};
inline void register_call_back(index_type s, index_type p, all_objects, index_triple_cback_ptr_type const& callback)
{
m_spo_cback_mgr.register_call_back(s, p, callback);
};
inline void register_call_back(index_type s, all_predicates , all_objects, index_triple_cback_ptr_type const& callback)
{
m_spo_cback_mgr.register_call_back(s, callback);
};
inline void register_call_back(all_subjects, index_type p, index_type o, index_triple_cback_ptr_type const& callback)
{
m_pos_cback_mgr.register_call_back(p, o, callback);
};
inline void register_call_back(all_subjects, index_type p, all_objects, index_triple_cback_ptr_type const& callback)
{
m_pos_cback_mgr.register_call_back(p, callback);
};
inline void register_call_back(index_type s, all_predicates, index_type o, index_triple_cback_ptr_type const& callback)
{
m_osp_cback_mgr.register_call_back(o, s, callback);
};
inline void register_call_back(all_subjects , all_predicates, index_type o, index_triple_cback_ptr_type const& callback)
{
m_osp_cback_mgr.register_call_back(o, callback);
};
inline index_triple_cback_mgr const& get_spo_cback_mgr()const{return m_spo_cback_mgr;};
inline index_triple_cback_mgr const& get_pos_cback_mgr()const{return m_pos_cback_mgr;};
inline index_triple_cback_mgr const& get_osp_cback_mgr()const{return m_osp_cback_mgr;};
private:
friend std::ostream& operator<<(std::ostream& out, rdf_cback_mgr const& mgr);
index_triple_cback_mgr m_spo_cback_mgr;
index_triple_cback_mgr m_pos_cback_mgr;
index_triple_cback_mgr m_osp_cback_mgr;
};
inline std::ostream& operator<<(std::ostream& out, rdf_cback_mgr const& mgr)
{
out << "rdf_cback_mgr at " << &mgr << ", which have " <<
(mgr.m_spo_cback_mgr.size() + mgr.m_pos_cback_mgr.size() + mgr.m_osp_cback_mgr.size()) <<
" callback functions registered";
return out;
};
}; /*rdf namespace */
#endif /*RDF_RULE_CBACK_H_*/
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.