Skip to content

Commit

Permalink
extended examples and stardog load/query tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-metalinkage committed Sep 3, 2024
1 parent 44806c7 commit 564766c
Show file tree
Hide file tree
Showing 23 changed files with 552 additions and 1 deletion.
20 changes: 20 additions & 0 deletions _sources/activities/aggregation/bblock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "Aggregation Activity",
"abstract": "An aggregation activity integrates a stream of observations into a persistent archive.",
"status": "under-development",
"dateTimeAddition": "2023-05-19T00:00:00Z",
"itemClass": "schema",
"register": "ccm",
"version": "1.0",
"dateOfLastChange": "2023-05-19",
"sources": [
],
"maturity": "development",
"scope": "unstable",
"dependsOn": [
],
"tags": [
"prov"],
"group": "Activities",
"highlighted": true
}
7 changes: 7 additions & 0 deletions _sources/activities/aggregation/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Aggregation Activity

This building block provides a JSON schema based on the PROV model for an activity that performs an aggregation over dimensions of a set of data observations.

The aggregation function must be described using an appropriate AggregationFunction entity.


24 changes: 24 additions & 0 deletions _sources/activities/aggregation/examples.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Prefixes will be used for all RDF Turtle resources so that they can be omitted from the actual snippets
# prefixes:
# dct: http://purl.org/dc/terms/

## List of examples
examples:
- title: Aggregation

## Markdown content that will be shown for the example.
content:
Shows a aggregation activity with a positive result.

## Base URI for semantic uplift
base-uri: http://example.com/activities/

## The example can define its own prefixes, that will be merged with the global ones
prefixes:
myData: http://example.com/datasets/

## List of code snippets for this example
snippets:
- language: json
# Reference to file
ref: examples/aggregation.json
15 changes: 15 additions & 0 deletions _sources/activities/aggregation/examples/aggregation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"provType": [ "Activity", "Aggregation" ],
"id": "aggregation_example",
"endedAtTime": "2029-01-01T22:05:19+02:00",
"wasAssociatedWith": "eg_agents:bc-3",
"generated": { "id": "myData:YearlySummary",
"wasDerivedFrom" : "myData:WeeklyRecords",
"wasGeneratedBy" : "aggregation_example"
},
"used": "myData:WeeklyRecords"
}




18 changes: 18 additions & 0 deletions _sources/activities/aggregation/schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
description: Aggregation activity
$defs:
Aggregation:
allOf:
- $ref: bblocks://ogc.ogc-utils.prov#/$defs/Activity
- properties:
provType:
type: array
contains:
type: string
const: Aggregation
required:
- provType
- generated
- used

anyOf:
- $ref: "#/$defs/Aggregation"
2 changes: 1 addition & 1 deletion _sources/activities/certification/bblock.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"status": "under-development",
"dateTimeAddition": "2023-05-19T00:00:00Z",
"itemClass": "schema",
"register": "ogc-building-block-examples",
"register": "ccm",
"version": "1.0",
"dateOfLastChange": "2023-05-19",
"sources": [
Expand Down
20 changes: 20 additions & 0 deletions _sources/activities/derivation/bblock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "Derivation Activity",
"abstract": "A derivation activity takes a data set and derives a new data set from it. It must reference an Entity which defines the derivation function, allowing identification of the derivation process.",
"status": "under-development",
"dateTimeAddition": "2023-05-19T00:00:00Z",
"itemClass": "schema",
"register": "ccm",
"version": "1.0",
"dateOfLastChange": "2023-05-19",
"sources": [
],
"maturity": "development",
"scope": "unstable",
"dependsOn": [
],
"tags": [
"prov"],
"group": "Activities",
"highlighted": true
}
8 changes: 8 additions & 0 deletions _sources/activities/derivation/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Derivation Activity

This building block provides a JSON schema based on the PROV model for an activity derives new data using a function based on properties of that data.





24 changes: 24 additions & 0 deletions _sources/activities/derivation/examples.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Prefixes will be used for all RDF Turtle resources so that they can be omitted from the actual snippets
# prefixes:
# dct: http://purl.org/dc/terms/

## List of examples
examples:
- title: Derivation

## Markdown content that will be shown for the example.
content:
Shows a derivation activity with a positive result.

## Base URI for semantic uplift
base-uri: http://example.com/activities/

## The example can define its own prefixes, that will be merged with the global ones
prefixes:
mydata: http://example.com/datasets/

