Search in sources :

Example 6 with ModelProperty

use of org.eclipse.vorto.model.ModelProperty in project vorto by eclipse.

the class SpecWithPropertyConditionXpath method createModel.

@Override
protected void createModel() {
    FunctionblockModel buttonModel = new FunctionblockModel(ModelId.fromPrettyFormat("demo.fb:PushButton:1.0.0"));
    ModelProperty digitalInputStateProperty = new ModelProperty();
    digitalInputStateProperty.setMandatory(true);
    digitalInputStateProperty.setName("sensor_value");
    digitalInputStateProperty.setType(PrimitiveType.FLOAT);
    digitalInputStateProperty.setTargetPlatformKey("iotbutton");
    digitalInputStateProperty.addStereotype(Stereotype.createWithConditionalXpath("xpath:eval('data[@id = 100]/value',this) == 'x'", "100"));
    buttonModel.setStatusProperties(Arrays.asList(new ModelProperty[] { digitalInputStateProperty }));
    infomodel.getFunctionblocks().add(ModelProperty.Builder("button", buttonModel).build());
}
Also used : FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) ModelProperty(org.eclipse.vorto.model.ModelProperty)

Example 7 with ModelProperty

use of org.eclipse.vorto.model.ModelProperty in project vorto by eclipse.

the class DataMapperJxpath method map.

public InfomodelValue map(Object input, MappingContext mappingContext) {
    JXPathContext context = jxpathHelper.newContext(input);
    InfomodelValue normalized = new InfomodelValue(specification.getInfoModel());
    final Infomodel deviceInfoModel = specification.getInfoModel();
    for (ModelProperty fbProperty : deviceInfoModel.getFunctionblocks()) {
        FunctionblockValue mappedFb = mapFunctionBlock(fbProperty, context);
        if (mappedFb != null) {
            normalized.withFunctionblock(fbProperty.getName(), mappedFb);
        }
    }
    return normalized;
}
Also used : FunctionblockValue(org.eclipse.vorto.model.runtime.FunctionblockValue) JXPathContext(org.apache.commons.jxpath.JXPathContext) Infomodel(org.eclipse.vorto.model.Infomodel) ModelProperty(org.eclipse.vorto.model.ModelProperty) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue)

Example 8 with ModelProperty

use of org.eclipse.vorto.model.ModelProperty in project vorto by eclipse.

the class DataMapperJxpath method mapProperty.

private Object mapProperty(IModel parent, ModelProperty property, JXPathContext input) {
    Optional<Stereotype> sourceStereotype = property.getStereotype(STEREOTYPE_SOURCE);
    if (sourceStereotype.isPresent() && hasXpath(sourceStereotype.get().getAttributes())) {
        String expression = replacePlaceHolders(sourceStereotype.get().getAttributes().get(ATTRIBUTE_XPATH), sourceStereotype.get().getAttributes());
        if (matchesPropertyCondition(sourceStereotype.get(), input)) {
            return input.getValue(expression);
        }
    } else if (property.getType() instanceof IModel) {
        IModel referencedModel = (IModel) property.getType();
        if (referencedModel instanceof EntityModel) {
            EntityModel entityModel = (EntityModel) referencedModel;
            EntityValue value = new EntityValue(entityModel);
            for (ModelProperty entityProperty : entityModel.getProperties()) {
                try {
                    Object mapped = this.mapProperty(entityModel, entityProperty, input);
                    if (mapped != null) {
                        value.withProperty(entityProperty.getName(), mapped);
                    }
                } catch (JXPathNotFoundException ex) {
                    if (entityProperty.isMandatory()) {
                        return null;
                    }
                } catch (JXPathInvalidAccessException ex) {
                    if (ex.getCause() instanceof JXPathNotFoundException) {
                        if (entityProperty.isMandatory()) {
                            return null;
                        }
                    }
                    throw new MappingException("A problem occured during mapping", ex);
                }
            }
            return onlyReturnIfPopulated(value);
        } else if (referencedModel instanceof EnumModel) {
            EnumModel enumModel = (EnumModel) referencedModel;
            EnumValue value = new EnumValue(enumModel);
            if (sourceStereotype.isPresent() && hasXpath(sourceStereotype.get().getAttributes())) {
                String expression = replacePlaceHolders(sourceStereotype.get().getAttributes().get(ATTRIBUTE_XPATH), sourceStereotype.get().getAttributes());
                if (matchesPropertyCondition(sourceStereotype.get(), input)) {
                    Object mappedEnumValue = input.getValue(expression);
                    if (mappedEnumValue instanceof String) {
                        value.setValue((String) mappedEnumValue);
                    }
                    return value;
                }
            }
        }
    }
    return null;
}
Also used : IModel(org.eclipse.vorto.model.IModel) JXPathNotFoundException(org.apache.commons.jxpath.JXPathNotFoundException) EnumValue(org.eclipse.vorto.model.runtime.EnumValue) EntityModel(org.eclipse.vorto.model.EntityModel) Stereotype(org.eclipse.vorto.model.Stereotype) EntityValue(org.eclipse.vorto.model.runtime.EntityValue) JXPathInvalidAccessException(org.apache.commons.jxpath.JXPathInvalidAccessException) MappingException(org.eclipse.vorto.mapping.engine.MappingException) EnumModel(org.eclipse.vorto.model.EnumModel) ModelProperty(org.eclipse.vorto.model.ModelProperty)

