package org.eclipse.basyx.examples.snippets.aas.submodels; | |
import static org.junit.Assert.assertTrue; | |
import java.util.Map; | |
import java.util.function.Function; | |
import org.eclipse.basyx.aas.api.resources.IOperation; | |
import org.eclipse.basyx.aas.api.resources.ISubModel; | |
import org.eclipse.basyx.aas.backend.connected.ConnectedAssetAdministrationShellManager; | |
import org.eclipse.basyx.aas.backend.connector.http.HTTPConnectorProvider; | |
import org.eclipse.basyx.aas.metamodel.factory.MetaModelElementFactory; | |
import org.eclipse.basyx.aas.metamodel.hashmap.aas.SubModel; | |
import org.eclipse.basyx.aas.metamodel.hashmap.aas.submodelelement.operation.Operation; | |
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.junit.ClassRule; | |
import org.junit.Test; | |
/** | |
* Code snippet that illustrates the definition and invocation of sub model 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 InvokeSubModelOperationSDK { | |
/** | |
* 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 { | |
/** | |
* Version number of serialized instance | |
*/ | |
private static final long serialVersionUID = 1L; | |
/** | |
* Constructor - create sub model | |
* | |
* This sub model contains operations | |
*/ | |
public SampleSubModel() { | |
// Set sub model ID | |
setId("sm-001"); | |
// Support creation of properties and operations | |
MetaModelElementFactory fac = new MetaModelElementFactory(); | |
// Define operations | |
getOperations().put("operation1", fac.createOperation(new Operation(), (Function<Object[], Object>) (v) -> { | |
return operation1(); | |
})); | |
getOperations().put("operation2", fac.createOperation(new Operation(), (Function<Object[], Object>) (v) -> { | |
return operation2((int) v[0], (int) v[1]); | |
})); | |
} | |
/** | |
* Example operation implementation | |
*/ | |
public int operation1() { | |
return 4; | |
} | |
/** | |
* Example operation with parameter | |
*/ | |
public int operation2(int par1, int par2) { | |
return par1+par2; | |
} | |
} | |
/** | |
* Create VAB connection manager backend | |
* | |
* The connection manager uses a preconfigured directory for resolving IDs to | |
* network addresses, and a HTTP connector to connect to VAB objects. | |
*/ | |
// protected VABConnectionManager connManager = new VABConnectionManager( | |
// // Add example specific mappings | |
// new ExamplesPreconfiguredDirectory() | |
// // - SDK connectors encapsulate relative path to sub model (/aas/submodels/sm-001) | |
// .addMapping("sm-001", "http://localhost:8080/basys.examples/Testsuite/components/BaSys/1.0/SampleModel"), | |
// // We connect via HTTP | |
// new HTTPConnectorProvider()); | |
protected ConnectedAssetAdministrationShellManager manager = new ConnectedAssetAdministrationShellManager( | |
// Add example specific mappings | |
new ExampleAASRegistry() | |
// - SDK connectors encapsulate relative path to sub model | |
// (/aas/submodels/sm-001) | |
.addOnlySubmodelMapping("sm-001", "http://localhost:8080/basys.examples/Testsuite/components/BaSys/1.0/SampleModel"), | |
// We connect via HTTP | |
new HTTPConnectorProvider()); | |
/** | |
* 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 { | |
// Retrieve sub model (created by factory) with SDK connector | |
{ | |
// Create and connect SDK connector | |
ISubModel subModel = manager.retrieveSM("sm-001"); | |
// Sub model operations | |
Map<String, IOperation> operations = subModel.getOperations(); | |
// Get first operation by name | |
IOperation op1 = operations.get("operation1"); | |
// - Invoke operation | |
int result1 = (int) op1.invoke(); | |
// Get second operation by name | |
IOperation op2 = operations.get("operation2"); | |
// - Invoke operation | |
int result2 = (int) op2.invoke(7, 5); | |
// Check results | |
assertTrue(result1 == 4); | |
assertTrue(result2 == 12); | |
} | |
} | |
} | |