|
1 | | -enumeration_query = r""" |
2 | | -PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
3 | | -PREFIX schema: <http://schema.org/> |
4 | | -PREFIX skos: <http://www.w3.org/2004/02/skos/core#> |
| 1 | +import os |
| 2 | +from dotenv import load_dotenv |
| 3 | +from config import args |
5 | 4 |
|
6 | | -construct { ?subject ?predicate ?object } |
7 | | -WHERE { |
8 | | - ?subject a schema:Enumeration . |
9 | | - ?subject ?predicate ?object . |
10 | | -} |
11 | | -""" |
| 5 | +# Load environment variables from the .env file |
| 6 | +load_dotenv() |
| 7 | + |
| 8 | +# Parse graph names from command-line arguments |
| 9 | +# Assign graph names |
| 10 | +INSTANCE_DATA_GRAPH = args.kg_uri |
| 11 | +ONTOLOGY_GRAPH = args.ontology_uri |
12 | 12 |
|
13 | | -find_matches_query = r""" |
14 | | -PREFIX skos: <http://www.w3.org/2004/02/skos/core#> |
| 13 | +# # Parametrize the queries |
| 14 | +# enumeration_query = r""" |
| 15 | +# PREFIX schema: <http://schema.org/> |
| 16 | +# PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
| 17 | +# PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
| 18 | +# CONSTRUCT {?subject ?predicate ?object } |
| 19 | +# WHERE { |
| 20 | +# ?subject rdf:type/rdfs:subClassOf* schema:Enumeration . |
| 21 | +# ?subject ?predicate ?object . |
| 22 | +# } |
| 23 | +# """ |
| 24 | + |
| 25 | +strings_to_things_query = f""" |
| 26 | +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
15 | 27 | PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
16 | 28 | PREFIX schema: <http://schema.org/> |
17 | | -PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> |
| 29 | +PREFIX sh: <http://www.w3.org/ns/shacl#> |
18 | 30 |
|
19 | | -CONSTRUCT { |
20 | | - ?s ?p ?result . |
21 | | -} |
22 | | -WHERE { |
23 | | - GRAPH <https://imaging-plaza.epfl.ch/finalGraph> { |
| 31 | +CONSTRUCT {{ |
| 32 | + ?s ?p ?finalValue . |
| 33 | +}} |
| 34 | +WHERE {{ |
| 35 | + GRAPH <{INSTANCE_DATA_GRAPH}> {{ |
24 | 36 | ?s ?p ?o . |
25 | | - FILTER (!(?p IN (schema:name, schema:description, rdfs:comment, skos:definition))) |
26 | | - FILTER (!regex(STR(?o), "^[ \t]*https?://")) |
27 | | - FILTER (!regex(STR(?o), "^\\d{4}-\\d{2}-\\d{2}T00:00:00\\.000Z$")) |
28 | | - FILTER (datatype(?o) = xsd:string) |
29 | | - } |
30 | | - GRAPH <https://imaging-plaza.epfl.ch/ontology#enums> { |
31 | | - OPTIONAL { |
32 | | - ?s2 rdfs:label ?o. |
33 | | - } |
34 | | -
|
35 | | - # Ensure only one IRI is bound to the label, skipping rows with multiple IRIs |
36 | | - FILTER NOT EXISTS { |
37 | | - ?s3 rdfs:label ?o. |
38 | | - FILTER (?s3 != ?s2) # Ensures ?s2 is the only IRI bound to the label |
39 | | - } |
40 | | - } |
41 | | -
|
42 | | - BIND(IF(BOUND(?s2), ?s2, ?o) AS ?result) |
43 | | -} |
| 37 | + }} |
| 38 | +
|
| 39 | + # Find the expected enumeration class for this property |
| 40 | + GRAPH <{ONTOLOGY_GRAPH}> {{ |
| 41 | + OPTIONAL {{ |
| 42 | + ?propertyShape sh:path ?p ; |
| 43 | + sh:class ?expectedEnumClass . |
| 44 | +
|
| 45 | + ?enumInstance rdf:type*/rdfs:subClassOf* ?expectedEnumClass ; |
| 46 | + rdfs:label ?o . |
| 47 | + }} |
| 48 | + }} |
| 49 | +
|
| 50 | + # Choose only one matching IRI per string, ensuring correct category |
| 51 | + BIND(COALESCE(?enumInstance, ?o) AS ?finalValue) |
| 52 | +}} |
44 | 53 | """ |
45 | 54 |
|
46 | | -find_predicate_query = r""" |
47 | | -PREFIX skos: <http://www.w3.org/2004/02/skos/core#> |
| 55 | +things_to_strings_query = f""" |
| 56 | +PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
48 | 57 | PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> |
49 | 58 | PREFIX schema: <http://schema.org/> |
50 | | -PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> |
51 | | -
|
52 | | -SELECT ?o ?p |
53 | | -WHERE { |
54 | | - ?s ?p ?o . |
55 | | - FILTER (!(?p IN (schema:name, schema:description, rdfs:comment, skos:definition))) |
56 | | - FILTER (!regex(STR(?o), "^[ \t]*https?://")) |
57 | | - FILTER (!regex(STR(?o), "^\\d{4}-\\d{2}-\\d{2}T00:00:00\\.000Z$")) |
58 | | - FILTER (datatype(?o) = xsd:string) |
59 | | -}""" |
| 59 | +PREFIX sh: <http://www.w3.org/ns/shacl#> |
| 60 | +
|
| 61 | +CONSTRUCT {{ |
| 62 | + ?s ?p ?finalValue . |
| 63 | +}} |
| 64 | +WHERE {{ |
| 65 | + GRAPH <{INSTANCE_DATA_GRAPH}> {{ |
| 66 | + ?s ?p ?o . |
| 67 | + }} |
| 68 | +
|
| 69 | + # Get the expected enum class for this property |
| 70 | + GRAPH <{ONTOLOGY_GRAPH}> {{ |
| 71 | + OPTIONAL {{ |
| 72 | + ?propertyShape sh:path ?p ; |
| 73 | + sh:class ?expectedEnumClass . |
| 74 | +
|
| 75 | + ?o a ?expectedEnumClass ; |
| 76 | + rdfs:label ?label . |
| 77 | + }} |
| 78 | + }} |
| 79 | +
|
| 80 | + # If a label is found (meaning ?o is an enum instance), use it; otherwise keep the original value |
| 81 | + BIND(COALESCE(?label, ?o) AS ?finalValue) |
| 82 | +}} |
| 83 | +""" |
0 commit comments