Skip to content

Commit

Permalink
Introduces overloaded variants
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Schnicke <[email protected]>
  • Loading branch information
FrankSchnicke committed Sep 13, 2024
1 parent 7037adc commit b92237c
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ public static Reference toReference(Identifiable identifiable, Class<? extends R
Reference reference = referenceType.getConstructor().newInstance();
reference.setType(ReferenceTypes.MODEL_REFERENCE);

setReferredSemanticIdIfHasSemantics(identifiable, reference);

Key key = keyType.getConstructor().newInstance();
key.setType(referableToKeyType(identifiable));
Expand All @@ -122,6 +121,23 @@ public static Reference toReference(Identifiable identifiable, Class<? extends R
}
}

/**
* Creates a reference for an Identifiable instance using provided implementation types for reference and key
*
* @param identifiable the identifiable to create the reference for
* @param referenceType implementation type of Reference interface
* @param keyType implementation type of Key interface
* @param setReferredSemanticIdIfHasSemantics if the referredSemanticId should be set if the identifiable is of HasSemantics
* @return a reference representing the identifiable
*/
public static Reference toReference(Identifiable identifiable, Class<? extends Reference> referenceType,
Class<? extends Key> keyType, boolean setReferredSemanticIdIfHasSemantics) {
Reference reference = toReference(identifiable, referenceType, keyType);

return handleReferredSemanticId(identifiable, setReferredSemanticIdIfHasSemantics, reference);
}


