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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import nl.mpi.kinnate.kindata.DataTypes;
import nl.mpi.kinnate.kindata.EntityData;
import nl.mpi.kinnate.kindata.EntityData.SymbolType;
import nl.mpi.kinnate.kindata.EntityRelation;
import nl.mpi.kinnate.kindata.GraphSorter;
import nl.mpi.kinnate.kintypestrings.ParserHighlight.ParserHighlightType;
import nl.mpi.kinnate.svg.DataStoreSvg;
import nl.mpi.kinnate.ui.KinTypeStringProvider;
import nl.mpi.kinnate.uniqueidentifiers.UniqueIdentifier;

/**
 * Document : KinTypeStringConverter
 * Created on : Sep 29, 2010, 12:52:33 PM
 * Author : <NAME>
 */
public class KinTypeStringConverter extends GraphSorter {

    DataStoreSvg dataStoreSvg;

    public KinTypeStringConverter(DataStoreSvg dataStoreSvg) {
        this.dataStoreSvg = dataStoreSvg;
    }

    public enum QueryType {

        Contains, Greater, Less, Equals
    }

    public class KinTypeElement {

        public KinTypeElement() {
            entityData = new ArrayList<EntityData>();
        }
        public KinTypeElement prevType;
        public KinTypeElement nextType;
        public KinType kinType;
        public ArrayList<QueryTerm> queryTerms;
        public ArrayList<EntityData> entityData; // there may be multiple entities for each kin term
        ParserHighlight[] highlightLocs;
    }

