Skip to content

Commit 39b21b2

Browse files
committed
initial commit
0 parents  commit 39b21b2

11 files changed

Lines changed: 522 additions & 0 deletions

File tree

.github/workflows/build.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# MIT licensed
2+
name: openMINDS_java_build_pipeline
3+
4+
on:
5+
push:
6+
branches:
7+
- pipeline
8+
workflow_dispatch: # This triggers the workflow when a webhook is received
9+
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
steps:
15+
16+
- name: Checkout Repository
17+
uses: actions/checkout@v3
18+
19+
- name: Set up Python 3.11
20+
uses: actions/setup-python@v2
21+
with:
22+
python-version: 3.11
23+
24+
- name: Run build
25+
run: |
26+
pip install -r requirements.txt
27+
python build.py
28+
29+
- name: Checkout main branch
30+
uses: actions/checkout@v3
31+
with:
32+
ref: main
33+
path: main
34+
token: ${{ secrets.GITHUB_TOKEN }}
35+
36+
- name: Push to main
37+
run: |
38+
cp -R target/* main
39+
cd main
40+
rm -rf build openMINDS.egg-info
41+
git config --global user.email "openminds@ebrains.eu"
42+
git config --global user.name "openMINDS pipeline"
43+
if [[ $(git add . --dry-run | wc -l) -gt 0 ]]; then
44+
git add .
45+
git commit -m "build triggered by submodule ${{ inputs.repository }} version ${{ inputs.branch }}"
46+
git push -f
47+
else
48+
echo "Nothing to commit"
49+
fi

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
sources/**
2+
target/**
3+
.idea/**
4+
src/**

build.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import os.path
2+
import shutil
3+
4+
from jinja2 import Environment, PackageLoader, select_autoescape
5+
6+
from pipeline.translator import JavaBuilder
7+
from pipeline.utils import clone_sources, SchemaLoader
8+
9+
print("***************************************")
10+
print(f"Triggering the generation of Java classes for openMINDS")
11+
print("***************************************")
12+
13+
# Step 1 - clone central repository in main branch to get the latest sources
14+
clone_sources()
15+
schema_loader = SchemaLoader()
16+
if os.path.exists("target"):
17+
shutil.rmtree("target")
18+
19+
ignored_versions = ["v1.0", "v2.0"]
20+
relevant_versions = [v for v in schema_loader.get_schema_versions() if v not in ignored_versions]
21+
22+
23+
def build_central():
24+
env = Environment(
25+
loader=PackageLoader("generator"),
26+
autoescape=select_autoescape()
27+
)
28+
template = env.get_template("OpenMINDS.java.j2")
29+
result = template.render(
30+
relevant_versions = built_versions,
31+
packages_by_version = packages_by_version
32+
)
33+
target_file = "target/src/main/java/org/openmetadatainitiative/openminds/OpenMINDS.java"
34+
os.makedirs(os.path.dirname(target_file), exist_ok=True)
35+
with open(target_file, "w") as target_file:
36+
target_file.write(result)
37+
38+
built_versions = []
39+
packages_by_version = {}
40+
41+
42+
for schema_version in relevant_versions:
43+
# Step 2 - find all involved schemas for the current version
44+
schemas_file_paths = schema_loader.find_schemas(schema_version)
45+
type_to_class_name = {}
46+
implemented_interfaces = {}
47+
builders = []
48+
for schema_file_path in schemas_file_paths:
49+
builder = JavaBuilder(schema_file_path, schema_loader.schemas_sources)
50+
if builder.version not in packages_by_version:
51+
packages_by_version[builder.version] = {}
52+
if builder.relative_path_without_extension[0] not in packages_by_version[builder.version]:
53+
packages_by_version[builder.version][builder.relative_path_without_extension[0]] = []
54+
packages_by_version[builder.version][builder.relative_path_without_extension[0]].append((builder.class_name, builder.canonical_class_name()))
55+
if builder.version not in built_versions:
56+
built_versions.append(builder.version)
57+
for property, types in builder.additional_interfaces.items():
58+
for t in types:
59+
if t not in implemented_interfaces:
60+
implemented_interfaces[t] = []
61+
implemented_interfaces[t].append(f"{builder.interface_path(property).replace('/', '.')}")
62+
type_to_class_name[builder.type]=builder.canonical_class_name()
63+
builders.append(builder)
64+
65+
66+
for builder in builders:
67+
# Step 3 - translate and build each openMINDS schema as JSON-Schema
68+
builder.build(type_to_class_name, implemented_interfaces)
69+
70+
build_central()

generator/__init__.py

Whitespace-only changes.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package org.openmetadatainitiative.openminds;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.openmetadatainitiative.openminds.utils.Instance;
5+
import org.openmetadatainitiative.openminds.utils.OpenMINDSContext;
6+
import org.openmetadatainitiative.openminds.utils.ParsingUtils;
7+
import org.openmetadatainitiative.openminds.utils.LocalId;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.util.Arrays;
12+
import java.util.List;
13+
import java.util.stream.Stream;
14+
15+
/**
16+
* ATTENTION! This is an autogenerated file based on the openMINDS schema - do not apply manual changes since they are going to be overwritten.
17+
*/
18+
public class OpenMINDS {
19+
20+
private final OpenMINDSContext context;
21+
22+
private OpenMINDS(OpenMINDSContext context){
23+
this.context = context;
24+
}
25+
26+
private static void persist(String targetDirectory, Stream<Instance> instances){
27+
File dir = new File(targetDirectory);
28+
if(!dir.exists()){
29+
dir.mkdirs();
30+
}
31+
instances.forEach(i -> {
32+
File f = new File(targetDirectory+File.separator+i.getLocalId().id()+".jsonld");
33+
try {
34+
ParsingUtils.OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValue(f, i);
35+
} catch (IOException e) {
36+
throw new RuntimeException(e);
37+
}
38+
});
39+
}
40+
41+
{% for version in relevant_versions %}
42+
public static OpenMINDS.{{version[0]|upper}}{{version[1:]}} {{version}}(){
43+
return {{version}}(OpenMINDSContext.defaultContext());
44+
}
45+
46+
public static OpenMINDS.{{version[0]|upper}}{{version[1:]}} {{version}}(String idPrefix){
47+
return {{version}}(new OpenMINDSContext(idPrefix, true));
48+
}
49+
50+
private static OpenMINDS.{{version[0]|upper}}{{version[1:]}} {{version}}(OpenMINDSContext context) {
51+
return new OpenMINDS(context).new {{version[0]|upper}}{{version[1:]}}();
52+
}
53+
54+
55+
56+
public class {{version[0]|upper}}{{version[1:]}} {
57+
58+
private final List<org.openmetadatainitiative.openminds.utils.Builder<?>> builders = new ArrayList<>();
59+
60+
{% for package in packages_by_version[version].keys() %}
61+
public final OpenMINDS.{{version[0]|upper}}{{version[1:]}}.{{package[0]|upper}}{{package[1:]}} {{package}} = new {{package[0]|upper}}{{package[1:]}}();
62+
63+
64+
public class {{package[0]|upper}}{{package[1:]}}{
65+
66+
{% for class in packages_by_version[version][package] %}
67+
public final OpenMINDS.{{version[0]|upper}}{{version[1:]}}.{{package[0]|upper}}{{package[1:]}}.{{class[0]}} {{class[0][0]|lower}}{{class[0][1:]}} = new {{class[0]}}();
68+
69+
public class {{class[0]}} {
70+
71+
public {{class[1]}}.Builder create(String localId){
72+
final {{class[1]}}.Builder builder = {{class[1]}}.create(new LocalId(localId));
73+
builders.add(builder);
74+
return builder;
75+
}
76+
}
77+
{% endfor %}
78+
}
79+
{% endfor %}
80+
81+
public void persist(String targetDirectory) {
82+
OpenMINDS.persist(targetDirectory, builders.stream().map(Builder::build));
83+
}
84+
}
85+
86+
{% endfor %}
87+
88+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package {{package_name}};
2+
3+
import org.openmetadatainitiative.openminds.utils.ByTypeDeserializer;
4+
import org.openmetadatainitiative.openminds.utils.Entity;
5+
import org.openmetadatainitiative.openminds.utils.Reference;
6+
7+
/**
8+
* ATTENTION! This is an autogenerated file based on the openMINDS schema - do not apply manual changes since they are going to be overwritten.
9+
*/
10+
public interface {{additional_interface}} extends Entity {
11+
Reference<? extends {{additional_interface}}> getReference();
12+
13+
class Deserializer extends ByTypeDeserializer<{{additional_interface}}> {
14+
public Deserializer() {
15+
super({{types|join(', ')}});
16+
}
17+
}
18+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package {{package_name}};
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
7+
import org.openmetadatainitiative.openminds.utils.*;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.UUID;
12+
13+
{% for import in imports %}import {{import}};
14+
{% endfor %}
15+
16+
import static {{package_name}}.{{ class_name }}.*;
17+
/**
18+
{{java_doc}}
19+
*
20+
* ATTENTION! This is an autogenerated file based on the openMINDS schema - do not apply manual changes since they are going to be overwritten.
21+
*/
22+
@InstanceType(SEMANTIC_NAME)
23+
@JsonIgnoreProperties(ignoreUnknown = true)
24+
public class {{ class_name }} extends Instance {% if implemented_interfaces %}implements {{ implemented_interfaces|join(', ') }}{% endif %}{
25+
static final String SEMANTIC_NAME = "{{ type }}";
26+
27+
@JsonIgnore
28+
public Reference<{{ class_name }}> getReference() {
29+
return doGetReference();
30+
}
31+
32+
public static Reference<{{ class_name }}> createReference(InstanceId instanceId) {
33+
return new Reference<>(instanceId);
34+
}
35+
36+
private {{ class_name }}(LocalId localId ) {
37+
super(localId);
38+
}
39+
40+
41+
public class Builder implements org.openmetadatainitiative.openminds.utils.Builder<{{ class_name }}>{
42+
{% for property in properties %}{% for line in builder_for_properties[property] %}
43+
{{ line }}
44+
{% endfor %}{% endfor %}
45+
46+
public {{ class_name }} build() {
47+
if ({{ class_name }}.this.id == null) {
48+
{{ class_name }}.this.id = new InstanceId(UUID.randomUUID().toString());
49+
}
50+
if({{ class_name }}.this.types == null || {{ class_name }}.this.types.isEmpty() || !{{ class_name }}.this.types.contains(SEMANTIC_NAME)){
51+
final List<String> oldValues = {{ class_name }}.this.types;
52+
{{ class_name }}.this.types = new ArrayList<>();
53+
{{ class_name }}.this.types.add(SEMANTIC_NAME);
54+
if(oldValues != null){
55+
{{ class_name }}.this.types.addAll(oldValues);
56+
}
57+
}
58+
return {{ class_name }}.this;
59+
}
60+
}
61+
62+
{% for property in properties %} @JsonProperty(value = "{%if property in absolute_property_translations%}{{absolute_property_translations[property]}}{%else%}{{property}}{%endif%}")
63+
{{member_for_properties[property]}}
64+
{%if property in property_descriptions and property_descriptions[property] %}
65+
/**
66+
* {{property_descriptions[property]}}
67+
*/{% endif %}
68+
{{getter_for_properties[property]}}
69+
70+
{% endfor %}
71+
public static {{ class_name }}.Builder create(LocalId localId){
72+
return new {{ class_name }}(localId).new Builder();
73+
}
74+
75+
public {{ class_name }}.Builder copy(){
76+
return ParsingUtils.OBJECT_MAPPER.convertValue(this, {{ class_name }}.class).new Builder();
77+
}
78+
}

pipeline/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)