~ubuntu-branches/ubuntu/wily/steam/wily

« back to all changes in this revision

Viewing changes to server/libraries/Visconte.pmod/Onto.pmod/Ontology.pike

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2013-10-29 19:51:18 UTC
  • mfrom: (1.1.4) (0.1.4 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20131029195118-b9bxciz5hwx5z459
Tags: 1:1.0.0.39-2ubuntu1
Add an epoch to the version number as there was an unrelated steam package
in the archive with a higher version number.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import Visconte.SemWeb;
2
 
 
3
 
/**
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
8
 
 */
9
 
 
10
 
/**
11
 
 * <p>Represents the xml schema namespace</p>
12
 
 */
13
 
    public constant xml_schema_base="http://www.w3.org/2001/XMLSchema";
14
 
    
15
 
/**
16
 
 * <p>Represents the xml schema namespace</p>
17
 
 */
18
 
    public constant THING="http://www.w3.org/2002/07/owl#Thing";
19
 
 
20
 
/**
21
 
 * <p>Represents the xml schema namespace</p>
22
 
 */
23
 
   private string template;
24
 
/**
25
 
 * <p>Represents the storage handler to load/save this ontology</p>
26
 
 */
27
 
    private object storage_handler;
28
 
 
29
 
/**
30
 
 * <p>Represents the ontology version</p>
31
 
 */
32
 
    private int version=0;
33
 
   
34
 
/**
35
 
 * <p>Represents the ontology filename</p>
36
 
 */
37
 
    private string ontology_file;
38
 
 
39
 
/**
40
 
 * <p>Represents the ontology filename</p>
41
 
 */
42
 
    private string ontology_log_file_suffix=".log";
43
 
    
44
 
/**
45
 
 * <p>Represents the ontology filename</p>
46
 
 */
47
 
    public  Parser.XML.Tree.Node ontology_log;
48
 
    
49
 
/**
50
 
 * <p>Represents the user who init this action, default anonym</p>
51
 
 */
52
 
    protected string user = "annonym";
53
 
    
54
 
/**
55
 
 * <p>The ontology as rdfset representation</p>
56
 
 */
57
 
    private OWL owlset;
58
 
    
59
 
/**
60
 
 * <p>The backup ot the current ontology</p>
61
 
 */
62
 
    private string backup;
63
 
    
64
 
/**
65
 
 * <p>The ObjectProperty Statement</p>
66
 
 */
67
 
    private OWL.OWLResource owl_ObjectProperty;
68
 
    
69
 
/**
70
 
 * <p>The DataTypeProperty Statement</p>
71
 
 */
72
 
    private OWL.OWLResource owl_DatatypeProperty;
73
 
 
74
 
/**
75
 
 * <p>The DataTypeProperty Statement</p>
76
 
 */
77
 
    private OWL.OWLResource owl_Ontology;
78
 
    
79
 
/**
80
 
 * <p>The DataTypeProperty Statement</p>
81
 
 */
82
 
    private OWL.OWLResource owl_versionInfo;  
83
 
 
84
 
/**
85
 
 * <p>The TransitiveProperty</p>
86
 
 */
87
 
    private OWL.OWLResource owl_TransitiveProperty;
88
 
    
89
 
/**
90
 
 * <p>The SymetricProperty</p>
91
 
 */
92
 
    private OWL.OWLResource owl_SymetricProperty;
93
 
 
94
 
/**
95
 
 * <p>Meta object for author</p>
96
 
 */    
97
 
    private RDF.URIResource anot_creator;
98
 
    
99
 
/**
100
 
 * <p>Meta object for author</p>
101
 
 */    
102
 
    private RDF.URIResource anot_changes;
103
 
    
104
 
/**
105
 
 * <p>Meta object for date of change</p>
106
 
 */    
107
 
    private RDF.URIResource anot_created;
108
 
 
109
 
/**
110
 
 * <p>is this a documenterm</p>
111
 
 */    
112
 
    private RDF.URIResource steam_is_document_term;
113
 
 
114
 
/**
115
 
 * <p>the object id</p>
116
 
 */    
117
 
    private RDF.URIResource steam_objectid;
118
 
 
119
 
/**
120
 
 * <p>the object id</p>
121
 
 */    
122
 
    private RDF.URIResource steam_mimetype;
123
 
 
124
 
       
125
 
/**
126
 
 * <p>The rdfs_label statement</p>
127
 
 */
128
 
    private RDFS.RDFSResource rdfs_label;
129
 
 
130
 
/**
131
 
 * <p>The Visconte.Onto.Item.OWLItem Factory to produce items</p>
132
 
 */
133
 
    private Visconte.Onto.OWLItemFactory factory = Visconte.Onto.OWLItemFactory();
134
 
 
135
 
 
136
 
    
137
 
/**
138
 
 * <p>Constructor: creates a new Ontology representation. If the ontology file not exists, 
139
 
 * a new ontology will be started</p>
140
 
 * 
141
 
 * 
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
146
 
 */
147
 
    public void create(string ontology_file, int version, object storage_handler, string|void user) 
148
 
    {        
149
 
        
150
 
            init();
151
 
            this_program::ontology_file=ontology_file;
152
 
            this_program::version=version;
153
 
            this_program::storage_handler = storage_handler;
154
 
            if(user)
155
 
                    this_program::user=user;
156
 
            
157
 
            load();
158
 
            
159
 
    } 
160
 
/**
161
 
 * <p>Inits common OWLResources and loads the ontology.</p>
162
 
 * 
163
 
 * 
164
 
 */
165
 
    private void init() 
166
 
    {        
167
 
        owlset = OWL();
168
 
        //set commomn OWL Ressources
169
 
        owl_ObjectProperty = owlset->OWLResource("ObjectProperty");
170
 
        owl_DatatypeProperty = owlset->OWLResource("DatatypeProperty");
171
 
        owl_TransitiveProperty = owlset->OWLResource("TransitiveProperty");
172
 
        
173
 
        //meta Items
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");
179
 
        
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;
186
 
 
187
 
    } 
188
 
    
189
 
    
190
 
/**
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
194
 
 * 
195
 
 * @param id - the id of the item as string
196
 
 * @return Visconte.Onto.Item.OWLItem
197
 
 */
198
 
    public Visconte.Onto.Item.OWLItem get_Item(string id) 
199
 
    {   
200
 
        
201
 
        Visconte.Onto.Item.OWLItem item;
202
 
        if (!item_exists(id))
203
 
        {
204
 
                
205
 
                throw( ({ "", backtrace(),11, id}) );
206
 
                
207
 
        }
208
 
        
209
 
        int type = get_type(id);
210
 
        switch(type)
211
 
        {
212
 
                case Visconte.Onto.Item.OWLItem.CLASS:
213
 
                item = get_Class(id);
214
 
                break;
215
 
                
216
 
                case Visconte.Onto.Item.OWLItem.OBJECT_PROPERTY:
217
 
                item = get_Property(id);
218
 
                break;
219
 
                
220
 
                case Visconte.Onto.Item.OWLItem.DATATYPE_PROPERTY:
221
 
                item = get_Property(id);
222
 
                break;
223
 
                
224
 
                case Visconte.Onto.Item.OWLItem.INDIVIDUAL:
225
 
                item = get_Indiviudal(id);
226
 
                break;
227
 
                
228
 
                default:
229
 
                 throw( ({ "",  backtrace(),601, id}) );
230
 
                break;
231
 
        }
232
 
        return item;
233
 
    } 
234
 
 
235
 
    
236
 
/**
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.
239
 
 * 
240
 
 * @param item 
241
 
 * @return int success
242
 
 */
243
 
    public int add_Item(Visconte.Onto.Item.OWLItem item) 
244
 
    {    
245
 
            
246
 
        //is this a valid item
247
 
        if(!item)
248
 
        {
249
 
                  throw( ({ "", backtrace(),11, 0}) );
250
 
                  return 0;
251
 
        }
252
 
        
253
 
        if(item_exists(item))
254
 
        {
255
 
                        throw( ({ "", backtrace(),602,item->get_Id()}) );                 
256
 
                        return 0;
257
 
        }
258
 
 
259
 
        switch(item->ITEMTYPE)
260
 
        {
261
 
                //if class
262
 
                case Visconte.Onto.Item.OWLItem.CLASS:
263
 
                return add_Class(item);
264
 
                break;
265
 
                
266
 
                case Visconte.Onto.Item.OWLItem.OBJECT_PROPERTY:
267
 
                return add_ObjectProperty(item);
268
 
                break;
269
 
                
270
 
                case Visconte.Onto.Item.OWLItem.DATATYPE_PROPERTY:
271
 
                return add_DatatypeProperty(item);
272
 
                break;
273
 
                
274
 
                case Visconte.Onto.Item.OWLItem.INDIVIDUAL:
275
 
                return add_Individual(item);
276
 
                break;
277
 
                
278
 
                default:
279
 
                 throw( ({ "", backtrace(),601,item->get_Id()}) );
280
 
                break;
281
 
                
282
 
        }
283
 
        return 1;
284
 
        
285
 
    } 
286
 
 
287
 
    
288
 
// Class Related
289
 
    
290
 
/**
291
 
 * <p>Adds a class to the ontology</p>
292
 
 * @param item - the OWLClass to add * 
293
 
 * @return success
294
 
 */
295
 
 private int add_Class(Visconte.Onto.Item.OWLItem item)
296
 
 {
297
 
        add_statements(create_Class_statement(item));
298
 
        set_meta(item);
299
 
        return 1;
300
 
 }
301
 
    
302
 
 
303
 
/**
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
306
 
 *
307
 
 * @param id - the id as string
308
 
 * @return Visconte.Onto.Item.OWLClass class
309
 
 * 
310
 
 */
311
 
    public Visconte.Onto.Item.OWLClass get_Class(string id) 
312
 
    {  
313
 
         
314
 
            if (item_exists(id))
315
 
            {
316
 
                    mixed parent_id;
317
 
                
318
 
                    RDF.Resource item = owlset->get_resource(id);
319
 
                    
320
 
                    //get the parent class (if one)
321
 
                    parent_id = resource_to_id(query(item,owlset->rdfs_subClassOf,0),2);
322
 
                 
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);
325
 
                    
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);
328
 
                    
329
 
                    //get the descendants ot this class
330
 
                    array childs = resources_to_ids(query(0,owlset->rdfs_subClassOf,item), 0);
331
 
                    
332
 
                    //get the diffenrent form classes
333
 
                    array different_from = resources_to_ids(query(item,owlset->owl_differentFrom,0),2);
334
 
                   
335
 
                    //get the diffenrent form classes
336
 
                    array equivalent_classes = resources_to_ids(query(item,owlset->owl_equivalentClass,0),2);
337
 
                    
338
 
                    array individuals = resources_to_ids(query(0,owlset->rdf_type,item),0);
339
 
                     
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);
342
 
                    return owl_class;
