Skip to content

Commit 775c3d5

Browse files
committed
trigstar fix
1 parent 468b2bc commit 775c3d5

File tree

4 files changed

+242
-33
lines changed

4 files changed

+242
-33
lines changed

rdflib/plugins/parsers/trigstar.py

Lines changed: 150 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ def print_quotation(self, tree):
484484
annotation_s_p_o = []
485485
annotation_dict = dict()
486486
to_remove = []
487+
trig_graph = []
487488
output = ""
488489

489490
def myHash(text:str):
@@ -558,6 +559,14 @@ def __init__(self):
558559
super().__init__()
559560
self.variable_list = []
560561

562+
def labelorsubject(self, var):
563+
564+
try:
565+
vr = Reconstructor(trig_lark).reconstruct(var)
566+
trig_graph.append(vr)
567+
except:
568+
pass
569+
561570
def quotation(self, var):
562571
qut = Reconstructor(trig_lark).reconstruct(var)
563572
qut = qut.replace(";", "")
@@ -669,7 +678,7 @@ def base(self, children):
669678
raise ValueError('Unexpected @base: ' + base_directive)
670679

671680
def RDFstarParsings(rdfstarstring):
672-
global quotationannolist, vblist, quotation_dict, quotationreif, prefix_list, constructors, assertedtriplelist, quoted_or_not, both_quoted_and_asserted, to_remove, annotation_s_p_o, output, annotation_dict
681+
global quotationannolist, vblist, quotation_dict, quotationreif, prefix_list, constructors, assertedtriplelist, quoted_or_not, both_quoted_and_asserted, to_remove, annotation_s_p_o, output, annotation_dict, trig_graph
673682
quotationannolist = []
674683
vblist = []
675684
quotationreif = []
@@ -682,6 +691,8 @@ def RDFstarParsings(rdfstarstring):
682691
annotation_s_p_o = []
683692
to_remove = []
684693
annotation_dict = dict()
694+
trig_graph = []
695+
685696
tree = trig_lark.parse(rdfstarstring)
686697

687698
tt = Expandanotation().visit(tree)
@@ -831,6 +842,8 @@ def expand_to_rdfstar(x):
831842
next_rdf_object = "_:" + str(value) + '\n' + " a rdfstar:AssertedStatement ;\n"+" rdf:subject "+subject+' ;\n'+" rdf:predicate "+predicate+" ;\n"+" rdf:object "+object+" ;\n"+".\n"
832843

833844
constructors+=next_rdf_object
845+
if len(trig_graph)!=0:
846+
constructors=trig_graph[0]+"{\n"+constructors+"\n}"
834847
for x in range(0, len(prefix_list)):
835848
prefix_list[x] = Reconstructor(trig_lark).reconstruct(prefix_list[x])
836849
constructors = prefix_list[x]+"\n"+constructors
@@ -2616,6 +2629,117 @@ def hexify(ustr):
26162629
s = s + ch
26172630
return s.encode("latin-1")
26182631

2632+
class TrigSinkParser(SinkParser):
2633+
def directiveOrStatement(self, argstr, h):
2634+
2635+
# import pdb; pdb.set_trace()
2636+
2637+
i = self.skipSpace(argstr, h)
2638+
if i < 0:
2639+
return i # EOF
2640+
2641+
j = self.graph(argstr, i)
2642+
if j >= 0:
2643+
return j
2644+
2645+
j = self.sparqlDirective(argstr, i)
2646+
if j >= 0:
2647+
return j
2648+
2649+
j = self.directive(argstr, i)
2650+
if j >= 0:
2651+
return self.checkDot(argstr, j)
2652+
2653+
j = self.statement(argstr, i)
2654+
if j >= 0:
2655+
return self.checkDot(argstr, j)
2656+
2657+
return j
2658+
2659+
def labelOrSubject(self, argstr, i, res):
2660+
j = self.skipSpace(argstr, i)
2661+
if j < 0:
2662+
return j # eof
2663+
i = j
2664+
2665+
j = self.uri_ref2(argstr, i, res)
2666+
if j >= 0:
2667+
return j
2668+
2669+
if argstr[i] == "[":
2670+
j = self.skipSpace(argstr, i + 1)
2671+
if j < 0:
2672+
self.BadSyntax(argstr, i, "Expected ] got EOF")
2673+
if argstr[j] == "]":
2674+
res.append(self.blankNode())
2675+
return j + 1
2676+
return -1
2677+
2678+
def graph(self, argstr, i):
2679+
"""
2680+
Parse trig graph, i.e.
2681+
2682+
<urn:graphname> = { .. triples .. }
2683+
2684+
return -1 if it doesn't look like a graph-decl
2685+
raise Exception if it looks like a graph, but isn't.
2686+
"""
2687+
2688+
# import pdb; pdb.set_trace()
2689+
j = self.sparqlTok("GRAPH", argstr, i) # optional GRAPH keyword
2690+
if j >= 0:
2691+
i = j
2692+
2693+
r = []
2694+
j = self.labelOrSubject(argstr, i, r)
2695+
if j >= 0:
2696+
graph = r[0]
2697+
i = j
2698+
else:
2699+
graph = self._store.graph.identifier # hack
2700+
2701+
j = self.skipSpace(argstr, i)
2702+
if j < 0:
2703+
self.BadSyntax(argstr, i, "EOF found when expected graph")
2704+
2705+
if argstr[j : j + 1] == "=": # optional = for legacy support
2706+
2707+
i = self.skipSpace(argstr, j + 1)
2708+
if i < 0:
2709+
self.BadSyntax(argstr, i, "EOF found when expecting '{'")
2710+
else:
2711+
i = j
2712+
2713+
if argstr[i : i + 1] != "{":
2714+
return -1 # the node wasn't part of a graph
2715+
2716+
j = i + 1
2717+
2718+
oldParentContext = self._parentContext
2719+
self._parentContext = self._context
2720+
reason2 = self._reason2
2721+
self._reason2 = becauseSubGraph
2722+
self._context = self._store.newGraph(graph)
2723+
print(self._context)
2724+
while 1:
2725+
i = self.skipSpace(argstr, j)
2726+
if i < 0:
2727+
self.BadSyntax(argstr, i, "needed '}', found end.")
2728+
2729+
if argstr[i : i + 1] == "}":
2730+
j = i + 1
2731+
break
2732+
2733+
j = self.directiveOrStatement(argstr, i)
2734+
if j < 0:
2735+
self.BadSyntax(argstr, i, "expected statement or '}'")
2736+
2737+
self._context = self._parentContext
2738+
self._reason2 = reason2
2739+
self._parentContext = oldParentContext
2740+
# res.append(subj.close()) # No use until closed
2741+
return j
2742+
26192743