Example 9 with ModelProperty

use of org.eclipse.vorto.model.ModelProperty in project vorto by eclipse.

the class DataMapperJxpath method mapFunctionBlock.

private FunctionblockValue mapFunctionBlock(ModelProperty fbProperty, JXPathContext context) {
    FunctionblockModel fbModel = specification.getFunctionBlock(fbProperty.getName());
    if (!matchesCondition(fbModel, context)) {
        return null;
    }
    FunctionblockValue fbData = new FunctionblockValue(fbModel);
    for (ModelProperty statusProperty : fbModel.getStatusProperties()) {
        try {
            Object mapped = this.mapProperty(fbModel, statusProperty, context);
            if (mapped != null) {
                fbData.withStatusProperty(statusProperty.getName(), mapped);
            }
        } catch (JXPathNotFoundException ex) {
            if (statusProperty.isMandatory()) {
                return null;
            }
        } catch (JXPathInvalidAccessException ex) {
            if (ex.getCause() instanceof JXPathNotFoundException) {
                if (statusProperty.isMandatory()) {
                    return null;
                }
            }
            throw new MappingException("A problem occured during mapping", ex);
        }
    }
    for (ModelProperty configProperty : fbModel.getConfigurationProperties()) {
        try {
            Object mapped = this.mapProperty(fbModel, configProperty, context);
            if (mapped != null) {
                fbData.withConfigurationProperty(configProperty.getName(), mapped);
            }
        } catch (JXPathNotFoundException ex) {
            if (configProperty.isMandatory()) {
                return null;
            }
        } catch (JXPathInvalidAccessException ex) {
            if (ex.getCause() instanceof JXPathNotFoundException) {
                if (configProperty.isMandatory()) {
                    return null;
                }
            }
            throw new MappingException("A problem occured during mapping", ex);
        }
    }
    return onlyReturnIfPopulated(fbData);
}
Also used : FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) FunctionblockValue(org.eclipse.vorto.model.runtime.FunctionblockValue) JXPathNotFoundException(org.apache.commons.jxpath.JXPathNotFoundException) ModelProperty(org.eclipse.vorto.model.ModelProperty) JXPathInvalidAccessException(org.apache.commons.jxpath.JXPathInvalidAccessException) MappingException(org.eclipse.vorto.mapping.engine.MappingException)

Example 10 with ModelProperty

use of org.eclipse.vorto.model.ModelProperty in project vorto by eclipse.

the class SpecWithConditionFunction method createModel.

@Override
protected void createModel() {
    FunctionblockModel buttonModel = new FunctionblockModel(ModelId.fromPrettyFormat("demo.fb:PushButton:1.0.0"));
    ModelProperty digitalInputStateProperty = new ModelProperty();
    digitalInputStateProperty.setMandatory(true);
    digitalInputStateProperty.setName("sensor_value");
    digitalInputStateProperty.setType(PrimitiveType.FLOAT);
    digitalInputStateProperty.setTargetPlatformKey("iotbutton");
    digitalInputStateProperty.addStereotype(Stereotype.createWithConditionalXpath("(vorto_base64:decodeString(data))[0] == 104", "vorto_string:toString(vorto_base64:decodeString(data),'utf8')"));
    buttonModel.setStatusProperties(Arrays.asList(new ModelProperty[] { digitalInputStateProperty }));
    infomodel.getFunctionblocks().add(ModelProperty.Builder("button", buttonModel).build());
}
Also used : FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) ModelProperty(org.eclipse.vorto.model.ModelProperty)

Aggregations

ModelProperty (org.eclipse.vorto.model.ModelProperty)39 FunctionblockModel (org.eclipse.vorto.model.FunctionblockModel)30 Property (org.eclipse.vorto.core.api.model.datatype.Property)5 EntityModel (org.eclipse.vorto.model.EntityModel)5 Infomodel (org.eclipse.vorto.model.Infomodel)5 ModelId (org.eclipse.vorto.core.api.model.model.ModelId)4 Constraint (org.eclipse.vorto.model.Constraint)4 EnumModel (org.eclipse.vorto.model.EnumModel)4 ModelEvent (org.eclipse.vorto.model.ModelEvent)3 ModelId (org.eclipse.vorto.model.ModelId)3 Operation (org.eclipse.vorto.model.Operation)3 Stereotype (org.eclipse.vorto.model.Stereotype)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 JXPathInvalidAccessException (org.apache.commons.jxpath.JXPathInvalidAccessException)2 JXPathNotFoundException (org.apache.commons.jxpath.JXPathNotFoundException)2 EntityBuilder (org.eclipse.vorto.core.api.model.BuilderUtils.EntityBuilder)2 EventBuilder (org.eclipse.vorto.core.api.model.BuilderUtils.EventBuilder)2