/**
* Creates a reference for an Identifiable instance
*
Expand All @@ -133,18 +149,16 @@ public static Reference toReference(Identifiable identifiable) {
}

/**
* Gets the KeyElements type matching the provided Referable
* Creates a reference for an Identifiable instance
*
* @param referable The referable to convert to KeyElements type
* @return the most specific KeyElements type representing the Referable, i.e. abstract types like SUBMODEL_ELEMENT
* or DATA_ELEMENT are never returned; null if there is no corresponding KeyElements type
* @param identifiable the identifiable to create the reference for
* @param setReferredSemanticIdIfHasSemantics if the referredSemanticId should be set if the identifiable is of HasSemantics
* @return a reference representing the identifiable
*/
public static KeyTypes referableToKeyType(Referable referable) {
Class<?> aasInterface = ReflectionHelper.getAasInterface(referable.getClass());
if (aasInterface != null) {
return KeyTypes.valueOf(EnumDeserializer.deserializeEnumName(aasInterface.getSimpleName()));
}
return null;
public static Reference toReference(Identifiable identifiable, boolean setReferredSemanticIdIfHasSemantics) {
Reference reference = toReference(identifiable);

return handleReferredSemanticId(identifiable, setReferredSemanticIdIfHasSemantics, reference);
}

/**
Expand All @@ -169,7 +183,6 @@ public static Reference toReference(Reference parent, Referable element, Class<?
} else {
Reference result = clone(parent, referenceType, keyType);
if (result != null) {
setReferredSemanticIdIfHasSemantics(element, result);
try {
Key newKey = keyType.getConstructor().newInstance();
newKey.setType(AasUtils.referableToKeyType(element));
Expand All @@ -183,6 +196,27 @@ public static Reference toReference(Reference parent, Referable element, Class<?
}
}

/**
* Creates a reference for an element given a potential parent using provided implementation types for reference and
* key
*
* @param parent Reference to the parent. Can only be null when element is instance of Identifiable, otherwise
* result will always be null
* @param element the element to create a reference for
* @param referenceType implementation type of Reference interface
* @param keyType implementation type of Key interface
* @param setReferredSemanticIdIfHasSemantics if the referredSemanticId should be set if the identifiable is of HasSemantics
*
* @return A reference representing the element or null if either element is null or parent is null and element not
* an instance of Identifiable. In case element is an instance of Identifiable, the returned reference will only
* contain one key pointing directly to the element.
*/
public static Reference toReference(Reference parent, Referable element, Class<? extends Reference> referenceType,
Class<? extends Key> keyType, boolean setReferredSemanticIdIfHasSemantics) {
Reference reference = toReference(parent, element, referenceType, keyType);
return handleReferredSemanticId(element, setReferredSemanticIdIfHasSemantics, reference);
}

/**
* Creates a reference for an element given a potential parent
*
Expand All @@ -200,6 +234,42 @@ public static Reference toReference(Reference parent, Referable element) {
ReflectionHelper.getDefaultImplementation(Key.class));
}

/**
* Creates a reference for an element given a potential parent
*
* @param parent Reference to the parent. Can only be null when element is instance of Identifiable, otherwise
* result will always be null
* @param element the element to create a reference for
* @param setReferredSemanticIdIfHasSemantics if the referredSemanticId should be set if the identifiable is of HasSemantics
*
* @return A reference representing the element or null if either element is null or parent is null and element not
* an instance of Identifiable. In case element is an instance of Identifiable, the returned reference will only
* contain one key pointing directly to the element.
*/
public static Reference toReference(Reference parent, Referable element, boolean setReferredSemanticIdIfHasSemantics) {
return toReference(parent,
element,
ReflectionHelper.getDefaultImplementation(Reference.class),
ReflectionHelper.getDefaultImplementation(Key.class),
setReferredSemanticIdIfHasSemantics);
}

/**
* Gets the KeyElements type matching the provided Referable
*
* @param referable The referable to convert to KeyElements type
* @return the most specific KeyElements type representing the Referable, i.e. abstract types like SUBMODEL_ELEMENT
* or DATA_ELEMENT are never returned; null if there is no corresponding KeyElements type
*/
public static KeyTypes referableToKeyType(Referable referable) {
Class<?> aasInterface = ReflectionHelper.getAasInterface(referable.getClass());
if (aasInterface != null) {
return KeyTypes.valueOf(EnumDeserializer.deserializeEnumName(aasInterface.getSimpleName()));
}
return null;
}


/**
* Checks if two references are refering to the same element ignoring referredSemanticId.
*
Expand Down Expand Up @@ -356,9 +426,13 @@ public static <T extends Referable> T resolve(Reference reference, Environment e
return type.cast(current);
}

private static void setReferredSemanticIdIfHasSemantics(Object obj, Reference reference) {
if (obj instanceof HasSemantics) {
reference.setReferredSemanticId(((HasSemantics) obj).getSemanticId());
private static Reference handleReferredSemanticId(Referable referable,
boolean setReferredSemanticIdIfHasSemantics, Reference reference) {
if (setReferredSemanticIdIfHasSemantics && referable instanceof HasSemantics) {
reference.setReferredSemanticId(((HasSemantics) referable).getSemanticId());
}

return reference;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -509,14 +509,21 @@ public void whenAsString_withReferredSemanticId_success() {
}

@Test
public void whenAsReference_IdentifiableWithSemanticId_success() {
public void whenAsReferenceWithReferredSemanticId_IdentifiableWithSemanticId_success() {
Submodel submodel = AASFull.SUBMODEL_3;
Reference ref = AasUtils.toReference(submodel);
Reference ref = AasUtils.toReference(submodel, true);
assertEquals(submodel.getSemanticId(), ref.getReferredSemanticId());
}

@Test
public void whenAsReference_PropertyWithSemanticId_success() {
public void whenAsReferenceWithoutReferredSemanticId_IdentifiableWithSemanticId_success() {
Submodel submodel = AASFull.SUBMODEL_3;
Reference ref = AasUtils.toReference(submodel, false);
assertEquals(null, ref.getReferredSemanticId());
}

@Test
public void whenAsReferenceWithReferredSemanticId_PropertyWithSemanticId_success() {
Property prop = createPropertyWithSemanticId();
Reference reference = new DefaultReference.Builder()
.type(ReferenceTypes.EXTERNAL_REFERENCE)
Expand All @@ -525,9 +532,23 @@ public void whenAsReference_PropertyWithSemanticId_success() {
.value("bar")
.build())
.build();
Reference ref = AasUtils.toReference(reference, prop);
Reference ref = AasUtils.toReference(reference, prop, true);
assertEquals(prop.getSemanticId(), ref.getReferredSemanticId());
}

@Test
public void whenAsReferenceWithoutReferredSemanticId_PropertyWithSemanticId_success() {
Property prop = createPropertyWithSemanticId();
Reference reference = new DefaultReference.Builder()
.type(ReferenceTypes.EXTERNAL_REFERENCE)
.keys(new DefaultKey.Builder()
.type(KeyTypes.GLOBAL_REFERENCE)
.value("bar")
.build())
.build();
Reference ref = AasUtils.toReference(reference, prop, false);
assertEquals(null, ref.getReferredSemanticId());
}

private Property createPropertyWithSemanticId() {
return new DefaultProperty.Builder()
Expand Down

0 comments on commit b92237c

Please sign in to comment.