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