343
 
            }
344
 
            else
345
 
            {
346
 
                    throw( ({ "", backtrace(),11, id}) );
347
 
                    return 0;
348
 
            }
349
 
           
350
 
    }
351
 
 
352
 
 
353
 
/**
354
 
 * <p>Transforms a Visconte.Onto.Item.OWLClass to a Container of RDF-Statements</p>
355
 
 * 
356
 
 * @param item - the class to translate into statement
357
 
 * @return Visconte.Onto.StatementContainer - the statements
358
 
 */ 
359
 
 private Visconte.Onto.StatementContainer create_Class_statement(Visconte.Onto.Item.OWLItem item)
360
 
 {
361
 
        Visconte.Onto.StatementContainer stmt_container = Visconte.Onto.StatementContainer();
362
 
         //construct Resource
363
 
        OWL.OWLResource class_resource = owlset->make_resource(item->get_Id());
364
 
        //add the class
365
 
        stmt_container->add(class_resource,owlset->rdf_type, owlset->owl_Class);
366
 
        //add the label
367
 
        stmt_container->add(class_resource,rdfs_label,owlset->LiteralResource(item->get_Label()));
368
 
        
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())
373
 
        {
374
 
                eq_class=c->next();
375
 
                stmt_container->add(class_resource,owlset->owl_equivalentClass,owlset->get_resource(eq_class->get_Id()));
376
 
        }
377
 
        //add different classes
