use of org.eclipse.vorto.model.Stereotype in project vorto by eclipse.
the class DataMapperJxpath method mapTarget.
@Override
public Object mapTarget(PropertyValue newValue, Optional<PropertyValue> oldValue, String infomodelProperty) {
FunctionblockModel fbm = this.specification.getFunctionBlock(infomodelProperty);
if (fbm == null) {
throw new IllegalArgumentException("No property with the given name could be found in Information Model");
}
Optional<Stereotype> targetStereotype = newValue.getMeta().getStereotype(STEREOTYPE_TARGET);
if (!targetStereotype.isPresent()) {
throw new MappingException("No mapping rule defined for property");
}
Map<String, Object> jxpathContext = new HashMap<String, Object>();
Map<String, Object> param = new HashMap<String, Object>();
param.put("newValue", newValue.getValue());
param.put("oldValue", oldValue.isPresent() ? oldValue.get().getValue() : null);
jxpathContext.put("ctx", param);
final String functionName = "convert" + newValue.getMeta().getName().substring(0, 1).toUpperCase() + newValue.getMeta().getName().substring(1);
final String xpath = infomodelProperty.toLowerCase() + ":" + functionName + "(ctx)";
JXPathContext context = jxpathHelper.newContext(jxpathContext);
try {
return context.getValue(xpath);
} catch (Exception ex) {
throw new MappingException("Problem occurred during mapping", ex);
}
}
use of org.eclipse.vorto.model.Stereotype 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.Stereotype in project vorto by eclipse.
the class DataMapperJxpath method matchesCondition.
private boolean matchesCondition(FunctionblockModel fbModel, JXPathContext context) {
Optional<Stereotype> conditionStereotype = fbModel.getStereotype("condition");
if (conditionStereotype.isPresent() && conditionStereotype.get().hasAttribute("value")) {
Expression e = jexlEngine.createExpression(normalizeCondition(conditionStereotype.get().getAttributes().get("value")));
JexlContext jc = new ObjectContext<Object>(jexlEngine, context.getContextBean());
jc.set("this", context.getContextBean());
jc.set("obj", context.getContextBean());
return (boolean) e.evaluate(jc);
} else {
return true;
}
}
use of org.eclipse.vorto.model.Stereotype in project vorto by eclipse.
the class ModelContentToEcoreConverter method convertFunctionblockMapping.
private Model convertFunctionblockMapping(FunctionblockModel model, ModelContent context, String platformKey) {
MappingBuilder builder = BuilderUtils.newMapping(new ModelId(ModelType.InformationModel, model.getId().getName() + "Mapping", model.getId().getNamespace() + ".mapping." + platformKey, model.getId().getVersion()), platformKey);
builder.withVortolang("1.0");
builder.withDescription("Mapping that contains " + platformKey + " specific meta data");
builder.withReference(ModelIdFactory.newInstance(ModelType.Functionblock, model.getId().getNamespace(), model.getId().getVersion(), model.getId().getVersion()));
org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel eFbm = convertFunctionblock(model, context);
for (Stereotype stereotype : model.getStereotypes()) {
MappingRuleBuilder ruleBuilder = new MappingRuleBuilder();
ruleBuilder.withStereotypeTarget(stereotype.getName(), stereotype.getAttributes());
FunctionBlockSource source = MappingFactory.eINSTANCE.createFunctionBlockSource();
source.setModel(eFbm);
ruleBuilder.withSource(source);
builder.addRule(ruleBuilder.build());
}
for (ModelProperty statusProperty : model.getStatusProperties()) {
for (Stereotype stereotype : statusProperty.getStereotypes()) {
MappingRuleBuilder ruleBuilder = new MappingRuleBuilder();
ruleBuilder.withStereotypeTarget(stereotype.getName(), stereotype.getAttributes());
StatusSource source = MappingFactory.eINSTANCE.createStatusSource();
source.setModel(eFbm);
source.setProperty(eFbm.getFunctionblock().getStatus().getProperties().stream().filter(property -> property.getName().equals(statusProperty.getName())).findAny().get());
ruleBuilder.withSource(source);
builder.addRule(ruleBuilder.build());
}
}
for (ModelProperty configProperty : model.getConfigurationProperties()) {
for (Stereotype stereotype : configProperty.getStereotypes()) {
MappingRuleBuilder ruleBuilder = new MappingRuleBuilder();
ruleBuilder.withStereotypeTarget(stereotype.getName(), stereotype.getAttributes());
ConfigurationSource source = MappingFactory.eINSTANCE.createConfigurationSource();
source.setModel(eFbm);
source.setProperty(eFbm.getFunctionblock().getConfiguration().getProperties().stream().filter(property -> property.getName().equals(configProperty.getName())).findAny().get());
ruleBuilder.withSource(source);
builder.addRule(ruleBuilder.build());
}
}
return builder.build();
}
use of org.eclipse.vorto.model.Stereotype in project vorto by eclipse.
the class MappingSpecification method getScriptFunctions.
@Override
public FunctionLibrary getScriptFunctions(IScriptEvalProvider factory) {
FunctionLibrary library = new FunctionLibrary();
this.infoModel.getFunctionblocks().stream().forEach(property -> {
IMappedElement mappedElement = (IMappedElement) property.getType();
if (mappedElement.getStereotype(STEREOTYPE_FUNCTIONS).isPresent()) {
Stereotype functionsStereoType = mappedElement.getStereotype(STEREOTYPE_FUNCTIONS).get();
IScriptEvaluator evaluator = factory.createEvaluator(property.getName().toLowerCase());
functionsStereoType.getAttributes().keySet().stream().filter(functionName -> !functionName.equalsIgnoreCase(STEREOTYPE_NAMESPACE)).forEach(functionName -> evaluator.addScriptFunction(new ScriptClassFunction(functionName, functionsStereoType.getAttributes().get(functionName))));
library.addFunctions(evaluator.getFunctions());
}
});
return library;
}
Aggregations