    public void setEgoKinTypeString(EntityData entityData) {
        for (KinType kinType : dataStoreSvg.getKinTypeDefinitions()) {
            if (kinType.isEgoType() && kinType.matchesEgonessAndSymbol(entityData, null)) { // todo: should this be passing a kin type modifier?
                entityData.addKinTypeString(kinType.codeString);
            }
        }
    }
//    public void highlightComments(String[] kinTypeStrings, ParserHighlight[][] parserHighlight) {
//        int lineCounter = 0;
//        for (String currentString : kinTypeStrings) {
//            parserHighlight[lineCounter] = new ParserHighlight[currentString.length()];
//            ParserHighlight currentHighlight = ParserHighlight.Unknown;
//            for (int charCounter = 0; charCounter < currentString.length(); charCounter++) {
//                if (currentHighlight != ParserHighlight.Comment && currentString.charAt(charCounter) == '#') {
//                    currentHighlight = ParserHighlight.Comment;
//                }
//                parserHighlight[lineCounter][charCounter] = currentHighlight;
//            }
//
//            lineCounter++;
//        }
//    }

//    @Deprecated
//    public boolean compareRequiresNextRelation(EntityData adjacentEntity, KinType requiredKinType, EntityRelation entityRelation) {
////        adjacentEntity.appendTempLabel("compareRequiresNextRelation" + "F: " + QueryParser.foundOrder++); // temp for testing // todo: remove testing labels
//        if (adjacentEntity.getSymbolType().equals(EntityData.SymbolType.union.name())) {
//            return true;
//        }
//        // todo: continue here....
//        if (requiredKinType.relationType.equals(DataTypes.RelationType.sibling) && entityRelation.relationType.equals(DataTypes.RelationType.ancestor)) {
//            return true;
//        }
//        return false;
//    }
//    @Deprecated
//    public boolean compareRelationsToKinType(EntityData egoEntity, EntityData alterEntity, KinType requiredKinType, EntityRelation entityRelation, int generationalDistance) {
////        egoEntity.appendTempLabel("compareRelationsToKinType-egoEntity" + "F: " + QueryParser.foundOrder++);
////        alterEntity.appendTempLabel("compareRelationsToKinType-alterEntity" + "F: " + QueryParser.foundOrder++);
//        // temp for testing // todo: remove testing labels
////        System.out.println("egoEntity.isEgo: " + egoEntity.isEgo);
////        System.out.println("alterEntity.isEgo: " + alterEntity.isEgo);
////        System.out.println("egoEntity.symbol: " + egoEntity.getSymbolType());
////        System.out.println("alterEntity.symbol: " + alterEntity.getSymbolType());
////        System.out.println("entityRelation.relationType: " + entityRelation.relationType);
////        System.out.println("entityRelation.symgenerationalDistancebol: " + entityRelation.generationalDistance);
//        // note that this will get the kin type reversed for one of the adjacent entities and this must be accounted for in the kin type comparison
//        // todo: note that the ego and alter are not correct labels
//        // this array will get the kin type reversed for one of the adjacent entities
//        if (egoEntity.isEgo && requiredKinType.isEgoType()) {// && alter.getSymbolType().equals(EntityData.SymbolType.triangle.name())) {
//            return true;
//        }
//        if (requiredKinType.relationType.equals(entityRelation.relationType)
//                && requiredKinType.symbolType.name().equals(alterEntity.getSymbolType())) {
//            return true;
//        }
//        return false;
//    }
    public ArrayList<KinTypeElement> getKinTypeElements(String consumableString, ParserHighlight parserHighlight) {
        int initialLength = consumableString.length();
//        if (consumableString.startsWith("[")) {
//            // todo: this is added so that a query can start with a [ since the initial kin type is redundant, however the addition of x= causes syntax highlighing issues partly because the ParserHighlight always creates an empty highlight ahead of the current one, but also it would be better to not be modifying this string
//            consumableString = "x" + consumableString;
//        }
        ArrayList<KinTypeElement> kinTypeElementList = new ArrayList<KinTypeElement>();
        KinTypeElement previousElement = null;
        boolean foundKinType = true;
        String errorMessage = null;

        if (consumableString.startsWith("@")) {
            parserHighlight = parserHighlight.addHighlight(ParserHighlightType.Message, initialLength - consumableString.length(), "A message will be shown if the required condition is met");
//if (consumableString.startsWith("@ShowMessageIfNotFound")){

//        consumableString
//                    throw new ImportRequiredException();
            return kinTypeElementList;
        }

        while (foundKinType && consumableString.length() > 0) {
            for (KinType currentReferenceKinType : dataStoreSvg.getKinTypeDefinitions()) {
                foundKinType = false;
                boolean foundStart = false;
                if (consumableString.startsWith(currentReferenceKinType.codeString)) {
                    // todo: this change is not correct and the outer for loop should be run separately and only if [ is not found etc.
                    // todo: this should also be moved into a parser package
                    foundStart = true;
                    parserHighlight = parserHighlight.addHighlight(ParserHighlightType.KinType, initialLength - consumableString.length(), currentReferenceKinType.displayString);
                } else if (consumableString.startsWith("[")) {
                    foundStart = true;
                }
                if (foundStart) {
                    KinTypeElement currentElement = new KinTypeElement();
                    if (previousElement != null) {
                        previousElement.nextType = currentElement;
                        currentElement.prevType = previousElement;
                    }
                    previousElement = currentElement;
                    currentElement.kinType = currentReferenceKinType;
                    consumableString = consumableString.substring(currentReferenceKinType.codeString.length());


                    QuerySectionParser querySectionParser = new QuerySectionParser(consumableString, parserHighlight, foundKinType, errorMessage);
                    querySectionParser.parseQuerySection(currentElement, initialLength);
                    consumableString = querySectionParser.consumableString;
                    parserHighlight = querySectionParser.parserHighlight;
                    foundKinType = querySectionParser.foundKinType;
                    errorMessage = querySectionParser.errorMessage;

                    kinTypeElementList.add(currentElement);
                    foundKinType = true;
                    break;
                }
            }
        }
        if (!foundKinType && !consumableString.startsWith("#")) {
            parserHighlight = parserHighlight.addHighlight(ParserHighlightType.Error, initialLength - consumableString.length(), errorMessage);
        }
        if (consumableString.contains("#")) {
            // check for any comments
            parserHighlight = parserHighlight.addHighlight(ParserHighlightType.Comment, initialLength - consumableString.length() + consumableString.indexOf("#"), null);
        }
        return kinTypeElementList;
    }

