blob: 1ba7baa0d807331221b0feae3605e568578197c0 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2015 CNES and others.
* All rights reserved. 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:
* JF Rolland (Atos) - initial API and implementation
*******************************************************************************/
package org.eclipse.ease.modules.modeling.ui.matchers;
import java.util.Collection;
import java.util.Collections;
import org.eclipse.ease.modules.modeling.ui.Messages;
import org.eclipse.ease.modules.modeling.ui.exceptions.MatcherException;
import org.eclipse.ease.modules.modeling.ui.utils.ModelExtentMap;
import org.eclipse.ease.modules.modeling.ui.utils.SelectionUtils;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.ocl.pivot.ExpressionInOCL;
import org.eclipse.ocl.pivot.utilities.OCL;
import org.eclipse.ocl.pivot.utilities.OCLHelper;
import org.eclipse.ocl.pivot.utilities.ParserException;
import org.eclipse.ocl.pivot.values.SetValue;
public class OCLMatcher implements IMatcher {
@Override
public Collection<EObject> getElements(String oclString, IEditingDomainProvider currentEditor) throws MatcherException {
final EObject root = SelectionUtils.getSelection(currentEditor);
final OCL ocl = OCL.newInstance(EPackage.Registry.INSTANCE);
ocl.setModelManager(new ModelExtentMap(ocl.getMetamodelManager(), root));
try {
final OCLHelper helper = ocl.createOCLHelper(root.eClass());
final ExpressionInOCL query = helper.createQuery(oclString);
final Object queryResult = ocl.evaluate(root, query);
if (queryResult instanceof SetValue) {
return (Collection<EObject>) ((SetValue) queryResult).asCollection();
}
} catch (final ParserException e) {
e.printStackTrace();
throw new MatcherException(Messages.OCLMatcher_CONSTRAINT_INVALID);
}
return Collections.emptyList();
}
@Override
public String getText() {
return Messages.OCLMatcher_COMBO_TEXT_OCL;
}
@Override
public String getHelp() {
return Messages.OCLMatcher_HELP_OCL;
}
}