Skip to content

Commit 73c26e0

Browse files
committed
Fix calculation of languages rachable via an aspect.
1 parent fa97aaf commit 73c26e0

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/metamodel/visitor/LanguageCollectorModelVisitor.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@
1919
import java.util.stream.Collectors;
2020
import java.util.stream.Stream;
2121

22-
import com.google.common.collect.Sets;
23-
22+
import org.eclipse.esmf.characteristic.Collection;
23+
import org.eclipse.esmf.characteristic.Trait;
2424
import org.eclipse.esmf.metamodel.Aspect;
25-
import org.eclipse.esmf.metamodel.ModelElement;
2625
import org.eclipse.esmf.metamodel.Entity;
26+
import org.eclipse.esmf.metamodel.ModelElement;
2727
import org.eclipse.esmf.metamodel.NamedElement;
2828
import org.eclipse.esmf.metamodel.Operation;
2929
import org.eclipse.esmf.metamodel.Property;
3030
import org.eclipse.esmf.metamodel.QuantityKind;
31-
import org.eclipse.esmf.characteristic.Trait;
3231
import org.eclipse.esmf.metamodel.Unit;
3332
import org.eclipse.esmf.metamodel.datatypes.LangString;
3433

34+
import com.google.common.collect.Sets;
35+
3536
/**
3637
* Aspect Model Visitor that retrieves all used Locales in an Aspect Model
3738
*/
@@ -101,4 +102,10 @@ public Set<Locale> visitUnit( final Unit unit, final Set<Locale> context ) {
101102
public Set<Locale> visitQuantityKind( final QuantityKind quantityKind, final Set<Locale> context ) {
102103
return Collections.emptySet();
103104
}
105+
106+
@Override
107+
public Set<Locale> visitCollection( final Collection collection, final Set<Locale> context ) {
108+
return collection.getElementCharacteristic().isPresent() ? collection.getElementCharacteristic().get().accept( this, Collections.emptySet() ) :
109+
collection.getDataType().get().accept( this, Collections.emptySet() );
110+
}
104111
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.eclipse.esmf.metamodel.visitor;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.ByteArrayInputStream;
6+
import java.io.InputStream;
7+
import java.nio.charset.StandardCharsets;
8+
import java.util.HashSet;
9+
import java.util.Locale;
10+
import java.util.Set;
11+
12+
import org.apache.jena.rdf.model.Model;
13+
import org.apache.jena.rdf.model.ModelFactory;
14+
import org.apache.jena.riot.RDFLanguages;
15+
import org.eclipse.esmf.aspectmodel.VersionNumber;
16+
import org.eclipse.esmf.aspectmodel.resolver.services.SammAspectMetaModelResourceResolver;
17+
import org.eclipse.esmf.aspectmodel.resolver.services.VersionedModel;
18+
import org.eclipse.esmf.metamodel.Aspect;
19+
import org.eclipse.esmf.metamodel.loader.AspectModelLoader;
20+
import org.junit.jupiter.api.Test;
21+
22+
class LanguageCollectorModelVisitorTest {
23+
24+
@Test
25+
void testLanguageCollector() {
26+
final VersionedModel versionedModel = model( """
27+
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.0.0#> .
28+
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.0.0#> .
29+
@prefix : <urn:samm:test:0.0.1#> .
30+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
31+
32+
:Reference a samm:Aspect ;
33+
samm:preferredName "Reference"@en ;
34+
samm:description "Beschreibung."@de ;
35+
samm:properties ( :items ) ;
36+
samm:operations ( ) .
37+
38+
:items a samm:Property ;
39+
samm:preferredName "Items"@es ;
40+
samm:description "Items."@it ;
41+
samm:characteristic [
42+
a samm-c:Set ;
43+
samm:dataType :AEntity
44+
] .
45+
46+
:AEntity a samm:Entity ;
47+
samm:preferredName "A name"@pt ;
48+
samm:preferredName "Ein name"@de ;
49+
samm:properties ( :AProperty ) .
50+
51+
:AProperty a samm:Property ;
52+
samm:preferredName "Common Property"@en ;
53+
samm:description "Bon idee"@fr ;
54+
samm:characteristic :ACharacteristic .
55+
56+
:ACharacteristic a samm-c:Code ;
57+
samm:dataType xsd:string .
58+
""" );
59+
final Aspect aspect = AspectModelLoader.getSingleAspectUnchecked( versionedModel );
60+
final Set<Locale> reachableLocales = new LanguageCollectorModelVisitor().visitAspect( aspect, new HashSet<>() );
61+
assertThat( reachableLocales ).containsExactlyInAnyOrder( Locale.ENGLISH, Locale.GERMAN, Locale.ITALIAN, Locale.FRENCH, Locale.forLanguageTag( "es" ),
62+
Locale.forLanguageTag( "pt" ) );
63+
}
64+
65+
private VersionedModel model( final String ttlRepresentation ) {
66+
final Model model = ModelFactory.createDefaultModel();
67+
final InputStream in = new ByteArrayInputStream( ttlRepresentation.getBytes( StandardCharsets.UTF_8 ) );
68+
model.read( in, "", RDFLanguages.strLangTurtle );
69+
return new SammAspectMetaModelResourceResolver().mergeMetaModelIntoRawModel( model, VersionNumber.parse( "2.0.0" ) )
70+
.get();
71+
}
72+
}

0 commit comments

Comments
 (0)