Skip to content

Commit

Permalink
Add PhysicalConstant and ConstantValue
Browse files Browse the repository at this point in the history
  • Loading branch information
fkleedorfer committed Mar 4, 2024
1 parent 87d9228 commit 30fa6d0
Show file tree
Hide file tree
Showing 19 changed files with 904 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add model classes PhysicalConstant and ConstantValue, populate from data.

## [6.4.3] - 2024-02-20

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ public class ConstantsGenerator {
private static final String DATA_QUANTITYKINDS = "qudtlib/qudt-quantitykinds.ttl";
private static final String DATA_PREFIXES = "qudtlib/qudt-prefixes.ttl";
private static final String DATA_SYSTEMS_OF_UNITS = "qudtlib/qudt-systems-of-units.ttl";
private static final String DATA_PHYSICAL_CONSTANTS = "qudtlib/qudt-constants.ttl";
// queries
private static final String QUERY_UNITS = "query/units.rq";
private static final String QUERY_QUANTITYKINDS = "query/quantitykinds.rq";
private static final String QUERY_PREFIXES = "query/prefixes.rq";
private static final String QUERY_SYSTEMS_OF_UNITS = "query/systems-of-units.rq";
private static final String QUERY_PHYSICAL_CONSTANTS = "query/physicalConstants.rq";
// output
private static final String DESTINATION_PACKAGE = "io.github.qudtlib.model";
// template
Expand Down Expand Up @@ -72,6 +74,7 @@ public void generate() throws IOException, TemplateException {
generateQuantityKindConstants(cfg);
generatePrefixConstants(cfg);
generateSystemOfUnitsConstants(cfg);
generatePhysicalConstantConstants(cfg);
}

private void generatePrefixConstants(Configuration config)
Expand Down Expand Up @@ -99,6 +102,13 @@ private void generateSystemOfUnitsConstants(Configuration config)
generateJavaFile(config, templateVars, "SystemOfUnits", "SystemsOfUnits");
}

private void generatePhysicalConstantConstants(Configuration config)
throws IOException, TemplateException {
Map<String, Object> templateVars =
getConstantNamesByQuery(QUERY_PHYSICAL_CONSTANTS, DATA_PHYSICAL_CONSTANTS);
generateJavaFile(config, templateVars, "PhysicalConstant", "PhysicalConstants");
}

