Search in sources :

Example 16 with FunctionblockModel

use of org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel in project vorto by eclipse.

the class FunctionBlockXmlTemplate method getContent.

/**
 * Retrieves the content of the XML generation template.
 *
 * @param model the {@link FunctionblockModel}
 * @return generated content for the specified model
 */
public String getContent(final FunctionblockModel model, InvocationContext context) {
    // FB name
    final String name = model.getName();
    try {
        final LWM2M lwm2m = new LWM2M();
        final List<Object> lwm2mObjects = lwm2m.getObject();
        final FunctionBlock functionblock = model.getFunctionblock();
        if (functionblock == null) {
            throw new RuntimeException("This model has no function block.");
        }
        final Object lwm2mObject = new Object();
        // waiting for Issue in
        lwm2mObject.setObjectType("MODefinition");
        // https://github.com/OpenMobileAlliance/OMA-LwM2M-Public-Review/issues
        lwm2mObject.setName(name);
        final IMapped<FunctionblockModel> mappedFunctionblock = context.getMappedElement(model, STEREOTYPE_OBJECT);
        lwm2mObject.setObjectID(Integer.parseInt(mappedFunctionblock.getAttributeValue(OBJECT_ID, "0")));
        lwm2mObject.setObjectURN(mappedFunctionblock.getAttributeValue(OBJECT_URN, "TBD"));
        lwm2mObject.setMultipleInstances(mappedFunctionblock.getAttributeValue(ATTR_MULTIPLE_INSTANCES_MULTIPLE_VALUE, "Single"));
        lwm2mObject.setMandatory(mappedFunctionblock.getAttributeValue(ATTRIBUTE_MANDATORY, "Optional"));
        lwm2mObject.setDescription1(model.getDescription());
        lwm2mObject.setDescription2(mappedFunctionblock.getAttributeValue(ATTRIBUTE_DESCRIPTION2, ""));
        // handle config ------------------------------------------------------
        final Configuration configuration = functionblock.getConfiguration();
        if (configuration != null) {
            final EList<Property> configProps = configuration.getProperties();
            // configuration ==> operations : RW (default)
            handleProperties(lwm2mObject, configProps, "RW", context);
        }
        // handle status ------------------------------------------------------
        final Status status = functionblock.getStatus();
        if (status != null) {
            final EList<Property> statusProps = status.getProperties();
            // status ==> operations : R
            handleProperties(lwm2mObject, statusProps, "R", context);
        }
        // handle operations --------------------------------------------------
        handleOperations(functionblock, lwm2mObject, context);
        // handle fault -------------------------------------------------------
        final Fault fault = functionblock.getFault();
        if (fault != null) {
            final String errMsg = "Unsupported element <fault>" + STR_ABORT_GENERATOR;
            throw new IllegalArgumentException(errMsg);
        }
        // handle events
        final EList<Event> events = functionblock.getEvents();
        if (!events.isEmpty()) {
            final String errMsg = "Unsupported element <events>" + STR_ABORT_GENERATOR;
            throw new IllegalArgumentException(errMsg);
        }
        addSortedResourceIds(name, lwm2mObject);
        lwm2mObjects.add(lwm2mObject);
        return handleMarshalling(lwm2m, name);
    } catch (final RuntimeException e) {
        throw e;
    }
}
Also used : Status(org.eclipse.vorto.core.api.model.functionblock.Status) Configuration(org.eclipse.vorto.core.api.model.functionblock.Configuration) LWM2M(org.eclipse.vorto.codegen.lwm2m.generated.LWM2M) Fault(org.eclipse.vorto.core.api.model.functionblock.Fault) FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) Event(org.eclipse.vorto.core.api.model.functionblock.Event) Object(org.eclipse.vorto.codegen.lwm2m.generated.LWM2M.Object) Property(org.eclipse.vorto.core.api.model.datatype.Property) FunctionBlock(org.eclipse.vorto.core.api.model.functionblock.FunctionBlock)

Example 17 with FunctionblockModel

use of org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel in project vorto by eclipse.

the class ModelReaderTest method testReadFunctionblockModelsFromZipFile.

@Test
public void testReadFunctionblockModelsFromZipFile() {
    IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(getClass().getClassLoader().getResourceAsStream("models.zip"))).read();
    assertEquals(2, workspace.get().stream().filter(p -> p instanceof FunctionblockModel).collect(Collectors.toList()).size());
}
Also used : InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) ZipInputStream(java.util.zip.ZipInputStream) FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) Assert.assertNotNull(org.junit.Assert.assertNotNull) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ModelType(org.eclipse.vorto.repository.api.ModelType) Collectors(java.util.stream.Collectors) Assert.assertEquals(org.junit.Assert.assertEquals) IModelWorkspace(org.eclipse.vorto.server.commons.reader.IModelWorkspace) Model(org.eclipse.vorto.core.api.model.model.Model) FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) ZipInputStream(java.util.zip.ZipInputStream) IModelWorkspace(org.eclipse.vorto.server.commons.reader.IModelWorkspace) Test(org.junit.Test)

Aggregations

FunctionblockModel (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel)17 ChainedCodeGeneratorTask (org.eclipse.vorto.codegen.api.ChainedCodeGeneratorTask)5 FunctionBlock (org.eclipse.vorto.core.api.model.functionblock.FunctionBlock)5 FunctionblockProperty (org.eclipse.vorto.core.api.model.informationmodel.FunctionblockProperty)5 InformationModel (org.eclipse.vorto.core.api.model.informationmodel.InformationModel)5 GenerationResultZip (org.eclipse.vorto.codegen.api.GenerationResultZip)4 Property (org.eclipse.vorto.core.api.model.datatype.Property)4 IGenerationResult (org.eclipse.vorto.codegen.api.IGenerationResult)3 MappingModel (org.eclipse.vorto.core.api.model.mapping.MappingModel)3 ZipInputStream (java.util.zip.ZipInputStream)2 CharacteristicProperty (org.eclipse.vorto.codegen.ble.model.blegatt.CharacteristicProperty)2 Model (org.eclipse.vorto.core.api.model.model.Model)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Collectors (java.util.stream.Collectors)1 Generated (org.eclipse.vorto.codegen.api.Generated)1 IMapped (org.eclipse.vorto.codegen.api.mapping.IMapped)1 Characteristic (org.eclipse.vorto.codegen.ble.model.blegatt.Characteristic)1