Skip to content

Commit

Permalink
[4346] Add support for a query view
Browse files Browse the repository at this point in the history
Bug: #4346
Signed-off-by: Stéphane Bégaudeau <stephane.begaudeau@obeo.fr>
  • Loading branch information
sbegaudeau committed Dec 23, 2024
1 parent 0d6a13e commit bce2046
Show file tree
Hide file tree
Showing 22 changed files with 1,077 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

=== New Features

- https://github.com/eclipse-sirius/sirius-web/issues/4346[#4346] Add support for a query view.
Specifiers can contribute dedicated AQL services for this feature using implementations of `IInterpreterJavaServiceProvider`.


=== Improvements

Expand Down
42 changes: 42 additions & 0 deletions doc/iterations/2025.2/add_support_for_an_interpreter_view.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
= (M) Shape

== Problem

Sirius Web users need an interpreter to perform some queries, view and export the results.

== Key Result

It shall be possible to run a query against the editing context and view the result along with its type (for example to distinguish one element from a list containing only one element).
End users shall be able to export the result in CSV for example to import it in another tool.
The current selection shall be usable as an entry point of the query.
An extension point on the backend shall be available to add custom Java services too.

This new view shall be contributed using an extension point in order to be removal by downstream projects which may not need it.

The result of the expression will only be computed when the user will ask for it.
This view will not be a synchronized representation.

=== Scenario

==== An user wants to query some model

- The user open the interpreter view
- The user click on an element in the explorer or another representation like a diagram
- They start typing an expression in the interpreter view and click on a button to perform the query
- The result appears in the result viewer underneath


=== Breadboarding

- A view on the right of the workbench with a textarea to enter an expression and a viewer underneath to display the result.


=== Cutting backs

- Content assist in the interpreter.


== Rabbit holes


== No-gos
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.views.query.controllers;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Objects;
import java.util.concurrent.CompletableFuture;

import org.eclipse.sirius.components.annotations.spring.graphql.MutationDataFetcher;
import org.eclipse.sirius.components.core.api.IPayload;
import org.eclipse.sirius.components.graphql.api.IDataFetcherWithFieldCoordinates;
import org.eclipse.sirius.components.graphql.api.IEditingContextDispatcher;
import org.eclipse.sirius.components.graphql.api.IExceptionWrapper;
import org.eclipse.sirius.web.application.views.query.dto.EvaluateExpressionInput;

import graphql.schema.DataFetchingEnvironment;

