Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[555096] Pluggable Validation Traversal #186

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.polarsys.capella.core.transition.common.constants.ISchemaConstants;
import org.polarsys.capella.core.transition.common.handlers.IHandler;
import org.polarsys.kitalpha.transposer.rules.handler.rules.api.IContext;
Expand Down Expand Up @@ -70,23 +72,18 @@ protected static boolean isValidExtension(IConfigurationElement element, String
return false;
}

public static Collection<IHandler> collectFromExtensions(IContext context, String extension_id, String childName,
String expectedPurpose, String expectedMapping) {
@SuppressWarnings("unchecked")
public static <T> Collection<T> collectFromExtensions(IContext context, String extension_id, String childName, String expectedPurpose, String expectedMapping){
IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(extension_id);
Collection<IHandler> result = new LinkedList<IHandler>();

Collection<T> result = new LinkedList<>();
for (IConfigurationElement element : point.getConfigurationElements()) {
if (isValidExtension(element, expectedPurpose, expectedMapping)) {
for (IConfigurationElement child : element.getChildren()) {

if (childName.equals(child.getName())) {
try {
Object extension = child.createExecutableExtension(ISchemaConstants.CLASS);
if ((extension != null) && (extension instanceof IHandler)) {
result.add((IHandler) extension);
}
} catch (CoreException exception) {
// Catch exception silently, we just can't load an extension
result.add((T) child.createExecutableExtension(ISchemaConstants.CLASS));
} catch (CoreException e) {
Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
package org.polarsys.capella.core.transition.common.handlers;

import org.eclipse.core.runtime.IStatus;

import org.eclipse.core.runtime.Status;
import org.polarsys.kitalpha.transposer.rules.handler.rules.api.IContext;

/**
*
*/
public interface IHandler {

IStatus init(IContext context);
default IStatus init(IContext context) {
return new Status(IStatus.OK, getClass().getCanonicalName(), "init ok"); //$NON-NLS-1$
}

IStatus dispose(IContext context);
default IStatus dispose(IContext context) {
return Status.OK_STATUS;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*******************************************************************************
* Copyright (c) 2020 THALES GLOBAL SERVICES.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Thales - initial API and implementation
*******************************************************************************/
package org.polarsys.capella.core.transition.common.handlers.scope;

import java.util.Collection;
import java.util.Collections;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.polarsys.kitalpha.transposer.rules.handler.rules.api.IContext;

public class EReferenceScopeRetriever implements IScopeRetriever {
private final EReference ref;

public EReferenceScopeRetriever(EReference ref) {
this.ref = ref;
}

@SuppressWarnings("unchecked")
@Override
public Collection<? extends EObject> retrieveRelatedElements(EObject element, IContext context) {
EClass refClass = ref.getEContainingClass();
if (refClass.isInstance(element)) {
if (element.eIsSet(ref)) {
Object target = element.eGet(ref);
if (ref.isMany()) {
return (Collection<? extends EObject>) element.eGet(ref);
} else if (target != null) {
return (Collection<? extends EObject>) Collections.singleton(target);
}
}
}
return Collections.emptyList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
package org.polarsys.capella.core.transition.common.handlers.scope;

import java.util.Collection;
import java.util.Collections;

import org.eclipse.emf.ecore.EObject;

import org.polarsys.capella.core.transition.common.handlers.IHandler;
import org.polarsys.kitalpha.transposer.rules.handler.rules.api.IContext;

Expand All @@ -29,8 +29,12 @@ public interface IScopeRetriever extends IHandler {
* @param context
* @return
*/
Collection<? extends EObject> retrieveRelatedElements(EObject element, IContext context);
default Collection<? extends EObject> retrieveRelatedElements(EObject element, IContext context){
return Collections.emptyList();
}

Collection<? extends EObject> retrieveSharedElements(IContext context);
default Collection<? extends EObject> retrieveSharedElements(IContext context){
return Collections.emptyList();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright (c) 2020 THALES GLOBAL SERVICES.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Thales - initial API and implementation
*******************************************************************************/
package org.polarsys.capella.core.transition.common.handlers.scope;

import java.util.Collection;
import java.util.Collections;

import org.eclipse.emf.ecore.EObject;
import org.polarsys.kitalpha.transposer.rules.handler.rules.api.IContext;

public abstract class TypedScopeRetriever<T> implements IScopeRetriever {

private final Class<T> clazz;

public TypedScopeRetriever(Class<T> clazz) {
this.clazz = clazz;
}

@SuppressWarnings("unchecked")
@Override
public final Collection<? extends EObject> retrieveRelatedElements(EObject element, IContext context) {
if (clazz.isInstance(element)) {
return doRetrieveRelatedElements((T) element, context);
}
return Collections.emptyList();
}

protected Collection<? extends EObject> doRetrieveRelatedElements(T element, IContext context) {
return Collections.emptyList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
*******************************************************************************/
package org.polarsys.capella.core.data.fa.ui.wizards.dialogs;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.validation.service.IConstraintDescriptor;
import org.eclipse.emf.validation.service.IConstraintFilter;
import org.eclipse.emf.validation.service.IValidator;
import org.eclipse.jface.viewers.IBaseLabelProvider;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.IFontProvider;
Expand All @@ -30,41 +30,48 @@
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Widget;
import org.polarsys.capella.common.ui.providers.MDEAdapterFactoryLabelProvider;
import org.polarsys.capella.core.validation.CapellaValidationActivator;
import org.polarsys.capella.core.validation.utils.ValidationHelper;

public class EIAllocationLabelProvider extends MDEAdapterFactoryLabelProvider implements IBaseLabelProvider, IColorProvider, IFontProvider {
protected final boolean isSourceViewer;
private final EIAllocationTreeViewer treeViewer;
private LinkManager linkManager;
private Font startedLinkElementFont;

private IConstraintFilter _filter = new IConstraintFilter() {
@Override
public boolean accept(IConstraintDescriptor constraint, EObject target) {
return (isSourceViewer ? _srcDesc.contains(constraint) : _tgtDesc.contains(constraint));
}
};

private static final String prefix = "org.polarsys.capella.core.data.fa.validation."; //$NON-NLS-1$

protected List<IConstraintDescriptor> _srcDesc = getConstraintDescriptors(Arrays.asList(
new String[]{prefix+"TC_DF_10",prefix+"TC_DF_11",prefix+"TC_DF_12",prefix+"TC_DF_13",prefix+"TC_DF_14"})); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$;

protected List<IConstraintDescriptor> _tgtDesc = getConstraintDescriptors(Arrays.asList(
new String[]{prefix+"TC_DF_11",prefix+"TC_DF_12",prefix+"TC_DF_13",prefix+"TC_DF_14"})); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //;
private final IValidator<EObject> validator;
public final static String VALIDATION_KEY = "Validation"; //$NON-NLS-1$

@SuppressWarnings("nls")
private static List<String> _srcDesc = Arrays.asList(
"org.polarsys.capella.core.data.fa.validation.TC_DF_10",
"org.polarsys.capella.core.data.fa.validation.TC_DF_11",
"org.polarsys.capella.core.data.fa.validation.TC_DF_12",
"org.polarsys.capella.core.data.fa.validation.TC_DF_13",
"org.polarsys.capella.core.data.fa.validation.TC_DF_14");

@SuppressWarnings("nls")
private static List<String> _tgtDesc = Arrays.asList(
"org.polarsys.capella.core.data.fa.validation.TC_DF_11",
"org.polarsys.capella.core.data.fa.validation.TC_DF_12",
"org.polarsys.capella.core.data.fa.validation.TC_DF_13",
"org.polarsys.capella.core.data.fa.validation.TC_DF_14");

/**
* @param linkManager
* @param treeViewer
* @param isSourceViewer
*/
public EIAllocationLabelProvider(LinkManager linkManager, EIAllocationTreeViewer treeViewer, boolean isSourceViewer) {
super();
this.isSourceViewer = isSourceViewer;
public EIAllocationLabelProvider(LinkManager linkManager, EIAllocationTreeViewer treeViewer, boolean isSourceViewer) {
this.isSourceViewer = isSourceViewer;
this.treeViewer = treeViewer;
this.linkManager = linkManager;
}
validator = ValidationHelper.newDefaultCapellaBatchValidator();
validator.addConstraintFilter(new IConstraintFilter() {
@Override
public boolean accept(IConstraintDescriptor constraint, EObject target) {
return (isSourceViewer ? _srcDesc.contains(constraint.getId()) : _tgtDesc.contains(constraint.getId()));
}
});
}

/**
* @see org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider#dispose()
Expand Down Expand Up @@ -102,13 +109,8 @@ public Font getFont(Object element) {
return super.getFont(element);
}

public final static String VALIDATION_KEY = "Validation"; //$NON-NLS-1$

private boolean isValid(EObject element) {
CapellaValidationActivator.getDefault().getCapellaValidatorAdapter().getValidator().addConstraintFilter(_filter);
IStatus status = CapellaValidationActivator.getDefault().getCapellaValidatorAdapter().getValidator().validate(element);
CapellaValidationActivator.getDefault().getCapellaValidatorAdapter().getValidator().removeConstraintFilter(_filter);

IStatus status = validator.validate(element);
Widget w = treeViewer.findItem(element);
if (w instanceof TreeItem) {
if (!status.isOK()) {
Expand All @@ -124,31 +126,13 @@ private boolean isValid(EObject element) {
* {@inheritDoc}
*/
@Override
public Color getForeground(Object element) {
public Color getForeground(Object element) {
if (!isValid((EObject) element)) {
return Display.getCurrent().getSystemColor(SWT.COLOR_RED);
}
return super.getForeground(element);
}

/**
* Get all constraints contributed via the EMF Validation framework for Capella purposes
* e.g. rules stored into the Capella constraint category
* @return
*/
public static List<IConstraintDescriptor> getConstraintDescriptors(List<String> ids) {
List<IConstraintDescriptor> result = new ArrayList<IConstraintDescriptor>();

ValidationHelper.ensureEMFValidationActivation();

for (IConstraintDescriptor icd: ValidationHelper.getAllCapellaConstraintDescriptors()) {
String id = icd.getId();
if (ids.contains(id)) {
result.add(icd);
}
}
return result;
return super.getForeground(element);
}


/**
* Create started link element font.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<schema targetNamespace="org.polarsys.capella.core.model.handler" xmlns="http://www.w3.org/2001/XMLSchema">
<annotation>
<appInfo>
<meta.schema plugin="org.polarsys.capella.core.model.handler" id="diagnosticianProviders" name="diagnosticianProviders"/>
<meta.schema plugin="org.polarsys.capella.core.model.handler" id="diagnosticianProviders" name="Diagnostician Providers"/>
</appInfo>
<documentation>
Allows plugins to choose a diagnostician for model validation
Allows plugins to contribute EMF Diagnosticians for model validation
</documentation>
</annotation>

Expand All @@ -17,7 +17,7 @@
</appInfo>
</annotation>
<complexType>
<sequence>
<sequence minOccurs="1" maxOccurs="unbounded">
<element ref="provider"/>
</sequence>
<attribute name="point" type="string" use="required">
Expand Down Expand Up @@ -59,6 +59,27 @@
</appInfo>
</annotation>
</attribute>
<attribute name="id" type="string" use="required">
<annotation>
<documentation>
The ID for this provider
</documentation>
</annotation>
</attribute>
<attribute name="description" type="string">
<annotation>
<documentation>
Description of this provider
</documentation>
</annotation>
</attribute>
<attribute name="name" type="string" use="required">
<annotation>
<documentation>

</documentation>
</annotation>
</attribute>
</complexType>
</element>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@

public abstract class AbstractDiagnosticianProvider {

public abstract Diagnostician getDiagnostician(AdapterFactory adapterFactory_p, IProgressMonitor progressMonitor_p);
public abstract Diagnostician getDiagnostician(AdapterFactory adapterFactory_p, IProgressMonitor progressMonitor_p);

}
Loading