Skip to content

LiaTemplates/Communica

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Communica - Template

                      --{{0}}--

This document defines some basic LiaScript-macros for dealing and experimenting with the semantic web-technologies, such as querying SPARQL and or dealing with RDF in the form of turtle. Others are about to come. This template is mainly based on Communica an open-source knowledge graph querying framework. I hope in the future, there will be more functions available. At the moment it is possible to execute and edit code-blocks with SPARQL queries.

Try it on LiaScript:

https://liascript.github.io/course/?https://raw.githubusercontent.com/liaTemplates/Communica/main/README.md

See the project on Github:

https://github.com/liaTemplates/Communica

                     --{{1}}--

There are three ways to use this template. The easiest way is to use the import statement and the url of the raw text-file of the master branch or any other branch or version. But you can also copy the required functionionality directly into the header of your Markdown document, see therefor the last slide. And of course, you could also clone this project and change it, as you wish.

                       {{1}}
  1. Load the macros via

    import: https://raw.githubusercontent.com/LiaTemplates/Communica/0.0.2/README.md

  2. Copy the definitions into your Project

  3. Clone this repository on GitHub

Minimal Requirements

                     --{{0}}--

What is actually needed to make your Markdown code-snippets with SPARQL executable and editable? Not much, simply add the following comment to the head of your Markdown document.

<!--
import: https://raw.githubusercontent.com/LiaTemplates/Communica/0.0.2/README.md
-->

# Title

...
                     --{{1}}--

And finally attach the macros/functions, which are defined by this document, to the end of a code-block. You can define as much code-blocks as you want.

                       {{1}}
``` sparql
# source: https://fragments.dbpedia.org/2015/en

SELECT ?s ?p ?o WHERE {
  ?s ?p <http://dbpedia.org/resource/Ukraine>.
  ?s ?p ?o
} LIMIT 100
```
@Communica.SPARQL

Macros

                      --{{0}}--

Currently you can use two macros, which can also be tweaked a bit. We will describe them both within this section.

  1. @Communica.SPARQL
  2. @Communica.RDF_SPARQL

@Communica.SPARQL

                      --{{0}}--

Add the macro @Communica.SPARQL to the end of your SPARQL query to make it executable and editable. You can add as much blocks as you want to.

``` sparql
# source: https://fragments.dbpedia.org/2015/en

SELECT ?s ?p ?o WHERE {
  ?s ?p <http://dbpedia.org/resource/Ukraine>.
  ?s ?p ?o
} LIMIT 10
```
@Communica.SPARQL
                      --{{1}}--

The result will look as follows, you can execute it, change the code of the query and go back and forth between your version.

                        {{1}}
# source: https://fragments.dbpedia.org/2015/en

SELECT ?s ?p ?o WHERE {
  ?s ?p <http://dbpedia.org/resource/Ukraine>.
  ?s ?p ?o
} LIMIT 10

@Communica.SPARQL

                      --{{2}}--

And to some extend also the errors are propagated to the user.

                        {{2}}
# source: https://fragments.dbpedia.org/2015/en

SELECT ?s ?p ?o WHERE {
  ?s ?p <http://dbpedia.org/resource/Ukraine>
  ?s ?p ?o
} LIMIT 10

@Communica.SPARQL

Sources

                      --{{0}}--

As you have seen earlier, it is possible by you and by your user to define the sources for your query. Simply add one or multiple comments of the following form into your code-block. If you use a one-liner, your sources have to be separated by one space.

# source: https://fragments.dbpedia.org/2015/en
# source: https://www.rubensworks.net https://ruben.verborgh.org/profile/

SELECT ?s ?p ?o WHERE {
?s ?p <http://dbpedia.org/resource/Belgium>.
?s ?p ?o
} LIMIT 100

@Communica.SPARQL

Formats

                      --{{0}}--

It is possible to define different output formats, simply by adding an comment of the form # format: _.

# format: table
# source: https://fragments.dbpedia.org/2015/en

SELECT ?s ?p ?o WHERE {
?s ?p <http://dbpedia.org/resource/Ukraine>.
?s ?p ?o
} LIMIT 5

@Communica.SPARQL

                      --{{1}}--

