| 1 | package de.uka.ipd.sdq.reliability.solver.test; |
| 2 | |
| 3 | import org.eclipse.emf.query.conditions.Condition; |
| 4 | import org.eclipse.emf.query.conditions.eobjects.EObjectCondition; |
| 5 | import org.eclipse.emf.query.conditions.eobjects.structuralfeatures.EObjectAttributeValueCondition; |
| 6 | import org.eclipse.emf.query.conditions.strings.StringValue; |
| 7 | import org.eclipse.emf.query.statements.FROM; |
| 8 | import org.eclipse.emf.query.statements.IQueryResult; |
| 9 | import org.eclipse.emf.query.statements.SELECT; |
| 10 | import org.eclipse.emf.query.statements.WHERE; |
| 11 | |
| 12 | import de.uka.ipd.sdq.identifier.Identifier; |
| 13 | import de.uka.ipd.sdq.identifier.IdentifierPackage; |
| 14 | |
| 15 | /** |
| 16 | * This class provides additional functionality for the Markov Test Cases. To |
| 17 | * use this functionality, the Test Case has to be run as a JUnit Plug-in Test |
| 18 | * (not only a JUnit Test). |
| 19 | * |
| 20 | * @author brosch |
| 21 | * |
| 22 | */ |
| 23 | public class MarkovTestHelper { |
| 24 | |
| 25 | /** |
| 26 | * Searches a tree of PCM model elements for an element with a certain GUID. |
| 27 | * Returns a reference to this element. |
| 28 | * |
| 29 | * @param root |
| 30 | * the root element of the tree to search |
| 31 | * @param guid |
| 32 | * the guid of the element to find |
| 33 | * @return the reference to the element |
| 34 | */ |
| 35 | public Identifier getModelElement(final Identifier root, final String guid) { |
| 36 | |
| 37 | // Search for a string which equals the given GUID: |
| 38 | Condition isGuidValue = new StringValue(guid); |
| 39 | |
| 40 | // Search for an identifier whose ID equals the given GUID: |
| 41 | EObjectCondition hasGuidValue = new EObjectAttributeValueCondition( |
| 42 | IdentifierPackage.Literals.IDENTIFIER__ID, isGuidValue); |
| 43 | |
| 44 | // Perform an EMF Model Query: |
| 45 | IQueryResult queryResult = new SELECT(new FROM(root), new WHERE( |
| 46 | hasGuidValue)).execute(); |
| 47 | |
| 48 | // Return the first element that matches the condition: |
| 49 | for (Object next : queryResult) { |
| 50 | return (Identifier) next; |
| 51 | } |
| 52 | |
| 53 | // Another possibility would be to use an OCL Query: |
| 54 | // OCL ocl = org.eclipse.ocl.ecore.OCL.newInstance(); |
| 55 | // EObjectCondition guidCondition = |
| 56 | // new BooleanOCLCondition<EClassifier, EClass, EObject>( |
| 57 | // ocl.getEnvironment(), "self.id = '" + guid + "'", |
| 58 | // IdentifierPackage.Literals.IDENTIFIER); |
| 59 | // IQueryResult queryResult = new SELECT(new FROM(root), new WHERE( |
| 60 | // guidCondition)).execute(); |
| 61 | |
| 62 | // Nothing found: |
| 63 | return null; |
| 64 | } |
| 65 | } |