Search in sources :

Example 1 with EntityModel

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

the class SpecWithNestedEntity method createModel.

@Override
protected void createModel() {
    EntityModel sensorValueEntity = EntityModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:SensorValue:1.0.0")).property(ModelProperty.Builder("value", PrimitiveType.FLOAT).withXPathStereotype("/temperature", "iotbutton").build()).build();
    FunctionblockModel temperatureModel = FunctionblockModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Temperature:1.0.0")).statusProperty(ModelProperty.Builder("value", sensorValueEntity).build()).build();
    infomodel.getFunctionblocks().add(ModelProperty.Builder("outdoorTemperature", temperatureModel).build());
}
Also used : FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) EntityModel(org.eclipse.vorto.model.EntityModel)

Example 2 with EntityModel

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

the class ModelContentToEcoreConverterTest method testEntityWithEnum.

@Test
public void testEntityWithEnum() {
    EnumModel enumModel = EnumModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Unit:1.0.0")).literal("celcius", null).literal("kg", null).build();
    EntityModel entityModel = EntityModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Location:1.0.0")).property(ModelProperty.Builder("lng", PrimitiveType.FLOAT).build()).property(ModelProperty.Builder("lat", PrimitiveType.STRING).build()).property(ModelProperty.Builder("unit", enumModel.getId()).build()).reference(enumModel.getId()).build();
    ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter();
    Entity model = (Entity) converter.convert(ModelContent.Builder(entityModel).withDependency(enumModel).build(), Optional.empty());
    assertEquals(1, model.getReferences().size());
    assertEquals(3, model.getProperties().size());
    Property unitProperty = model.getProperties().stream().filter(p -> p.getName().equals("unit")).findAny().get();
    ObjectPropertyType type = (ObjectPropertyType) unitProperty.getType();
    Enum unit = (Enum) type.getType();
    assertNotNull(unit);
}
Also used : Enum(org.eclipse.vorto.core.api.model.datatype.Enum) Entity(org.eclipse.vorto.core.api.model.datatype.Entity) ObjectPropertyType(org.eclipse.vorto.core.api.model.datatype.ObjectPropertyType) EnumModel(org.eclipse.vorto.model.EnumModel) EntityModel(org.eclipse.vorto.model.EntityModel) FunctionblockProperty(org.eclipse.vorto.core.api.model.informationmodel.FunctionblockProperty) Property(org.eclipse.vorto.core.api.model.datatype.Property) ModelProperty(org.eclipse.vorto.model.ModelProperty) Test(org.junit.Test)

Example 3 with EntityModel

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

the class ModelDtoFactory method createResource.

private static EntityModel createResource(Entity model, Optional<MappingModel> mappingModel) {
    EntityModel resource = new EntityModel(new ModelId(model.getName(), model.getNamespace(), model.getVersion()));
    resource.setDescription(model.getDescription());
    resource.setDisplayName(model.getDisplayname());
    resource.setReferences(model.getReferences().stream().map(reference -> createModelId(reference)).collect(Collectors.toList()));
    resource.setProperties(model.getProperties().stream().map(p -> createProperty(p, mappingModel)).collect(Collectors.toList()));
    resource.setCategory(model.getCategory());
    if (model.getSuperType() != null)
        resource.setSuperType(new ModelId(model.getSuperType().getName(), model.getSuperType().getNamespace(), model.getSuperType().getVersion()));
    if (mappingModel.isPresent()) {
        resource.setTargetPlatformKey(mappingModel.get().getTargetPlatform());
        for (MappingRule rule : getEntityRule(mappingModel.get().getRules())) {
            if (rule.getTarget() instanceof StereoTypeTarget) {
                StereoTypeTarget target = (StereoTypeTarget) rule.getTarget();
                resource.addStereotype(Stereotype.create(target.getName(), convertAttributesToMap(target.getAttributes())));
            } else if (rule.getTarget() instanceof ReferenceTarget) {
                ReferenceTarget target = (ReferenceTarget) rule.getTarget();
                resource.setMappingReference(createModelId(target.getMappingModel()));
            }
        }
    }
    return resource;
}
Also used : ReferenceTarget(org.eclipse.vorto.core.api.model.mapping.ReferenceTarget) EntityModel(org.eclipse.vorto.model.EntityModel) MappingRule(org.eclipse.vorto.core.api.model.mapping.MappingRule) StereoTypeTarget(org.eclipse.vorto.core.api.model.mapping.StereoTypeTarget) ModelId(org.eclipse.vorto.model.ModelId)

Example 4 with EntityModel

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

the class SpecWithNestedEntityAndCustomFunction method createModel.

@Override
protected void createModel() {
    EntityModel countEntity = EntityModel.Builder(ModelId.fromPrettyFormat("demo:Count:1.0.0")).property(ModelProperty.Builder("value", PrimitiveType.INT).withXPathStereotype("button:convertClickType(clickType)", "iotbutton").build()).build();
    FunctionblockModel fbm = FunctionblockModel.Builder(ModelId.fromPrettyFormat("demo.fb:PushButton:1.0.0")).statusProperty(ModelProperty.Builder("count", countEntity).build()).build();
    infomodel.getFunctionblocks().add(ModelProperty.Builder("button", fbm).build());
}
Also used : FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) EntityModel(org.eclipse.vorto.model.EntityModel)

Example 5 with EntityModel

use of org.eclipse.vorto.model.EntityModel 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)

Aggregations

EntityModel (org.eclipse.vorto.model.EntityModel)11 ModelProperty (org.eclipse.vorto.model.ModelProperty)5 FunctionblockModel (org.eclipse.vorto.model.FunctionblockModel)4 Test (org.junit.Test)4 EnumModel (org.eclipse.vorto.model.EnumModel)3 ModelContent (org.eclipse.vorto.model.ModelContent)3 ModelId (org.eclipse.vorto.model.ModelId)3 Entity (org.eclipse.vorto.core.api.model.datatype.Entity)2 Property (org.eclipse.vorto.core.api.model.datatype.Property)2 FunctionblockProperty (org.eclipse.vorto.core.api.model.informationmodel.FunctionblockProperty)2 IModel (org.eclipse.vorto.model.IModel)2 JXPathInvalidAccessException (org.apache.commons.jxpath.JXPathInvalidAccessException)1 JXPathNotFoundException (org.apache.commons.jxpath.JXPathNotFoundException)1 Enum (org.eclipse.vorto.core.api.model.datatype.Enum)1 ObjectPropertyType (org.eclipse.vorto.core.api.model.datatype.ObjectPropertyType)1 MappingRule (org.eclipse.vorto.core.api.model.mapping.MappingRule)1 ReferenceTarget (org.eclipse.vorto.core.api.model.mapping.ReferenceTarget)1 StereoTypeTarget (org.eclipse.vorto.core.api.model.mapping.StereoTypeTarget)1 MappingException (org.eclipse.vorto.mapping.engine.MappingException)1 Infomodel (org.eclipse.vorto.model.Infomodel)1