26202744
class TrigParser(Parser):
26212745

@@ -2635,21 +2759,40 @@ def parse(
26352759
turtle: bool = True,
26362760
):
26372761
if encoding not in [None, "utf-8"]:
2638-
raise ParserError(
2639-
"N3/Turtle files are always utf-8 encoded, I was passed: %s" % encoding
2762+
raise Exception(
2763+
("TriG files are always utf-8 encoded, ", "I was passed: %s") % encoding
26402764
)
26412765

2642-
sink = RDFSink(graph)
2766+
# we're currently being handed a Graph, not a ConjunctiveGraph
2767+
print("Contextawareasdasdasdasd\n\n\n\n", graph.store.context_aware)
2768+
assert graph.store.context_aware, "TriG Parser needs a context-aware store!"
2769+
2770+
conj_graph = ConjunctiveGraph(store=graph.store, identifier=graph.identifier)
2771+
conj_graph.default_context = graph # TODO: CG __init__ should have a
2772+
# default_context arg
2773+
# TODO: update N3Processor so that it can use conj_graph as the sink
2774+
conj_graph.namespace_manager = graph.namespace_manager
2775+
2776+
sink = RDFSink(conj_graph)
26432777

2644-
baseURI = graph.absolutize(source.getPublicId() or source.getSystemId() or "")
2645-
p = SinkParser(sink, baseURI=baseURI, turtle=turtle)
2778+
baseURI = conj_graph.absolutize(
2779+
source.getPublicId() or source.getSystemId() or ""
2780+
)
2781+
p = TrigSinkParser(sink, baseURI=baseURI, turtle=True)
2782+
2783+
# return ???
2784+
# sink = RDFSink(graph)
2785+
2786+
# baseURI = graph.absolutize(source.getPublicId() or source.getSystemId() or "")
2787+
# p = SinkParser(sink, baseURI=baseURI, turtle=turtle)
26462788
# N3 parser prefers str stream
26472789
# stream = source.getCharacterStream()
26482790
# if not stream:
26492791
# stream = source.getByteStream()
26502792
# p.loadStream(stream)
26512793

26522794
# print("tests", source)
2795+
26532796
if hasattr(source, "file"):
26542797
f = open(source.file.name, "rb")
26552798
rdbytes = f.read()
@@ -2666,4 +2809,4 @@ def parse(
26662809
p.feed(ou)
26672810
p.endDoc()
26682811
for prefix, namespace in p._bindings.items():
2669-
graph.bind(prefix, namespace)
2812+
conj_graph.bind(prefix, namespace)

rdflib/plugins/serializers/trigstar.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
See <https://github.com/ontola/hextuples> for details about the format.
44
"""
55
# from this import d
6-
from typing import IO, Optional, Type, Union
6+
from typing import IO, Optional, Type, Union, TYPE_CHECKING
77
import json
88
from rdflib.graph import Graph, ConjunctiveGraph
99
from rdflib.term import Literal, URIRef, Node, BNode, RdfstarTriple
@@ -27,18 +27,18 @@ class TrigstarSerializer(Serializer):
2727

2828
def __init__(self, store: Union[Graph, ConjunctiveGraph]):
2929
self.default_context: Optional[Node]
30-
self.graph_type: Type[Graph]
31-
if isinstance(store, ConjunctiveGraph):
32-
self.graph_type = ConjunctiveGraph
30+
# print("init", list(store.contexts()))
31+
if store.context_aware:
32+
if TYPE_CHECKING:
33+
assert isinstance(store, ConjunctiveGraph)
3334
self.contexts = list(store.contexts())
35+
# print("sadasd", [store])
36+
self.default_context = store.default_context.identifier
3437
if store.default_context:
35-
self.default_context = store.default_context
3638
self.contexts.append(store.default_context)
37-
else:
38-
self.default_context = None
3939
else:
40-
self.graph_type = Graph
4140
self.contexts = [store]
41+
# print("asdasdas", store.default_context.identifier)
4242
self.default_context = None
4343

4444
Serializer.__init__(self, store)
@@ -527,11 +527,9 @@ def expand_Bnode_and_RdfstarTriple(node, g, dictionary, properties, collection_o
527527
if(isinstance(predicate, rdflib.term.URIRef)):
528528
predicate = "<"+str(predicate)+">"
529529

530-
output = output+subject+" "+predicate+" "+object+" ."+"\n"
531-
532-
if output is not None:
533-
output = "_:"+str(myHash(output))+ "{\n"+ output + "}"
534-
stream.write(output.encode())
530+
output = subject+" "+predicate+" "+object+" <"+str(g.identifier)+"> "" ."+"\n"
531+
if output is not None:
532+
stream.write(output.encode())
535533

536534
def _iri_or_bn(self, i_):
537535
if isinstance(i_, URIRef):

test_serializer_trigstar.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from rdflib.exceptions import ParserError
88

9-
from rdflib import Graph
9+
from rdflib import Graph, ConjunctiveGraph
1010
from rdflib.util import guess_format
1111

1212

@@ -19,50 +19,50 @@
1919
from rdflib.namespace import RDF
2020
from rdflib.namespace import FOAF
2121

22-
g = Graph()
22+
g = ConjunctiveGraph()
2323
g.parse(data="test/trig-star/trig-star-syntax-basic-01.trig", format = "trigs")
2424
print(g.serialize(format = "trigstar"))
2525

26-
g = Graph()
26+
g = ConjunctiveGraph()
2727
g.parse(data="test/trig-star/trig-star-syntax-basic-02.trig", format = "trigs")
2828
print(g.serialize(format = "trigstar"))
2929

30-
g = Graph()
30+
g = ConjunctiveGraph()
3131
g.parse(data="test/trig-star/trig-star-syntax-bnode-01.trig", format = "trigs")
3232
print(g.serialize(format = "trigstar"))
3333

34-
g = Graph()
34+
g = ConjunctiveGraph()
3535
g.parse(data="test/trig-star/trig-star-syntax-bnode-02.trig", format = "trigs")
3636
print(g.serialize(format = "trigstar"))
3737

38-
g = Graph()
38+
g = ConjunctiveGraph()
3939
g.parse(data="test/trig-star/trig-star-syntax-bnode-03.trig", format = "trigs")
4040
print(g.serialize(format = "trigstar"))
4141

42-
g = Graph()
42+
g = ConjunctiveGraph()
4343
g.parse(data="test/trig-star/trig-star-syntax-compound.trig", format = "trigs")
4444
print(g.serialize(format = "trigstar"))
4545

46-
g = Graph()
46+
g = ConjunctiveGraph()
4747
g.parse(data="test/trig-star/trig-star-syntax-inside-01.trig", format = "trigs")
4848
print(g.serialize(format = "trigstar"))
4949

50-
g = Graph()
50+
g = ConjunctiveGraph()
5151
g.parse(data="test/trig-star/trig-star-syntax-inside-02.trig", format = "trigs")
5252
print(g.serialize(format = "trigstar"))
5353

54-
g = Graph()
54+
g = ConjunctiveGraph()
5555
g.parse(data="test/trig-star/trig-star-syntax-nested-01.trig", format = "trigs")
5656
print(g.serialize(format = "trigstar"))
5757

58-
g = Graph()
58+
g = ConjunctiveGraph()
5959
g.parse(data="test/trig-star/trig-star-syntax-nested-02.trig", format = "trigs")
6060
print(g.serialize(format = "trigstar"))
6161

62-
g = Graph()
62+
g = ConjunctiveGraph()
6363
g.parse(data="test/trig-star/trig-star-annotation-1.trig", format = "trigs")
6464
print(g.serialize(format = "trigstar"))
6565

66-
g = Graph()
66+
g = ConjunctiveGraph()
6767
g.parse(data="test/trig-star/trig-star-annotation-2.trig", format = "trigs")
6868
print(g.serialize(format = "trigstar"))

0 commit comments

Comments
 (0)