## List of code snippets for this example
snippets:
- language: json
# Reference to file
ref: examples/derivation.json
65 changes: 65 additions & 0 deletions _sources/activities/derivation/examples/a-plus-b.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
@prefix mydata: <http://example.com/data/> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix sdo: <https://schema.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ex: <http://example.org/> .
@prefix geo: <http://www.opengis.net/ont/geosparql#> .
@prefix sf: <http://www.opengis.net/ont/sf#> .


<http://example.com/activities/add1>
a prov:Activity ;
rdfs:label "Adder-run1" ;
prov:endedAtTime "2029-01-01T22:05:19+02:00"^^xsd:dateTime ;
prov:generated mydata:c ;
prov:used mydata:a, mydata:b ;
prov:wasAssociatedWith <http://example.com/agents/adder> .

mydata:a
a prov:Entity ;
rdfs:label "A" ;
sdo:value 1
.

mydata:b
a prov:Entity ;
rdfs:label "B" ;
sdo:value 2
.

mydata:c
a <http://example.com/data/results/c> ;
geo:hasGeometry [ geo:asWKT """POLYGON((150.5000 -34.0000, 150.5020 -34.0005, 150.5040 -34.0010, 150.5060 -34.0015,
150.5080 -34.0020, 150.5100 -34.0025, 150.5120 -34.0030, 150.5140 -34.0035,
150.5160 -34.0040, 150.5180 -34.0045, 150.5200 -34.0050, 150.5220 -34.0045,
150.5240 -34.0040, 150.5260 -34.0035, 150.5280 -34.0030, 150.5300 -34.0025,
150.5280 -34.0020, 150.5260 -34.0015, 150.5240 -34.0010, 150.5220 -34.0005,
150.5200 -34.0000, 150.5180 -34.0005, 150.5160 -34.0010, 150.5140 -34.0015,
150.5120 -34.0020, 150.5100 -34.0025, 150.5080 -34.0030, 150.5060 -34.0025,
150.5040 -34.0020, 150.5020 -34.0015, 150.5000 -34.0010, 150.5000 -34.0000))"""^^geo:wktLiteral ;
geo:asGeoJSON """{
"type": "Polygon",
"coordinates": [[
[150.5000, -34.0000], [150.5020, -34.0005], [150.5040, -34.0010], [150.5060, -34.0015],
[150.5080, -34.0020], [150.5100, -34.0025], [150.5120, -34.0030], [150.5140, -34.0035],
[150.5160, -34.0040], [150.5180, -34.0045], [150.5200, -34.0050], [150.5220, -34.0045],
[150.5240, -34.0040], [150.5260, -34.0035], [150.5280, -34.0030], [150.5300, -34.0025],
[150.5280, -34.0020], [150.5260, -34.0015], [150.5240, -34.0010], [150.5220, -34.0005],
[150.5200, -34.0000], [150.5180, -34.0005], [150.5160, -34.0010], [150.5140, -34.0015],
[150.5120, -34.0020], [150.5100, -34.0025], [150.5080, -34.0030], [150.5060, -34.0025],
[150.5040, -34.0020], [150.5020, -34.0015], [150.5000, -34.0010], [150.5000, -34.0000]
]]
}"""^^geo:geoJSONLiteral
];

rdfs:label "C" ;
sdo:value 3 ;
prov:wasDerivedFrom mydata:a, mydata:b ;
prov:wasGeneratedBy <http://example.com/activities/add1> .

<http://example.com/agents/adder>
a prov:Agent ;
rdfs:label "Add" ;
sdo:description "Adds all input values"
.
15 changes: 15 additions & 0 deletions _sources/activities/derivation/examples/derivation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"provType": [ "Activity", "Derivation" ],
"id": "derivation_example",
"endedAtTime": "2029-01-01T22:05:19+02:00",
"wasAssociatedWith": "eg_agents:bc-3",
"generated": { "id": "mydata:YearlySummary",
"wasDerivedFrom" : "mydata:WeeklyRecords",
"wasGeneratedBy" : "derivation_example"
},
"used": "mydata:WeeklyRecords"
}




18 changes: 18 additions & 0 deletions _sources/activities/derivation/schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
description: Derivation activity
$defs:
Derivation:
allOf:
- $ref: bblocks://ogc.ogc-utils.prov#/$defs/Activity
- properties:
provType:
type: array
contains:
type: string
const: Derivation
required:
- provType
- generated
- used

