sparna-git / Sparnatural

Sparnatural : Typescript visual SPARQL query builder for knowledge graphs, configurable with SHACL
http://sparnatural.eu
GNU Lesser General Public License v3.0
206 stars 34 forks source link

User Story : parameterized queries #592

Open tfrancart opened 1 month ago

tfrancart commented 1 month ago

The goal is to create query templates, and indicate which variables in this query template could be choosed by a user. Then users can be proposed with very simple forms containing only these variables to select, without having to redesign the entire query. This opens the possibility to offer multiple simple forms to end-users, using the same value selection widgets as in Sparnatural.

The story is the following:

  1. An administrator creates a Sparnatural query and store it somewhere, probably on a server (see #591). For example "All museums that are located in Any Country, with photo and geo coordinates"
  2. The administrator associates it with "variable bindings" to mark the variables that can be assigned by a user. For example the variable _"Museum2" in our example
  3. This parameterized query is saved somewhere, with a name a description. For example "List museums in a country".
  4. An end-user can select this parameterized query. He is presented with a very simple form containing only one field : "Country".
  5. The end-user selects a Country in the same widget as the original widget in Sparnatural
  6. He runs the query, and gets a list of musuem with photo and geo coordinates.

VocBench actually has this functionnality:

  1. Store query:

image

  1. Create variable binding:

image

  1. Store parametrized query

image

  1. End-user loads the query:

image

  1. End-user is presented with a simple form with only the variable binding to enter:

image

end-user selects a value:

image

  1. End-user runs the query:

image

@antoine37120

tfrancart commented 1 week ago

Metaphacts also have something like this when saving queries:

image

tfrancart commented 1 day ago

I. A new webcomponent

<spar-natural-form
    src="config.ttl"
    query="query.json"
    form="form.json"
/>

II. The form configuration file

form.json :

{
    "bindings": [
        {
            "variable" : "Country_4",
            "node" : {
                "type" : "UserPrompt",
                "name" : {
                    "en" : "Country",
                    "fr" : "Pays"
                }
            }
        },
        {
            "variable" : "Person_10",
            "node" : {
                "type" : "UserPrompt",
                "name" : {
                    "en" : "Author",
                    "fr" : "Auteur"
                }
            }
        }
    ]   
}

III. The query json file (saved from Sparnatural)

image

query.json:

{
  "distinct": true,
  "variables": [
    {
      "termType": "Variable",
      "value": "Artwork_1"
    },
    {
      "termType": "Variable",
      "value": "Country_4"
    },
    {
      "termType": "Variable",
      "value": "Image_6"
    }
  ],
  "order": null,
  "branches": [
    {
      "line": {
        "s": "Artwork_1",
        "p": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#displayedAt",
        "o": "Museum_2",
        "sType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Artwork",
        "oType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Museum",
        "values": []
      },
      "children": [
        {
          "line": {
            "s": "Museum_2",
            "p": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#country",
            "o": "Country_4",
            "sType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Museum",
            "oType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Country",
            "values": []
          },
          "children": []
        }
      ]
    },
    {
      "line": {
        "s": "Artwork_1",
        "p": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#thumbnail",
        "o": "Image_6",
        "sType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Artwork",
        "oType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Image",
        "values": []
      },
      "children": [],
      "optional": true
    },
    {
      "line": {
        "s": "Artwork_1",
        "p": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#description",
        "o": "Text_8",
        "sType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Artwork",
        "oType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Text",
        "values": []
      },
      "children": [],
      "optional": true
    },
    {
      "line": {
        "s": "Artwork_1",
        "p": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#author",
        "o": "Person_10",
        "sType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Artwork",
        "oType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Person",
        "values": []
      },
      "children": [],
      "optional": true
    }
  ]
}

IV The generated form

image

IV The final query

Si l'utilisateur saisi seulement le pays mais pas l'auteur, alors:

Requête JSON finale:

{
  "distinct": true,
  "variables": [
    {
      "termType": "Variable",
      "value": "Artwork_1"
    },
    {
      "termType": "Variable",
      "value": "Country_4"
    },
    {
      "termType": "Variable",
      "value": "Image_6"
    }
  ],
  "order": null,
  "branches": [
    {
      "line": {
        "s": "Artwork_1",
        "p": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#displayedAt",
        "o": "Museum_2",
        "sType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Artwork",
        "oType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Museum",
        "values": []
      },
      "children": [
        {
          "line": {
            "s": "Museum_2",
            "p": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#country",
            "o": "Country_4",
            "sType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Museum",
            "oType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Country",
            "values": [
                {
                "label": "France (3987)",
                "rdfTerm": {
                  "type": "uri",
                  "value": "http://fr.dbpedia.org/resource/France"
                }
               }
            ]
          },
          "children": []
        }
      ]
    },
    {
      "line": {
        "s": "Artwork_1",
        "p": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#thumbnail",
        "o": "Image_6",
        "sType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Artwork",
        "oType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Image",
        "values": []
      },
      "children": [],
      "optional": true
    },
    {
      "line": {
        "s": "Artwork_1",
        "p": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#description",
        "o": "Text_8",
        "sType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Artwork",
        "oType": "http://ontologies.sparna.fr/sparnatural-demo-dbpedia#Text",
        "values": []
      },
      "children": [],
      "optional": true
    }
  ]
}

V We could use other inputs than user forms

Par exemple pour positionner une variable à la date du jour, ou par un bout de script:

{
    "bindings": [
        {
            "variable" : "Country_1",
            "node" : {
                "type" : "UserPrompt",
                "name" : {
                    "en" : "Select a country",
                    "fr" : "Sélectionner un pays"
                }
            }
        },
        {
            "variable" : "Date_2",
            "node" : {
                "type" : "DynamicValueProvider",
                "valueProvider" : "TypescriptClassNameOfTheValueProvider"
            }
        }
    ]   
}