forked from pwin/owlready2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prop.py
1151 lines (904 loc) · 49.3 KB
/
prop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# Owlready2
# Copyright (C) 2013-2019 Jean-Baptiste LAMY
# LIMICS (Laboratoire d'informatique médicale et d'ingénierie des connaissances en santé), UMR_S 1142
# University Paris 13, Sorbonne paris-Cité, Bobigny, France
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import weakref
from owlready2.namespace import *
from owlready2.entity import *
from owlready2.base import _universal_abbrev_2_datatype, _universal_datatype_2_abbrev
_NEXT_DOMAIN_RANGE = ContextVar("_NEXT_DOMAIN_RANGE", default = None)
SymmetricProperty = () # Forward declaration
#_check_superclasses = False
_default_class_property_type = ["some"]
def set_default_class_property_type(types):
global _default_class_property_type
_default_class_property_type = types
class PropertyClass(EntityClass):
_rdfs_is_a = rdfs_subpropertyof
_owl_equivalent = owl_equivalentproperty
_owl_disjointwith = owl_propdisjointwith
# def __new__(MetaClass, name, superclasses, obj_dict):
# if _check_superclasses:
# nb_base = 0
# if ObjectProperty in superclasses: nb_base += 1
# if DataProperty in superclasses: nb_base += 1
# if AnnotationProperty in superclasses: nb_base += 1
# if nb_base > 1:
# iri = "%s%s" % (obj_dict["namespace"].base_iri, name)
# if (ObjectProperty in superclasses) and (DataProperty in superclasses):
# raise TypeError("Property '%s' is both an ObjectProperty and a DataProperty!" % iri)
# if (ObjectProperty in superclasses) and (AnnotationProperty in superclasses):
# raise TypeError("Property '%s' is both an ObjectProperty and an AnnotationProperty!" % iri)
# if (AnnotationProperty in superclasses) and (DataProperty in superclasses):
# raise TypeError("Property '%s' is both an AnnotationProperty and a DataProperty!" % iri)
# return EntityClass.__new__(MetaClass, name, superclasses, obj_dict)
def __init__(Prop, name, bases, obj_dict):
next_domain_range = _NEXT_DOMAIN_RANGE.get()
if next_domain_range:
domain = [next_domain_range[0]]
range = [next_domain_range[1]]
_NEXT_DOMAIN_RANGE.set(None)
else:
domain = obj_dict.pop("domain", False)
range = obj_dict.pop("range", False)
inverse_property = obj_dict.pop("inverse_property", None) or obj_dict.pop("inverse", False)
python_name = obj_dict.pop("python_name", None)
class_property_type = obj_dict.pop("class_property_type", None)
super().__init__(name, bases, obj_dict)
Prop.namespace.world._props[name] = Prop
type.__setattr__(Prop, "_domain", None)
type.__setattr__(Prop, "_range", None)
type.__setattr__(Prop, "_property_chain", None)
type.__setattr__(Prop, "_inverse_property", False)
type.__setattr__(Prop, "_python_name", name)
_class_property_type = CallbackList(
(o for o, d in Prop.namespace.world._get_data_triples_sp_od(Prop.storid, owlready_class_property_type)),
Prop, PropertyClass._class_property_type_changed)
type.__setattr__(Prop, "_class_property_type", _class_property_type)
types = _class_property_type or _default_class_property_type
type.__setattr__(Prop, "_class_property_some", "some" in types)
type.__setattr__(Prop, "_class_property_only", "only" in types)
type.__setattr__(Prop, "_class_property_relation", "relation" in types)
if not LOADING:
if not domain is False:
Prop.domain.extend(domain)
if not range is False:
Prop.range.extend(range)
if not inverse_property is False:
Prop.inverse_property = inverse_property
if not python_name is None:
Prop.python_name = python_name
if not class_property_type is None:
Prop.class_property_type = class_property_type
def _check_update(Prop, onto):
if onto._has_obj_triple_spo(Prop.storid):
Prop._range = None
Prop._domain = None
type.__setattr__(Prop, "_property_chain", None)
Prop._python_name = (Prop.namespace.world._get_data_triple_sp_od(Prop.storid, owlready_python_name) or (Prop.name, ""))[0]
_class_property_type = CallbackList(
(o for o, d in Prop.namespace.world._get_data_triples_sp_od(Prop.storid, owlready_class_property_type)),
Prop, PropertyClass._class_property_type_changed)
type.__setattr__(Prop, "_class_property_type", _class_property_type)
if issubclass(Prop, ObjectProperty):
Prop._define_inverse_property()
return True
if issubclass(Prop, ObjectProperty) and onto._has_obj_triple_spo(None, owl_inverse_property, Prop.storid):
Prop._define_inverse_property()
return True
def _add_is_a_triple(Prop, base):
if base in _CLASS_PROPS: pass
elif base in _TYPE_PROPS: Prop.namespace.ontology._add_obj_triple_spo(Prop.storid, rdf_type , base.storid)
else: Prop.namespace.ontology._add_obj_triple_spo(Prop.storid, Prop._rdfs_is_a, base.storid)
def _del_is_a_triple(Prop, base):
if base in _CLASS_PROPS: pass
elif base in _TYPE_PROPS: Prop.namespace.ontology._del_obj_triple_spo(Prop.storid, rdf_type , base.storid)
else: Prop.namespace.ontology._del_obj_triple_spo(Prop.storid, Prop._rdfs_is_a, base.storid)
def get_domain(Prop):
if Prop._domain is None:
Prop._domain = CallbackList((Prop.namespace.world._to_python(o, default_to_none = True) for o in Prop.namespace.world._get_obj_triples_sp_o(Prop.storid, rdf_domain)),
Prop, PropertyClass._domain_changed)
return Prop._domain
def set_domain(Prop, value): Prop.domain.reinit(value)
domain = property(get_domain, set_domain)
def _domain_changed(Prop, old):
new = frozenset(Prop.domain)
old = frozenset(old)
for x in old - new:
Prop.namespace.ontology._del_obj_triple_spo(Prop.storid, rdf_domain, x.storid)
if isinstance(x, Construct): x._set_ontology(None)
for x in new - old:
if isinstance(x, Construct): x = x._set_ontology_copy_if_needed(Prop.namespace.ontology, Prop.domain)
Prop.namespace.ontology._add_obj_triple_spo(Prop.storid, rdf_domain, x.storid)
def domains_indirect(Prop):
yield from Prop.domain
for parent_prop in Prop.__bases__:
if isinstance(parent_prop, PropertyClass): yield from parent_prop.domains_indirect()
def get_range(Prop):
if Prop._range is None:
Prop._range = CallbackList(
(_universal_abbrev_2_datatype.get(o) or Prop.namespace.world._to_python(o, default_to_none = True)
for o in Prop.namespace.world._get_obj_triples_sp_o(Prop.storid, rdf_range)),
Prop, PropertyClass._range_changed)
return Prop._range
def set_range(Prop, value): Prop.range.reinit(value)
range = property(get_range, set_range)
def _range_changed(Prop, old):
new = frozenset(Prop.range)
old = frozenset(old)
for x in old - new:
x2 = _universal_datatype_2_abbrev.get(x) or x.storid
Prop.namespace.ontology._del_obj_triple_spo(Prop.storid, rdf_range, x2)
if isinstance(x, Construct): x._set_ontology(None)
for x in new - old:
if isinstance(x, Construct): x = x._set_ontology_copy_if_needed(Prop.namespace.ontology, Prop.range)
x2 = _universal_datatype_2_abbrev.get(x) or x.storid
Prop.namespace.ontology._add_obj_triple_spo(Prop.storid, rdf_range, x2)
if "_range_iri" in Prop.__dict__: del Prop._range_iri
def get_range_iri(Prop):
if not "_range_iri" in Prop.__dict__:
iris = []
for o in Prop.namespace.world._get_obj_triples_sp_o(Prop.storid, rdf_range):
if o > 0: iris.append(Prop.namespace.world._unabbreviate(o))
else: iris.append("_:%s" % (-o))
type.__setattr__(Prop, "_range_iri", CallbackList(iris, Prop, PropertyClass._range_iri_changed))
return Prop._range_iri
def set_range_iri(Prop, value): Prop.range_iri.reinit(value)
range_iri = property(get_range_iri, set_range_iri)
def _range_iri_changed(Prop, old):
new = frozenset(Prop.range_iri)
old = frozenset(old)
for x in old - new:
if x.startswith("_"): x2 = -int(x[2:])
else: x2 = Prop.namespace.world._abbreviate(x)
Prop.namespace.ontology._del_obj_triple_spo(Prop.storid, rdf_range, x2)
for x in new - old:
if x.startswith("_"): x2 = -int(x[2:])
else: x2 = Prop.namespace.world._abbreviate(x)
Prop.namespace.ontology._add_obj_triple_spo(Prop.storid, rdf_range, x2)
if Prop._range: Prop._range = None
def get_class_property_type(Prop): return Prop._class_property_type
def set_class_property_type(Prop, value): Prop.class_property_type.reinit(value)
class_property_type = property(get_class_property_type, set_class_property_type)
def _class_property_type_changed(Prop, old):
types = Prop._class_property_type or _default_class_property_type
type.__setattr__(Prop, "_class_property_some", "some" in types)
type.__setattr__(Prop, "_class_property_only", "only" in types)
type.__setattr__(Prop, "_class_property_relation", "relation" in types)
Prop.namespace.ontology._del_data_triple_spod(Prop.storid, owlready_class_property_type, None, None)
for x in Prop._class_property_type:
Prop.namespace.ontology._add_data_triple_spod(Prop.storid, owlready_class_property_type, x, 0)
def get_property_chain(Prop):
if Prop._property_chain is None:
Prop._property_chain = CallbackList(
(PropertyChain(o, Prop.namespace.ontology)
for o in Prop.namespace.world._get_obj_triples_sp_o(Prop.storid, owl_propertychain)),
Prop, PropertyClass._property_chain_changed)
return Prop._property_chain
def set_property_chain(Prop, value): Prop.property_chain.reinit(value)
property_chain = property(get_property_chain, set_property_chain)
def _property_chain_changed(Prop, old):
new = frozenset(Prop._property_chain)
old = frozenset(old)
for x in old - new:
Prop.namespace.ontology._del_obj_triple_spo(Prop.storid, owl_propertychain, x.storid)
x._set_ontology(None)
for x in new - old:
x = x._set_ontology_copy_if_needed(Prop.namespace.ontology, Prop._property_chain)
Prop.namespace.ontology._add_obj_triple_spo(Prop.storid, owl_propertychain, x.storid)
def __getattr__(Prop, attr):
if Prop.namespace.world is owl_world: Annot = CURRENT_NAMESPACES.get()[-1].world._props.get(attr)
else: Annot = Prop.namespace.world._props.get(attr)
#l = CURRENT_NAMESPACES.get()
#Annot = ((l and l[-1]) or Prop.namespace).world._props.get(attr)
if Annot is None:
raise AttributeError("'%s' annotation property is not defined." % attr)
if not issubclass_python(Annot, AnnotationProperty):
raise AttributeError("Property can only have annotation property values!")
return IndividualValueList(
(Prop.namespace.ontology._to_python(o,d) for o,d in Prop.namespace.world._get_triples_sp_od(Prop.storid, Annot.storid)),
Prop, Annot) # Do NOT cache in __dict__, to avoid inheriting annotations
def __setattr__(Class, attr, value):
if attr in SPECIAL_PROP_ATTRS:
super().__setattr__(attr, value)
return
Prop = Class.namespace.world._props.get(attr)
if isinstance(Prop, ReasoningPropertyClass):
raise AttributeError("Property cannot have non-annotation properties!")
if value is None: value = []
elif not isinstance(value, list): value = [value]
getattr(Class, attr).reinit(value)
def get_python_name(Prop):
return Prop._python_name
def set_python_name(Prop, python_name):
if not LOADING: Prop.namespace.ontology._set_data_triple_spod(Prop.storid, owlready_python_name, *to_literal(python_name))
Prop.namespace.world._props.pop(Prop._python_name, None)
Prop.namespace.world._props[python_name] = Prop
Prop._python_name = python_name
python_name = property(get_python_name, set_python_name)
def some (Prop, value): return Restriction(Prop, SOME , None, value)
def only (Prop, value): return Restriction(Prop, ONLY , None, value)
def value (Prop, value): return Restriction(Prop, VALUE , None, value)
def has_self(Prop, value = True): return Restriction(Prop, HAS_SELF, None, value)
def exactly (Prop, nb, value = None): return Restriction(Prop, EXACTLY, nb , value)
def min (Prop, nb, value = None): return Restriction(Prop, MIN , nb , value)
def max (Prop, nb, value = None): return Restriction(Prop, MAX , nb , value)
def __lt__(prop, value): return prop.some(ConstrainedDatatype(type(value), max_exclusive = value))
def __le__(prop, value): return prop.some(ConstrainedDatatype(type(value), max_inclusive = value))
def __gt__(prop, value): return prop.some(ConstrainedDatatype(type(value), min_exclusive = value))
def __ge__(prop, value): return prop.some(ConstrainedDatatype(type(value), min_inclusive = value))
def _get_value_for_individual(Prop, entity):
value = entity.namespace.world._get_triple_sp_od(entity.storid, Prop.storid)
if not value is None: return entity.namespace.ontology._to_python(*value)
def _get_values_for_individual(Prop, entity):
return IndividualValueList((entity.namespace.ontology._to_python(o, d) for o, d in entity.namespace.world._get_triples_sp_od(entity.storid, Prop.storid)),
entity, Prop)
_get_value_for_class = _get_value_for_individual
_get_values_for_class = _get_values_for_individual
def _get_indirect_value_for_individual(Prop, entity):
values = Prop._get_indirect_values_for_individual(entity)
if not values: return None
elif len(values) == 1: return values[0]
return _most_specific(values)
def _get_indirect_values_for_individual(Prop, entity):
world = entity.namespace.world
onto = entity.namespace.ontology
if not isinstance(entity, EntityClass):
eqs = list(entity.equivalent_to.self_and_indirect_equivalent())
values = { onto._to_python(o, d)
for P in Prop.descendants()
for eq in eqs
for o, d in world._get_triples_sp_od(eq.storid, P.storid) }
for eq in eqs:
values.extend(Prop._get_indirect_values_for_individual(eq.__class__))
else:
storids = [ancestor.storid for ancestor in entity.ancestors()]
values = { onto._to_python(o, d)
for P in Prop.descendants()
for storid in storids
for o, d in world._get_triples_sp_od(storid, P.storid) }
return list(values)
def _get_indirect_value_for_class(Prop, entity):
values = Prop._get_indirect_values_for_class(entity)
if not values: return None
elif len(values) == 1: return values[0]
return _most_specific(values)
_get_indirect_values_for_class = _get_indirect_values_for_individual
def _set_value_for_individual(Prop, entity, value):
if value is None: entity.namespace.ontology._del_triple_spod(entity.storid, Prop.storid, None, None)
else: entity.namespace.ontology._set_triple_spod(entity.storid, Prop.storid, *entity.namespace.ontology._to_rdf(value))
if (not instance(entity, EntityClass)) and (Prop is entity.namespace.world._props.get(Prop._python_name)):
entity.__dict__[Prop.python_name] = [value]
_set_value_for_class = _set_value_for_individual
def _set_values_for_individual(Prop, entity, values): Prop[entity].reinit(values)
_set_values_for_class = _set_values_for_individual
def __getitem__(Prop, entity):
if isinstance(entity, EntityClass):
return Prop._get_values_for_class(entity)
else:
if Prop is entity.namespace.world._props.get(Prop._python_name): # use cached value
if Prop.is_functional_for(entity.__class__): return FunctionalIndividualValueList([getattr(entity, Prop._python_name)], entity, Prop)
else: return getattr(entity, Prop._python_name)
else:
l = Prop._get_values_for_individual(entity)
if Prop.is_functional_for(entity.__class__): l.__class__ = FunctionalIndividualValueList
return l
def __setitem__(Prop, entity, value):
if isinstance(entity, EntityClass):
Prop._set_values_for_class(entity, value)
else:
Prop._set_values_for_individual(entity, value)
_FUNCTIONAL_FOR_CACHE = weakref.WeakKeyDictionary()
RESTRICTIONS_AS_FUNCTIONAL_PROPERTIES = False
class Property(metaclass = PropertyClass):
namespace = rdf
_inverse_storid = 0
@classmethod
def is_functional_for(Prop, Class, force_restriction = False):
if not(RESTRICTIONS_AS_FUNCTIONAL_PROPERTIES or force_restriction): return False
cache = _FUNCTIONAL_FOR_CACHE.get(Class)
if cache is None:
cache = _FUNCTIONAL_FOR_CACHE[Class] = {}
else:
r = cache.get(Prop)
if not r is None: return r
ranges = set(Prop.range)
singles = set()
for restriction in _inherited_properties_value_restrictions(Class, {Prop}, set()):
if restriction.type == ONLY:
ranges.add(restriction.value)
elif ((restriction.type == EXACTLY) or (restriction.type == MAX)) and (restriction.cardinality == 1):
if restriction.value is Thing:
cache[Prop] = True
return True
singles.add(restriction.value)
cache[Prop] = r = not ranges.isdisjoint(singles)
return r
@classmethod
def get_relations(Prop):
for s,p,o,d in Prop.namespace.world._get_triples_spod_spod(None, Prop.storid, None, ""):
s = Prop.namespace.world._get_by_storid(s)
o = Prop.namespace.ontology._to_python(o, d)
yield s, o
if Prop._inverse_storid:
for s,p,o,d in Prop.namespace.world._get_triples_spod_spod(None, Prop._inverse_storid, None, ""):
s = Prop.namespace.world._get_by_storid(s)
o = Prop.namespace.ontology._to_python(o, d)
yield o, s
class ReasoningPropertyClass(PropertyClass):
def __init__(Prop, name, bases, obj_dict):
super().__init__(name, bases, obj_dict)
if (not Prop.namespace.world is owl_world):
Prop.namespace.world._reasoning_props[Prop._python_name] = Prop
def set_python_name(Prop, python_name):
Prop.namespace.world._reasoning_props.pop(Prop._python_name, None)
Prop.namespace.world._reasoning_props[python_name] = Prop
PropertyClass.set_python_name(Prop, python_name)
python_name = property(PropertyClass.get_python_name, set_python_name)
class ObjectPropertyClass(ReasoningPropertyClass):
_owl_type = owl_object_property
def __init__(Prop, name, bases, obj_dict):
super().__init__(name, bases, obj_dict)
if SymmetricProperty in Prop.is_a:
type.__setattr__(Prop, "_inverse_storid", Prop.storid)
Prop._inverse_property = Prop
else:
Prop._define_inverse_property()
def _define_inverse_property(Prop):
for inverse_storid in Prop.namespace.world._get_obj_triples_sp_o(Prop.storid, owl_inverse_property):
if inverse_storid > 0: break
else:
for inverse_storid in Prop.namespace.world._get_obj_triples_po_s(owl_inverse_property, Prop.storid):
if inverse_storid > 0: break
else: inverse_storid = 0
type.__setattr__(Prop, "_inverse_storid", inverse_storid or 0)
if inverse_storid: type.__setattr__(Prop, "_inverse_property", Prop.namespace.world._get_by_storid(inverse_storid))
else: type.__setattr__(Prop, "_inverse_property", None)
def get_inverse_property(Prop):
return Prop._inverse_property
def set_inverse_property(Prop, value):
if value:
Prop.namespace.ontology._set_obj_triple_spo(Prop.storid, owl_inverse_property, value and value.storid)
type.__setattr__(Prop, "_inverse_property", value)
type.__setattr__(Prop, "_inverse_storid", value.storid)
if not value._inverse_property is Prop: value.inverse_property = Prop
else:
inverse = Prop._inverse_property
type.__setattr__(Prop, "_inverse_property", value)
Prop.namespace.ontology._del_obj_triple_spo(Prop.storid, owl_inverse_property, None)
type.__setattr__(Prop, "_inverse_storid", 0)
if inverse._inverse_property: inverse.inverse_property = None
inverse_property = inverse = property(get_inverse_property, set_inverse_property)
def _class_is_a_changed(Prop, old):
super()._class_is_a_changed(old)
if (SymmetricProperty in old) and (not SymmetricProperty in Prop.is_a):
if Prop._inverse_property: type.__setattr__(Prop, "_inverse_storid", Prop._inverse_property.storid)
else: type.__setattr__(Prop, "_inverse_storid", 0)
elif (SymmetricProperty in Prop.is_a) and (not SymmetricProperty in old):
type.__setattr__(Prop, "_inverse_storid", Prop.storid)
def _get_value_for_individual(Prop, entity):
value = (entity.namespace.world._get_obj_triple_sp_o(entity.storid, Prop.storid)
or (Prop._inverse_storid and
entity.namespace.world._get_obj_triple_po_s(Prop._inverse_storid, entity.storid)) )
if value:
return entity.namespace.ontology._to_python(value)
def _get_inverse_value_for_individual(Prop, entity):
value = (entity.namespace.world._get_obj_triple_po_s(Prop.storid, entity.storid)
or (Prop._inverse_storid and
entity.namespace.world._get_obj_triple_sp_o(entity.storid, Prop._inverse_storid)) )
if value:
return entity.namespace.ontology._to_python(value)
def _get_value_for_class(Prop, entity):
if Prop._class_property_relation: return Prop._get_value_for_individual(entity)
elif Prop._class_property_some:
for r in _property_value_restrictions(entity, Prop):
if (r.type == VALUE) or (r.type == SOME) or ((r.type == EXACTLY) and r.cardinality >= 1) or ((r.type == MIN) and r.cardinality >= 1): return r.value
elif Prop._class_property_only:
for r in _property_value_restrictions(Class, Prop):
if (r.type == ONLY):
for value in _flatten_only(r): return value
def _get_values_for_individual(Prop, entity):
if Prop._inverse_storid:
return IndividualValueList((entity.namespace.ontology._to_python(o)
for o in entity.namespace.world._get_obj_triples_spi_o(entity.storid, Prop.storid, Prop._inverse_storid)),
entity, Prop)
else:
return IndividualValueList((entity.namespace.ontology._to_python(o)
for o in entity.namespace.world._get_obj_triples_sp_o(entity.storid, Prop.storid)),
entity, Prop)
def _get_inverse_values_for_individual(Prop, entity):
if Prop._inverse_storid:
return InverseIndividualValueList((entity.namespace.ontology._to_python(s)
for s in entity.namespace.world._get_obj_triples_pio_s(Prop.storid, Prop._inverse_storid, entity.storid)),
entity, Prop)
else:
return InverseIndividualValueList((entity.namespace.ontology._to_python(s)
for s in entity.namespace.world._get_obj_triples_po_s(Prop.storid, entity.storid)),
entity, Prop)
def _get_values_for_class(Prop, entity):
if Prop._class_property_relation: return Prop._get_values_for_individual(entity)
elif Prop._class_property_some:
return ClassValueList(set(r.value for r in _property_value_restrictions(entity, Prop)
if (r.type == VALUE) or (r.type == SOME) or ((r.type == EXACTLY) and r.cardinality >= 1) or ((r.type == MIN) and r.cardinality >= 1)),
entity, Prop)
elif Prop._class_property_only:
return ClassValueList(set(x for r in _property_value_restrictions(entity, Prop)
if (r.type == ONLY)
for x in _flatten_only(r) ), entity, Prop)
def _get_indirect_values_for_individual(Prop, entity):
world = entity.namespace.world
onto = entity.namespace.ontology
Props = Prop.descendants()
eqs = list(entity.equivalent_to.self_and_indirect_equivalent())
already_applied_class = set()
prop_storids = []
values = set()
if issubclass_python(Prop, ReflexiveProperty): values.add(entity)
for P in Props:
if issubclass(P, TransitiveProperty):
if P._inverse_storid: prop_storids.append((P.storid, P._inverse_storid))
else: prop_storids.append((P.storid, None))
else:
if P._inverse_storid:
values.update(onto._to_python(o)
for eq in eqs
for g in (world._get_obj_triples_sp_o(eq.storid, P.storid), world._get_obj_triples_po_s(P._inverse_storid, eq.storid))
for o in g )
else:
values.update(onto._to_python(o)
for eq in eqs
for o in world._get_obj_triples_sp_o(eq.storid, P.storid) )
if prop_storids:
for eq in eqs:
new_values = [onto._to_python(o) for o in world._get_obj_triples_transitive_sp_indirect(eq.storid, prop_storids)]
for o in new_values:
values.add(o)
if not o.__class__ in already_applied_class:
values.update(Prop._get_indirect_values_for_class(o.__class__, True))
already_applied_class.add(o.__class__)
for o2 in o.equivalent_to.indirect():
if not ((o2 in new_values) or (o2 in values)):
values.add(o2)
if not o2.__class__ in already_applied_class:
values.update(Prop._get_indirect_values_for_class(o2.__class__, True))
already_applied_class.add(o2.__class__)
for eq in eqs:
if not eq.__class__ in already_applied_class:
values.update(Prop._get_indirect_values_for_class(eq.__class__, True))
already_applied_class.add(eq.__class__)
return list(values)
# def _get_indirect_inverse_values_for_individual(Prop, entity):
# world = entity.namespace.world
# onto = entity.namespace.ontology
# Props = Prop.descendants()
# eqs = list(entity.equivalent_to.self_and_indirect_equivalent())
# already_applied_class = set()
# prop_storids = []
# values = set()
# for P in Props:
# if issubclass(P, TransitiveProperty):
# if P._inverse_storid: prop_storids.append((P.storid, P._inverse_storid))
# else: prop_storids.append((P.storid, None))
# else:
# if P._inverse_storid:
# values.update(onto._to_python(o)
# for eq in eqs
# for g in (world._get_obj_triples_sp_o(eq.storid, P.storid), world._get_obj_triples_po_s(P._inverse_storid, eq.storid))
# for o in g )
# else:
# values.update(onto._to_python(o)
# for eq in eqs
# for o in world._get_obj_triples_sp_o(eq.storid, P.storid) )
# if prop_storids:
# for eq in eqs:
# new_values = [onto._to_python(s) for s in world._get_obj_triples_transitive_po_indirect(eq.storid, prop_storids)]
# for o in new_values:
# values.add(o)
# if not o.__class__ in already_applied_class:
# values.update(Prop._get_indirect_values_for_class(o.__class__, True))
# already_applied_class.add(o.__class__)
# for o2 in o.equivalent_to.indirect():
# if not ((o2 in new_values) or (o2 in values)):
# values.add(o2)
# if not o2.__class__ in already_applied_class:
# values.update(Prop._get_indirect_values_for_class(o2.__class__, True))
# already_applied_class.add(o2.__class__)
# for eq in eqs:
# if not eq.__class__ in already_applied_class:
# values.update(Prop._get_indirect_values_for_class(eq.__class__, True))
# already_applied_class.add(eq.__class__)
# return list(values)
def _get_indirect_values_for_class(Prop, entity, transitive_exclude_self = True):
world = entity.namespace.world
onto = entity.namespace.ontology
Props = Prop.descendants()
if Prop._class_property_relation:
storids = [ancestor.storid for ancestor in entity.ancestors()]
prop_storids = []
values = set()
for P in Props:
if issubclass_python(P, TransitiveProperty):
if P._inverse_storid: prop_storids.append((P.storid, P._inverse_storid))
else: prop_storids.append((P.storid, None))
else:
if P._inverse_storid:
values.update(onto._to_python(o) for storid in storids
for g in (world._get_obj_triples_sp_o(storid, P.storid),
world._get_obj_triples_po_s(P._inverse_storid, storid))
for o in g )
else:
values.update(onto._to_python(o) for storid in storids
for o in world._get_obj_triples_sp_o(storid, P.storid) )
if prop_storids:
values.update(onto._to_python(o) for storid in storids
for o in world._get_obj_triples_transitive_sp_indirect(storid, prop_storids))
if transitive_exclude_self: values.discard(entity)
elif Prop._class_property_some:
if issubclass_python(Prop, TransitiveProperty):
values = set()
def walk(o):
values.add(o)
for r in _inherited_properties_value_restrictions(o, Props, set()):
if r.type == VALUE:
if not r.value in values:
for o2 in r.value.equivalent_to.self_and_indirect_equivalent():
if not o2 in values:
values.add(o2)
values.update(Prop._get_indirect_values_for_individual(o2))
elif (r.type == SOME) or ((r.type == EXACTLY) and r.cardinality >= 1) or ((r.type == MIN) and r.cardinality >= 1):
if not r.value in values: walk(r.value)
if isinstance(o, ThingClass):
for e in o.equivalent_to.indirect():
if not e in values: walk(e)
walk(entity)
if transitive_exclude_self: values.discard(entity)
else:
values = set(r.value for r in _inherited_properties_value_restrictions(entity, Props, set())
if (r.type == VALUE) or (r.type == SOME) or ((r.type == EXACTLY) and r.cardinality >= 1) or ((r.type == MIN) and r.cardinality >= 1) )
elif Prop._class_property_only: # Effect of transitivity on ONLY restrictions is unclear -- probably no effect?
or_valuess = [set(_flatten_only(r)) for r in _inherited_properties_value_restrictions(entity, Props, set())
if (r.type == ONLY)]
values = or_valuess[0]
for or_values in or_valuess[1:]:
new_values = values & or_values
for vs1, vs2 in ((values, or_values), (or_values, values)):
vs2_classes = tuple(o for o in vs2 if isinstance(o, EntityClass))
for v in vs1 - vs2:
if isinstance(v, EntityClass):
if issubclass(v, vs2_classes): new_values.add(v)
else:
if isinstance(v, vs2_classes): new_values.add(v)
values = new_values
return list(values)
def _set_value_for_individual(Prop, entity, value):
if value is None: entity.namespace.ontology._del_obj_triple_spo(entity.storid, Prop.storid, None)
else: entity.namespace.ontology._set_obj_triple_spo(entity.storid, Prop.storid, value.storid)
if (not instance(entity, EntityClass)) and (Prop is entity.namespace.world._props.get(Prop._python_name)):
entity.__dict__[Prop.python_name] = value
def _set_value_for_class (Prop, entity, value ): Prop._get_values_for_class(entity).reinit([value])
class ObjectProperty(Property, metaclass = ObjectPropertyClass):
namespace = owl
class DataPropertyClass(ReasoningPropertyClass):
_owl_type = owl_data_property
inverse_property = None
def _get_value_for_individual(Prop, entity):
value = entity.namespace.world._get_data_triple_sp_od(entity.storid, Prop.storid)
if not value is None: return entity.namespace.ontology._to_python(*value)
def _get_value_for_class(Prop, entity):
if Prop._class_property_relation: Prop._get_value_for_individual(entity)
elif Prop._class_property_some:
for r in _property_value_restrictions(entity, Prop):
if (r.type == VALUE) or (r.type == SOME) or ((r.type == EXACTLY) and r.cardinality >= 1) or ((r.type == MIN) and r.cardinality >= 1):
return r.value
elif Prop._class_property_only:
for r in _property_value_restrictions(Class, Prop):
if (r.type == ONLY):
for value in _flatten_only(r): return value
def _get_values_for_individual(Prop, entity):
return IndividualValueList((entity.namespace.ontology._to_python(o, d)
for o, d in entity.namespace.world._get_data_triples_sp_od(entity.storid, Prop.storid)),
entity, Prop)
def _get_values_for_class(Prop, entity):
if Prop._class_property_relation:
return Prop._get_values_for_individual(entity)
elif Prop._class_property_some:
return ClassValueList(set(r.value for r in _property_value_restrictions(entity, Prop)
if (r.type == VALUE) or (r.type == SOME) or ((r.type == EXACTLY) and r.cardinality >= 1) or ((r.type == MIN) and r.cardinality >= 1) ),
entity, Prop)
elif Prop._class_property_only:
return ClassValueList(set(x for r in _property_value_restrictions(entity, Prop)
if (r.type == ONLY)
for x in _flatten_only(r) ),
entity, Prop)
def _get_indirect_value_for_individual(Prop, entity):
values = Prop._get_indirect_values_for_individual(entity)
if len(values) == 0: return None
elif len(values) == 1: return values[0]
# XXX datatype
return _most_specific(values)
def _get_indirect_value_for_class(Prop, entity):
values = Prop._get_indirect_values_for_class(entity)
if len(values) == 0: return None
elif len(values) == 1: return values[0]
# XXX datatype
return _most_specific(values)
def _get_indirect_values_for_individual(Prop, entity):
eqs = list(entity.equivalent_to.self_and_indirect_equivalent())
values = [entity.namespace.ontology._to_python(o, d)
for P in Prop.descendants()
for eq in eqs
for o, d in entity.namespace.world._get_data_triples_sp_od(eq.storid, P.storid)]
values.extend(Prop._get_indirect_values_for_class(entity.__class__))
return values
def _get_indirect_values_for_class(Prop, entity):
Props = Prop.descendants()
if Prop._class_property_relation:
storids = [ancestor.storid for ancestor in entity.ancestors()]
return [ entity.namespace.ontology._to_python(o, d)
for storid in storids
for P in Props
for o, d in entity.namespace.world._get_data_triples_sp_od(storid, P.storid) ]
elif Prop._class_property_some:
return list(set(r.value for r in _inherited_properties_value_restrictions(entity, Props, set())
if (r.type == VALUE) or (r.type == SOME) or ((r.type == EXACTLY) and r.cardinality >= 1) or ((r.type == MIN) and r.cardinality >= 1) ))
elif Prop._class_property_only:
return list(set(x for r in _inherited_properties_value_restrictions(entity, Props, set())
if (r.type == ONLY)
for x in _flatten_only(r) ))
def _set_value_for_individual(Prop, entity, value):
if value is None: entity.namespace.ontology._del_data_triple_spod(entity.storid, Prop.storid, None, None)
else: entity.namespace.ontology._set_data_triple_spod(entity.storid, Prop.storid, *entity.namespace.ontology._to_rdf(value))
if (not instance(entity, EntityClass)) and (Prop is entity.namespace.world._props.get(Prop._python_name)):
entity.__dict__[Prop.python_name] = value
def _set_value_for_class (Prop, entity, value ): Prop._get_values_for_class(entity).reinit([value])
class DatatypeProperty(Property, metaclass = DataPropertyClass):
namespace = owl
DataProperty = DatatypeProperty
class FunctionalProperty(Property):
namespace = owl
@classmethod
def is_functional_for(Prop, o): return True
class InverseFunctionalProperty(Property): namespace = owl
class TransitiveProperty (Property): namespace = owl
class SymmetricProperty (Property): namespace = owl
class AsymmetricProperty (Property): namespace = owl
class ReflexiveProperty (Property): namespace = owl
class IrreflexiveProperty (Property): namespace = owl
_CLASS_PROPS = { DataProperty, ObjectProperty }
_TYPE_PROPS = { FunctionalProperty, InverseFunctionalProperty, TransitiveProperty, SymmetricProperty, AsymmetricProperty, ReflexiveProperty, IrreflexiveProperty }
def destroy_entity(e, undoable = False):
if undoable: undoer_objs = []; undoer_datas = []; undoer_bnodes = []; undoer_relations = []
else: undoer_objs = undoer_datas = None; undoer_bnodes = None; undoer_relations = None
if hasattr(e, "__destroy__"): e.__destroy__(undoer_objs, undoer_datas)
elif isinstance(e, PropertyClass):
modified_entities = set()
if e._owl_type == owl_object_property:
for s,p,o in e.namespace.world._get_obj_triples_spo_spo(None, e.storid, None):
modified_entities.add(s)
e.namespace.world._del_obj_triple_spo(None, e.storid, None)
# XXX inverse ?
elif e._owl_type == owl_data_property:
for s,p,o,d in e.namespace.world._get_data_triples_spod_spod(None, e.storid, None, None):
modified_entities.add(s)
e.namespace.world._del_data_triple_spod(None, e.storid, None, None)
else: #e._owl_type == owl_annotation_property:
for s,p,o,d in e.namespace.world._get_triples_spod_spod(None, e.storid, None, None):
modified_entities.add(s)
e.namespace.world._del_obj_triple_spo (None, e.storid, None)
e.namespace.world._del_data_triple_spod(None, e.storid, None, None)
for s in modified_entities:
s = e.namespace.world._entities.get(s)
if s:
delattr(s, e._python_name)
e.namespace.world._props .pop(e._python_name, None)
e.namespace.world._reasoning_props.pop(e._python_name, None)
def destroyer(bnode):
if bnode == e.storid: return
if undoer_bnodes: undoer_bnodes.append(bnode)
class_construct = e.namespace.ontology._bnodes.pop(bnode, None)
if class_construct:
for subclass in class_construct.subclasses(True):
if isinstance(subclass, EntityClass) or isinstance(subclass, Thing):
subclass.is_a.remove(class_construct)
def relation_updater(destroyed_storids, storid, relations):
if undoer_relations is not None: undoer_relations.append((destroyed_storids, storid, relations))
update_relation(destroyed_storids, storid, relations)
def update_relation(destroyed_storids, storid, relations):
o = e.namespace.world._entities.get(storid)
if o:
for r in relations:
if (r == rdf_type) or (r == rdfs_subpropertyof) or (r == rdfs_subclassof):
#o.is_a.reinit([i for i in o.is_a if not i.storid in destroyed_storids])
parents = [e.namespace.world._to_python(i) for i in e.namespace.world._get_obj_triples_sp_o(storid, r)]
o.is_a.reinit([i for i in parents if not i is None])
if r == rdfs_subclassof:
for Subclass in o.descendants(True, True): _FUNCTIONAL_FOR_CACHE.pop(Subclass, None)
elif (r == owl_equivalentproperty) or (r == owl_equivalentindividual):
if o._equivalent_to._indirect:
for o2 in o.equivalent_to._indirect: o2._equivalent_to._indirect = None
o._equivalent_to._indirect = None
elif r == owl_equivalentclass:
if o._equivalent_to._indirect:
for o2 in o.equivalent_to._indirect: o2._equivalent_to._indirect = None
o._equivalent_to._indirect = None
for Subclass in o.descendants(True, True): _FUNCTIONAL_FOR_CACHE.pop(Subclass, None)
elif r == rdf_domain:
o._domain = None
elif r == rdf_range:
o._range = None
else:
r = e.namespace.world._entities.get(r)
if r:
try: del o.__dict__[r.python_name]
except: pass
e.namespace.world.graph.destroy_entity(e.storid, destroyer, relation_updater, undoer_objs, undoer_datas)
e.namespace.world._entities.pop(e.storid, None)
e.namespace.ontology._entity_destroyed(e)
if undoable:
def undestroy():
e.namespace.world.graph.restore_iri(e.storid, e.iri)
c_2_onto = e.namespace.world.graph.c_2_onto
for c,s,p,o in undoer_objs:
c_2_onto[c]._add_obj_triple_spo(s,p,o)
for c,s,p,o,d in undoer_datas:
c_2_onto[c]._add_data_triple_spo(s,p,o,d)
#e.namespace.world.graph.db.executemany("INSERT INTO objs VALUES (?,?,?,?)", undoer_objs)
#e.namespace.world.graph.db.executemany("INSERT INTO datas VALUES (?,?,?,?,?)", undoer_datas)
e.namespace.world._entities[e.storid] = e
for bnode in undoer_bnodes:
class_construct = e.namespace.world._parse_bnode(bnode)
for subclass in class_construct.subclasses(True):
if isinstance(subclass, EntityClass) or isinstance(subclass, Thing):
subclass.is_a._append(class_construct)
for destroyed_storids, storid, relations in undoer_relations:
update_relation(destroyed_storids, storid, relations)
return undestroy
class bottomObjectProperty(ObjectProperty): pass
class bottomDataProperty(DataProperty): pass
def _property_value_restrictions(x, Prop):
for parents in (x.is_a, x.equivalent_to.indirect()):
for r in parents:
if isinstance(r, Restriction):
if (Prop is None) or (r.property is Prop): yield r
elif isinstance(r, And):
for r2 in r.Classes:
if isinstance(r2, Restriction):
if (Prop is None) or (r2.property is Prop): yield r2
def _inherited_properties_value_restrictions(x, Props, already):
if isinstance(x, Restriction):
if (Props is None) or (x.property in Props): yield x
elif isinstance(x, And):
for x2 in x.Classes:
yield from _inherited_properties_value_restrictions(x2, Props, already)
elif isinstance(x, EntityClass) or isinstance(x, Thing):