Skip to content

579 implement mapping elements namespaces files #588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public interface AspectMetaModelResourceResolver {
*/
Try<VersionedModel> mergeMetaModelIntoRawModel( final Model rawModel, final VersionNumber version );

default Try<VersionedModel> mergeMetaModelIntoRawModel( final Model rawModel ) {
return mergeMetaModelIntoRawModel( rawModel, KnownVersion.getLatest() );
}

default Try<VersionedModel> mergeMetaModelIntoRawModel( final Model rawModel, final KnownVersion version ) {
return mergeMetaModelIntoRawModel( rawModel, VersionNumber.parse( version.toVersionString() ) );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2024 Robert Bosch Manufacturing Solutions GmbH
*
* See the AUTHORS file(s) distributed with this work for additional
* information regarding authorship.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/

package org.eclipse.esmf.aspectmodel.resolver.services;

import java.net.URI;
import java.util.List;
import java.util.Optional;

import org.eclipse.esmf.aspectmodel.vocabulary.Namespace;

import org.apache.jena.rdf.model.Model;

public interface ModelFile {
Model sourceModel();

List<String> headerComment();

Optional<URI> sourceLocation();

Namespace namespace();

default ModelFile withModel( final Model model ) {
return new ModelFile() {
@Override
public Model sourceModel() {
return model;
}

@Override
public List<String> headerComment() {
return ModelFile.this.headerComment();
}

@Override
public Optional<URI> sourceLocation() {
return ModelFile.this.sourceLocation();
}

@Override
public Namespace namespace() {
return ModelFile.this.namespace();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package org.eclipse.esmf.aspectmodel.resolver.services;

import java.util.List;

import org.eclipse.esmf.aspectmodel.VersionNumber;
import org.eclipse.esmf.samm.KnownVersion;

Expand All @@ -21,6 +23,7 @@
/**
* Encapsulates an Aspect Model (as RDF model) and the Meta Model version it uses
*/
// TODO: Remove use of this class throughout API, replace by ModelFile/AspectModel
public class VersionedModel {
/**
* The model including its corresponding meta model
Expand All @@ -37,10 +40,20 @@ public class VersionedModel {
*/
private final VersionNumber version;

public VersionedModel( final Model model, final VersionNumber version, final Model rawModel ) {
/**
* The source files of the model
*/
private final List<ModelFile> sources;

public VersionedModel( final Model model, final VersionNumber version, final Model rawModel, final List<ModelFile> sources ) {
this.model = model;
this.version = version;
this.rawModel = rawModel;
this.sources = sources;
}

public VersionedModel( final Model model, final VersionNumber version, final Model rawModel ) {
this( model, version, rawModel, List.of() );
}

public VersionedModel( final Model model, final KnownVersion version, final Model rawModel ) {
Expand All @@ -58,4 +71,12 @@ public VersionNumber getMetaModelVersion() {
public Model getRawModel() {
return rawModel;
}

public List<ModelFile> getSources() {
return sources;
}

public VersionedModel withSources( final List<ModelFile> sources ) {
return new VersionedModel( model, version, rawModel, sources );
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH
* Copyright (c) 2024 Robert Bosch Manufacturing Solutions GmbH
*
* See the AUTHORS file(s) distributed with this work for additional
* information regarding authorship.
Expand Down Expand Up @@ -36,29 +36,40 @@ default String getNamespace() {
return uri.endsWith( "#" ) ? uri : uri + "#";
}

default String urn( final String element ) {
return getNamespace() + element;
}

default Property property( final String name ) {
return ResourceFactory.createProperty( getNamespace() + name );
return ResourceFactory.createProperty( urn( name ) );
}

default Resource resource( final String name ) {
return ResourceFactory.createResource( getNamespace() + name );
return ResourceFactory.createResource( urn( name ) );
}

static Map<String, String> createPrefixMap( final KnownVersion metaModelVersion ) {
final Map<String, String> result = new LinkedHashMap<>();
final SAMM samm = new SAMM( metaModelVersion );
final SAMMC sammc = new SAMMC( metaModelVersion );
final SAMME samme = new SAMME( metaModelVersion, samm );
final UNIT unit = new UNIT( metaModelVersion, samm );
result.put( "samm", samm.getNamespace() );
result.put( "samm-c", new SAMMC( metaModelVersion ).getNamespace() );
result.put( "samm-e", new SAMME( metaModelVersion, samm ).getNamespace() );
result.put( "unit", new UNIT( metaModelVersion, samm ).getNamespace() );
result.put( "rdf", RDF.getURI() );
result.put( "rdfs", RDFS.getURI() );
result.put( "xsd", XSD.getURI() );
return result;
}

static Map<String, String> createPrefixMap() {
final Map<String, String> result = new LinkedHashMap<>();
result.put( "samm", samm.getUri() + "#" );
result.put( "samm-c", sammc.getUri() + "#" );
result.put( "samm-e", samme.getUri() + "#" );
result.put( "unit", unit.getUri() + "#" );
result.put( "samm", SammNs.SAMM.getNamespace() );
result.put( "samm-c", SammNs.SAMMC.getNamespace() );
result.put( "samm-e", SammNs.SAMME.getNamespace() );
result.put( "unit", SammNs.UNIT.getNamespace() );
result.put( "rdf", RDF.getURI() );
result.put( "rdfs", RDFS.getURI() );
result.put( "xsd", XSD.getURI() );

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@
@SuppressWarnings( { "checkstyle:AbbreviationAsWordInName", "NewMethodNamingConvention" } )
public class SAMMC implements Namespace {
private final KnownVersion metaModelVersion;
private final SAMM samm;

public SAMMC( final KnownVersion metaModelVersion ) {
this.metaModelVersion = metaModelVersion;
samm = new SAMM( metaModelVersion );
}

@Override
public String getUri() {
return samm.getBaseUri() + "characteristic:" + metaModelVersion.toVersionString();
return SammNs.SAMM.getBaseUri() + "characteristic:" + metaModelVersion.toVersionString();
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2024 Robert Bosch Manufacturing Solutions GmbH
*
* See the AUTHORS file(s) distributed with this work for additional
* information regarding authorship.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/

package org.eclipse.esmf.aspectmodel.vocabulary;

import org.eclipse.esmf.samm.KnownVersion;

public class SammNs {
public static final SAMM SAMM = new SAMM( KnownVersion.getLatest() );
public static final SAMMC SAMMC = new SAMMC( KnownVersion.getLatest() );
public static final SAMME SAMME = new SAMME( KnownVersion.getLatest(), SAMM );
public static final UNIT UNIT = new UNIT( KnownVersion.getLatest(), SAMM );
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.stream.Stream;

import org.eclipse.esmf.aspectmodel.vocabulary.SAMM;
import org.eclipse.esmf.aspectmodel.vocabulary.SammNs;
import org.eclipse.esmf.samm.KnownVersion;

import com.google.common.collect.Lists;
Expand Down Expand Up @@ -76,10 +77,8 @@ public class GenerateUnitsTtl {
import java.util.stream.Collectors;

import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
import org.eclipse.esmf.metamodel.QuantityKind;
import org.eclipse.esmf.metamodel.Unit;
import org.eclipse.esmf.aspectmodel.vocabulary.SammNs;
import org.eclipse.esmf.metamodel.impl.DefaultUnit;
import org.eclipse.esmf.samm.KnownVersion;
import org.eclipse.esmf.metamodel.loader.MetaModelBaseAttributes;
import org.eclipse.esmf.metamodel.datatypes.LangString;

Expand Down Expand Up @@ -168,10 +167,10 @@ public static Set<Unit> unitsWithQuantityKind( final QuantityKind quantityKind )
import java.util.Arrays;
import java.util.Optional;

import org.eclipse.esmf.aspectmodel.resolver.services.ModelFile;
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
import org.eclipse.esmf.metamodel.QuantityKind;
import org.eclipse.esmf.aspectmodel.vocabulary.SammNs;
import org.eclipse.esmf.metamodel.visitor.AspectVisitor;
import org.eclipse.esmf.samm.KnownVersion;

/**
* Enumeration of Quantity Kinds as defined in <a href="http://tfig.unece.org/contents/recommendation-20.htm">Recommendation 20</a>
Expand All @@ -187,6 +186,16 @@ public enum QuantityKinds implements QuantityKind {
this.name = name;
this.label = label;
}

@Override
public AspectModelUrn urn() {
return AspectModelUrn.fromUrn( SammNs.UNIT.urn( name ) );
}

@Override
public Optional<ModelFile> getSourceFile() {
return Optional.of( MetaModelFiles.UNITS );
}

/**
* Returns the quantity kind's unique name
Expand All @@ -212,17 +221,6 @@ public String toString() {
return getLabel();
}

@Override
public Optional<AspectModelUrn> getAspectModelUrn() {
return Optional.of( AspectModelUrn.fromUrn(
String.format( "urn:samm:org.eclipse.esmf.samm:unit:%s#%s", KnownVersion.getLatest().toVersionString(), name ) ) );
}

@Override
public KnownVersion getMetaModelVersion() {
return KnownVersion.getLatest();
}

@Override
public <T, C> T accept( final AspectVisitor<T, C> visitor, final C context ) {
return visitor.visitQuantityKind( this, context );
Expand Down Expand Up @@ -316,7 +314,7 @@ private List<Statement> attributeValues( final Resource resource, final Property
}

private List<String> unitDeclarations() {
final SAMM samm = new SAMM( KnownVersion.getLatest() );
final SAMM samm = SammNs.SAMM;

final Function<Optional<String>, String> buildDeclaration = optionalValue ->
optionalValue.map( StringEscapeUtils::escapeJava ).map( "Optional.of(\"%s\")"::formatted ).orElse( "Optional.empty()" );
Expand All @@ -336,28 +334,33 @@ private List<String> unitDeclarations() {
optionalAttributeValue( unit, samm.referenceUnit() ).map( Statement::getResource ).map( Resource::getLocalName ) );
final String conversionFactorDeclaration = buildDeclaration.apply(
optionalAttributeValue( unit, samm.conversionFactor() ).map( Statement::getString ) );
final String preferredNames = attributeValues( unit, samm.preferredName() ).stream().flatMap( buildLangString )
.collect( Collectors.joining( ", ", "Set.of(", ")" ) );
final String descriptions = attributeValues( unit, samm.description() ).stream().flatMap( buildLangString )
.collect( Collectors.joining( ", ", "Set.of(", ")" ) );
final String see = attributeValues( unit, samm.see() ).stream().map( seeValue -> "\"" + seeValue + "\"" )
.collect( Collectors.joining( ", ", "List.of(", ")" ) );
final String preferredNames = attributeValues( unit, samm.preferredName() ).stream()
.map( statement -> ".withPreferredName( Locale.forLanguageTag( \"%s\" ), \"%s\" )".formatted( statement.getLanguage(),
statement.getString() ) ).collect( Collectors.joining() );
final String quantityKindDefs = attributeValues( unit, samm.quantityKind() ).stream()
.map( quantityKind -> "QuantityKinds." + toUpperSnakeCase( quantityKind.getResource().getLocalName() ) )
.collect( Collectors.joining( ", ", "new HashSet<>(Arrays.asList(", "))" ) );
final String quantityKinds = quantityKindDefs.contains( "()" ) ? "Collections.emptySet()" : quantityKindDefs;

final String metaModelBaseAttributes =
"new MetaModelBaseAttributes( AspectModelUrn.fromUrn( \"%s\" ), \"%s\", %s, %s, %s )".formatted(
unit.getURI(), name, preferredNames, descriptions, see );
"MetaModelBaseAttributes.builder().withUrn( SammNs.UNIT.urn( \"%s\" ) )%s%s%s.build()".formatted(
unit.getLocalName(),
attributeValues( unit, samm.preferredName() ).stream()
.map( statement -> ".withPreferredName( Locale.forLanguageTag( \"%s\" ), \"%s\" )".formatted(
statement.getLanguage(), statement.getString() ) ).collect( Collectors.joining() ),
attributeValues( unit, samm.description() ).stream()
.map( statement -> ".withDescription( Locale.forLanguageTag( \"%s\" ), \"%s\" )".formatted(
statement.getLanguage(), statement.getString() ) ).collect( Collectors.joining() ),
attributeValues( unit, samm.see() ).stream().map( seeValue -> "withSee( \"" + seeValue + "\" )" )
.collect( Collectors.joining() ) );

final String unitDefinition = "new DefaultUnit( %s, %s, %s, %s, %s, %s )".formatted( metaModelBaseAttributes, symbolDeclaration,
commonCodeDeclaration, referenceUnitDeclaration, conversionFactorDeclaration, quantityKinds );
return "UNITS_BY_NAME.put( \"%s\", %s );".formatted( name, unitDefinition );
} ).sorted().toList();
}

private List<String> quantityKindDeclarations() {
final SAMM samm = new SAMM( KnownVersion.getLatest() );
final SAMM samm = SammNs.SAMM;

return Streams.stream( unitsModel.listStatements( null, RDF.type, samm.QuantityKind() ) ).map( Statement::getSubject )
.map( quantityKind -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public <T, C> T accept( final AspectVisitor<T, C> visitor, final C context ) {
@Override
public String toString() {
return new StringJoiner( ", ", DefaultSingleEntity.class.getSimpleName() + "[", "]" )
.add( getName() )
.toString();
}
}

This file was deleted.

Loading
Loading