Search in sources :

Example 1 with ModelContent

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

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

the class GeneratorExecutionHandler method handleRequest.

@Override
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
    ObjectMapper mapper = ObjectMapperFactory.getInstance();
    ApiGatewayRequest request = ApiGatewayRequest.createFromJson(input);
    Optional<ICodeGenerator> generator = generators.stream().filter(gen -> gen.getMeta().getKey().equals(request.getPathParam(PLUGINKEY))).findAny();
    if (!generator.isPresent()) {
        objectMapper.writeValue(output, createHttpReponse(404));
    }
    ModelContent modelContent = mapper.readValue(request.getInput(), ModelContent.class);
    ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter();
    Model converted = converter.convert(modelContent, Optional.empty());
    InvocationContext invocationContext = InvocationContext.simpleInvocationContext(request.getQueryParams());
    InformationModel infomodel = org.eclipse.vorto.plugin.utils.Utils.toInformationModel(converted);
    try {
        IGenerationResult generatorResult = generator.get().generate(infomodel, invocationContext);
        ApiGatewayResponse validResponse = createResponse(generatorResult);
        OutputStreamWriter writer = new OutputStreamWriter(output, "UTF-8");
        writer.write(objectMapper.writeValueAsString(validResponse));
        writer.close();
    } catch (GeneratorException e) {
        ApiGatewayResponse response = createHttpReponse(500);
        objectMapper.writeValue(output, response);
    }
}
Also used : RequestStreamHandler(com.amazonaws.services.lambda.runtime.RequestStreamHandler) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) Context(com.amazonaws.services.lambda.runtime.Context) EclipseHonoGenerator(org.eclipse.vorto.codegen.hono.EclipseHonoGenerator) ApiGatewayRequest(org.eclipse.vorto.plugin.utils.ApiGatewayRequest) ApiGatewayResponse(org.eclipse.vorto.plugin.utils.ApiGatewayResponse) Model(org.eclipse.vorto.core.api.model.model.Model) EclipseDittoGenerator(org.eclipse.vorto.codegen.ditto.EclipseDittoGenerator) HashSet(java.util.HashSet) ObjectMapperFactory(org.eclipse.vorto.plugin.generator.adapter.ObjectMapperFactory) ICodeGenerator(org.eclipse.vorto.plugin.generator.ICodeGenerator) OutputStreamWriter(java.io.OutputStreamWriter) IGenerationResult(org.eclipse.vorto.plugin.generator.IGenerationResult) OutputStream(java.io.OutputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BoschIoTSuiteGenerator(org.eclipse.vorto.codegen.bosch.BoschIoTSuiteGenerator) Set(java.util.Set) IOException(java.io.IOException) GeneratorException(org.eclipse.vorto.plugin.generator.GeneratorException) OpenAPIGenerator(org.eclipse.vorto.codegen.openapi.OpenAPIGenerator) ModelContent(org.eclipse.vorto.model.ModelContent) InvocationContext(org.eclipse.vorto.plugin.generator.InvocationContext) JSONSchemaGenerator(org.eclipse.vorto.codegen.jsonschema.JSONSchemaGenerator) ModelContentToEcoreConverter(org.eclipse.vorto.model.conversion.ModelContentToEcoreConverter) Optional(java.util.Optional) InputStream(java.io.InputStream) ModelContent(org.eclipse.vorto.model.ModelContent) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) IGenerationResult(org.eclipse.vorto.plugin.generator.IGenerationResult) ModelContentToEcoreConverter(org.eclipse.vorto.model.conversion.ModelContentToEcoreConverter) ICodeGenerator(org.eclipse.vorto.plugin.generator.ICodeGenerator) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) Model(org.eclipse.vorto.core.api.model.model.Model) OutputStreamWriter(java.io.OutputStreamWriter) InvocationContext(org.eclipse.vorto.plugin.generator.InvocationContext) GeneratorException(org.eclipse.vorto.plugin.generator.GeneratorException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ApiGatewayResponse(org.eclipse.vorto.plugin.utils.ApiGatewayResponse) ApiGatewayRequest(org.eclipse.vorto.plugin.utils.ApiGatewayRequest)

Example 3 with ModelContent

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

the class ModelIdToModelContentConverterTest method testConvertWithTargetPlatform.

@Test
public void testConvertWithTargetPlatform() throws Exception {
    importModel("Color.type");
    importModel("sample.mapping");
    ModelIdToModelContentConverter converter = new ModelIdToModelContentConverter(this.repositoryFactory);
    ModelContent content = converter.convert(ModelId.fromPrettyFormat("org.eclipse.vorto.examples.type:Color:1.0.0"), Optional.of("ios"));
    assertNotNull(content);
    assertEquals(ModelId.fromPrettyFormat("org.eclipse.vorto.examples.type:Color:1.0.0"), content.getModels().get(content.getRoot()).getId());
    assertEquals("colortype", ((EntityModel) content.getModels().get(content.getRoot())).getStereotypes().get(0).getName());
}
Also used : ModelContent(org.eclipse.vorto.model.ModelContent) Test(org.junit.Test)

Example 4 with ModelContent

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

the class ModelContentDeserializationTest method testDeserializeWithDictionaryType.

@Test
public void testDeserializeWithDictionaryType() throws Exception {
    ModelContent content = ObjectMapperFactory.getInstance().readValue(new ClassPathResource("json/TestModelContent_dictionary.json").getInputStream(), ModelContent.class);
    assertNotNull(content);
    ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter();
    FunctionblockModel model = (FunctionblockModel) converter.convert(content, Optional.empty());
    assertNotNull(model.getFunctionblock().getStatus().getProperties().get(0).getType() instanceof DictionaryPropertyType);
}
Also used : FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) DictionaryPropertyType(org.eclipse.vorto.core.api.model.datatype.DictionaryPropertyType) ModelContent(org.eclipse.vorto.model.ModelContent) ModelContentToEcoreConverter(org.eclipse.vorto.model.conversion.ModelContentToEcoreConverter) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 5 with ModelContent

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

