-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TS code generation and Refactor Code (#16)
* Extracts some code used for java/js generation into new module * Prepares a first version of the typescript generator (incorrect result for now). * Adds qudtlib-js as a submodule * Fixes typo in package name * Refactors constants generation * Generate units, quantitykinds and prefixes in allunits.ts * Fixes Bug in factor unit queries: * overspecification was possible * simple units could not be selected using exponent 1 * Both issues are fixed and checked with unit tests. * Improves factor unit matching algorithm * Simplifies matched check * Removes unused code * Removes unnecessary isMatched check * Removes unnecessary method * Enables exact and lenient matching modes * Refactors unit/qk/prefix access methods to return optional * Adds 'required' unit/qk/prefix access methods that throw NotFoundExceptions * Adds tests for search/maching modes * Removes cluttering convenience methods * Refactors Qudt.java and tests slightly * Add scalingOf triples if missing in QUDT * Updates submodule to use latest qudtlib-js/main
- Loading branch information
1 parent
2102f96
commit f9eeaea
Showing
42 changed files
with
1,487 additions
and
722 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[submodule "qudtlib-js"] | ||
path = qudtlib-js | ||
url = https://github.com/qudtlib/qudtlib-js.git | ||
branch = main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>qudtlib-java</artifactId> | ||
<groupId>io.github.qudtlib</groupId> | ||
<version>1.1-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>qudtlib-common-codegen</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.freemarker</groupId> | ||
<artifactId>freemarker</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.github.qudtlib</groupId> | ||
<artifactId>qudtlib-model</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
39 changes: 39 additions & 0 deletions
39
...lib-common-codegen/src/main/java/io/github/qudtlib/common/BigDecimalFormatterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.github.qudtlib.common; | ||
|
||
import freemarker.core.Environment; | ||
import freemarker.core.TemplateNumberFormat; | ||
import freemarker.core.TemplateNumberFormatFactory; | ||
import freemarker.template.TemplateModelException; | ||
import freemarker.template.TemplateNumberModel; | ||
import java.math.BigDecimal; | ||
import java.util.Locale; | ||
|
||
public class BigDecimalFormatterFactory extends TemplateNumberFormatFactory { | ||
@Override | ||
public TemplateNumberFormat get(String s, Locale locale, Environment environment) { | ||
return new TemplateNumberFormat() { | ||
@Override | ||
public String formatToPlainText(TemplateNumberModel templateNumberModel) | ||
throws TemplateModelException { | ||
Number num = templateNumberModel.getAsNumber(); | ||
if (!(num instanceof BigDecimal)) { | ||
throw new IllegalArgumentException( | ||
"This formatter can only be used with BigDecimals but was asked to format a " | ||
+ num.getClass()); | ||
} | ||
BigDecimal bd = (BigDecimal) templateNumberModel.getAsNumber(); | ||
return bd.toString(); | ||
} | ||
|
||
@Override | ||
public boolean isLocaleBound() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Number format for BigDecimal using BigDecimal.toString()"; | ||
} | ||
}; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
qudtlib-common-codegen/src/main/java/io/github/qudtlib/common/CodeGen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.github.qudtlib.common; | ||
|
||
import freemarker.core.Environment; | ||
import freemarker.template.*; | ||
import io.github.qudtlib.common.safenames.SafeStringMapper; | ||
import io.github.qudtlib.constgen.Constant; | ||
import io.github.qudtlib.model.LangString; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.regex.Pattern; | ||
|
||
public abstract class CodeGen { | ||
|
||
public static Configuration getFreemarkerConfiguration() { | ||
return getFreemarkerConfiguration(Thread.currentThread().getContextClassLoader()); | ||
} | ||
|
||
public static Configuration getFreemarkerConfiguration(ClassLoader classLoaderForTemplate) { | ||
Objects.requireNonNull(classLoaderForTemplate); | ||
Configuration cfg = new Configuration(Configuration.VERSION_2_3_31); | ||
cfg.setClassLoaderForTemplateLoading(classLoaderForTemplate, "/"); | ||
cfg.setDefaultEncoding("UTF-8"); | ||
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); | ||
cfg.setCustomNumberFormats(Map.of("toString", new BigDecimalFormatterFactory())); | ||
return cfg; | ||
} | ||
|
||
public static void generateFileFromTemplate( | ||
Configuration config, | ||
String templateFileClasspathUrl, | ||
Map<String, Object> templateVars, | ||
File outFile) | ||
throws IOException, TemplateException { | ||
Template template = config.getTemplate(templateFileClasspathUrl); | ||
FileWriter out = new FileWriter(outFile, StandardCharsets.UTF_8); | ||
Environment env = template.createProcessingEnvironment(templateVars, out); | ||
env.setOutputEncoding(StandardCharsets.UTF_8.toString()); | ||
env.process(); | ||
} | ||
|
||
public static Constant makeConstant( | ||
Set<LangString> labels, String iri, SafeStringMapper constantNameMapper) { | ||
String label = labels.stream().findFirst().map(LangString::getString).orElse("[no label]"); | ||
String iriLocalName = iri.replaceAll("^.+[/|#]", ""); | ||
String codeConstantName = constantNameMapper.applyMapping(iriLocalName); | ||
return new Constant(codeConstantName, iriLocalName, label); | ||
} | ||
|
||
public static SafeStringMapper javaConstantMapper() { | ||
return new SafeStringMapper(javaConstantNameMapper); | ||
} | ||
|
||
static final Function<String, String> javaConstantNameMapper = | ||
constName -> { | ||
Pattern startPattern = Pattern.compile("^[$€a-zA-Z_]"); | ||
if (!startPattern.matcher(constName).lookingAt()) { | ||
constName = "_" + constName; | ||
} | ||
constName = constName.replaceAll("-", "__"); | ||
return constName; | ||
}; | ||
} |
10 changes: 10 additions & 0 deletions
10
...mmon-codegen/src/main/java/io/github/qudtlib/common/safenames/NameCollisionException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.github.qudtlib.common.safenames; | ||
|
||
public class NameCollisionException extends RuntimeException { | ||
public NameCollisionException(String collidingInput, String input, String mappedOutput) { | ||
super( | ||
String.format( | ||
"IdentifierCollision detected! Input String '%s' clashes with previously established Mapping '%s' => '%s'", | ||
collidingInput, input, mappedOutput)); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...lib-common-codegen/src/main/java/io/github/qudtlib/common/safenames/SafeStringMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.github.qudtlib.common.safenames; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Applies the provided <b>deterministic</b> </b><code>mappingFunction</code> provided in the | ||
* constructor, keeping track of the mappings produced. If two different inputs result in the same | ||
* output, a {@link NameCollisionException} is thrown. | ||
*/ | ||
public class SafeStringMapper { | ||
private final Map<String, String> outputToInput = new ConcurrentHashMap<>(); | ||
private final Function<String, String> mappingFunction; | ||
|
||
public SafeStringMapper(Function<String, String> mappingFunction) { | ||
this.mappingFunction = mappingFunction; | ||
} | ||
|
||
/** | ||
* Applies the specified mapping, throwing an exception if a previously produced output is | ||
* reproduced with a different input. | ||
* | ||
* @param input the value to map safely | ||
* @return the mapped value | ||
*/ | ||
public String applyMapping(String input) { | ||
Objects.requireNonNull(input); | ||
final String output = mappingFunction.apply(input); | ||
outputToInput.merge( | ||
output, | ||
input, | ||
(previousInput, currentInput) -> { | ||
if (previousInput.equals(currentInput)) { | ||
return input; | ||
} | ||
throw new NameCollisionException(input, previousInput, output); | ||
}); | ||
return output; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
qudtlib-common-codegen/src/main/java/io/github/qudtlib/constgen/Constant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.github.qudtlib.constgen; | ||
|
||
/** | ||
* Class representing constant names/labels/local names in IRIs for generating RDF vocabularies. | ||
* | ||
* @author Florian Kleedorfer | ||
* @since 1.0 | ||
*/ | ||
public class Constant { | ||
private final String codeConstantName; | ||
private final String iriLocalname; | ||
private final String label; | ||
|
||
public Constant(String codeConstantName, String iriLocalname, String label) { | ||
this.codeConstantName = codeConstantName; | ||
this.iriLocalname = iriLocalname; | ||
this.label = label; | ||
} | ||
|
||
public String getCodeConstantName() { | ||
return codeConstantName; | ||
} | ||
|
||
public String getIriLocalname() { | ||
return iriLocalname; | ||
} | ||
|
||
public String getLabel() { | ||
return label; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
qudtlib-common-codegen/src/test/java/io/github/qudtlib/common/CodeGenTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.github.qudtlib.common; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import io.github.qudtlib.common.safenames.NameCollisionException; | ||
import io.github.qudtlib.common.safenames.SafeStringMapper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class CodeGenTest { | ||
@Test | ||
public void testConstantMapper() { | ||
SafeStringMapper mapper = CodeGen.javaConstantMapper(); | ||
assertEquals("_1constant", mapper.applyMapping("1constant")); | ||
assertEquals("_1constant", mapper.applyMapping("1constant")); | ||
assertEquals("_2constant", mapper.applyMapping("2constant")); | ||
assertThrows(NameCollisionException.class, () -> mapper.applyMapping("_1constant")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 0 additions & 31 deletions
31
qudtlib-constants-gen/src/main/java/io/github/qudtlib/constgen/Constant.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.