Skip to content

Commit

Permalink
ttlser ListRanker fix infinite loop case for cyclical lists
Browse files Browse the repository at this point in the history
Any cyclical linked list would go infinite while traversing rdf:rest,
this resolves the issue and provides a simplified and clear test case.
  • Loading branch information
tgbugs committed Apr 27, 2022
1 parent 1d771b8 commit db811ac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
41 changes: 12 additions & 29 deletions ttlser/test/evil.ttl
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
@prefix TEMP: <http://uri.interlex.org/temp/uris/> .
@prefix ilxtr: <http://uri.interlex.org/tgbugs/uris/readable/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

TEMP:271 a owl:Class ;
rdfs:label "electrical stimulation technique" ;
owl:equivalentClass [ a owl:Class ;
owl:intersectionOf [ rdf:first ilxtr:technique ;
rdf:rest [ rdf:first [ a owl:Restriction ;
owl:onProperty ilxtr:hasPrimaryAspect ;
owl:someValuesFrom <http://uri.interlex.org/tgbugs/uris/readable/aspect/electrical> ] ;
rdf:rest _:sg_0_4,
_:sg_0_6 ] ] ] .
ilxtr:b0 rdf:first ilxtr:x ;
rdf:rest
ilxtr:b1 .

_:sg_0_5 a owl:Restriction ;
owl:onProperty ilxtr:hasProbe,
ilxtr:hasSomething ;
owl:someValuesFrom TEMP:272,
ilxtr:electricalField .
ilxtr:b1 rdf:first ilxtr:y ;
rdf:rest
ilxtr:b2 .

_:sg_0_7 a owl:Restriction ;
owl:onProperty ilxtr:hasProbe,
ilxtr:hasSomething ;
owl:someValuesFrom TEMP:272,
ilxtr:electricalField .
ilxtr:b2 rdf:first ilxtr:z ;
rdf:rest
ilxtr:b3 .

_:sg_0_4 rdf:first _:sg_0_5 ;
rdf:rest _:sg_0_6,
() .

_:sg_0_6 rdf:first _:sg_0_7 ;
rdf:rest _:sg_0_4,
() .
ilxtr:b3 rdf:first ilxtr:e ;
rdf:rest
ilxtr:b1 .
17 changes: 16 additions & 1 deletion ttlser/ttlser/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,25 @@ def __init__(self, node, serializer):
self.vals = []
self.nodes = [] # list helper nodes
l = self.node
while l:
previous = set()
#count = 0
#enough = 99999
while l: # infinite loop posibility
item = self.serializer.store.value(l, RDF.first)
self.add(item, l)
# if there is a self referential node anywhere in a list
# beyond the first element this goes infinite
previous.add(l)
l = self.serializer.store.value(l, RDF.rest)
if l in previous: # cyclical reference case
break
# XXX beware lists where there are multiple rdf:rest
# values for a node, I don't think they will break
# serialization, but they may produce unexpected ranking
#count += 1
#if count > enough:
#print(self.serializer.store)
#breakpoint()
self.vis_vals = [v for v in self.vals if not isinstance(v, BNode)]
self.bvals = [v for v in self.vals if isinstance(v, BNode)]

Expand Down

0 comments on commit db811ac

Please sign in to comment.