private Map<String, Object> getConstantNamesByQuery(String queryFile, String dataFile) {
String queryStr = RdfOps.loadQuery(queryFile);
Repository repo = new SailRepository(new MemoryStore());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
PREFIX qudt: <http://qudt.org/schema/qudt/>
PREFIX unit: <http://qudt.org/vocab/unit/>
PREFIX constant: <http://qudt.org/vocab/constant/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT DISTINCT ?constName ?localName ?label (?qk as ?iri) ("PhysicalConstant" as ?typeName) ?symbol
where
{
?qk a qudt:PhysicalConstant .
{
select ?qk (MIN(STR(?labelVal)) as ?label) where {
?qk a qudt:PhysicalConstant .
optional {
?qk rdfs:label ?labelVal .
FILTER(LANG(?labelVal) = "en")
}
} GROUP BY ?qk ?symbol
}
{
select ?qk (GROUP_CONCAT(STR(?smb); separator=" or ") as ?symbol) where {
?qk a qudt:PhysicalConstant .
optional {
?qk qudt:symbol ?smb .
}
} GROUP BY ?qk
}
BIND(REPLACE(STR(?qk), "^.+/", "") as ?localName)
BIND(REPLACE(?localName, "-", "__") as ?constName)
} order by ?qk
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class DataGenerator {
private static final String PREFIXES_OUTFILE = "qudt-prefixes.ttl";
private static final String QUANTITYKINDS_OUTFILE = "qudt-quantitykinds.ttl";
private static final String SYSTEMS_OF_UNITS_OUTFILE = "qudt-systems-of-units.ttl";
private static final String CONSTANTS_OUTFILE = "qudt-constants.ttl";

// QUDT files
private static final String UNITS_FILE = "qudt/vocab/unit/VOCAB_QUDT-UNITS-ALL-v2.1.ttl";
Expand All @@ -40,6 +41,9 @@ public class DataGenerator {
private static final String QUANTITYKINDS_FILE =
"qudt/vocab/quantitykinds/VOCAB_QUDT-QUANTITY-KINDS-ALL-v2.1.ttl";

private static final String CONSTANTS_FILE =
"qudt/vocab/constants/VOCAB_QUDT-CONSTANTS-v2.1.ttl";

// queries
private static final String FACTOR_UNITS_QUERY = "factorUnit.rq";
private static final String IS_SCALING_OF_QUERY = "isScalingOf.rq";
Expand Down Expand Up @@ -95,6 +99,7 @@ public static void main(String[] args) {
generator.processPrefixes();
generator.processQuantityKinds();
generator.processSystemsOfUnits();
generator.processConstants();
RdfOps.message("Done generating qudtlib-flavored TTL files");
}

Expand All @@ -107,6 +112,15 @@ private void processSystemsOfUnits() {
}
}

private void processConstants() {
RdfOps.message("Processing QUDT Constants");
Repository outputRepo = new SailRepository(new MemoryStore());
try (RepositoryConnection outputCon = outputRepo.getConnection()) {
RdfOps.addStatementsFromFile(outputCon, CONSTANTS_FILE);
RdfOps.writeTurtleFile(outputCon, outFile(CONSTANTS_OUTFILE));
}
}

private void processPrefixes() {
RdfOps.message("Processing QUDT prefixes");
Repository outputRepo = new SailRepository(new MemoryStore());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ private void generateInitializer(Configuration config) throws IOException, Templ
templateVars.put("quantityKinds", new TreeMap<>(Qudt.getQuantityKindsMap()));
templateVars.put("units", new TreeMap<>(Qudt.getUnitsMap()));
templateVars.put("systemsOfUnits", new TreeMap<>(Qudt.getSystemsOfUnitsMap()));
templateVars.put("constantValues", new TreeMap<>(Qudt.getConstantValuesMap()));
templateVars.put("physicalConstants", new TreeMap<>(Qudt.getPhysicalConstantsMap()));
generateJavaFile(config, templateVars);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class InitializerImpl implements Initializer {
populatePrefixDefinitions(definitions);
populateFactorUnits(definitions);
populateSystemOfUnitsDefinitions(definitions);
populateConstantValueDefinitions(definitions);
populatePhysicalConstantDefinitions(definitions);
return definitions;
}

Expand Down Expand Up @@ -222,5 +224,39 @@ public class InitializerImpl implements Initializer {
definitions.addSystemOfUnitsDefinition(def);
</#list>
}

private void populateConstantValueDefinitions(Definitions definitions) {
ConstantValue.Definition def = null;
<#list constantValues as iri, cv>
def = ConstantValue.definition(${q(iri)})
<#list cv.labels as label>
.addLabel(new LangString(${q(label.string)}, ${optStr(label.languageTag)}))
</#list>
<#if cv.isDeprecated()>
.deprecated(true)
</#if>
.value(${bigDec(cv.value)})
.standardUncertainty((BigDecimal) ${optBigDec(cv.standardUncertainty)})
.unit(definitions.expectUnitDefinition(${q(cv.unit.iri)}));
definitions.addConstantValueDefinition(def);
</#list>
}


private void populatePhysicalConstantDefinitions(Definitions definitions) {
PhysicalConstant.Definition def = null;
<#list physicalConstants as iri, pc>
def = PhysicalConstant.definition(${q(iri)})
<#list pc.labels as label>
.addLabel(new LangString(${q(label.string)}, ${optStr(label.languageTag)}))
</#list>
<#if pc.isDeprecated()>
.deprecated(true)
</#if>
.constantValue(definitions.expectConstantValueDefinition(${q(pc.constantValue.iri)}))
.quantityKind(definitions.expectQuantityKindDefinition(${q(pc.quantityKind.iri)}));
definitions.addPhysicalConstantDefinition(def);
</#list>
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public class InitializerImpl implements Initializer {
private final String queryLoadFactorUnits;
private final String queryLoadSystemsOfUnits;

private final String queryLoadConstantValue;

private final String queryLoadPhysicalConstant;

public InitializerImpl() {
Model m = loadQudtModel();
qudtRepository = new SailRepository(new MemoryStore());
Expand All @@ -59,6 +63,8 @@ public InitializerImpl() {
queryLoadPrefix = loadQuery("qudtlib/query/prefix.rq");
queryLoadFactorUnits = loadQuery("qudtlib/query/factor-units.rq");
queryLoadSystemsOfUnits = loadQuery("qudtlib/query/system-of-units.rq");
queryLoadConstantValue = loadQuery("qudtlib/query/constantValue.rq");
queryLoadPhysicalConstant = loadQuery("qudtlib/query/physicalConstant.rq");
}

@Override
Expand All @@ -70,6 +76,8 @@ public Definitions loadData() {
populatePrefixDefinitions(con, definitions);
populateFactorUnits(con, definitions);
populateSystemOfUnitsDefinitions(con, definitions);
populateConstantValues(con, definitions);
populatePhysicalConstants(con, definitions);
}
return definitions;
}
Expand Down Expand Up @@ -320,6 +328,93 @@ private void populateFactorUnits(RepositoryConnection con, Definitions definitio
}
}

private void populateConstantValues(RepositoryConnection con, Definitions definitions) {
TupleQuery query = con.prepareTupleQuery(queryLoadConstantValue);
try (TupleQueryResult result = query.evaluate()) {
Iterator<BindingSet> solutions = result.iterator();
ConstantValue.Definition constantValueDef = null;
while (solutions.hasNext()) {
BindingSet bs = solutions.next();
String currentIri = bs.getValue("constantValue").stringValue();
if (constantValueDef != null && !constantValueDef.getId().equals(currentIri)) {
definitions.addConstantValueDefinition(constantValueDef);
constantValueDef = null;
}
if (constantValueDef == null) {
constantValueDef = makeConstantValueBuilder(bs, definitions);
}
constantValueDef
.standardUncertainty(
(String)
getIfPresent(
bs, "standardUncertainty", v -> v.stringValue()))
.deprecated(
Optional.ofNullable(
getIfPresent(
bs,
"deprecated",
v -> ((Literal) v).booleanValue()))
.orElse(false))
.addLabel(
(LangString)
getIfPresent(
bs,
"label",
v ->
new LangString(
((Literal) v).getLabel(),
((Literal) v)
.getLanguage()
.orElse(null))));
}
if (constantValueDef != null) {
definitions.addConstantValueDefinition(constantValueDef);
}
}
}

private void populatePhysicalConstants(RepositoryConnection con, Definitions definitions) {
TupleQuery query = con.prepareTupleQuery(queryLoadPhysicalConstant);
try (TupleQueryResult result = query.evaluate()) {
Iterator<BindingSet> solutions = result.iterator();
PhysicalConstant.Definition physicalConstantDef = null;
while (solutions.hasNext()) {
BindingSet bs = solutions.next();
String currentIri = bs.getValue("physicalConstant").stringValue();
if (physicalConstantDef != null
&& !physicalConstantDef.getId().equals(currentIri)) {
definitions.addPhysicalConstantDefinition(physicalConstantDef);
physicalConstantDef = null;
}
if (physicalConstantDef == null) {
physicalConstantDef = makePhysicalConstantBuilder(bs, definitions);
}
physicalConstantDef
.deprecated(
Optional.ofNullable(
getIfPresent(
bs,
"deprecated",
v -> ((Literal) v).booleanValue()))
.orElse(false))
.addLabel(
(LangString)
getIfPresent(
bs,
"label",
v ->
new LangString(
((Literal) v).getLabel(),
((Literal) v)
.getLanguage()
.orElse(null))));
}
if (physicalConstantDef != null) {
definitions.addPhysicalConstantDefinition(physicalConstantDef);
}
}
}

private String loadQuery(String queryFile) {
try (InputStream in =
Thread.currentThread().getContextClassLoader().getResourceAsStream(queryFile)) {
Expand All @@ -341,6 +436,7 @@ private Model loadQudtModel() {
loadTtlFile("qudtlib/qudt-quantitykinds.ttl", parser);
loadTtlFile("qudtlib/qudt-units.ttl", parser);
loadTtlFile("qudtlib/qudt-systems-of-units.ttl", parser);
loadTtlFile("qudtlib/qudt-constants.ttl", parser);
return model;
}

Expand Down Expand Up @@ -404,6 +500,24 @@ private static QuantityKind.Definition makeQuantityKindBuilder(BindingSet bs) {
.symbol(getIfPresent(bs, "symbol", Value::stringValue));
}

private static ConstantValue.Definition makeConstantValueBuilder(
BindingSet bs, Definitions definitions) {
return ConstantValue.definition(bs.getValue("constantValue").stringValue())
.value(bs.getValue("value").stringValue())
.unit(definitions.expectUnitDefinition(bs.getValue("unit").stringValue()));
}

private static PhysicalConstant.Definition makePhysicalConstantBuilder(
BindingSet bs, Definitions definitions) {
return PhysicalConstant.definition(bs.getValue("physicalConstant").stringValue())
.constantValue(
definitions.expectConstantValueDefinition(
bs.getValue("quantityValue").stringValue()))
.quantityKind(
definitions.expectQuantityKindDefinition(
bs.getValue("quantityKind").stringValue()));
}

private static Prefix.Definition makePrefixBuilder(BindingSet bs) {
return Prefix.definition(bs.getValue("prefix").stringValue())
.multiplier(
Expand Down
18 changes: 18 additions & 0 deletions qudtlib-init-rdf/src/main/resources/qudtlib/query/constantValue.rq
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
PREFIX qudt: <http://qudt.org/schema/qudt/>
PREFIX unit: <http://qudt.org/vocab/unit/>
PREFIX kind: <http://qudt.org/vocab/quantitykind/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT * where
{
?constantValue
a qudt:ConstantValue ;
qudt:hasUnit ?unit ;
qudt:value ?value .
OPTIONAL {
?constantValue qudt:standardUncertainty ?standardUncertainty ;
}
OPTIONAL {
?constantValue qudt:deprecated ?deprecated ;
}
} order by ?constantValue
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
PREFIX qudt: <http://qudt.org/schema/qudt/>
PREFIX unit: <http://qudt.org/vocab/unit/>
PREFIX kind: <http://qudt.org/vocab/quantitykind/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT * where
{
?physicalConstant
a qudt:PhysicalConstant ;
qudt:hasQuantityKind ?quantityKind ;
qudt:quantityValue ?quantityValue ;
rdfs:label ?label .
OPTIONAL {
?physicalConstant qudt:deprecated ?deprecated ;
}
} order by ?physicalConstant
2 changes: 1 addition & 1 deletion qudtlib-js
Submodule qudtlib-js updated 1 files
+24,481 −47,116 allunits/src/units.ts
Loading

0 comments on commit 30fa6d0

Please sign in to comment.