Menu

[r45]: / trunk / libtop_engine / psearch_session.h  Maximize  Restore  History

Download this file

465 lines (361 with data), 15.0 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#ifndef PSEARCH_SESSION_H_
#define PSEARCH_SESSION_H_
#include "rule_engine.h"
#include "psearch_db.h"
#include "psearch_query.h"
#include "psearch_infer.h"
namespace psearch {
// defined in rule_engine.h:
// class PSearchSession;
// typedef std::tr1::shared_ptr<PSearchSession> psession_ptr_type;
/////////////////////////////////////////////////////////////////////////////////////////
// class PSearchSession
//
// The has_node set remain unchanged during the inferrence process, initialized from the asserted graph.
// The inferred_has_node set is augmented with the inferred nodes and put into the inferred graph.
// The has_not nodes remain unchanged during the inferrence process, initialized from the asserted graph.
/////////////////////////////////////////////////////////////////////////////////////////
class PSearchSession
{
public:
PSearchSession(rule::knowledge_base const* kbase_p, rdf::rdf_session_ptr_type session_p):
m_kbase_p(kbase_p),
m_psearch_db_p(kbase_p->get_psearch_db()),
m_rdf_session_p(session_p),
m_subject(),
m_has_nodes(),
m_inferred_has_nodes(),
m_has_not_nodes(),
has_index(NULL),
has_not_index(NULL)
{
has_index = m_rdf_session_p->create_resource_as_index("top:has_node");
has_not_index = m_rdf_session_p->create_resource_as_index("top:has_not_node");
};
~PSearchSession(){};
inline rule::knowledge_base const*
get_knowledge_base()const
{
return m_kbase_p;
};
inline PSearchDB const*
get_psearch_db()const
{
return m_psearch_db_p;
};
inline rdf::rdf_session_ptr_type
get_rdf_session()
{
return m_rdf_session_p;
};
inline void
set_session_subject(index_type subject_index)
{
m_subject = subject_index;
// initialize the session from assertion/inferred facts from rdf session.
// has nodes
rdf::rdf_graph::index_iterator itor = m_rdf_session_p->get_asserted_graph()->find_index(m_subject, has_index, rdf::all_objects());
while(not itor.is_end()) {
index_type index = itor.get_triple().get_object();
node_index_type node_index = m_psearch_db_p->get_node_index(index);
if(node_index) {
m_has_nodes.insert(node_index);
}
itor.next();
}
// inferred has nodes - m_inferred_has_nodes
itor = m_rdf_session_p->get_inferred_graph()->find_index(m_subject, has_index, rdf::all_objects());
while(not itor.is_end()) {
index_type index = itor.get_triple().get_object();
node_index_type node_index = m_psearch_db_p->get_node_index(index);
if(node_index) {
m_inferred_has_nodes.insert(node_index);
}
itor.next();
}
// has not nodes
itor = m_rdf_session_p->get_asserted_graph()->find_index(m_subject, has_not_index, rdf::all_objects());
while(not itor.is_end()) {
index_type index = itor.get_triple().get_object();
node_index_type node_index = m_psearch_db_p->get_node_index(index);
if(node_index) {
m_has_not_nodes.insert(node_index);
}
itor.next();
}
};
inline void
set_session_subject(std::string const& name)
{
set_session_subject(m_rdf_session_p->create_resource_as_index(name));
};
inline index_type
get_session_subject()const
{
return m_subject;
};
inline unsigned int
get_nbr_nodes()const
{
return m_has_nodes.size() + m_inferred_has_nodes.size();
};
inline unsigned int
get_nbr_negated_nodes()const
{
return m_has_not_nodes.size();
};
inline psresult_ptr_type
query(query_params const& params, bool verbose)
{
if(not m_subject) throw rdf::rdf_exception(rdf::invalid_index, "ERROR-P01: psearch_session subject must be set");
PSearchQuery query(m_psearch_db_p, this);
return query.query(params, verbose);
};
// return the same psresult_ptr_type as it was psearch query but returns all
// nodes of the category (wrapped in nsc)
inline psresult_ptr_type
query(category_index_type category_index, bool verbose)
{
if(not m_subject) throw rdf::rdf_exception(rdf::invalid_index, "ERROR-P01: psearch_session subject must be set");
PSearchQuery query(m_psearch_db_p, this);
return query.query(category_index, verbose);
};
inline psresult_ptr_type
infer(bool verbose)
{
if(not m_subject) throw rdf::rdf_exception(rdf::invalid_index, "ERROR-P01: psearch_session subject must be set");
PSearchInfer infer(m_psearch_db_p, this);
return infer.infer(verbose);
};
/////////////////////////////////////////////////////////////////////////////
// has nodes methods
/////////////////////////////////////////////////////////////////////////////
// asserted nodes
inline node_const_iterator_type
get_nodes_begin()const
{
return m_has_nodes.begin();
};
inline node_const_iterator_type
get_nodes_end()const
{
return m_has_nodes.end();
};
// inferred nodes
inline node_const_iterator_type
get_inferred_nodes_begin()const
{
return m_inferred_has_nodes.begin();
};
inline node_const_iterator_type
get_inferred_nodes_end()const
{
return m_inferred_has_nodes.end();
};
// both asserted and inferred
inline node_dual_set_const_iterator
get_nodes_iterator()const
{
return node_dual_set_const_iterator(
get_nodes_begin(), get_nodes_end(),
get_inferred_nodes_begin(), get_inferred_nodes_end());
};
// asserted only
inline void
add_nodes(node_set_type const& nodes)
{
for(node_set_type::const_iterator itor=nodes.begin(); itor!=nodes.end(); ++itor) add_node(*itor);
};
inline void
add_node(node_index_type node_index)
{
if(!node_index) return;
m_has_nodes.insert(node_index);
m_rdf_session_p->insert(m_subject, has_index, node_index->get_rdf_index());
};
inline void
add_node(std::string const& name)
{
node_index_type node_index = m_psearch_db_p->get_node_index(name);
if(not node_index) return;
add_node(node_index);
};
inline void
add_node(index_type index)
{
node_index_type node_index = m_psearch_db_p->get_node_index(index);
if(not node_index) return;
add_node(node_index);
};
// inferred
inline void
add_inferred_node(node_index_type node_index)
{
if(!node_index) return;
m_inferred_has_nodes.insert(node_index);
m_rdf_session_p->insert_inferred(m_subject, has_index, node_index->get_rdf_index());
};
inline void
add_inferred_node(std::string const& name)
{
node_index_type node_index = m_psearch_db_p->get_node_index(name);
if(not node_index) return;
add_inferred_node(node_index);
};
inline void
add_inferred_node(index_type index)
{
node_index_type node_index = m_psearch_db_p->get_node_index(index);
if(not node_index) return;
add_inferred_node(node_index);
};
// inferred
inline void
clear_inferred_nodes()
{
node_const_iterator_type itor = get_inferred_nodes_begin();
node_const_iterator_type end = get_inferred_nodes_end();
for(; itor!=end; ++itor) {
m_rdf_session_p->erase(m_subject, has_index, (*itor)->get_rdf_index());
}
m_inferred_has_nodes.clear();
};
// either asserted or inferred
inline bool
has_node(node_index_type node_index)const
{
if(!node_index) return false;
if(m_has_nodes.find(node_index) != m_has_nodes.end()) return true;
if(m_inferred_has_nodes.find(node_index) != m_inferred_has_nodes.end()) return true;
return false;
};
inline bool
has_node(index_type index)const
{
node_index_type node_index = m_psearch_db_p->get_node_index(index);
if(!node_index) return false;
if(m_has_nodes.find(node_index) != m_has_nodes.end()) return true;
if(m_inferred_has_nodes.find(node_index) != m_inferred_has_nodes.end()) return true;
return false;
};
inline bool
is_asserted_has_node(node_index_type node_index)const
{
if(!node_index) return false;
if(m_has_nodes.find(node_index) != m_has_nodes.end()) return true;
return false;
};
inline bool
is_asserted_has_node(index_type index)const
{
node_index_type node_index = m_psearch_db_p->get_node_index(index);
if(!node_index) return false;
if(m_has_nodes.find(node_index) != m_has_nodes.end()) return true;
return false;
};
inline void
remove_node(node_index_type node_index)
{
if(!node_index) return;
if(m_has_nodes.erase(node_index) > 0 or m_inferred_has_nodes.erase(node_index) > 0) {
m_rdf_session_p->erase(m_subject, has_index, node_index->get_rdf_index());
}
if(m_has_not_nodes.erase(node_index) > 0) {
m_rdf_session_p->erase(m_subject, has_not_index, node_index->get_rdf_index());
}
};
inline void
remove_node(std::string const& name)
{
node_index_type node_index = m_psearch_db_p->get_node_index(name);
if(not node_index) return;
remove_node(node_index);
};
inline void
remove_node(index_type index)
{
node_index_type node_index = m_psearch_db_p->get_node_index(index);
if(not node_index) return;
remove_node(node_index);
};
/////////////////////////////////////////////////////////////////////////////
// has not nodes methods
/////////////////////////////////////////////////////////////////////////////
inline node_const_iterator_type
get_negated_nodes_begin()const
{
return m_has_not_nodes.begin();
};
inline node_const_iterator_type
get_negated_nodes_end()const
{
return m_has_not_nodes.end();
};
inline node_set_const_iterator
get_negated_nodes_iterator()const
{
return node_set_const_iterator(get_negated_nodes_begin(), get_negated_nodes_end());
};
inline void
add_negated_nodes(node_set_type const& nodes)
{
for(node_set_type::const_iterator itor=nodes.begin(); itor!=nodes.end(); ++itor) add_negated_node(*itor);
};
inline void
add_negated_node(node_index_type node_index)
{
if(!node_index) return;
m_has_not_nodes.insert(node_index);
m_rdf_session_p->insert(m_subject, has_not_index, node_index->get_rdf_index());
};
inline void
add_negated_node(std::string const& name)
{
node_index_type node_index = m_psearch_db_p->get_node_index(name);
if(not node_index) return;
add_negated_node(node_index);
};
inline void
add_negated_node(index_type index)
{
node_index_type node_index = m_psearch_db_p->get_node_index(index);
if(not node_index) return;
add_negated_node(node_index);
};
inline bool
has_negated_node(node_index_type node_index)const
{
if(!node_index) return false;
return m_has_not_nodes.find(node_index) != m_has_not_nodes.end();
};
inline bool
has_negated_node(index_type index)const
{
node_index_type node_index = m_psearch_db_p->get_node_index(index);
if(not node_index) return false;
return m_has_not_nodes.find(node_index) != m_has_not_nodes.end();
};
private:
friend class PSearchQuery;
friend class PSearchInfer;
rule::knowledge_base const* m_kbase_p;
PSearchDB const* m_psearch_db_p;
rdf::rdf_session_ptr_type m_rdf_session_p;
index_type m_subject;
node_set_type m_has_nodes;
node_set_type m_inferred_has_nodes;
node_set_type m_has_not_nodes;
public:
index_type has_index;
index_type has_not_index;
};
}; /* psearch namespace */
namespace rule {
inline psearch::psession_ptr_type
rule_engine::create_psearch_session(rdf::rdf_session_ptr_type rdf_sn_p)
{
psearch::psession_ptr_type session_p(new psearch::PSearchSession(&*m_kbase_p, rdf_sn_p));
return session_p;
};
}; /* rule namespace */
#endif /*PSEARCH_SESSION_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.