378
 
        Visconte.Onto.Item.OWLItem dif_class;
379
 
        c=item->get_different_from_classes();
380
 
        while(c->has_items())
381
 
        {
382
 
                diff_class=c->next();
383
 
                stmt_container->add(class_resource,owlset->owl_differentFrom,owlset->get_resource(diff_class->get_Id()));
384
 
        }
385
 
        
386
 
        
387
 
        //set as subclass if parent exists
388
 
        if(item->has_parent() && item_exists(item->get_parent()))
389
 
        {
390
 
                stmt_container->add(class_resource,owlset->rdfs_subClassOf,owlset->get_resource( item->get_parent()->get_Id() ) );
391
 
                
392
 
        }
393
 
        else
394
 
        {
395
 
                stmt_container->add(class_resource,owlset->rdfs_subClassOf,owlset->get_resource(THING));
396
 
        }
397
 
        return stmt_container;
398
 
 }
399
 
 
400
 
 
401
 
 
402
 
// Individual related
403
 
 
404
 
/**
405
 
 * <p>Adds an Individual to the ontology</p>
406
 
 * 
407
 
 * @param item - OWLIndividual to add
408
 
 * @return int success
409
 
 */
410
 
 private int add_Individual(Visconte.Onto.Item.OWLItem item)
411
 
 {
412
 
        
413
 
         add_statements(create_Individual_statement(item));
414
 
         set_meta(item);
415
 
         return 1;
416
 
 } 
417
 
 
418
 
 /**
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
421
 
 *
422
 
 * @param id - the id as string
423
 
 * @return Visconte.Onto.Item.OWLIndividual individual
424
 
 * 
425
 
 */
426
 
    public Visconte.Onto.Item.OWLIndividual get_Indiviudal(string id) 
427
 
    {
428
 
            if (item_exists(id))
429
 
            {
430
 
            
431
 
                     RDF.Resource individual = owlset->get_resource(id);
432
 
                     
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);
437
 
                     do
438
 
                     {
439
 
                             Visconte.Onto.OWLContainer property_container = model_class->get_domain_properties();
440
 
                             
441
 
                             while(property_container->has_items())
442
 
                             {
443
 
                                     Visconte.Onto.Item.OWLItem item = property_container->next();
444
 
                                     //add this property to all inherited properties
445
 
                                     all_properties->add(item);
446
 
                                     
447
 
                                     RDF.Resource property = owlset->get_resource(item->get_Id());
448
 
                                     array resources;
449
 
                                     if(item->ITEMTYPE==Visconte.Onto.Item.OWLItem.OBJECT_PROPERTY)
450
 
                                     {
451
 
                                              resources = resources_to_ids(query(individual,property,0),2);
452
 
                                     }
453
 
                                     else
454
 
                                     {
455
 
                                              resources = resources_to_literals(query(individual,property,0),2);
456
 
                                     }
457
 
                                     if(sizeof(resources)>0)
458
 
                                               property_values->add(item->get_Id() ,resources );
459
 
                             } 
460
 
                             
461
 
                             model_class=model_class->get_parent();
462
 
                     }
463
 
                     while(model_class!=0);
464
 
                  
465
 
                    // write("RETR:"+id+"\n");
466
 
                    return Visconte.Onto.Item.OWLIndividual( this_object(), id,class_id, get_label(id), all_properties, property_values);
467
 
             }
468
 
            else
469
 
            {
470
 
                    throw( ({ "", backtrace(),11, id}) );
471
 
                    return 0;
472
 
            }
473
 
    }
474
 
 
475
 
/**
476
 
 * <p>Transforms a Visconte.Onto.Item.OWLIndividual to a Container of RDF-Statements</p>
477
 
 * 
478
 
 * @param item - the class to translate into statement
479
 
 * @return Visconte.Onto.StatementContainer - the statements
480
 
 */ 
481
 
 private Visconte.Onto.StatementContainer create_Individual_statement(Visconte.Onto.Item.OWLItem item)
482
 
 {
483
 
 
484
 
         Visconte.Onto.StatementContainer stmt_container = Visconte.Onto.StatementContainer();
485
 
        
486
 
         Visconte.Onto.Item.OWLClass model_class = item->get_model_class();
487
 
         //construct Resource
488
 
         
489
 
         OWL.OWLResource individual_resource = owlset->make_resource(item->get_Id());
490
 
         
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());
495
 
         else
496
 
                 model_resource = owlset->make_resource(THING);
497
 
         
498
 
         //add Individual
499
 
         stmt_container->add(individual_resource,owlset->rdf_type,model_resource);
500
 
         //add the label
501
 
         stmt_container->add(individual_resource,rdfs_label,owlset->LiteralResource(item->get_Label()));
502
 
        
503
 
         //add the properties by testing the defined properties in the modelclass and it parents
504
 
         do
505
 
         {
506
 
                 if(item_exists(model_class))
507
 
                 {
508
 
                         array(array(mixed)) property_map = item->get_property_map();
509
 
                         Visconte.Onto.OWLContainer control_properties = model_class->get_domain_properties();
510
 
                         
511
 
                         while(control_properties->has_items())
512
 
                         {
513
 
                                  
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)
517
 
                                {
518
 
                                        string property_id=prop_value[0]->get_Id();
519
 
                                        if(property_id==control_id)
520
 
                                        {
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]);
524
 
                                                 else
525
 
                                                        value_resource=owlset->LiteralResource(prop_value[1]);
526
 
                                                        
527
 
                                                 stmt_container->add(individual_resource,owlset->get_resource(control_id),value_resource);
528
 
                                        }
529
 
                                }
530
 
                                 
531
 
                         }
532
 
                 }
533
 
                  model_class=model_class->get_parent();
534
 
         }
535
 
         while(model_class!=0);
536
 
                
537
 
         return stmt_container;
538
 
         
539
 
         
540
 
 }
541
 
 
542
 
// Property Related
543
 
 
544
 
 
545
 
 
546
 
/**
547
 
 * <p>Adds a Object Property to the ontology</p>
548
 
 * 
549
 
 * 
550
 
 * @param item - the OWLObjectProperty to add
551
 
 * @return int success
552
 
 */
