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());
}
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);
}
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;
}
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;
}
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());
}
Aggregations