Search in sources :

Example 6 with IModelWorkspace

use of org.eclipse.vorto.utilities.reader.IModelWorkspace in project vorto by eclipse.

the class VortoService method getModel.

public Optional<InformationModel> getModel(String namespace, String name, String version, Optional<String> headerAuth) {
    Optional<byte[]> modelResources = downloadUrl(urlForModel(namespace, name, version), headerAuth);
    if (!modelResources.isPresent()) {
        return Optional.empty();
    }
    IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(modelResources.get()))).read();
    return Optional.of(Utils.toInformationModel(workspace.get().stream().filter(p -> p.getName().equals(name)).findFirst().get()));
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace)

Example 7 with IModelWorkspace

use of org.eclipse.vorto.utilities.reader.IModelWorkspace in project vorto by eclipse.

the class RemoteImporter method convert.

@Override
protected List<ModelResource> convert(FileUpload fileUpload, Context context) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
    body.add("file", new FileContentResource(fileUpload));
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
    ResponseEntity<byte[]> conversionResult = restTemplate.postForEntity(this.endpointUrl + "/api/2/plugins/importers/{pluginkey}/file_conversion", requestEntity, byte[].class, this.info.getKey());
    IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(conversionResult.getBody()))).read();
    List<ModelResource> resources = new ArrayList<>();
    ChangeSet changeSet = RefactoringTask.from(workspace).toNamespaceForAllModels(context.getTargetNamespace().get()).execute();
    for (Model model : changeSet.get()) {
        resources.add(new ModelResource(model));
    }
    return resources;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ArrayList(java.util.ArrayList) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) ZipInputStream(java.util.zip.ZipInputStream) ModelResource(org.eclipse.vorto.repository.core.ModelResource) ByteArrayInputStream(java.io.ByteArrayInputStream) Model(org.eclipse.vorto.core.api.model.model.Model) FileContentResource(org.eclipse.vorto.repository.plugin.FileContentResource) ChangeSet(org.eclipse.vorto.model.refactor.ChangeSet) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 8 with IModelWorkspace

use of org.eclipse.vorto.utilities.reader.IModelWorkspace in project vorto by eclipse.

the class ModelReaderTest method testFlatInheritanceFBwithDatatypes.

@Test
public void testFlatInheritanceFBwithDatatypes() {
    IModelWorkspace workspace = IModelWorkspace.newReader().addFile(getClass().getClassLoader().getResourceAsStream("dsls/Brightness.type"), ModelType.Datatype).addFile(getClass().getClassLoader().getResourceAsStream("dsls/Light.type"), ModelType.Datatype).addFile(getClass().getClassLoader().getResourceAsStream("dsls/ColorLight.type"), ModelType.Datatype).addFile(getClass().getClassLoader().getResourceAsStream("dsls/TestModel.infomodel"), ModelType.InformationModel).addFile(getClass().getClassLoader().getResourceAsStream("dsls/SomeFb.fbmodel"), ModelType.Functionblock).addFile(getClass().getClassLoader().getResourceAsStream("dsls/SuperFb.fbmodel"), ModelType.Functionblock).addFile(getClass().getClassLoader().getResourceAsStream("dsls/SuperSuperFb.fbmodel"), ModelType.Functionblock).read();
    InformationModel infomodel = ModelConversionUtils.convertToFlatHierarchy((InformationModel) workspace.get().get(3));
    assertEquals("TestModel", infomodel.getName());
    Property colorLightProperty = infomodel.getProperties().get(0).getType().getFunctionblock().getStatus().getProperties().stream().filter(p -> p.getName().equals("light")).findFirst().get();
    assertNotNull(colorLightProperty);
    ObjectPropertyType type = (ObjectPropertyType) colorLightProperty.getType();
    Entity colorLight = (Entity) type.getType();
    assertEquals(2, colorLight.getProperties().size());
    assertEquals(1, colorLight.getReferences().size());
    assertEquals(1, infomodel.getProperties().get(0).getType().getReferences().size());
    assertEquals("iot.ColorLight", infomodel.getProperties().get(0).getType().getReferences().get(0).getImportedNamespace());
}
Also used : Entity(org.eclipse.vorto.core.api.model.datatype.Entity) ObjectPropertyType(org.eclipse.vorto.core.api.model.datatype.ObjectPropertyType) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) Property(org.eclipse.vorto.core.api.model.datatype.Property) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) Test(org.junit.Test)

Example 9 with IModelWorkspace

use of org.eclipse.vorto.utilities.reader.IModelWorkspace in project vorto by eclipse.

the class ModelReaderTest method testReadInfomodelFromZipFile.

@Test
public void testReadInfomodelFromZipFile() throws Exception {
    IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(getClass().getClassLoader().getResourceAsStream("models.zip"))).read();
    Model model = workspace.get().stream().filter(p -> p.getName().equals("TI_SensorTag_CC2650")).findAny().get();
    assertNotNull(model);
    assertTrue(model instanceof InformationModel);
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Model(org.eclipse.vorto.core.api.model.model.Model) FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) Test(org.junit.Test)

Example 10 with IModelWorkspace

use of org.eclipse.vorto.utilities.reader.IModelWorkspace in project vorto by eclipse.

the class ModelReaderTest method testFlatInheritanceFBMultiLevel.

