| package org.eclipse.basyx.examples.snippets.aas.submodels; |
| |
| import static org.junit.Assert.assertEquals; |
| |
| import java.util.Map; |
| |
| import org.eclipse.basyx.aas.manager.ConnectedAssetAdministrationShellManager; |
| import org.eclipse.basyx.components.servlet.submodel.SubmodelServlet; |
| import org.eclipse.basyx.examples.contexts.BaSyxExamplesContext_Empty; |
| import org.eclipse.basyx.examples.deployment.BaSyxDeployment; |
| import org.eclipse.basyx.examples.support.directory.ExampleAASRegistry; |
| import org.eclipse.basyx.submodel.metamodel.api.ISubModel; |
| import org.eclipse.basyx.submodel.metamodel.api.identifier.IIdentifier; |
| import org.eclipse.basyx.submodel.metamodel.api.identifier.IdentifierType; |
| import org.eclipse.basyx.submodel.metamodel.api.submodelelement.ISubmodelElement; |
| import org.eclipse.basyx.submodel.metamodel.api.submodelelement.ISubmodelElementCollection; |
| import org.eclipse.basyx.submodel.metamodel.api.submodelelement.dataelement.IProperty; |
| import org.eclipse.basyx.submodel.metamodel.map.SubModel; |
| import org.eclipse.basyx.submodel.metamodel.map.identifier.Identifier; |
| import org.eclipse.basyx.submodel.metamodel.map.submodelelement.SubmodelElementCollection; |
| import org.eclipse.basyx.submodel.metamodel.map.submodelelement.dataelement.property.Property; |
| import org.eclipse.basyx.vab.protocol.http.connector.HTTPConnectorProvider; |
| import org.junit.ClassRule; |
| import org.junit.Test; |
| |
| /** |
| * Code snippet that illustrates the use of CRUD Virtual Automation Bus (VAB) operations |
| * |
| * The snippet communicates with a VAB element that is deployed to a VABLambdaServlet on a |
| * Apache Tomcat HTTP server instance. The VABLambdaServlet provides an empty container that |
| * is able to host any VAB object. |
| * |
| * @author kuhn |
| * |
| */ |
| public class ConnectToAASSubModelSDK { |
| |
| |
| /** |
| * Example sub model. This example sub model is created with the BaSyx SDK factory and defines the AAS meta model properties |
| */ |
| static class SampleSubModel extends SubModel { |
| /** |
| * Constructor - create sub model |
| * |
| * This sub model contains static properties, i.e. properties that have a static value assigned. |
| */ |
| @SuppressWarnings("unchecked") |
| public SampleSubModel() { |
| // Set sub model id and name |
| setIdShort("smName"); |
| setIdentification(IdentifierType.CUSTOM, "sm-001"); |
| |
| // Add example properties |
| // - Add simple property |
| Property prop1 = new Property(234); |
| prop1.setIdShort("prop1"); |
| addSubModelElement(prop1); |
| |
| Property prop11 = new Property(123); |
| prop11.setIdShort("prop11"); |
| // - Add container property that holds other properties |
| SubmodelElementCollection container = new SubmodelElementCollection(); |
| container.setIdShort("prop2"); |
| container.addSubModelElement(prop11); |
| // - Add container to property map |
| addSubModelElement(container); |
| |
| // Add another property manually to sub model container "properties" |
| Property prop3 = new Property(17); |
| prop3.setIdShort("prop3"); |
| ((Map<String, Object>) this.get("submodelElements")).put("prop3", prop3); |
| } |
| } |
| |
| |
| |
| /** |
| * The BaSyx Deployment instantiates and starts context elements for this example. |
| * |
| * This example instantiates the BaSyxExamplesContext_1MemoryAASServer_1SQLDirectory |
| * example context that creates one AAS server, and one SQL based AAS registry. |
| * |
| * BaSyxDeployment contexts instantiate all components on the IP address of the host. |
| * Therefore, all components use the same IP address. |
| */ |
| @ClassRule |
| public static BaSyxDeployment context = new BaSyxDeployment( |
| // Servlets for example snippet |
| new BaSyxExamplesContext_Empty(). |
| // Deploy example specific servlets to Tomcat server in this context |
| addServletMapping("/Testsuite/components/BaSys/1.0/SampleModel/*", new SubmodelServlet(new SampleSubModel())) |
| ); |
| |
| |
| /** |
| * Run code snippet. Access sub model, query data |
| */ |
| @Test |
| public void accessSubModel() throws Exception { |
| // Create the AAS registry |
| ExampleAASRegistry registry = new ExampleAASRegistry(); |
| registry.addAASMapping("aas-001", "http://localhost:8080/basys.examples/Testsuite/components/BaSys/1.0/SampleModel/") |
| .addSubmodelMapping("aas-001", "sm-001", "http://localhost:8080/basys.examples/Testsuite/components/BaSys/1.0/SampleModel/"); |
| |
| // Create manager using the directory stub and the HTTPConnectorProvider |
| ConnectedAssetAdministrationShellManager manager = new ConnectedAssetAdministrationShellManager(registry, |
| // We connect via HTTP |
| new HTTPConnectorProvider()); |
| |
| |
| // Retrieve sub model (created by factory) with SDK connector |
| // - Create and connect SDK connector |
| IIdentifier aasId = new Identifier(IdentifierType.CUSTOM, "aas-001"); |
| IIdentifier smId = new Identifier(IdentifierType.CUSTOM, "sm-001"); |
| ISubModel subModel = manager.retrieveSubModel(aasId, smId); |
| |
| // - Retrieve sub model values and compare to expected values |
| Map<String, ISubmodelElement> smElements = subModel.getSubmodelElements(); |
| IProperty prop1 = (IProperty) smElements.get("prop1"); |
| ISubmodelElementCollection prop2 = (ISubmodelElementCollection) smElements.get("prop2"); |
| IProperty prop11 = prop2.getProperties().get("prop11"); |
| IProperty prop3 = (IProperty) smElements.get("prop3"); |
| |
| assertEquals(smId.getId(), subModel.getIdentification().getId()); |
| assertEquals("smName", subModel.getIdShort()); |
| assertEquals("prop1", prop1.getIdShort()); |
| assertEquals(234, prop1.getValue()); |
| assertEquals("prop2", prop2.getIdShort()); |
| assertEquals("prop11", prop11.getIdShort()); |
| assertEquals(123, prop11.getValue()); |
| assertEquals("prop3", prop3.getIdShort()); |
| assertEquals(17, prop3.getValue()); |
| } |
| } |
| |
| |