<commit_before>package cspom

import java.io.IOException
import java.io.InputStream
import java.net.URI
import java.net.URL
import java.util.zip.GZIPInputStream
import scala.Iterator
import scala.collection.JavaConversions
import scala.util.parsing.combinator.JavaTokenParsers
import scala.util.parsing.input.CharSequenceReader
import org.apache.tools.bzip2.CBZip2InputStream
import cspom.dimacs.CNFParser
import cspom.extension.Relation
import cspom.extension.Table
import cspom.flatzinc.FlatZincParser
import cspom.variable.BoolVariable
import cspom.variable.CSPOMConstant
import cspom.variable.CSPOMExpression
import cspom.variable.CSPOMSeq
import cspom.variable.FreeVariable
import cspom.variable.IntVariable
import cspom.variable.SimpleExpression
import cspom.xcsp.XCSPParser
import cspom.variable.CSPOMVariable

object NameParser extends JavaTokenParsers {

  def parse: Parser[(String, Seq[Int])] = ident ~ rep("[" ~> wholeNumber <~ "]") ^^ {
    case n ~ i => (n, i.map(_.toInt))
  }
}

/**
 *
 * CSPOM is the central class of the Constraint Satisfaction Problem Object
 * Model. You can create a problem from scratch by instantiating this class and
 * then using the ctr and var methods. The CSPOM.load() method can be used in
 * order to create a CSPOM object from an XCSP file.
 * <p>
 * The CSPOM class adheres to the following definition :
 *
 * A CSP is defined as a pair (X, C), X being a finite set of variables, and C a
 * finite set of constraints.
 *
 * A domain is associated to each variable x in X. Each constraint involves a
 * finite set of variables (its scope) and defines a set of allowed and
 * forbidden instantiations of these variables.
 *
 * @author <NAME>
 * @see CSPOMConstraint
 * @see CSPOMVariable
 *
 */
class CSPOM {

  /**
   * Map used to easily retrieve a variable according to its name.
   */
  private var _namedExpressions = Map[String, CSPOMExpression[_]]()

  /**
   * @return The named expressions of this problem.
   */
  def namedExpressions = _namedExpressions

  def getExpressions = JavaConversions.asJavaCollection(namedExpressions)

  /**
   * @param variableName
   *            A variable name.
   * @return The variable with the corresponding name.
   */
  def expression(name: String): Option[CSPOMExpression[_]] = {
    NameParser.parse(new CharSequenceReader(name)).map(Some(_)).getOrElse(None).flatMap {
      case (n, s) => getInSeq(namedExpressions.get(n), s)
    }

  }

  def variable(name: String): CSPOMVariable[_] = {
    expression(name).map {
      case v: CSPOMVariable[_] => v
      case e: CSPOMExpression[_] => throw new IllegalArgumentException(s"$e is not a variable")
    } getOrElse {
      throw new IllegalArgumentException(s"Could not find $name")
    }
  }

  private def getInSeq(e: Option[CSPOMExpression[_]], s: Seq[Int]): Option[CSPOMExpression[_]] = {
    if (s.isEmpty) {
      e
    } else {
      e.collect {
        case v: CSPOMSeq[_] => getInSeq(Some(v(s.head)), s.tail)
      } flatten
    }
  }

  /**
   * Collection of all constraints of the problem.
   */
  private val _constraints = collection.mutable.Set[CSPOMConstraint[_]]()

  def constraints = _constraints.iterator<commit_msg>Added a method to collect all names of a variable<commit_after>package cspom

import java.io.IOException
import java.io.InputStream
import java.net.URI
import java.net.URL
import java.util.zip.GZIPInputStream
import scala.Iterator
import scala.collection.JavaConversions
import scala.util.parsing.combinator.JavaTokenParsers
import scala.util.parsing.input.CharSequenceReader
import org.apache.tools.bzip2.CBZip2InputStream
import cspom.dimacs.CNFParser
import cspom.extension.Relation
import cspom.extension.Table
import cspom.flatzinc.FlatZincParser
import cspom.variable.BoolVariable
import cspom.variable.CSPOMConstant
import cspom.variable.CSPOMExpression
import cspom.variable.CSPOMSeq
import cspom.variable.FreeVariable
import cspom.variable.IntVariable
import cspom.variable.SimpleExpression
import cspom.xcsp.XCSPParser
import cspom.variable.CSPOMVariable

object NameParser extends JavaTokenParsers {

  def parse: Parser[(String, Seq[Int])] = ident ~ rep("[" ~> wholeNumber <~ "]") ^^ {
    case n ~ i => (n, i.map(_.toInt))
  }
}

/**
 *
 * CSPOM is the central class of the Constraint Satisfaction Problem Object
 * Model. You can create a problem from scratch by instantiating this class and
 * then using the ctr and var methods. The CSPOM.load() method can be used in
 * order to create a CSPOM object from an XCSP file.
 * <p>
 * The CSPOM class adheres to the following definition :
 *
 * A CSP is defined as a pair (X, C), X being a finite set of variables, and C a
 * finite set of constraints.
 *
 * A domain is associated to each variable x in X. Each constraint involves a
 * finite set of variables (its scope) and defines a set of allowed and
 * forbidden instantiations of these variables.
 *
 * @author <NAME>
 * @see CSPOMConstraint
 * @see CSPOMVariable
 *
 */
class CSPOM {

  /**
   * Map used to easily retrieve a variable according to its name.
   */
  private var _namedExpressions = Map[String, CSPOMExpression[_]]()

  /**
   * @return The named expressions of this problem.
   */
  def namedExpressions = _namedExpressions

  def getExpressions = JavaConversions.asJavaCollection(namedExpressions)

  /**
   * @param variableName
   *            A variable name.
   * @return The variable with the corresponding name.
   */
  def expression(name: String): Option[CSPOMExpression[_]] = {
    NameParser.parse(new CharSequenceReader(name)).map(Some(_)).getOrElse(None).flatMap {
      case (n, s) => getInSeq(namedExpressions.get(n), s)
    }

  }

  def namesOf(e: CSPOMExpression[_]): Iterable[String] = {
    namedExpressions.filter(_._2 == e).map(_._1)
  }

  def variable(name: String): CSPOMVariable[_] = {
    expression(name).map {
      case v: CSPOMVariable[_] => v
      case e: CSPOMExpression[_] => throw new IllegalArgumentException(s"$e is not a variable")
    } getOrElse {
      throw new IllegalArgumentException(s"Could not find $name")
    }
  }

  private def getInSeq(e: Option[CSPOMExpression[_]], s: Seq[Int]): Option[CSPOMExpression[_]] = {
    if (s.isEmpty) {
      e
    } else {
      e.collect {
        case v: CSPOMSeq[_] => getInSeq(Some(v(s.head)), s.tail)
      } flatten
    }
  }

  /**
   * Collection of all constraints of the problem.
   */
  private val _constraints = collection.mutable.Set[CSPOMConstraint[_]]()

  def constraints = _constraints.iterator
{'fraction_non_alphanumeric': 0.10053722179585571, 'fraction_numerical': 0.000920951650038373, 'mean_word_length': 3.8446096654275093, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, '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': '500717', 'n_tokens_mistral': 2054, 'n_tokens_neox': 2019, 'n_words': 602}