553
 
 private int add_ObjectProperty(Visconte.Onto.Item.OWLItem item)
554
 
 {
555
 
        
556
 
        add_statements(create_ObjectProperty_statement(item));
557
 
        set_meta(item);
558
 
        return 1;
559
 
                
560
 
 } 
561
 
 
562
 
 
563
 
/**
564
 
 * <p>Adds a Datatype Property to the ontology</p>
565
 
 * 
566
 
 * @param item - the OWLDatatypeProperty to add
567
 
 * @return int success
568
 
 */
569
 
 private int add_DatatypeProperty(Visconte.Onto.Item.OWLItem item)
570
 
 {
571
 
        add_statements(create_DatatypeProperty_statement(item));
572
 
         set_meta(item);
573
 
        return 1;
574
 
                
575
 
 } 
576
 
 
577
 
/**
578
 
 * <p>Get a property with a specific id</p>
579
 
 *
580
 
 * @param id - the id as string
581
 
 * @return OWLProperty property
582
 
 * 
583
 
 */
584
 
    public Visconte.Onto.Item.OWLProperty get_Property(string id) 
585
 
    { 
586
 
            Visconte.Onto.Item.OWLProperty property;
587
 
            string domain, range, range_namespace;
588
 
            
589
 
             if (item_exists(id))
590
 
             {
591
 
                RDF.Resource item = owlset->get_resource(id);
592
 
                //retrieve if its a Datatype or an Object Property by querieing the range type
593
 
                
594
 
                range=resource_to_id(query(item,owlset->rdfs_range,0),2);
595
 
                domain=resource_to_id(query(item,owlset->rdfs_domain,0),2);
596
 
                
597
 
                //test for datatype
598
 
                if(has_prefix(range,"xsd:"))
599
 
                {
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);
602
 
                }
603
 
                else
604
 
                {                       
605
 
                                property = Visconte.Onto.Item.OWLObjectProperty(this_object(),id,get_Property_restriction(id),get_label(id),get_Class(domain),get_Class(range));
606
 
                }
607
 
                
608
 
                return property;
609
 
             }
610
 
             else
611
 
             {
612
 
                    
613
 
                     throw( ({ "",  backtrace(),11, id}) );                
614
 
                     return 0;
615
 
             }
616
 
    }    
617
 
    
618
 
/**
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
623
 
 * 
624
 
 */
625
 
    private mapping get_Property_restriction(string id) 
626
 
    {     
627
 
           // array restriction;
628
 
            return (["transitive" : 1, "symetric" : 1]);
629
 
    }
630
 
    
631
 
 
632
 
/**
633
 
 * <p>Construct the resources container for the Object Property</p>
634
 
 * 
635
 
 * 
636
 
* @param item - the property (OWLObjectProperty) to translate into statement
637
 
 * @return Visconte.Onto.StatementContainer - the statements
638
 
 */
639
 
 private Visconte.Onto.StatementContainer create_ObjectProperty_statement(Visconte.Onto.Item.OWLItem item)
640
 
 {
641
 
        Visconte.Onto.StatementContainer stmt_container = Visconte.Onto.StatementContainer();
642
 
 
643
 
        
644
 
        OWL.OWLResource property_resource = owlset->make_resource(item->get_Id());
645
 
        OWL.OWLResource domain;
646
 
        OWL.OWLResource range;
647
 
        
648
 
        if(item_exists(item->get_domain()))
649
 
        {
650
 
                domain = owlset->get_resource(item->get_domain()->get_Id());
651
 
        }
652
 
        else
653
 
        {
654
 
                domain = owlset->make_resource(THING);
655
 
        }
656
 
        
657
 
        if(item_exists(item->get_range()))
658
 
        {
659
 
                range = owlset->get_resource(item->get_range()->get_Id());
660
 
        }
661
 
        else
662
 
        {
663
 
                range = owlset->make_resource(THING);
664
 
        }
665
 
        
666
 
        
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()));
671
 
        
672
 
        return stmt_container;
673
 
                
674
 
 }
675
 
 
676
 
/**
677
 
 * <p>Adds a Datatype Property to the ontology</p>
678
 
 * 
679
 
 * @param item - the property (OWLDatatypeProperty) to add
680
 
 * @return success
681
 
 */
682
 
 private Visconte.Onto.StatementContainer create_DatatypeProperty_statement(Visconte.Onto.Item.OWLItem item)
683
 
 {
684
 
        Visconte.Onto.StatementContainer stmt_container = Visconte.Onto.StatementContainer();
685
 
        
686
 
        OWL.OWLResource property_resource = owlset->make_resource(item->get_Id());
687
 
        OWL.OWLResource domain;
688
 
        OWL.OWLResource range;
689
 
        
690
 
        if(item_exists(item->get_domain()))
691
 
        {
692
 
                domain = owlset->get_resource(item->get_domain()->get_Id());
693
 
        }
694
 
        else
695
 
        {
696
 
                domain = owlset->make_resource(THING);
697
 
        }
698
 
        
699
 
        //the range of the property
700
 
        range = owlset->make_resource(xml_schema_base+"#"+item->get_range());
701
 
        
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()));
706
 
 
707
 
        return stmt_container;
708
 
                
709
 
 }
710
 
 
711
 
 
712
 
 
713
 
 // Statement handling
714
 
 
715
 
 /**
716
 
 * <p>add the statement to the ontology</p>
717
 
 * 
718
 
 * @param Visconte.Onto.StatementContainer container - the statements to add 
719
 
 * @return 
720
 
 */
721
 
 private int add_statements(Visconte.Onto.StatementContainer container)
722
 
 {
723
 
        mapping stmt;
724
 
        while(container->has_statements())
725
 
        {
726
 
                stmt = container->next();
727
 
                owlset->add_statement(stmt["subj"],stmt["pred"],stmt["obj"]);
728
 
        }
729
 
        
730
 
        return 1;
731
 
 }
732
 
 
733
 
 /**
734
 
 * <p>removes statements from the ontology</p>
735
 
 * 
736
 
 * @param Visconte.Onto.StatementContainer container - the statements to remove 
737
 
 * @return 
738
 
 */
739
 
 private int remove_statements(Visconte.Onto.StatementContainer container)
