text:
"""Helper/utility functions that a tf-transform implementation would find handy."""

import os
import re
from typing import Callable, Dict, List, Mapping, Optional, Tuple, Union

from absl import logging
import numpy as np
import pyarrow as pa

import tensorflow as tf
from tensorflow_transform import analyzer_nodes
from tensorflow_transform import annotators
from tensorflow_transform import common_types
from tensorflow_transform import graph_context
from tensorflow_transform import graph_tools
from tensorflow_transform import schema_inference
from tensorflow_transform import tf2_utils
from tensorflow_transform import tf_utils
from tensorflow_transform.output_wrapper import TFTransformOutput
from tensorflow_transform.saved import saved_transform_io
from tensorflow_transform.saved import saved_transform_io_v2
from tensorflow_transform.tf_metadata import dataset_metadata
from tensorflow_transform.tf_metadata import metadata_io
from tensorflow_transform.tf_metadata import schema_utils
from tfx_bsl.tfxio import tensor_to_arrow
# pylint: disable=g-direct-tensorflow-import
from tensorflow.python.eager import function
from tensorflow.python.framework import ops
# pylint: enable=g-direct-tensorflow-import
from tensorflow_metadata.proto.v0 import schema_pb2

_CompositeInstanceComponentType = np.ndarray
_CompositeComponentType = List[_CompositeInstanceComponentType]

_CACHED_EMPTY_ARRAY_BY_DTYPE = {}
_VALID_SCOPE_REGEX = re.compile('^[A-Za-z0-9]*$')
_INVALID_SCOPE_CHAR = re.compile('[^A-Za-z0-9_.\\-/>]')

METADATA_DIR_NAME = '.tft_metadata'

def _get_empty_array(dtype):
  if dtype not in _CACHED_EMPTY_ARRAY_BY_DTYPE:
    empty_array = np.array([], dtype)
    empty_array.setflags(write=False)
    _CACHED_EMPTY_ARRAY_BY_DTYPE[dtype] = empty_array
  return _CACHED_EMPTY_ARRAY_BY_DTYPE[dtype]

def batched_placeholders_from_specs(specs):
  """Returns placeholders for the given tf.TypeSpecs or feature specs.

  Args:
    specs: a Dict[Text, Union[tf.TypeSpec, FeatureSpec]]. Note that the values
      in this dict must be of the same type. Mixing is not allowed.

  Returns:
    A dictionary from strings to `Tensor`, `SparseTensor`s, or `RaggedTensor`s.

  Raises:
    ValueError: when the TypeSpec or feature spec has an unsupported dtype.
  """
  if not (all([_is_feature_spec(s) for s in specs.values()]) or
          all([isinstance(s, tf.TypeSpec) for s in specs.values()])):
    raise TypeError('Specs must be all tf.TypeSpecs or feature specs. '
                    'Mixing is not allowed. Got: {}'.format(specs))

  result = {}
  for name, spec in specs.items():
    if isinstance(spec, tf.RaggedTensorSpec):
      # TODO(b/159717195): clean up protected-access
      spec_dtype = spec._dtype  # pylint: disable=protected-access
    else:
      spec_dtype = spec.dtype
    if spec_dtype not in (tf.int64, tf.float32, tf.string):
      raise ValueError('Feature {} ({}, {}) had invalid dtype'.format(
          name, spec, type(spec)))
    if isinstance(spec, tf.TypeSpec):
      result[name] = _batched_placeholder_from_typespec(name, spec)
    else:
      result[name] = _batched_placeholder_from_feature_spec(name, spec)

  return result

def _is_feature_spec(spec):
  if isinstance(
      spec, (tf.io.VarLenFeature, tf.io.SparseFeature, tf.io.FixedLenFeature)):
    return True
  return common_types.is_ragged_feature(spec)

def _sanitize_scope_name(name):
  scope_name = _INVALID_SCOPE_CHAR.sub('_', name)
  if not _VALID_SCOPE_REGEX.match(scope_name):
    scope_name = 'F_{}'.format(scope_name)
  return scope_name

def _batched_placeholder_from_typespec(name, typespec):
  """Creates a batched placeholder from a tf.TypeSpec."""
  if isinstance(typespec,
                (tf.TensorSpec, tf.SparseTensorSpec, tf.RaggedTensorSpec)):
    sanitized_name = _sanitize_scope_name(name)
    with tf.name_scope(sanitized_name):
      return tf.nest.map_structure(
          lambda tspec: tf.raw_ops.Placeholder(  # pylint: disable=g-long-lambda
              dtype=tspec.dtype,
              shape=tspec.shape,
              name=sanitized_name),
          typespec,
          expand_composites=True)

  raise ValueError('Unsupported typespec: {}({}) for feature {}'.format(
      typespec, type(typespec), name))

def _batched_placeholder_from_feature_spec(name, feature_spec):
  """Creates a batched placeholder from a feature spec."""
  scope_name = _sanitize_scope_name(name)
  if isinstance(feature_spec, tf.io.FixedLenFeature):
    return tf.compat.v1.placeholder(
        feature_spec.dtype, [None] + feature_spec.shape, name=scope_name)
  elif isinstance(feature_spec, tf.io.VarLenFeature):
    return tf.compat.v1.sparse_placeholder(
        feature_spec.dtype, [None, None], name=scope_name)
  elif isinstance(feature_spec, tf.io.SparseFeature):
    shape = [None] + feature_spec.size if isinstance(
        feature_spec.size, list) else [None, feature_spec.size]
    return tf.compat.v1.sparse_placeholder(
        feature_spec.dtype, shape, name=scope_name)

  raise ValueError('Unsupported feature spec: {}({}) for feature {}'.format(
      feature_spec, type(feature_spec), name))

