Skip to content

Commit 4f06cd0

Browse files
Added possibility of specifying iri in new_entity (#844)
# Description closes #843 ## Type of change <!-- Put an `x` in the box that applies. --> - [ ] Bug fix. - [x] New feature. - [ ] Documentation update. - [ ] Test update. ## Checklist <!-- Put an `x` in the boxes that apply. You can also fill these out after creating the PR. --> This checklist can be used as a help for the reviewer. - [ ] Is the code easy to read and understand? - [ ] Are comments for humans to read, not computers to disregard? - [ ] Does a new feature has an accompanying new test (in the CI or unit testing schemes)? - [ ] Has the documentation been updated as necessary? - [ ] Does this close the issue? - [ ] Is the change limited to the issue? - [ ] Are errors handled for all outcomes? - [ ] Does the new feature provide new restrictions on dependencies, and if so is this documented? ## Comments <!-- Additional comments here, including clarifications on checklist if applicable. --> --------- Co-authored-by: Jesper Friis <[email protected]>
1 parent 8b81fd2 commit 4f06cd0

File tree

2 files changed

+106
-14
lines changed

2 files changed

+106
-14
lines changed

ontopy/ontology.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,7 +2025,7 @@ def get_wu_palmer_measure(self, cls1, cls2):
20252025
generations2 = self.number_of_generations(cls2, cca)
20262026
return 2 * ccadepth / (generations1 + generations2 + 2 * ccadepth)
20272027

2028-
def new_entity(
2028+
def new_entity( # pylint: disable=too-many-arguments,too-many-branches,too-many-positional-arguments
20292029
self,
20302030
name: str,
20312031
parent: Union[
@@ -2045,6 +2045,7 @@ def new_entity(
20452045
]
20462046
] = "class",
20472047
preflabel: Optional[str] = None,
2048+
iri: Optional[str] = None,
20482049
) -> Union[
20492050
ThingClass,
20502051
ObjectPropertyClass,
@@ -2068,6 +2069,8 @@ def new_entity(
20682069
be added as prefLabel if skos:prefLabel is in the ontology
20692070
and listed in `self.label_annotations`. Set `preflabel` to
20702071
False, to avoid assigning a prefLabel.
2072+
iri: IRI of the entity. If None, a new IRI will be generated
2073+
based on the ontology base IRI and the entity name.
20712074
20722075
Returns:
20732076
the new entity.
@@ -2113,7 +2116,8 @@ def new_entity(
21132116

21142117
with self:
21152118
entity = types.new_class(name, parents)
2116-
2119+
if iri:
2120+
entity.iri = iri
21172121
preflabel_iri = "http://www.w3.org/2004/02/skos/core#prefLabel"
21182122
if preflabel:
21192123
if not self.world[preflabel_iri]:
@@ -2134,63 +2138,81 @@ def new_entity(
21342138

21352139
# Method that creates new ThingClass using new_entity
21362140
def new_class(
2137-
self, name: str, parent: Union[ThingClass, Iterable]
2141+
self,
2142+
name: str,
2143+
parent: Union[ThingClass, Iterable],
2144+
iri: Optional[str] = None,
21382145
) -> ThingClass:
21392146
"""Create and return new class.
21402147
21412148
Args:
21422149
name: name of the class
21432150
parent: parent(s) of the class
2151+
iri: IRI of the new class. If None, a new IRI will be generated
2152+
based on the ontology base IRI and the entity name.
2153+
21442154
21452155
Returns:
21462156
the new class.
21472157
"""
2148-
return self.new_entity(name, parent, "class")
2158+
return self.new_entity(name, parent, "class", iri=iri)
21492159

21502160
# Method that creates new ObjectPropertyClass using new_entity
21512161
def new_object_property(
2152-
self, name: str, parent: Union[ObjectPropertyClass, Iterable]
2162+
self,
2163+
name: str,
2164+
parent: Union[ObjectPropertyClass, Iterable],
2165+
iri: Optional[str] = None,
21532166
) -> ObjectPropertyClass:
21542167
"""Create and return new object property.
21552168
21562169
Args:
21572170
name: name of the object property
21582171
parent: parent(s) of the object property
2159-
2172+
iri: IRI of the new object property. If None, a new IRI will be
2173+
based on the ontology base IRI and the entity name.
21602174
Returns:
21612175
the new object property.
21622176
"""
2163-
return self.new_entity(name, parent, "object_property")
2177+
return self.new_entity(name, parent, "object_property", iri=iri)
21642178

21652179
# Method that creates new DataPropertyClass using new_entity
21662180
def new_data_property(
2167-
self, name: str, parent: Union[DataPropertyClass, Iterable]
2181+
self,
2182+
name: str,
2183+
parent: Union[DataPropertyClass, Iterable],
2184+
iri: Optional[str] = None,
21682185
) -> DataPropertyClass:
21692186
"""Create and return new data property.
21702187
21712188
Args:
21722189
name: name of the data property
21732190
parent: parent(s) of the data property
2174-
2191+
iri: IRI of the new data property. If None, a new IRI will be
2192+
based on the ontology base IRI and the entity name.
21752193
Returns:
21762194
the new data property.
21772195
"""
2178-
return self.new_entity(name, parent, "data_property")
2196+
return self.new_entity(name, parent, "data_property", iri=iri)
21792197

21802198
# Method that creates new AnnotationPropertyClass using new_entity
21812199
def new_annotation_property(
2182-
self, name: str, parent: Union[AnnotationPropertyClass, Iterable]
2200+
self,
2201+
name: str,
2202+
parent: Union[AnnotationPropertyClass, Iterable],
2203+
iri: Optional[str] = None,
21832204
) -> AnnotationPropertyClass:
21842205
"""Create and return new annotation property.
21852206
21862207
Args:
21872208
name: name of the annotation property
21882209
parent: parent(s) of the annotation property
2189-
2210+
iri: IRI of the new annotation property. If None, a new IRI will
2211+
be based on the ontology base IRI and the entity name.
21902212
Returns:
21912213
the new annotation property.
21922214
"""
2193-
return self.new_entity(name, parent, "annotation_property")
2215+
return self.new_entity(name, parent, "annotation_property", iri=iri)
21942216

21952217
def difference(self, other: owlready2.Ontology) -> set:
21962218
"""Return a set of triples that are in this, but not in the

tests/ontopy_tests/test_new_entity.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,30 @@ def test_new_entity(testonto: "Ontology") -> None:
101101
entitytype="nonexistingpropertytype",
102102
)
103103
testonto.new_class("AnotherClass3", (testonto.AnotherClass,))
104+
105+
assert (
106+
testonto.AnotherClass3.iri == "http://emmo.info/testonto#AnotherClass3"
107+
)
104108
testonto.new_object_property(
105109
"hasSubObjectProperty3", testonto.hasObjectProperty
106110
)
111+
112+
assert (
113+
testonto.hasSubObjectProperty3.iri
114+
== "http://emmo.info/testonto#hasSubObjectProperty3"
115+
)
107116
testonto.new_data_property("hasSubDataProperty3", testonto.hasDataProperty)
117+
assert (
118+
testonto.hasSubDataProperty3.iri
119+
== "http://emmo.info/testonto#hasSubDataProperty3"
120+
)
108121
testonto.new_annotation_property(
109122
"hasSubAnnotationProperty3", testonto.hasAnnotationProperty
110123
)
124+
assert (
125+
testonto.hasSubAnnotationProperty3.iri
126+
== "http://emmo.info/testonto#hasSubAnnotationProperty3"
127+
)
111128

112129

113130
def test_new_entity_w_preflabel() -> None:
@@ -122,4 +139,57 @@ def test_new_entity_w_preflabel() -> None:
122139
preflabel="NewClass",
123140
)
124141

125-
# assert testonto2.NewClass.preflabel == "NewClass"
142+
assert testonto2.NewClass.prefLabel.en == ["NewClass"]
143+
144+
145+
def test_new_entity_w_iri(testonto: "Ontology") -> None:
146+
"""Test adding entities to ontology"""
147+
from ontopy import get_ontology
148+
import owlready2
149+
150+
testonto.new_entity(
151+
"NewClass", owlready2.Thing, iri="http://different_ontology#NewClass"
152+
)
153+
154+
assert testonto.NewClass.iri == "http://different_ontology#NewClass"
155+
156+
testonto.new_class(
157+
"AnotherClass",
158+
(testonto.NewClass,),
159+
iri="http://different_ontology#AnotherClass",
160+
)
161+
162+
assert testonto.AnotherClass.iri == "http://different_ontology#AnotherClass"
163+
164+
testonto.new_object_property(
165+
"hasSubObjectProperty",
166+
testonto.hasObjectProperty,
167+
iri="http://different_ontology#hasSubObjectProperty",
168+
)
169+
170+
assert (
171+
testonto.hasSubObjectProperty.iri
172+
== "http://different_ontology#hasSubObjectProperty"
173+
)
174+
175+
testonto.new_data_property(
176+
"hasSubDataProperty",
177+
testonto.hasDataProperty,
178+
iri="http://different_ontology#hasSubDataProperty",
179+
)
180+
181+
assert (
182+
testonto.hasSubDataProperty.iri
183+
== "http://different_ontology#hasSubDataProperty"
184+
)
185+
186+
testonto.new_annotation_property(
187+
"hasSubAnnotationProperty",
188+
testonto.hasAnnotationProperty,
189+
iri="http://different_ontology#hasSubAnnotationProperty",
190+
)
191+
192+
assert (
193+
testonto.hasSubAnnotationProperty.iri
194+
== "http://different_ontology#hasSubAnnotationProperty"
195+
)

0 commit comments

Comments
 (0)