diff options
| author | Stéphane Bégaudeau | 2018-05-30 09:23:05 +0000 |
|---|---|---|
| committer | Stéphane Bégaudeau | 2018-07-24 16:17:44 +0000 |
| commit | 8a9d068ae67d693435f677bf033799a169888b68 (patch) | |
| tree | 99c8668dec890cfd43f1f23bb0fbe282d2e34f02 | |
| parent | d37f66f706862b00b0c18fd08df9cc7ba82c6b8b (diff) | |
| download | org.eclipse.sirius-8a9d068ae67d693435f677bf033799a169888b68.tar.gz org.eclipse.sirius-8a9d068ae67d693435f677bf033799a169888b68.tar.xz org.eclipse.sirius-8a9d068ae67d693435f677bf033799a169888b68.zip | |
[509735] Improve support for expressions
Expressions are now supported for the title and label expression of the
activity and the section of the workflow.
Bug: 509735
Change-Id: I79ca7d2a834e7309919f47cd142a9be6e43a40ad
Signed-off-by: Stéphane Bégaudeau <stephane.begaudeau@obeo.fr>
4 files changed, 185 insertions, 25 deletions
diff --git a/plugins/org.eclipse.sirius.server.backend/META-INF/MANIFEST.MF b/plugins/org.eclipse.sirius.server.backend/META-INF/MANIFEST.MF index fb11ec8876..1fc45b35b7 100644 --- a/plugins/org.eclipse.sirius.server.backend/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.sirius.server.backend/META-INF/MANIFEST.MF @@ -23,9 +23,11 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="3.8.0", org.eclipse.sirius.ecore.extender;bundle-version="6.0.0", org.eclipse.emf.transaction;bundle-version="1.9.0", org.eclipse.sirius.common;bundle-version="6.0.0", - org.eclipse.jetty.servlets;bundle-version="[9.0.0,10.0.0)" + org.eclipse.jetty.servlets;bundle-version="[9.0.0,10.0.0)", + org.eclipse.sirius.common.interpreter;bundle-version="6.0.0" Bundle-Localization: plugin Export-Package: org.eclipse.sirius.server.backend.internal;x-friends:="org.eclipse.sirius.tests.server.backend", + org.eclipse.sirius.server.backend.internal.expressions;x-internal:=true, org.eclipse.sirius.server.backend.internal.services.activities;x-internal:=true, org.eclipse.sirius.server.backend.internal.services.dashboard;x-internal:=true, org.eclipse.sirius.server.backend.internal.services.pages;x-internal:=true, diff --git a/plugins/org.eclipse.sirius.server.backend/src/org/eclipse/sirius/server/backend/internal/expressions/SiriusBackendInterpreter.java b/plugins/org.eclipse.sirius.server.backend/src/org/eclipse/sirius/server/backend/internal/expressions/SiriusBackendInterpreter.java new file mode 100644 index 0000000000..570d6a659e --- /dev/null +++ b/plugins/org.eclipse.sirius.server.backend/src/org/eclipse/sirius/server/backend/internal/expressions/SiriusBackendInterpreter.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright (c) 2018 Obeo. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.server.backend.internal.expressions; + +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.Set; + +import org.eclipse.emf.common.util.BasicDiagnostic; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.sirius.business.api.session.Session; +import org.eclipse.sirius.common.interpreter.api.EvaluationResult; +import org.eclipse.sirius.common.interpreter.api.IEvaluationResult; +import org.eclipse.sirius.common.interpreter.api.IInterpreter; +import org.eclipse.sirius.common.tools.api.interpreter.EvaluationException; +import org.eclipse.sirius.common.tools.api.interpreter.IInterpreterWithDiagnostic; + +/** + * The interpreter used by the backend. + * + * @author sbegaudeau + */ +public class SiriusBackendInterpreter implements IInterpreter { + /** + * The interpreter of the Sirius session. + */ + private IInterpreterWithDiagnostic interpreter; + + /** + * The constructor. + * + * @param session + * The Sirius session + */ + public SiriusBackendInterpreter(Session session) { + this((IInterpreterWithDiagnostic) session.getInterpreter()); + } + + /** + * The constructor. + * + * @param interpreterWithDiagnostic + * An interpreter + */ + public SiriusBackendInterpreter(IInterpreterWithDiagnostic interpreterWithDiagnostic) { + this.interpreter = Objects.requireNonNull(interpreterWithDiagnostic); + } + + @Override + public IEvaluationResult evaluateExpression(Map<String, Object> variables, String expr) { + IEvaluationResult result = EvaluationResult.noEvaluation(); + Object self = variables.get("self"); //$NON-NLS-1$ + if (self instanceof EObject) { + try { + setupInterpreter(variables); + org.eclipse.sirius.common.tools.api.interpreter.IInterpreterWithDiagnostic.IEvaluationResult evaluationResult = this.interpreter.evaluateExpression((EObject) self, expr); + result = EvaluationResult.of(evaluationResult.getValue(), evaluationResult.getDiagnostic()); + } catch (EvaluationException e) { + result = EvaluationResult.withError(BasicDiagnostic.toDiagnostic(e)); + } finally { + tearDownInterpreter(variables); + } + } + return result; + } + + private void setupInterpreter(Map<String, Object> variables) { + if (this.interpreter instanceof org.eclipse.sirius.common.tools.api.interpreter.IInterpreter) { + org.eclipse.sirius.common.tools.api.interpreter.IInterpreter i = (org.eclipse.sirius.common.tools.api.interpreter.IInterpreter) this.interpreter; + declareLocals(variables, i); + } + } + + private void declareLocals(Map<String, Object> variables, org.eclipse.sirius.common.tools.api.interpreter.IInterpreter i) { + Set<Entry<String, Object>> entries = variables.entrySet(); + for (Entry<String, Object> entry : entries) { + i.setVariable(entry.getKey(), entry.getValue()); + } + } + + private void tearDownInterpreter(Map<String, Object> variables) { + if (this.interpreter instanceof org.eclipse.sirius.common.tools.api.interpreter.IInterpreter) { + unsetLocals(variables, (org.eclipse.sirius.common.tools.api.interpreter.IInterpreter) this.interpreter); + } + } + + private void unsetLocals(Map<String, Object> variables, org.eclipse.sirius.common.tools.api.interpreter.IInterpreter iInterpreter) { + variables.keySet().forEach(iInterpreter::unSetVariable); + } +} diff --git a/plugins/org.eclipse.sirius.server.backend/src/org/eclipse/sirius/server/backend/internal/services/project/SiriusServerPageDto.java b/plugins/org.eclipse.sirius.server.backend/src/org/eclipse/sirius/server/backend/internal/services/project/SiriusServerPageDto.java index 3253d37ccd..859016ac3e 100644 --- a/plugins/org.eclipse.sirius.server.backend/src/org/eclipse/sirius/server/backend/internal/services/project/SiriusServerPageDto.java +++ b/plugins/org.eclipse.sirius.server.backend/src/org/eclipse/sirius/server/backend/internal/services/project/SiriusServerPageDto.java @@ -21,22 +21,17 @@ public class SiriusServerPageDto { private String name; - private String description; - /** * The constructor. - * + * * @param identifier * The identifier * @param name * The name - * @param description - * The description */ - public SiriusServerPageDto(String identifier, String name, String description) { + public SiriusServerPageDto(String identifier, String name) { this.identifier = identifier; this.name = name; - this.description = description; } public String getIdentifier() { @@ -47,7 +42,4 @@ public class SiriusServerPageDto { return this.name; } - public String getDescription() { - return this.description; - } } diff --git a/plugins/org.eclipse.sirius.server.backend/src/org/eclipse/sirius/server/backend/internal/services/project/SiriusServerProjectService.java b/plugins/org.eclipse.sirius.server.backend/src/org/eclipse/sirius/server/backend/internal/services/project/SiriusServerProjectService.java index 705a217f0f..f2b16ca089 100644 --- a/plugins/org.eclipse.sirius.server.backend/src/org/eclipse/sirius/server/backend/internal/services/project/SiriusServerProjectService.java +++ b/plugins/org.eclipse.sirius.server.backend/src/org/eclipse/sirius/server/backend/internal/services/project/SiriusServerProjectService.java @@ -22,8 +22,8 @@ import java.io.InputStreamReader; import java.io.Reader; import java.nio.file.Files; import java.text.DecimalFormat; -import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -40,23 +40,30 @@ import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Status; +import org.eclipse.emf.common.util.BasicEList; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.sirius.business.api.dialect.DialectManager; import org.eclipse.sirius.business.api.modelingproject.ModelingProject; import org.eclipse.sirius.business.api.session.Session; +import org.eclipse.sirius.business.api.session.danalysis.DAnalysisSession; +import org.eclipse.sirius.common.interpreter.api.IEvaluationResult; import org.eclipse.sirius.diagram.description.DiagramDescription; import org.eclipse.sirius.server.backend.internal.ISiriusServerService; import org.eclipse.sirius.server.backend.internal.SiriusServerBackendPlugin; import org.eclipse.sirius.server.backend.internal.SiriusServerPath; import org.eclipse.sirius.server.backend.internal.SiriusServerResponse; +import org.eclipse.sirius.server.backend.internal.expressions.SiriusBackendInterpreter; import org.eclipse.sirius.server.backend.internal.services.workflow.WorkflowHelper; import org.eclipse.sirius.server.backend.internal.utils.SiriusServerUtils; import org.eclipse.sirius.table.metamodel.table.description.TableDescription; import org.eclipse.sirius.tree.description.TreeDescription; +import org.eclipse.sirius.viewpoint.DAnalysis; import org.eclipse.sirius.viewpoint.DRepresentationDescriptor; import org.eclipse.sirius.viewpoint.description.RepresentationDescription; import org.eclipse.sirius.viewpoint.description.Viewpoint; +import org.eclipse.sirius.workflow.ActivityDescription; +import org.eclipse.sirius.workflow.PageDescription; import org.eclipse.sirius.workflow.SectionDescription; /** @@ -68,6 +75,11 @@ import org.eclipse.sirius.workflow.SectionDescription; public class SiriusServerProjectService implements ISiriusServerService { /** + * The name of the self variable. + */ + private static final String SELF = "self"; //$NON-NLS-1$ + + /** * The name of the variable used to capture the name of the project. */ private static final Object PROJECT_NAME = "projectName"; //$NON-NLS-1$ @@ -113,7 +125,7 @@ public class SiriusServerProjectService implements ISiriusServerService { String description = SiriusServerUtils.getProjectDescription(modelingProject.getProject()); List<AbstractSiriusServerRepresentationDto> representations = this.getRepresentations(session); List<SiriusServerSemanticResourceDto> semanticResources = this.getSemanticResources(modelingProject.getProject(), session); - List<SiriusServerPageDto> pages = this.getPages(session); + List<SiriusServerPageDto> pages = this.getPages(modelingProject, session); List<SiriusServerSectionDto> currentPageSections = this.getFirstPageSections(session); return new SiriusServerProjectDto(projectName, description, representations, semanticResources, pages, currentPageSections); } @@ -121,13 +133,22 @@ public class SiriusServerProjectService implements ISiriusServerService { /** * Returns the list of workflow page from the given session. * + * @param modelingProject + * The modeling project * @param session * The session * @return The list of workflow page from the given session */ - private List<SiriusServerPageDto> getPages(Session session) { + private List<SiriusServerPageDto> getPages(ModelingProject modelingProject, Session session) { return WorkflowHelper.on(session).getPageDescriptions().map(page -> { - return new SiriusServerPageDto(page.getName(), page.getLabel(), page.getDescriptionExpression()); + DAnalysis self = ((DAnalysisSession) session).allAnalyses().stream().findFirst().orElse(null); + Map<String, Object> variables = new HashMap<>(); + variables.put(SELF, self); + IEvaluationResult result = new SiriusBackendInterpreter(session).evaluateExpression(variables, page.getTitleExpression()); + + String identifier = page.getName(); + String name = result.asString(); + return new SiriusServerPageDto(identifier, name); }).collect(Collectors.toList()); } @@ -139,16 +160,62 @@ public class SiriusServerProjectService implements ISiriusServerService { * @return The list of the sections of the current page */ private List<SiriusServerSectionDto> getFirstPageSections(Session session) { - List<SiriusServerSectionDto> sections = new ArrayList<>(); - WorkflowHelper.on(session).getPageDescriptions().findFirst().ifPresent(page -> { - for (SectionDescription sectionDesc : page.getSections()) { - List<SiriusServerActivityDto> activities = sectionDesc.getActivities().stream().map(desc -> { - return new SiriusServerActivityDto(desc.getName(), desc.getLabelExpression()); - }).collect(Collectors.toList()); - sections.add(new SiriusServerSectionDto(sectionDesc.getName(), sectionDesc.getTitleExpression(), activities)); - } - }); - return sections; + Optional<PageDescription> optionalPageDescription = WorkflowHelper.on(session).getPageDescriptions().findFirst(); + List<SectionDescription> sectionDescriptions = optionalPageDescription.map(PageDescription::getSections).orElseGet(BasicEList::new); + + // @formatter:off + return sectionDescriptions.stream() + .map(sectionDescription -> this.convertSection(session, sectionDescription)) + .collect(Collectors.toList()); + // @formatter:on + } + + /** + * Converts the given {@link SectionDescription}. + * + * @param session + * The Sirius session + * @param sectionDescription + * The section description + * @return The section DTO + */ + private SiriusServerSectionDto convertSection(Session session, SectionDescription sectionDescription) { + String sectionIdentifier = sectionDescription.getName(); + + DAnalysis self = ((DAnalysisSession) session).allAnalyses().stream().findFirst().orElse(null); + Map<String, Object> variables = new HashMap<>(); + variables.put(SELF, self); + IEvaluationResult result = new SiriusBackendInterpreter(session).evaluateExpression(variables, sectionDescription.getTitleExpression()); + String sectionName = result.asString(); + + // @formatter:off + List<SiriusServerActivityDto> activities = sectionDescription.getActivities().stream() + .map(activityDescription -> this.convertActivity(session, activityDescription)) + .collect(Collectors.toList()); + // @formatter:on + + return new SiriusServerSectionDto(sectionIdentifier, sectionName, activities); + } + + /** + * Converts the given {@link ActivityDescription}. + * + * @param session + * The Sirius session + * @param activityDescription + * The activity description + * @return The activity DTO + */ + private SiriusServerActivityDto convertActivity(Session session, ActivityDescription activityDescription) { + String activityIdentifier = activityDescription.getName(); + + DAnalysis self = ((DAnalysisSession) session).allAnalyses().stream().findFirst().orElse(null); + Map<String, Object> variables = new HashMap<>(); + variables.put(SELF, self); + IEvaluationResult result = new SiriusBackendInterpreter(session).evaluateExpression(variables, activityDescription.getLabelExpression()); + String activityName = result.asString(); + + return new SiriusServerActivityDto(activityIdentifier, activityName); } /** |
