Search in sources :

Example 1 with EnumModel

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

the class ModelContentToEcoreConverterTest method testConvertFunctionblockWithEvents.

@Test
public void testConvertFunctionblockWithEvents() {
    EnumModel enumModel = EnumModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Unit:1.0.0")).literal("celcius", null).literal("kg", null).build();
    FunctionblockModel fbModel = FunctionblockModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Sensor:1.0.0")).event(ModelEvent.Builder("Exceeded").withProperty(ModelProperty.Builder("unit", enumModel.getId()).build()).build()).build();
    ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter();
    org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel model = (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) converter.convert(ModelContent.Builder(fbModel).withDependency(enumModel).build(), Optional.empty());
    assertEquals(1, model.getReferences().size());
    assertEquals(1, model.getFunctionblock().getEvents().size());
}
Also used : FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) EnumModel(org.eclipse.vorto.model.EnumModel) Infomodel(org.eclipse.vorto.model.Infomodel) Test(org.junit.Test)

Example 2 with EnumModel

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

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

the class ModelDtoFactory method createResource.

private static EnumModel createResource(Enum model, Optional<MappingModel> mappingModel) {
    EnumModel resource = new EnumModel(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.setLiterals(model.getEnums().stream().map(p -> createLiteral(p)).collect(Collectors.toList()));
    resource.setCategory(model.getCategory());
    if (mappingModel.isPresent()) {
        resource.setTargetPlatformKey(mappingModel.get().getTargetPlatform());
        for (MappingRule rule : getEnumRule(mappingModel.get().getRules())) {
            StereoTypeTarget target = (StereoTypeTarget) rule.getTarget();
            resource.addStereotype(Stereotype.create(target.getName(), convertAttributesToMap(target.getAttributes())));
        }
    }
    return resource;
}
Also used : EnumModel(org.eclipse.vorto.model.EnumModel) 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 EnumModel

use of org.eclipse.vorto.model.EnumModel 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 5 with EnumModel

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

the class ModelContentToEcoreConverterTest method testConvertFunctionblockOperationWithObjectTypes.

@Test
public void testConvertFunctionblockOperationWithObjectTypes() {
    EnumModel enumModel = EnumModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Unit:1.0.0")).literal("celcius", null).literal("kg", null).build();
    FunctionblockModel fbModel = FunctionblockModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Sensor:1.0.0")).operation(Operation.Builder("op").withParam(Param.Builder("p1", enumModel.getId()).build()).build()).operation(Operation.Builder("op2").withResult(new ReturnType(enumModel.getId(), false)).build()).build();
    ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter();
    org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel model = (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) converter.convert(ModelContent.Builder(fbModel).withDependency(enumModel).build(), Optional.empty());
    assertEquals(2, model.getFunctionblock().getOperations().size());
    assertEquals(1, model.getReferences().size());
    org.eclipse.vorto.core.api.model.functionblock.Operation op1 = model.getFunctionblock().getOperations().stream().filter(p -> p.getName().equals("op")).findAny().get();
    assertEquals(1, op1.getParams().size());
    assertEquals("Unit", ((Enum) ((RefParam) op1.getParams().get(0)).getType()).getName());
    assertEquals(2, ((Enum) ((RefParam) op1.getParams().get(0)).getType()).getEnums().size());
    org.eclipse.vorto.core.api.model.functionblock.Operation op2 = model.getFunctionblock().getOperations().stream().filter(p -> p.getName().equals("op2")).findAny().get();
    assertEquals("Unit", ((Enum) (((ReturnObjectType) op2.getReturnType()).getReturnType())).getName());
    assertEquals(2, ((Enum) (((ReturnObjectType) op2.getReturnType()).getReturnType())).getEnums().size());
}
Also used : Enum(org.eclipse.vorto.core.api.model.datatype.Enum) RefParam(org.eclipse.vorto.core.api.model.functionblock.RefParam) ReturnType(org.eclipse.vorto.model.ReturnType) FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) EnumModel(org.eclipse.vorto.model.EnumModel) Infomodel(org.eclipse.vorto.model.Infomodel) ReturnObjectType(org.eclipse.vorto.core.api.model.functionblock.ReturnObjectType) Test(org.junit.Test)

Aggregations

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