anyOf:
- $ref: "#/$defs/Derivation"
1 change: 1 addition & 0 deletions bblocks-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ imports:
- https://ogcincubator.github.io/bblock-prov-schema/build/register.json
- https://ogcincubator.github.io/geodcat-ogcapi-records/build/register.json


# SPARQL configuration (optional)
# Authentication can be set through the sparql_username and sparql_password GitHub Workflow inputs
# (see .github/workflow/process-bblocks.yml)
Expand Down
1 change: 1 addition & 0 deletions queries/clearGraph.sparql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CLEAR GRAPH $graph
3 changes: 3 additions & 0 deletions queries/cross-domain-alias.trig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<tag:stardog:api:graph:aliases> {
<tag:cross-domain-model> <tag:stardog:api:graph:alias> <https://www.w3.org/ns/prov> .
}
9 changes: 9 additions & 0 deletions queries/getActivity.sparql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
prefix prov: <http://www.w3.org/ns/prov#>
SELECT ?A ?p ?plabel ?o ?olabel
WHERE {
$entity prov:wasGeneratedBy ?A .
?A ?p ?o .
OPTIONAL { ?p rdfs:label ?plabel }
OPTIONAL { ?o rdfs:label ?olabel }

}
8 changes: 8 additions & 0 deletions queries/getEntity.sparql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
prefix prov: <http://www.w3.org/ns/prov#>
SELECT $entity ?p ?plabel ?o ?olabel
WHERE {
$entity ?p ?o .
OPTIONAL { ?p rdfs:label ?plabel }
OPTIONAL { ?o rdfs:label ?olabel }

}
11 changes: 11 additions & 0 deletions queries/getSpatialEntities.sparql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
prefix prov: <http://www.w3.org/ns/prov#>
prefix geo: <http://www.opengis.net/ont/geosparql#>
SELECT $entity ?label ?geojson ?wkt
WHERE {
$entity geo:hasGeometry ?g .
OPTIONAL { ?g geo:asGeoJSON ?geojson }
OPTIONAL { ?g geo:asWKT ?wkt }
OPTIONAL { $entity rdfs:label ?label }


}
79 changes: 79 additions & 0 deletions queries/loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import csv
import os
import requests
from requests.auth import HTTPBasicAuth

# Retrieve the server, database, username, and password from environment variables
stardog_server = os.getenv('STARDOG_SERVER')
stardog_database = os.getenv('STARDOG_DATABASE')
username = os.getenv('STARDOG_USERNAME')
password = os.getenv('STARDOG_PASSWORD')

# Check if all required environment variables are set
if not all([stardog_server, stardog_database, username, password]):
raise ValueError("One or more required environment variables are not set")


# Define the default value for the "model" column
DEFAULT_MODEL = None


def load(filename, uri, context, model):
"""
Load a data file into stardog, so that it appears as a named graph.
The named graph is included in an "alias" - allowing multiple queryable data collections to co-exist.
Todo: force entailment using the named "model" as a reasoning graph
Parameters:
- name: The filename of the graph.
- uri: The URI for the named graph.
- context: The alias (collection) graph to include the new data in.
- [ model: The reasoning model (T-box) . ]
"""
# Set up the headers
with open(filename, 'r') as file:
data = file.read()

headers = {
'Content-Type': 'text/turtle',
'Accept': 'application/sparql-results+json'
}
# Construct the Stardog URL
stardog_url = f"{stardog_server}/{stardog_database}?graph={uri}"

# Send the POST request with Basic Authentication
response = requests.put(
stardog_url,
headers=headers,
data=data,
auth=HTTPBasicAuth(username, password)
)

# Check if the request was successful
if response.status_code in [200,201]:
# Print the response JSON (assuming it's in JSON format)
print(f"{filename} - > {uri} : {response.status_code}")
else:
print(f"Error: {filename} - > {uri} : {response.status_code}")
print(response.text)


def process_csv(file_path):
with open(file_path, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
name = row.get('filename')
uri = row.get('uri')
context = row.get('context')
model = row.get('model', DEFAULT_MODEL) # Use default model if not present

# Call the load function with the values from the CSV row
load(name, uri, context, model)


# Specify the path to your CSV file
csv_file_path = 'manifest.csv'

# Process the CSV file
process_csv(csv_file_path)
2 changes: 2 additions & 0 deletions queries/manifest.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
filename,uri,context,model
../_sources/activities/derivation/examples/a-plus-b.ttl,tag:prov:a-plus-b,tag:prov-demo,tag:prov-demo-model
Loading

0 comments on commit 564766c

Please sign in to comment.