<commit_before>/*******************************************************************************
 * Copyright (c) 2011 Bolton University, UK.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the License
 * which accompanies this distribution in the file LICENSE.txt
 *******************************************************************************/
package uk.ac.bolton.archimate.model.util;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EClass;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import uk.ac.bolton.archimate.model.IArchimateElement;
import uk.ac.bolton.archimate.model.IArchimatePackage;


/**
 * Representation of the ArchiMate Relationships Matrix
 * 
 * @author <NAME>
 */
public class RelationshipsMatrix {
    
    /**
     * Singleton instance
     */
    public static RelationshipsMatrix INSTANCE = new RelationshipsMatrix();
    
    /**
     * The Bundle ID
     */
    static final String BUNDLE_ID = "uk.ac.bolton.archimate.model"; //$NON-NLS-1$

    /**
     * The Key letters XML file
     */
    static final String RELATIONSHIPS_KEYS_FILE = "model/relationships-keys.xml"; //$NON-NLS-1$

    /**
     * The Relationships 1.0 XML file
     */
    static final String RELATIONSHIPS_1_0_FILE = "model/relationships-2.0.xml"; //$NON-NLS-1$
    
    /*
     * XML element and attribute names
     */
    static final String XML_ELEMENT_ROOT = "relationships"; //$NON-NLS-1$
    static final String XML_ELEMENT_KEYS = "keys"; //$NON-NLS-1$
    static final String XML_ELEMENT_KEY = "key"; //$NON-NLS-1$
    static final String XML_ELEMENT_ELEMENTS = "elements"; //$NON-NLS-1$
    static final String XML_ELEMENT_SOURCE = "source"; //$NON-NLS-1$
    static final String XML_ELEMENT_TARGET = "target"; //$NON-NLS-1$
    static final String XML_ATTRIBUTE_CHAR = "char"; //$NON-NLS-1$
    static final String XML_ATTRIBUTE_RELATIONSHIP = "relationship"; //$NON-NLS-1$
    static final String XML_ATTRIBUTE_RELATIONS = "relations"; //$NON-NLS-1$
    static final String XML_ATTRIBUTE_ELEMENT = "element"; //$NON-NLS-1$

    public static class TargetMatrix {
        EClass targetClass;
        List<EClass> relationships = new ArrayList<EClass>();
        
        public EClass getTargetClass() {
            return targetClass;
        }
        
        public List<EClass> getRelationships() {
            return relationships;
        }
    }
    
    /**
     * Mapping of source elements to target elements and possible relations
     */
    private Map<EClass, List<TargetMatrix>> matrixMap = new HashMap<EClass, List<TargetMatrix>>();
    
    /**
     * Mapping of key letters to relationships
     */
    private Map<Character, EClass> relationsKeyMap = new LinkedHashMap<Character, EClass>();
        
    /**
     * Mapping of relationships to key letters
     */
    private Map<EClass, Character> relationsValueMap = new LinkedHashMap<EClass, Character>();

    private RelationshipsMatrix() {
        // Load Key letters file
        loadKeyLetters();
        
        // URL to the Relationships 1.0 XML file
        URL url = Platform.getBundle(BUNDLE_ID).getResource(RELATIONSHIPS_1_0_FILE);
        loadRelationships(url);
    }
    
    public Map<EClass, List<TargetMatrix>> getRelationshipsMatrix() {
        return matrixMap;
    }
    
    public Map<EClass, Character> getRelationshipsValueMap() {
        return relationsValueMap;
    }

    boolean isValidRelationshipStart(IArchimateElement sourceElement, EClass relationshipType) {
        EClass sourceType = getSuperEClass(sourceElement.eClass());
        
        List<TargetMatrix> listMatrix = matrixMap.get(sourceType);
        if(listMatrix != null) {
            for(TargetMatrix targetMatrix : listMatrix) {
                if(targetMatrix.getRelationships().contains(relationshipType)) {
                    return true;
                }
            }
        }
        
        return false;
    }
    
