<commit_before>package hu.bme.mit.ire

import java.util.{Iterator => JIterator}

import hu.bme.mit.ire.datatypes.Tuple
import ingraph.ire.{EntityToTupleMapper, IdParser, IngraphEdge, IngraphVertex}
import ingraph.model.tplan.{GetEdges, GetVertices}

class Neo4jEntityToTupleMapper(vertexConverters: Map[Set[String], Set[GetVertices]],
                               edgeConverters: Map[String, Set[GetEdges]]) extends IdParser with EntityToTupleMapper {

  override def idParser(obj: Any): Any = obj

  var transaction: Transaction = _

  def elementToNode(element: IngraphVertex, required: Seq[String]): Tuple = {
    Vector(idParser(element.id)) ++
      required.tail.map(key => element.properties(key))
  }

  def addEdge(edge: IngraphEdge): Unit = {
    for (operators <- edgeConverters.get(edge.`type`); operator <- operators) {
      val sourceLabels = operator.fnode.jnode.src.labels.vertexLabels
      val targetLabels = operator.fnode.jnode.trg.labels.vertexLabels
      if (sourceLabels.subsetOf(edge.sourceVertex.labels) &&
        targetLabels.subsetOf(edge.targetVertex.labels)) {
        val tuple = edgeToTupleType(edge, operator)
        transaction.add(operator.fnode.jnode.edge.name, tuple)
      }
    }
  }

  def removeEdge(edge: IngraphEdge): Unit = {
    for (operators <- edgeConverters.get(edge.`type`); operator <- operators) {
      val sourceLabels = operator.fnode.jnode.src.labels.vertexLabels
      val targetLabels = operator.fnode.jnode.trg.labels.vertexLabels
      if (sourceLabels.subsetOf(edge.sourceVertex.labels) &&
        targetLabels.subsetOf(edge.targetVertex.labels)) {
        val tuple = edgeToTupleType(edge, operator)
        transaction.remove(operator.fnode.jnode.edge.name, tuple)
      }
    }
  }

  def addVertex(vertex: IngraphVertex): Unit = {
    vertexConverters.filter(p => p._1.subsetOf(vertex.labels)).foreach {
      f =>
        for (operator <- f._2) {
          val tuple = elementToNode(vertex, operator.fnode.internalSchema.map(_.name))
          transaction.add(operator.fnode.jnode.v.name, tuple)
        }
    }
  }

  def removeVertex(vertex: IngraphVertex): Unit = {
    vertexConverters.filter(p => p._1.subsetOf(vertex.labels)).foreach {
      f =>
        for (operator <- f._2) {
          val tuple = elementToNode(vertex, operator.fnode.internalSchema.map(_.name))
          transaction.remove(operator.fnode.jnode.v.name, tuple)
        }
    }
  }

  private def edgeToTupleType(edge: IngraphEdge, operator: GetEdges): Tuple = {
    Vector(idParser(edge.sourceVertex.id), idParser(edge.id), idParser(edge.targetVertex.id)) ++
      operator.fnode.internalSchema.drop(3)
        .map {
          case a if a.nodeName == operator.fnode.jnode.src.name => edge.sourceVertex.properties(a.name)
          case a if a.nodeName == operator.fnode.jnode.trg.name => edge.targetVertex.properties(a.name)
          case a if a.nodeName == operator.fnode.jnode.edge.name => edge.properties(a.name)
        }
  }
}<commit_msg>Return null instead of exception on nonexsistent propertyt p<commit_after>package hu.bme.mit.ire

import java.util.{Iterator => JIterator}

import hu.bme.mit.ire.datatypes.Tuple
import ingraph.ire.{EntityToTupleMapper, IdParser, IngraphEdge, IngraphVertex}
import ingraph.model.tplan.{GetEdges, GetVertices}

class Neo4jEntityToTupleMapper(vertexConverters: Map[Set[String], Set[GetVertices]],
                               edgeConverters: Map[String, Set[GetEdges]]) extends IdParser with EntityToTupleMapper {

  override def idParser(obj: Any): Any = obj

  var transaction: Transaction = _

  def elementToNode(element: IngraphVertex, required: Seq[String]): Tuple = {
    Vector(idParser(element.id)) ++
      required.tail.map(key => element.properties.getOrElse(key, null))
  }

  def addEdge(edge: IngraphEdge): Unit = {
    for (operators <- edgeConverters.get(edge.`type`); operator <- operators) {
      val sourceLabels = operator.fnode.jnode.src.labels.vertexLabels
      val targetLabels = operator.fnode.jnode.trg.labels.vertexLabels
      if (sourceLabels.subsetOf(edge.sourceVertex.labels) &&
        targetLabels.subsetOf(edge.targetVertex.labels)) {
        val tuple = edgeToTupleType(edge, operator)
        transaction.add(operator.fnode.jnode.edge.name, tuple)
      }
    }
  }

  def removeEdge(edge: IngraphEdge): Unit = {
    for (operators <- edgeConverters.get(edge.`type`); operator <- operators) {
      val sourceLabels = operator.fnode.jnode.src.labels.vertexLabels
      val targetLabels = operator.fnode.jnode.trg.labels.vertexLabels
      if (sourceLabels.subsetOf(edge.sourceVertex.labels) &&
        targetLabels.subsetOf(edge.targetVertex.labels)) {
        val tuple = edgeToTupleType(edge, operator)
        transaction.remove(operator.fnode.jnode.edge.name, tuple)
      }
    }
  }

  def addVertex(vertex: IngraphVertex): Unit = {
    vertexConverters.filter(p => p._1.subsetOf(vertex.labels)).foreach {
      f =>
        for (operator <- f._2) {
          val tuple = elementToNode(vertex, operator.fnode.internalSchema.map(_.name))
          transaction.add(operator.fnode.jnode.v.name, tuple)
        }
    }
  }

  def removeVertex(vertex: IngraphVertex): Unit = {
    vertexConverters.filter(p => p._1.subsetOf(vertex.labels)).foreach {
      f =>
        for (operator <- f._2) {
          val tuple = elementToNode(vertex, operator.fnode.internalSchema.map(_.name))
          transaction.remove(operator.fnode.jnode.v.name, tuple)
        }
    }
  }

  private def edgeToTupleType(edge: IngraphEdge, operator: GetEdges): Tuple = {
    Vector(idParser(edge.sourceVertex.id), idParser(edge.id), idParser(edge.targetVertex.id)) ++
      operator.fnode.internalSchema.drop(3)
        .map {
          case a if a.nodeName == operator.fnode.jnode.src.name => edge.sourceVertex.properties.getOrElse(a.name, null)
          case a if a.nodeName == operator.fnode.jnode.trg.name => edge.targetVertex.properties.getOrElse(a.name, null)
          case a if a.nodeName == operator.fnode.jnode.edge.name => edge.properties.getOrElse(a.name, null)
        }
  }
}
{'fraction_non_alphanumeric': 0.12465146793505003, 'fraction_numerical': 0.0019681810726586845, 'mean_word_length': 3.2733006306937633, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, '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': '2654302', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1904, 'n_words': 320}