From 6086120c7fad3c27de8d48488ba8174d65b7c18e Mon Sep 17 00:00:00 2001 From: Simon Barner <barner@fortiss.org> Date: Fri, 29 Aug 2014 12:26:10 +0000 Subject: [PATCH] Introduce "derived" annotation, i.e. based on calculations instead of explicit annotations / model contents can be implemented - DerivedAnnotationBase meta-model element, with getValue() EOperation, and a extensive documentation. - Corresponding DerivedAnnotationValueProviderBase, implementing a read-only, text-view of the calculation result - To implement the above, move handling of annotation EClass & factory to ValueProviderBase refs 1841 --- .../DerivedAnnotationValueProviderBase.java | 66 +++++++++++++++++++ ...leEStructuralFeatureValueProviderBase.java | 41 +----------- .../valueprovider/ValueProviderBase.java | 63 ++++++++++++++++++ .../trunk/model/base.ecore | 11 ++++ .../trunk/model/base.genmodel | 3 + 5 files changed, 146 insertions(+), 38 deletions(-) create mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/valueprovider/DerivedAnnotationValueProviderBase.java diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/valueprovider/DerivedAnnotationValueProviderBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/valueprovider/DerivedAnnotationValueProviderBase.java new file mode 100644 index 000000000..ef9ae195a --- /dev/null +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/valueprovider/DerivedAnnotationValueProviderBase.java @@ -0,0 +1,66 @@ +/*--------------------------------------------------------------------------+ +$Id$ +| | +| Copyright 2014 ForTISS GmbH | +| | +| Licensed under the Apache License, Version 2.0 (the "License"); | +| you may not use this file except in compliance with the License. | +| You may obtain a copy of the License at | +| | +| http://www.apache.org/licenses/LICENSE-2.0 | +| | +| Unless required by applicable law or agreed to in writing, software | +| distributed under the License is distributed on an "AS IS" BASIS, | +| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | +| See the License for the specific language governing permissions and | +| limitations under the License. | ++--------------------------------------------------------------------------*/ +package org.fortiss.tooling.base.ui.annotation.valueprovider; + +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EFactory; +import org.eclipse.jface.viewers.ColumnViewer; +import org.eclipse.jface.viewers.EditingSupport; +import org.fortiss.tooling.base.model.base.DerivedAnnotationBase; +import org.fortiss.tooling.base.model.element.IAnnotatedSpecification; + +/** + * Base class for {@link IAnnotationValueProvider} for derived annotations (i.e., based on + * {@link DerivedAnnotationBase}). + * + * @author barner + * @author $Author$ + * @version $Rev$ + * @ConQAT.Rating RED Hash: + */ +public abstract class DerivedAnnotationValueProviderBase<T extends DerivedAnnotationBase> extends + ValueProviderBase<T> { + + /** + * Constructs a {@link IAnnotationValueProvider} for derived annotations (i.e., based on + * {@link DerivedAnnotationBase}). + */ + public DerivedAnnotationValueProviderBase(EClass annotatedSpecificationEClass, + EFactory annotationFactory) { + super(annotatedSpecificationEClass, annotationFactory); + } + + /** {@inheritDoc} */ + @SuppressWarnings("unchecked") + @Override + public <U> U getAnnotationValue(T specification) { + // Delegate retrieval of annotation value to {@link EOperation} to be provided for the + // concrete @link DerivedAnnotationBase}. + return (U)specification.getValue(); + } + + /** {@inheritDoc} */ + @Override + public EditingSupport createEditingSupport(ColumnViewer viewer, + Class<? extends IAnnotatedSpecification> clazz, String instanceKey) { + + // Derived annotations are results of calculations can thus cannot be edited. + return null; + } + +} diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/valueprovider/SingleEStructuralFeatureValueProviderBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/valueprovider/SingleEStructuralFeatureValueProviderBase.java index 1cbd3bb58..b9168f139 100644 --- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/valueprovider/SingleEStructuralFeatureValueProviderBase.java +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/valueprovider/SingleEStructuralFeatureValueProviderBase.java @@ -25,7 +25,6 @@ import org.eclipse.emf.ecore.EReference; import org.eclipse.emf.ecore.EStructuralFeature; import org.fortiss.tooling.base.model.element.IAnnotatedSpecification; import org.fortiss.tooling.base.model.element.IModelElement; -import org.fortiss.tooling.base.model.element.IModelElementSpecification; /** * Base class for {@link IAnnotationValueProvider}s that manages a single {@link EStructuralFeature} @@ -39,18 +38,6 @@ import org.fortiss.tooling.base.model.element.IModelElementSpecification; public abstract class SingleEStructuralFeatureValueProviderBase<T extends IAnnotatedSpecification> extends ValueProviderBase<T> { - /** - * {@link EClass} of {@link IAnnotatedSpecification} that contains the respective structural - * feature and that is managed by this {@link IAnnotationValueProvider}. - */ - final protected EClass annotatedSpecificationEClass; - - /** - * {@link EFactory} of ECore model that contains {@link IAnnotatedSpecification} identified by - * {@code annotatedSpecificationEClass} (used to create new instances of the annotation). - */ - final protected EFactory annotationFactory; - /** * The structural feature of the {@link IAnnotatedSpecification} that is managed by this * {@link IAnnotationValueProvider}. @@ -88,8 +75,8 @@ public abstract class SingleEStructuralFeatureValueProviderBase<T extends IAnnot EFactory annotationFactory, EStructuralFeature structuralFeature, EFactory structuralFeatureFactory, final String structuralFeatureDefaultValue) { - this.annotatedSpecificationEClass = annotatedSpecificationEClass; - this.annotationFactory = annotationFactory; + super(annotatedSpecificationEClass, annotationFactory); + this.structuralFeature = structuralFeature; this.structuralFeatureFactory = structuralFeatureFactory; this.structuralFeatureDefaultValue = structuralFeatureDefaultValue; @@ -186,35 +173,13 @@ public abstract class SingleEStructuralFeatureValueProviderBase<T extends IAnnot } /** {@inheritDoc} */ - @SuppressWarnings("unchecked") @Override - public T getAnnotatedSpecificationForModelElement(IModelElement element) throws Exception { - // Return existing annotation - for(IModelElementSpecification specification : element.getSpecifications()) { - if(annotatedSpecificationEClass.equals(specification.eClass())) { - return (T)specification; - } - } - - // Create annotation - T specification = (T)annotationFactory.create(annotatedSpecificationEClass); - + protected void decorateAnnotationSpecification(T specification) throws Exception { // Create and set structural feature implementing the annotation Object structuralFeatureVal = createStructuralFeatureInstance(); if(structuralFeatureVal != null) { specification.eSet(structuralFeature, structuralFeatureVal); } - - // Hook specification to model element - element.addSpecification(specification); - - return specification; } - /** {@inheritDoc} */ - @SuppressWarnings("unchecked") - @Override - public Class<T> getAnnotationClazz() { - return (Class<T>)annotatedSpecificationEClass.getClass(); - } } diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/valueprovider/ValueProviderBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/valueprovider/ValueProviderBase.java index 82bcc9b28..b964a1fd0 100644 --- a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/valueprovider/ValueProviderBase.java +++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/annotation/valueprovider/ValueProviderBase.java @@ -19,10 +19,14 @@ package org.fortiss.tooling.base.ui.annotation.valueprovider; import java.util.List; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EFactory; import org.eclipse.emf.ecore.EObject; import org.eclipse.jface.viewers.ColumnViewer; import org.eclipse.jface.viewers.EditingSupport; import org.fortiss.tooling.base.model.element.IAnnotatedSpecification; +import org.fortiss.tooling.base.model.element.IModelElement; +import org.fortiss.tooling.base.model.element.IModelElementSpecification; import org.fortiss.tooling.base.ui.annotation.editingsupport.TextEditingSupport; /** @@ -36,6 +40,26 @@ import org.fortiss.tooling.base.ui.annotation.editingsupport.TextEditingSupport; public abstract class ValueProviderBase<T extends IAnnotatedSpecification> implements IAnnotationValueProvider<T> { + /** + * {@link EClass} of {@link IAnnotatedSpecification} that contains the respective structural + * feature and that is managed by this {@link IAnnotationValueProvider}. + */ + protected final EClass annotatedSpecificationEClass; + /** + * {@link EFactory} of ECore model that contains {@link IAnnotatedSpecification} identified by + * {@code annotatedSpecificationEClass} (used to create new instances of the annotation). + */ + protected final EFactory annotationFactory; + + /** + * Constructs a new {@link IAnnotationValueProvider} for an annotation specified in a given + * {@link EClass}, and constructed using a given {@link EFactory}. + */ + public ValueProviderBase(EClass annotatedSpecificationEClass, EFactory annotationFactory) { + this.annotatedSpecificationEClass = annotatedSpecificationEClass; + this.annotationFactory = annotationFactory; + } + /** {@inheritDoc} */ @Override public boolean setAnnotationName(String name, T specification) { @@ -110,4 +134,43 @@ public abstract class ValueProviderBase<T extends IAnnotatedSpecification> imple return null; } + /** + * Add decorations to an annotation when its instance is newly created by + * {@link #getAnnotatedSpecificationForModelElement(IModelElement)}. + * + * @param specification + * annotation to be decorated + */ + protected void decorateAnnotationSpecification(T specification) throws Exception { + // No decoration + } + + /** {@inheritDoc} */ + @SuppressWarnings("unchecked") + @Override + public T getAnnotatedSpecificationForModelElement(IModelElement element) throws Exception { + // Return existing annotation + for(IModelElementSpecification specification : element.getSpecifications()) { + if(annotatedSpecificationEClass.equals(specification.eClass())) { + return (T)specification; + } + } + + // Create annotation + T specification = (T)annotationFactory.create(annotatedSpecificationEClass); + decorateAnnotationSpecification(specification); + + // Hook specification to model element + element.addSpecification(specification); + + return specification; + } + + /** {@inheritDoc} */ + @SuppressWarnings("unchecked") + @Override + public Class<T> getAnnotationClazz() { + return (Class<T>)annotatedSpecificationEClass.getClass(); + } + } diff --git a/org.fortiss.tooling.base/trunk/model/base.ecore b/org.fortiss.tooling.base/trunk/model/base.ecore index 7661acf9b..4339ba1f6 100644 --- a/org.fortiss.tooling.base/trunk/model/base.ecore +++ b/org.fortiss.tooling.base/trunk/model/base.ecore @@ -294,5 +294,16 @@ </eAnnotations> </eOperations> </eClassifiers> + <eClassifiers xsi:type="ecore:EClass" name="DerivedAnnotationBase" abstract="true" + eSuperTypes="#//element/IAnnotatedSpecification #//element/IHiddenSpecification"> + <eAnnotations source="http://www.eclipse.org/emf/2002/GenModel"> + <details key="documentation" value="Base class for {@link IAnnotationSpecification}s that are derived from the state of other annotations and/or model elements.

<ul>
<li>Concrete specifications must provide a specialized getValue() {@link EOperation}s that perform the required calculation. Example: Calculation of {@link Component} WCET for current deployment (if available) based on {@link Component} instruction count and performance (e.g., MIPS) of {@link ExecutionUnit}, some warning indicator otherwise. </li>
<li>Concrete specifications may provide additional {@link EOperation} that provide an advanced query interface to the annoation. Example: WCET for an explicitly specified {@link ExecutionUnit}. This can be used e.g. in a DSE to explore different deployments.</li>
<li>The corresponding {@link IAnnotationValueProvider} should be based on {@link DerivedAnnotationValueProviderBase}.
</ul>"/> + </eAnnotations> + <eOperations name="getValue" lowerBound="1" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EJavaObject"> + <eAnnotations source="http://www.eclipse.org/emf/2002/GenModel"> + <details key="body" value="return null;"/> + </eAnnotations> + </eOperations> + </eClassifiers> </eSubpackages> </ecore:EPackage> diff --git a/org.fortiss.tooling.base/trunk/model/base.genmodel b/org.fortiss.tooling.base/trunk/model/base.genmodel index c44c3b725..ea8a322df 100644 --- a/org.fortiss.tooling.base/trunk/model/base.genmodel +++ b/org.fortiss.tooling.base/trunk/model/base.genmodel @@ -97,6 +97,9 @@ <genOperations ecoreOperation="base.ecore#//base/LibraryElementBase/getURI"/> <genOperations ecoreOperation="base.ecore#//base/LibraryElementBase/getName"/> </genClasses> + <genClasses image="false" ecoreClass="base.ecore#//base/DerivedAnnotationBase"> + <genOperations ecoreOperation="base.ecore#//base/DerivedAnnotationBase/getValue"/> + </genClasses> </nestedGenPackages> </genPackages> </genmodel:GenModel> -- GitLab