<commit_before>package ingraph.ire.nodes

import akka.actor.{ActorRef, ActorSystem, Props, actorRef2Scala}
import akka.testkit.{ImplicitSender, TestActors, TestKit}
import ingraph.ire.datatypes.Tuple
import ingraph.ire.engine.RelationalEngine
import ingraph.ire.inputs.InputTransactionFactory
import ingraph.ire.listeners.ConsistentChangeListener
import ingraph.ire.messages
import ingraph.ire.messages.{ChangeSet, Terminator}
import ingraph.ire.nodes.unary.{ProductionNode, SelectionNode}
import ingraph.ire.util.TestUtil._
import ingraph.ire.messages.Terminator
import ingraph.ire.nodes.unary.SelectionNode
import org.scalatest._

class RelationalEngineIntegrationTest(_system: ActorSystem) extends TestKit(_system) with ImplicitSender
  with FlatSpecLike with Matchers with BeforeAndAfterAll {

  def this() = this(ActorSystem("MySpec"))

  override def afterAll {
    TestKit.shutdownActorSystem(system)
  }

  class TestQuery1 extends RelationalEngine {
    override val production: ActorRef = system.actorOf(Props(new ProductionNode("TestQuery1")))
    override val inputLookup: Map[String, (ChangeSet) => Unit] = Map(
      "testval" -> ((cs: ChangeSet) => forwarder ! cs)
    )
    override val terminator: Terminator = Terminator(Vector(forwarder ! _), production)
    val forwarder = system.actorOf(Props(new SelectionNode(production ! _, a => true)))
  }

  class TestQuery2 extends RelationalEngine {
    override val production: ActorRef = system.actorOf(Props(new ProductionNode("TestQuery2", 2)))
    override val inputLookup: Map[String, (ChangeSet) => Unit] = Map(
      "testval" -> ((cs: ChangeSet) => forwarder ! cs),
      "testval2" -> ((cs: ChangeSet) => forwarder2 ! cs)
    )
    override val terminator: Terminator = Terminator(Vector(forwarder ! _, forwarder2 ! _), production)
    val forwarder = system.actorOf(Props(new SelectionNode(production ! _, a => true)))
    val forwarder2 = system.actorOf(Props(new SelectionNode(production ! _, a => true)))
  }

  "multiple queries" should "work" in {
    for (i <- 1 to 1000) {
      val input = new InputTransactionFactory
      val query1 = new TestQuery1
      val query2 = new TestQuery2
      input.subscribe(query1.inputLookup)
      input.subscribe(query2.inputLookup)
      val inputTransaction1 = input.newInputTransaction
      inputTransaction1.add("testval", tuple(5, 5))
      inputTransaction1.add("testval", tuple(5, 6))
      inputTransaction1.add("testval2", tuple(5, 7))
      inputTransaction1.sendAll()
      val res1 = query1.getResults()
      assert(res1.toSet == Set(tuple(5, 5), tuple(5, 6)))
      val res2 = query2.getResults()
      assert(res2.toSet == Set(tuple(5, 5), tuple(5, 6), tuple(5, 7)))
    }
  }

  "integration" should "work" in {
    val input = new InputTransactionFactory
    val query = new TestQuery1
    input.subscribe(query.inputLookup)
    val inputTransaction1 = input.newInputTransaction
    inputTransaction1.add("testval", tuple(5, 5))<commit_msg>Shut down queries in integration test<commit_after>package ingraph.ire.nodes

import akka.actor.{ActorRef, ActorSystem, Props, actorRef2Scala}
import akka.testkit.{ImplicitSender, TestActors, TestKit}
import ingraph.ire.datatypes.Tuple
import ingraph.ire.engine.RelationalEngine
import ingraph.ire.inputs.InputTransactionFactory
import ingraph.ire.listeners.ConsistentChangeListener
import ingraph.ire.messages
import ingraph.ire.messages.{ChangeSet, Terminator}
import ingraph.ire.nodes.unary.{ProductionNode, SelectionNode}
import ingraph.ire.util.TestUtil._
import ingraph.ire.messages.Terminator
import ingraph.ire.nodes.unary.SelectionNode
import org.scalatest._

class RelationalEngineIntegrationTest(_system: ActorSystem) extends TestKit(_system) with ImplicitSender
  with FlatSpecLike with Matchers with BeforeAndAfterAll {

  def this() = this(ActorSystem("MySpec"))

  override def afterAll {
    TestKit.shutdownActorSystem(system)
  }

  class TestQuery1 extends RelationalEngine {
    override val production: ActorRef = system.actorOf(Props(new ProductionNode("TestQuery1")))
    override val inputLookup: Map[String, (ChangeSet) => Unit] = Map(
      "testval" -> ((cs: ChangeSet) => forwarder ! cs)
    )
    override val terminator: Terminator = Terminator(Vector(forwarder ! _), production)
    val forwarder = system.actorOf(Props(new SelectionNode(production ! _, a => true)))
  }

  class TestQuery2 extends RelationalEngine {
    override val production: ActorRef = system.actorOf(Props(new ProductionNode("TestQuery2", 2)))
    override val inputLookup: Map[String, (ChangeSet) => Unit] = Map(
      "testval" -> ((cs: ChangeSet) => forwarder ! cs),
      "testval2" -> ((cs: ChangeSet) => forwarder2 ! cs)
    )
    override val terminator: Terminator = Terminator(Vector(forwarder ! _, forwarder2 ! _), production)
    val forwarder = system.actorOf(Props(new SelectionNode(production ! _, a => true)))
    val forwarder2 = system.actorOf(Props(new SelectionNode(production ! _, a => true)))
  }

  "multiple queries" should "work" in {
    for (i <- 1 to 1000) {
      val input = new InputTransactionFactory
      val query1 = new TestQuery1
      val query2 = new TestQuery2
      input.subscribe(query1.inputLookup)
      input.subscribe(query2.inputLookup)
      val inputTransaction1 = input.newInputTransaction
      inputTransaction1.add("testval", tuple(5, 5))
      inputTransaction1.add("testval", tuple(5, 6))
      inputTransaction1.add("testval2", tuple(5, 7))
      inputTransaction1.sendAll()
      val res1 = query1.getResults()
      assert(res1.toSet == Set(tuple(5, 5), tuple(5, 6)))
      val res2 = query2.getResults()
      assert(res2.toSet == Set(tuple(5, 5), tuple(5, 6), tuple(5, 7)))
      query1.shutdown()
      query2.shutdown()
    }
  }

  "integration" should "work" in {
    val input = new InputTransactionFactory
    val query = new TestQuery1
    input.subscribe(query.inputLookup)
    val inputTransaction1 = input.newInputTransaction
    inputTransaction1.add("testval", tuple(5, 5))
{'fraction_non_alphanumeric': 0.1121805509459011, 'fraction_numerical': 0.018254231662794558, 'mean_word_length': 4.410233393177738, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '18896', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1789, 'n_words': 426}