@@ -484,6 +484,7 @@ def print_quotation(self, tree):
484484annotation_s_p_o = []
485485annotation_dict = dict ()
486486to_remove = []
487+ trig_graph = []
487488output = ""
488489
489490def 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
671680def 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
26202744class 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 )
0 commit comments