You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jena.apache.org by hartig <gi...@git.apache.org> on 2018/10/01 10:43:06 UTC

[GitHub] jena pull request #473: Extendable versions of classes for printing result s...

GitHub user hartig opened a pull request:

    https://github.com/apache/jena/pull/473

    Extendable versions of classes for printing result sets

    This PR modifies the classes `CSVOutput` and `TSVOutput` to make it possible to extend them and override their behavior in subclasses. I want to implement such subclasses for my SPARQL* extension of Jena.
    
    A related question: Is there a reason why the mapping from `ResultsFormat` objects to `ResultSetLang`object (i.e., the [`mapResultsFormatToLang` in `ResultsFormat.java`](https://github.com/apache/jena/blob/aef35ee50eef9e0820d23d5ba184906b177a5c15/jena-arq/src/main/java/org/apache/jena/sparql/resultset/ResultsFormat.java#L190)) does not map `ResultsFormat.FMT_TEXT` to `ResultSetLang.SPARQLResultSetText`?
    Without this, the command line program `arq.query` falls back to the old `ResultSetWriter` and there is no elegant way for my SPARQL* extension of this program to use my SPARQL*-aware version of `ResultSetWriter` for text output. Hence, it would be great to add the following line into code that sets up `mapResultsFormatToLang`.
    
    ```
    mapResultsFormatToLang.put(ResultsFormat.FMT_TEXT, ResultSetLang.SPARQLResultSetText) ;
    ```
    
    Alternatively, the could be a method that can be used for adding something to `mapResultsFormatToLang`.
    
    If you are fine with any of these two options, I would like to add it to this PR.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/hartig/jena master

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/jena/pull/473.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #473
    
----
commit ce453fe9a930ce060c662360dfb4e17a8c6e03fc
Author: Olaf Hartig <ol...@...>
Date:   2018-09-30T09:32:35Z

    Merge pull request #1 from apache/master
    
    update this fork to reflect the latest version of the original

commit 3ea4a230610fea61ff288dc15e5ce8f4ae36924e
Author: Olaf Hartig <ol...@...>
Date:   2018-09-30T18:53:08Z

    changed the access modifier of both 'output' and 'csvSafe' to be protected

commit a8550192ed39c899687764fd9beda410a2560542
Author: Olaf Hartig <ol...@...>
Date:   2018-09-30T18:59:09Z

    added method createNodeFormatter() which can be overridden in subclasses

commit ea7b430b825636595e92aebe7366d27b8ddcf47c
Author: Olaf Hartig <ol...@...>
Date:   2018-09-30T19:00:09Z

    removed irrelevant comment

----


---

[GitHub] jena issue #473: Extendable versions of classes for printing result sets

Posted by hartig <gi...@git.apache.org>.
Github user hartig commented on the issue:

    https://github.com/apache/jena/pull/473
  
    > Where and how are you wanting to output text format?
    
    Where? In the command line tool for executing SPARQL* queries (see [`ExecuteSPARQLStar` in RDFstarTools](https://github.com/RDFstar/RDFstarTools/blob/master/src/main/java/se/liu/ida/rdfstar/tools/ExecuteSPARQLStar.java)).
    
    How? As it is done by the `TextOutput` class in Jena, plus added support for the case in which the `Node` for a variable is of type `Node_Triple` (which is possible in results of SPARQL* queries). In this case, the triple wrapped by that `Node_Triple` should be printed Turtle*-style, i.e., enclosed within `<<` and `>>`. I have already implemented the necessary extension of the `TextOutput` class: see [`TextOutputStar` in RDFstarTools](https://github.com/RDFstar/RDFstarTools/blob/master/src/main/java/se/liu/ida/rdfstar/tools/sparqlstar/resultset/TextOutputStar.java).
    
    > `QueryExecutils.outputResultSet` handles it - making `ResultSetFormatter.output(OutputStream, ResultSet, ResultsFormat)` could do the same (lookup properly for first-class results formats, and have some adapter code to handle the other ways) is one way. Would that work or you?
    
    Not really I think. I am indirectly using `QueryExecUtils.outputResultSet` already. That is, my command line tool for executing SPARQL* queries (`ExecuteSPARQLStar`) ends up in exactly that method, simply because `ExecuteSPARQLStar` extends `arq.query` (which uses `QueryExecUtils.executeQuery(Prologue, QueryExecution, ResultsFormat)`). Then, `QueryExecUtils.outputResultSet` calls `ResultSetFormatter.out(OutputStream, ResultSet, Prologue)`.
    The problem is that all of these are static methods and there is no way for me to specify which `TextOutput` object should be used by `ResultSetFormatter.out(OutputStream, ResultSet, Prologue)`. I want it to use my `TextOutputStar` (see above) instead of `TextOutput`.
    
    So, again, I think the easiest solution would be if my command line tool for executing SPARQL* queries (`ExecuteSPARQLStar`) could simply call a to-be-created method of `ResultsFormat` that allows my program to add something to `ResultsFormat.mapResultsFormatToLang`. Then, my program would simply add `ResultsFormat.FMT_TEXT` -> `ResultSetLang.SPARQLResultSetText` (which would be used only within the context of my program), and `ResultsWriter` would find and use my `ResultSetWriterFactory` (which is created and registered in [`ResultSetWritersSPARQLStar`](https://github.com/RDFstar/RDFstarTools/blob/master/src/main/java/se/liu/ida/rdfstar/tools/sparqlstar/resultset/ResultSetWritersSPARQLStar.java)). Hence, all that would be needed is to add the following code to the `ResultsFormat` class of Jena.
    ```
    public static void addResultsFormatToLangPair(ResultsFormat fmt, Lang lang) {
           mapResultsFormatToLang.put(fmt, lang) ;
    }
    ```
    I believe such a method can be helpful for other use cases as well (e.g., assume someone wants to introduce a new result format language). What do you say?


---

[GitHub] jena issue #473: Extendable versions of classes for printing result sets

Posted by afs <gi...@git.apache.org>.
Github user afs commented on the issue:

    https://github.com/apache/jena/pull/473
  
    Recorded as https://issues.apache.org/jira/browse/JENA-1640.



---

[GitHub] jena issue #473: Extendable versions of classes for printing result sets

Posted by hartig <gi...@git.apache.org>.
Github user hartig commented on the issue:

    https://github.com/apache/jena/pull/473
  
    Thanks Andy!! I will be waiting for Jena 3.10 to use this in my code.
    
    What about the other point mentioned in my initial comment in this PR? You have been hesitant to add `ResultsFormat.FMT_TEXT` to `mapResultsFormatToLang` directly in the Jena code base. Is that still the case?
    If it is, as an alternative I proposed to introduce a new method for adding something to `mapResultsFormatToLang` from external code (like mine). Does this sound reasonable? If it does, I can create another PR for it.


---

[GitHub] jena issue #473: Extendable versions of classes for printing result sets

Posted by afs <gi...@git.apache.org>.
Github user afs commented on the issue:

    https://github.com/apache/jena/pull/473
  
    Jena is already extensible for results formats. See `ResultSetWriterRegistry` and `ResultSetReaderRegistry`. I'm sorry there is old code around but, well, time ... . 
    
    Adding `ResultsFormat.FMT_TEXT` to `mapResultsFormatToLang` breaks text output. The text format needs prefix mapping for abbreviation and that is bypassed if that change is made to `mapResultsFormatToLang`. Give it a try! For `<< >>` output, abbreviated is good!
    
    It would be like:
    
        ResultsWriter.create()
            .lang(ResultSetLang.SPARQLResultSetText)
            .build()
            .write(System.out, resultSet);
    
    If `ResultSetWriterRegistry.writerText` used the `Context` to get a optional prologue; that should work out if setting the prologue is rolled out into `arq.query`.
    
    It might be better for the long term is to break up `query.queryExec` into steps with protected methods for the steps as well.
    



---

[GitHub] jena pull request #473: Extendable versions of classes for printing result s...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/jena/pull/473


---

[GitHub] jena issue #473: Extendable versions of classes for printing result sets

Posted by afs <gi...@git.apache.org>.
Github user afs commented on the issue:

    https://github.com/apache/jena/pull/473
  
    Sorry this has taken so long to get round to.


---

[GitHub] jena issue #473: Extendable versions of classes for printing result sets

Posted by hartig <gi...@git.apache.org>.
Github user hartig commented on the issue:

    https://github.com/apache/jena/pull/473
  
    > When Jena 3.9.0 is completed, I can come back and look at this PR. In principle, there is no reason not have have them extensible.
    
    Sounds good. Thanks!
    
    > What syntax are you using for TSV extensions?
    
    My idea was to simply extend the TSV format that you have in Jena with a possibility to also represent `Node_Triple` objects (as solution mappings in SPARQL* may map variables not only to URIs, literals, and bnodes, but also to triples).
    
    > On the question of mapping `ResultsFormat.FMT_TEXT`: IIRC it is because text is only an output format and not an input format (but then CSV is also only an output format).
    
    I see. If you are hesitant to add `ResultsFormat.FMT_TEXT` to `mapResultsFormatToLang` directly in the Jena code base, my alternative proposal of introducing a method for adding something to `mapResultsFormatToLang` from external code (like mine) would work for me too.


---

[GitHub] jena issue #473: Extendable versions of classes for printing result sets

Posted by afs <gi...@git.apache.org>.
Github user afs commented on the issue:

    https://github.com/apache/jena/pull/473
  
    On the question of mapping `ResultsFormat.FMT_TEXT`: IIRC it is because text is only an output format and not an input format (but then CSV is also only an output format).
    
    When Jena 3.9.0 is completed, I can come back and look at this PR. In principle, there is no reason not have have them extensible.
    
    What syntax are you using for TSV extensions?



---

[GitHub] jena issue #473: Extendable versions of classes for printing result sets

Posted by afs <gi...@git.apache.org>.
Github user afs commented on the issue:

    https://github.com/apache/jena/pull/473
  
    I got sidetracked :-) last time.  The code in this area is ancient.
    
    Text format isn't like the other formats because (1) it is isn't self-contained, It needs prefixes for abbreviation and (2) there isn't an input version.
    
    Where are you wanting to output text format?
    
    `QueryExecutils.outputResultSet` handles it - making `ResultSetFormatter.output(OutputStream, ResultSet, ResultsFormat)` do the same (lookup properly for first-class results formats, and have some adapter code to handle the other ways) is one way. Would that work or you?


---

[GitHub] jena issue #473: Extendable versions of classes for printing result sets

Posted by afs <gi...@git.apache.org>.
Github user afs commented on the issue:

    https://github.com/apache/jena/pull/473
  
    @hartig - see PR #507 ; that should allow you to `ResultSetWriterRegistry.register(SPARQLResultSetText, <YourCodeFactory>)` to set your own text output engine.
    
    For grabbing the prologue, see:
    https://github.com/apache/jena/pull/507/files#diff-6efebeadd02ab276d49ddc9bf99f0758R117


---