@@ -49,7 +49,7 @@ kb = KnowledgeBase(path="file://KGs/father.owl")
49
49
What happens in the background is that the ontology located in this path will be loaded
50
50
in the ` OWLOntology ` object of ` kb ` as done [ here] ( 03_ontologies.md#loading-an-ontology ) .
51
51
52
- In our recent version you can also initialize the KnowledgeBase using a dataset hosted in a triplestore.
52
+ In our recent version you can also initialize a knowledge base using a dataset hosted in a triplestore.
53
53
Since that knowledge base is mainly used for executing a concept learner, we cover that matter more in depth
54
54
in _ [ Use Triplestore Knowledge Base] ( 06_concept_learners.md#use-triplestore-knowledge-base ) _
55
55
section of _ [ Concept Learning] ( 06_concept_learners.md ) _ .
@@ -253,11 +253,140 @@ You can now:
253
253
print (evaluated_concept.ic) # 3
254
254
```
255
255
256
+ # # Obtaining axioms
257
+
258
+ You can retrieve Tbox and Abox axioms by using `tbox` and `abox` methods respectively.
259
+ Let us take them one at a time. The `tbox` method has 2 parameters, `entities` and `mode` .
260
+ `entities` specifies the owl entity from which we want to obtain the Tbox axioms. It can be
261
+ a single entity, a `Iterable` of entities, or `None ` .
262
+
263
+ The allowed types of entities are:
264
+ - OWLClass
265
+ - OWLObjectProperty
266
+ - OWLDataProperty
267
+
268
+ Only the Tbox axioms related to the given entit- y/ ies will be returned. If no entities are
269
+ passed, then it returns all the Tbox axioms.
270
+ The second parameter `mode` _(str )_ sets the return format type . It can have the
271
+ following values:
272
+ 1 ) `' native' ` -> triples are represented as tuples of owlapy objects.
273
+ 2 ) `' iri' ` -> triples are represented as tuples of IRIs as strings.
274
+ 3 ) `' axiom' ` -> triples are represented as owlapy axioms.
275
+
276
+ For the `abox` method the idea is similar. Instead of the parameter `entities` , there is the parameter
277
+ `individuals` which accepts an object of type OWLNamedIndividuals or Iterable[OWLNamedIndividuals].
278
+
279
+ If you want to obtain all the axioms (Tbox + Abox) of the knowledge base, you can use the method `triples` . It
280
+ requires only the `mode` parameter.
281
+
282
+ > ** NOTE ** : The results of these methods are limited only to named and direct entities.
283
+ > That means that especially the axioms that contain anonymous owl objects (objects that don' t have an IRI)
284
+ > will not be part of the result set . For example, if there is a Tbox T = { C ⊑ (A ⊓ B), C ⊑ D },
285
+ > only the latter subsumption axiom will be returned.
286
+
287
+
288
+ # # Sampling the Knowledge Base
289
+
290
+ Sometimes ontologies and therefore knowledge bases can get very large and our
291
+ concept learners become inefficient in terms of runtime. Sampling is an approach
292
+ to extract a portion of the whole knowledge base without changing its semantic and
293
+ still being expressive enough to yield results with as little loss of quality as
294
+ possible. [OntoSample](https:// github.com/ alkidbaci/ OntoSample/ tree/ main) is
295
+ a library that we use to perform the sampling process. It offers different sampling
296
+ techniques which fall into the following categories:
297
+
298
+ - Node- based samplers
299
+ - Edge- based samplers
300
+ - Exploration- based samplers
301
+
302
+ and almost each sampler is offered in 3 modes:
303
+
304
+ - Classic
305
+ - Learning problem first (LPF )
306
+ - Learning problem centered (LPC )
307
+
308
+ You can check them [here](ontosample).
309
+
310
+ When operated on its own, Ontosample uses a light version of Ontolearn (`ontolearn_light` )
311
+ to reason over ontologies, but when both packages are installed in the same environment
312
+ it will use `ontolearn` module instead. This is made for compatibility reasons.
313
+
314
+ Ontosample treats the knowledge base as a graph where nodes are individuals
315
+ and edges are object properties. However, Ontosample also offers support for
316
+ data properties sampling, although they are not considered as _" edges" _.
317
+
318
+ # ### Sampling steps:
319
+ 1 . Initialize the sample using a `KnowledgeBase` object . If you are using an LPF or LPC
320
+ sampler than you also need to pass the set of learning problem individuals (`lp_nodes` ).
321
+ 2 . To perform the sampling use the `sample` method where you pass the number
322
+ of nodes (`nodes_number` ) that you want to sample, the amount of data properties in percentage
323
+ (`data_properties_percentage` ) that you want to sample which is represented by float values
324
+ form 0 to 1 and jump probability (`jump_prob` ) for samplers that
325
+ use " jumping" , a technique to avoid infinite loops during sampling.
326
+ 3 . The `sample` method returns the sampled knowledge which you can store to a
327
+ variable, use directly in the code or save locally by using the static method
328
+ `save_sample` .
329
+
330
+ Let' s see an example where we use [RandomNodeSampler](ontosample.classic_samplers.RandomNodeSampler) to sample a
331
+ knowledge base:
332
+
333
+ ```python
334
+ from ontosample.classic_samplers import RandomNodeSampler
335
+
336
+ # 1. Initialize KnowledgeBase object using the path of the ontology
337
+ kb = KnowledgeBase(path = " KGs/Family/family-benchmark_rich_background.owl" )
338
+
339
+ # 2. Initialize the sampler and generate the sample
340
+ sampler = RandomNodeSampler(kb)
341
+ sampled_kb = sampler.sample(30 ) # will generate a sample with 30 nodes
342
+
343
+ # 3. Save the sampled ontology
344
+ sampler.save_sample(kb = sampled_kb, filename = " some_name" )
345
+ ```
346
+
347
+ Here is another example where this time we use an LPC sampler:
348
+
349
+ ```python
350
+ from ontosample.lpc_samplers import RandomWalkerJumpsSamplerLPCentralized
351
+ from owlapy.model import OWLNamedIndividual,IRI
352
+ import json
353
+
354
+ # 0. Load json that stores the learning problem
355
+ with open (" examples/uncle_lp2.json" ) as json_file:
356
+ examples = json.load(json_file)
357
+
358
+ # 1. Initialize KnowledgeBase object using the path of the ontology
359
+ kb = KnowledgeBase(path = " KGs/Family/family-benchmark_rich_background.owl" )
360
+
361
+ # 2. Initialize learning problem (required only for LPF and LPC samplers)
362
+ pos = set (map (OWLNamedIndividual, map (IRI .create, set (examples[' positive_examples' ]))))
363
+ neg = set (map (OWLNamedIndividual, map (IRI .create, set (examples[' negative_examples' ]))))
364
+ lp = pos.union(neg)
365
+
366
+ # 3. Initialize the sampler and generate the sample
367
+ sampler = RandomWalkerJumpsSamplerLPCentralized(graph = kb, lp_nodes = lp)
368
+ sampled_kb = sampler.sample(nodes_number = 40 ,jump_prob = 0.15 )
369
+
370
+ # 4. Save the sampled ontology
371
+ sampler.save_sample(kb = sampled_kb, filename = " some_other_name" )
372
+ ```
373
+
374
+ > WARNING ! Random walker and Random Walker with Prioritization are two samplers that suffer
375
+ > from non- termination in case that the ontology contains nodes that point to each other and
376
+ > form an inescapable loop for the " walker" . In this scenario you can use their " jumping"
377
+ > version to make the " walker" escape these loops and ensure termination.
378
+
379
+ To see how to use a sampled knowledge base for the task of concept learning check
380
+ the `sampling_example.py` in [examples](https:// github.com/ dice- group/ Ontolearn/ tree/ develop/ examples)
381
+ folder. You will find descriptive comments in that script that will help you understand it better.
382
+
383
+ For more details about OntoSample you can see [this paper](https:// dl.acm.org/ doi/ 10.1145 / 3583780.3615158 ).
384
+
256
385
---------------------------------------------------------------------------------------------------- -
257
386
258
- See [KnowledgeBase API documentation](ontolearn.knowledge_base.KnowledgeBase)
259
- to check all the methods that this class has to offer. You will find methods to
260
- access the class / property hierarchy, convenient methods that use the reasoner indirectly and
387
+ Since we cannot cover everything here in details, see [KnowledgeBase API documentation](ontolearn.knowledge_base.KnowledgeBase)
388
+ to check all the methods that this class has to offer. You will find convenient methods to
389
+ access the class / property hierarchy, methods that use the reasoner indirectly and
261
390
a lot more.
262
391
263
392
Speaking of the reasoner, it is important that an ontology
0 commit comments