740
 
 {
741
 
        mapping stmt;
742
 
        while(container->has_statements())
743
 
        {
744
 
                stmt = container->next();
745
 
                // workaround for literals
746
 
                if(stmt["obj"]->is_literal_resource)
747
 
                {
748
 
                         array lit = query(stmt["subj"],stmt["pred"],0);
749
 
                         if(!query_is_empty(lit))
750
 
                         stmt["obj"]=lit[0][2];
751
 
                        
752
 
                }
753
 
                owlset->remove_statement(stmt["subj"],stmt["pred"],stmt["obj"]);
754
 
        }
755
 
 }
756
 
/**
757
 
 * <p>upadtes statements in the ontology</p>
758
 
 * 
759
 
 * 
760
 
 * @param Visconte.Onto.StatementContainer container - the statements to update 
761
 
 * @return 
762
 
 */
763
 
 private int update_statements(Visconte.Onto.StatementContainer old, Visconte.Onto.StatementContainer new)
764
 
 {
765
 
              remove_statements(old);
766
 
              add_statements(new);
767
 
 }
768
 
 
769
 
 
770
 
 
771
 
/**
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) 
776
 
 
777
 
 */
778
 
    public mapping get_label(string id)
779
 
    {
780
 
            string label=id;
781
 
            string lang="en";
782
 
            array label_query = query(id,rdfs_label,0);
783
 
            if(!query_is_empty(label_query))
784
 
            {
785
 
                    label=label_query[0][2]->get_literal();
786
 
            }
787
 
            return ([lang : label]);
788
 
    }
789
 
   
790
 
 
791
 
/**
792
 
 * <p>Checks if a query array is empty</p>
793
 
 * 
794
 
 * 
795
 
 * @param query - the query array  
796
 
 * @return int success
797
 
 */
798
 
    public int query_is_empty(array query)
799
 
    {
800
 
            if(sizeof(query)>0)
801
 
                    return 0;
802
 
            else
803
 
                    return 1;
804
 
    }
805
 
    
806
 
/**
807
 
 * <p>Transforms resources to literals and returns them as array</p>
808
 
 * 
809
 
 * 
810
 
 * @param query - the query array
811
 
 * @param statement_position - 0=Subject, 1=Predicate, 2=Object
812
 
 * @return literals - array with literals
813
 
 */
814
 
    public array resources_to_literals(array query,int statement_position)
815
 
    {
816
 
            
817
 
        string literal;
818
 
        array literals=({ });
819
 
        if(!query_is_empty(query))
820
 
        {
821
 
                foreach(query,array result)
822
 
                {
823
 
                        if(result[statement_position]->is_literal_resource)
824
 
                        {
825
 
                                literal=result[statement_position]->get_literal();
826
 
                                literals+=({literal});
827
 
                        }
828
 
                }
829
 
        }
830
 
        return literals;
831
 
    }   
832
 
    
833
 
/**
834
 
 * <p>Transforms a single resources to an id and returns it</p>
835
 
 * 
836
 
 * @param query - the query array
837
 
 * @param statement_position - 0=Subject, 1=Predicate, 2=Object
838
 
 * @return id - the id as string
839
 
 */
840
 
    public string resource_to_id(array query,int statement_position)
841
 
    { 
842
 
        string id;
843
 
        if(!query_is_empty(query))
844
 
        {
845
 
                //debug(sprintf("%O",query));
846
 
                if(query[0][statement_position]->is_literal_resource)
847
 
                        
848
 
                        id=namespace_conversion(query[0][statement_position]->get_literal());
849
 
                else
850
 
                        id=Visconte.ID(namespace_conversion(query[0][statement_position]->get_qname()))->ref;
851
 
                
852
 
        }
853
 
        
854
 
        return id;
855
 
    }
856
 
    
857
 
/**
858
 
 * <p>Transforms resources to ids and returns them as array</p>
859
 
 * 
860
 
 * @param query - the query array
861
 
 * @param statement_position - 0=Subject, 1=Predicate, 2=Object
862
 
 * @return id - array with ids
863
 
 */
864
 
    public array resources_to_ids(array query,int statement_position)
865
 
    {
866
 
        string id;
867
 
        array ids=({ });
868
 
        
869
 
        if(!query_is_empty(query))
870
 
        {       
871
 
                foreach(query,array result)
872
 
                {
873
 
                        if(!result[2]->is_literal_resource)
874
 
                        {
875
 
                                id=Visconte.ID(namespace_conversion(result[statement_position]->get_qname()))->ref;
876
 
                                ids+=({id});
877
 
                        }
878
 
                }
879
 
        }
880
 
        return ids;
881
 
    }       
882
 
 
883
 
/**
884
 
 * <p>Checks an id for trailing basenames an returns them a modfied id</p>
885
 
 * 
886
 
 * 
887
 
 * @param id - the id to check 
888
 
 * @return the id
889
 
 */
890
 
    public string namespace_conversion(string id)
891
 
    {
892
 
            int basename_separator_pos = search(id,":");
893
 
            if(basename_separator_pos>=0)
894
 
            {
895
 
                    string base = id[0..basename_separator_pos];
896
 
                    string identifier = id[basename_separator_pos+1..];                  
897
 
                    switch (base)
898
 
                    {
899
 
                            case "xsd:":
900
 
                            return id;
901
 
                            break;
902
 
                            
903
 
                            case "owl:":
904
 
                            return id;
905
 
                            break;
906
 
                            
907
 
                            case "rdfs:":
908
 
                            return id;
909
 
                            break;
910
 
                            
911
 
                            case "rdf:":
912
 
                            return id;
913
 
                            break;
914
 
                            
915
 
                            default:
916
 
                            return "#"+identifier;
917
 
                            break;
918
 
                    }
919
 
            }
920
 
            return id;
921
 
    }
922
 
    
923
 
 
924
 
/**
925
 
 * <p>Check for the existence of an item in the Ontology</p>
926
 
 * 
927
 
 * 
928
 
 * @param item (string or Visconte.Onto.Item.OWLItem)
929
 
 * @return int success 
930
 
 */
931
 
    public int item_exists(string|Visconte.Onto.Item.OWLItem item) 
