<commit_before>package org.alien4cloud.tosca.utils;

import static alien4cloud.utils.AlienUtils.safe;
import static org.alien4cloud.tosca.utils.ToscaTypeUtils.isOfType;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.alien4cloud.tosca.model.definitions.AbstractPropertyValue;
import org.alien4cloud.tosca.model.templates.Capability;
import org.alien4cloud.tosca.model.templates.NodeTemplate;
import org.alien4cloud.tosca.model.templates.PolicyTemplate;
import org.alien4cloud.tosca.model.templates.RelationshipTemplate;
import org.alien4cloud.tosca.model.templates.Topology;
import org.alien4cloud.tosca.model.types.NodeType;
import org.alien4cloud.tosca.model.types.PolicyType;
import org.alien4cloud.tosca.model.types.RelationshipType;
import org.alien4cloud.tosca.normative.constants.NormativeRelationshipConstants;
import org.apache.commons.collections4.CollectionUtils;

import com.google.common.collect.Sets;

import alien4cloud.tosca.context.ToscaContext;
import alien4cloud.utils.PropertyUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * Utility class for navigation purpose operations on node templates.
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class TopologyNavigationUtil {

    /**
     * Utility method to find the immediate host template of the current given template.
     * 
     * @param template The template for wich to get the immediate host.
     * @return
     */
    public static NodeTemplate getImmediateHostTemplate(Topology topology, NodeTemplate template) {
        RelationshipTemplate host = getRelationshipFromType(template, NormativeRelationshipConstants.HOSTED_ON);
        if (host == null) {
            return null;
        }
        return topology.getNodeTemplates().get(host.getTarget());
    }

    public static NodeTemplate getImmediateHostTemplate(Topology topology, NodeTemplate template, IToscaTypeFinder toscaTypeFinder) {
        RelationshipTemplate host = getRelationshipFromType(template, NormativeRelationshipConstants.HOSTED_ON,
                id -> toscaTypeFinder.findElement(RelationshipType.class, id));
        if (host == null) {
            return null;
        }
        return topology.getNodeTemplates().get(host.getTarget());
    }

    /**
     * Get all incoming relationships of a node that match the requested type.
     * 
     * @param template The node template for which to get relationships.
     * @param type The expected type.
     * @return
     */
    public static RelationshipTemplate getRelationshipFromType(NodeTemplate template, String type) {
        return getRelationshipFromType(template, type, id -> ToscaContext.getOrFail(RelationshipType.class, type));
    }

    public static RelationshipTemplate getRelationshipFromType(NodeTemplate template, String type, IRelationshipTypeFinder toscaTypeFinder) {
        for (RelationshipTemplate relationshipTemplate : safe(template.getRelationships()).values()) {
            RelationshipType relationshipType = toscaTypeFinder.findElement(relationshipTemplate.getType());
            if (relationshipType != null && (relationshipType.getElementId().equals(type) || relationshipType.getDerivedFrom().contains(type))) {
                return relationshipTemplate;
            }
        }
        return null;
    }

    /**
     * @return true if this node is hosted on another.
     */<commit_msg>fix issue in relationship finder.<commit_after>package org.alien4cloud.tosca.utils;

import static alien4cloud.utils.AlienUtils.safe;
import static org.alien4cloud.tosca.utils.ToscaTypeUtils.isOfType;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.alien4cloud.tosca.model.definitions.AbstractPropertyValue;
import org.alien4cloud.tosca.model.templates.Capability;
import org.alien4cloud.tosca.model.templates.NodeTemplate;
import org.alien4cloud.tosca.model.templates.PolicyTemplate;
import org.alien4cloud.tosca.model.templates.RelationshipTemplate;
import org.alien4cloud.tosca.model.templates.Topology;
import org.alien4cloud.tosca.model.types.NodeType;
import org.alien4cloud.tosca.model.types.PolicyType;
import org.alien4cloud.tosca.model.types.RelationshipType;
import org.alien4cloud.tosca.normative.constants.NormativeRelationshipConstants;
import org.apache.commons.collections4.CollectionUtils;

import com.google.common.collect.Sets;

import alien4cloud.tosca.context.ToscaContext;
import alien4cloud.utils.PropertyUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * Utility class for navigation purpose operations on node templates.
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class TopologyNavigationUtil {

    /**
     * Utility method to find the immediate host template of the current given template.
     * 
     * @param template The template for wich to get the immediate host.
     * @return
     */
    public static NodeTemplate getImmediateHostTemplate(Topology topology, NodeTemplate template) {
        RelationshipTemplate host = getRelationshipFromType(template, NormativeRelationshipConstants.HOSTED_ON);
        if (host == null) {
            return null;
        }
        return topology.getNodeTemplates().get(host.getTarget());
    }

    public static NodeTemplate getImmediateHostTemplate(Topology topology, NodeTemplate template, IToscaTypeFinder toscaTypeFinder) {
        RelationshipTemplate host = getRelationshipFromType(template, NormativeRelationshipConstants.HOSTED_ON,
                id -> toscaTypeFinder.findElement(RelationshipType.class, id));
        if (host == null) {
            return null;
        }
        return topology.getNodeTemplates().get(host.getTarget());
    }

    /**
     * Get all incoming relationships of a node that match the requested type.
     * 
     * @param template The node template for which to get relationships.
     * @param type The expected type.
     * @return
     */
    public static RelationshipTemplate getRelationshipFromType(NodeTemplate template, String type) {
        return getRelationshipFromType(template, type, id -> ToscaContext.getOrFail(RelationshipType.class, id));
    }

    public static RelationshipTemplate getRelationshipFromType(NodeTemplate template, String type, IRelationshipTypeFinder toscaTypeFinder) {
        for (RelationshipTemplate relationshipTemplate : safe(template.getRelationships()).values()) {
            RelationshipType relationshipType = toscaTypeFinder.findElement(relationshipTemplate.getType());
            if (relationshipType != null && (relationshipType.getElementId().equals(type) || relationshipType.getDerivedFrom().contains(type))) {
                return relationshipTemplate;
            }
        }
        return null;
    }

    /**
     * @return true if this node is hosted on another.
     */
{'fraction_non_alphanumeric': 0.08549373725604427, 'fraction_numerical': 0.00466064666472473, 'mean_word_length': 4.012408759124088, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5775808', 'n_tokens_mistral': 1864, 'n_tokens_neox': 1864, 'n_words': 408}