Skip to content
Snippets Groups Projects
Commit e5a26c69 authored by Simon Barner's avatar Simon Barner
Browse files

- Move IdConsistencyChecker and IdUniquenessChecker since it is a generic functionality

- Keep registration of these constraint checkers for FileProject objects in org.fortiss.af3.project
refs 2309
parent 4b85bb4e
Branches
Tags
No related merge requests found
......@@ -15,6 +15,7 @@ Require-Bundle: org.fortiss.tooling.common;visibility:=reexport;bundle-version="
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Export-Package: org.fortiss.tooling.kernel;uses:="org.eclipse.core.runtime,org.osgi.framework",
org.fortiss.tooling.kernel.constraint,
org.fortiss.tooling.kernel.extension,
org.fortiss.tooling.kernel.extension.base,
org.fortiss.tooling.kernel.extension.data,
......
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2015 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.kernel.constraint;
import org.eclipse.emf.ecore.EObject;
import org.fortiss.tooling.kernel.model.IIdLabeled;
import org.fortiss.tooling.kernel.model.INamedElement;
/**
* Class for implementation of constraint violation message.
*
* @author barner
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: FA47E0B446DFB0E62523A73A2DB8A1E6
*/
public class ConstraintMessage {
/** Formats the name of {@code element} for the {@link IdUniquenessConstraintViolation} message. */
private static String formatElementName(EObject element) {
return formatElementName(element, true);
}
/** Helper method to implement {@link #formatElementName(EObject)}. */
private static String formatElementName(EObject element, boolean offspring) {
if(element instanceof INamedElement) {
String elementName = ((INamedElement)element).getName();
if(elementName != null && !elementName.isEmpty()) {
return "\"" + elementName + "\"";
}
}
if(element != null && element.eContainer() != null) {
String elementName = formatElementName(element.eContainer(), false);
return offspring ? "<unnamed offspring of> " + elementName : elementName;
}
return "<unnamed>";
}
/** Creates the violation for an {@link IIdLabeled} element that contains a duplicated ID. */
public static IdUniquenessConstraintViolation<IIdLabeled> createDuplicateIdViolation(
IIdLabeled element, IIdLabeled duplicate) {
return new IdUniquenessConstraintViolation<IIdLabeled>(element, "The ID " +
element.getId() + " of model element " + formatElementName(element) +
" has already been assigned to model element " + formatElementName(duplicate) + ".");
}
/**
* Creates the violation for an {@link IIdLabeled} whose ID attribute is inconsistent with the
* elements persisted id (e.g., {@code xmi:id}).
*/
public static IdConsistencyConstraintViolation<IIdLabeled> createConsistencyIdViolation(
IIdLabeled element, String persistedId) {
return new IdConsistencyConstraintViolation<IIdLabeled>(element, "The ID " +
element.getId() + " of model element " + formatElementName(element) +
" does not match the element's persisted ID " + persistedId + ".");
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2015 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.kernel.constraint;
import static org.fortiss.tooling.kernel.constraint.ConstraintMessage.createConsistencyIdViolation;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.getChildrenWithType;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.fortiss.tooling.kernel.extension.IConstraintChecker;
import org.fortiss.tooling.kernel.extension.base.ConstraintCheckerBase;
import org.fortiss.tooling.kernel.extension.data.ITopLevelElement;
import org.fortiss.tooling.kernel.model.IIdLabeled;
import org.fortiss.tooling.kernel.service.IPersistencyService;
/**
* <p>
* {@link IConstraintChecker} to check whether in the tree below a given root {@link EObject}, the
* IDs provided by {@link IIdLabeled} is identical to the XMI ID stored in the corresponding
* {@link XMLResource}.
* </p>
* <p>
* While this consistency of the IDs should be ensured by of the kernel (see {@code ModelContext}),
* this constraint checker is an additional safety layer.
* </p>
* <p>
* <b>CAVEAT:</b> For performance reasons, be sure to register this constraint checker only for a
* class the exclusively represents the root of your model.
* </p>
*
* @author barner
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: 514D3F212D60B27FB9CDE4B3E2F85560
*/
public class IdConsistencyChecker extends ConstraintCheckerBase<EObject> {
/** {@inheritDoc} */
@Override
public List<IdConsistencyConstraintViolation<IIdLabeled>> apply(EObject rootObject) {
ArrayList<IdConsistencyConstraintViolation<IIdLabeled>> result =
new ArrayList<IdConsistencyConstraintViolation<IIdLabeled>>();
ITopLevelElement topLevelElement =
IPersistencyService.INSTANCE.getTopLevelElementFor(rootObject);
if(topLevelElement != null) {
for(IIdLabeled current : getChildrenWithType(rootObject, IIdLabeled.class)) {
String persistedId = topLevelElement.getId(current);
if(!(new Integer(current.getId()).toString()).equals(persistedId)) {
result.add(createConsistencyIdViolation(current, persistedId));
}
}
}
return result;
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2015 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.kernel.constraint;
import static org.fortiss.tooling.kernel.extension.data.IConstraintViolation.ESeverity.FATAL;
import org.fortiss.tooling.kernel.extension.base.ConstraintViolationBase;
import org.fortiss.tooling.kernel.extension.data.IConstraintViolation;
import org.fortiss.tooling.kernel.model.IIdLabeled;
/**
* {@link IConstraintViolation} for {@link IIdLabeled} elements.
*
* @author barner
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: 399E20804354E3EA3EC65237BE6DE821
*/
public class IdConsistencyConstraintViolation<T extends IIdLabeled> extends
ConstraintViolationBase<T> {
/** Constructor. */
public IdConsistencyConstraintViolation(T modelElement, String explanation) {
super(modelElement, FATAL, explanation);
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2015 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.kernel.constraint;
import static org.fortiss.tooling.kernel.constraint.ConstraintMessage.createDuplicateIdViolation;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.getChildrenWithType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.fortiss.tooling.kernel.extension.IConstraintChecker;
import org.fortiss.tooling.kernel.extension.base.ConstraintCheckerBase;
import org.fortiss.tooling.kernel.model.IIdLabeled;
/**
* <p>
* {@link IConstraintChecker} to check whether all {@link IIdLabeled} elements below a given root
* {@link EObject} actually have unique IDs.
* </p>
* <p>
* While the uniqueness of IDs should be ensured by the assignment strategy of the kernel, and
* {@code ModelContext} eliminates duplicated IDs when loading and saving models, this constraint
* checker is an additional safety layer.
* </p>
* <p>
* This is because in persisted model resources, the encoding of {@link EReference} is based on IDs.
* Hence, model resources with duplicated IDs in referenced model elements are invalid and cannot be
* loaded.
* </p>
* <p>
* <b>CAVEAT:</b> For performance reasons, be sure to register this constraint checker only for a
* class the exclusively represents the root of your model.
* </p>
*
* @author barner
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: 0AB4B0FC7A63A2A975085BFE6C67EBE9
*/
public class IdUniquenessChecker extends ConstraintCheckerBase<EObject> {
/** {@inheritDoc} */
@Override
public List<IdUniquenessConstraintViolation<IIdLabeled>> apply(EObject rootObject) {
ArrayList<IdUniquenessConstraintViolation<IIdLabeled>> result =
new ArrayList<IdUniquenessConstraintViolation<IIdLabeled>>();
Map<Integer, IIdLabeled> idToElementMap = new HashMap<Integer, IIdLabeled>();
for(IIdLabeled current : getChildrenWithType(rootObject, IIdLabeled.class)) {
// In case there are duplicates, the first occurrence is considered to be legitimate
// and is hence kept in the map
int id = current.getId();
if(idToElementMap.containsKey(id)) {
result.add(createDuplicateIdViolation(current, idToElementMap.get(id)));
} else {
idToElementMap.put(id, current);
}
}
return result;
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2015 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.kernel.constraint;
import static org.fortiss.tooling.kernel.extension.data.IConstraintViolation.ESeverity.FATAL;
import org.fortiss.tooling.kernel.extension.base.ConstraintViolationBase;
import org.fortiss.tooling.kernel.extension.data.IConstraintViolation;
import org.fortiss.tooling.kernel.model.IIdLabeled;
/**
* {@link IConstraintViolation} for {@link IIdLabeled} elements.
*
* @author barner
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: 94BDDCCA8D8AC743AE40F428DF0DEF3C
*/
public class IdUniquenessConstraintViolation<T extends IIdLabeled> extends
ConstraintViolationBase<T> {
/** Constructor. */
public IdUniquenessConstraintViolation(T modelElement, String explanation) {
super(modelElement, FATAL, explanation);
}
}
<!--
$Id$
@version $Rev$
@ConQAT.Rating YELLOW Hash: 1469932365423F2E8747E876625E2D73
-->
<body>
Package for constraint checkers of the kernel.
</body>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment