Search in sources :

Example 1 with Stereotype

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);
    }
}
Also used : FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) JXPathContext(org.apache.commons.jxpath.JXPathContext) HashMap(java.util.HashMap) Stereotype(org.eclipse.vorto.model.Stereotype) JXPathNotFoundException(org.apache.commons.jxpath.JXPathNotFoundException) JXPathInvalidAccessException(org.apache.commons.jxpath.JXPathInvalidAccessException) MappingException(org.eclipse.vorto.mapping.engine.MappingException) MappingException(org.eclipse.vorto.mapping.engine.MappingException)

Example 2 with Stereotype

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;
}
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 3 with Stereotype

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;
    }
}
Also used : Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) Stereotype(org.eclipse.vorto.model.Stereotype) ObjectContext(org.apache.commons.jexl2.ObjectContext)

Example 4 with Stereotype

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();
}
Also used : EntityBuilder(org.eclipse.vorto.core.api.model.BuilderUtils.EntityBuilder) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) ReturnPrimitiveType(org.eclipse.vorto.core.api.model.functionblock.ReturnPrimitiveType) ModelIdFactory(org.eclipse.vorto.core.api.model.model.ModelIdFactory) MappingPackageImpl(org.eclipse.vorto.core.api.model.mapping.impl.MappingPackageImpl) Entity(org.eclipse.vorto.core.api.model.datatype.Entity) DictionaryPropertyType(org.eclipse.vorto.core.api.model.datatype.DictionaryPropertyType) PrimitivePropertyType(org.eclipse.vorto.core.api.model.datatype.PrimitivePropertyType) FunctionblockBuilder(org.eclipse.vorto.core.api.model.BuilderUtils.FunctionblockBuilder) RefParam(org.eclipse.vorto.core.api.model.functionblock.RefParam) MappingBuilder(org.eclipse.vorto.core.api.model.BuilderUtils.MappingBuilder) MappingRuleBuilder(org.eclipse.vorto.core.api.model.BuilderUtils.MappingRuleBuilder) ReturnObjectType(org.eclipse.vorto.core.api.model.functionblock.ReturnObjectType) Operation(org.eclipse.vorto.model.Operation) Type(org.eclipse.vorto.core.api.model.datatype.Type) IModel(org.eclipse.vorto.model.IModel) ReturnType(org.eclipse.vorto.core.api.model.functionblock.ReturnType) ConfigurationSource(org.eclipse.vorto.core.api.model.mapping.ConfigurationSource) ReturnDictonaryType(org.eclipse.vorto.core.api.model.functionblock.ReturnDictonaryType) ModelId(org.eclipse.vorto.core.api.model.model.ModelId) ModelBuilder(org.eclipse.vorto.core.api.model.BuilderUtils.ModelBuilder) Collectors(java.util.stream.Collectors) FunctionblockPackageImpl(org.eclipse.vorto.core.api.model.functionblock.impl.FunctionblockPackageImpl) InformationModelPackageImpl(org.eclipse.vorto.core.api.model.informationmodel.impl.InformationModelPackageImpl) List(java.util.List) DictionaryType(org.eclipse.vorto.model.DictionaryType) ModelEvent(org.eclipse.vorto.model.ModelEvent) PrimitiveType(org.eclipse.vorto.model.PrimitiveType) Optional(java.util.Optional) Constraint(org.eclipse.vorto.model.Constraint) Infomodel(org.eclipse.vorto.model.Infomodel) DatatypePackageImpl(org.eclipse.vorto.core.api.model.datatype.impl.DatatypePackageImpl) StatusSource(org.eclipse.vorto.core.api.model.mapping.StatusSource) ArrayList(java.util.ArrayList) EnumBuilder(org.eclipse.vorto.core.api.model.BuilderUtils.EnumBuilder) Model(org.eclipse.vorto.core.api.model.model.Model) ConstraintIntervalType(org.eclipse.vorto.core.api.model.datatype.ConstraintIntervalType) IReferenceType(org.eclipse.vorto.model.IReferenceType) Property(org.eclipse.vorto.core.api.model.datatype.Property) FunctionblockFactory(org.eclipse.vorto.core.api.model.functionblock.FunctionblockFactory) MappingFactory(org.eclipse.vorto.core.api.model.mapping.MappingFactory) DatatypeFactory(org.eclipse.vorto.core.api.model.datatype.DatatypeFactory) PrimitiveParam(org.eclipse.vorto.core.api.model.functionblock.PrimitiveParam) EntityModel(org.eclipse.vorto.model.EntityModel) Presence(org.eclipse.vorto.core.api.model.datatype.Presence) ModelProperty(org.eclipse.vorto.model.ModelProperty) PropertyType(org.eclipse.vorto.core.api.model.datatype.PropertyType) InformationModelBuilder(org.eclipse.vorto.core.api.model.BuilderUtils.InformationModelBuilder) Param(org.eclipse.vorto.core.api.model.functionblock.Param) BuilderUtils(org.eclipse.vorto.core.api.model.BuilderUtils) FunctionBlockSource(org.eclipse.vorto.core.api.model.mapping.FunctionBlockSource) EventBuilder(org.eclipse.vorto.core.api.model.BuilderUtils.EventBuilder) DictonaryParam(org.eclipse.vorto.core.api.model.functionblock.DictonaryParam) FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) Stereotype(org.eclipse.vorto.model.Stereotype) ModelContent(org.eclipse.vorto.model.ModelContent) ModelType(org.eclipse.vorto.core.api.model.model.ModelType) EnumModel(org.eclipse.vorto.model.EnumModel) ObjectPropertyType(org.eclipse.vorto.core.api.model.datatype.ObjectPropertyType) ConfigurationSource(org.eclipse.vorto.core.api.model.mapping.ConfigurationSource) FunctionBlockSource(org.eclipse.vorto.core.api.model.mapping.FunctionBlockSource) Stereotype(org.eclipse.vorto.model.Stereotype) MappingBuilder(org.eclipse.vorto.core.api.model.BuilderUtils.MappingBuilder) StatusSource(org.eclipse.vorto.core.api.model.mapping.StatusSource) ModelProperty(org.eclipse.vorto.model.ModelProperty) ModelId(org.eclipse.vorto.core.api.model.model.ModelId) MappingRuleBuilder(org.eclipse.vorto.core.api.model.BuilderUtils.MappingRuleBuilder)

