#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 */