the class ModelContentDeserializationTest method testDeserializeWithPrimitiveType.

@Test
public void testDeserializeWithPrimitiveType() throws Exception {
    ModelContent content = ObjectMapperFactory.getInstance().readValue(new ClassPathResource("json/TestModelContent_primitive.json").getInputStream(), ModelContent.class);
    assertNotNull(content);
    ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter();
    FunctionblockModel model = (FunctionblockModel) converter.convert(content, Optional.empty());
    assertNotNull(model.getFunctionblock().getStatus().getProperties().get(0).getType() instanceof PrimitivePropertyType);
}
Also used : FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) ModelContent(org.eclipse.vorto.model.ModelContent) PrimitivePropertyType(org.eclipse.vorto.core.api.model.datatype.PrimitivePropertyType) ModelContentToEcoreConverter(org.eclipse.vorto.model.conversion.ModelContentToEcoreConverter) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Aggregations

ModelContent (org.eclipse.vorto.model.ModelContent)12 Test (org.junit.Test)6 EntityModel (org.eclipse.vorto.model.EntityModel)3 ModelContentToEcoreConverter (org.eclipse.vorto.model.conversion.ModelContentToEcoreConverter)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Optional (java.util.Optional)2 DictionaryPropertyType (org.eclipse.vorto.core.api.model.datatype.DictionaryPropertyType)2 PrimitivePropertyType (org.eclipse.vorto.core.api.model.datatype.PrimitivePropertyType)2 FunctionblockModel (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel)2 InformationModel (org.eclipse.vorto.core.api.model.informationmodel.InformationModel)2 Model (org.eclipse.vorto.core.api.model.model.Model)2 FunctionblockModel (org.eclipse.vorto.model.FunctionblockModel)2 Infomodel (org.eclipse.vorto.model.Infomodel)2 ModelId (org.eclipse.vorto.model.ModelId)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Context (com.amazonaws.services.lambda.runtime.Context)1 RequestStreamHandler (com.amazonaws.services.lambda.runtime.RequestStreamHandler)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1