932
 
    {   
933
 
        string id;
934
 
        if(!item)
935
 
                return 0;
936
 
        if (objectp(item))
937
 
        {
938
 
                id=item->get_Id();
939
 
        }
940
 
        else
941
 
                id=Visconte.ID(item)->ref;
942
 
        
943
 
        RDF.Resource test = owlset->get_resource(id);
944
 
        if (test)
945
 
                return 1;
946
 
        else
947
 
                return 0;
948
 
    } 
949
 
 
950
 
 
951
 
 
952
 
/**
953
 
 * <p>Replace an old item with a new one</p>
954
 
 * 
955
 
 * 
956
 
 * @param old_item - the item to replace
957
 
 * @param new_item - the exchange item
958
 
 */
959
 
    public void update(Visconte.Onto.Item.OWLItem updated_item) 
960
 
    {      
961
 
            //the actual item on the storage
962
 
            Visconte.Onto.Item.OWLItem  current_item;
963
 
            
964
 
       switch(updated_item->ITEMTYPE)
965
 
        {
966
 
                //if class
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));
970
 
                break;
971
 
                
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));
975
 
                break;
976
 
                
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));
980
 
                break;
981
 
                
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));
985
 
                break;
986
 
        }
987
 
        
988
 
    } 
989
 
 
990
 
/**
991
 
 * <p>Removes an item from the ontology</p>
992
 
 * 
993
 
 * 
994
 
 * @param item - the OWLItem to remove
995
 
 * @return int success
996
 
 */
997
 
    public int remove(Visconte.Onto.Item.OWLItem owlitem) 
998
 
    {        
999
 
        Visconte.Onto.StatementContainer cont = Visconte.Onto.StatementContainer();
1000
 
        remove_meta(owlitem);
1001
 
        
1002
 
        switch(owlitem->ITEMTYPE)
1003
 
        {
1004
 
                
1005
 
                case Visconte.Onto.Item.OWLItem.CLASS:
1006
 
                        
1007
 
                if(owlitem->is_root())
1008
 
                        return 0;
1009
 
                remove_statements(create_Class_statement(owlitem));
1010
 
                break;
1011
 
                        
1012
 
                case Visconte.Onto.Item.OWLItem.OBJECT_PROPERTY:
1013
 
                remove_statements(create_ObjectProperty_statement(owlitem));
1014
 
                break;
1015
 
                
1016
 
                case Visconte.Onto.Item.OWLItem.DATATYPE_PROPERTY:
1017
 
                remove_statements(create_DatatypeProperty_statement(owlitem));
1018
 
                break;
1019
 
                
1020
 
                case Visconte.Onto.Item.OWLItem.INDIVIDUAL:
1021
 
                remove_statements(create_Individual_statement(owlitem));
1022
 
                break;
1023
 
        }
1024
 
                
1025
 
    }
1026
 
    
1027
 
/**
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.)
1031
 
 *
1032
 
 * @param id 
1033
 
 * @return int the constant of the  type
1034
 
 * 
1035
 
 */
1036
 
    public int get_type(string id) 
1037
 
    {  
1038
 
            int type=0;
1039
 
            if(item_exists(id))
1040
 
            {
1041
 
                    RDF.Resource item = owlset->get_resource(id);
1042
 
                    
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)
1046
 
                    {   
1047
 
                            //object statement
1048
 
                            string typename = statement[0][2]->get_qname();
1049
 
                            if(typename=="Class" || typename=="owl:Class")
1050
 
                                    type=Visconte.Onto.Item.OWLItem.CLASS;
1051
 
                            
1052
 
                            else if(typename=="ObjectProperty" || typename=="owl:ObjectProperty")
1053
 
                                    type=Visconte.Onto.Item.OWLItem.OBJECT_PROPERTY;
1054
 
                            
1055
 
                            else if(typename=="DatatypeProperty" || typename=="owl:DatatypeProperty")
1056
 
                                    type=Visconte.Onto.Item.OWLItem.DATATYPE_PROPERTY;
1057
 
                            
1058
 
                            else 
1059
 
                                    type=Visconte.Onto.Item.OWLItem.INDIVIDUAL;
1060
 
                    }
1061
 
            }
1062
 
                    
1063
 
        return type;
1064
 
    } 
1065
 
 
1066
 
    
1067
 
/**
1068
 
 * <p>Generates an OWLContainer from an array of ids</p>
1069
 
 * 
1070
 
 * @param ids - an array with ids
1071
 
 * @return OWLContainer - the container with the items 
1072
 
 */
1073
 
    public Visconte.Onto.OWLContainer get_Items(array ids) 
1074
 
    {     
1075
 
        Visconte.Onto.OWLContainer container = Visconte.Onto.OWLContainer();
1076
 
         
1077
 
        foreach(ids ,string id)
1078
 
        {
1079
 
           container->add(get_Item(Visconte.ID(id)->ref));
1080
 
        }
1081
 
        return container;
1082
 
    } 
1083
 
 
1084
 
/**
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 
1093
 
 */
1094
 
    public Visconte.Onto.OWLContainer get_items_by_object(string _object, int position, int type) 
1095
 
    {
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)
1099
 
            {
1100
 
                           if(type==0 || get_type(id)==type)
1101
 
                           object_cnt->add(get_Item(id));
1102
 
            }
1103
 
            return object_cnt;
1104
 
            
1105
 
    }
1106
 
    
1107
 
/**
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 
1116
 
 */
1117
 
    public Visconte.Onto.OWLContainer get_items_by_subject(string subject, int position, int type) 
1118
 
    {
1119
 
            Visconte.Onto.OWLContainer cnt = Visconte.Onto.OWLContainer();
1120
 
            array ids = resources_to_ids(query(subject,0,0),position);
1121
 
            foreach(ids,string id)
1122
 
            {
1123
 
                           if(type==0 || get_type(id)==type)
1124
 
                           cnt->add(get_Item(id));
1125
 
            }
1126
 
            
1127
 
            return cnt;
1128
 
            
1129
 
    }
1130
 
/**
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 
1133
 
 * RDF-Class
1134
 
 * 
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
1139
 
 */
1140
 
    public array query(string|RDF.Resource|int subject, string|RDF.Resource|int predicate, string|RDF.Resource|int objekt) 