Example 5 with Stereotype

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;
}
Also used : Stereotype(org.eclipse.vorto.model.Stereotype) IScriptEvalProvider(org.eclipse.vorto.mapping.engine.functions.IScriptEvalProvider) FunctionLibrary(org.apache.commons.jxpath.FunctionLibrary) IScriptEvaluator(org.eclipse.vorto.mapping.engine.functions.IScriptEvaluator) Infomodel(org.eclipse.vorto.model.Infomodel) ScriptClassFunction(org.eclipse.vorto.mapping.engine.functions.ScriptClassFunction) IMappedElement(org.eclipse.vorto.model.IMappedElement) FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) IScriptEvaluator(org.eclipse.vorto.mapping.engine.functions.IScriptEvaluator) IMappedElement(org.eclipse.vorto.model.IMappedElement) FunctionLibrary(org.apache.commons.jxpath.FunctionLibrary) Stereotype(org.eclipse.vorto.model.Stereotype) ScriptClassFunction(org.eclipse.vorto.mapping.engine.functions.ScriptClassFunction)

Aggregations

Stereotype (org.eclipse.vorto.model.Stereotype)5 FunctionblockModel (org.eclipse.vorto.model.FunctionblockModel)3 JXPathInvalidAccessException (org.apache.commons.jxpath.JXPathInvalidAccessException)2 JXPathNotFoundException (org.apache.commons.jxpath.JXPathNotFoundException)2 MappingException (org.eclipse.vorto.mapping.engine.MappingException)2 EntityModel (org.eclipse.vorto.model.EntityModel)2 EnumModel (org.eclipse.vorto.model.EnumModel)2 IModel (org.eclipse.vorto.model.IModel)2 ModelProperty (org.eclipse.vorto.model.ModelProperty)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Expression (org.apache.commons.jexl2.Expression)1 JexlContext (org.apache.commons.jexl2.JexlContext)1 ObjectContext (org.apache.commons.jexl2.ObjectContext)1 FunctionLibrary (org.apache.commons.jxpath.FunctionLibrary)1 JXPathContext (org.apache.commons.jxpath.JXPathContext)1 BuilderUtils (org.eclipse.vorto.core.api.model.BuilderUtils)1