Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "BaSyx.Examples.Snippets.AASSMCreation.Java"
(Created page with "This code snippet illustrates the creation of an Asset Administration Shell (AAS) sub model using the Java SDK with the <code>MetaModelElementFactory</code> class. The BaSys s...") |
|||
Line 13: | Line 13: | ||
*/ | */ | ||
static class SampleSubModel extends SubModel { | 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") | @SuppressWarnings("unchecked") | ||
public SampleSubModel() { | public SampleSubModel() { | ||
// Set sub model ID | // Set sub model ID | ||
− | + | setIdShort("sm-001"); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
// Add example properties | // Add example properties | ||
// - Add simple property | // - 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 | // - Add container property that holds other properties | ||
− | + | SubmodelElementCollection container = new SubmodelElementCollection(); | |
− | + | container.setIdShort("prop2"); | |
− | + | container.addElement(prop11); | |
// - Add container to property map | // - Add container to property map | ||
− | + | addSubModelElement(container); | |
// Add another property manually to sub model container "properties" | // Add another property manually to sub model container "properties" | ||
− | {((Map<String, Object>) this.get(" | + | Property prop3 = new Property(17); |
+ | prop3.setIdShort("prop3"); | ||
+ | { | ||
+ | ((Map<String, Object>) this.get("submodelElements")).put("prop3", prop3); | ||
+ | } | ||
} | } | ||
} | } | ||
Line 64: | Line 64: | ||
<syntaxhighlight lang="java" style="margin-left: 4em"> | <syntaxhighlight lang="java" style="margin-left: 4em"> | ||
/** | /** | ||
− | * | + | * 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( | 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())) | |
− | + | ); | |
</syntaxhighlight> | </syntaxhighlight> | ||
Line 78: | Line 84: | ||
− | The complete, executable code is available in the basyx.examples project in package | + | The complete, executable code is available in the basyx.examples project in package org.eclipse.basyx.examples.snippets.aas.submodels. |
Revision as of 09:55, 18 August 2020
This code snippet illustrates the creation of an Asset Administration Shell (AAS) sub model using the Java SDK with the MetaModelElementFactory
class. The BaSys setup for this code snippet is the following:
The BaSys setup consists of a Apache Tomcat server that runs BaSyx Servlets. It contains a sub model provider that exports the created example sub model. The snippet code runs in context of a servlet in the tomcat server and creates an AAS sub model. The sub model is created as following:
/** * 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 setIdShort("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.addElement(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 sub model exports the following property structure:
- Sub Model "sm-001"
- prop1 : Integer = 234
- prop3 : Integer = 17
- prop2 : Container
- prop11 : Integer = 123
The following code illustrates the deployment of the AAS sub model to the Apache Tomcat server. It maps the path on the HTTP server "/Testsuite/components/BaSys/1.0/SampleModel/*" to an instance of class SubmodelServlet
. This class exports a sub model as HTTP accessible servlet using the BaSys API for sub models.
/** * 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())) );
The complete, executable code is available in the basyx.examples project in package org.eclipse.basyx.examples.snippets.aas.submodels.
BaSyx project links: Project BaSyx main wiki page | What is BaSyx? | BaSyx Developer Documentation |