The default format is currently table, however, you can overwrite this by one of the following media types. All possible formats are defined here. However, currently it only possible to use a subset of them, which are highlighted in the table.

                        {{1}}
Media type Supported Description
application/json A custom, simplified JSON result format.
simple A custom, text-based result format.
application/sparql-results+json The SPARQL/JSON results format.
application/sparql-results+xml The SPARQL/XML results format.
text/csv The SPARQL/CSV results format.
text/tab-separated-values The SPARQL/TSV results format.
stats A custom results format for testing and debugging.
table A text-based visual table result format.
tree A tree-based result format for GraphQL-LD result compacting.
application/trig The TriG RDF serialization.
application/n-quads The N-Quads RDF serialization.
text/turtle The Turtle RDF serialization.
application/n-triples The N-Triples RDF serialization.
text/n3 The Notation3 serialization.
application/ld+json The JSON-LD RDF serialization.

@Communica.RDF_SPARQL

                      --{{0}}--

It is possible also to define your own files and to query them, by combining two Markdown-blocks in conjunction. The first contains your turtle file, while the second code-block contains your query.

``` turtle       Turtle
@prefix :     <http://www.example.org/sample.rdfs#> .
@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.

:Dog      rdfs:subClassOf :Animal.
:Person   rdfs:subClassOf :Animal.

:hasChild rdfs:range :Animal;
          rdfs:domain :Animal.
:hasSon   rdfs:subPropertyOf :hasChild.

:Max      a :Dog.
:Abel     a :Person.
:Adam     a :Person;
            :hasSon :Abel.
```
``` sparql      -SPARQL-Query
SELECT * {
  ?s ?p ?o
} LIMIT 5
```
@Communica.RDF_SPARQL
                      --{{1}}--

The text behind the language definition is used as a title for your code-block, whereby a starting + or - define, wheather the editor view should be opened or closed.

                        {{1}}
@prefix :     <http://www.example.org/sample.rdfs#> .
@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.

:Dog      rdfs:subClassOf :Animal.
:Person   rdfs:subClassOf :Animal.

:hasChild rdfs:range :Animal;
          rdfs:domain :Animal.
:hasSon   rdfs:subPropertyOf :hasChild.

:Max      a :Dog.
:Abel     a :Person.
:Adam     a :Person;
            :hasSon :Abel.
# format: simple
# source: https://fragments.dbpedia.org/2015/en

SELECT * {
  ?s ?p ?o
} LIMIT 5

@Communica.RDF_SPARQL

                      --{{2}}--

It is still possible to add different sources or to change the output format, as described before.

Implementation

                      --{{0}}--

All the functionionality is defined within the main-comment of this document. It makes use of the typescript-functions defined in src/index.ts, which defines a global object ẁindow.RDF.

script:   dist/index.js

@Communica.SPARQL
<script>
  function output(ok, message) {
    if (ok) send.lia(message)
    else {
      let errMessage = []
      let err = message.match(/Parse error on line (\d+)[^\n]\n((?:.|\n)+)/)

      if (err.length >= 3) {
        errMessage = [[{
          row : parseInt(err[1]) - 1,
          column : 1,
          text : err[2],
          type : "error"
        }]]
      }

      send.lia(message, errMessage, false)
    }
  }

  window.RDF.query(`@input`, output, [] )

  "LIA: wait"
</script>
@end

@Communica.RDF_SPARQL
<script>
  function output(ok, message) {
    if (ok) send.lia(message)
    else {
      let errMessage = []
      let err = message.match(/Parse error on line (\d+)[^\n]\n((?:.|\n)+)/)

      if (err.length >= 3) {
        errMessage = [[], [{
          row : parseInt(err[1]) - 1,
          column : 1,
          text : err[2],
          type : "error"
        }]]
      }

      send.lia(message, errMessage, false)
    }
  }

  window.RDF.parse.rdf(`@input`).then((e) => {
    window.RDF.query(`@input(1)`, output, [ e ] )
  })

  "LIA: wait"
</script>
@end
                      --{{1}}--

If you want to build this project by your own, then follow the following instructions:

                        {{1}}
$ git clone https://github.com/LiaTemplates/Communica

$ cd Communica

$ git checkout dev

$ npm i

$ npm run build