Search in sources :

Example 1 with IModelWorkspace

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

the class RefactoringWorkspaceTest method testChangeNamespaceIgnoringSpecificNamespace.

@Test
public void testChangeNamespaceIgnoringSpecificNamespace() {
    IModelWorkspace workspace = IModelWorkspace.newReader().addFile(getClass().getClassLoader().getResourceAsStream("dsls/Brightness.type"), ModelType.Datatype).addFile(getClass().getClassLoader().getResourceAsStream("dsls/Dimmer.type"), ModelType.Datatype).addFile(getClass().getClassLoader().getResourceAsStream("dsls/Color.fbmodel"), ModelType.Functionblock).read();
    ChangeSet changeSet = RefactoringTask.from(workspace).toNamespaceForAllModels("private.vorto.alex", "org.eclipse.vorto").execute();
    assertEquals(3, changeSet.get().size());
    assertEquals(2, changeSet.getChanges().size());
    FunctionblockModel color = (FunctionblockModel) changeSet.get().stream().filter(m -> m.getName().equals("Color")).findFirst().get();
    assertEquals("org.eclipse.vorto", color.getNamespace());
}
Also used : FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) BeforeClass(org.junit.BeforeClass) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) ModelWorkspaceReader(org.eclipse.vorto.utilities.reader.ModelWorkspaceReader) Assert.assertTrue(org.junit.Assert.assertTrue) ObjectPropertyType(org.eclipse.vorto.core.api.model.datatype.ObjectPropertyType) Test(org.junit.Test) Enum(org.eclipse.vorto.core.api.model.datatype.Enum) Entity(org.eclipse.vorto.core.api.model.datatype.Entity) Assert.assertEquals(org.junit.Assert.assertEquals) ModelType(org.eclipse.vorto.model.ModelType) Model(org.eclipse.vorto.core.api.model.model.Model) FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) Test(org.junit.Test)

Example 2 with IModelWorkspace

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

the class RefactoringWorkspaceTest method testChangeNamespaceWithoutReferences.

@Test
public void testChangeNamespaceWithoutReferences() {
    IModelWorkspace workspace = IModelWorkspace.newReader().addFile(getClass().getClassLoader().getResourceAsStream("dsls/Brightness.type"), ModelType.Datatype).read();
    ChangeSet changeSet = RefactoringTask.from(workspace).toNamespaceForAllModels("private.vorto.alex").execute();
    assertEquals(1, changeSet.get().size());
    assertEquals(1, changeSet.getChanges().size());
    assertEquals("private.vorto.alex.iot", changeSet.get().get(0).getNamespace());
}
Also used : IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) Test(org.junit.Test)

Example 3 with IModelWorkspace

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

the class VortoModelImporter method refactor.

private FileUpload refactor(FileUpload fileUpload, String targetNamespace) {
    IModelWorkspace workspace = IModelWorkspace.newReader().addFile(new ByteArrayInputStream(fileUpload.getContent()), ModelType.fromFileName(fileUpload.getFileName())).read();
    ChangeSet changeSet = RefactoringTask.from(workspace).toNamespaceForAllModels(targetNamespace).execute();
    ModelResource resource = new ModelResource(changeSet.get().get(0));
    return FileUpload.create(fileUpload.getFileName(), resource.toDSL());
}
Also used : ModelResource(org.eclipse.vorto.repository.core.ModelResource) ByteArrayInputStream(java.io.ByteArrayInputStream) ChangeSet(org.eclipse.vorto.model.refactor.ChangeSet) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace)

Example 4 with IModelWorkspace

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

the class VortoModelImporter method preValidation.

/**
 * changes the namespace of the uploaded vorto model(s) , if target namespace is specified
 */
