1 /*******************************************************************************
2 * Copyright (c) 2013 Pascal Hirmer.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * and the Apache License 2.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
10 * Pascal Hirmer - initial API and implementation
11 *******************************************************************************/
13 package org.eclipse.winery.topologymodeler.addons.topologycompleter.helper;
15 import java.util.ArrayList;
16 import java.util.List;
18 import org.eclipse.winery.model.tosca.TCapability;
19 import org.eclipse.winery.model.tosca.TNodeTemplate;
20 import org.eclipse.winery.model.tosca.TRelationshipType;
21 import org.eclipse.winery.model.tosca.TRequirement;
22 import org.eclipse.winery.topologymodeler.addons.topologycompleter.analyzer.TOSCAAnalyzer;
25 * This class searches a {@link TRelationshipType} which is able to connect two given {@link TNodeTemplate}s.
28 public class NodeTemplateConnector {
31 * Searches a compatible {@link TRelationshipType} to connect two {@link TNodeTemplate}s.
34 * the source {@link TNodeTemplate}
36 * the target {@link TNodeTemplate}
37 * @param toscaAnalyzer
38 * the {@link TOSCAAnalyzer} object to access the data model
40 * the {@link TRequirement} of the source {@link TNodeTemplate}
42 * @return a list of suitable {@link TRelationshipType}s
44 public static List<TRelationshipType> findRelationshipType(TNodeTemplate source, TNodeTemplate target, TOSCAAnalyzer toscaAnalyzer, TRequirement requirement) {
46 List<TRelationshipType> suitableRelationshipTypes = new ArrayList<TRelationshipType>();
47 List<TRelationshipType> allRelationshipTypes = toscaAnalyzer.getRelationshipTypes();
49 // in case the connection to a placeholder is searched, no requirement exists
50 if (requirement != null) {
52 List<TCapability> capabilities = target.getCapabilities().getCapability();
54 // check if a RelationshipType can connect a requirement of the source NodeTemplate to a capability of the target NodeTemplate
55 for (TRelationshipType relationshipType : allRelationshipTypes) {
56 if (relationshipType.getValidSource() != null && relationshipType.getValidTarget() != null) {
57 for (TCapability capability : capabilities) {
58 if ((relationshipType.getValidSource().getTypeRef().equals(requirement.getType()) && relationshipType.getValidTarget().getTypeRef().equals(capability.getType()))) {
59 suitableRelationshipTypes.add(relationshipType);
66 // to extend the selection check if a RelationshipType can connect the type of the source NodeTemplate to the type of the target NodeTemplate
67 for (TRelationshipType rt : allRelationshipTypes) {
68 if (rt.getValidSource() != null && rt.getValidTarget() != null) {
69 if ((rt.getValidSource().getTypeRef().equals(source.getType()) && rt.getValidTarget().getTypeRef().equals(target.getType()))) {
70 suitableRelationshipTypes.add(rt);
75 // in case no suitable relationship type could be found, search for generic types without the optional ValidSource / ValidTarget elements.
76 if (suitableRelationshipTypes.isEmpty()) {
77 for (TRelationshipType rt : allRelationshipTypes) {
78 if (rt.getValidSource() == null && rt.getValidTarget() == null) {
79 suitableRelationshipTypes.add(rt);
84 return suitableRelationshipTypes;