1
import Visconte.SemWeb;
4
* <p>Class Ontology</p>
5
* contoller class for the translation betweenn RDF/OWl and OWLItems.
6
* Manages the input/output with a storage handler. Further Responsible for the
7
* logging of an ontology
11
* <p>Represents the xml schema namespace</p>
13
public constant xml_schema_base="http://www.w3.org/2001/XMLSchema";
16
* <p>Represents the xml schema namespace</p>
18
public constant THING="http://www.w3.org/2002/07/owl#Thing";
21
* <p>Represents the xml schema namespace</p>
23
private string template;
25
* <p>Represents the storage handler to load/save this ontology</p>
27
private object storage_handler;
30
* <p>Represents the ontology version</p>
32
private int version=0;
35
* <p>Represents the ontology filename</p>
37
private string ontology_file;
40
* <p>Represents the ontology filename</p>
42
private string ontology_log_file_suffix=".log";
45
* <p>Represents the ontology filename</p>
47
public Parser.XML.Tree.Node ontology_log;
50
* <p>Represents the user who init this action, default anonym</p>
52
protected string user = "annonym";
55
* <p>The ontology as rdfset representation</p>
60
* <p>The backup ot the current ontology</p>
62
private string backup;
65
* <p>The ObjectProperty Statement</p>
67
private OWL.OWLResource owl_ObjectProperty;
70
* <p>The DataTypeProperty Statement</p>
72
private OWL.OWLResource owl_DatatypeProperty;
75
* <p>The DataTypeProperty Statement</p>
77
private OWL.OWLResource owl_Ontology;
80
* <p>The DataTypeProperty Statement</p>
82
private OWL.OWLResource owl_versionInfo;
85
* <p>The TransitiveProperty</p>
87
private OWL.OWLResource owl_TransitiveProperty;
90
* <p>The SymetricProperty</p>
92
private OWL.OWLResource owl_SymetricProperty;
95
* <p>Meta object for author</p>
97
private RDF.URIResource anot_creator;
100
* <p>Meta object for author</p>
102
private RDF.URIResource anot_changes;
105
* <p>Meta object for date of change</p>
107
private RDF.URIResource anot_created;
110
* <p>is this a documenterm</p>
112
private RDF.URIResource steam_is_document_term;
115
* <p>the object id</p>
117
private RDF.URIResource steam_objectid;
120
* <p>the object id</p>
122
private RDF.URIResource steam_mimetype;
126
* <p>The rdfs_label statement</p>
128
private RDFS.RDFSResource rdfs_label;
131
* <p>The Visconte.Onto.Item.OWLItem Factory to produce items</p>
133
private Visconte.Onto.OWLItemFactory factory = Visconte.Onto.OWLItemFactory();
138
* <p>Constructor: creates a new Ontology representation. If the ontology file not exists,
139
* a new ontology will be started</p>
142
* @param file - the ontology file
143
* @param version - the version of the last ontology edition
144
* @param storage_handler - the interface of the storage handler to load and save the ontology
145
* @param user - the user which operates on the ontology
147
public void create(string ontology_file, int version, object storage_handler, string|void user)
151
this_program::ontology_file=ontology_file;
152
this_program::version=version;
153
this_program::storage_handler = storage_handler;
155
this_program::user=user;
161
* <p>Inits common OWLResources and loads the ontology.</p>
168
//set commomn OWL Ressources
169
owl_ObjectProperty = owlset->OWLResource("ObjectProperty");
170
owl_DatatypeProperty = owlset->OWLResource("DatatypeProperty");
171
owl_TransitiveProperty = owlset->OWLResource("TransitiveProperty");
174
owl_Ontology = owlset->OWLResource("Ontology");
175
owl_versionInfo = owlset->OWLResource("versionInfo");
176
anot_creator = owlset->URIResource("http://steam.upb.de/annot#creator");
177
anot_created = owlset->URIResource("http://steam.upb.de/annot#created");
178
anot_changes = owlset->URIResource("http://steam.upb.de/annot#changes");
180
steam_is_document_term = owlset->URIResource("http://steam.upb.de/steam#isDocumentTerm");
181
steam_objectid = owlset->URIResource("http://steam.upb.de/steam#objectID");
182
steam_mimetype = owlset->URIResource("http://steam.upb.de/steam#mimetype");
183
//set rdfs:label statement
184
rdfs_label = owlset->RDFSResource("label");
185
//load the ontology-file;
191
* <p>Get an Item with a special id</p>
192
* This method will be check the type of the id and delegate it to the special functions
193
* e.g. get_class etc. and finally returns a Visconte.Onto.Item.OWLItem
195
* @param id - the id of the item as string
196
* @return Visconte.Onto.Item.OWLItem
198
public Visconte.Onto.Item.OWLItem get_Item(string id)
201
Visconte.Onto.Item.OWLItem item;
202
if (!item_exists(id))
205
throw( ({ "", backtrace(),11, id}) );
209
int type = get_type(id);
212
case Visconte.Onto.Item.OWLItem.CLASS:
213
item = get_Class(id);
216
case Visconte.Onto.Item.OWLItem.OBJECT_PROPERTY:
217
item = get_Property(id);
220
case Visconte.Onto.Item.OWLItem.DATATYPE_PROPERTY:
221
item = get_Property(id);
224
case Visconte.Onto.Item.OWLItem.INDIVIDUAL:
225
item = get_Indiviudal(id);
229
throw( ({ "", backtrace(),601, id}) );
237
* <p>Adds an Visconte.Onto.Item.OWLItem to the ontology</p>
238
* By checking the type of the item, this method delegates to specific methods lilke add_Class() etc.
241
* @return int success
243
public int add_Item(Visconte.Onto.Item.OWLItem item)
246
//is this a valid item
249
throw( ({ "", backtrace(),11, 0}) );
253
if(item_exists(item))
255
throw( ({ "", backtrace(),602,item->get_Id()}) );
259
switch(item->ITEMTYPE)
262
case Visconte.Onto.Item.OWLItem.CLASS:
263
return add_Class(item);
266
case Visconte.Onto.Item.OWLItem.OBJECT_PROPERTY:
267
return add_ObjectProperty(item);
270
case Visconte.Onto.Item.OWLItem.DATATYPE_PROPERTY:
271
return add_DatatypeProperty(item);
274
case Visconte.Onto.Item.OWLItem.INDIVIDUAL:
275
return add_Individual(item);
279
throw( ({ "", backtrace(),601,item->get_Id()}) );
291
* <p>Adds a class to the ontology</p>
292
* @param item - the OWLClass to add *
295
private int add_Class(Visconte.Onto.Item.OWLItem item)
297
add_statements(create_Class_statement(item));
304
* <p>Get a class with a specific id</p>
305
* Constructs a class of the given id and returns an Visconte.Onto.Item.OWLClass object
307
* @param id - the id as string
308
* @return Visconte.Onto.Item.OWLClass class
311
public Visconte.Onto.Item.OWLClass get_Class(string id)
318
RDF.Resource item = owlset->get_resource(id);
320
//get the parent class (if one)
321
parent_id = resource_to_id(query(item,owlset->rdfs_subClassOf,0),2);
323
//get the properties related to this class (only the one in the domain)
324
array properties = resources_to_ids(query(0,owlset->rdfs_domain,id), 0);
326
//get the properties related to this class (only the one in the range)
327
array range_properties = resources_to_ids(query(0,owlset->rdfs_range,id), 0);
329
//get the descendants ot this class
330
array childs = resources_to_ids(query(0,owlset->rdfs_subClassOf,item), 0);
332
//get the diffenrent form classes
333
array different_from = resources_to_ids(query(item,owlset->owl_differentFrom,0),2);
335
//get the diffenrent form classes
336
array equivalent_classes = resources_to_ids(query(item,owlset->owl_equivalentClass,0),2);
338
array individuals = resources_to_ids(query(0,owlset->rdf_type,item),0);
340
//construct the class
341
Visconte.Onto.Item.OWLClass owl_class = Visconte.Onto.Item.OWLClass(this_object(),id,parent_id,get_label(id),properties, range_properties, childs, individuals,equivalent_classes,different_from);
346
throw( ({ "", backtrace(),11, id}) );
354
* <p>Transforms a Visconte.Onto.Item.OWLClass to a Container of RDF-Statements</p>
356
* @param item - the class to translate into statement
357
* @return Visconte.Onto.StatementContainer - the statements
359
private Visconte.Onto.StatementContainer create_Class_statement(Visconte.Onto.Item.OWLItem item)
361
Visconte.Onto.StatementContainer stmt_container = Visconte.Onto.StatementContainer();
363
OWL.OWLResource class_resource = owlset->make_resource(item->get_Id());
365
stmt_container->add(class_resource,owlset->rdf_type, owlset->owl_Class);
367
stmt_container->add(class_resource,rdfs_label,owlset->LiteralResource(item->get_Label()));
369
//add equivalent classes
370
Visconte.Onto.OWLContainer c = item->get_equivalent_classes();
371
Visconte.Onto.Item.OWLItem eq_class, diff_class;
372
while(c->has_items())
375
stmt_container->add(class_resource,owlset->owl_equivalentClass,owlset->get_resource(eq_class->get_Id()));
377
//add different classes
378
Visconte.Onto.Item.OWLItem dif_class;
379
c=item->get_different_from_classes();
380
while(c->has_items())
382
diff_class=c->next();
383
stmt_container->add(class_resource,owlset->owl_differentFrom,owlset->get_resource(diff_class->get_Id()));
387
//set as subclass if parent exists
388
if(item->has_parent() && item_exists(item->get_parent()))
390
stmt_container->add(class_resource,owlset->rdfs_subClassOf,owlset->get_resource( item->get_parent()->get_Id() ) );
395
stmt_container->add(class_resource,owlset->rdfs_subClassOf,owlset->get_resource(THING));
397
return stmt_container;
402
// Individual related
405
* <p>Adds an Individual to the ontology</p>
407
* @param item - OWLIndividual to add
408
* @return int success
410
private int add_Individual(Visconte.Onto.Item.OWLItem item)
413
add_statements(create_Individual_statement(item));
419
* <p>Get a individual with a specific id</p>
420
* Constructs a OWLIndividual of the given id and returns an Visconte.Onto.Item.OWLIndividual object
422
* @param id - the id as string
423
* @return Visconte.Onto.Item.OWLIndividual individual
426
public Visconte.Onto.Item.OWLIndividual get_Indiviudal(string id)
431
RDF.Resource individual = owlset->get_resource(id);
433
string class_id = resource_to_id(query(individual,owlset->rdf_type,0),2);
434
Visconte.MVList property_values =Visconte.MVList();
435
Visconte.Onto.OWLContainer all_properties = Visconte.Onto.OWLContainer();
436
Visconte.Onto.Item.OWLClass model_class = get_Item(class_id);
439
Visconte.Onto.OWLContainer property_container = model_class->get_domain_properties();
441
while(property_container->has_items())
443
Visconte.Onto.Item.OWLItem item = property_container->next();
444
//add this property to all inherited properties
445
all_properties->add(item);
447
RDF.Resource property = owlset->get_resource(item->get_Id());
449
if(item->ITEMTYPE==Visconte.Onto.Item.OWLItem.OBJECT_PROPERTY)
451
resources = resources_to_ids(query(individual,property,0),2);
455
resources = resources_to_literals(query(individual,property,0),2);
457
if(sizeof(resources)>0)
458
property_values->add(item->get_Id() ,resources );
461
model_class=model_class->get_parent();
463
while(model_class!=0);
465
// write("RETR:"+id+"\n");
466
return Visconte.Onto.Item.OWLIndividual( this_object(), id,class_id, get_label(id), all_properties, property_values);
470
throw( ({ "", backtrace(),11, id}) );
476
* <p>Transforms a Visconte.Onto.Item.OWLIndividual to a Container of RDF-Statements</p>
478
* @param item - the class to translate into statement
479
* @return Visconte.Onto.StatementContainer - the statements
481
private Visconte.Onto.StatementContainer create_Individual_statement(Visconte.Onto.Item.OWLItem item)
484
Visconte.Onto.StatementContainer stmt_container = Visconte.Onto.StatementContainer();
486
Visconte.Onto.Item.OWLClass model_class = item->get_model_class();
489
OWL.OWLResource individual_resource = owlset->make_resource(item->get_Id());
491
OWL.OWLResource model_resource;
492
//test if the model class exists
493
if(item_exists(model_class))
494
model_resource = owlset->get_resource(model_class->get_Id());
496
model_resource = owlset->make_resource(THING);
499
stmt_container->add(individual_resource,owlset->rdf_type,model_resource);
501
stmt_container->add(individual_resource,rdfs_label,owlset->LiteralResource(item->get_Label()));
503
//add the properties by testing the defined properties in the modelclass and it parents
506
if(item_exists(model_class))
508
array(array(mixed)) property_map = item->get_property_map();
509
Visconte.Onto.OWLContainer control_properties = model_class->get_domain_properties();
511
while(control_properties->has_items())
514
Visconte.Onto.Item.OWLItem control_property = control_properties->next();
515
string control_id=control_property->get_Id();
516
foreach(property_map, array prop_value)
518
string property_id=prop_value[0]->get_Id();
519
if(property_id==control_id)
521
OWL.OWLResource value_resource;
522
if(control_property->ITEMTYPE==Visconte.Onto.Item.OWLItem.OBJECT_PROPERTY)
523
value_resource=owlset->get_resource(prop_value[1]);
525
value_resource=owlset->LiteralResource(prop_value[1]);
527
stmt_container->add(individual_resource,owlset->get_resource(control_id),value_resource);
533
model_class=model_class->get_parent();
535
while(model_class!=0);
537
return stmt_container;
547
* <p>Adds a Object Property to the ontology</p>
550
* @param item - the OWLObjectProperty to add
551
* @return int success
553
private int add_ObjectProperty(Visconte.Onto.Item.OWLItem item)
556
add_statements(create_ObjectProperty_statement(item));
564
* <p>Adds a Datatype Property to the ontology</p>
566
* @param item - the OWLDatatypeProperty to add
567
* @return int success
569
private int add_DatatypeProperty(Visconte.Onto.Item.OWLItem item)
571
add_statements(create_DatatypeProperty_statement(item));
578
* <p>Get a property with a specific id</p>
580
* @param id - the id as string
581
* @return OWLProperty property
584
public Visconte.Onto.Item.OWLProperty get_Property(string id)
586
Visconte.Onto.Item.OWLProperty property;
587
string domain, range, range_namespace;
591
RDF.Resource item = owlset->get_resource(id);
592
//retrieve if its a Datatype or an Object Property by querieing the range type
594
range=resource_to_id(query(item,owlset->rdfs_range,0),2);
595
domain=resource_to_id(query(item,owlset->rdfs_domain,0),2);
598
if(has_prefix(range,"xsd:"))
600
//construct the property and construct range as xsd:type statement
601
property = Visconte.Onto.Item.OWLDatatypeProperty(this_object(),id,get_Property_restriction(id),get_label(id),get_Class(domain),range);
605
property = Visconte.Onto.Item.OWLObjectProperty(this_object(),id,get_Property_restriction(id),get_label(id),get_Class(domain),get_Class(range));
613
throw( ({ "", backtrace(),11, id}) );
619
* <p>Get property restrictions of a specific id</p>
620
* NOTE: This method has currently no effect
621
* @param id - the id as string
622
* @return mapping with restrictions
625
private mapping get_Property_restriction(string id)
627
// array restriction;
628
return (["transitive" : 1, "symetric" : 1]);
633
* <p>Construct the resources container for the Object Property</p>
636
* @param item - the property (OWLObjectProperty) to translate into statement
637
* @return Visconte.Onto.StatementContainer - the statements
639
private Visconte.Onto.StatementContainer create_ObjectProperty_statement(Visconte.Onto.Item.OWLItem item)
641
Visconte.Onto.StatementContainer stmt_container = Visconte.Onto.StatementContainer();
644
OWL.OWLResource property_resource = owlset->make_resource(item->get_Id());
645
OWL.OWLResource domain;
646
OWL.OWLResource range;
648
if(item_exists(item->get_domain()))
650
domain = owlset->get_resource(item->get_domain()->get_Id());
654
domain = owlset->make_resource(THING);
657
if(item_exists(item->get_range()))
659
range = owlset->get_resource(item->get_range()->get_Id());
663
range = owlset->make_resource(THING);
667
stmt_container->add(property_resource,owlset->rdf_type,owl_ObjectProperty);
668
stmt_container->add(property_resource,owlset->rdfs_domain,domain);
669
stmt_container->add(property_resource,owlset->rdfs_range,range);
670
stmt_container->add(property_resource,rdfs_label,owlset->LiteralResource(item->get_Label()));
672
return stmt_container;
677
* <p>Adds a Datatype Property to the ontology</p>
679
* @param item - the property (OWLDatatypeProperty) to add
682
private Visconte.Onto.StatementContainer create_DatatypeProperty_statement(Visconte.Onto.Item.OWLItem item)
684
Visconte.Onto.StatementContainer stmt_container = Visconte.Onto.StatementContainer();
686
OWL.OWLResource property_resource = owlset->make_resource(item->get_Id());
687
OWL.OWLResource domain;
688
OWL.OWLResource range;
690
if(item_exists(item->get_domain()))
692
domain = owlset->get_resource(item->get_domain()->get_Id());
696
domain = owlset->make_resource(THING);
699
//the range of the property
700
range = owlset->make_resource(xml_schema_base+"#"+item->get_range());
702
stmt_container->add(property_resource,owlset->rdf_type,owl_DatatypeProperty);
703
stmt_container->add(property_resource,owlset->rdfs_domain,domain);
704
stmt_container->add(property_resource,owlset->rdfs_range,range);
705
stmt_container->add(property_resource,rdfs_label,owlset->LiteralResource(item->get_Label()));
707
return stmt_container;
713
// Statement handling
716
* <p>add the statement to the ontology</p>
718
* @param Visconte.Onto.StatementContainer container - the statements to add
721
private int add_statements(Visconte.Onto.StatementContainer container)
724
while(container->has_statements())
726
stmt = container->next();
727
owlset->add_statement(stmt["subj"],stmt["pred"],stmt["obj"]);
734
* <p>removes statements from the ontology</p>
736
* @param Visconte.Onto.StatementContainer container - the statements to remove
739
private int remove_statements(Visconte.Onto.StatementContainer container)
742
while(container->has_statements())
744
stmt = container->next();
745
// workaround for literals
746
if(stmt["obj"]->is_literal_resource)
748
array lit = query(stmt["subj"],stmt["pred"],0);
749
if(!query_is_empty(lit))
750
stmt["obj"]=lit[0][2];
753
owlset->remove_statement(stmt["subj"],stmt["pred"],stmt["obj"]);
757
* <p>upadtes statements in the ontology</p>
760
* @param Visconte.Onto.StatementContainer container - the statements to update
763
private int update_statements(Visconte.Onto.StatementContainer old, Visconte.Onto.StatementContainer new)
765
remove_statements(old);
772
* <p>returns a label as mapping in the form "lang":"label".</p>
773
* Currently this is only en for english
774
* @param id - the id of the OWLItem
775
* @return label - the mapping with the label(s)
778
public mapping get_label(string id)
782
array label_query = query(id,rdfs_label,0);
783
if(!query_is_empty(label_query))
785
label=label_query[0][2]->get_literal();
787
return ([lang : label]);
792
* <p>Checks if a query array is empty</p>
795
* @param query - the query array
796
* @return int success
798
public int query_is_empty(array query)
807
* <p>Transforms resources to literals and returns them as array</p>
810
* @param query - the query array
811
* @param statement_position - 0=Subject, 1=Predicate, 2=Object
812
* @return literals - array with literals
814
public array resources_to_literals(array query,int statement_position)
818
array literals=({ });
819
if(!query_is_empty(query))
821
foreach(query,array result)
823
if(result[statement_position]->is_literal_resource)
825
literal=result[statement_position]->get_literal();
826
literals+=({literal});
834
* <p>Transforms a single resources to an id and returns it</p>
836
* @param query - the query array
837
* @param statement_position - 0=Subject, 1=Predicate, 2=Object
838
* @return id - the id as string
840
public string resource_to_id(array query,int statement_position)
843
if(!query_is_empty(query))
845
//debug(sprintf("%O",query));
846
if(query[0][statement_position]->is_literal_resource)
848
id=namespace_conversion(query[0][statement_position]->get_literal());
850
id=Visconte.ID(namespace_conversion(query[0][statement_position]->get_qname()))->ref;
858
* <p>Transforms resources to ids and returns them as array</p>
860
* @param query - the query array
861
* @param statement_position - 0=Subject, 1=Predicate, 2=Object
862
* @return id - array with ids
864
public array resources_to_ids(array query,int statement_position)
869
if(!query_is_empty(query))
871
foreach(query,array result)
873
if(!result[2]->is_literal_resource)
875
id=Visconte.ID(namespace_conversion(result[statement_position]->get_qname()))->ref;
884
* <p>Checks an id for trailing basenames an returns them a modfied id</p>
887
* @param id - the id to check
890
public string namespace_conversion(string id)
892
int basename_separator_pos = search(id,":");
893
if(basename_separator_pos>=0)
895
string base = id[0..basename_separator_pos];
896
string identifier = id[basename_separator_pos+1..];
916
return "#"+identifier;
925
* <p>Check for the existence of an item in the Ontology</p>
928
* @param item (string or Visconte.Onto.Item.OWLItem)
929
* @return int success
931
public int item_exists(string|Visconte.Onto.Item.OWLItem item)
941
id=Visconte.ID(item)->ref;
943
RDF.Resource test = owlset->get_resource(id);
953
* <p>Replace an old item with a new one</p>
956
* @param old_item - the item to replace
957
* @param new_item - the exchange item
959
public void update(Visconte.Onto.Item.OWLItem updated_item)
961
//the actual item on the storage
962
Visconte.Onto.Item.OWLItem current_item;
964
switch(updated_item->ITEMTYPE)
967
case Visconte.Onto.Item.OWLItem.CLASS:
968
current_item = get_Class(updated_item->get_Id());
969
update_statements(create_Class_statement(current_item),create_Class_statement(updated_item));
972
case Visconte.Onto.Item.OWLItem.INDIVIDUAL:
973
current_item = get_Indiviudal(updated_item->get_Id());
974
update_statements(create_Individual_statement(current_item),create_Individual_statement(updated_item));
977
case Visconte.Onto.Item.OWLItem.OBJECT_PROPERTY:
978
current_item = get_Property(updated_item->get_Id());
979
update_statements(create_ObjectProperty_statement(current_item),create_ObjectProperty_statement(updated_item));
982
case Visconte.Onto.Item.OWLItem.DATATYPE_PROPERTY:
983
current_item = get_Property(updated_item->get_Id());
984
update_statements(create_DatatypeProperty_statement(current_item),create_DatatypeProperty_statement(updated_item));
991
* <p>Removes an item from the ontology</p>
994
* @param item - the OWLItem to remove
995
* @return int success
997
public int remove(Visconte.Onto.Item.OWLItem owlitem)
999
Visconte.Onto.StatementContainer cont = Visconte.Onto.StatementContainer();
1000
remove_meta(owlitem);
1002
switch(owlitem->ITEMTYPE)
1005
case Visconte.Onto.Item.OWLItem.CLASS:
1007
if(owlitem->is_root())
1009
remove_statements(create_Class_statement(owlitem));
1012
case Visconte.Onto.Item.OWLItem.OBJECT_PROPERTY:
1013
remove_statements(create_ObjectProperty_statement(owlitem));
1016
case Visconte.Onto.Item.OWLItem.DATATYPE_PROPERTY:
1017
remove_statements(create_DatatypeProperty_statement(owlitem));
1020
case Visconte.Onto.Item.OWLItem.INDIVIDUAL:
1021
remove_statements(create_Individual_statement(owlitem));
1028
* <p>Tries to get the type of an item.</p>
1029
* This method tries to retrieve the type of an item as id, by checking the rdf:type statement
1030
* It will return the constants defined in Visconte.Onto.Item.OWLItem (see Visconte.Onto.Item.OWLItem.CLASS etc.)
1033
* @return int the constant of the type
1036
public int get_type(string id)
1041
RDF.Resource item = owlset->get_resource(id);
1043
//find the statement, to get the object type
1044
array statement = owlset->find_statements(item,owlset->rdf_type,0);
1045
if (sizeof(statement)>0)
1048
string typename = statement[0][2]->get_qname();
1049
if(typename=="Class" || typename=="owl:Class")
1050
type=Visconte.Onto.Item.OWLItem.CLASS;
1052
else if(typename=="ObjectProperty" || typename=="owl:ObjectProperty")
1053
type=Visconte.Onto.Item.OWLItem.OBJECT_PROPERTY;
1055
else if(typename=="DatatypeProperty" || typename=="owl:DatatypeProperty")
1056
type=Visconte.Onto.Item.OWLItem.DATATYPE_PROPERTY;
1059
type=Visconte.Onto.Item.OWLItem.INDIVIDUAL;
1068
* <p>Generates an OWLContainer from an array of ids</p>
1070
* @param ids - an array with ids
1071
* @return OWLContainer - the container with the items
1073
public Visconte.Onto.OWLContainer get_Items(array ids)
1075
Visconte.Onto.OWLContainer container = Visconte.Onto.OWLContainer();
1077
foreach(ids ,string id)
1079
container->add(get_Item(Visconte.ID(id)->ref));
1085
* <p>Generates an OWLContainer of a defined RDF-object</p>
1086
* Returns an Visconte.Onto.OWLContainer by searching in the ontology for all objects
1087
* that fit to the requested one. Param position indicates what should be constructed
1088
* for the query (0:Subject,1:Predicate,2:Object)
1089
* @param _object - the objects name as string
1090
* @param position - the position of the statment
1091
* @param type - the desired type of OWLItems to return (0:all, 1-4 see OWLItem.ITEMTYPE-Constant)
1092
* @return OWLContainer - the container with the items
1094
public Visconte.Onto.OWLContainer get_items_by_object(string _object, int position, int type)
1096
Visconte.Onto.OWLContainer object_cnt = Visconte.Onto.OWLContainer();
1097
array ids = resources_to_ids(query(0,0,_object),position);
1098
foreach(ids,string id)
1100
if(type==0 || get_type(id)==type)
1101
object_cnt->add(get_Item(id));
1108
* <p>Generates an OWLContainer from an array of ids</p>
1109
* Returns an Visconte.Onto.OWLContainer by searching in the ontology for all subjects
1110
* that fit to the requested one. Param position indicates what should be constructed
1111
* for the query (0:Subject,1:Predicate,2:Object)
1112
* @param _object - the objects name as string
1113
* @param position - the position of the statment
1114
* @param type - the desired type of OWLItems to return (0:all, 1-4 see OWLItem.ITEMTYPE-Constant)
1115
* @return OWLContainer - the container with the items
1117
public Visconte.Onto.OWLContainer get_items_by_subject(string subject, int position, int type)
1119
Visconte.Onto.OWLContainer cnt = Visconte.Onto.OWLContainer();
1120
array ids = resources_to_ids(query(subject,0,0),position);
1121
foreach(ids,string id)
1123
if(type==0 || get_type(id)==type)
1124
cnt->add(get_Item(id));
1131
* <p>Query the ontology in a subject - predicate - object fashion</p>
1132
* Similar to find_statements, except the arguments are less restrictive in the
1135
* @param subject - string|RDF.Resource|int[0] - the subject
1136
* @param predicate - string|RDF.Resource|int[0] the predicate
1137
* @param object - string|RDF.Resource|int[0] - the object
1138
* @return array with mathing statements
1140
public array query(string|RDF.Resource|int subject, string|RDF.Resource|int predicate, string|RDF.Resource|int objekt)
1142
Visconte.Onto.OWLContainer container = Visconte.Onto.OWLContainer();
1143
if(stringp(subject))
1144
subject = owlset->make_resource(subject);
1146
if(stringp(predicate))
1147
predicate = owlset->make_resource(predicate);
1150
objekt = owlset->make_resource(objekt);
1152
return owlset->find_statements(subject,predicate,objekt);
1158
* <p>Returns the XML Representation of the Ontology</p>
1161
* @return string - xml rendering
1163
public string get_OWL()
1165
return to_string();;
1171
* <p>Equivalent to get_OWL.</p>
1173
* @return string - xml rendering
1175
public string to_string()
1177
return owlset->get_xml();
1181
* <p>Returns a String with the tuple representation of the ontology.</p>
1183
* @return string - 3tuple representation
1185
public string to_tuple()
1187
return owlset->get_3_tuples();
1192
* <p>Set the version to the ontology</p>
1195
private void set_version()
1197
Visconte.Onto.StatementContainer remove = Visconte.Onto.StatementContainer();
1198
Visconte.Onto.StatementContainer cont = Visconte.Onto.StatementContainer();
1199
array old_version = query(owlset->get_resource("#_THIS"),owl_versionInfo,0);
1200
if(!query_is_empty(old_version))
1201
remove->add(old_version[0][0] ,old_version[0][1],old_version[0][2]);
1202
cont->add(owlset->make_resource("#_THIS"),owlset->rdf_type,owl_Ontology);
1203
cont->add(owlset->get_resource("#_THIS"),owl_versionInfo,owlset->LiteralResource((string)version));
1204
update_statements(remove,cont);
1208
* <p>Get the current version of this ontology</p>
1209
* @return int - the versionnumber
1211
public int get_version()
1213
array v_info = query(owlset->get_resource(""),owl_versionInfo,0);
1214
if(!query_is_empty(v_info))
1215
return (int) v_info[0][2]->get_literal();
1221
* <p>Tries to fetchback a previous ontology</p>
1222
* Looks for the given version and loads the ontology. Set the version to 0
1223
* @param version - the version to fetchback
1226
public int fetchback_version(int version)
1228
string old_ont = storage_handler->fetchback(ontology_file,version);
1232
owlset->parse_xml(old_ont);
1243
* <p>Init the versioning</p>
1246
private void versioning()
1248
version=get_version();
1250
backup=owlset->get_xml();
1254
* <p>Saves the Ontology File.</p>
1255
* Will save the ontology, the backup and the logfile with the given StorageHandler
1259
//set version and save ontology
1261
storage_handler->save(ontology_file, owlset->get_xml());
1264
storage_handler->backup(ontology_file,backup,version);
1266
storage_handler->save(ontology_file+ontology_log_file_suffix,ontology_log->render_xml());
1271
* <p>Load the ontology File</p>
1277
//test if the choosen file exists
1278
if (storage_handler->exist(ontology_file))
1282
owlset->parse_xml(storage_handler->open(ontology_file));
1287
owlset->parse_xml(make_template());
1295
owlset->parse_xml(make_template());
1299
if (storage_handler->exist(ontology_file+ontology_log_file_suffix))
1301
ontology_log = Parser.XML.Tree.parse_input(storage_handler->open(ontology_file+ontology_log_file_suffix));
1305
ontology_log = Parser.XML.Tree.parse_input("<ontologyLog/>");
1312
* <p>Makes a template for an empty file</p>
1313
* @return template - a XML-string
1315
private string make_template()
1317
string template= "<?xml version='1.0'?>\n";
1318
template+="<rdf:RDF \n";
1319
template+="xmlns='http://www.w3.org/2002/07/owl#'\n";
1320
template+="xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'\n";
1321
template+="xmlns:rdfs='http://www.w3.org/2000/01/rdf-schema#'\n";
1322
template+="xmlns:annot='http://steam.upb.de/annot#'\n";
1323
template+="xmlns:steam='http://steam.upb.de/steam#'\n";
1324
template+="xmlns:xsd='http://www.w3.org/2001/XMLSchema#'\n";
1326
//the xsd:int is a hack to force the usage of this namespace!
1327
template+="<Class rdf:about='#Thing' rdfs:label='Abstract Thing' xsd:int='0'>\n<annot:author>Init</annot:author>\n</Class>";
1328
template+="\n</rdf:RDF>\n";
1337
* <p>Add an annotation to a single item</p>
1338
* This annotation is either automatically added by a specific action or a normal user comment
1340
* @param item (string or Visconte.Onto.Item.OWLItem) - the items or the itmes id
1341
* @paramstring action - the action to add to the annotation
1342
* @param string comment - the comment of the user
1343
* @param string|int affected, a item which is affected to of an action (optional)
1345
* @return int success
1347
public int add_annotation(Visconte.Onto.Item.OWLItem|string item, string action, string comment, string|int affected_label)
1349
Parser.XML.Tree.Node new_annotation_node;
1350
mapping(string:string) anotation_data = ([]);
1351
anotation_data+=(["user_id":user]);
1352
anotation_data+=(["action": action]);
1353
anotation_data+=(["timestamp": (string) time()]);
1354
anotation_data+=(["date": (string) ctime(time())]);
1357
string classContext="null";
1358
string classContextLabel="null";
1359
anotation_data+=(["about": Visconte.ID(item->get_Id())->uref]);
1360
anotation_data+=(["aboutLabel": item->get_Label()]);
1361
switch(item->ITEMTYPE)
1363
case Visconte.Onto.Item.OWLItem.CLASS:
1364
classContext = item->get_Id();
1365
classContextLabel = item->get_Label();
1368
case Visconte.Onto.Item.OWLItem.OBJECT_PROPERTY:
1369
classContext = item->get_domain()->get_Id();
1370
classContextLabel = item->get_domain()->get_Label();
1373
case Visconte.Onto.Item.OWLItem.DATATYPE_PROPERTY:
1374
classContext = item->get_domain()->get_Id();
1375
classContextLabel = item->get_domain()->get_Label();
1378
case Visconte.Onto.Item.OWLItem.INDIVIDUAL:
1379
classContext = item->get_model_class()->get_Id();
1380
classContextLabel = item->get_model_class()->get_Label();
1383
anotation_data+=(["classContext": Visconte.ID(classContext)->uref]);
1384
anotation_data+=(["classContextLabel": classContextLabel]);
1388
anotation_data+=(["about": "0"]);
1389
anotation_data+=(["aboutLabel": (string)item ]);
1390
anotation_data+=(["classContext": "0"]);
1393
anotation_data+=(["affectedLabel": (string) affected_label]);
1395
anotation_data+=(["ontologyVersion": (string) get_version() ]);
1396
anotation_data+=(["comment": comment]);
1397
ontology_log->get_last_child()->add_child(Parser.XML.Tree.Node(Parser.XML.Tree.XML_ELEMENT,"annotation",anotation_data,""));
1402
* <p>Returns the xml representation of the log file</p>
1404
* @return string xml
1406
public string get_annotations()
1408
return ontology_log->render_xml();
1412
* <p>Sets a class or individual as document term</p>
1414
* @param item (string or Visconte.Onto.Item.OWLItem)
1415
* @return int success
1417
public int set_as_document_term(Visconte.Onto.Item.OWLItem item, int isDocumentTerm)
1419
RDF.Resource res = owlset->get_resource(item->get_Id());
1420
Visconte.Onto.StatementContainer old_stmt = Visconte.Onto.StatementContainer();
1421
Visconte.Onto.StatementContainer stmt = Visconte.Onto.StatementContainer();
1422
//the new statements
1423
array documentterm = query(res,steam_is_document_term,0);
1424
if(!query_is_empty(documentterm))
1426
old_stmt->add(documentterm[0][0], documentterm[0][1], documentterm[0][2] );
1427
remove_statements(old_stmt);
1430
stmt->add(res,steam_is_document_term,owlset->LiteralResource("true"));
1432
stmt->add(res,steam_is_document_term,owlset->LiteralResource("false"));
1433
add_statements(stmt);
1437
* <p>Checks if a class or individual is as document term</p>
1439
* @param item (string or Visconte.Onto.Item.OWLItem)
1440
* @return int success
1442
public int is_document_term(Visconte.Onto.Item.OWLItem item)
1444
RDF.Resource res = owlset->get_resource(item->get_Id());
1445
Visconte.Onto.StatementContainer stmt = Visconte.Onto.StatementContainer();
1446
string isDocumentTerm = resource_to_id(query(res,steam_is_document_term,0),2);
1447
if(isDocumentTerm=="true")
1454
* <p>Sets properties for an individual of a document term</p>
1456
* @param item (string or Visconte.Onto.Item.OWLItem)
1457
* @return int success
1459
public int set_document_term_properties(Visconte.Onto.Item.OWLItem item, string objectID, string mimetype)
1461
if(item && item->get_model_class()->is_document_term())
1463
RDF.Resource res = owlset->get_resource(item->get_Id());
1464
Visconte.Onto.StatementContainer stmt = Visconte.Onto.StatementContainer();
1465
stmt->add(res,steam_objectid,owlset->LiteralResource(objectID));
1466
stmt->add(res,steam_mimetype,owlset->LiteralResource(mimetype));
1467
reset_document_term_properties(item);
1468
add_statements(stmt);
1473
* <p>Sets properties for an individual of a document term</p>
1475
* @param item (string or Visconte.Onto.Item.OWLItem)
1476
* @return int success
1478
public int reset_document_term_properties(Visconte.Onto.Item.OWLItem item)
1480
if(item && item->get_model_class()->is_document_term())
1482
RDF.Resource res = owlset->get_resource(item->get_Id());
1483
Visconte.Onto.StatementContainer old_stmt = Visconte.Onto.StatementContainer();
1484
array old_objectid = query(res,steam_objectid,0);
1485
array old_mimetype = query(res,steam_mimetype,0);
1486
//the old statements
1487
if(!query_is_empty(old_objectid))
1488
old_stmt->add(old_objectid[0][0], old_objectid[0][1], old_objectid[0][2] );
1489
if(!query_is_empty(old_mimetype))
1490
old_stmt->add(old_mimetype[0][0], old_mimetype[0][1], old_mimetype[0][2] );
1491
remove_statements(old_stmt);
1497
* <p>Return the object id and the mimetype of an indidual</p>
1500
* @param item (string or Visconte.Onto.Item.OWLItem)
1501
* @return int success
1503
public mapping get_steam_properties(Visconte.Onto.Item.OWLItem item)
1505
RDF.Resource res = owlset->get_resource(item->get_Id());
1506
Visconte.Onto.StatementContainer stmt = Visconte.Onto.StatementContainer();
1507
string objectid = (string)resource_to_id(query(res,steam_objectid,0),2);
1508
string mimetype = (string)resource_to_id(query(res,steam_mimetype,0),2);
1509
return ([ "objectid" : objectid, "mimetype" : mimetype ]);
1513
* <p>Appends a author and change date to the ontology elements</p>
1515
* @param item (string or Visconte.Onto.Item.OWLItem)
1516
* @return int success
1518
public int set_meta(Visconte.Onto.Item.OWLItem item)
1520
RDF.Resource res = owlset->get_resource(item->get_Id());
1521
Visconte.Onto.StatementContainer stmt = Visconte.Onto.StatementContainer();
1522
//the new statements
1523
stmt->add(res,anot_creator,owlset->LiteralResource(user));
1524
stmt->add(res,anot_created,owlset->LiteralResource((string) time()));
1526
add_statements(stmt);
1531
* <p>Check for the existence of an item in the OWLset</p>
1534
* @param item (string or Visconte.Onto.Item.OWLItem)
1535
* @return int success
1537
public int remove_meta(Visconte.Onto.Item.OWLItem item)
1539
RDF.Resource res = owlset->get_resource(item->get_Id());
1540
Visconte.Onto.StatementContainer old_stmt = Visconte.Onto.StatementContainer();
1541
array old_author = query(res,anot_creator,0);
1542
array old_date = query(res,anot_created,0);
1543
//the old statements
1544
if(!query_is_empty(old_author))
1545
old_stmt->add(old_author[0][0], old_author[0][1], old_author[0][2] );
1546
if(!query_is_empty(old_date))
1547
old_stmt->add(old_date[0][0], old_date[0][1], old_date[0][2] );
1549
remove_statements(old_stmt);
1553
* <p>Check for the existence of an item in the OWLset</p>
1556
* @param item (string or Visconte.Onto.Item.OWLItem)
1557
* @return int success
1559
public mapping get_meta(Visconte.Onto.Item.OWLItem item)
1561
RDF.Resource res = owlset->get_resource(item->get_Id());
1562
Visconte.Onto.StatementContainer stmt = Visconte.Onto.StatementContainer();
1563
string author = resource_to_id(query(res,anot_creator,0),2);
1564
string lastChanged = resource_to_id(query(res,anot_created,0),2);
1565
return ([ "author" : author, "lastChanged" : lastChanged ]);
1569
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1570
public void debug(string dbg)
1572
// write("\nDebugging: "+dbg+"\n");