use of org.eclipse.vorto.utilities.reader.IModelWorkspace in project vorto by eclipse.
the class RefactoringWorkspaceTest method testChangeNamespaceWithReferences.
@Test
public void testChangeNamespaceWithReferences() {
IModelWorkspace workspace = IModelWorkspace.newReader().addFile(getClass().getClassLoader().getResourceAsStream("dsls/Brightness.type"), ModelType.Datatype).addFile(getClass().getClassLoader().getResourceAsStream("dsls/Dimmer.type"), ModelType.Datatype).read();
ChangeSet changeSet = RefactoringTask.from(workspace).toNamespaceForAllModels("private.vorto.alex").execute();
assertEquals(2, changeSet.get().size());
assertEquals(2, changeSet.getChanges().size());
Entity dimmer = (Entity) changeSet.get().stream().filter(m -> m.getName().equals("Dimmer")).findFirst().get();
assertEquals(2, dimmer.getReferences().size());
assertEquals("private.vorto.alex.iot", dimmer.getNamespace());
assertEquals("private.vorto.alex.iot.Brightness", dimmer.getReferences().get(0).getImportedNamespace());
assertEquals("org.eclipse.vorto.Temperature", dimmer.getReferences().get(1).getImportedNamespace());
ObjectPropertyType type = (ObjectPropertyType) dimmer.getProperties().get(0).getType();
assertTrue(type.getType() instanceof Enum);
assertEquals("private.vorto.alex.iot", type.getType().getNamespace());
}
use of org.eclipse.vorto.utilities.reader.IModelWorkspace in project vorto by eclipse.
the class RefactoringWorkspaceTest method testChangeNamespaceWithManyReferences.
@Test
public void testChangeNamespaceWithManyReferences() {
IModelWorkspace workspace = IModelWorkspace.newReader().addFile(getClass().getClassLoader().getResourceAsStream("dsls/Unit.type"), ModelType.Datatype).addFile(getClass().getClassLoader().getResourceAsStream("dsls/ConnectivityStatus.type"), ModelType.Datatype).addFile(getClass().getClassLoader().getResourceAsStream("dsls/Connectivity.fbmodel"), ModelType.Functionblock).read();
ChangeSet changeSet = RefactoringTask.from(workspace).toNamespaceForAllModels("vorto.private.alex").execute();
assertEquals(3, changeSet.get().size());
assertEquals(3, changeSet.getChanges().size());
Model fbmodel = changeSet.get().stream().filter(c -> c.getName().equals("Connectivity")).findAny().get();
assertEquals("vorto.private.alex.org.eclipse.vorto", fbmodel.getNamespace());
assertEquals("vorto.private.alex.org.eclipse.vorto.types.ConnectivityStatus", fbmodel.getReferences().get(0).getImportedNamespace());
assertEquals("org.eclipse.vorto.types.SomeUnit", fbmodel.getReferences().get(1).getImportedNamespace());
}
use of org.eclipse.vorto.utilities.reader.IModelWorkspace in project vorto by eclipse.
the class DefaultPayloadMappingService method getModelContentByModelAndMappingId.
private IModel getModelContentByModelAndMappingId(final String _modelId, @PathVariable final String mappingId) {
final ModelId modelId = ModelId.fromPrettyFormat(_modelId);
final ModelId mappingModelId = ModelId.fromPrettyFormat(mappingId);
IModelRepository repository = this.modelRepositoryFactory.getRepositoryByModel(modelId);
ModelInfo vortoModelInfo = repository.getById(modelId);
ModelInfo mappingModelInfo = this.modelRepositoryFactory.getRepositoryByModel(mappingModelId).getById(mappingModelId);
if (vortoModelInfo == null) {
throw new ModelNotFoundException(String.format("Could not find vorto model with ID: %s", modelId));
} else if (mappingModelInfo == null) {
throw new ModelNotFoundException(String.format("Could not find mapping with ID: %s", mappingId));
}
IModelWorkspace mappingWorkspace = getWorkspaceForModel(mappingModelInfo.getId());
Optional<Model> model = mappingWorkspace.get().stream().filter(_model -> ModelUtils.fromEMFModelId(ModelIdFactory.newInstance(_model)).equals(vortoModelInfo.getId())).findFirst();
if (model.isPresent()) {
final Model flattenedModel = ModelConversionUtils.convertToFlatHierarchy(model.get());
return ModelDtoFactory.createResource(flattenedModel, Optional.of((MappingModel) mappingWorkspace.get().stream().filter(_model -> _model instanceof MappingModel && mappingMatchesModelId((MappingModel) _model, vortoModelInfo)).findFirst().get()));
} else {
return null;
}
}
use of org.eclipse.vorto.utilities.reader.IModelWorkspace in project vorto by eclipse.
the class ModelIdToModelContentConverter method convert.
@Override
public ModelContent convert(ModelId modelId, Optional<String> platformKey) {
modelId = repositoryFactory.getRepositoryByNamespace(modelId.getNamespace()).getLatestModelVersionIfLatestTagIsSet(modelId);
if (!repositoryFactory.getRepositoryByModel(modelId).exists(modelId)) {
throw new ModelNotFoundException(String.format("Model [%s] does not exist", modelId.getPrettyFormat()), null);
}
ModelWorkspaceReader workspaceReader = getWorkspaceForModel(modelId);
ModelContent result = new ModelContent();
result.setRoot(modelId);
if (platformKey.isPresent()) {
final List<ModelInfo> mappingResources = repositoryFactory.getRepositoryByModel(modelId).getMappingModelsForTargetPlatform(modelId, platformKey.get(), Optional.empty());
if (!mappingResources.isEmpty()) {
// adding to workspace reader in order to resolve cross linking between mapping models correctly
mappingResources.forEach(mapping -> workspaceReader.addFile(new ByteArrayInputStream(repositoryFactory.getRepositoryByModel(mapping.getId()).getFileContent(mapping.getId(), Optional.empty()).get().getContent()), org.eclipse.vorto.model.ModelType.Mapping));
final IModelWorkspace workspace = workspaceReader.read();
workspace.get().forEach(model -> {
Optional<MappingModel> mappingModel = getMappingModelForModel(mappingResources, model);
if (mappingModel.isPresent()) {
AbstractModel createdModel = ModelDtoFactory.createResource(flattenHierarchy(model), mappingModel);
createdModel.setTargetPlatformKey(platformKey.get());
result.getModels().put(new ModelId(model.getName(), model.getNamespace(), model.getVersion()), createdModel);
} else {
result.getModels().put(new ModelId(model.getName(), model.getNamespace(), model.getVersion()), ModelDtoFactory.createResource(flattenHierarchy(model), Optional.empty()));
}
});
} else {
final IModelWorkspace workspace = workspaceReader.read();
workspace.get().forEach(model -> {
AbstractModel createdModel = ModelDtoFactory.createResource(flattenHierarchy(model), Optional.empty());
createdModel.setTargetPlatformKey(platformKey.get());
result.getModels().put(new ModelId(model.getName(), model.getNamespace(), model.getVersion()), createdModel);
});
}
} else {
final IModelWorkspace workspace = workspaceReader.read();
workspace.get().forEach(model -> {
AbstractModel createdModel = ModelDtoFactory.createResource(flattenHierarchy(model), Optional.empty());
result.getModels().put(new ModelId(model.getName(), model.getNamespace(), model.getVersion()), createdModel);
});
}
return result;
}
use of org.eclipse.vorto.utilities.reader.IModelWorkspace in project vorto by eclipse.
the class VortoService method getMappings.
public List<MappingModel> getMappings(String generatorKey, String namespace, String name, String version, Optional<String> headerAuth) {
Optional<byte[]> mappingResources = downloadUrl(urlForMapping(generatorKey, namespace, name, version), headerAuth);
if (mappingResources.isPresent()) {
IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(mappingResources.get()))).read();
List<Model> models = workspace.get().stream().filter(p -> p instanceof MappingModel).collect(Collectors.toList());
return models.stream().map(MappingModel.class::cast).collect(Collectors.toList());
} else {
return Collections.emptyList();
}
}
Aggregations