Skip to content

Commit

Permalink
Use aas:index property to map sorted elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
kenwenzel committed Nov 5, 2024
1 parent f410e4a commit b6d1728
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.linkedfactory.core.kvin.Record;
import io.github.linkedfactory.core.rdf4j.common.Conversions;
import io.github.linkedfactory.core.rdf4j.common.IRIWithValue;
import net.enilink.komma.core.URI;
import net.enilink.komma.core.URIs;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Value;
Expand Down Expand Up @@ -33,9 +34,16 @@ static Value toRdfValue(Object value, ValueFactory vf) {
idStr = id.getValue() != null ? id.getValue().toString() : null;
}
if (idStr == null && "ModelReference".equals(r.first(URIs.createURI(AAS_NAMESPACE + "type")).getValue())) {
Object keys = r.first(URIs.createURI(AAS_NAMESPACE + "keys")).getValue();
if (keys instanceof List<?> && ((List<?>) keys).size() == 1) {
Record firstKey = (Record) ((List<?>) keys).get(0);
URI keysProperty = URIs.createURI(AAS_NAMESPACE + "keys");
Record keys = r.first(keysProperty);
Record firstKey = null;
// there is exactly one aas:keys element
if (keys.getValue() instanceof Record && keys.next().first(keysProperty).getValue() == null) {
firstKey = (Record) keys.getValue();
} else if (keys.getValue() instanceof List<?> && ((List<?>) keys.getValue()).size() == 1) {
firstKey = (Record) ((List<?>) keys.getValue()).get(0);
}
if (firstKey != null) {
Object typeValue = firstKey.first(URIs.createURI(AAS_NAMESPACE + "type")).getValue();
if (typeValue != null) {
type = typeValue.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class AasClient implements Closeable {
final String endpoint;
ObjectMapper mapper = new ObjectMapper();
CloseableHttpClient httpClient;
static URI AAS_INDEX_PROPERTY = URIs.createURI(AAS.AAS_NAMESPACE + "index");
static boolean USE_AAS_INDEX_PROPERTY = true;
static Set<String> orderedElements = Set.of("keys", "SubmodelElementList", "ValueList");

public AasClient(String endpoint) {
this.endpoint = endpoint;
Expand Down Expand Up @@ -125,12 +128,17 @@ private Object nodeToValue(JsonNode node) {
}
URI property = URIs.createURI(AAS.AAS_NAMESPACE + recordNode.getKey());
JsonNode nodeValue = recordNode.getValue();
if (nodeValue.isArray() && !("keys".equals(recordNode.getKey())
|| "SubmodelElementList".equals(recordNode.getKey())
|| "ValueList".equals(recordNode.getKey()))) {

if (nodeValue.isArray() && (USE_AAS_INDEX_PROPERTY || !orderedElements.contains(recordNode.getKey()))) {
boolean addIndex = USE_AAS_INDEX_PROPERTY && orderedElements.contains(recordNode.getKey());
// each element of the array is added as (unordered) property value
int i = 0;
for (JsonNode element : nodeValue) {
value = value.append(new Record(property, nodeToValue(element)));
var converted = nodeToValue(element);
if (addIndex && converted instanceof Record) {
converted = ((Record)converted).append(new Record(AAS_INDEX_PROPERTY, BigInteger.valueOf(i++)));
}
value = value.append(new Record(property, converted));
}
} else {
// convert value as whole (object or ordered list/array)
Expand Down

0 comments on commit b6d1728

Please sign in to comment.