Skip to content

Commit

Permalink
Fix NGSI-LD table in wrong order when sh:ord is not sorted
Browse files Browse the repository at this point in the history
Creation of NGSI-LD table assumed the columns are in correct order in SHACL-file. However,
in can happen that e.g. the sh:order number is decreasing and that confuses the ngsi-ld model creator.

Signed-off-by: marcel <[email protected]>
  • Loading branch information
wagmarcel authored and abhijith-hr committed Feb 2, 2024
1 parent fe166c7 commit d2e2dda
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions semantic-model/shacl2flink/create_ngsild_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.
#

from rdflib import Graph, Namespace
from rdflib import Graph, Namespace, Variable
from rdflib.namespace import RDF
import os
import sys
Expand All @@ -25,6 +25,22 @@
from ruamel.yaml.scalarstring import (SingleQuotedScalarString as sq)


field_query = """
PREFIX iff: <https://industry-fusion.com/types/v0.9/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ngsild: <https://uri.etsi.org/ngsi-ld/>
PREFIX sh: <http://www.w3.org/ns/shacl#>
SELECT DISTINCT ?path
where {
?nodeshape a sh:NodeShape .
?nodeshape sh:targetClass ?shacltype .
?nodeshape sh:property [ sh:path ?path ; sh:order ?ord ] .
}
ORDER BY ?ord
"""


def parse_args(args=sys.argv[1:]):
parser = argparse.ArgumentParser(description='create_ngsild_tables.py \
<shacl.ttl>')
Expand Down Expand Up @@ -53,9 +69,11 @@ def main(shaclfile, output_folder='output'):
table = tables[stripped_class]
table.append({sq("id"): "STRING"})
table.append({sq("type"): "STRING"})

for _, _, target_property in g.triples((s, sh.property, None)):
target_path = g.value(target_property, sh.path)
# Query the fields in order
bindings = {Variable("shacltype"): target_class}
qres = g.query(field_query, initBindings=bindings)
for row in qres:
target_path = row.path
table.append({sq(f'{target_path}'): "STRING"})
table.append({sq("ts"): "TIMESTAMP(3) METADATA FROM 'timestamp'"})
table.append({"watermark": "FOR `ts` AS `ts`"})
Expand Down

0 comments on commit d2e2dda

Please sign in to comment.