Skip to content

Commit

Permalink
Change getSubmodelList() von MongoDBSubmodelAggregator.java to retrie…
Browse files Browse the repository at this point in the history
…ve only submodels that belongs to the shell.

Signed-off-by: Zai Zhang <[email protected]>
Co-authored-by: Jannis Jung <[email protected]>
  • Loading branch information
zhangzai123 and jannisjung committed Aug 9, 2023
1 parent 95d1188 commit a93dde0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,6 @@ private void addSubmodelProvidersById(String submodelIdentificationId, MultiSubm

}

@SuppressWarnings("unchecked")
@Override
public Collection<IAssetAdministrationShell> getAASList() {
return shellStorageApi.retrieveAll().stream().map(aas -> (IAssetAdministrationShell) aas).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,25 @@

package org.eclipse.basyx.components.aas.mongodb;

import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.eclipse.basyx.aas.metamodel.map.AssetAdministrationShell;
import org.eclipse.basyx.components.configuration.BaSyxMongoDBConfiguration;
import org.eclipse.basyx.components.internal.mongodb.MongoDBBaSyxStorageAPI;
import org.eclipse.basyx.components.internal.mongodb.MongoDBBaSyxStorageAPIFactory;
import org.eclipse.basyx.submodel.aggregator.SubmodelAggregator;
import org.eclipse.basyx.submodel.metamodel.api.ISubmodel;
import org.eclipse.basyx.submodel.metamodel.api.identifier.IIdentifier;
import org.eclipse.basyx.submodel.metamodel.api.reference.IKey;
import org.eclipse.basyx.submodel.metamodel.api.reference.IReference;
import org.eclipse.basyx.submodel.metamodel.map.Submodel;
import org.eclipse.basyx.submodel.restapi.api.ISubmodelAPI;
import org.eclipse.basyx.submodel.restapi.api.ISubmodelAPIFactory;
import org.eclipse.basyx.submodel.restapi.vab.VABSubmodelAPIFactory;
import org.eclipse.basyx.vab.exception.provider.ResourceNotFoundException;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Query;

import com.mongodb.client.MongoClient;

Expand All @@ -56,6 +55,7 @@
*/
public class MongoDBSubmodelAggregator extends SubmodelAggregator {
private MongoDBBaSyxStorageAPI<Submodel> storageApi;
private MongoDBBaSyxStorageAPI<AssetAdministrationShell> aasStorageApi;
private IIdentifier shellId;

public MongoDBSubmodelAggregator(ISubmodelAPIFactory submodelApiFactory, BaSyxMongoDBConfiguration config, MongoClient client) {
Expand All @@ -64,6 +64,7 @@ public MongoDBSubmodelAggregator(ISubmodelAPIFactory submodelApiFactory, BaSyxMo

public MongoDBSubmodelAggregator(ISubmodelAPIFactory submodelApiFactory, BaSyxMongoDBConfiguration config, MongoClient client, IIdentifier shellId) {
this(submodelApiFactory, MongoDBBaSyxStorageAPIFactory.<Submodel>create(config.getSubmodelCollection(), Submodel.class, config, client));
aasStorageApi = MongoDBBaSyxStorageAPIFactory.<AssetAdministrationShell>create(config.getAASCollection(), AssetAdministrationShell.class, config, client);
this.shellId = shellId;
}

Expand All @@ -90,13 +91,33 @@ public void deleteSubmodelByIdShort(String idShort) {

@Override
public Collection<ISubmodel> getSubmodelList() {
MongoOperations mongoOperation = (MongoOperations) storageApi.getStorageConnection();
Query hasParentId = query(where("parent.keys.[0].value").is(shellId.getId()));
List<ISubmodel> submodels = mongoOperation.find(hasParentId, ISubmodel.class, storageApi.getCollectionName());
return submodels;
if (shellId == null)
return returnAllSubmodels();

AssetAdministrationShell shell = aasStorageApi.retrieve(shellId.getId());
Collection<IReference> submodelRefs = shell.getSubmodelReferences();

if (submodelRefs.isEmpty()) {
return returnAllSubmodels();
}
List<String> submodelIds = submodelRefs.stream().map(ref -> {
return getLastKeyFromReference(ref).getValue();
}).collect(Collectors.toList());

return submodelIds.stream().map(sm -> {
return storageApi.retrieve(sm);
}).collect(Collectors.toList());
}

private List<ISubmodel> returnAllSubmodels() {
return storageApi.retrieveAll().stream().map(submodel -> (ISubmodel) submodel).collect(Collectors.toList());
}

private IKey getLastKeyFromReference(IReference reference) {
List<IKey> keys = reference.getKeys();
IKey lastKey = keys.get(keys.size() - 1);
return lastKey;
}

@Override
public ISubmodel getSubmodel(IIdentifier identifier) throws ResourceNotFoundException {
Expand All @@ -121,18 +142,13 @@ public void createSubmodel(ISubmodelAPI submodelAPI) {
@Override
public ISubmodel getSubmodelbyIdShort(String idShort) throws ResourceNotFoundException {
Optional<ISubmodel> submodelOptional = getSubmodelList().stream().filter(submodel -> {
return isTargetSubmodel(shellId, submodel, idShort);
return submodel.getIdShort().equals(idShort);
}).findAny();
if (submodelOptional.isEmpty())
throw new ResourceNotFoundException("The submodel with idShort '" + idShort + "' could not be found");
return submodelOptional.get();
}

private boolean isTargetSubmodel(IIdentifier shellId, ISubmodel submodel, String idShort) {
String shellIdValue = submodel.getParent().getKeys().get(0).getValue();
return (submodel.getIdShort().equals(idShort)) && shellIdValue.equals(shellId.getId());
}

@Override
public ISubmodelAPI getSubmodelAPIById(IIdentifier identifier) throws ResourceNotFoundException {
Submodel submodel = (Submodel) getSubmodel(identifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class TestMongoDBSubmodelAggregator extends SubmodelAggregatorSuite {
public static void initialize() {
BaSyxMongoDBConfiguration config = getMongoDBConfiguration();
MongoClient client = MongoClients.create(config.getConnectionUrl());
aggregator = new MongoDBSubmodelAggregator(new MongoDBSubmodelAPIFactory(config, client), config, client, new CustomId("testShellId"));
aggregator = new MongoDBSubmodelAggregator(new MongoDBSubmodelAPIFactory(config, client), config, client);
}

@Override
Expand Down

0 comments on commit a93dde0

Please sign in to comment.