def _extract_sparse_components(
    sparse_value: common_types.SparseTensorValueType
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
  if isinstance(sparse_value, tf.SparseTensor):
    return (np.asarray(sparse_value.indices), np.asarray(sparse_value.values),
            np.asarray(sparse_value.dense_shape))
  elif isinstance(sparse_value, tf.compat.v1.SparseTensorValue):
    return sparse_value
  else:
    raise ValueError(
        'Expected SparseTensor or SparseTensorValue , but got {}'.format(
            sparse_value))

def _get_num_values_per_instance_in_sparse_batch(batch_indices: np.ndarray,
                                                 batch_size: int) -> List[int]:
  """Computes the number of values per instance of the batch."""
  result = [0] * batch_size
  for arr in batch_indices:
    result[arr[0]] += 1
  return result

def _decompose_sparse_batch(
    sparse_value: common_types.SparseTensorValueType
) -> Tuple[List[_CompositeComponentType], _CompositeComponentType]:
  """Decomposes a sparse batch into a list of sparse instances.

  Args:
    sparse_value: A `SparseTensor` or `SparseTensorValue` representing a batch
      of N sparse instances. The indices of the SparseTensorValue are expected
      to be sorted by row order.

  Returns:
    A tuple (instance_indices, instance_values) where the elements are lists
    of N ndarrays representing the indices and values, respectively, of the
    instances in the batch. The `instance_indices` include an ndarray per
    dimension.
  """
  batch_indices, batch_values, batch_shape = _extract_sparse_components(
      sparse_value)
  batch_size = batch_shape[0]
  instance_rank = len(batch_shape) - 1

  # Preallocate lists of length batch_size, initialized to empty ndarrays,
  # representing the indices and values of instances. We can reuse the return
  # value of _get_empty_array here because it is immutable.
  instance_values = [_get_empty_array(batch_values.dtype)] * batch_size
  instance_indices = [[_get_empty_array(batch_indices.dtype)] * instance_rank
                      for idx in range(batch_size)]

  values_per_instance = _get_num_values_per_instance_in_sparse_batch(
      batch_indices, batch_size)

  offset = 0
  for idx, num_values in enumerate(values_per_instance):
    if num_values < 1:
      continue
    instance_values[idx] = batch_values[offset:offset + num_values]
    for dim in range(instance_rank):
      # Skipping the first dimension since that is the batch dimension.
      instance_indices[idx][dim] = batch_indices[offset:offset + num_values,
                                                 dim + 1]
    offset += num_values
  return instance_indices, instance_values

def _decompose_varlen_batch(
    sparse_value: common_types.SparseTensorValueType
) -> Tuple[_CompositeComponentType, _CompositeComponentType]:
  """Decomposes a sparse batch into a list of sparse/varlen instances.

  Args:
    sparse_value: A `SparseTensor` or `SparseTensorValue` representing a batch
      of N sparse instances. The indices of the SparseTensorValue are expected
      to be sorted by row order.

  Returns:
    A tuple (instance_indices, instance_values) where the elements are lists
    of N ndarrays representing the indices and values, respectively, of the
    instances in the batch.

  Raises:
    ValueError: If `sparse_value` is neither `SparseTensor` nor
      `SparseTensorValue`.
    ValueError: If `sparse_value` contains out-of-order indices.
  """
  batch_indices, batch_values, batch_shape = _extract_sparse_components(
      sparse_value)
  batch_size = batch_shape[0]
  instance_rank = len(batch_shape) - 1

  # Preallocate lists of length batch_size, initialized to empty ndarrays,
  # representing the indices and values of instances. We can reuse the return
  # value of _get_empty_array here because it is immutable.
  instance_values = [_get_empty_array(batch_values.dtype)] * batch_size
  instance_indices = [_get_empty_array(batch_indices.dtype)] * batch_size

  # Iterate over the rows in the batch. At each row, consume all the elements
  # that belong to that row.
  current_offset = 0
  for current_row in range(batch_size):
    start_offset = current_offset

    # Scan forward until we reach an element that does not belong to the
    # current row.
    while current_offset < len(batch_indices):
      row = batch_indices[current_offset][0]
      if row == current_row:
        # This element belongs to the current row.
        current_offset += 1
      elif row > current_row:
        # We've reached the end of the current row.
        break
      else:
        raise ValueError('Encountered out-of-order sparse index: {}.'.format(
            batch_indices[current_offset]))

    if current_offset == start_offset:
      # If the current row is empty, leave the default value, which is an
      # empty array.
      pass
    else:
      instance_indices[current_row] = batch_indices[start_offset:current_offset,
                                                    1:]
      if instance_rank == 1:
        # In this case indices will have length 1, so for convenience we
        # reshape from [-1, 1] to [-1].
        current_row_indices = instance_indices[current_row]  # type: np.ndarray
        instance_indices[current_row] = current_row_indices.reshape([-1])
      instance_values[current_row] = batch_values[start_offset:current_offset]
  return instance_indices, instance_values

def _handle_varlen_batch(tensor_or_value: common_types.SparseTensorValueType,
                         name: str) -> _CompositeComponentType:
  """Decomposes a varlen tensor value into sparse tensor components."""
  instance_indices, instance_values = _decompose_varlen_batch(tensor_or_value)
  for indices in instance_indices:  # type: np.ndarray
    if len(indices.shape) > 1 or np.any(indices != np.arange(len(indices))):
      raise ValueError('Encountered a SparseTensorValue that cannot be '
                       'decoded by ListColumnRepresentation.\n'
                       '"{}" : {}'.format(name, tensor_or_value))
  return instance_values

def _handle_sparse_batch(
    tensor_or_value: common_types.SparseTensorValueType,
    spec: common_types.FeatureSpecType, name: str
) -> Dict[str, Union[List[_CompositeComponentType], _CompositeComponentType]]:
  """Decomposes a sparse tensor value into sparse tensor components."""
  if len(spec.index_key) == 1:
    index_keys = spec.index_key[0]
    instance_indices, instance_values = _decompose_varlen_batch(tensor_or_value)
  else:
    index_keys = spec.index_key
    instance_indices, instance_values = _decompose_sparse_batch(tensor_or_value)
  result = {}
  if isinstance(index_keys, list):
    assert isinstance(instance_indices, list)
    for key, indices in zip(index_keys, zip(*instance_indices)):
      result[key] = indices
  else:
    result[index_keys] = instance_indices
  result[spec.value_key] = instance_values
  _check_valid_sparse_tensor(instance_indices, instance_values, spec.size, name)
  return result

def _get_ragged_instance_component(
    component_batch: _CompositeInstanceComponentType, batch_splits: np.ndarray,
    instance_idx: int) -> _CompositeInstanceComponentType:
  """Extracts an instance component from a flat batch with given splits."""
  instance_begin = batch_splits[instance_idx]
  instance_end = batch_splits[instance_idx + 1]
  return component_batch[instance_begin:instance_end]

def _get_num_inner_uniform_elements(spec: common_types.RaggedFeature,
                                    name: str) -> int:
  """Extracts the number of elements in inner dimensions of a ragged feature.

  Also validates partitions in the feature spec.

  Args:
    spec: A ragged feature spec.
    name: Name of the feature that the spec belongs to.

  Returns:
    A number of elements in the inner uniform dimensions per one innermost
    ragged element. It is calculated as a product of inner uniform dimension
    sizes.

  Raises:
    NotImplementedError: if the spec contains partitions other than `RowLengths`
      and `UniformRowLengths` or there is a `RowLengths` partition that follows
      a `UniformRowLengths` partition.
  """
  result = 1
  uniform_partition = False
  for partition in spec.partitions:
    if isinstance(partition, tf.io.RaggedFeature.RowLengths):  # pytype: disable=attribute-error
      if uniform_partition:
        # Upstream and downstream logic only supports inner uniform dimensions.
        # This function will have to be extended to support other cases if
        # needed as well, so we fail with an explicit error for now.
        raise NotImplementedError(
            'Only inner partitions are allowed to be uniform, unsupported '
            'partition sequence for feature "{}" with spec {}'.format(
                name, spec))
    elif isinstance(partition, tf.io.RaggedFeature.UniformRowLength):  # pytype: disable=attribute-error
      uniform_partition = True
      result *= partition.length
    else:
      raise ValueError(
          'Only `RowLengths` and `UniformRowLengths` partitions of ragged '
          'features are supported, got {} for ragged feature "{}" with spec {}'
          .format(partition, name, spec))
  return result

def _handle_ragged_batch(tensor_or_value: common_types.RaggedTensorValueType,
                         spec: common_types.FeatureSpecType,
                         name: str) -> Dict[str, _CompositeComponentType]:
  """Decomposes a ragged tensor or value into ragged tensor components."""
  if isinstance(tensor_or_value, tf.RaggedTensor):
    nested_row_splits = tuple(
        x.numpy() for x in tensor_or_value.nested_row_splits)
    flat_values = np.ravel(tensor_or_value.flat_values.numpy())
  elif isinstance(tensor_or_value, tf.compat.v1.ragged.RaggedTensorValue):
    nested_row_splits = tensor_or_value.nested_row_splits
    flat_values = np.ravel(tensor_or_value.flat_values)
  else:
    raise ValueError('Expected RaggedTensor or RaggedTensorValue , but '
                     'got {}'.format(tensor_or_value))

  result = {}
  # The outermost row split represents batch dimension.
  batch_splits = nested_row_splits[0]
  batch_size = len(batch_splits) - 1
  inner_uniform_elements = _get_num_inner_uniform_elements(spec, name)

  # Iterate over all but batch dimension splits. Note that
  # `nested_row_splits[1:]` may be shorter than the list of partitions in the
  # presense of inner uniform partitions. Partition types and sequence is
  # validated in `_get_num_inner_uniform_elements`.
  for row_splits, partition in zip(nested_row_splits[1:], spec.partitions):
    assert isinstance(partition, tf.io.RaggedFeature.RowLengths), partition  # pytype: disable=attribute-error
    row_lengths = (row_splits[1:] - row_splits[:-1]) * inner_uniform_elements
    result[partition.key] = [
        _get_ragged_instance_component(row_lengths, batch_splits, idx)
        for idx in range(batch_size)
    ]

    # Translate batch split indices for the current dimension to the
    # next dimension.
    batch_splits = row_splits[batch_splits]

  # Split flat values according to the innermost dimension batch splits.
  result[spec.value_key] = [
      _get_ragged_instance_component(flat_values,
                                     batch_splits * inner_uniform_elements, idx)
      for idx in range(batch_size)
  ]
  return result

def to_instance_dicts(schema, fetches):
  """Converts fetches to the internal batch format.

  Maps the values fetched by `tf.Session.run` or returned by a tf.function to
  the internal batch format.

  Args:
    schema: A `Schema` proto.
    fetches: A dict representing a batch of data, either as returned by
      `Session.run` or eager tensors.

  Returns:
    A list of dicts where each dict is an in-memory representation of an
        instance.

  Raises:
    ValueError: If `schema` is invalid.
  """

  batch_dict = {}
  batch_sizes = {}
  feature_spec = schema_utils.schema_as_feature_spec(schema).feature_spec
  for name, tensor_or_value in fetches.items():
    spec = feature_spec[name]
    if isinstance(spec, tf.io.FixedLenFeature):
      value = np.asarray(tensor_or_value)
      batch_dict[name] = [value[i] for i in range(value.shape[0])]
      batch_sizes[name] = value.shape[0]

    elif isinstance(spec, tf.io.VarLenFeature):
      instance_values = _handle_varlen_batch(tensor_or_value, name)
      batch_dict[name] = instance_values
      batch_sizes[name] = len(instance_values)

    elif isinstance(spec, tf.io.SparseFeature):
      batch_dict_update = _handle_sparse_batch(tensor_or_value, spec, name)
      batch_dict.update(batch_dict_update)
      batch_sizes[name] = len(batch_dict_update[spec.value_key])

    elif common_types.is_ragged_feature(spec):
      batch_dict_update = _handle_ragged_batch(tensor_or_value, spec, name)
      batch_dict.update(batch_dict_update)
      batch_sizes[name] = len(batch_dict_update[spec.value_key])

    else:
      raise ValueError('Invalid feature spec {}.'.format(spec))

  # Check batch size is the same for each output.  Note this assumes that
  # fetches is not empty.
  batch_size = next(iter(batch_sizes.values()))
  for name, batch_size_for_name in batch_sizes.items():
    if batch_size_for_name != batch_size:
      raise ValueError(
          'Inconsistent batch sizes: "{}" had batch dimension {}, "{}" had'
          ' batch dimension {}'.format(name, batch_size_for_name,
                                       next(iter(batch_sizes.keys())),
                                       batch_size))

  # The following is the simplest way to convert batch_dict from a dict of
  # iterables to a list of dicts.  It does this by first extracting the values
  # of batch_dict, and reversing the order of iteration, then recombining with
  # the keys of batch_dict to create a dict.
  return [
      dict(zip(batch_dict, instance_values))
      for instance_values in zip(*batch_dict.values())
  ]

def _tf_dtype_to_arrow_type(dtype: tf.DType) -> pa.DataType:
  """Maps a tf data type to a pyarrow data type."""
  if dtype == tf.string:
    return pa.large_binary()
  elif dtype == tf.int64:
    return pa.int64()
  elif dtype == tf.float32:
    return pa.float32()
  else:
    raise TypeError('Unable to handle data type {}'.format(dtype))

def get_type_specs_from_feature_specs(
    feature_specs: Dict[str, common_types.FeatureSpecType]
) -> Dict[str, tf.TypeSpec]:
  """Returns `tf.TypeSpec`s for the given feature specs.

  Returns a dictionary of type_spec with the same type and shape as defined by
  `feature_specs`.

  Args:
    feature_specs: A TensorFlow feature spec.

  Returns:
    A dictionary from strings to `tf.TensorSpec`, `tf.SparseTensorSpec` or
    `tf.RaggedTensorSpec`s.

  Raises:
    ValueError: If the feature spec contains feature types not supported.
  """
  result = {}
  for name, feature_spec in feature_specs.items():
    if isinstance(feature_spec, tf.io.FixedLenFeature):
      result[name] = tf.TensorSpec([None] + list(feature_spec.shape),
                                   feature_spec.dtype)
    elif isinstance(feature_spec, tf.io.VarLenFeature):
      result[name] = tf.SparseTensorSpec([None, None], feature_spec.dtype)
    elif isinstance(feature_spec, tf.io.SparseFeature):
      # `TensorsToRecordBatchConverter` ignores `SparseFeature`s since arbitrary
      # `SparseTensor`s are not yet supported. They are handled in
      # `convert_to_arrow`.
      # TODO(b/181868576): Handle `SparseFeature`s by the converter once the
      # support is implemented.
      pass
    elif common_types.is_ragged_feature(feature_spec):
      # Number of dimensions is number of partitions + 1 + 1 batch dimension.
      shape = [None, None]
      ragged_rank = 1
      for partition in feature_spec.partitions:
        if isinstance(partition, tf.io.RaggedFeature.UniformRowLength):  # pytype: disable=attribute-error
          shape.append(partition.length)
        else:
          shape.append(None)
          ragged_rank += 1
      result[name] = tf.RaggedTensorSpec(
          shape=shape,
          dtype=feature_spec.dtype,
          ragged_rank=ragged_rank,
          row_splits_dtype=feature_spec.row_splits_dtype)
    else:
      raise ValueError('Invalid feature spec {}.'.format(feature_spec))
  return result

def make_tensor_to_arrow_converter(
    schema: schema_pb2.Schema) -> tensor_to_arrow.TensorsToRecordBatchConverter:
  """Constructs a `tf.Tensor` to `pa.RecordBatch` converter."""
  feature_specs = schema_utils.schema_as_feature_spec(schema).feature_spec
  type_specs = get_type_specs_from_feature_specs(feature_specs)
  return tensor_to_arrow.TensorsToRecordBatchConverter(type_specs)

def convert_to_arrow(
    schema: schema_pb2.Schema,
    converter: tensor_to_arrow.TensorsToRecordBatchConverter,
    fetches: Dict[str, common_types.TensorValueType]
) -> Tuple[List[pa.Array], pa.Schema]:
  """Converts fetches to a list of pyarrow arrays and schema.

  Maps the values fetched by `tf.Session.run` or returned by a tf.function to
  pyarrow format.

  Args:
    schema: A `Schema` proto.
    converter: A `tf.Tensor` to `pa.RecordBatch` converter that contains
      `tf.TypeSpec`s of `FixedLen` and `VarLen` features. Note that the
      converter doesn't support general `SparseFeature`s, they are handled here.
    fetches: A dict representing a batch of data, either as returned by
      `Session.run` or eager tensors.

  Returns:
    A tuple of a list of pyarrow arrays and schema representing fetches.

  Raises:
    ValueError: If batch sizes are inconsistent.
  """

  tensors = {}
  sparse_arrays = []
  sparse_fields = []
  feature_specs = schema_utils.schema_as_feature_spec(schema).feature_spec
  for name, tensor_or_value in fetches.items():
    feature_spec = feature_specs[name]
    if isinstance(feature_spec, tf.io.SparseFeature):
      sparse_components = _handle_sparse_batch(tensor_or_value, feature_spec,
                                               name)
      values_type = pa.large_list(_tf_dtype_to_arrow_type(feature_spec.dtype))
      indices_type = pa.large_list(pa.int64())
      sparse_arrays.append(
          pa.array(
              sparse_components.pop(feature_spec.value_key), type=values_type))
      sparse_fields.append(pa.field(feature_spec.value_key, values_type))
      for indices_key, instance_indices in sparse_components.items():
        flat_indices = [np.ravel(indices) for indices in instance_indices]
        sparse_arrays.append(pa.array(flat_indices, type=indices_type))
        sparse_fields.append(pa.field(indices_key, indices_type))
    else:
      tensors[name] = tensor_or_value
  record_batch = converter.convert(tensors)
  arrow_schema = record_batch.schema
  for field in sparse_fields:
    arrow_schema = arrow_schema.append(field)

  return record_batch.columns + sparse_arrays, arrow_schema

# TODO(b/36040669): Consider moving this to where it can be shared with coders.
def _check_valid_sparse_tensor(indices: Union[_CompositeComponentType,
                                              List[_CompositeComponentType]],
                               values: _CompositeComponentType,
                               size: Union[int, List[int]], name: str):
  """Validates sparse tensor components."""
  # Check that all indices are in range.
  for current_indices in indices:
    if isinstance(current_indices, np.ndarray):
      current_indices = [current_indices]
    for dim, indices_array in enumerate(current_indices):
      if indices_array.size and size[dim] >= 0:
        i_min, i_max = min(indices_array), max(indices_array)
        if i_min < 0 or i_max >= size[dim]:
          i_bad = i_min if i_min < 0 else i_max
          raise ValueError(
              'Sparse column {} has index {} out of range [0, {})'.format(
                  name, i_bad, size[dim]))

  if len(indices) != len(values):
    raise ValueError(
        'Sparse column {} has indices and values of different lengths: '
        'values: {}, indices: {}'.format(name, values, indices))

# TODO(b/149997088): Split into two APIs one that will just trace the
# `preprocessing_fn` using tf.function as is and another that will return
# specific outputs requested for.
def get_traced_transform_fn(
    preprocessing_fn: Callable[[Mapping[str, common_types.TensorType]],
                               Mapping[str, common_types.TensorType]],
    input_signature: Mapping[str, tf.TypeSpec],
    tf_graph_context: graph_context.TFGraphContext,
    output_keys_to_name_map: Optional[Dict[str,
                                           str]] = None) -> function.Function:
  """Get preprocessing_fn traced using tf.function.

  Args:
    preprocessing_fn: A user defined python function to be traced.
    input_signature: `tf.TypeSpec`s describing the inputs to the
      `preprocessing_fn`.
    tf_graph_context: A `TFGraphContext` context manager to invoke the
      `preprocessing_fn` in.
    output_keys_to_name_map: (Optional) A map from output dictionary keys to the
      names of the tensors that they represent.

  Returns:
    A tf.function object representing a function with the same input signature
    as `preprocessing_fn`.
    If `output_keys_to_name_map` is None or there are no more TFT analyzers to
    evaluate in the `preprocessing_fn`, the output signature of this
    tf.function
    is the same as the `preprocessing_fn`.
    Otherwise, its output signature contains the keys in
    `output_keys_to_name_map` and the tensor represented by the corresponding
    dictionary values.
  """

  assert all([isinstance(s, tf.TypeSpec) for s in input_signature.values()])

  # TODO(b/177672051): Investigate performance impact of enabling autograph.
  @tf.function(input_signature=[input_signature], autograph=False)
  def transform_fn(inputs):
    graph = ops.get_default_graph()
    # If any analyzers have already been evaluated, pass them using the
    # `graph_context.TFGraphContext`. This will be used in place of the analyzer
    # nodes.
    # The user defined `preprocessing_fn` may directly modify its inputs which
    # is not allowed in a tf.function. Hence, we make a copy here.
    inputs_copy = tf_utils.copy_tensors(inputs)
    with tf_graph_context:
      transformed_features = preprocessing_fn(inputs_copy)
    # An empty `TENSOR_REPLACEMENTS` collection symbolizes that there is no
    # analyzer left for Transform to evaluate. Either if this collection is
    # empty or if no specific outputs have been requested, return
    # the same output as `preprocessing_fn` (i.e, transformed_features).
    if (output_keys_to_name_map is None or
        not graph.get_collection(analyzer_nodes.TENSOR_REPLACEMENTS)):
      return transformed_features
    else:
      return {
          key: graph.get_tensor_by_name(value)
          for key, value in output_keys_to_name_map.items()
      }

  return transform_fn

def _trace_preprocessing_fn_v1(preprocessing_fn, specs):
  """Trace TF1 graph for `preprocessing_fn`."""
  with tf.compat.v1.Graph().as_default() as graph:
    with tf.compat.v1.name_scope('inputs'):
      structured_inputs = batched_placeholders_from_specs(specs)
      # In order to avoid a bug where import_graph_def fails when the
      # input_map and return_elements of an imported graph are the same
      # (b/34288791), we avoid using the placeholder of an input column as an
      # output of a graph. We do this by applying tf.identity to all inputs of
      # the preprocessing_fn.  Note this applies at the level of raw tensors.
      # TODO(b/34288791): Remove this workaround and use a shallow copy of
      # inputs instead.  A shallow copy is needed in case
      # self._preprocessing_fn mutates its input.
      copied_inputs = tf_utils.copy_tensors(structured_inputs)

    structured_outputs = preprocessing_fn(copied_inputs)
  return graph, structured_inputs, structured_outputs

def _trace_preprocessing_fn_v2(preprocessing_fn, specs, base_temp_dir):
  """Trace TF2 graph for `preprocessing_fn`."""
  tf_graph_context = graph_context.TFGraphContext(
      module_to_export=tf.Module(),
      temp_dir=base_temp_dir,
      evaluated_replacements=None)
  with annotators.object_tracker_scope(annotators.ObjectTracker()):
    concrete_fn = get_traced_transform_fn(
        preprocessing_fn, specs, tf_graph_context).get_concrete_function()
  return (concrete_fn.graph,
          tf2_utils.get_structured_inputs_from_func_graph(concrete_fn.graph),
          concrete_fn.structured_outputs)

def trace_preprocessing_function(preprocessing_fn,
                                 input_specs,
                                 use_tf_compat_v1,
                                 base_temp_dir=None):
  """Trace graph for `preprocessing_fn`.

  Args:
    preprocessing_fn: A user defined python function to be traced.
    input_specs: A dictionary from input feature name to its FeatureSpec or
      TypeSpec. If use_tf_compat_v1 is `False`, input_specs must be a dictionary
      of TypeSpecs.
    use_tf_compat_v1: (Optional) If `True`, the `preprocessing_fn` is traced as
      a TF 1.x graph. Else, it is traced using tf.function.
    base_temp_dir: (Optional) Base path to write any dummy assets to during
      tracing. Required when `use_tf_compat_v1` is `False`.

  Returns:
    A tuple of:

      0. the graph representing the traced `preprocessing_fn`
      1. the graph's structured inputs
      2. the graph's structured outputs

  """
  if use_tf_compat_v1:
    return _trace_preprocessing_fn_v1(preprocessing_fn, input_specs)
  else:
    return _trace_preprocessing_fn_v2(preprocessing_fn, input_specs,
                                      base_temp_dir)

def _trace_and_write_transform_fn(
    saved_model_dir: str,
    preprocessing_fn: Callable[[Mapping[str, common_types.TensorType]],
                               Mapping[str, common_types.TensorType]],
    input_signature: Mapping[str, tf.TypeSpec], base_temp_dir: Optional[str],
    tensor_replacement_map: Optional[Dict[str, tf.Tensor]],
    output_keys_to_name_map: Optional[Dict[str,
                                           str]]) -> function.ConcreteFunction:
  """Trace `preprocessing_fn` and serialize to a SavedModel."""
  tf_graph_context = graph_context.TFGraphContext(
      module_to_export=tf.Module(),
      temp_dir=base_temp_dir,
      evaluated_replacements=tensor_replacement_map)
  transform_fn = get_traced_transform_fn(
      preprocessing_fn,
      input_signature,
      tf_graph_context,
      output_keys_to_name_map=output_keys_to_name_map)
  return saved_transform_io_v2.write_v2_saved_model(
      tf_graph_context.module_to_export, transform_fn, 'transform_fn',
      saved_model_dir)

def _trace_and_get_metadata(
    concrete_transform_fn: function.ConcreteFunction,
    structured_inputs: Mapping[str, common_types.TensorType],
    preprocessing_fn: Callable[[Mapping[str, common_types.TensorType]],
                               Mapping[str, common_types.TensorType]],
    base_temp_dir: Optional[str],
    tensor_replacement_map: Optional[Dict[str, tf.Tensor]]
) -> dataset_metadata.DatasetMetadata:
  """Compute and return metadata for the outputs of `concrete_transform_fn`."""
  tf_graph_context = graph_context.TFGraphContext(
      module_to_export=tf.Module(),
      temp_dir=base_temp_dir,
      evaluated_replacements=tensor_replacement_map)
  concrete_metadata_fn = schema_inference.get_traced_metadata_fn(
      preprocessing_fn,
      structured_inputs,
      tf_graph_context,
      evaluate_schema_overrides=True)
  return dataset_metadata.DatasetMetadata(
      schema=schema_inference.infer_feature_schema_v2(
          concrete_transform_fn.structured_outputs,
          concrete_metadata_fn,
          evaluate_schema_overrides=True))

def _validate_analyzers_fingerprint(
    baseline_analyzers_fingerprint: Mapping[str,
                                            graph_tools.AnalyzersFingerprint],
    graph: tf.Graph, structured_inputs: Mapping[str, common_types.TensorType]):
  """Validates analyzers fingerprint in `graph` is same as baseline."""
  analyzers_fingerprint = graph_tools.get_analyzers_fingerprint(
      graph, structured_inputs)
  error_msg = (
      'The order of analyzers in your `preprocessing_fn` appears to be '
      'non-deterministic. This can be fixed either by changing your '
      '`preprocessing_fn` such that tf.Transform analyzers are encountered '
      'in a deterministic order or by passing a unique name to each '
      'analyzer API call.')
  for analyzer in analyzers_fingerprint:
    if analyzer not in baseline_analyzers_fingerprint:
      prefix_msg = (f'Analyzer node ({analyzer}) not found in '
                    f'{baseline_analyzers_fingerprint.keys()}. ')
      raise RuntimeError(prefix_msg + error_msg)
    if (baseline_analyzers_fingerprint[analyzer].source_keys !=
        analyzers_fingerprint[analyzer].source_keys):
      raise RuntimeError(error_msg)

    if (baseline_analyzers_fingerprint[analyzer].unique_path_hash !=
        analyzers_fingerprint[analyzer].unique_path_hash):
      logging.warning(
          'Analyzer (%s) node\'s cache key varies on repeated tracing.'
          ' This warning is safe to ignore if you either specify `name` for all'
          ' analyzers or if the order in which they are invoked is'
          ' deterministic. If not, please file a bug with details.', analyzer)

def trace_and_write_v2_saved_model(
    saved_model_dir: str,
    preprocessing_fn: Callable[[Mapping[str, common_types.TensorType]],
                               Mapping[str, common_types.TensorType]],
    input_signature: Mapping[str, tf.TypeSpec], base_temp_dir: Optional[str],
    baseline_analyzers_fingerprint: Mapping[str,
                                            graph_tools.AnalyzersFingerprint],
    tensor_replacement_map: Optional[Dict[str, tf.Tensor]],
    output_keys_to_name_map: Optional[Dict[str, str]]):
  """Writes out a SavedModelV2 with preprocessing_fn traced using tf.function.

  The SavedModel written contains a method called `transform_fn` that
  represents the traced `preprocessing_fn`. Additionally, if this is the final
  SavedModel being written out, it will contain a method called `metadata_fn`
  that provides deferred schema annotations.

  Args:
    saved_model_dir: Path to write SavedModel to.
    preprocessing_fn: A user defined python function to be traced.
    input_signature: TypeSpecs describing the inputs to the `preprocessing_fn`.
    base_temp_dir: Base path to write temporary artifacts to.
    baseline_analyzers_fingerprint: A mapping from analyzer name to a set of
      paths that define its fingerprint.
    tensor_replacement_map: A map from placeholder tensor names to their
      evaluated replacement tensors.
    output_keys_to_name_map: A map from output dictionary keys to the names of
      the tensors that they represent.

  Returns:
    A tuple containing a pair of `tf.ConcreteFunction`s:
      1. The traced preprocessing_fn.
      2. A metadata_fn that returns a dictionary containing the deferred
      annotations added to the graph when invoked with any valid input.

  Raises:
    RuntimeError: if analyzers in `preprocessing_fn` are encountered in a
    non-deterministic order.
  """
  concrete_transform_fn = _trace_and_write_transform_fn(
      saved_model_dir, preprocessing_fn, input_signature, base_temp_dir,
      tensor_replacement_map, output_keys_to_name_map)
  structured_inputs = tf2_utils.get_structured_inputs_from_func_graph(
      concrete_transform_fn.graph)
  _validate_analyzers_fingerprint(baseline_analyzers_fingerprint,
                                  concrete_transform_fn.graph,
                                  structured_inputs)

  # If the `TENSOR_REPLACEMENTS` graph collection is empty, all TFT analyzers
  # in the `preprocessing_fn` have already been evaluated.
  if not concrete_transform_fn.graph.get_collection(
      analyzer_nodes.TENSOR_REPLACEMENTS):
    metadata = _trace_and_get_metadata(concrete_transform_fn, structured_inputs,
                                       preprocessing_fn, base_temp_dir,
                                       tensor_replacement_map)
    metadata_io.write_metadata(metadata,
                               os.path.join(saved_model_dir, METADATA_DIR_NAME))

def _assert_no_analyzers_in_graph(graph):
  if graph.get_collection(analyzer_nodes.TENSOR_REPLACEMENTS):
    raise RuntimeError('TFT analyzers found when tracing the given '
                       '`preprocessing_fn`. Please use '
                       '`tft.beam.AnalyzeDataset` to analyze this function.')

def analyze_in_place(preprocessing_fn, force_tf_compat_v1, feature_specs,
                     type_specs, transform_output_path):
  """Analyzes the `preprocessing_fn` in-place without looking at the data.

  This should only be used if the `preprocessing_fn` contains no TFT
  analyzers or TFT mappers that use analyzers.

  Writes out a transform function and transformed metadata to subdirs under
  `transform_output_path`.

  Args:
    preprocessing_fn: The tf.Transform preprocessing_fn.
    force_tf_compat_v1: If True, call Transform's API to use Tensorflow in
      tf.compat.v1 mode.
    feature_specs: a Dict from input feature key to its feature spec.
    type_specs: a Dict from input feature key to its type spec.
    transform_output_path: An absolute path to write the output to.

  Raises:
    RuntimeError if `preprocessing_fn` contains TFT analyzers.
  """
  use_tf_compat_v1 = tf2_utils.use_tf_compat_v1(force_tf_compat_v1)
  transform_fn_path = os.path.join(transform_output_path,
                                   TFTransformOutput.TRANSFORM_FN_DIR)
  if use_tf_compat_v1:
    graph, structured_inputs, structured_outputs = (
        trace_preprocessing_function(
            preprocessing_fn, feature_specs, use_tf_compat_v1=use_tf_compat_v1))
    _assert_no_analyzers_in_graph(graph)
    with tf.compat.v1.Session(graph=graph) as sess:
      sess.run(tf.compat.v1.global_variables_initializer())
      sess.run(tf.compat.v1.tables_initializer())
      saved_transform_io.write_saved_transform_from_session(
          sess, structured_inputs, structured_outputs, transform_fn_path)

      transformed_metadata = dataset_metadata.DatasetMetadata(
          schema=schema_inference.infer_feature_schema(structured_outputs,
                                                       graph, sess))
  else:
    concrete_transform_fn = _trace_and_write_transform_fn(
        saved_model_dir=transform_fn_path,
        preprocessing_fn=preprocessing_fn,
        input_signature=type_specs,
        base_temp_dir=None,
        tensor_replacement_map=None,
        output_keys_to_name_map=None)
    _assert_no_analyzers_in_graph(concrete_transform_fn.graph)
    structured_inputs = tf2_utils.get_structured_inputs_from_func_graph(
        concrete_transform_fn.graph)
    transformed_metadata = _trace_and_get_metadata(
        concrete_transform_fn=concrete_transform_fn,
        structured_inputs=structured_inputs,
        preprocessing_fn=preprocessing_fn,
        base_temp_dir=None,
        tensor_replacement_map=None)
  transformed_metadata_dir = os.path.join(
      transform_output_path, TFTransformOutput.TRANSFORMED_METADATA_DIR)
  metadata_io.write_metadata(transformed_metadata, transformed_metadata_dir)


text:
package org.datasyslab.geospark.rangeJudgement;

import java.io.Serializable;

import org.apache.spark.api.java.function.Function;

import com.vividsolutions.jts.geom.Envelope;

// TODO: Auto-generated Javadoc
/**
 * The Class RectangleRangeFilter.
 */
public class RectangleRangeFilter implements Function<Object,Boolean>,Serializable
{

	/** The condition. */
	Integer condition=0;

	/** The query window. */
	Object queryWindow;

	/**
	 * Instantiates a new rectangle range filter.
	 *
	 * @param queryWindow the query window
	 * @param condition the condition
	 */
	public RectangleRangeFilter(Envelope queryWindow,Integer condition)
	{
		this.condition=condition;
		this.queryWindow=queryWindow;
	}

	/* (non-Javadoc)
	 * @see org.apache.spark.api.java.function.Function#call(java.lang.Object)
	 */
	public Boolean call(Object tuple) throws Exception {
			if(condition==0)
			{
				if(((Envelope)queryWindow).contains((Envelope) tuple))
				{
					return true;
				}
				else return false;
			}
			else
			{
				if(((Envelope)queryWindow).intersects(((Envelope) tuple)))
				{
					return true;
				}
				else return false;
			}
	}
}

redpajama_set_name: RedPajamaGithub


text:
<!DOCTYPE html>

<meta charset="utf-8">
<title>Shadow DOM Test: Basic reprojection (reference)</title>
<link rel="author" title="Anna Ogawa" href="mailto:anna.ogawa.0219@gmail.com">
<link rel="author" title="Hayato Ito" href="mailto:hayato@google.com">

You should see green text saying "Apple" and "Orange" below.

Hello a Shadow Root2.

      Hello a Shadow Root.

Apple.

Orange.

Banana.


text:
package com.intellij.openapi.externalSystem.service.project.manage;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.externalSystem.model.DataNode;
import com.intellij.openapi.externalSystem.model.Key;
import com.intellij.openapi.externalSystem.model.ProjectKeys;
import com.intellij.openapi.externalSystem.model.project.*;
import com.intellij.openapi.externalSystem.service.project.PlatformFacade;
import com.intellij.openapi.externalSystem.util.DisposeAwareProjectChange;
import com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil;
import com.intellij.openapi.externalSystem.util.ExternalSystemConstants;
import com.intellij.openapi.externalSystem.util.Order;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.LibraryOrderEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.OrderEntry;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.impl.ModuleLibraryOrderEntryImpl;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.containers.ContainerUtilRt;
import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import static com.intellij.openapi.externalSystem.model.ProjectKeys.MODULE;

/**
 * @author Denis Zhdanov
 * @since 4/12/13 6:19 PM
 */
@Order(ExternalSystemConstants.BUILTIN_SERVICE_ORDER)
public class LibraryDependencyDataService extends AbstractDependencyDataService<LibraryDependencyData, LibraryOrderEntry> {

  private static final Logger LOG = Logger.getInstance("#" + LibraryDependencyDataService.class.getName());

  @NotNull private final LibraryDataService myLibraryManager;

  public LibraryDependencyDataService(@NotNull LibraryDataService libraryManager) {
    myLibraryManager = libraryManager;
  }

  @NotNull
  @Override
  public Key<LibraryDependencyData> getTargetDataKey() {
    return ProjectKeys.LIBRARY_DEPENDENCY;
  }

  @Override
  public void importData(@NotNull Collection<DataNode<LibraryDependencyData>> toImport,
                         @Nullable ProjectData projectData,
                         @NotNull Project project,
                         @NotNull PlatformFacade platformFacade,
                         boolean synchronous) {
    if (toImport.isEmpty()) {
      return;
    }

    MultiMap<DataNode<ModuleData>, DataNode<LibraryDependencyData>> byModule = ExternalSystemApiUtil.groupBy(toImport, MODULE);
    for (Map.Entry<DataNode<ModuleData>, Collection<DataNode<LibraryDependencyData>>> entry : byModule.entrySet()) {
      Module module = platformFacade.findIdeModule(entry.getKey().getData(), project);
      if (module == null) {
        LOG.warn(String.format(
          "Can't import library dependencies %s. Reason: target module (%s) is not found at the ide and can't be imported",
          entry.getValue(), entry.getKey()
        ));
        continue;
      }
      importData(entry.getValue(), module, platformFacade, synchronous);
    }
  }

  @NotNull
  @Override
  public Class<LibraryOrderEntry> getOrderEntryType() {
    return LibraryOrderEntry.class;
  }

  @Override
  protected String getOrderEntryName(@NotNull LibraryOrderEntry orderEntry) {
    return orderEntry.getLibraryName();
  }

  private void importData(@NotNull final Collection<DataNode<LibraryDependencyData>> nodesToImport,
                         @NotNull final Module module,
                         @NotNull final PlatformFacade platformFacade,
                          final boolean synchronous) {
    ExternalSystemApiUtil.executeProjectChangeAction(synchronous, new DisposeAwareProjectChange(module) {
      @Override
      public void execute() {
        // The general idea is to import all external project library dependencies and module libraries which don't present at the
        // ide side yet and remove all project library dependencies and module libraries which present at the ide but not at
        // the given collection.
        // The trick is that we should perform module settings modification inside try/finally block against target root model.
        // That means that we need to prepare all necessary data, obtain a model and modify it as necessary.
        Map<Set<String>/* library paths */, LibraryDependencyData> moduleLibrariesToImport = ContainerUtilRt.newHashMap();
        Map<String/* library name + scope */, LibraryDependencyData> projectLibrariesToImport = ContainerUtilRt.newHashMap();
        Set<LibraryDependencyData> toImport = ContainerUtilRt.newLinkedHashSet();

        boolean hasUnresolved = false;
        for (DataNode<LibraryDependencyData> dependencyNode : nodesToImport) {
          LibraryDependencyData dependencyData = dependencyNode.getData();
          LibraryData libraryData = dependencyData.getTarget();
          hasUnresolved |= libraryData.isUnresolved();
          switch (dependencyData.getLevel()) {
            case MODULE:
              if (!libraryData.isUnresolved()) {
                Set<String> paths = ContainerUtilRt.newHashSet();
                for (String path : libraryData.getPaths(LibraryPathType.BINARY)) {
                  paths.add(ExternalSystemApiUtil.toCanonicalPath(path) + dependencyData.getScope().name());
                }
                moduleLibrariesToImport.put(paths, dependencyData);
                toImport.add(dependencyData);
              }
              break;
            case PROJECT:
              projectLibrariesToImport.put(libraryData.getInternalName() + dependencyData.getScope().name(), dependencyData);
              toImport.add(dependencyData);
          }
        }

        final ModifiableRootModel moduleRootModel = platformFacade.getModuleModifiableModel(module);
        LibraryTable moduleLibraryTable = moduleRootModel.getModuleLibraryTable();
        LibraryTable libraryTable = platformFacade.getProjectLibraryTable(module.getProject());
        try {
          syncExistingAndRemoveObsolete(moduleLibrariesToImport, projectLibrariesToImport, toImport, moduleRootModel, hasUnresolved);

          // Import missing library dependencies.
          if (!toImport.isEmpty()) {
            importMissing(toImport, moduleRootModel, moduleLibraryTable, libraryTable, module);
          }
        }
        finally {
          moduleRootModel.commit();
        }
      }
    });
  }

  private void importMissing(@NotNull Set<LibraryDependencyData> toImport,
                             @NotNull ModifiableRootModel moduleRootModel,
                             @NotNull LibraryTable moduleLibraryTable,
                             @NotNull LibraryTable libraryTable,
                             @NotNull Module module) {
    for (final LibraryDependencyData dependencyData : toImport) {
      final LibraryData libraryData = dependencyData.getTarget();
      final String libraryName = libraryData.getInternalName();
      switch (dependencyData.getLevel()) {
        case MODULE:
          final Library moduleLib = moduleLibraryTable.createLibrary(libraryName);
          syncExistingLibraryDependency(dependencyData, moduleLib, moduleRootModel, module);
          break;
        case PROJECT:
          final Library projectLib = libraryTable.getLibraryByName(libraryName);
          if (projectLib == null) {
            syncExistingLibraryDependency(dependencyData, moduleLibraryTable.createLibrary(libraryName), moduleRootModel, module);
            break;
          }
          LibraryOrderEntry orderEntry = moduleRootModel.addLibraryEntry(projectLib);
          setLibraryScope(orderEntry, projectLib, module, dependencyData);
      }
    }
  }

  private static void setLibraryScope(@NotNull LibraryOrderEntry orderEntry,
                                      @NotNull Library lib,
                                      @NotNull Module module,
                                      @NotNull LibraryDependencyData dependencyData) {
    LOG.debug(String.format("Adding library dependency '%s' to module '%s'", lib.getName(), module.getName()));
    orderEntry.setExported(dependencyData.isExported());
    orderEntry.setScope(dependencyData.getScope());
    LOG.debug(String.format(
      "Configuring library dependency '%s' of module '%s' to be%s exported and have scope %s",
      lib.getName(), module.getName(), dependencyData.isExported() ? " not" : "", dependencyData.getScope()
    ));
  }

  private void syncExistingAndRemoveObsolete(@NotNull Map<Set<String>, LibraryDependencyData> moduleLibrariesToImport,
                                             @NotNull Map<String, LibraryDependencyData> projectLibrariesToImport,
                                             @NotNull Set<LibraryDependencyData> toImport,
                                             @NotNull ModifiableRootModel moduleRootModel,
                                             boolean hasUnresolvedLibraries) {
    Set<String> moduleLibraryKey = ContainerUtilRt.newHashSet();
    for (OrderEntry entry : moduleRootModel.getOrderEntries()) {
      if (entry instanceof ModuleLibraryOrderEntryImpl) {
        ModuleLibraryOrderEntryImpl moduleLibraryOrderEntry = (ModuleLibraryOrderEntryImpl)entry;
        Library library = moduleLibraryOrderEntry.getLibrary();
        if (library == null) {
          LOG.warn("Skipping module-level library entry because it doesn't have backing Library object. Entry: " + entry);
          continue;
        }
        moduleLibraryKey.clear();
        for (VirtualFile file : library.getFiles(OrderRootType.CLASSES)) {
          moduleLibraryKey.add(ExternalSystemApiUtil.getLocalFileSystemPath(file) + moduleLibraryOrderEntry.getScope().name());
        }
        LibraryDependencyData existing = moduleLibrariesToImport.remove(moduleLibraryKey);
        if (existing == null) {
          moduleRootModel.removeOrderEntry(entry);
        }
        else {
          syncExistingLibraryDependency(existing, library, moduleRootModel, moduleLibraryOrderEntry.getOwnerModule());
          toImport.remove(existing);
        }
      }
      else if (entry instanceof LibraryOrderEntry) {
        final LibraryOrderEntry libraryOrderEntry = (LibraryOrderEntry)entry;
        final String libraryName = libraryOrderEntry.getLibraryName();
        final LibraryDependencyData existing = projectLibrariesToImport.remove(libraryName + libraryOrderEntry.getScope().name());
        if (existing != null) {
          toImport.remove(existing);
        }
        else if (!hasUnresolvedLibraries) {
          // There is a possible case that a project has been successfully imported from external model and after
          // that network/repo goes down. We don't want to drop existing binary mappings then.
          moduleRootModel.removeOrderEntry(entry);
        }
      }
    }
  }

  private void syncExistingLibraryDependency(@NotNull LibraryDependencyData libraryDependencyData,
                                             @NotNull Library library,
                                             @NotNull ModifiableRootModel moduleRootModel,
                                             @NotNull Module module) {
    final Library.ModifiableModel libModel = library.getModifiableModel();
    try {
      final String libraryName = libraryDependencyData.getInternalName();
      Map<OrderRootType, Collection<File>> files = myLibraryManager.prepareLibraryFiles(libraryDependencyData.getTarget());
      myLibraryManager.registerPaths(files, libModel, libraryName);
      LibraryOrderEntry orderEntry = moduleRootModel.findLibraryOrderEntry(library);
      assert orderEntry != null;
      setLibraryScope(orderEntry, library, module, libraryDependencyData);
    }
    finally {
      libModel.commit();
    }
  }
}


text:
<?php

namespace TDN\NanaBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\DefaultCsrfProvider as CsrfProvider;

use TDN\NanaBundle\Form\Type\shortRegisterType;

class letterInscriptionType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('user', new letterRegisterType());
        $builder->add('termsAccepted', 'checkbox',
            array(
                'label' => 'J'accepte',
            ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {

        $resolver->setDefaults(array(
            'data_class' => 'TDN\NanaBundle\Form\Model\Inscription',
            'csrf_protection' => true,
            'csrf_field_name' => '_token',
            'intention'       => 'register_check'
        ));
    }

    public function getName()
    {
        return 'marylin_inscription_lettre_nana';
    }
}


text:
title: PapljLogicalOrExprImpl - aesi-intellij
---

[aesi-intellij](../../index.html) / [org.metaborg.paplj.psi.impl](../index.html) / [PapljLogicalOrExprImpl](.)

# PapljLogicalOrExprImpl

`open class PapljLogicalOrExprImpl : `[`PapljExprImpl`](../-paplj-expr-impl/index.html)`, `[`PapljLogicalOrExpr`](../../org.metaborg.paplj.psi/-paplj-logical-or-expr/index.html)

### Constructors

| [<init>](-init-.html) | `PapljLogicalOrExprImpl(node: ASTNode)` |

### Functions

| [accept](accept.html) | `open fun accept(visitor: `[`PapljVisitor`](../../org.metaborg.paplj.psi/-paplj-visitor/index.html)`): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)<br>`open fun accept(visitor: PsiElementVisitor): `[`Unit`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) |
| [getExprList](get-expr-list.html) | `open fun getExprList(): `[`MutableList`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html)`<`[`PapljExpr`](../../org.metaborg.paplj.psi/-paplj-expr/index.html)`>` |
| [getOror](get-oror.html) | `open fun getOror(): PsiElement` |

### Inherited Functions

| [getBraceL](../-paplj-expr-impl/get-brace-l.html) | `open fun getBraceL(): PsiElement?` |
| [getBraceR](../-paplj-expr-impl/get-brace-r.html) | `open fun getBraceR(): PsiElement?` |
| [getExpr](../-paplj-expr-impl/get-expr.html) | `open fun getExpr(): `[`PapljExpr`](../../org.metaborg.paplj.psi/-paplj-expr/index.html)`?` |


text:
<title>Zend Framework Extras API Documentation</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta><link rel="stylesheet" href="css/black-tie/jquery-ui-1.8.2.custom.css" type="text/css"></link><link rel="stylesheet" href="css/jquery.treeview.css" type="text/css"></link><link rel="stylesheet" href="css/theme.css" type="text/css"></link>

API Documentation

JQuery/View/Helper/JQuery.php

Includes Classes

category

ZendX

copyright

Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)

license

http://framework.zend.com/license/new-bsd     New BSD License

package

ZendX_JQuery

subpackage

View

version

$Id: JQuery.php 20184 2010-01-10 21:22:54Z freak $

Classes

ZendX_JQuery_View_Helper_JQuery

Description

Zend Framework

LICENSE

This source file is subject to the new BSD license that is bundled
with this package in the file LICENSE.txt.
It is also available through the world-wide-web at this URL:
http://framework.zend.com/license/new-bsd
If you did not receive a copy of the license and are unable to
obtain it through the world-wide-web, please send an email
to license@zend.com so we can send you a copy immediately.

\ZendX_JQuery_View_Helper_JQuery
jump to top

Extends from

\Zend_View_Helper_Abstract

copyright

Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)

license

http://framework.zend.com/license/new-bsd     New BSD License

package

ZendX_JQuery

subpackage

View

uses

Zend_Json

                Properties

$noConflictMode

$view

                Methods

__call

__construct

disableNoConflictMode

enableNoConflictMode

getJQueryHandler

getNoConflictMode

jQuery

setView

Description

jQuery Helper. Functions as a stack for code and loads all jQuery dependencies.

Properties

$noConflictMode
jump to class

 $noConflictMode = 'false'
jQuery no Conflict Mode

 Details

visibility

private

default

false

final

false

static

true

see

http://docs.jquery.com/Using_jQuery_with_Other_Libraries

staticvar

Boolean Status of noConflict Mode

$view
jump to class

\Zend_View_Interface $view = ''

 Details

\Zend_View_Interface

visibility

public

default

final

false

static

false

Methods

__call
jump to class

__call(
          string $method, array $args
          )

        :
        mixed
Proxy to container methods

Arguments

$method

string

$args

array

Output

mixed

 Details

visibility

public

final

false

static

false

throws

For invalid method calls

__construct
jump to class

__construct(

          )

        :
        void
Initialize helper

Retrieve container from registry or create new container and store in
registry.

 Details

visibility

public

final

false

static

false

disableNoConflictMode
jump to class

disableNoConflictMode(

          )

        :
        void
Disable noConflict Mode of jQuery if this was previously enabled.

 Details

visibility

public

final

false

static

true

enableNoConflictMode
jump to class

enableNoConflictMode(

          )

        :

Enable the jQuery internal noConflict Mode to work with
other Javascript libraries. Will setup jQuery in the variable
$j instead of $ to overcome conflicts.

 Details

visibility

public

final

false

static

true

link

http://docs.jquery.com/Using_jQuery_with_Other_Libraries

getJQueryHandler
jump to class

getJQueryHandler(

          )

        :
        String
Return current jQuery handler based on noConflict mode settings.

Output

String

 Details

visibility

public

final

false

static

true

getNoConflictMode
jump to class

getNoConflictMode(

          )

        :
        Boolean
Return current status of the jQuery no Conflict Mode

Output

Boolean

 Details

visibility

public

final

false

static

true

jQuery
jump to class

jQuery(

          )

        :
        \ZendX_JQuery_View_Helper_JQuery_Container
Return jQuery View Helper class, to execute jQuery library related functions.

Output

\ZendX_JQuery_View_Helper_JQuery_Container

 Details

visibility

public

final

false

static

false

setView
jump to class

setView(
          \Zend_View_Interface $view
          )

        :
        void
Set view object

Arguments

$view

\Zend_View_Interface

 Details

visibility

public

final

false

static

false

Documentation was generated by DocBlox 0.12.2.


text:
<!DOCTYPE html>

    <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Hugo 0.73.0" />

<title>likelihood - Is this Math?</title>
<meta property="og:title" content="likelihood - Is this Math?">

  <link href='/favicon.ico' rel='icon' type='image/x-icon'/>

<link href="/tags/likelihood/index.xml" rel="alternate" type="application/rss+xml" title="Is this Math?" />

<link rel="stylesheet" href="/css/fonts.css" media="all">
<link rel="stylesheet" href="/css/main.css" media="all">

Home

2020

      Some notation for extended likelihoods

        2020-08-03

      Transforming the data of a likelihood involves a Jacobian; why the likelihood ratio is the key object

        2020-07-27

      Axiomatically, the frequentist likelihood is invariant under parameter transformation

        2020-07-27

            RSS feed

            Made with


text:
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Diagnostics;
using Autofac.Core;
using Autofac.Core.Resolving;

namespace Autofac.Test.Core.Resolving;

public class ResolveOperationTests
{
    [Fact]
    public void NullLifetimeScope_ThrowsArgumentNullException()
    {
        var ex = Assert.Throws<ArgumentNullException>(() => new ResolveOperation(null!, new DiagnosticListener("SomeListener")));
        Assert.Contains("(Parameter 'mostNestedLifetimeScope')", ex.Message);
    }

    [Fact]
    public void NullDiagnosticSource_ThrowsArgumentNullException()
    {
        var ex = Assert.Throws<ArgumentNullException>(() => new ResolveOperation(Mock.Of<ISharingLifetimeScope>(), null!));
        Assert.Contains("(Parameter 'diagnosticSource')", ex.Message);
    }

    [Fact]
    public void EmptyInProgessRequestWhenInitializing()
    {
        var resolveOperation = new ResolveOperation(Mock.Of<ISharingLifetimeScope>(), new DiagnosticListener("SomeName"));

        var inProgressStack = resolveOperation.InProgressRequests;

        Assert.Empty(inProgressStack);
    }

    [Fact]
    public void GetOrCreateInstanceThrowsArgumentNullExceptionWhenResolveRequestIsNull()
    {
        var lifetimeScope = Mock.Of<ISharingLifetimeScope>();
        var resolveOperation = new ResolveOperation(lifetimeScope, new DiagnosticListener("SomeName"));

        Assert.Throws<ArgumentNullException>(() => resolveOperation.GetOrCreateInstance(lifetimeScope, null!));
    }

    [Fact]
    public void AfterTheOperationIsFinished_ReusingTheTemporaryContextThrows()
    {
        IComponentContext ctx = null;
        var builder = new ContainerBuilder();
        builder.Register(c =>
        {
            ctx = c;
            return new object();
        });
        builder.RegisterInstance("Hello");
        var container = builder.Build();
        container.Resolve<string>();
        container.Resolve<object>();
        Assert.Throws<ObjectDisposedException>(() => ctx.Resolve<string>());
    }

    [Fact]
    public void OperationRaisesSuccessTraceEvents()
    {
        var builder = new ContainerBuilder();

        builder.RegisterInstance("Hello");

        var container = builder.Build();
        var mockTracer = Mocks.GetTracer();
        container.SubscribeToDiagnostics(mockTracer);

        var scope = container.Resolve<ILifetimeScope>() as ISharingLifetimeScope;

        var resolveOp = new ResolveOperation(scope, container.DiagnosticSource);

        var raisedEvents = new List<string>();

        var request = new ResolveRequest(new TypedService(typeof(string)), scope.ResolvableImplementationFor<string>(), Enumerable.Empty<Parameter>());

        mockTracer.OperationStarting += (op, req) =>
        {
            raisedEvents.Add("opstart");
            Assert.Equal(resolveOp, op);
            Assert.Equal(request, req);
        };

        mockTracer.RequestStarting += (op, ctxt) =>
        {
            raisedEvents.Add("reqstart");
            Assert.Equal(resolveOp, op);
            Assert.Equal(request.Service, ctxt.Service);
        };

        mockTracer.RequestSucceeding += (op, ctxt) =>
        {
            raisedEvents.Add("reqsuccess");
            Assert.Equal(resolveOp, op);
        };

        mockTracer.OperationSucceeding += (op, instance) =>
        {
            raisedEvents.Add("opsuccess");
            Assert.Equal("Hello", instance);
        };

        resolveOp.Execute(request);

        Assert.Equal(new[] { "opstart", "reqstart", "reqsuccess", "opsuccess" }, raisedEvents);
    }

    [Fact]
    public void OperationRaisesFailureTraceEvents()
    {
        var builder = new ContainerBuilder();

        builder.Register<string>(ctxt => throw new InvalidOperationException());

        var container = builder.Build();
        var mockTracer = Mocks.GetTracer();
        container.SubscribeToDiagnostics(mockTracer);

        var scope = container.Resolve<ILifetimeScope>() as ISharingLifetimeScope;

        var resolveOp = new ResolveOperation(scope, container.DiagnosticSource);

        var raisedEvents = new List<string>();

        var request = new ResolveRequest(new TypedService(typeof(string)), scope.ResolvableImplementationFor<string>(), Enumerable.Empty<Parameter>());

        mockTracer.OperationStarting += (op, req) =>
        {
            raisedEvents.Add("opstart");
            Assert.Equal(resolveOp, op);
            Assert.Equal(request, req);
        };

        mockTracer.RequestStarting += (op, ctxt) =>
        {
            raisedEvents.Add("reqstart");
            Assert.Equal(resolveOp, op);
            Assert.Equal(request.Service, ctxt.Service);
        };

        mockTracer.RequestFailing += (op, ctxt, ex) =>
        {
            raisedEvents.Add("reqfail");
            Assert.Equal(resolveOp, op);
            Assert.IsType<DependencyResolutionException>(ex);
        };

        mockTracer.OperationFailing += (op, ex) =>
        {
            raisedEvents.Add("opfail");
            Assert.IsType<DependencyResolutionException>(ex);
        };

        try
        {
            resolveOp.Execute(request);
        }
        catch
        {
        }

        Assert.Equal(new[] { "opstart", "reqstart", "reqfail", "opfail" }, raisedEvents);
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#24d7d7d7"
    tools:context="com.example.yiyoudoctor.ui.activity.SignFirstActivity">

    <include
        android:id="@+id/include8"
        layout="@layout/toolbar_back" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/include8"
        android:id="@+id/linearLayout3"
        android:orientation="horizontal"
        android:layout_marginTop="30dp"
        >

        <EditText
            android:id="@+id/editText2"
            android:layout_width="0dp"
            android:layout_weight="5"
            android:layout_height="50dp"
            android:background="#fff"
            android:hint="     请输入验证码"
            android:textColorHint="#88808080"
            android:textSize="20sp" />

        <Button
            android:id="@+id/prove_button"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:background="#06bae0"
            android:layout_weight="1.5"
            android:text="获取验证码"
            android:textColor="#fff"
            android:textSize="18sp"/>
    </LinearLayout>

    <Button
        android:id="@+id/sign2_button"
        android:layout_width="270dp"
        android:layout_height="40dp"
        android:layout_marginTop="89dp"
        android:background="@drawable/sign_button"
        android:text="确定"
        android:textColor="#fff"
        android:textSize="20sp"
        android:layout_below="@+id/linearLayout3"
        android:layout_centerHorizontal="true" />

</RelativeLayout>


text:
package org.axonframework.integrationtests.loopbacktest.synchronous;

import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.commandhandling.CommandMessage;
import org.axonframework.commandhandling.GenericCommandMessage;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.axonframework.config.AggregateConfigurer;
import org.axonframework.config.Configuration;
import org.axonframework.config.DefaultConfigurer;
import org.axonframework.eventhandling.DomainEventMessage;
import org.axonframework.eventsourcing.AggregateFactory;
import org.axonframework.eventsourcing.EventSourcingHandler;
import org.axonframework.eventsourcing.eventstore.inmemory.InMemoryEventStorageEngine;
import org.axonframework.messaging.Message;
import org.axonframework.messaging.unitofwork.DefaultUnitOfWork;
import org.axonframework.messaging.unitofwork.UnitOfWork;
import org.axonframework.modelling.command.Aggregate;
import org.axonframework.modelling.command.AggregateIdentifier;
import org.axonframework.modelling.command.AggregateNotFoundException;
import org.axonframework.modelling.command.AggregateRoot;
import org.axonframework.modelling.command.Repository;
import org.junit.jupiter.api.*;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.axonframework.modelling.command.AggregateLifecycle.apply;
import static org.junit.jupiter.api.Assertions.*;

/**
 * Test class validating that command dispatched within a UnitOfWork that publishes events withholds to the desired
 * event ordering.
 *
 * @author Gerard de Leeuw
 * @author Allard Buijze
 */
class LoopBackWithInterwovenCommandsAndEventsTest {

    // This ensures we do not wire Axon Server components
    private static final boolean DO_NOT_AUTO_LOCATE_CONFIGURER_MODULES = false;

    private final String aggregateIdentifier = "Aggregate";
    private MyCommand command;
    private Configuration configuration;

    @BeforeEach
    void setUp() {
        AggregateConfigurer<MyAggregate> aggregateConfigurer =
                AggregateConfigurer.defaultConfiguration(MyAggregate.class)
                                   .configureAggregateFactory(c -> new AggregateFactory<MyAggregate>() {
                                       @Override
                                       public MyAggregate createAggregateRoot(String aggregateIdentifier,
                                                                              DomainEventMessage<?> firstEvent) {
                                           return new MyAggregate(aggregateIdentifier);
                                       }

                                       @Override
                                       public Class<MyAggregate> getAggregateType() {
                                           return MyAggregate.class;
                                       }
                                   });
        configuration = DefaultConfigurer.defaultConfiguration(DO_NOT_AUTO_LOCATE_CONFIGURER_MODULES)
                                         .configureEmbeddedEventStore(c -> new InMemoryEventStorageEngine())
                                         .configureAggregate(aggregateConfigurer)
                                         .registerCommandHandler(c -> new MyCommandHandler(
                                                 c.repository(MyAggregate.class), c.commandGateway()
                                         ))
                                         .buildConfiguration();
        configuration.start();

        command = new MyCommand("outer", aggregateIdentifier,
                                new MyCommand("middle", aggregateIdentifier,
                                              new MyCommand("inner", aggregateIdentifier)));
    }

    @Test
    void orderInCommandHandlerAggregate() {
        MyAggregate commandHandlerAggregate = configuration.commandGateway().sendAndWait(command);

        assertEquals(expectedDescriptions(command), commandHandlerAggregate.getHandledCommands());
    }

    @Test
    void orderInEventSourcedAggregate() {
        Repository<MyAggregate> repository = configuration.repository(MyAggregate.class);
        configuration.commandGateway().sendAndWait(command);

        UnitOfWork<CommandMessage<?>> unitOfWork =
                DefaultUnitOfWork.startAndGet(GenericCommandMessage.asCommandMessage("loading"));
        MyAggregate loadedAggregate = repository.load(aggregateIdentifier).invoke(Function.identity());
        unitOfWork.commit();

        assertEquals(expectedDescriptions(command), loadedAggregate.getHandledCommands());
    }

    @Test
    void orderInEventStore() {
        configuration.commandGateway().sendAndWait(command);
        assertEquals(expectedDescriptions(command), configuration.eventStore()
                                                                 .readEvents(aggregateIdentifier)
                                                                 .asStream()
                                                                 .map(Message::getPayload)
                                                                 .map(MyEvent.class::cast)
                                                                 .map(MyEvent::getDescription)
                                                                 .collect(Collectors.toList()));
    }

    private List<String> expectedDescriptions(MyCommand command) {
        List<String> descriptions = new ArrayList<>();
        descriptions.add(command.startDescription());
        if (command.getInnerCommand() != null) {
            descriptions.addAll(expectedDescriptions(command.getInnerCommand()));
        }
        descriptions.add(command.doneDescription());
        return descriptions;
    }

    /**
     * @author Gerard de Leeuw
     * @since 0.1.0 on 4-1-2017
     */
    @AggregateRoot
    public static class MyAggregate {

        private final List<String> handledCommands;
        @AggregateIdentifier
        private String aggregateIdentifier;

        public MyAggregate(String aggregateIdentifier) {
            this.aggregateIdentifier = aggregateIdentifier;
            this.handledCommands = new ArrayList<>();
        }

        public void handle(MyCommand command, CommandGateway commandGateway) {
            apply(new MyEvent(aggregateIdentifier, command.startDescription()));
            if (command.getInnerCommand() != null) {
                commandGateway.sendAndWait(command.getInnerCommand());
            }
            apply(new MyEvent(aggregateIdentifier, command.doneDescription()));
        }

        @EventSourcingHandler
        public void handle(MyEvent event) {
            this.aggregateIdentifier = event.getAggregateIdentifier();
            handledCommands.add(event.getDescription());
        }

        public List<String> getHandledCommands() {
            return handledCommands;
        }
    }

    /**
     * @author Gerard de Leeuw
     * @since 0.1.0 on 4-1-2017
     */
    public static class MyCommand {

        private final String name;
        private final String aggregateIdentifier;
        private final MyCommand innerCommand;

        public MyCommand(String name, String aggregateIdentifier) {
            this(name, aggregateIdentifier, null);
        }

        public MyCommand(String name, String aggregateIdentifier, MyCommand innerCommand) {
            this.name = name;
            this.aggregateIdentifier = aggregateIdentifier;
            this.innerCommand = innerCommand;
        }

        public String getName() {
            return name;
        }

        public String getAggregateIdentifier() {
            return aggregateIdentifier;
        }

        public MyCommand getInnerCommand() {
            return innerCommand;
        }

        public String startDescription() {
            return "Start handling command " + name;
        }

        public String doneDescription() {
            return "Done handling command " + name;
        }

        @Override
        public String toString() {
            return name;
        }
    }

    /**
     * @author Gerard de Leeuw
     * @since 0.1.0 on 4-1-2017
     */
    public static class MyCommandHandler {

        private final Repository<MyAggregate> repository;
        private final CommandGateway commandGateway;

        public MyCommandHandler(Repository<MyAggregate> repository, CommandGateway commandGateway) {
            this.repository = repository;
            this.commandGateway = commandGateway;
        }

        @CommandHandler
        public MyAggregate handle(MyCommand command) throws Exception {
            Aggregate<MyAggregate> aggregate;
            try {
                aggregate = repository.load(command.getAggregateIdentifier());
            } catch (AggregateNotFoundException e) {
                aggregate = repository.newInstance(() -> new MyAggregate(command.getAggregateIdentifier()));
            }
            aggregate.execute(a -> a.handle(command, commandGateway));
            return aggregate.invoke(Function.identity());
        }
    }

    /**
     * @author Gerard de Leeuw
     * @since 0.1.0 on 4-1-2017
     */
    public static class MyEvent {

        private final String aggregateIdentifier;
        private final String description;

        public MyEvent(String aggregateIdentifier, String description) {
            this.aggregateIdentifier = aggregateIdentifier;
            this.description = description;
        }

        public String getAggregateIdentifier() {
            return aggregateIdentifier;
        }

        public String getDescription() {
            return description;
        }

        @Override
        public String toString() {
            return description;
        }
    }
}


text:
package fortiss.gui;

import java.awt.Graphics;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import fortiss.components.Building;
import fortiss.gui.listeners.textfield.BNameListener;
import fortiss.gui.listeners.textfield.ComponentFieldListener;
import fortiss.gui.style.Colors;
import fortiss.gui.style.Fonts;
import fortiss.gui.style.StyleGenerator;
import net.miginfocom.swing.MigLayout;

/**
 * Input panel for building parameters.
 */
public class BuildingInputPanel extends InformationPanel {

	/** Building name */
	public JTextField txtBName;
	/** Building area title label */
	private JLabel lblBuilding;
	/** Building name label */
	private JLabel lb_id;

	private static final long serialVersionUID = 1L;

	/** Necessary for dark mode on/off implementation */
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		setBackground(Colors.background);
		setForeground(Colors.normal);
		lb_id.setForeground(Colors.normal);
		lblBuilding.setForeground(Colors.title);
	}

	public BuildingInputPanel() {
		StyleGenerator.setupStyle();
		initialize();
	}

	/**
	 * Initialize the contents of the panel.
	 */
	public void initialize() {
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Building information",
				TitledBorder.RIGHT, TitledBorder.TOP, null, Colors.accent2));

		setLayout(new MigLayout("insets 30 20 20 20, fillx, wrap 2, hidemode 2, width 99%",
				"[left, growprio 50]30[right]",
				""));

		lblBuilding = new JLabel("BUILDING");
		lblBuilding.setFont(Fonts.getOswald());
		add(lblBuilding, "spanx, center, gapbottom 30");

		// Add fields to show EMS details
		lb_id = new JLabel("Building name");
		add(lb_id);

		txtBName = new JTextField();
		ComponentFieldListener bnameListener = new BNameListener();
		txtBName.addKeyListener(bnameListener);
		txtBName.addFocusListener(bnameListener);
		txtBName.setColumns(10);
		add(txtBName, "growx");
	}

	@Override
	public void update() {
		Building building = DesignerPanel.selectedBuilding;
		txtBName.setText(building.getName());
	}

}


text:
using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
#if GDI
using System.Drawing;
using System.Drawing.Imaging;
#endif
#if WPF
using System.Windows;
using System.Windows.Media;
#endif
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.UnitTests.Helpers;

namespace PdfSharp.UnitTests.Shapes
{
  /// <summary>
  ///
  /// </summary>
  [TestClass]
  public class Polygons : TestBase
  {
    /// <summary>
    /// Gets or sets the test context which provides information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext { get; set; }

    [TestInitialize()]
    public void TestInitialize()
    {
      BeginPdf();
      BeginImage();
    }

    [TestCleanup()]
    public void TestCleanup()
    {
      EndPdf();
      EndImage();
    }

    [TestMethod]
    public void TestPolygons()
    {
      Render("Polygons", RenderPolygons);
    }

    void RenderPolygons(XGraphics gfx)
    {
      gfx.TranslateTransform(15, 20);

      XPen pen = new XPen(XColors.DarkBlue, 2.5);

      gfx.DrawPolygon(pen, XBrushes.LightCoral, GeometryObjects.GetPentagram(50, new XPoint(60, 70)), XFillMode.Winding);
      gfx.DrawPolygon(pen, XBrushes.LightCoral, GeometryObjects.GetPentagram(50, new XPoint(180, 70)), XFillMode.Alternate);
    }
  }
}


text:
/**
 * Package for SimpleArrayList.
 * Необходимо создать динамический контейнер с методами:
 *
 * 1) add(E value);
 * 2) E get(int index);
 * 3) реализовать интерфейс Iterable<E>.
 *
 * Внутри контейнер должен базироваться на массиве (Object[] container).
 * Использовать стандартные коллекции JDK (ArrayList, LinkedList и т.д.) запрещено.
 * Контейнер должен быть динамическим, т.е. при полном заполнении увеличиваться.
 *
 * Итератор должен реализовывать fail-fast поведение, т.е. если с момента создания итератора
 * коллекция подверглась структурному изменению, итератор должен кидать ConcurrentModificationException.
 * Это достигается через введение счетчика изменений - modCount.
 * Каждая операция, которая структурно модифицирует коллекцию должна инкрементировать этот счетчик.
 * В свою очередь итератор запоминает значение этого счетчика на момент своего создания (expectedModCount),
 * а затем на каждой итерации сравнивает сохраненное значение, с текущим значением поля modCount,
 * если они отличаются, то генерируется исключение.
 *
 * @author Mikhail Vlasov (mailto:hednight@gmail.com)
 * @version 1
 * @since 03.24.2018
 */
package ru.job4j.simplearraylist;


text:
<?php defined('BASEPATH') || exit('No direct script access allowed');

/**
 * Roles Settings Context
 *
 * Allows the management of the Bonfire roles.
 *
 * @package Bonfire\Modules\Roles\Controllers\Settings
 * @author  Bonfire Dev Team
 * @link    http://cibonfire.com/docs/bonfire/roles_and_permissions
 */
class Settings extends Admin_Controller
{
    private $permissionContext = 'Site.Settings.View';
    private $permissionCreate = 'Bonfire.Roles.Add';
    private $permissionEdit = 'Bonfire.Roles.Manage';
    private $permissionView = 'Bonfire.Roles.View';

    /**
     * Setup the required permissions and load required classes
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();

        $this->auth->restrict($this->permissionContext);
        $this->auth->restrict($this->permissionView);

        $this->load->model('role_model');
        $this->lang->load('roles');

        Assets::add_module_css('roles', 'css/settings.css');

        Assets::add_js('codeigniter-csrf.js');
        Assets::add_module_js('roles', 'jquery.tablehover.pack.js');
        Assets::add_module_js('roles', 'js/settings.js');

        Template::set_block('sub_nav', 'settings/_sub_nav');
    }

    /**
     * Display a list of all roles
     *
     * @return void
     */
    public function index()
    {
        // Get User Counts
        Template::set('deleted_users', $this->user_model->count_all(true));
        Template::set('role_counts', $this->user_model->count_by_roles());
        Template::set('total_users', $this->user_model->count_all());

        Template::set('roles', $this->role_model->where('deleted', 0)->find_all());

        Template::set('toolbar_title', lang('role_manage'));
        Template::render();
    }

    /**
     * Create a new role in the database
     *
     * @return void
     */
    public function create()
    {
        $this->auth->restrict($this->permissionCreate);

        if (isset($_POST['save'])) {
            if ($this->saveRole()) {
                Template::set_message(lang('role_create_success'), 'success');
                if (! empty($this->error)) {
                    Template::set_message(lang('role_create_error') . $this->error, 'danger');
                }
                redirect(SITE_AREA . '/settings/roles');
            }

            if (! empty($this->role_model->error)) {
                Template::set_message(lang('role_create_error') . $this->role_model->error, 'danger');
            }
        }

        if (! class_exists('Contexts', false)) {
            $this->load->library('ui/contexts');
        }

        Template::set_view('settings/role_form');
        Template::set('contexts', Contexts::getContexts(true));
        Template::set('toolbar_title', 'Create New Role');

        Template::render();
    }

    /**
     * Edit a role record
     *
     * @return void
     */
    public function edit()
    {
        $this->auth->restrict($this->permissionEdit);

        $id = (int) $this->uri->segment(5);
        if (empty($id)) {
            Template::set_message(lang('role_invalid_id'), 'danger');
            redirect(SITE_AREA . '/settings/roles');
        }

        if (isset($_POST['save'])) {
            if ($this->saveRole('update', $id)) {
                Template::set_message(lang('role_edit_success'), 'success');
                redirect(SITE_AREA . '/settings/roles');
            }
            if (! empty($this->role_model->error)) {
                Template::set_message(lang('role_edit_error') . $this->role_model->error, 'danger');
            }
        } elseif (isset($_POST['delete'])) {
            if ($this->role_model->delete($id)) {
                Template::set_message(lang('role_delete_success'), 'success');
                redirect(SITE_AREA . '/settings/roles');
            }
            if (! empty($this->role_model->error)) {
                Template::set_message(lang('role_delete_error') . $this->role_model->error, 'danger');
            }
        }

        if (! class_exists('Contexts', false)) {
            $this->load->library('ui/contexts');
        }

        $title = lang('bf_action_edit') . ' ' . lang('matrix_role');
        $role = $this->role_model->find($id);

        Template::set_view('settings/role_form');
        Template::set('contexts', Contexts::getContexts(true));
        Template::set('role', $role);
        Template::set('toolbar_title', isset($role->role_name) ? "{$title}: {$role->role_name}" : $title);

        Template::render();
    }

    //--------------------------------------------------------------------------
    // !HMVC METHODS
    //--------------------------------------------------------------------------

    /**
     * Build the matrix for display in the role permissions form.
     *
     * @return string The table(s) of settings, ready to be used in a form.
     */
    public function matrix()
    {
        $id = (int) $this->uri->segment(5);
        $role = $this->role_model->find($id);

        // ID is empty for a new role, so permissions won't be assigned, yet.
        if ($id == 0) {
            return '
' . lang('role_new_permission_message') . '
';
        }

        $auth_failed = '';
        $domains = '';

        // Verify role has permission to modify this role's permissions.
        if (! $this->auth->has_permission('Permissions.' . ucwords($role->role_name) . '.Manage')) {
            $auth_failed = lang('matrix_auth_fail');
        } else {
            $permissions_full = $role->permissions;
            $role_permissions = $role->role_permissions;

            $template = array();
            foreach ($permissions_full as $key => $perm) {
                $template[$perm->name]['perm_id'] = $perm->permission_id;
                $template[$perm->name]['value'] = 0;
                if (isset($role_permissions[$perm->permission_id])) {
                    $template[$perm->name]['value'] = 1;
                }
            }

            // Extract the pieces from each permission.
            $domains = array();
            foreach ($template as $key => $value) {
                list($domain, $name, $action) = explode('.', $key);

                // Add it to the domain if it's not already there.
                if (! empty($domain) && ! array_key_exists($domain, $domains)) {
                    $domains[$domain] = array();
                }

                // Add the preference to the domain array.
                if (isset($domains[$domain][$name])) {
                    $domains[$domain][$name][$action] = $value;
                } else {
                    $domains[$domain][$name] = array(
                        $action => $value
                    );
                }

                // Store the actions separately to build the table header.
                if (! isset($domains[$domain]['actions'])) {
                    $domains[$domain]['actions'] = array();
                }

                if (! in_array($action, $domains[$domain]['actions'])) {
                    $domains[$domain]['actions'][] = $action;
                }
            }
        }

        // Build the table(s) in the view to make things a little clearer,
        // and return it!
        return $this->load->view(
            'settings/matrix',
            array('domains' => $domains, 'authentication_failed' => $auth_failed),
            true
        );
    }

    //--------------------------------------------------------------------
    // !PRIVATE METHODS
    //--------------------------------------------------------------------

    /**
     * Save the role record to the database.
     *
     * @param string $type The type of save operation (insert or edit).
     * @param int    $id   The record ID in the case of edit (ignored on insert).
     *
     * @return bool
     */
    private function saveRole($type = 'insert', $id = 0)
    {
        $this->form_validation->set_rules($this->role_model->get_validation_rules($type));
        if ($this->form_validation->run() === false) {
            return false;
        }

        // Grab the permissions and role name from the POST vars, if available.
        $permissions = $this->input->post('role_permissions');
        $roleName = $this->input->post('role_name');
        $data = $this->role_model->prep_data($this->input->post());

        if ($type == 'insert') {
            $id = $this->role_model->insert($data);
            $return = is_numeric($id);
        } elseif ($type == 'update') {
            // Grab the name of the role being updated.
            $current_name = $this->role_model->find($id)->role_name;
            $return = $this->role_model->update($id, $data);
        }

        if (! $return) {
            return $return;
        }

        // Reset validation so the permission model can use it.
        $this->form_validation->reset_validation();
        if (! class_exists('permission_model', false)) {
            $this->load->model('permissions/permission_model');
        }

        // Add a new management permission for the role.
        $add_perm = array(
            'name'        => 'Permissions.' . ucwords($roleName) . '.Manage',
            'description' => "To manage the access control permissions for the {$roleName} role.",
            'status'      => 'active'
        );

        if ($type == 'insert') {
            $permissionId = $this->permission_model->insert($add_perm);

            if (! $permissionId) {
                $this->error = 'There was an error creating the ACL permission.';
                if (! empty($this->permission_model->error)) {
                    $this->error .= " {$this->permission_model->error}";
                }
            } else {
                // Give current_role and admin new Manage permission.
                $roleIds = array(1);
                if (class_exists('auth')) {
                    $roleId = $this->auth->role_id();
                    if ($roleId != false) {
                        $rolesIds[] = $roleId;
                    }
                }

                $rolePermissions = array();
                foreach ($roleIds as $roleId) {
                    $rolePermissions[] = array(
                        'role_id'       => $roleId,
                        'permission_id' => $permissionId,
                    );
                }

                // Reset validation so the role_permissions model can use it.
                $this->form_validation->reset_validation();
                $this->role_permission_model->insert_batch($rolePermissions);
            }
        } elseif ($type == 'update') {
            // Update the permission name.
            $currentPermission = $this->permission_model->find_by('name', 'Permissions.' . ucwords($current_name) . '.Manage');
            $permissionKey = $this->permission_model->get_key();
            $add_perm['status'] = $currentPermission->status;
            $this->permission_model->update($currentPermission->{$permissionKey}, $add_perm);
        }

        // Reset validation so the role_permissions model can use it.
        $this->form_validation->reset_validation();

        // Save the permissions.
        if ($permissions
            && ! $this->role_permission_model->set_for_role($id, $permissions)
           ) {
            $this->error = 'There was an error saving the permissions.';
        }

        return $return;
    }

    /**
     * Create a real-time modifiable summary table of all roles and permissions
     *
     * @return void
     */
    public function permission_matrix()
    {
        $this->load->helper('inflector');

        Template::set(
            'matrix_permissions',
            $this->permission_model->select('permission_id, name')
                                   ->order_by('name')
                                   ->find_all()
        );
        Template::set(
            'matrix_roles',
            $this->role_model->select('role_id, role_name')
                             ->where('deleted', 0)
                             ->find_all()
        );

        $role_permissions = $this->role_permission_model->find_all_role_permissions();
        foreach ($role_permissions as $rp) {
            $current_permissions[] = "{$rp->role_id},{$rp->permission_id}";
        }

        Template::set('matrix_role_permissions', $current_permissions);
        Template::set("toolbar_title", lang('matrix_header'));

        Template::set_view('settings/permission_matrix');
        Template::render();
    }

    /**
     * Update the role_permissions table.
     *
     * @return void
     */
    public function matrix_update()
    {
        // Disable the profile for AJAX response
        $this->output->enable_profiler(false);

        $pieces = explode(',', $this->input->post('role_perm'));

        if (! $this->auth->has_permission('Permissions.' . $this->role_model->find((int)$pieces[0])->role_name . '.Manage')) {
            $this->output->set_output(lang("matrix_auth_fail"));
            return;
        }

        // A box was checked
        if ($this->input->post('action') == 'true') {
            if (is_numeric($this->role_permission_model->create_role_permissions($pieces[0], $pieces[1]))) {
                $msg = lang("matrix_insert_success");
            } else {
                $msg = lang("matrix_insert_fail") . $this->role_permission_model->error;
            }
        } else {
            // A box was unchecked
            if ($this->role_permission_model->delete_role_permissions($pieces[0], $pieces[1])) {
                $msg = lang("matrix_delete_success");
            } else {
                $msg = lang("matrix_delete_fail") . $this->role_permission_model->error;
            }
        }

        $this->output->set_output($msg);
    }
}


text:
using System.Collections.Generic;
using System.ComponentModel.Composition;
using Viticulture.Logic.Actions;
using Viticulture.Logic.Actions.Winter;

namespace Viticulture.Logic.Cards.Automa
{
    public class AutomaCard23 : AutomaCard
    {
        [ImportingConstructor]
        public AutomaCard23(DrawOrderAction drawOrder, TrainWorkerAction trainWorker, FillOrderAction fillOrder)
        {
            BlockedSummerActions = new List<BonusAction> { };
            BlockedWinterActions = new List<BonusAction> { drawOrder, trainWorker, fillOrder };
        }
        public AutomaCard23() { }

        public override List<BonusAction> BlockedSummerActions { get; }
        public override List<BonusAction> BlockedWinterActions { get; }
    }
}


text:
package t7014; // package needed due to other bug in scalac's java parser

// since we parse eagerly, we have not yet parsed the classfile when parsing the annotation,
// and on doing so, fail to find a symbol for the COMPLETELY_THREADSAFE reference
// from the annotation's argument to the enum's member
// for now, let's just not crash -- should implement lazy completing at some point
@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
public enum ThreadSafetyLevel { COMPLETELY_THREADSAFE }