Skip to content

Commit

Permalink
Optimize Thing.subclasses()
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Baptiste "Jiba" Lamy authored and Jean-Baptiste "Jiba" Lamy committed Jun 25, 2019
1 parent 016e353 commit 523d959
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ version 2 - 0.19
* INDIRECT_is_a / INDIRECT_is_instance_of now include class contructs. ancestors() has a 'include_constructs' parameter, which defaults to False.
* Add more aliases for XMLSchema datatypes
* Add is_a property to class constructs
* Optimize Thing.subclasses()
* Bugfixes:
- After reasoning, keep all equivalent classes as parents of individuals (as they may have methods)
- Fix RDF serialization for nested RDF lists
Expand Down
37 changes: 24 additions & 13 deletions entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,20 +322,31 @@ def subclasses(Class, only_loaded = False, world = None):
if world is None:
import owlready2
world = owlready2.default_world
r = []
for x in world._get_obj_triples_po_s(rdf_type, owl_class):
if x < 0: continue
for y in world._get_obj_triples_sp_o(x, Class._rdfs_is_a):
if (y == owl_thing) or y < 0: continue
break

for x, in world.graph.db.execute(
"""SELECT q1.s FROM objs q1 WHERE q1.s > 0 and q1.p = ? AND q1.o = ?
EXCEPT SELECT q2.s FROM objs q2 WHERE q2.p = ? and q2.p != ?""",
(rdf_type, owl_class, rdfs_subclassof, owl_thing)):
if only_loaded:
subclass = world._entities.get(x)
if not subclass is None: yield subclass
else:
if only_loaded:
subclass = world._entities.get(x)
if not subclass is None: yield subclass
else:
yield world._get_by_storid(x, None, ThingClass)
return r

yield world._get_by_storid(x, None, ThingClass)

#r = []
#for x in world._get_obj_triples_po_s(rdf_type, owl_class):
# if x < 0: continue
# for y in world._get_obj_triples_sp_o(x, Class._rdfs_is_a):
# if (y == owl_thing) or y < 0: continue
# break
# else:
# if only_loaded:
# subclass = world._entities.get(x)
# if not subclass is None: yield subclass
# else:
# yield world._get_by_storid(x, None, ThingClass)
#return r

else:
world = world or Class.namespace.world
if only_loaded:
Expand Down

0 comments on commit 523d959

Please sign in to comment.