Search in sources :

Example 1 with ModelWorkspaceReader

use of org.eclipse.vorto.utilities.reader.ModelWorkspaceReader 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 2 with ModelWorkspaceReader

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

the class ModelIdToModelContentConverter method getWorkspaceForModel.

private ModelWorkspaceReader getWorkspaceForModel(final ModelId modelId) {
    Collection<ModelInfo> allModels = getModelWithAllDependencies(modelId);
    DependencyManager dm = new DependencyManager(new HashSet<>(allModels));
    allModels = dm.getSorted();
    ModelWorkspaceReader workspaceReader = IModelWorkspace.newReader();
    for (ModelInfo model : allModels) {
        FileContent modelContent = repositoryFactory.getRepositoryByModel(model.getId()).getFileContent(model.getId(), Optional.of(model.getFileName())).get();
        workspaceReader.addFile(new ByteArrayInputStream(modelContent.getContent()), model.getType());
    }
    return workspaceReader;
}
Also used : FileContent(org.eclipse.vorto.repository.core.FileContent) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ModelWorkspaceReader(org.eclipse.vorto.utilities.reader.ModelWorkspaceReader) ByteArrayInputStream(java.io.ByteArrayInputStream) DependencyManager(org.eclipse.vorto.repository.core.impl.utils.DependencyManager)

Example 3 with ModelWorkspaceReader

use of org.eclipse.vorto.utilities.reader.ModelWorkspaceReader 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)

Example 4 with ModelWorkspaceReader

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

the class AbstractRepositoryController method getWorkspaceForModel.

protected IModelWorkspace getWorkspaceForModel(final ModelId modelId) {
    List<ModelInfo> allModels = getModelWithAllDependencies(modelId);
    DependencyManager dm = new DependencyManager(new HashSet<>(allModels));
    allModels = dm.getSorted();
    ModelWorkspaceReader workspaceReader = IModelWorkspace.newReader();
    for (ModelInfo model : allModels) {
        FileContent modelContent = getModelRepository(model.getId()).getFileContent(model.getId(), Optional.of(model.getFileName())).get();
        workspaceReader.addFile(new ByteArrayInputStream(modelContent.getContent()), model.getType());
    }
    return workspaceReader.read();
}
Also used : FileContent(org.eclipse.vorto.repository.core.FileContent) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ModelWorkspaceReader(org.eclipse.vorto.utilities.reader.ModelWorkspaceReader) ByteArrayInputStream(java.io.ByteArrayInputStream) DependencyManager(org.eclipse.vorto.repository.core.impl.utils.DependencyManager)

Example 5 with ModelWorkspaceReader

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

the class EclipseDittoGeneratorTest method modelProvider.

public InformationModel modelProvider(String... filenames) {
    ModelWorkspaceReader mwr = IModelWorkspace.newReader();
    for (String filename : filenames) {
        mwr.addFile(getClass().getClassLoader().getResourceAsStream(folderPath + filename), FILENAME_TO_TYPE_MAP.get(FilenameUtils.getExtension(filename)));
    }
    IModelWorkspace workspace = mwr.read();
    InformationModel model = (InformationModel) workspace.get().stream().filter(p -> p instanceof InformationModel).findAny().get();
    return model;
}
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) ModelWorkspaceReader(org.eclipse.vorto.utilities.reader.ModelWorkspaceReader) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace)

Aggregations

ModelWorkspaceReader (org.eclipse.vorto.utilities.reader.ModelWorkspaceReader)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ModelInfo (org.eclipse.vorto.repository.core.ModelInfo)4 IModelWorkspace (org.eclipse.vorto.utilities.reader.IModelWorkspace)4 FileContent (org.eclipse.vorto.repository.core.FileContent)3 DependencyManager (org.eclipse.vorto.repository.core.impl.utils.DependencyManager)3 File (java.io.File)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 FileUtils (org.apache.commons.io.FileUtils)2 FilenameUtils (org.apache.commons.io.FilenameUtils)2 IOUtils (org.apache.commons.io.IOUtils)2 EclipseDittoGenerator (org.eclipse.vorto.codegen.ditto.EclipseDittoGenerator)2 BuilderUtils (org.eclipse.vorto.core.api.model.BuilderUtils)2 FunctionblockModel (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel)2 InformationModel (org.eclipse.vorto.core.api.model.informationmodel.InformationModel)2 ModelId (org.eclipse.vorto.core.api.model.model.ModelId)2 ModelType (org.eclipse.vorto.model.ModelType)2 AbstractGeneratorTest (org.eclipse.vorto.plugin.AbstractGeneratorTest)2 GeneratorException (org.eclipse.vorto.plugin.generator.GeneratorException)2