<commit_before>package at.fhjoanneum.ippr.eventlogger.services;


import at.fhjoanneum.ippr.eventlogger.helper.LogKey;
import at.fhjoanneum.ippr.eventlogger.helper.XMLParserCommons;
import at.fhjoanneum.ippr.eventlogger.persistence.EventLogEntry;
import at.fhjoanneum.ippr.persistence.objects.model.enums.StateFunctionType;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.ParseLong;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanReader;
import org.supercsv.io.ICsvBeanReader;
import org.supercsv.prefs.CsvPreference;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;


@Transactional(isolation = Isolation.READ_COMMITTED)
@Service
public class ManipulatePNMLServiceImpl implements ManipulatePNMLService {

  private static final Logger LOG = LoggerFactory.getLogger(ManipulatePNMLServiceImpl.class);

  @Override
  public StreamResult manipulatePNML(final String pnmlContent, final String csvLog)
      throws Exception {
    StreamResult result = new StreamResult();

    try {
      final LinkedHashMap<LogKey, EventLogEntry> logQuintuplets = parseCSV(csvLog);
      final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
      final InputSource input = new InputSource(new StringReader(pnmlContent));
      final Document document = documentBuilderFactory.newDocumentBuilder().parse(input);
      final Element root = document.getDocumentElement();
      final NodeList nets = root.getElementsByTagName("net");

      for (int temp = 0; temp < nets.getLength(); temp++) {
        final Node netNode = nets.item(temp);
        if (netNode.getNodeType() == Node.ELEMENT_NODE) {
          final Element net = (Element) netNode;
          final HashMap<String, String> transitions = XMLParserCommons.getTransitionNameIdMap(net);

          int numOfCustomPlaces = 1;
          int numOfCustomArcs = 1;
          final Iterator<Map.Entry<LogKey, EventLogEntry>> it = logQuintuplets.entrySet().iterator();
          Map.Entry<LogKey, EventLogEntry> afterReceiveStateEntry = null;
          while (it.hasNext() || afterReceiveStateEntry != null) {
            Map.Entry<LogKey, EventLogEntry> entry;
            if (afterReceiveStateEntry != null) {
              entry = afterReceiveStateEntry;
            } else {
              entry = it.next();
            }

            final EventLogEntry eventLogEntry = entry.getValue();

            final String state = eventLogEntry.getState();
            final String activity = eventLogEntry.getActivity();
            final String messageType = eventLogEntry.getMessageType();
            final String recipient = eventLogEntry.getRecipient();
            final String sender = eventLogEntry.getSender();

            if (state.equals(StateFunctionType.SEND.name())) {
              afterReceiveStateEntry = null;
              final String placeId = addPlace(document, net, numOfCustomPlaces++, messageType, recipient, sender, "send", "");
              addArc(document, net, numOfCustomArcs++, transitions.get(activity), placeId,
                  messageType);
            } else if (state.equals(StateFunctionType.RECEIVE.name())) {
              if (!it.hasNext()) {
                throw (new Exception("Letzter Log-Eintrag darf kein Receive State sein!"));
              }

              final Map.Entry<LogKey, EventLogEntry> nextEntry = it.next();
              final EventLogEntry nextEventLogEntry = nextEntry.getValue();

              final String nextActivity = nextEventLogEntry.getActivity();
              final String nextMessageType = nextEventLogEntry.getMessageType();

              final String placeId = addPlace(document, net, numOfCustomPlaces++, messageType, recipient, sender, "receive", transitions.get(activity));
              addArc(document, net, numOfCustomArcs++, placeId, transitions.get(nextActivity),
                  messageType);

              afterReceiveStateEntry = nextEntry;
            } else {
              afterReceiveStateEntry = null;
            }
          }
        }

        final DOMSource source = new DOMSource(document);<commit_msg>added check if transition exists to PNML manipulation (updated algorithm)<commit_after>package at.fhjoanneum.ippr.eventlogger.services;


import at.fhjoanneum.ippr.eventlogger.helper.LogKey;
import at.fhjoanneum.ippr.eventlogger.helper.XMLParserCommons;
import at.fhjoanneum.ippr.eventlogger.persistence.EventLogEntry;
import at.fhjoanneum.ippr.persistence.objects.model.enums.StateFunctionType;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.ParseLong;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanReader;
import org.supercsv.io.ICsvBeanReader;
import org.supercsv.prefs.CsvPreference;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;


@Transactional(isolation = Isolation.READ_COMMITTED)
@Service
public class ManipulatePNMLServiceImpl implements ManipulatePNMLService {

  private static final Logger LOG = LoggerFactory.getLogger(ManipulatePNMLServiceImpl.class);

  @Override
  public StreamResult manipulatePNML(final String pnmlContent, final String csvLog)
      throws Exception {
    StreamResult result = new StreamResult();

    try {
      final LinkedHashMap<LogKey, EventLogEntry> logQuintuplets = parseCSV(csvLog);
      final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
      final InputSource input = new InputSource(new StringReader(pnmlContent));
      final Document document = documentBuilderFactory.newDocumentBuilder().parse(input);
      final Element root = document.getDocumentElement();
      final NodeList nets = root.getElementsByTagName("net");

      for (int temp = 0; temp < nets.getLength(); temp++) {
        final Node netNode = nets.item(temp);
        if (netNode.getNodeType() == Node.ELEMENT_NODE) {
          final Element net = (Element) netNode;
          final HashMap<String, String> transitions = XMLParserCommons.getTransitionNameIdMap(net);

          int numOfCustomPlaces = 1;
          int numOfCustomArcs = 1;
          final Iterator<Map.Entry<LogKey, EventLogEntry>> it = logQuintuplets.entrySet().iterator();
          Map.Entry<LogKey, EventLogEntry> afterReceiveStateEntry = null;
          while (it.hasNext() || afterReceiveStateEntry != null) {
            Map.Entry<LogKey, EventLogEntry> entry;
            if (afterReceiveStateEntry != null) {
              entry = afterReceiveStateEntry;
            } else {
              entry = it.next();
            }

            final EventLogEntry eventLogEntry = entry.getValue();

            final String state = eventLogEntry.getState();
            final String activity = eventLogEntry.getActivity();
            final String messageType = eventLogEntry.getMessageType();
            final String recipient = eventLogEntry.getRecipient();
            final String sender = eventLogEntry.getSender();

            if (state.equals(StateFunctionType.SEND.name()) && transitions.containsKey(activity)) {
              afterReceiveStateEntry = null;
              final String placeId = addPlace(document, net, numOfCustomPlaces++, messageType, recipient, sender, "send", "");
              addArc(document, net, numOfCustomArcs++, transitions.get(activity), placeId,
                  messageType);
            } else if (state.equals(StateFunctionType.RECEIVE.name())) {
              if (!it.hasNext()) {
                throw (new Exception("Letzter Log-Eintrag darf kein Receive State sein!"));
              }

              final Map.Entry<LogKey, EventLogEntry> nextEntry = it.next();
              final EventLogEntry nextEventLogEntry = nextEntry.getValue();

              final String nextActivity = nextEventLogEntry.getActivity();

              if(transitions.containsKey(nextActivity)){
                final String placeId = addPlace(document, net, numOfCustomPlaces++, messageType, recipient, sender, "receive", transitions.get(activity));
                addArc(document, net, numOfCustomArcs++, placeId, transitions.get(nextActivity),
                        messageType);

                afterReceiveStateEntry = nextEntry;
              } else {
                afterReceiveStateEntry = null;
              }

            } else {
              afterReceiveStateEntry = null;
            }
          }
        }

        final DOMSource source = new DOMSource(document);
{'fraction_non_alphanumeric': 0.09448740828871703, 'fraction_numerical': 0.001784651992861392, 'mean_word_length': 3.372345036844387, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '4945437', 'n_tokens_mistral': 2761, 'n_tokens_neox': 2672, 'n_words': 571}