1141
 
    {       
1142
 
       Visconte.Onto.OWLContainer container = Visconte.Onto.OWLContainer();
1143
 
       if(stringp(subject)) 
1144
 
               subject = owlset->make_resource(subject);
1145
 
    
1146
 
       if(stringp(predicate)) 
1147
 
               predicate = owlset->make_resource(predicate);
1148
 
       
1149
 
       if(stringp(objekt)) 
1150
 
               objekt = owlset->make_resource(objekt);
1151
 
    
1152
 
       return owlset->find_statements(subject,predicate,objekt);
1153
 
               
1154
 
    } 
1155
 
 
1156
 
    
1157
 
/**
1158
 
 * <p>Returns the XML Representation of the Ontology</p>
1159
 
 * 
1160
 
 * 
1161
 
 * @return string - xml rendering
1162
 
 */
1163
 
    public string get_OWL() 
1164
 
    {        
1165
 
        return to_string();;
1166
 
    } 
1167
 
            
1168
 
   
1169
 
 
1170
 
/**
1171
 
 * <p>Equivalent to get_OWL.</p>
1172
 
 * 
1173
 
 * @return string - xml rendering
1174
 
 */
1175
 
    public string to_string() 
1176
 
    {    
1177
 
            return owlset->get_xml();
1178
 
    
1179
 
    }
1180
 
/**
1181
 
 * <p>Returns a String with the tuple representation of the ontology.</p>
1182
 
 *
1183
 
 * @return string - 3tuple representation 
1184
 
 */
1185
 
    public string to_tuple() 
1186
 
    {    
1187
 
            return owlset->get_3_tuples();
1188
 
    }   
1189
 
 
1190
 
   
1191
 
/**
1192
 
 * <p>Set the version to the ontology</p>
1193
 
 * 
1194
 
 */
1195
 
    private void set_version() 
1196
 
    {   
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);
1205
 
    }   
1206
 
 
1207
 
/**
1208
 
 * <p>Get the current version of this ontology</p>
1209
 
 * @return int - the versionnumber
1210
 
 */
1211
 
    public int get_version()
1212
 
    {
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();
1216
 
             else
1217
 
                    return 1;
1218
 
    }
1219
 
    
1220
 
/**
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
1224
 
 * @return succes
1225
 
 */
1226
 
    public int fetchback_version(int version)
1227
 
    {
1228
 
        string old_ont = storage_handler->fetchback(ontology_file,version);
1229
 
        if(old_ont)
1230
 
        {
1231
 
                init();
1232
 
                owlset->parse_xml(old_ont);
1233
 
                version=0;
1234
 
                return 1;
1235
 
                
1236
 
        }
1237
 
        else
1238
 
                return 0;
1239
 
    }
1240
 
            
1241
 
            
1242
 
/**
1243
 
 * <p>Init the versioning</p>
1244
 
 * 
1245
 
 */
1246
 
    private void versioning() 
1247
 
    {   
1248
 
        version=get_version();
1249
 
        version++;
1250
 
        backup=owlset->get_xml();
1251
 
    }     
1252
 
   
1253
 
/**
1254
 
 * <p>Saves the Ontology File.</p>
1255
 
 * Will save the ontology, the backup and the logfile with the given StorageHandler
1256
 
 */
1257
 
    public void save() 
1258
 
    {    
1259
 
       //set version and save ontology
1260
 
       set_version();
1261
 
       storage_handler->save(ontology_file, owlset->get_xml());
1262
 
       //save backup
1263
 
       if(version>1)
1264
 
               storage_handler->backup(ontology_file,backup,version);
1265
 
       
1266
 
       storage_handler->save(ontology_file+ontology_log_file_suffix,ontology_log->render_xml());
1267
 
       
1268
 
    } 
1269
 
 
1270
 
/**
1271
 
 * <p>Load the ontology File</p>
1272
 
 * 
1273
 
 */
1274
 
    private void load() 
1275
 
    {     
1276
 
        
1277
 
        //test if the choosen file exists
1278
 
        if (storage_handler->exist(ontology_file))
1279
 
        {
1280
 
                mixed error = catch
1281
 
                {
1282
 
                        owlset->parse_xml(storage_handler->open(ontology_file));
1283
 
                        versioning();
1284
 
                };
1285
 
                if(error)
1286
 
                {
1287
 
                        owlset->parse_xml(make_template());
1288
 
                        version=1;
1289
 
                        set_version();
1290
 
                }
1291
 
                
1292
 
        }
1293
 
        else
1294
 
        {
1295
 
                owlset->parse_xml(make_template());
1296
 
                version=1;
1297
 
                set_version();
1298
 
        }
1299
 
        if (storage_handler->exist(ontology_file+ontology_log_file_suffix))
1300
 
        {
1301
 
                ontology_log = Parser.XML.Tree.parse_input(storage_handler->open(ontology_file+ontology_log_file_suffix));
1302
 
        }
1303
 
        else
1304
 
        {
1305
 
                ontology_log = Parser.XML.Tree.parse_input("<ontologyLog/>");
1306
 
        }
1307
 
        
1308
 
                
1309
 
    } 
1310
 
 
1311
 
/**
1312
 
 * <p>Makes a template for an empty file</p>
1313
 
 * @return template - a XML-string 
1314
 
 */
1315
 
  private string make_template() 
1316
 
  {
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";
1325
 
                 template+=">\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";
1329
 
                
1330
 
                return template;
1331
 
                
1332
 
 
1333
 
 
1334
 
  }  
1335
 
    
1336
 
/**
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
1339
 
 * 
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)
1344
 
 * 
1345
 
 * @return int success 
1346
 
 */
1347
 
    public int add_annotation(Visconte.Onto.Item.OWLItem|string item, string action, string comment, string|int affected_label) 
1348
 
    { 
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())]);
1355
 
                if(objectp(item))
