| /** |
| * Copyright (c)2020 CEA LIST, Committer Name, and others. |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * CEA LIST - Initial API and implementation |
| * Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr |
| * Gabriel Pedroza (CEA LIST) gabriel.pedroza@cea.fr |
| * |
| */ |
| package org.eclipse.papyrus.pdp4eng.req.profile.constraints; |
| |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.Iterator; |
| |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.emf.ecore.resource.ResourceSet; |
| import org.eclipse.uml2.uml.DirectedRelationship; |
| import org.eclipse.uml2.uml.Element; |
| import org.eclipse.uml2.uml.resource.UMLResource; |
| |
| public class TraceabilityIndexer { |
| private HashMap<Element,ArrayList<DirectedRelationship> > upwardTraceability= new HashMap<Element, ArrayList<DirectedRelationship>>(); |
| private HashMap<Element,ArrayList<DirectedRelationship> > downwardTraceability= new HashMap<Element, ArrayList<DirectedRelationship>>(); |
| private static TraceabilityIndexer instance=null; |
| public static TraceabilityIndexer getInstance() { |
| if( instance==null) { |
| instance= new TraceabilityIndexer(); |
| } |
| return instance; |
| } |
| |
| |
| |
| public void loadTraceability(Element context) { |
| ResourceSet resourceSet=context.eResource().getResourceSet(); |
| for(int i=0; i< resourceSet.getResources().size();i++) { |
| Resource resource = (Resource) resourceSet.getResources().get(i); |
| if( resource instanceof UMLResource) { |
| for (Iterator<EObject> iteratorObject = resource.getAllContents(); iteratorObject.hasNext();) { |
| EObject ownedElement = (EObject) iteratorObject.next(); |
| if(ownedElement instanceof DirectedRelationship) { |
| DirectedRelationship directedRelationship= (DirectedRelationship)ownedElement; |
| for (Iterator<Element> iterator = directedRelationship.getSources().iterator(); iterator.hasNext();) { |
| Element currentSource = (Element) iterator.next(); |
| if( upwardTraceability.get(currentSource)!=null) { |
| upwardTraceability.get(currentSource).add(directedRelationship); |
| } |
| else { |
| ArrayList<DirectedRelationship> aCollection= new ArrayList<DirectedRelationship>(); |
| aCollection.add(directedRelationship); |
| upwardTraceability.put(currentSource, aCollection); |
| } |
| } |
| for (Iterator<Element> iterator = directedRelationship.getTargets().iterator(); iterator.hasNext();) { |
| Element currentTarget = (Element) iterator.next(); |
| if( downwardTraceability.get(currentTarget)!=null) { |
| downwardTraceability.get(currentTarget).add(directedRelationship); |
| } |
| else { |
| ArrayList<DirectedRelationship> aCollection= new ArrayList<DirectedRelationship>(); |
| aCollection.add(directedRelationship); |
| downwardTraceability.put(currentTarget, aCollection); |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| public ArrayList<DirectedRelationship> getUpwardTraceabiltiy(Element context){ |
| return upwardTraceability.get(context); |
| } |
| |
| public ArrayList<DirectedRelationship> getDownwardTraceabiltiy(Element context){ |
| return downwardTraceability.get(context); |
| } |
| } |