/**
* The data fetcher used to evaluate an expression for the interpreter view.
*
* @author sbegaudeau
*/
@MutationDataFetcher(type = "Mutation", field = "evaluateExpression")
public class MutationEvaluateExpressionDataFetcher implements IDataFetcherWithFieldCoordinates<CompletableFuture<IPayload>> {

private static final String INPUT_ARGUMENT = "input";

private final ObjectMapper objectMapper;

private final IExceptionWrapper exceptionWrapper;

private final IEditingContextDispatcher editingContextDispatcher;

public MutationEvaluateExpressionDataFetcher(ObjectMapper objectMapper, IExceptionWrapper exceptionWrapper, IEditingContextDispatcher editingContextDispatcher) {
this.objectMapper = Objects.requireNonNull(objectMapper);
this.exceptionWrapper = Objects.requireNonNull(exceptionWrapper);
this.editingContextDispatcher = Objects.requireNonNull(editingContextDispatcher);
}

@Override
public CompletableFuture<IPayload> get(DataFetchingEnvironment environment) throws Exception {
Object argument = environment.getArgument(INPUT_ARGUMENT);
var input = this.objectMapper.convertValue(argument, EvaluateExpressionInput.class);
return this.exceptionWrapper.wrapMono(() -> this.editingContextDispatcher.dispatchMutation(input.editingContextId(), input), input).toFuture();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.views.query.dto;

/**
* Used to return a boolean value.
*
* @author sbegaudeau
*/
public record BooleanExpressionResult(boolean value) implements IEvaluateExpressionResult {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.views.query.dto;

import java.util.UUID;

import org.eclipse.sirius.components.core.api.IInput;

/**
* Used to execute an expression.
*
* @author sbegaudeau
*/
public record EvaluateExpressionInput(UUID id, String editingContextId, String expression) implements IInput {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.views.query.dto;

import java.util.UUID;

import org.eclipse.sirius.components.core.api.IPayload;

import jakarta.validation.constraints.NotNull;

/**
* Used to indicate that the expression has been successfully evaluated.
*
* @author sbegaudeau
*/
public record EvaluateExpressionSuccessPayload(
@NotNull UUID id,
@NotNull IEvaluateExpressionResult result) implements IPayload {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.views.query.dto;

public interface IEvaluateExpressionResult {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.views.query.dto;

/**
* Used to return an integer value.
*
* @author sbegaudeau
*/
public record IntExpressionResult(int value) implements IEvaluateExpressionResult {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.views.query.dto;

public record ObjectExpressionResult() implements IEvaluateExpressionResult {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.views.query.dto;

import java.util.List;

/**
* Used to return a list of objects.
*
* @author sbegaudeau
*/
public record ObjectsExpressionResult(List<Object> value) implements IEvaluateExpressionResult {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.views.query.dto;

/**
* Used to return a string based value.
*
* @author sbegaudeau
*/
public record StringExpressionResult(String value) implements IEvaluateExpressionResult {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.views.query.dto;

public record VoidExpressionResult() implements IEvaluateExpressionResult {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.views.query.services;

import java.util.List;
import java.util.Objects;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.emf.query.EditingContextServices;
import org.eclipse.sirius.components.emf.services.api.IEMFEditingContext;
import org.eclipse.sirius.components.interpreter.AQLInterpreter;
import org.eclipse.sirius.web.application.views.query.services.api.IAQLInterpreterProvider;
import org.eclipse.sirius.web.application.views.query.services.api.IInterpreterJavaServiceProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

/**
* Used to provide the AQL interpreter.
*
* @author sbegaudeau
*/
@Service
public class AQLInterpreterProvider implements IAQLInterpreterProvider {

private final List<IInterpreterJavaServiceProvider> interpreterJavaServiceProviders;

private final ApplicationContext applicationContext;

private final Logger logger = LoggerFactory.getLogger(AQLInterpreterProvider.class);

public AQLInterpreterProvider(List<IInterpreterJavaServiceProvider> interpreterJavaServiceProviders, ApplicationContext applicationContext) {
this.interpreterJavaServiceProviders = Objects.requireNonNull(interpreterJavaServiceProviders);
this.applicationContext = Objects.requireNonNull(applicationContext);
}

@Override
public AQLInterpreter getInterpreter(IEditingContext editingContext) {
var ePackages = this.getEPackages(editingContext);
var services = this.getServices(editingContext);
return new AQLInterpreter(List.of(EditingContextServices.class), services, ePackages);
}

private List<EPackage> getEPackages(IEditingContext editingContext) {
if (editingContext instanceof IEMFEditingContext emfEditingContext) {
EPackage.Registry packageRegistry = emfEditingContext.getDomain().getResourceSet().getPackageRegistry();
return packageRegistry.values().stream()
.filter(EPackage.class::isInstance)
.map(EPackage.class::cast)
.toList();
}
return List.of();
}

private List<Object> getServices(IEditingContext editingContext) {
AutowireCapableBeanFactory beanFactory = this.applicationContext.getAutowireCapableBeanFactory();
return this.interpreterJavaServiceProviders.stream()
.flatMap(provider -> provider.getServiceClasses(editingContext).stream())
.map(serviceClass -> {
try {
return beanFactory.createBean(serviceClass);
} catch (BeansException beansException) {
this.logger.warn("Error while trying to instantiate Java service class " + serviceClass.getName(), beansException);
return null;
}
})
.filter(Objects::nonNull)
.map(Object.class::cast)
.toList();
}
}
Loading

0 comments on commit bce2046

Please sign in to comment.