From a8596c6e12fa1cecf8eb02aee3d6eb19cf2fe010 Mon Sep 17 00:00:00 2001 From: Grzegorz Kostkowski Date: Fri, 20 Sep 2024 17:30:07 +0200 Subject: [PATCH] Implement generation of enriched namespaces file, reorganize project to depend on the new file The new XML contains user namespaces (defined in namespaces.xml) and internal namespaces (such as core-shape). Both model2owl internals and get_namespaces.sh relies on that file and not on namespaces.xml. Moreover, model2owl internals now depends on a new template which generates namespaces declarations based on enriched file which is passed to the XSLT transformation engine as command line argument. For now, the path of the new XML file is kept in the Makefile, but this can be further improved in the future (if we come up with application internal XSL config or other solution). Scope of changes: * new XSL file for generation of the enriched namespaces XML file * unit test for the above XSL file * project codebase: * new function generating namespaces declarations and references to `$namespacePrefixes` replaced with `$internalNamespacePrefixes` * Makefile changes: * new recipe * existing 3 recipes generating *.rdf artefacts now include generation of the new XML file * required adjustments * Adjustments in the get_namespaces.sh script --- .gitignore | 1 + Makefile | 34 +++++++++++++----- scripts/get_namespaces.sh | 10 +++--- src/common/checkers.xsl | 8 ++--- src/common/utils.xsl | 18 +++++++++- src/owl-core.xsl | 6 ++-- src/owl-restrictions.xsl | 8 ++--- src/shacl-shapes.xsl | 10 ++---- src/xml/enriched-namespaces.xsl | 36 +++++++++++++++++++ .../test-xml/test-enriched-namespaces.xspec | 18 ++++++++++ 10 files changed, 113 insertions(+), 36 deletions(-) create mode 100644 src/xml/enriched-namespaces.xsl create mode 100644 test/unitTests/test-xml/test-enriched-namespaces.xspec diff --git a/.gitignore b/.gitignore index cc938d59..c2e099ce 100644 --- a/.gitignore +++ b/.gitignore @@ -325,3 +325,4 @@ lib64 pyvenv.cfg /build/ /node_modules/ +.temp diff --git a/Makefile b/Makefile index c5009dd2..a3739584 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ # Model2owl directory MODEL2OWL_FOLDER?=. +ABSOLUTE_MODEL2OWL_FOLDER?=$(shell realpath "${MODEL2OWL_FOLDER}") # rdflib version RDF_LIB_VERSION?=6.2.0 #Saxon path @@ -33,8 +34,10 @@ TURTLE_FILELIST=$(shell ls ${ONTOLOGY_FOLDER_PATH}/*.ttl) # Widoco variables WIDOCO_RDF_INPUT_FILE_PATH?=test/reasoning-investigation/model-2020-12-16/ePO_restrictions.rdf WIDOCO_OUTPUT_FOLDER_PATH?=output/widoco -NAMESPACES_XML_FILE_PATH?=${MODEL2OWL_FOLDER}/test/ePO-default-config/namespaces.xml -NAMESPACES_AS_RDFPIPE_ARGS=$(shell ${MODEL2OWL_FOLDER}/scripts/get_namespaces.sh ${NAMESPACES_XML_FILE_PATH}) +NAMESPACES_USER_XML_FILE_PATH?=${MODEL2OWL_FOLDER}/test/ePO-default-config/namespaces.xml +INTERM_FOLDER_PATH?=${ABSOLUTE_MODEL2OWL_FOLDER}/.temp +ENRICHED_NAMESPACES_XML_PATH:=${INTERM_FOLDER_PATH}/enriched-namespaces.xml +NAMESPACES_AS_RDFPIPE_ARGS=$(shell ${MODEL2OWL_FOLDER}/scripts/get_namespaces.sh ${ENRICHED_NAMESPACES_XML_PATH}) RDF_XML_MIME_TYPE:='application/rdf+xml' TURTLE_MIME_TYPE:='turtle' @@ -114,8 +117,10 @@ generate-convention-SVRL-report: #Example how to run transformation commands : # make owl-core XMI_INPUT_FILE_PATH=/home/mypc/work/model2owl/eNotice_CM.xml OUTPUT_FOLDER_PATH=./my-folder owl-core: + @make gen-enriched-ns-file @java -jar ${SAXON} -s:${XMI_INPUT_FILE_PATH} -xsl:${MODEL2OWL_FOLDER}/src/owl-core.xsl \ - -o:${OUTPUT_FOLDER_PATH}/${XMI_INPUT_FILENAME_WITHOUT_EXTENSION}.tmp.rdf + -o:${OUTPUT_FOLDER_PATH}/${XMI_INPUT_FILENAME_WITHOUT_EXTENSION}.tmp.rdf \ + enrichedNamespacesPath="${ENRICHED_NAMESPACES_XML_PATH}" @make convert-between-serialization-formats INPUT_FORMAT=${RDF_XML_MIME_TYPE} \ OUTPUT_FORMAT=${RDF_XML_MIME_TYPE} \ FILE_PATH=${OUTPUT_FOLDER_PATH}/${XMI_INPUT_FILENAME_WITHOUT_EXTENSION}.tmp.rdf \ @@ -125,8 +130,10 @@ owl-core: @rm -f ${OUTPUT_FOLDER_PATH}/${XMI_INPUT_FILENAME_WITHOUT_EXTENSION}.tmp.rdf owl-restrictions: + @make gen-enriched-ns-file @java -jar ${SAXON} -s:${XMI_INPUT_FILE_PATH} -xsl:${MODEL2OWL_FOLDER}/src/owl-restrictions.xsl \ - -o:${OUTPUT_FOLDER_PATH}/${XMI_INPUT_FILENAME_WITHOUT_EXTENSION}_restrictions.tmp.rdf + -o:${OUTPUT_FOLDER_PATH}/${XMI_INPUT_FILENAME_WITHOUT_EXTENSION}_restrictions.tmp.rdf \ + enrichedNamespacesPath="${ENRICHED_NAMESPACES_XML_PATH}" @make convert-between-serialization-formats INPUT_FORMAT=${RDF_XML_MIME_TYPE} \ OUTPUT_FORMAT=${RDF_XML_MIME_TYPE} \ FILE_PATH=${OUTPUT_FOLDER_PATH}/${XMI_INPUT_FILENAME_WITHOUT_EXTENSION}_restrictions.tmp.rdf \ @@ -136,8 +143,10 @@ owl-restrictions: @rm -f ${OUTPUT_FOLDER_PATH}/${XMI_INPUT_FILENAME_WITHOUT_EXTENSION}_restrictions.tmp.rdf shacl: + @make gen-enriched-ns-file @java -jar ${SAXON} -s:${XMI_INPUT_FILE_PATH} -xsl:${MODEL2OWL_FOLDER}/src/shacl-shapes.xsl \ - -o:${OUTPUT_FOLDER_PATH}/${XMI_INPUT_FILENAME_WITHOUT_EXTENSION}_shapes.tmp.rdf + -o:${OUTPUT_FOLDER_PATH}/${XMI_INPUT_FILENAME_WITHOUT_EXTENSION}_shapes.tmp.rdf \ + enrichedNamespacesPath="${ENRICHED_NAMESPACES_XML_PATH}" @make convert-between-serialization-formats INPUT_FORMAT=${RDF_XML_MIME_TYPE} \ OUTPUT_FORMAT=${RDF_XML_MIME_TYPE} \ FILE_PATH=${OUTPUT_FOLDER_PATH}/${XMI_INPUT_FILENAME_WITHOUT_EXTENSION}_shapes.tmp.rdf \ @@ -146,6 +155,12 @@ shacl: @ls -lh ${OUTPUT_FOLDER_PATH}/${XMI_INPUT_FILENAME_WITHOUT_EXTENSION}_shapes.rdf @rm -f ${OUTPUT_FOLDER_PATH}/${XMI_INPUT_FILENAME_WITHOUT_EXTENSION}_shapes.tmp.rdf +# Generate enriched namespaces XML file which contains user namespaces (defined +# in namespaces.xml) and internal namespaces (such as core-shape) +gen-enriched-ns-file: + @mkdir -p ${INTERM_FOLDER_PATH} + @java -jar ${SAXON} -s:${NAMESPACES_USER_XML_FILE_PATH} -xsl:${MODEL2OWL_FOLDER}/src/xml/enriched-namespaces.xsl \ + -o:${ENRICHED_NAMESPACES_XML_PATH} # Combine xmi UML files # all files for combine should be in test/test-multi-xmi (or in XMI_MERGED_OUTPUT_FOLDER_PATH) @@ -220,10 +235,11 @@ convert-rdf-to-rdf: # OUTPUT_FILE_PATH: Path for the output file # INPUT_FORMAT: a MIME type of the given input RDF file # OUTPUT_FORMAT: a MIME type of any of the valid RDF serializations -# USE_NAMESPACES: optional; if non-empty then namespaces (from the namespaces.xml file). -# This can be used if the input (FILE_PATH) doesn't include -# namespaces we want to be applied (e.g. to have compact -# instead of full URIs in the output file). +# USE_NAMESPACES: optional; if non-empty then namespaces (from the +# enriched-namespaces.xml file). This can be used if the input +# (FILE_PATH) doesn't include namespaces we want to be applied +# (e.g. to have compact instead of full URIs in the output +# file). # # Supported MIME types: https://rdflib.readthedocs.io/en/7.0.0/plugin_serializers.html # diff --git a/scripts/get_namespaces.sh b/scripts/get_namespaces.sh index a08e37ec..f22d1d99 100755 --- a/scripts/get_namespaces.sh +++ b/scripts/get_namespaces.sh @@ -1,10 +1,12 @@ #!/bin/bash # -# Gets namespaces from an XML file with the model2owl project namespaces -# (`namespaces.xml` by default) and prepares argument list from them to be -# used with `rdfpipe` tool. Uses Saxon installed in the project main directory. +# Gets namespaces from an XML file with the model2owl project namespaces and +# prepares argument list from them to be used with `rdfpipe` tool. Uses Saxon +# installed in the project main directory. # # USAGE: get_namespaces.sh NAMESPACES_XML_FILE_PATH +PROJECT_DIR=$(dirname $(dirname $(realpath ${BASH_SOURCE[0]}))) +SAXON=${PROJECT_DIR}/saxon/saxon.jar if [ -z "$1" ]; then echo "ERROR: path to *.xml file with namespaces not given." @@ -17,7 +19,7 @@ namespaces_file_name=$(basename $namespaces_file_path) cd ${namespaces_file_dir} namespaces=$( - java -cp ../../saxon/saxon.jar net.sf.saxon.Query -s:${namespaces_file_name} \ + java -cp $SAXON net.sf.saxon.Query -s:${namespaces_file_name} \ -qs:'for $x in /*:prefixes/*:prefix return concat(string($x/@name), "=", string($x/@value))' \ \!method=text ) diff --git a/src/common/checkers.xsl b/src/common/checkers.xsl index b6ad192a..982063e7 100644 --- a/src/common/checkers.xsl +++ b/src/common/checkers.xsl @@ -512,15 +512,15 @@ - This function will check if a given list of namespaces are defined in - namespaces.xml file. If not all the namespaces were defined it will return a list with - those namespaces + This function will check if a given list of namespaces are + defined in enriched-namespaces.xml file. If not all the namespaces were + defined it will return a list with those namespaces + select="($internalNamespacePrefixes/*:prefixes/*:prefix/@name)"/> + + + Lookup a data-type in the xsd and rdf accepted data-type document (usually an external file with xsd and rdf data-types definitions) and return false or the data-type @@ -61,7 +68,7 @@ - + + + + + + diff --git a/src/owl-core.xsl b/src/owl-core.xsl index a2fb4041..f4d029be 100644 --- a/src/owl-core.xsl +++ b/src/owl-core.xsl @@ -46,9 +46,7 @@ - - - + @@ -63,7 +61,7 @@ - + diff --git a/src/owl-restrictions.xsl b/src/owl-restrictions.xsl index 22772ac9..fbcf6b2c 100644 --- a/src/owl-restrictions.xsl +++ b/src/owl-restrictions.xsl @@ -40,11 +40,7 @@ - - - - - + @@ -58,7 +54,7 @@ - + diff --git a/src/shacl-shapes.xsl b/src/shacl-shapes.xsl index 00c0349c..7892e87a 100644 --- a/src/shacl-shapes.xsl +++ b/src/shacl-shapes.xsl @@ -44,13 +44,7 @@ - - - - - - - + @@ -62,7 +56,7 @@ - + diff --git a/src/xml/enriched-namespaces.xsl b/src/xml/enriched-namespaces.xsl new file mode 100644 index 00000000..b3d9c88e --- /dev/null +++ b/src/xml/enriched-namespaces.xsl @@ -0,0 +1,36 @@ + + + + + + + + + A template for generating enriched namespaces XML file that + contains internal model2owl namespaces which are constructed + based on the model2owl configuration. + + + + + + + + + + + + + + + + + + diff --git a/test/unitTests/test-xml/test-enriched-namespaces.xspec b/test/unitTests/test-xml/test-enriched-namespaces.xspec new file mode 100644 index 00000000..ba778260 --- /dev/null +++ b/test/unitTests/test-xml/test-enriched-namespaces.xspec @@ -0,0 +1,18 @@ + + + + + + + + + + +