Menu

[r12]: / trunk / libtop_engine / top_model.cpp  Maximize  Restore  History

Download this file

466 lines (339 with data), 15.6 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
465
#include "top_model.h"
#include "top_class.h"
#include "top_property.h"
namespace model {
void
top_model::compute_asserted_class_hierarchy()
{
//*
// std::cout << "...compute_asserted_class_hierarchy: computing class hierarchy\n";
// start from owl:Thing and walk down the hierarchy
bool b;
rdf::rdf_graph_ptr_type graph_p = m_rule_session_p->get_rdf_session().get_asserted_graph();
// clear the asserted hierarchy
m_top_classes = top_class_map_type();
std::vector<top_class_ptr_type> stack;
stack.reserve(graph_p->size()>200 ? 200:graph_p->size());
// load the stack with the root nodes
top_class_ptr_type top_thing_p = top_class_ptr_type(new top_class(*this, m_owl.owl_Thing));
top_class_ptr_type top_nothing_p = top_class_ptr_type(new top_class(*this, m_owl.owl_Nothing));
m_top_classes.insert(std::make_pair(m_owl.owl_Thing, top_thing_p));
m_top_classes.insert(std::make_pair(m_owl.owl_Nothing, top_nothing_p));
stack.push_back(top_thing_p);
stack.push_back(top_nothing_p);
while(!stack.empty()) {
top_class_ptr_type base_p = stack.back();
rdf::index_type base_class = base_p->get_index();
stack.pop_back();
//*
// std::cout << "looking at: " << rdf::internal::to_resource_base(base_p->get_index()) << std::endl;
rdf::rdf_graph::index_iterator itor = graph_p->find_index(rdf::all_subjects(), m_owl.rdfs_subClassOf, base_class);
while(!itor.is_end()) {
rdf::index_triple t3 = itor.get_triple();
rdf::index_type sub_class = t3.get_subject();
//*
// std::cout << "got subclass: " << rdf::internal::to_resource_base(sub_class) << std::endl;
// keep this class as a sub class of base_p if it's not an equivalent class
bool skip_it = false;
if(graph_p->contains(base_class, m_owl.rdfs_subClassOf, sub_class)) skip_it = true;
// create the top_class, even if we skip it
top_class_map_type::iterator pos = m_top_classes.find(sub_class);
if(pos == m_top_classes.end()) {
top_class_ptr_type sub_class_p(new top_class(*this, sub_class));
boost::tie(pos, b) = m_top_classes.insert(std::make_pair(sub_class, sub_class_p));
stack.push_back(sub_class_p);
}
if(!skip_it) {
base_p->add_asserted_sub_class(pos->second);
pos->second->add_asserted_base_class(base_p);
}
itor.next();
}
}
m_is_asserted_hierarchy_computed = true;
//*
// std::cout << "...done\n";
};
void
load_asserted_property_index(top_model const& model, index_type_set_type & base_properties, index_type property_type)
{
rdf_graph::index_iterator itor = model.find_asserted_subjects(model.m_owl.rdf_type, property_type);
while(!itor.is_end()) {
base_properties.insert(itor.get_triple().get_subject());
itor.next();
}
};
void
load_inferred_property_index(top_model const& model, index_type_set_type & base_properties, index_type property_type)
{
rdf_session::index_iterator itor = model.find_subjects(model.m_owl.rdf_type, property_type);
while(!itor.is_end()) {
base_properties.insert(itor.get_triple().get_subject());
itor.next();
}
};
void
top_model::compute_asserted_property_hierarchy()
{
//*
// std::cout << "...compute_asserted_property_hierarchy: computing property hierarchy\n";
// Since there is no obvious root to the property hierarchy, will need to do it manually
bool b;
rdf::rdf_graph_ptr_type graph_p = m_rule_session_p->get_rdf_session().get_asserted_graph();
// clear the asserted hierarchies
m_asserted_base_object_properties = top_property_vector_type();
m_asserted_base_object_properties.reserve(100);
m_asserted_base_data_type_properties = top_property_vector_type();
m_asserted_base_data_type_properties.reserve(100);
// find the base property manually
// a property can be asserted as type: owl:ObjectProperty, owl:DatatypeProperty, owl:InverseFunctionalProperty,
// owl:TransitiveProperty, owl:SymmetricProperty
index_type_set_type base_properties;
load_asserted_property_index(*this, base_properties, m_owl.owl_ObjectProperty);
load_asserted_property_index(*this, base_properties, m_owl.owl_DatatypeProperty);
load_asserted_property_index(*this, base_properties, m_owl.owl_InverseFunctionalProperty);
load_asserted_property_index(*this, base_properties, m_owl.owl_TransitiveProperty);
load_asserted_property_index(*this, base_properties, m_owl.owl_SymmetricProperty);
std::vector<top_property_ptr_type> stack;
stack.reserve(100);
// need to identify the base property (those who don't have rdfs_subPropertyOf and load them into the stack)
index_type_set_type::const_iterator itor = base_properties.begin();
index_type_set_type::const_iterator end = base_properties.end();
for(; itor!=end; ++itor) {
if(!contains_asserted(*itor, m_owl.rdfs_subPropertyOf)) {
top_property_ptr_type property(new top_property(*this, *itor));
stack.push_back(property);
// check if it is an object or data type property
if(contains_asserted(*itor, m_owl.rdf_type, m_owl.owl_DatatypeProperty)) {
m_asserted_base_data_type_properties.push_back(property);
} else {
m_asserted_base_object_properties.push_back(property);
}
}
}
while(!stack.empty()) {
top_property_ptr_type base_p = stack.back();
rdf::index_type base_property = base_p->get_index();
stack.pop_back();
//*
// std::cout << "looking at: " << rdf::internal::to_resource_base(base_p->get_index()) << std::endl;
rdf::rdf_graph::index_iterator itor = graph_p->find_index(rdf::all_subjects(), m_owl.rdfs_subPropertyOf, base_property);
while(!itor.is_end()) {
rdf::index_triple t3 = itor.get_triple();
rdf::index_type sub_class = t3.get_subject();
//*
// std::cout << "got sub property: " << rdf::internal::to_resource_base(sub_class) << std::endl;
// keep this property as a sub property if it is not an equivalent property
bool skip_it = false;
if(graph_p->contains(base_property, m_owl.rdfs_subPropertyOf, sub_class)) skip_it = true;
// keep this property as a sub property of base_p
top_property_map_type::iterator pos = m_top_properties.find(sub_class);
if(pos == m_top_properties.end()) {
top_property_ptr_type sub_class_p(new top_property(*this, sub_class));
boost::tie(pos, b) = m_top_properties.insert(std::make_pair(sub_class, sub_class_p));
stack.push_back(sub_class_p);
}
if(!skip_it) {
base_p->add_asserted_sub_property(pos->second);
pos->second->add_asserted_base_property(base_p);
}
itor.next();
}
}
//*
// std::cout << "...done\n";
};
void
top_model::compute_inferred_class_hierarchy()
{
//*
std::cout << "...compute_inferred_class_hierarchy\n";
// start from owl:Thing and walk down the hierarchy
bool b;
rdf::rdf_session const& rdfsession = m_rule_session_p->get_rdf_session();
std::vector<top_class_ptr_type> stack;
stack.reserve(rdfsession.size()>200 ? 200:rdfsession.size());
// load the stack with the root nodes
top_class_map_type::iterator pos = m_top_classes.find(m_owl.owl_Thing);
if(pos == m_top_classes.end()) {
//*
std::cout << "...humm, can't find owl:Thing?!\n";
top_class_ptr_type top_thing_p = top_class_ptr_type(new top_class(*this, m_owl.owl_Thing));
boost::tie(pos, b) = m_top_classes.insert(std::make_pair(m_owl.owl_Thing, top_thing_p));
}
stack.push_back(pos->second);
pos = m_top_classes.find(m_owl.owl_Nothing);
if(pos == m_top_classes.end()) {
//*
std::cout << "...humm, can't find owl:Nothing?!\n";
top_class_ptr_type top_nothing_p = top_class_ptr_type(new top_class(*this, m_owl.owl_Nothing));
boost::tie(pos, b) = m_top_classes.insert(std::make_pair(m_owl.owl_Nothing, top_nothing_p));
}
stack.push_back(pos->second);
index_type_set_type visited_index;
while(!stack.empty()) {
top_class_ptr_type base_p = stack.back();
rdf::index_type base_class = base_p->get_index();
stack.pop_back();
//*
std::cout << "looking at: " << rdf::internal::to_resource_base(base_class) << std::endl;
rdf::rdf_session::index_iterator itor = rdfsession.find_index(rdf::all_subjects(), m_owl.rdfs_subClassOf, base_class);
while(!itor.is_end()) {
rdf::index_triple t3 = itor.get_triple();
rdf::index_type sub_class = t3.get_subject();
//*
std::cout << "got subclass: " << rdf::internal::to_resource_base(sub_class);
// check if sub class is an equivalent class, then skip it
bool skip_it = false;
if(rdfsession.contains(base_class, m_owl.rdfs_subClassOf, sub_class)) skip_it = true;
if(!skip_it) {
// check if the sub_class is a sub class of any sibling
// note that the sibling must be of type owl:Class (a sibling of type owl:Restriction won't do.)
rdf::rdf_session::index_iterator jtor = rdfsession.find_index(rdf::all_subjects(), m_owl.rdfs_subClassOf, base_class);
while(!jtor.is_end()) {
rdf::index_type other_sub_class = jtor.get_triple().get_subject();
if(rdfsession.contains(sub_class, m_owl.rdfs_subClassOf, other_sub_class)
and rdfsession.contains(other_sub_class, m_owl.rdf_type, m_owl.owl_Class)
and not rdfsession.contains(other_sub_class, m_owl.rdfs_subClassOf, sub_class)
and not rdfsession.contains(base_class, m_owl.rdfs_subClassOf, other_sub_class)
) {
//*
std::cout << ". . .skipping it because it's a sub class of " << rdf::internal::to_resource_base(other_sub_class) << std::endl;
skip_it = true;
break;
}
jtor.next();
}
if(!skip_it) {
//*
std::cout << ". . .ok, keep it " << std::endl;
// keep this class as a sub class of base_p
top_class_map_type::iterator pos = m_top_classes.find(sub_class);
if(pos == m_top_classes.end()) {
top_class_ptr_type sub_class_p(new top_class(*this, sub_class));
boost::tie(pos, b) = m_top_classes.insert(std::make_pair(sub_class, sub_class_p));
}
if(visited_index.find(sub_class) == visited_index.end()) {
visited_index.insert(sub_class);
stack.push_back(pos->second);
}
base_p->add_inferred_sub_class(pos->second);
pos->second->add_inferred_base_class(base_p);
}
}
itor.next();
}
}
//*
std::cout << "...done\n";
};
void
top_model::compute_inferred_property_hierarchy()
{
//*
// std::cout << "...compute_inferred_property_hierarchy: computing property hierarchy\n";
// Since there is no obvious root to the property hierarchy, will need to do it manually
bool b;
rdf::rdf_session const& rdfsession = m_rule_session_p->get_rdf_session();
// clear the asserted hierarchies
m_inferred_base_object_properties = top_property_vector_type();
m_inferred_base_object_properties.reserve(100);
m_inferred_base_data_type_properties = top_property_vector_type();
m_inferred_base_data_type_properties.reserve(100);
// find the base property manually
// a property can be inferred as type: owl:ObjectProperty or owl:DatatypeProperty
index_type_set_type base_properties;
load_inferred_property_index(*this, base_properties, m_owl.owl_ObjectProperty);
load_inferred_property_index(*this, base_properties, m_owl.owl_DatatypeProperty);
std::vector<top_property_ptr_type> stack;
stack.reserve(100);
// need to identify the base property (those who don't have rdfs_subPropertyOf and load them into the stack)
index_type_set_type::const_iterator itor = base_properties.begin();
index_type_set_type::const_iterator end = base_properties.end();
for(; itor!=end; ++itor) {
if(!contains(*itor, m_owl.rdfs_subPropertyOf)) {
top_property_ptr_type property(new top_property(*this, *itor));
stack.push_back(property);
// check if it is an object or data type property
if(contains(*itor, m_owl.rdf_type, m_owl.owl_DatatypeProperty)) {
m_inferred_base_data_type_properties.push_back(property);
} else {
m_inferred_base_object_properties.push_back(property);
}
}
}
index_type_set_type visited_index;
while(!stack.empty()) {
top_property_ptr_type base_p = stack.back();
rdf::index_type base_property = base_p->get_index();
stack.pop_back();
//*
// std::cout << "looking at: " << rdf::internal::to_resource_base(base_property) << std::endl;
rdf::rdf_session::index_iterator itor = rdfsession.find_index(rdf::all_subjects(), m_owl.rdfs_subPropertyOf, base_property);
while(!itor.is_end()) {
rdf::index_triple t3 = itor.get_triple();
rdf::index_type sub_property = t3.get_subject();
//*
// std::cout << "got subproperty: " << rdf::internal::to_resource_base(sub_property);
// keep this property as a sub property if it is not an equivalent property
bool skip_it = false;
if(rdfsession.contains(base_property, m_owl.rdfs_subPropertyOf, sub_property)) skip_it = true;
if(!skip_it) {
// check if t3.get_subject() is sub class of any sibling
rdf::rdf_session::index_iterator jtor = rdfsession.find_index(rdf::all_subjects(), m_owl.rdfs_subPropertyOf, base_property);
while(!jtor.is_end()) {
rdf::index_type other_sub_property = jtor.get_triple().get_subject();
if(rdfsession.contains(sub_property, m_owl.rdfs_subPropertyOf, other_sub_property)
and not rdfsession.contains(other_sub_property, m_owl.rdfs_subPropertyOf, sub_property)
and not rdfsession.contains(base_property, m_owl.rdfs_subPropertyOf, other_sub_property)
) {
//*
// std::cout << ". . .skipping it " << std::endl;
skip_it = true;
break;
}
jtor.next();
}
if(!skip_it) {
//*
// std::cout << ". . .ok, keep it " << std::endl;
// keep this property as a sub property of base_p
top_property_map_type::iterator pos = m_top_properties.find(sub_property);
if(pos == m_top_properties.end()) {
top_property_ptr_type sub_property_p(new top_property(*this, sub_property));
boost::tie(pos, b) = m_top_properties.insert(std::make_pair(sub_property, sub_property_p));
}
if(visited_index.find(sub_property) == visited_index.end()) {
visited_index.insert(sub_property);
stack.push_back(pos->second);
}
base_p->add_inferred_sub_property(pos->second);
pos->second->add_inferred_base_property(base_p);
}
}
itor.next();
}
}
//*
// std::cout << "...done\n";
};
///////////////////////////////////////////////////////////////////////////////////////////
// methods to access individuals and their properties
//
// rdf_type property of individual
///////////////////////////////////////////////////////////////////////////////////////////
top_class_lookup_iterator_by_asserted_object
top_model::get_asserted_types(index_type subject)const
{
return top_class_lookup_iterator_by_asserted_object(
find_asserted_objects(subject, m_owl.rdf_type),
top_class_lookup_by_object(*this));
};
top_class_lookup_iterator_by_object
top_model::get_inferred_types(index_type subject)const
{
return top_class_lookup_iterator_by_object(
find_objects(subject, m_owl.rdf_type),
top_class_lookup_by_object(*this));
};
}; /* model namespace */
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.