/**
 * Tests the flattening mechanism for multi level inheritance in function blocks
 */
@Test
public void testFlatInheritanceFBMultiLevel() {
    IModelWorkspace workspace = IModelWorkspace.newReader().addFile(getClass().getClassLoader().getResourceAsStream("dsls/ParentFbToBeExtended.functionblock"), ModelType.Functionblock).addFile(getClass().getClassLoader().getResourceAsStream("dsls/TestFb.functionblock"), ModelType.Functionblock).addFile(getClass().getClassLoader().getResourceAsStream("dsls/SubTestFb.functionblock"), ModelType.Functionblock).read();
    List<Model> modelList = new ArrayList<>();
    workspace.get().forEach(model -> {
        modelList.add(ModelConversionUtils.convertToFlatHierarchy(model));
    });
    FunctionblockModel fbm = (FunctionblockModel) modelList.get(2);
    assertEquals("SubTestFb", fbm.getName());
    // check status props
    Status statusProps = fbm.getFunctionblock().getStatus();
    assertEquals(4, statusProps.getProperties().size());
    Optional<Property> propFound = statusProps.getProperties().stream().filter(p -> p.getName().equals("myFloatInSubTestFb")).findFirst();
    Assert.assertTrue(propFound.isPresent());
    propFound = statusProps.getProperties().stream().filter(p -> p.getName().equals("myFloatInTestFb")).findFirst();
    Assert.assertTrue(propFound.isPresent());
    propFound = statusProps.getProperties().stream().filter(p -> p.getName().equals("ParentFloat")).findFirst();
    Assert.assertTrue(propFound.isPresent());
    propFound = statusProps.getProperties().stream().filter(p -> p.getName().equals("Parent2ndFloat")).findFirst();
    Assert.assertTrue(propFound.isPresent());
    // check config props
    Configuration config = fbm.getFunctionblock().getConfiguration();
    assertEquals(4, config.getProperties().size());
    propFound = config.getProperties().stream().filter(p -> p.getName().equals("temperatureSub")).findFirst();
    Assert.assertTrue(propFound.isPresent());
    propFound = config.getProperties().stream().filter(p -> p.getName().equals("temperatureTest")).findFirst();
    Assert.assertTrue(propFound.isPresent());
    propFound = config.getProperties().stream().filter(p -> p.getName().equals("temperatureParent")).findFirst();
    Assert.assertTrue(propFound.isPresent());
    propFound = config.getProperties().stream().filter(p -> p.getName().equals("temperatureParent2")).findFirst();
    Assert.assertTrue(propFound.isPresent());
}
Also used : Status(org.eclipse.vorto.core.api.model.functionblock.Status) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) ZipInputStream(java.util.zip.ZipInputStream) BeforeClass(org.junit.BeforeClass) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Test(org.junit.Test) Configuration(org.eclipse.vorto.core.api.model.functionblock.Configuration) Entity(org.eclipse.vorto.core.api.model.datatype.Entity) Collectors(java.util.stream.Collectors) ModelType(org.eclipse.vorto.model.ModelType) ModelConversionUtils(org.eclipse.vorto.core.api.model.ModelConversionUtils) Model(org.eclipse.vorto.core.api.model.model.Model) ArrayList(java.util.ArrayList) List(java.util.List) FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) Property(org.eclipse.vorto.core.api.model.datatype.Property) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) Status(org.eclipse.vorto.core.api.model.functionblock.Status) ModelWorkspaceReader(org.eclipse.vorto.utilities.reader.ModelWorkspaceReader) ObjectPropertyType(org.eclipse.vorto.core.api.model.datatype.ObjectPropertyType) Optional(java.util.Optional) FunctionBlock(org.eclipse.vorto.core.api.model.functionblock.FunctionBlock) Assert(org.junit.Assert) FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) Configuration(org.eclipse.vorto.core.api.model.functionblock.Configuration) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Model(org.eclipse.vorto.core.api.model.model.Model) FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) ArrayList(java.util.ArrayList) Property(org.eclipse.vorto.core.api.model.datatype.Property) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) Test(org.junit.Test)

Aggregations

IModelWorkspace (org.eclipse.vorto.utilities.reader.IModelWorkspace)27 Test (org.junit.Test)19 FunctionblockModel (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel)16 Model (org.eclipse.vorto.core.api.model.model.Model)15 InformationModel (org.eclipse.vorto.core.api.model.informationmodel.InformationModel)13 ModelWorkspaceReader (org.eclipse.vorto.utilities.reader.ModelWorkspaceReader)11 ZipInputStream (java.util.zip.ZipInputStream)10 MappingModel (org.eclipse.vorto.core.api.model.mapping.MappingModel)8 ModelType (org.eclipse.vorto.model.ModelType)8 BeforeClass (org.junit.BeforeClass)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Entity (org.eclipse.vorto.core.api.model.datatype.Entity)7 ObjectPropertyType (org.eclipse.vorto.core.api.model.datatype.ObjectPropertyType)7 Property (org.eclipse.vorto.core.api.model.datatype.Property)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Optional (java.util.Optional)6 ModelConversionUtils (org.eclipse.vorto.core.api.model.ModelConversionUtils)6 Collectors (java.util.stream.Collectors)5 FunctionBlock (org.eclipse.vorto.core.api.model.functionblock.FunctionBlock)5