1356
 
                {
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)
1362
 
                        {
1363
 
                                case Visconte.Onto.Item.OWLItem.CLASS:
1364
 
                                classContext = item->get_Id();
1365
 
                                classContextLabel = item->get_Label();
1366
 
                                break;
1367
 
                                
1368
 
                                case Visconte.Onto.Item.OWLItem.OBJECT_PROPERTY:
1369
 
                                classContext = item->get_domain()->get_Id();
1370
 
                                classContextLabel = item->get_domain()->get_Label();
1371
 
                                break;
1372
 
                                
1373
 
                                case Visconte.Onto.Item.OWLItem.DATATYPE_PROPERTY:
1374
 
                                classContext = item->get_domain()->get_Id();
1375
 
                                classContextLabel = item->get_domain()->get_Label();
1376
 
                                break;
1377
 
                                
1378
 
                                case Visconte.Onto.Item.OWLItem.INDIVIDUAL:
1379
 
                                classContext = item->get_model_class()->get_Id();
1380
 
                                classContextLabel = item->get_model_class()->get_Label();
1381
 
                                break;
1382
 
                        }
1383
 
                        anotation_data+=(["classContext": Visconte.ID(classContext)->uref]);
1384
 
                        anotation_data+=(["classContextLabel": classContextLabel]);
1385
 
                }
1386
 
                else
1387
 
                {
1388
 
                        anotation_data+=(["about": "0"]);
1389
 
                        anotation_data+=(["aboutLabel": (string)item ]);
1390
 
                        anotation_data+=(["classContext": "0"]);
1391
 
                }
1392
 
                
1393
 
                anotation_data+=(["affectedLabel": (string) affected_label]);
1394
 
                
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,""));
1398
 
                
1399
 
    }    
1400
 
 
1401
 
/**
1402
 
 * <p>Returns the xml representation of the log file</p>
1403
 
 * 
1404
 
 * @return string xml 
1405
 
 */
1406
 
    public string get_annotations() 
1407
 
    {
1408
 
            return ontology_log->render_xml();
1409
 
    }
1410
 
 
1411
 
/**
1412
 
 * <p>Sets a class or individual as document term</p>
1413
 
 * 
1414
 
 * @param item (string or Visconte.Onto.Item.OWLItem)
1415
 
 * @return int success 
1416
 
 */
1417
 
    public int set_as_document_term(Visconte.Onto.Item.OWLItem item, int isDocumentTerm) 
1418
 
    { 
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))
1425
 
                {
1426
 
                        old_stmt->add(documentterm[0][0], documentterm[0][1], documentterm[0][2] );
1427
 
                        remove_statements(old_stmt);
1428
 
                }
1429
 
                if(isDocumentTerm)
1430
 
                        stmt->add(res,steam_is_document_term,owlset->LiteralResource("true"));
1431
 
                else
1432
 
                        stmt->add(res,steam_is_document_term,owlset->LiteralResource("false"));
1433
 
                add_statements(stmt);
1434
 
    }
1435
 
    
1436
 
/**
1437
 
 * <p>Checks if a class or individual is as document term</p>
1438
 
 * 
1439
 
 * @param item (string or Visconte.Onto.Item.OWLItem)
1440
 
 * @return int success 
1441
 
 */
1442
 
    public int is_document_term(Visconte.Onto.Item.OWLItem item) 
1443
 
    { 
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")
1448
 
                        return 1;
1449
 
                else
1450
 
                        return 0;
1451
 
    }
1452
 
 
1453
 
/**
1454
 
 * <p>Sets properties for an individual of a document term</p>
1455
 
 * 
1456
 
 * @param item (string or Visconte.Onto.Item.OWLItem)
1457
 
 * @return int success 
1458
 
 */
1459
 
    public int set_document_term_properties(Visconte.Onto.Item.OWLItem item, string objectID, string mimetype) 
1460
 
    { 
1461
 
                if(item && item->get_model_class()->is_document_term())
1462
 
                {
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);
1469
 
                }
1470
 
    }
1471
 
     
1472
 
/**
1473
 
 * <p>Sets properties for an individual of a document term</p>
1474
 
 * 
1475
 
 * @param item (string or Visconte.Onto.Item.OWLItem)
1476
 
 * @return int success 
1477
 
 */
1478
 
    public int reset_document_term_properties(Visconte.Onto.Item.OWLItem item) 
1479
 
    { 
1480
 
                if(item && item->get_model_class()->is_document_term())
1481
 
                {
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);
1492
 
                        
1493
 
                }
1494
 
    }
1495
 
 
1496
 
/**
1497
 
 * <p>Return the object id and the mimetype of an indidual</p>
1498
 
 * 
1499
 
 * 
1500
 
 * @param item (string or Visconte.Onto.Item.OWLItem)
1501
 
 * @return int success 
1502
 
 */
1503
 
    public mapping get_steam_properties(Visconte.Onto.Item.OWLItem item) 
1504
 
    { 
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 ]);
1510
 
                
1511
 
    }
1512
 
/**
1513
 
 * <p>Appends a author and change date to the ontology elements</p>
1514
 
 * 
1515
 
 * @param item (string or Visconte.Onto.Item.OWLItem)
1516
 
 * @return int success 
1517
 
 */
1518
 
    public int set_meta(Visconte.Onto.Item.OWLItem item) 
1519
 
    { 
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()));
1525
 
                remove_meta(item);
1526
 
                add_statements(stmt);
1527
 
    }
1528
 
 
1529
 
    
1530
 
/**
1531
 
 * <p>Check for the existence of an item in the OWLset</p>
1532
 
 * 
1533
 
 * 
1534
 
 * @param item (string or Visconte.Onto.Item.OWLItem)
1535
 
 * @return int success 
1536
 
 */
1537
 
    public int remove_meta(Visconte.Onto.Item.OWLItem item) 
1538
 
    { 
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] );
1548
 
                
1549
 
                remove_statements(old_stmt);
1550
 
    }
1551
 
    
1552
 
/**
1553
 
 * <p>Check for the existence of an item in the OWLset</p>
1554
 
 * 
1555
 
 * 
1556
 
 * @param item (string or Visconte.Onto.Item.OWLItem)
1557
 
 * @return int success 
1558
 
 */
1559
 
    public mapping get_meta(Visconte.Onto.Item.OWLItem item) 
1560
 
    { 
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 ]);
1566
 
                
1567
 
    }
1568
 
 
1569
 
   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
1570
 
    public void debug(string dbg)
1571
 
    {
1572
 
         // write("\nDebugging: "+dbg+"\n");
1573
 
    }
1574