From 24d3ecf35012691edc1ff668a735b88439083ee4 Mon Sep 17 00:00:00 2001 From: Florian Kleedorfer Date: Tue, 12 Mar 2024 09:53:52 +0100 Subject: [PATCH] Improve SN/decimal values generation --- CHANGELOG.md | 1 + ...NumericValuesAsXsdDoubleAndXsdDecimal.java | 84 +++++++++++++++++-- 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d426de3..b5f1731 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Add model classes PhysicalConstant and ConstantValue, populate from data. +- Add tool for generating the decimal/scientific values for QUDT ## [6.4.3] - 2024-02-20 diff --git a/qudtlib-tools/src/main/java/io/github/qudtlib/tools/contributions/GenerateNumericValuesAsXsdDoubleAndXsdDecimal.java b/qudtlib-tools/src/main/java/io/github/qudtlib/tools/contributions/GenerateNumericValuesAsXsdDoubleAndXsdDecimal.java index 9f1ac58..b3ac98b 100644 --- a/qudtlib-tools/src/main/java/io/github/qudtlib/tools/contributions/GenerateNumericValuesAsXsdDoubleAndXsdDecimal.java +++ b/qudtlib-tools/src/main/java/io/github/qudtlib/tools/contributions/GenerateNumericValuesAsXsdDoubleAndXsdDecimal.java @@ -1,11 +1,14 @@ package io.github.qudtlib.tools.contributions; +import static java.util.stream.Collectors.joining; + import io.github.qudtlib.Qudt; +import io.github.qudtlib.model.Namespace; import io.github.qudtlib.model.QudtNamespaces; import io.github.qudtlib.tools.contribute.QudtEntityGenerator; import io.github.qudtlib.vocab.QUDT; import java.math.BigDecimal; -import java.util.Optional; +import java.util.*; import org.eclipse.rdf4j.model.IRI; import org.eclipse.rdf4j.model.Model; import org.eclipse.rdf4j.model.Value; @@ -16,6 +19,8 @@ public class GenerateNumericValuesAsXsdDoubleAndXsdDecimal { + private static Map> unitsByReplacedPredicate = new HashMap<>(); + public static void main(String[] args) { QudtEntityGenerator generator = new QudtEntityGenerator(); Model addedStatements = new TreeModel(); @@ -32,7 +37,8 @@ public static void main(String[] args) { val.get(), QUDT.conversionMultiplier, QUDT.conversionMultiplierSN, - addedStatements); + addedStatements, + unitsByReplacedPredicate); } val = unit.getConversionOffset(); if (val.isPresent()) { @@ -41,7 +47,8 @@ public static void main(String[] args) { val.get(), QUDT.conversionOffset, QUDT.conversionOffsetSN, - addedStatements); + addedStatements, + unitsByReplacedPredicate); } }); tool.writeOut(addedStatements, System.out); @@ -61,7 +68,8 @@ public static void main(String[] args) { constantValue.getValue(), QUDT.value, QUDT.valueSN, - addedStatements); + addedStatements, + unitsByReplacedPredicate); Optional val = constantValue.getStandardUncertainty(); if (val.isPresent()) { @@ -70,16 +78,30 @@ public static void main(String[] args) { val.get(), QUDT.standardUncertainty, QUDT.standardUncertaintySN, - addedStatements); + addedStatements, + unitsByReplacedPredicate); } }); tool.writeOut(addedStatements, System.out); }); + + printDeleteQuery(unitsByReplacedPredicate); } private static void addNewNumericProperty( - String iri, BigDecimal value, IRI property, IRI propertySN, Model addedStatements) { + String iri, + BigDecimal value, + IRI property, + IRI propertySN, + Model addedStatements, + Map> unitsByReplacedPredicate) { ValueFactory vf = SimpleValueFactory.getInstance(); + Collection units = unitsByReplacedPredicate.get(property); + if (units == null) { + units = new HashSet<>(); + unitsByReplacedPredicate.put(property, units); + } + units.add(iri); addedStatements.add(vf.createIRI(iri), property, vf.createLiteral(value)); addedStatements.add(vf.createIRI(iri), propertySN, createDoubleLiteral(value)); } @@ -88,4 +110,54 @@ private static Value createDoubleLiteral(BigDecimal val) { ValueFactory vf = SimpleValueFactory.getInstance(); return vf.createLiteral(val.toString(), CoreDatatype.XSD.DOUBLE); } + + private static void printDeleteQuery(Map> unitsByReplacedPredicate) { + for (Map.Entry> entry : unitsByReplacedPredicate.entrySet()) { + IRI predicate = entry.getKey(); + Collection subjects = entry.getValue(); + System.out.println("\n\n\nSTATEMENTS TO DELETE:\n"); + System.out.println("PREFIX qudt: "); + System.out.println("PREFIX unit: "); + System.out.format( + "PREFIX %s: <%s>\n", + Qudt.NAMESPACES.constant.getAbbreviationPrefix(), + Qudt.NAMESPACES.constant.getBaseIri()); + System.out.format( + "PREFIX %s: <%s>\n", + Qudt.NAMESPACES.currency.getAbbreviationPrefix(), + Qudt.NAMESPACES.currency.getBaseIri()); + System.out.format("DELETE { ?u %s ?m } \n", predicate); + System.out.format("WHERE { ?u %s ?m .\n", predicate); + System.out.println("\tVALUES ?u {"); + System.out.println( + subjects.stream() + .map(iri -> abbreviateIfPossible(iri)) + .sorted() + .collect(joining("\n\t\t", "\n\t\t", ""))); + System.out.println("\t}\n}\n\n\n"); + } + } + + private static String abbreviateIfPossible(String iri) { + String abbrev = abbreviateIfPossible(iri, Qudt.NAMESPACES.unit); + if (abbrev != null) { + return abbrev; + } + abbrev = abbreviateIfPossible(iri, Qudt.NAMESPACES.constant); + if (abbrev != null) { + return abbrev; + } + abbrev = abbreviateIfPossible(iri, Qudt.NAMESPACES.currency); + if (abbrev != null) { + return abbrev; + } + return iri; + } + + private static String abbreviateIfPossible(String iri, Namespace namespace) { + if (namespace.isFullNamespaceIri(iri)) { + return namespace.abbreviate(iri); + } + return null; + } }