Menu

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

Download this file

412 lines (325 with data), 12.2 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
#ifndef OWL_IMPORT_H_
#define OWL_IMPORT_H_
//#include <cstring>
#include "rdf_rule_core.h"
#include "rdf_graph.h"
#include <xercesc/parsers/SAXParser.hpp>
#include <xercesc/sax/HandlerBase.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/sax/AttributeList.hpp>
#include <xercesc/util/OutOfMemoryException.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
#include <boost/algorithm/string/trim.hpp>
namespace parser {
namespace xml {
XERCES_CPP_NAMESPACE_USE
// ---------------------------------------------------------------------------
// This is a simple class that lets us do easy (though not terribly efficient)
// trancoding of XMLCh data to local code page for display.
// ---------------------------------------------------------------------------
class strx
{
public :
strx(XMLCh const* const toTranscode)
{
fLocalForm = XMLString::transcode(toTranscode);
};
~strx()
{
XMLString::release(&fLocalForm);
}
const char* localForm() const
{
return fLocalForm;
}
private :
char* fLocalForm;
};
inline std::ostream& operator<<(std::ostream& target, const strx& toDump)
{
target << toDump.localForm();
return target;
}
typedef std::tr1::shared_ptr<SAXParser> sax_parser_ptr_type;
typedef std::tr1::shared_ptr<MemBufInputSource> mem_bufinput_source_ptr_type;
/////////////////////////////////////////////////////////////////////////////////////////
// class parse_event_state
//
/////////////////////////////////////////////////////////////////////////////////////////
struct parse_event_state
{
typedef rdf::index_type index_t;
parse_event_state(index_t s, index_t p, index_t o, bool from_class, bool is_collection):
subject(s),
predicate(p),
object(o),
from_class_event(from_class),
is_collection_parse_type(is_collection),
data_type(0)
{};
index_t subject;
index_t predicate;
index_t object;
bool from_class_event;
bool is_collection_parse_type;
int data_type;
};
inline std::ostream & operator<<(std::ostream & sout, parse_event_state const& estate)
{
sout << "--> event state: (";
if(estate.subject) {
sout << rdf::internal::to_resource(estate.subject) << " , ";
} else {
sout << " (null) , ";
}
if(estate.predicate) {
sout << rdf::internal::to_resource(estate.predicate) << " , ";
} else {
sout << " (null) , ";
}
if(estate.object) {
sout << rdf::internal::to_resource(estate.object) << " ) ";
} else {
sout << " (null) ) ";
}
if(estate.from_class_event) {
sout << " from class elm";
} else {
sout << " from predicate elm";
}
if(estate.is_collection_parse_type) {
sout << ", is collection ";
}
if(estate.data_type == 1) {
sout << ", the obj is int ";
}
return sout;
};
/////////////////////////////////////////////////////////////////////////////////////////
// class rdf_handler
//
/////////////////////////////////////////////////////////////////////////////////////////
class rdf_handler: public HandlerBase
{
typedef rdf::index_type index_t;
typedef std::vector<parse_event_state> stack_type;
typedef std::map<std::string, std::string> str_map_type;
typedef std::set<std::string> str_set_type;
public:
rdf_handler(rdf::rdf_graph_ptr_type const& graph_p, bool verbose):
HandlerBase(),
m_graph_p(graph_p),
m_stack(),
m_xml_ns_rmap(),
m_known_class_name(),
m_known_predicate_name(),
m_model_xml_base("http://top/test"),
m_xmlns(""),
m_namespace_done(false),
m_rdf_type(NULL),
m_owl_thing(NULL),
m_rdf_description(NULL),
m_top_label(NULL),
m_rdf_id("rdf:ID"), m_rdf_res("rdf:resource"), m_rdf_about("rdf:about"),
m_rdf_datatype("rdf:datatype"),
m_non_neg_num("nonNegativeInteger"),
m_int_num("int"),
m_parse_type("rdf:parseType"),
m_rdf_collection("Collection"),
m_verbose(verbose)
{};
~rdf_handler(){};
inline bool is_verbose()const{return m_verbose;};
// -----------------------------------------------------------------------
// Implementations of the SAX DocumentHandler interface
// -----------------------------------------------------------------------
void endDocument();
void endElement(XMLCh const* const name);
void characters(XMLCh const* const chars, unsigned int const length);
void processingInstruction(XMLCh const* const target, XMLCh const* const data);
void startDocument();
void startElement(XMLCh const* const name, AttributeList & attributes);
// -----------------------------------------------------------------------
// Implementations of the SAX ErrorHandler interface
// -----------------------------------------------------------------------
void warning(SAXParseException const& exc);
void error(SAXParseException const& exc);
void fatalError(SAXParseException const& exc);
// -----------------------------------------------------------------------
// Utility methods
// -----------------------------------------------------------------------
bool
expect_class()
{
if(m_stack.empty()) return true;
parse_event_state & e = m_stack.back();
return !e.from_class_event;
};
std::string
short_uri(std::string const& uri)
{
std::string::size_type pos = 0;
if(uri[0] == '#') {
return m_xmlns + uri.substr(1);
}
if(uri.find("http:") == 0 and (pos=uri.find('#'))!=std::string::npos) {
std::string prefix = uri.substr(0, pos+1);
str_map_type::const_iterator itor = m_xml_ns_rmap.find(prefix);
if(itor != m_xml_ns_rmap.end()) {
return itor->second + uri.substr(pos+1);
}
}
return uri;
};
index_t
get_resource(str_map_type const& map)
{
str_map_type::const_iterator end = map.end();
str_map_type::const_iterator itor;
itor = map.find(m_rdf_id);
if(itor != end) {
std::string top_label = itor->second;
index_t index = m_graph_p->create_resource_as_index(m_xmlns + top_label);
// usually rdf::ID only show up one per resource without the '#'
// so take opportunity to add a label - use top:label rather than rdf:label in case one exist
// in the ontology if the xmlns is not empty (to avoid collision with resource name.)
if(m_xmlns.size() > 0) {
m_graph_p->insert(index, m_top_label, m_graph_p->create_literal_as_index(top_label, top_label));
}
return index;
}
itor = map.find(m_rdf_res);
if(itor != end) return m_graph_p->create_resource_as_index(short_uri(itor->second));
itor = map.find(m_rdf_about);
if(itor != end) {
std::string v = short_uri(itor->second);
boost::trim(v);
if(v == "") v = m_model_xml_base;
return m_graph_p->create_resource_as_index(v);
}
return NULL;
}
index_t
get_subject(str_map_type const& map)
{
index_t i = get_resource(map);
if(!i) return m_graph_p->create_bnode_as_index();
return i;
}
index_t
get_resource(std::string & elm_name)
{
// if(elm_name.find(':')==std::string::npos and elm_name[0]!='#') elm_name = "#"+elm_name;
return m_graph_p->create_resource_as_index(elm_name);
};
int
get_data_type(str_map_type const& map)
{
int type = 0;
str_map_type::const_iterator end = map.end();
str_map_type::const_iterator itor = map.find(m_rdf_datatype);
if(itor != end) {
if(itor->second.find(m_int_num) != std::string::npos) type = 1;
else if(itor->second.find(m_non_neg_num) != std::string::npos) type = 1;
}
return type;
};
bool
has_collection_parse_type(str_map_type const& map)
{
str_map_type::const_iterator end = map.end();
str_map_type::const_iterator itor = map.find(m_parse_type);
if(itor != end) return itor->second == m_rdf_collection;
return false;
};
bool
validate_tag(std::string elm_name, bool for_class_event)
{
// if this is a class event then we don't expect
// to have the elm_name to be a known predicate name
if(for_class_event) {
if(m_known_predicate_name.find(elm_name) != m_known_predicate_name.end()) return false;
} else {
if(m_known_class_name.find(elm_name) != m_known_class_name.end()) return false;
}
return true;
};
private:
rdf::rdf_graph_ptr_type m_graph_p;
stack_type m_stack;
str_map_type m_xml_ns_rmap;
str_set_type m_known_class_name;
str_set_type m_known_predicate_name;
std::string m_model_xml_base;
std::string m_xmlns;
bool m_namespace_done;
index_t m_rdf_type;
index_t m_owl_thing;
index_t m_rdf_description;
index_t m_top_label;
std::string m_rdf_id;
std::string m_rdf_res;
std::string m_rdf_about;
std::string m_rdf_datatype;
std::string m_non_neg_num;
std::string m_int_num;
std::string m_parse_type;
std::string m_rdf_collection;
bool m_verbose;
};
/////////////////////////////////////////////////////////////////////////////////////////
// process_imported_owl_model
//
// Utility function to print info about imported model
/////////////////////////////////////////////////////////////////////////////////////////
void
process_imported_owl_model(rdf::rdf_graph_ptr_type & graph_p, bool verbose);
/////////////////////////////////////////////////////////////////////////////////////////
// import_owl_model
//
// main function to import owl model
/////////////////////////////////////////////////////////////////////////////////////////
rdf::rdf_graph_ptr_type
import_owl_model(std::string const& fname, bool verbose=false);
/////////////////////////////////////////////////////////////////////////////////////////
// import_owl_model
//
// main function to import owl model - importing to the provided graph
/////////////////////////////////////////////////////////////////////////////////////////
rdf::rdf_graph_ptr_type
import_owl_model(std::string const& fname, rdf::rdf_graph_ptr_type & graph_p, bool verbose=false);
/////////////////////////////////////////////////////////////////////////////////////////
// import_owl_model_membuffer
//
// main function to import owl model - importing to the provided graph from memory buffer
// unstead of from file
// Import into new graph
/////////////////////////////////////////////////////////////////////////////////////////
rdf::rdf_graph_ptr_type
import_owl_model_membuffer(std::string const& buffer, bool verbose=false);
/////////////////////////////////////////////////////////////////////////////////////////
// import_owl_model_membuffer
//
// main function to import owl model - importing to the provided graph from memory buffer
// unstead of from file
// Import into existing graph
/////////////////////////////////////////////////////////////////////////////////////////
rdf::rdf_graph_ptr_type
import_owl_model_membuffer(std::string const& buffer, rdf::rdf_graph_ptr_type & graph_p, bool verbose=false);
/////////////////////////////////////////////////////////////////////////////////////////
// process_file
//
// process .trd file
/////////////////////////////////////////////////////////////////////////////////////////
void
process_file(std::string const& fname, rdf::rdf_graph_ptr_type & graph_p, bool verbose=false);
/////////////////////////////////////////////////////////////////////////////////////////
// process_membuffer
//
// parse the mem buffer containing the a trd file.
/////////////////////////////////////////////////////////////////////////////////////////
void
process_membuffer(std::string const& buffer, rdf::rdf_graph_ptr_type & graph_p, bool verbose=false);
}; /* xml namespace */
}; /* parser namespace */
#endif /*OWL_IMPORT_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.