    boolean isValidRelationship(EClass sourceType, EClass targetType, EClass relationshipType) {
        if(relationshipType == null) {
            return false;
        }
        <commit_msg>Use correct variable name for Relationships XML file<commit_after>/*******************************************************************************
 * Copyright (c) 2011 Bolton University, UK.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the License
 * which accompanies this distribution in the file LICENSE.txt
 *******************************************************************************/
package uk.ac.bolton.archimate.model.util;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EClass;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import uk.ac.bolton.archimate.model.IArchimateElement;
import uk.ac.bolton.archimate.model.IArchimatePackage;


/**
 * Representation of the ArchiMate Relationships Matrix
 * 
 * @author <NAME>
 */
public class RelationshipsMatrix {
    
    /**
     * Singleton instance
     */
    public static RelationshipsMatrix INSTANCE = new RelationshipsMatrix();
    
    /**
     * The Bundle ID
     */
    static final String BUNDLE_ID = "uk.ac.bolton.archimate.model"; //$NON-NLS-1$

    /**
     * The Key letters XML file
     */
    static final String RELATIONSHIPS_KEYS_FILE = "model/relationships-keys.xml"; //$NON-NLS-1$

    /**
     * The Relationships XML file
     */
    static final String RELATIONSHIPS_FILE = "model/relationships-2.0.xml"; //$NON-NLS-1$
    
    /*
     * XML element and attribute names
     */
    static final String XML_ELEMENT_ROOT = "relationships"; //$NON-NLS-1$
    static final String XML_ELEMENT_KEYS = "keys"; //$NON-NLS-1$
    static final String XML_ELEMENT_KEY = "key"; //$NON-NLS-1$
    static final String XML_ELEMENT_ELEMENTS = "elements"; //$NON-NLS-1$
    static final String XML_ELEMENT_SOURCE = "source"; //$NON-NLS-1$
    static final String XML_ELEMENT_TARGET = "target"; //$NON-NLS-1$
    static final String XML_ATTRIBUTE_CHAR = "char"; //$NON-NLS-1$
    static final String XML_ATTRIBUTE_RELATIONSHIP = "relationship"; //$NON-NLS-1$
    static final String XML_ATTRIBUTE_RELATIONS = "relations"; //$NON-NLS-1$
    static final String XML_ATTRIBUTE_ELEMENT = "element"; //$NON-NLS-1$

    public static class TargetMatrix {
        EClass targetClass;
        List<EClass> relationships = new ArrayList<EClass>();
        
        public EClass getTargetClass() {
            return targetClass;
        }
        
        public List<EClass> getRelationships() {
            return relationships;
        }
    }
    
    /**
     * Mapping of source elements to target elements and possible relations
     */
    private Map<EClass, List<TargetMatrix>> matrixMap = new HashMap<EClass, List<TargetMatrix>>();
    
    /**
     * Mapping of key letters to relationships
     */
    private Map<Character, EClass> relationsKeyMap = new LinkedHashMap<Character, EClass>();
        
    /**
     * Mapping of relationships to key letters
     */
    private Map<EClass, Character> relationsValueMap = new LinkedHashMap<EClass, Character>();

    private RelationshipsMatrix() {
        // Load Key letters file
        loadKeyLetters();
        
        // URL to the Relationships XML file
        URL url = Platform.getBundle(BUNDLE_ID).getResource(RELATIONSHIPS_FILE);
        loadRelationships(url);
    }
    
    public Map<EClass, List<TargetMatrix>> getRelationshipsMatrix() {
        return matrixMap;
    }
    
    public Map<EClass, Character> getRelationshipsValueMap() {
        return relationsValueMap;
    }

    boolean isValidRelationshipStart(IArchimateElement sourceElement, EClass relationshipType) {
        EClass sourceType = getSuperEClass(sourceElement.eClass());
        
        List<TargetMatrix> listMatrix = matrixMap.get(sourceType);
        if(listMatrix != null) {
            for(TargetMatrix targetMatrix : listMatrix) {
                if(targetMatrix.getRelationships().contains(relationshipType)) {
                    return true;
                }
            }
        }
        
        return false;
    }
    
    boolean isValidRelationship(EClass sourceType, EClass targetType, EClass relationshipType) {
        if(relationshipType == null) {
            return false;
        }
        
{'fraction_non_alphanumeric': 0.13309066090319646, 'fraction_numerical': 0.005232624274826527, 'mean_word_length': 2.839301310043668, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '1469028', 'n_tokens_mistral': 2530, 'n_tokens_neox': 2452, 'n_words': 607}