@Override
protected FileUpload preValidation(FileUpload fileUpload, Context context) {
    if (context.getTargetNamespace().isPresent()) {
        if (fileUpload.getFileExtension().endsWith(EXTENSION_ZIP)) {
            ModelWorkspaceReader reader = IModelWorkspace.newReader();
            getUploadedFilesFromZip(fileUpload.getContent()).stream().filter(this::isSupported).forEach(extractedFile -> {
                reader.addFile(new ByteArrayInputStream(addVortolangIfMissing(extractedFile).getContent()), ModelType.fromFileName(extractedFile.getFileExtension()));
            });
            IModelWorkspace workspace = reader.read();
            ChangeSet changeSet = RefactoringTask.from(workspace).toNamespaceForAllModels(context.getTargetNamespace().get()).execute();
            ZipUploadFile zipFile = new ZipUploadFile(fileUpload.getFileName());
            for (Model model : changeSet.get()) {
                ModelResource resource = new ModelResource(model);
                zipFile.addToZip(FileUpload.create(resource.getId().getPrettyFormat().replace("\\.", "_") + resource.getType().getExtension(), resource.toDSL()));
            }
            return zipFile.getFileUpload();
        } else {
            return refactor(addVortolangIfMissing(fileUpload), context.getTargetNamespace().get());
        }
    } else {
        return addVortolangIfMissing(fileUpload);
    }
}
Also used : ModelWorkspaceReader(org.eclipse.vorto.utilities.reader.ModelWorkspaceReader) ModelResource(org.eclipse.vorto.repository.core.ModelResource) ByteArrayInputStream(java.io.ByteArrayInputStream) Model(org.eclipse.vorto.core.api.model.model.Model) ChangeSet(org.eclipse.vorto.model.refactor.ChangeSet) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace)

Example 5 with IModelWorkspace

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

the class EclipseDittoGeneratorTest method inheritanceFunctionBlockProvider.

public InformationModel inheritanceFunctionBlockProvider(String rootFunctionBlockName, String... functionBlocks) {
    ModelWorkspaceReader mwr = IModelWorkspace.newReader();
    for (String parentFb : functionBlocks) {
        mwr.addFile(getClass().getClassLoader().getResourceAsStream(folderPath + parentFb), ModelType.Functionblock);
    }
    IModelWorkspace workspace = mwr.read();
    FunctionblockModel fb = (FunctionblockModel) workspace.get().stream().filter(p -> p.getName().equals(rootFunctionBlockName)).findAny().get();
    return Utils.toInformationModel(fb);
}
Also used : InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) BeforeClass(org.junit.BeforeClass) HashMap(java.util.HashMap) JSONAssert(org.skyscreamer.jsonassert.JSONAssert) EclipseDittoGenerator(org.eclipse.vorto.codegen.ditto.EclipseDittoGenerator) JSONException(org.json.JSONException) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) Map(java.util.Map) ICodeGenerator(org.eclipse.vorto.plugin.generator.ICodeGenerator) IGenerationResult(org.eclipse.vorto.plugin.generator.IGenerationResult) Utils(org.eclipse.vorto.plugin.utils.Utils) AbstractGeneratorTest(org.eclipse.vorto.plugin.AbstractGeneratorTest) ModelId(org.eclipse.vorto.core.api.model.model.ModelId) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) BuilderUtils(org.eclipse.vorto.core.api.model.BuilderUtils) GeneratorException(org.eclipse.vorto.plugin.generator.GeneratorException) ModelType(org.eclipse.vorto.model.ModelType) File(java.io.File) IOUtils(org.apache.commons.io.IOUtils) FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) InvocationContext(org.eclipse.vorto.plugin.generator.InvocationContext) ModelWorkspaceReader(org.eclipse.vorto.utilities.reader.ModelWorkspaceReader) Generated(org.eclipse.vorto.plugin.generator.utils.Generated) FilenameUtils(org.apache.commons.io.FilenameUtils) Assert.assertEquals(org.junit.Assert.assertEquals) FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) ModelWorkspaceReader(org.eclipse.vorto.utilities.reader.ModelWorkspaceReader) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace)

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