    public ArrayList<KinType> getKinTypes(String consumableString) {
        ArrayList<KinType> kinTypeList = new ArrayList<KinType>();<commit_msg>Added message syntax which will be used to warn the user when a required import is not available for the current diagram. Added correct parsing of the syntax including the message string and an optional import url. Implemented syntax highlighting and warning messages when the syntax is incorrect. Refactored the kin type string syntax parser. refs #1677<commit_after>package nl.mpi.kinnate.kintypestrings;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import nl.mpi.kinnate.kindata.DataTypes;
import nl.mpi.kinnate.kindata.EntityData;
import nl.mpi.kinnate.kindata.EntityData.SymbolType;
import nl.mpi.kinnate.kindata.EntityRelation;
import nl.mpi.kinnate.kindata.GraphSorter;
import nl.mpi.kinnate.kintypestrings.ParserHighlight.ParserHighlightType;
import nl.mpi.kinnate.svg.DataStoreSvg;
import nl.mpi.kinnate.ui.KinTypeStringProvider;
import nl.mpi.kinnate.uniqueidentifiers.UniqueIdentifier;

/**
 * Document : KinTypeStringConverter
 * Created on : Sep 29, 2010, 12:52:33 PM
 * Author : <NAME>
 */
public class KinTypeStringConverter extends GraphSorter {

    DataStoreSvg dataStoreSvg;

    public KinTypeStringConverter(DataStoreSvg dataStoreSvg) {
        this.dataStoreSvg = dataStoreSvg;
    }

    public enum QueryType {

        Contains, Greater, Less, Equals
    }

    public void setEgoKinTypeString(EntityData entityData) {
        for (KinType kinType : dataStoreSvg.getKinTypeDefinitions()) {
            if (kinType.isEgoType() && kinType.matchesEgonessAndSymbol(entityData, null)) { // todo: should this be passing a kin type modifier?
                entityData.addKinTypeString(kinType.codeString);
            }
        }
    }
//    public void highlightComments(String[] kinTypeStrings, ParserHighlight[][] parserHighlight) {
//        int lineCounter = 0;
//        for (String currentString : kinTypeStrings) {
//            parserHighlight[lineCounter] = new ParserHighlight[currentString.length()];
//            ParserHighlight currentHighlight = ParserHighlight.Unknown;
//            for (int charCounter = 0; charCounter < currentString.length(); charCounter++) {
//                if (currentHighlight != ParserHighlight.Comment && currentString.charAt(charCounter) == '#') {
//                    currentHighlight = ParserHighlight.Comment;
//                }
//                parserHighlight[lineCounter][charCounter] = currentHighlight;
//            }
//
//            lineCounter++;
//        }
//    }

//    @Deprecated
//    public boolean compareRequiresNextRelation(EntityData adjacentEntity, KinType requiredKinType, EntityRelation entityRelation) {
////        adjacentEntity.appendTempLabel("compareRequiresNextRelation" + "F: " + QueryParser.foundOrder++); // temp for testing // todo: remove testing labels
//        if (adjacentEntity.getSymbolType().equals(EntityData.SymbolType.union.name())) {
//            return true;
//        }
//        // todo: continue here....
//        if (requiredKinType.relationType.equals(DataTypes.RelationType.sibling) && entityRelation.relationType.equals(DataTypes.RelationType.ancestor)) {
//            return true;
//        }
//        return false;
//    }
//    @Deprecated
//    public boolean compareRelationsToKinType(EntityData egoEntity, EntityData alterEntity, KinType requiredKinType, EntityRelation entityRelation, int generationalDistance) {
////        egoEntity.appendTempLabel("compareRelationsToKinType-egoEntity" + "F: " + QueryParser.foundOrder++);
////        alterEntity.appendTempLabel("compareRelationsToKinType-alterEntity" + "F: " + QueryParser.foundOrder++);
//        // temp for testing // todo: remove testing labels
////        System.out.println("egoEntity.isEgo: " + egoEntity.isEgo);
////        System.out.println("alterEntity.isEgo: " + alterEntity.isEgo);
////        System.out.println("egoEntity.symbol: " + egoEntity.getSymbolType());
////        System.out.println("alterEntity.symbol: " + alterEntity.getSymbolType());
////        System.out.println("entityRelation.relationType: " + entityRelation.relationType);
////        System.out.println("entityRelation.symgenerationalDistancebol: " + entityRelation.generationalDistance);
//        // note that this will get the kin type reversed for one of the adjacent entities and this must be accounted for in the kin type comparison
//        // todo: note that the ego and alter are not correct labels
//        // this array will get the kin type reversed for one of the adjacent entities
//        if (egoEntity.isEgo && requiredKinType.isEgoType()) {// && alter.getSymbolType().equals(EntityData.SymbolType.triangle.name())) {
//            return true;
//        }
//        if (requiredKinType.relationType.equals(entityRelation.relationType)
//                && requiredKinType.symbolType.name().equals(alterEntity.getSymbolType())) {
//            return true;
//        }
//        return false;
//    }
    public ArrayList<KinTypeElement> getKinTypeElements(String consumableString, ParserHighlight parserHighlight) {
        int initialLength = consumableString.length();
        //if (consumableString.startsWith("[")) {
        // todo: this is added so that a query can start with a [ since the initial kin type is redundant, however the addition of x= causes syntax highlighing issues partly because the ParserHighlight always creates an empty highlight ahead of the current one, but also it would be better to not be modifying this string
        //  consumableString = "x" + consumableString;
        //}
        ArrayList<KinTypeElement> kinTypeElementList = new ArrayList<KinTypeElement>();
        KinTypeElement previousElement = null;
        boolean foundKinType = true;
        String errorMessage = null;

        while (foundKinType && consumableString.length() > 0) {
            for (KinType currentReferenceKinType : dataStoreSvg.getKinTypeDefinitions()) {
                foundKinType = false;
                boolean foundStart = false;
                if (consumableString.startsWith(currentReferenceKinType.codeString)) {
                    // todo: this change is not correct and the outer for loop should be run separately and only if [ is not found etc.
                    // todo: this should also be moved into a parser package
                    foundStart = true;
                    parserHighlight = parserHighlight.addHighlight(ParserHighlightType.KinType, initialLength - consumableString.length(), currentReferenceKinType.displayString);
                } else if (consumableString.startsWith("[")) {
                    foundStart = true;
                }
                if (foundStart) {
                    KinTypeElement currentElement = new KinTypeElement();
                    if (previousElement != null) {
                        previousElement.nextType = currentElement;
                        currentElement.prevType = previousElement;
                    }
                    previousElement = currentElement;
                    currentElement.kinType = currentReferenceKinType;
                    consumableString = consumableString.substring(currentReferenceKinType.codeString.length());


                    foundKinType = true;
                    QuerySectionParser querySectionParser = new QuerySectionParser(consumableString, parserHighlight, foundKinType, errorMessage);
                    querySectionParser.parseQuerySection(currentElement, initialLength);
                    consumableString = querySectionParser.consumableString;
                    parserHighlight = querySectionParser.parserHighlight;
                    foundKinType = querySectionParser.foundKinType;
                    errorMessage = querySectionParser.errorMessage;

                    kinTypeElementList.add(currentElement);
                    break;
                }
            }
        }
        if (!foundKinType && !consumableString.startsWith("#")) {
            parserHighlight = parserHighlight.addHighlight(ParserHighlightType.Error, initialLength - consumableString.length(), errorMessage);
        }
        if (consumableString.contains("#")) {
            // check for any comments
            parserHighlight = parserHighlight.addHighlight(ParserHighlightType.Comment, initialLength - consumableString.length() + consumableString.indexOf("#"), null);
        }
        return kinTypeElementList;
    }

    public ArrayList<KinType> getKinTypes(String consumableString) {
        ArrayList<KinType> kinTypeList = new ArrayList<KinType>();
{'fraction_non_alphanumeric': 0.09670828603859251, 'fraction_numerical': 0.0019296254256526673, 'mean_word_length': 2.6664585934248857, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, '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': '2146887', 'n_tokens_mistral': 4605, 'n_tokens_neox': 4305, 'n_words': 1030}