<commit_before>package nl.mpi.kinnate.uniqueidentifiers;

import java.util.UUID;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
import nl.mpi.arbil.data.ArbilField;

/**
 *  Document   : UniqueIdentifier
 *  Created on : Jul 21, 2011, 3:23:17 PM
 *  Author     : <NAME>
 */
@XmlRootElement(name = "UniqueIdentifier")
public class UniqueIdentifier {

    public enum IdentifierType {

        pid /*Persistent Identifier*/, lid /*Local Identifier*/, tid /*Transient Identifier*/, gid /*Graphics Identifier*/

    }
    @XmlValue
    private String identifierString = null;
    @XmlAttribute(name = "type")
    private IdentifierType identifierType = null;

    private UniqueIdentifier() {
    }

    public UniqueIdentifier(IdentifierType identifierTypeLocal) {
        identifierType = identifierTypeLocal;
        identifierString = UUID.randomUUID().toString();
    }

    public UniqueIdentifier(ArbilField arbilField) {
        if (arbilField.getFullXmlPath().endsWith(".UniqueIdentifier.LocalIdentifier")) {
            identifierType = IdentifierType.lid;
        } else if (arbilField.getFullXmlPath().endsWith(".UniqueIdentifier.PersistantIdentifier")) {
            identifierType = IdentifierType.pid;
        } else {
            throw new UnsupportedOperationException("Incorrect ArbilField: " + arbilField.getFullXmlPath());
        }
        identifierString = arbilField.getFieldValue();
    }

    public UniqueIdentifier(String attributeIdentifier) {
        // reconstruct the identifier from an attribte string originally obtained by getAttributeIdentifier
        String[] attributeIdentifierParts = attributeIdentifier.split("\\:");
        if (attributeIdentifierParts.length != 2) {
            throw new UnsupportedOperationException("Incorrect identifier format: " + attributeIdentifier);
        }
        identifierType = IdentifierType.valueOf(attributeIdentifierParts[0]);
        identifierString = attributeIdentifierParts[1];
    }

//    public UniqueIdentifier(String userDefinedIdentifier, IdentifierType identifierTypeLocal) {
//        switch (identifierTypeLocal) {
//            case tid:
//                identifierString = userDefinedIdentifier;
//                identifierType = identifierTypeLocal;
//                break;
//            case pid:
//            case lid:
//            case gid:
//                throw new UnsupportedOperationException("Unsupported user defined identifier, these must be transient identifiers");
//        }
//    }

    public String getQueryIdentifier() {
        // todo: limit the query to local or persistent
        if (identifierString != null) {
            return identifierString;
        } else {
            throw new UnsupportedOperationException();
        }
    }

    public String getAttributeIdentifier() {
        return identifierType.name() + ":" + identifierString;
    }

    public boolean isGraphicsIdentifier() {
        return identifierType == IdentifierType.gid;
    }
<commit_msg>Modified the unique identifier that transient entities use to be based on an md5 sum of the users input so that they have the same identifier on each redraw and on loading a saved document, otherwise the entity positions on the graph get lost. Simplified the unique identifier and its xml form. Modified the import process to insert back references for each entity relation so that the xml queries can take 1/10th of the time. Added options to highlight or not the relation lines of selected entities and to hide or show the diagram border. Replaced the string based unique identifier with the unique identifier object. Added a common unique identifier used by entities and graphics elements alike.<commit_after>package nl.mpi.kinnate.uniqueidentifiers;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
import nl.mpi.arbil.data.ArbilField;

/**
 *  Document   : UniqueIdentifier
 *  Created on : Jul 21, 2011, 3:23:17 PM
 *  Author     : <NAME>
 */
@XmlRootElement(name = "UniqueIdentifier")
public class UniqueIdentifier {

    public enum IdentifierType {

        pid /*Persistent Identifier*/, lid /*Local Identifier*/, tid /*Transient Identifier produced by md5 summing a string */, gid /*Graphics Identifier*/

    }
    @XmlValue
    private String identifierString = null;
    @XmlAttribute(name = "type")
    private IdentifierType identifierType = null;

    private UniqueIdentifier() {
    }

    public UniqueIdentifier(IdentifierType identifierTypeLocal) {
        identifierType = identifierTypeLocal;
        identifierString = UUID.randomUUID().toString();
    }

    public UniqueIdentifier(ArbilField arbilField) {
        if (arbilField.getFullXmlPath().endsWith(".UniqueIdentifier.LocalIdentifier")) {
            identifierType = IdentifierType.lid;
        } else if (arbilField.getFullXmlPath().endsWith(".UniqueIdentifier.PersistantIdentifier")) {
            identifierType = IdentifierType.pid;
        } else {
            throw new UnsupportedOperationException("Incorrect ArbilField: " + arbilField.getFullXmlPath());
        }
        identifierString = arbilField.getFieldValue();
    }

    public UniqueIdentifier(String attributeIdentifier) {
        // reconstruct the identifier from an attribte string originally obtained by getAttributeIdentifier
        String[] attributeIdentifierParts = attributeIdentifier.split("\\:");
        if (attributeIdentifierParts.length != 2) {
            throw new UnsupportedOperationException("Incorrect identifier format: " + attributeIdentifier);
        }
        identifierType = IdentifierType.valueOf(attributeIdentifierParts[0]);
        identifierString = attributeIdentifierParts[1];
    }

    public UniqueIdentifier(String userDefinedIdentifier, IdentifierType identifierTypeLocal) {
        // this is required so that transient entities have the same identifier on each redraw and on loading a saved document, otherwise the entity positions on the graph get lost
        // hash the string text so that it is valid in xml attributes but still reconstructable from the same user input
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            byte[] digestbytes = messageDigest.digest(userDefinedIdentifier.getBytes());
            StringBuilder hexString = new StringBuilder();
            for (int i = 0; i < digestbytes.length; ++i) {
                hexString.append(Integer.toHexString(0x0100 + (digestbytes[i] & 0x00FF)).substring(1));
            }
            switch (identifierTypeLocal) {
                case tid:
                    identifierString = hexString.toString();
                    identifierType = identifierTypeLocal;
                    break;
                case pid:
                case lid:
                case gid:
                    throw new UnsupportedOperationException("Unsupported user defined identifier, these must be transient identifiers");
            }
        } catch (NoSuchAlgorithmException exception) {
            throw new UnsupportedOperationException("Cannot hash the transient identifier");
        }
    }

    public String getQueryIdentifier() {
        // todo: limit the query to local or persistent
        if (identifierString != null) {
            return identifierString;
        } else {
            throw new UnsupportedOperationException();
        }
    }

    public String getAttributeIdentifier() {
        return identifierType.name() + ":" + identifierString;
    }

    public boolean isGraphicsIdentifier() {
        return identifierType == IdentifierType.gid;
    }

{'fraction_non_alphanumeric': 0.08106388393993383, 'fraction_numerical': 0.00559938915754645, 'mean_word_length': 2.9591939546599497, 'pattern_counts': {'":': 2, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 9}, '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': '7592398', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1828, 'n_words': 559}