Skip to content

Commit

Permalink
Improve SN/decimal values generation
Browse files Browse the repository at this point in the history
  • Loading branch information
fkleedorfer committed Mar 14, 2024
1 parent 8e94938 commit 24d3ecf
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,6 +19,8 @@

public class GenerateNumericValuesAsXsdDoubleAndXsdDecimal {

private static Map<IRI, Collection<String>> unitsByReplacedPredicate = new HashMap<>();

public static void main(String[] args) {
QudtEntityGenerator generator = new QudtEntityGenerator();
Model addedStatements = new TreeModel();
Expand All @@ -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()) {
Expand All @@ -41,7 +47,8 @@ public static void main(String[] args) {
val.get(),
QUDT.conversionOffset,
QUDT.conversionOffsetSN,
addedStatements);
addedStatements,
unitsByReplacedPredicate);
}
});
tool.writeOut(addedStatements, System.out);
Expand All @@ -61,7 +68,8 @@ public static void main(String[] args) {
constantValue.getValue(),
QUDT.value,
QUDT.valueSN,
addedStatements);
addedStatements,
unitsByReplacedPredicate);
Optional<BigDecimal> val =
constantValue.getStandardUncertainty();
if (val.isPresent()) {
Expand All @@ -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<IRI, Collection<String>> unitsByReplacedPredicate) {
ValueFactory vf = SimpleValueFactory.getInstance();
Collection<String> 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));
}
Expand All @@ -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<IRI, Collection<String>> unitsByReplacedPredicate) {
for (Map.Entry<IRI, Collection<String>> entry : unitsByReplacedPredicate.entrySet()) {
IRI predicate = entry.getKey();
Collection<String> subjects = entry.getValue();
System.out.println("\n\n\nSTATEMENTS TO DELETE:\n");
System.out.println("PREFIX qudt: <http://qudt.org/schema/qudt/>");
System.out.println("PREFIX unit: <http://qudt.org/vocab/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;
}
}

0 comments on commit 24d3ecf

Please sign in to comment.