<commit_before>package edu.gemini.lchquery.servlet

import edu.gemini.pot.sp.{ISPObservation, ISPProgram}
import edu.gemini.spModel.`type`.DisplayableSpType
import edu.gemini.spModel.ao.{AOConstants, AOTreeUtil}
import edu.gemini.spModel.core.ProgramId
import edu.gemini.spModel.core.ProgramId.Science
import edu.gemini.spModel.data.YesNoType
import edu.gemini.spModel.dataset.{DatasetLabel, DatasetQaRecord}
import edu.gemini.spModel.gemini.altair.{AltairParams, InstAltair}
import edu.gemini.spModel.gemini.obscomp.{SPProgram, SPSiteQuality}
import edu.gemini.spModel.gemini.obscomp.SPProgram.Active
import edu.gemini.spModel.gemini.phase1.GsaPhase1Data
import edu.gemini.spModel.obs.{ObsClassService, ObsTargetCalculatorService, ObservationStatus, SPObservation}
import edu.gemini.spModel.obsclass.ObsClass
import edu.gemini.spModel.obscomp.SPInstObsComp
import edu.gemini.spModel.obslog.ObsLog
import edu.gemini.spModel.target.env.TargetEnvironment
import edu.gemini.spModel.target.obsComp.TargetObsComp
import edu.gemini.spModel.too.{Too, TooType}
import edu.gemini.spModel.util.SPTreeUtil

import scala.collection.JavaConverters._
import scala.util.matching.Regex
import scalaz._
import Scalaz._


sealed trait ValueMatcher[A] {
  def matches(expression: String, x: A): Boolean
}

object ValueMatcher {
  private[servlet] implicit class ToRegex(val expression: String) extends AnyVal {
    def toRegex: Regex = ("^(?i)" +
      (expression.contains("|") ? s"($expression)" | expression).
        replaceAllLiterally("+", "\\+").
        replaceAllLiterally("*", ".*").
        replaceAllLiterally("?", ".")
      + "$").r
  }

  abstract class StringValueMatcher[A] extends ValueMatcher[A] {
    protected def extractor(a: A): Option[String]

    override def matches(expression: String, x: A): Boolean = (for {
      r <- Option(expression).map(_.toRegex)
      m <- Option(x).flatMap(extractor)
    } yield r.findFirstMatchIn(m).isDefined).getOrElse(false)
  }


  abstract class BooleanValueMatcher[A, B <: DisplayableSpType] extends ValueMatcher[A] {
    protected def extractor(a: A): Option[B]

    private def transform(expression: String): String = expression.toLowerCase match {<commit_msg>REL-2655: Fixed test cases and regex.<commit_after>package edu.gemini.lchquery.servlet

import edu.gemini.pot.sp.{ISPObservation, ISPProgram}
import edu.gemini.spModel.`type`.DisplayableSpType
import edu.gemini.spModel.ao.{AOConstants, AOTreeUtil}
import edu.gemini.spModel.core.ProgramId
import edu.gemini.spModel.core.ProgramId.Science
import edu.gemini.spModel.data.YesNoType
import edu.gemini.spModel.dataset.{DatasetLabel, DatasetQaRecord}
import edu.gemini.spModel.gemini.altair.{AltairParams, InstAltair}
import edu.gemini.spModel.gemini.obscomp.{SPProgram, SPSiteQuality}
import edu.gemini.spModel.gemini.obscomp.SPProgram.Active
import edu.gemini.spModel.gemini.phase1.GsaPhase1Data
import edu.gemini.spModel.obs.{ObsClassService, ObsTargetCalculatorService, ObservationStatus, SPObservation}
import edu.gemini.spModel.obsclass.ObsClass
import edu.gemini.spModel.obscomp.SPInstObsComp
import edu.gemini.spModel.obslog.ObsLog
import edu.gemini.spModel.target.env.TargetEnvironment
import edu.gemini.spModel.target.obsComp.TargetObsComp
import edu.gemini.spModel.too.{Too, TooType}
import edu.gemini.spModel.util.SPTreeUtil

import scala.collection.JavaConverters._
import scala.util.matching.Regex
import scalaz._
import Scalaz._


sealed trait ValueMatcher[A] {
  def matches(expression: String, x: A): Boolean
}

object ValueMatcher {
  private[servlet] implicit class ToRegex(val expression: String) extends AnyVal {
    def toRegex: Regex = ("^(?i)" +
        (expression.contains("|") ? s"($expression)" | expression).
          replaceAllLiterally("-", "\\-").
          replaceAllLiterally("+", "\\+").
          replaceAllLiterally("*", ".*").
          replaceAllLiterally("%", ".*").
          replaceAllLiterally("?", ".")
        + "$").r
  }

  abstract class StringValueMatcher[A] extends ValueMatcher[A] {
    protected def extractor(a: A): Option[String]

    override def matches(expression: String, x: A): Boolean = (for {
      r <- Option(expression).map(_.toRegex)
      m <- Option(x).flatMap(extractor)
    } yield r.findFirstMatchIn(m).isDefined).getOrElse(false)
  }


  abstract class BooleanValueMatcher[A, B <: DisplayableSpType] extends ValueMatcher[A] {
    protected def extractor(a: A): Option[B]

    private def transform(expression: String): String = expression.toLowerCase match {
{'fraction_non_alphanumeric': 0.12541399867520425, 'fraction_numerical': 0.0017663943475380878, 'mean_word_length': 4.89076723016905, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '6821591', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1498, 'n_words': 267}