text:
"""Tests for dnn_linear_combined.py."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import shutil
import tempfile

from absl.testing import parameterized
import numpy as np
import six

from tensorflow.core.example import example_pb2
from tensorflow.core.example import feature_pb2
from tensorflow.python.estimator import estimator
from tensorflow.python.estimator.canned import dnn_linear_combined
from tensorflow.python.estimator.canned import dnn_testing_utils
from tensorflow.python.estimator.canned import linear_testing_utils
from tensorflow.python.estimator.canned import prediction_keys
from tensorflow.python.estimator.export import export
from tensorflow.python.estimator.inputs import numpy_io
from tensorflow.python.estimator.inputs import pandas_io
from tensorflow.python.feature_column import feature_column
from tensorflow.python.feature_column import feature_column_v2
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.ops import nn
from tensorflow.python.ops import parsing_ops
from tensorflow.python.ops import variables as variables_lib
from tensorflow.python.platform import gfile
from tensorflow.python.platform import test
from tensorflow.python.summary.writer import writer_cache
from tensorflow.python.training import checkpoint_utils
from tensorflow.python.training import gradient_descent
from tensorflow.python.training import input as input_lib
from tensorflow.python.training import optimizer as optimizer_lib

try:
  # pylint: disable=g-import-not-at-top
  import pandas as pd
  HAS_PANDAS = True
except IOError:
  # Pandas writes a temporary file during import. If it fails, don't use pandas.
  HAS_PANDAS = False
except ImportError:
  HAS_PANDAS = False

class DNNOnlyModelFnTest(dnn_testing_utils.BaseDNNModelFnTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    dnn_testing_utils.BaseDNNModelFnTest.__init__(self, self._dnn_only_model_fn)

  def _dnn_only_model_fn(self,
                         features,
                         labels,
                         mode,
                         head,
                         hidden_units,
                         feature_columns,
                         optimizer='Adagrad',
                         activation_fn=nn.relu,
                         dropout=None,
                         input_layer_partitioner=None,
                         config=None):
    return dnn_linear_combined._dnn_linear_combined_model_fn(
        features=features,
        labels=labels,
        mode=mode,
        head=head,
        linear_feature_columns=[],
        dnn_hidden_units=hidden_units,
        dnn_feature_columns=feature_columns,
        dnn_optimizer=optimizer,
        dnn_activation_fn=activation_fn,
        dnn_dropout=dropout,
        input_layer_partitioner=input_layer_partitioner,
        config=config)

# A function to mimic linear-regressor init reuse same tests.
def _linear_regressor_fn(feature_columns,
                         model_dir=None,
                         label_dimension=1,
                         weight_column=None,
                         optimizer='Ftrl',
                         config=None,
                         partitioner=None,
                         sparse_combiner='sum'):
  return dnn_linear_combined.DNNLinearCombinedRegressor(
      model_dir=model_dir,
      linear_feature_columns=feature_columns,
      linear_optimizer=optimizer,
      label_dimension=label_dimension,
      weight_column=weight_column,
      input_layer_partitioner=partitioner,
      config=config,
      linear_sparse_combiner=sparse_combiner)

class LinearOnlyRegressorPartitionerTest(
    linear_testing_utils.BaseLinearRegressorPartitionerTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearRegressorPartitionerTest.__init__(
        self, _linear_regressor_fn, fc_lib=feature_column)

class LinearOnlyRegressorPartitionerV2Test(
    linear_testing_utils.BaseLinearRegressorPartitionerTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearRegressorPartitionerTest.__init__(
        self, _linear_regressor_fn, fc_lib=feature_column_v2)

class LinearOnlyRegressorEvaluationTest(
    linear_testing_utils.BaseLinearRegressorEvaluationTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearRegressorEvaluationTest.__init__(
        self, _linear_regressor_fn, fc_lib=feature_column)

class LinearOnlyRegressorEvaluationV2Test(
    linear_testing_utils.BaseLinearRegressorEvaluationTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearRegressorEvaluationTest.__init__(
        self, _linear_regressor_fn, fc_lib=feature_column_v2)

class LinearOnlyRegressorPredictTest(
    linear_testing_utils.BaseLinearRegressorPredictTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearRegressorPredictTest.__init__(
        self, _linear_regressor_fn, fc_lib=feature_column)

class LinearOnlyRegressorPredictV2Test(
    linear_testing_utils.BaseLinearRegressorPredictTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearRegressorPredictTest.__init__(
        self, _linear_regressor_fn, fc_lib=feature_column_v2)

class LinearOnlyRegressorIntegrationTest(
    linear_testing_utils.BaseLinearRegressorIntegrationTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearRegressorIntegrationTest.__init__(
        self, _linear_regressor_fn, fc_lib=feature_column)

class LinearOnlyRegressorIntegrationV2Test(
    linear_testing_utils.BaseLinearRegressorIntegrationTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearRegressorIntegrationTest.__init__(
        self, _linear_regressor_fn, fc_lib=feature_column_v2)

class LinearOnlyRegressorTrainingTest(
    linear_testing_utils.BaseLinearRegressorTrainingTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearRegressorTrainingTest.__init__(
        self, _linear_regressor_fn, fc_lib=feature_column)

class LinearOnlyRegressorTrainingV2Test(
    linear_testing_utils.BaseLinearRegressorTrainingTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearRegressorTrainingTest.__init__(
        self, _linear_regressor_fn, fc_lib=feature_column_v2)

def _linear_classifier_fn(feature_columns,
                          model_dir=None,
                          n_classes=2,
                          weight_column=None,
                          label_vocabulary=None,
                          optimizer='Ftrl',
                          config=None,
                          partitioner=None,
                          sparse_combiner='sum'):
  return dnn_linear_combined.DNNLinearCombinedClassifier(
      model_dir=model_dir,
      linear_feature_columns=feature_columns,
      linear_optimizer=optimizer,
      n_classes=n_classes,
      weight_column=weight_column,
      label_vocabulary=label_vocabulary,
      input_layer_partitioner=partitioner,
      config=config,
      linear_sparse_combiner=sparse_combiner)

class LinearOnlyClassifierTrainingTest(
    linear_testing_utils.BaseLinearClassifierTrainingTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearClassifierTrainingTest.__init__(
        self, linear_classifier_fn=_linear_classifier_fn, fc_lib=feature_column)

class LinearOnlyClassifierTrainingV2Test(
    linear_testing_utils.BaseLinearClassifierTrainingTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearClassifierTrainingTest.__init__(
        self,
        linear_classifier_fn=_linear_classifier_fn,
        fc_lib=feature_column_v2)

class LinearOnlyClassifierClassesEvaluationTest(
    linear_testing_utils.BaseLinearClassifierEvaluationTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearClassifierEvaluationTest.__init__(
        self, linear_classifier_fn=_linear_classifier_fn, fc_lib=feature_column)

class LinearOnlyClassifierClassesEvaluationV2Test(
    linear_testing_utils.BaseLinearClassifierEvaluationTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearClassifierEvaluationTest.__init__(
        self,
        linear_classifier_fn=_linear_classifier_fn,
        fc_lib=feature_column_v2)

class LinearOnlyClassifierPredictTest(
    linear_testing_utils.BaseLinearClassifierPredictTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearClassifierPredictTest.__init__(
        self, linear_classifier_fn=_linear_classifier_fn, fc_lib=feature_column)

class LinearOnlyClassifierPredictV2Test(
    linear_testing_utils.BaseLinearClassifierPredictTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearClassifierPredictTest.__init__(
        self,
        linear_classifier_fn=_linear_classifier_fn,
        fc_lib=feature_column_v2)

class LinearOnlyClassifierIntegrationTest(
    linear_testing_utils.BaseLinearClassifierIntegrationTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearClassifierIntegrationTest.__init__(
        self, linear_classifier_fn=_linear_classifier_fn, fc_lib=feature_column)

class LinearOnlyClassifierIntegrationV2Test(
    linear_testing_utils.BaseLinearClassifierIntegrationTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    linear_testing_utils.BaseLinearClassifierIntegrationTest.__init__(
        self,
        linear_classifier_fn=_linear_classifier_fn,
        fc_lib=feature_column_v2)

@parameterized.parameters((feature_column,), (feature_column_v2,))
class DNNLinearCombinedRegressorIntegrationTest(test.TestCase):

  def setUp(self):
    self._model_dir = tempfile.mkdtemp()

  def tearDown(self):
    if self._model_dir:
      writer_cache.FileWriterCache.clear()
      shutil.rmtree(self._model_dir)

  def _test_complete_flow_helper(
      self, linear_feature_columns, dnn_feature_columns, feature_spec,
      train_input_fn, eval_input_fn, predict_input_fn, input_dimension,
      label_dimension, batch_size):
    est = dnn_linear_combined.DNNLinearCombinedRegressor(
        linear_feature_columns=linear_feature_columns,
        dnn_hidden_units=(2, 2),
        dnn_feature_columns=dnn_feature_columns,
        label_dimension=label_dimension,
        model_dir=self._model_dir)

    # TRAIN
    num_steps = 10
    est.train(train_input_fn, steps=num_steps)

    # EVALUTE
    scores = est.evaluate(eval_input_fn)
    self.assertEqual(num_steps, scores[ops.GraphKeys.GLOBAL_STEP])
    self.assertIn('loss', six.iterkeys(scores))

    # PREDICT
    predictions = np.array([
        x[prediction_keys.PredictionKeys.PREDICTIONS]
        for x in est.predict(predict_input_fn)
    ])
    self.assertAllEqual((batch_size, label_dimension), predictions.shape)

    # EXPORT
    serving_input_receiver_fn = export.build_parsing_serving_input_receiver_fn(
        feature_spec)
    export_dir = est.export_savedmodel(tempfile.mkdtemp(),
                                       serving_input_receiver_fn)
    self.assertTrue(gfile.Exists(export_dir))

  def _test_complete_flow(self, train_input_fn, eval_input_fn, predict_input_fn,
                          input_dimension, label_dimension, batch_size,
                          fc_impl):
    linear_feature_columns = [
        fc_impl.numeric_column('x', shape=(input_dimension,))
    ]
    dnn_feature_columns = [
        fc_impl.numeric_column('x', shape=(input_dimension,))
    ]
    feature_columns = linear_feature_columns + dnn_feature_columns
    feature_spec = fc_impl.make_parse_example_spec(feature_columns)
    self._test_complete_flow_helper(linear_feature_columns, dnn_feature_columns,
                                    feature_spec, train_input_fn, eval_input_fn,
                                    predict_input_fn, input_dimension,
                                    label_dimension, batch_size)

  def _test_complete_flow_mix1(self, train_input_fn, eval_input_fn,
                               predict_input_fn, input_dimension,
                               label_dimension, batch_size, fc_impl):
    del fc_impl
    linear_feature_columns = [
        feature_column.numeric_column('x', shape=(input_dimension,))
    ]
    dnn_feature_columns = [
        feature_column_v2.numeric_column('x', shape=(input_dimension,))
    ]
    feature_columns = linear_feature_columns + dnn_feature_columns
    feature_spec = feature_column.make_parse_example_spec(feature_columns)
    self._test_complete_flow_helper(linear_feature_columns, dnn_feature_columns,
                                    feature_spec, train_input_fn, eval_input_fn,
                                    predict_input_fn, input_dimension,
                                    label_dimension, batch_size)

  def _test_complete_flow_mix2(self, train_input_fn, eval_input_fn,
                               predict_input_fn, input_dimension,
                               label_dimension, batch_size, fc_impl):
    del fc_impl
    linear_feature_columns = [
        feature_column_v2.numeric_column('x', shape=(input_dimension,))
    ]
    dnn_feature_columns = [
        feature_column.numeric_column('x', shape=(input_dimension,))
    ]
    feature_columns = linear_feature_columns + dnn_feature_columns
    feature_spec = feature_column.make_parse_example_spec(feature_columns)
    self._test_complete_flow_helper(linear_feature_columns, dnn_feature_columns,
                                    feature_spec, train_input_fn, eval_input_fn,
                                    predict_input_fn, input_dimension,
                                    label_dimension, batch_size)

  def _test_numpy_input_fn_helper(self, fc_impl, fn_to_run):
    """Tests complete flow with numpy_input_fn."""
    label_dimension = 2
    batch_size = 10
    data = np.linspace(0., 2., batch_size * label_dimension, dtype=np.float32)
    data = data.reshape(batch_size, label_dimension)
    # learn y = x
    train_input_fn = numpy_io.numpy_input_fn(
        x={'x': data},
        y=data,
        batch_size=batch_size,
        num_epochs=None,
        shuffle=True)
    eval_input_fn = numpy_io.numpy_input_fn(
        x={'x': data},
        y=data,
        batch_size=batch_size,
        shuffle=False)
    predict_input_fn = numpy_io.numpy_input_fn(
        x={'x': data},
        batch_size=batch_size,
        shuffle=False)

    fn_to_run(
        train_input_fn=train_input_fn,
        eval_input_fn=eval_input_fn,
        predict_input_fn=predict_input_fn,
        input_dimension=label_dimension,
        label_dimension=label_dimension,
        batch_size=batch_size,
        fc_impl=fc_impl)

  def test_numpy_input_fn_basic(self, fc_impl):
    self._test_numpy_input_fn_helper(fc_impl, self._test_complete_flow)

  def test_numpy_input_fn_mix1(self, fc_impl):
    self._test_numpy_input_fn_helper(fc_impl, self._test_complete_flow_mix1)

  def test_numpy_input_fn_mix2(self, fc_impl):
    self._test_numpy_input_fn_helper(fc_impl, self._test_complete_flow_mix2)

  def _test_pandas_input_fn_helper(self, fc_impl, fn_to_run):
    """Tests complete flow with pandas_input_fn."""
    if not HAS_PANDAS:
      return
    label_dimension = 1
    batch_size = 10
    data = np.linspace(0., 2., batch_size, dtype=np.float32)
    x = pd.DataFrame({'x': data})
    y = pd.Series(data)
    train_input_fn = pandas_io.pandas_input_fn(
        x=x,
        y=y,
        batch_size=batch_size,
        num_epochs=None,
        shuffle=True)
    eval_input_fn = pandas_io.pandas_input_fn(
        x=x,
        y=y,
        batch_size=batch_size,
        shuffle=False)
    predict_input_fn = pandas_io.pandas_input_fn(
        x=x,
        batch_size=batch_size,
        shuffle=False)

    fn_to_run(
        train_input_fn=train_input_fn,
        eval_input_fn=eval_input_fn,
        predict_input_fn=predict_input_fn,
        input_dimension=label_dimension,
        label_dimension=label_dimension,
        batch_size=batch_size,
        fc_impl=fc_impl)

  def test_pandas_input_fn_basic(self, fc_impl):
    self._test_pandas_input_fn_helper(fc_impl, self._test_complete_flow)

  def test_pandas_input_fn_mix1(self, fc_impl):
    self._test_pandas_input_fn_helper(fc_impl, self._test_complete_flow_mix1)

  def test_pandas_input_fn_mix2(self, fc_impl):
    self._test_pandas_input_fn_helper(fc_impl, self._test_complete_flow_mix2)

  def _test_input_fn_from_parse_example_helper(self, fc_impl, fn_to_run):
    """Tests complete flow with input_fn constructed from parse_example."""
    label_dimension = 2
    batch_size = 10
    data = np.linspace(0., 2., batch_size * label_dimension, dtype=np.float32)
    data = data.reshape(batch_size, label_dimension)

    serialized_examples = []
    for datum in data:
      example = example_pb2.Example(features=feature_pb2.Features(
          feature={
              'x': feature_pb2.Feature(
                  float_list=feature_pb2.FloatList(value=datum)),
              'y': feature_pb2.Feature(
                  float_list=feature_pb2.FloatList(value=datum)),
          }))
      serialized_examples.append(example.SerializeToString())

    feature_spec = {
        'x': parsing_ops.FixedLenFeature([label_dimension], dtypes.float32),
        'y': parsing_ops.FixedLenFeature([label_dimension], dtypes.float32),
    }
    def _train_input_fn():
      feature_map = parsing_ops.parse_example(serialized_examples, feature_spec)
      features = linear_testing_utils.queue_parsed_features(feature_map)
      labels = features.pop('y')
      return features, labels
    def _eval_input_fn():
      feature_map = parsing_ops.parse_example(
          input_lib.limit_epochs(serialized_examples, num_epochs=1),
          feature_spec)
      features = linear_testing_utils.queue_parsed_features(feature_map)
      labels = features.pop('y')
      return features, labels
    def _predict_input_fn():
      feature_map = parsing_ops.parse_example(
          input_lib.limit_epochs(serialized_examples, num_epochs=1),
          feature_spec)
      features = linear_testing_utils.queue_parsed_features(feature_map)
      features.pop('y')
      return features, None

    fn_to_run(
        train_input_fn=_train_input_fn,
        eval_input_fn=_eval_input_fn,
        predict_input_fn=_predict_input_fn,
        input_dimension=label_dimension,
        label_dimension=label_dimension,
        batch_size=batch_size,
        fc_impl=fc_impl)

  def test_input_fn_from_parse_example_basic(self, fc_impl):
    self._test_input_fn_from_parse_example_helper(fc_impl,
                                                  self._test_complete_flow)

  def test_input_fn_from_parse_example_mix1(self, fc_impl):
    self._test_input_fn_from_parse_example_helper(fc_impl,
                                                  self._test_complete_flow_mix1)

  def test_input_fn_from_parse_example_mix2(self, fc_impl):
    self._test_input_fn_from_parse_example_helper(fc_impl,
                                                  self._test_complete_flow_mix2)

# A function to mimic dnn-classifier init reuse same tests.
def _dnn_classifier_fn(hidden_units,
                       feature_columns,
                       model_dir=None,
                       n_classes=2,
                       weight_column=None,
                       label_vocabulary=None,
                       optimizer='Adagrad',
                       config=None,
                       input_layer_partitioner=None):
  return dnn_linear_combined.DNNLinearCombinedClassifier(
      model_dir=model_dir,
      dnn_hidden_units=hidden_units,
      dnn_feature_columns=feature_columns,
      dnn_optimizer=optimizer,
      n_classes=n_classes,
      weight_column=weight_column,
      label_vocabulary=label_vocabulary,
      input_layer_partitioner=input_layer_partitioner,
      config=config)

class DNNOnlyClassifierEvaluateTest(
    dnn_testing_utils.BaseDNNClassifierEvaluateTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    dnn_testing_utils.BaseDNNClassifierEvaluateTest.__init__(
        self, _dnn_classifier_fn, fc_impl=feature_column)

class DNNOnlyClassifierEvaluateV2Test(
    dnn_testing_utils.BaseDNNClassifierEvaluateTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    dnn_testing_utils.BaseDNNClassifierEvaluateTest.__init__(
        self, _dnn_classifier_fn, fc_impl=feature_column_v2)

class DNNOnlyClassifierPredictTest(
    dnn_testing_utils.BaseDNNClassifierPredictTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    dnn_testing_utils.BaseDNNClassifierPredictTest.__init__(
        self, _dnn_classifier_fn, fc_impl=feature_column)

class DNNOnlyClassifierPredictV2Test(
    dnn_testing_utils.BaseDNNClassifierPredictTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    dnn_testing_utils.BaseDNNClassifierPredictTest.__init__(
        self, _dnn_classifier_fn, fc_impl=feature_column_v2)

class DNNOnlyClassifierTrainTest(
    dnn_testing_utils.BaseDNNClassifierTrainTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    dnn_testing_utils.BaseDNNClassifierTrainTest.__init__(
        self, _dnn_classifier_fn, fc_impl=feature_column)

class DNNOnlyClassifierTrainV2Test(dnn_testing_utils.BaseDNNClassifierTrainTest,
                                   test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    dnn_testing_utils.BaseDNNClassifierTrainTest.__init__(
        self, _dnn_classifier_fn, fc_impl=feature_column_v2)

# A function to mimic dnn-regressor init reuse same tests.
def _dnn_regressor_fn(hidden_units,
                      feature_columns,
                      model_dir=None,
                      label_dimension=1,
                      weight_column=None,
                      optimizer='Adagrad',
                      config=None,
                      input_layer_partitioner=None):
  return dnn_linear_combined.DNNLinearCombinedRegressor(
      model_dir=model_dir,
      dnn_hidden_units=hidden_units,
      dnn_feature_columns=feature_columns,
      dnn_optimizer=optimizer,
      label_dimension=label_dimension,
      weight_column=weight_column,
      input_layer_partitioner=input_layer_partitioner,
      config=config)

class DNNOnlyRegressorEvaluateTest(
    dnn_testing_utils.BaseDNNRegressorEvaluateTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    dnn_testing_utils.BaseDNNRegressorEvaluateTest.__init__(
        self, _dnn_regressor_fn, fc_impl=feature_column)

class DNNOnlyRegressorEvaluateV2Test(
    dnn_testing_utils.BaseDNNRegressorEvaluateTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    dnn_testing_utils.BaseDNNRegressorEvaluateTest.__init__(
        self, _dnn_regressor_fn, fc_impl=feature_column_v2)

class DNNOnlyRegressorPredictTest(
    dnn_testing_utils.BaseDNNRegressorPredictTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    dnn_testing_utils.BaseDNNRegressorPredictTest.__init__(
        self, _dnn_regressor_fn, fc_impl=feature_column)

class DNNOnlyRegressorPredictV2Test(
    dnn_testing_utils.BaseDNNRegressorPredictTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    dnn_testing_utils.BaseDNNRegressorPredictTest.__init__(
        self, _dnn_regressor_fn, fc_impl=feature_column_v2)

class DNNOnlyRegressorTrainTest(
    dnn_testing_utils.BaseDNNRegressorTrainTest, test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    dnn_testing_utils.BaseDNNRegressorTrainTest.__init__(
        self, _dnn_regressor_fn, fc_impl=feature_column)

class DNNOnlyRegressorTrainV2Test(dnn_testing_utils.BaseDNNRegressorTrainTest,
                                  test.TestCase):

  def __init__(self, methodName='runTest'):  # pylint: disable=invalid-name
    test.TestCase.__init__(self, methodName)
    dnn_testing_utils.BaseDNNRegressorTrainTest.__init__(
        self, _dnn_regressor_fn, fc_impl=feature_column_v2)

@parameterized.parameters((feature_column,), (feature_column_v2,))
class DNNLinearCombinedClassifierIntegrationTest(test.TestCase):

  def setUp(self):
    self._model_dir = tempfile.mkdtemp()

  def tearDown(self):
    if self._model_dir:
      writer_cache.FileWriterCache.clear()
      shutil.rmtree(self._model_dir)

  def _as_label(self, data_in_float):
    return np.rint(data_in_float).astype(np.int64)

  def _test_complete_flow(self, train_input_fn, eval_input_fn, predict_input_fn,
                          input_dimension, n_classes, batch_size, fc_impl):
    linear_feature_columns = [
        fc_impl.numeric_column('x', shape=(input_dimension,))
    ]
    dnn_feature_columns = [
        fc_impl.numeric_column('x', shape=(input_dimension,))
    ]
    feature_columns = linear_feature_columns + dnn_feature_columns
    est = dnn_linear_combined.DNNLinearCombinedClassifier(
        linear_feature_columns=linear_feature_columns,
        dnn_hidden_units=(2, 2),
        dnn_feature_columns=dnn_feature_columns,
        n_classes=n_classes,
        model_dir=self._model_dir)

    # TRAIN
    num_steps = 10
    est.train(train_input_fn, steps=num_steps)

    # EVALUTE
    scores = est.evaluate(eval_input_fn)
    self.assertEqual(num_steps, scores[ops.GraphKeys.GLOBAL_STEP])
    self.assertIn('loss', six.iterkeys(scores))

    # PREDICT
    predicted_proba = np.array([
        x[prediction_keys.PredictionKeys.PROBABILITIES]
        for x in est.predict(predict_input_fn)
    ])
    self.assertAllEqual((batch_size, n_classes), predicted_proba.shape)

    # EXPORT
    feature_spec = fc_impl.make_parse_example_spec(feature_columns)
    serving_input_receiver_fn = export.build_parsing_serving_input_receiver_fn(
        feature_spec)
    export_dir = est.export_savedmodel(tempfile.mkdtemp(),
                                       serving_input_receiver_fn)
    self.assertTrue(gfile.Exists(export_dir))

  def test_numpy_input_fn(self, fc_impl):
    """Tests complete flow with numpy_input_fn."""
    n_classes = 3
    input_dimension = 2
    batch_size = 10
    data = np.linspace(
        0., n_classes - 1., batch_size * input_dimension, dtype=np.float32)
    x_data = data.reshape(batch_size, input_dimension)
    y_data = self._as_label(np.reshape(data[:batch_size], (batch_size, 1)))
    # learn y = x
    train_input_fn = numpy_io.numpy_input_fn(
        x={'x': x_data},
        y=y_data,
        batch_size=batch_size,
        num_epochs=None,
        shuffle=True)
    eval_input_fn = numpy_io.numpy_input_fn(
        x={'x': x_data},
        y=y_data,
        batch_size=batch_size,
        shuffle=False)
    predict_input_fn = numpy_io.numpy_input_fn(
        x={'x': x_data},
        batch_size=batch_size,
        shuffle=False)

    self._test_complete_flow(
        train_input_fn=train_input_fn,
        eval_input_fn=eval_input_fn,
        predict_input_fn=predict_input_fn,
        input_dimension=input_dimension,
        n_classes=n_classes,
        batch_size=batch_size,
        fc_impl=fc_impl)

  def test_pandas_input_fn(self, fc_impl):
    """Tests complete flow with pandas_input_fn."""
    if not HAS_PANDAS:
      return
    input_dimension = 1
    n_classes = 2
    batch_size = 10
    data = np.linspace(0., n_classes - 1., batch_size, dtype=np.float32)
    x = pd.DataFrame({'x': data})
    y = pd.Series(self._as_label(data))
    train_input_fn = pandas_io.pandas_input_fn(
        x=x,
        y=y,
        batch_size=batch_size,
        num_epochs=None,
        shuffle=True)
    eval_input_fn = pandas_io.pandas_input_fn(
        x=x,
        y=y,
        batch_size=batch_size,
        shuffle=False)
    predict_input_fn = pandas_io.pandas_input_fn(
        x=x,
        batch_size=batch_size,
        shuffle=False)

    self._test_complete_flow(
        train_input_fn=train_input_fn,
        eval_input_fn=eval_input_fn,
        predict_input_fn=predict_input_fn,
        input_dimension=input_dimension,
        n_classes=n_classes,
        batch_size=batch_size,
        fc_impl=fc_impl)

  def test_input_fn_from_parse_example(self, fc_impl):
    """Tests complete flow with input_fn constructed from parse_example."""
    input_dimension = 2
    n_classes = 3
    batch_size = 10
    data = np.linspace(0., n_classes-1., batch_size * input_dimension,
                       dtype=np.float32)
    data = data.reshape(batch_size, input_dimension)

    serialized_examples = []
    for datum in data:
      example = example_pb2.Example(features=feature_pb2.Features(
          feature={
              'x':
                  feature_pb2.Feature(float_list=feature_pb2.FloatList(
                      value=datum)),
              'y':
                  feature_pb2.Feature(int64_list=feature_pb2.Int64List(
                      value=self._as_label(datum[:1]))),
          }))
      serialized_examples.append(example.SerializeToString())

    feature_spec = {
        'x': parsing_ops.FixedLenFeature([input_dimension], dtypes.float32),
        'y': parsing_ops.FixedLenFeature([1], dtypes.int64),
    }
    def _train_input_fn():
      feature_map = parsing_ops.parse_example(serialized_examples, feature_spec)
      features = linear_testing_utils.queue_parsed_features(feature_map)
      labels = features.pop('y')
      return features, labels
    def _eval_input_fn():
      feature_map = parsing_ops.parse_example(
          input_lib.limit_epochs(serialized_examples, num_epochs=1),
          feature_spec)
      features = linear_testing_utils.queue_parsed_features(feature_map)
      labels = features.pop('y')
      return features, labels
    def _predict_input_fn():
      feature_map = parsing_ops.parse_example(
          input_lib.limit_epochs(serialized_examples, num_epochs=1),
          feature_spec)
      features = linear_testing_utils.queue_parsed_features(feature_map)
      features.pop('y')
      return features, None

    self._test_complete_flow(
        train_input_fn=_train_input_fn,
        eval_input_fn=_eval_input_fn,
        predict_input_fn=_predict_input_fn,
        input_dimension=input_dimension,
        n_classes=n_classes,
        batch_size=batch_size,
        fc_impl=fc_impl)

@parameterized.parameters((feature_column,), (feature_column_v2,))
class DNNLinearCombinedTests(test.TestCase):

  def setUp(self):
    self._model_dir = tempfile.mkdtemp()

  def tearDown(self):
    if self._model_dir:
      shutil.rmtree(self._model_dir)

  def _mock_optimizer(self, real_optimizer, var_name_prefix):
    """Verifies global_step is None and var_names start with given prefix."""

    def _minimize(loss, global_step=None, var_list=None):
      self.assertIsNone(global_step)
      trainable_vars = var_list or ops.get_collection(
          ops.GraphKeys.TRAINABLE_VARIABLES)
      var_names = [var.name for var in trainable_vars]
      self.assertTrue(
          all([name.startswith(var_name_prefix) for name in var_names]))
      # var is used to check this op called by training.
      with ops.name_scope(''):
        var = variables_lib.Variable(0., name=(var_name_prefix + '_called'))
      with ops.control_dependencies([var.assign(100.)]):
        return real_optimizer.minimize(loss, global_step, var_list)

    optimizer_mock = test.mock.NonCallableMagicMock(
        spec=optimizer_lib.Optimizer, wraps=real_optimizer)
    optimizer_mock.minimize = test.mock.MagicMock(wraps=_minimize)

    return optimizer_mock

  def test_train_op_calls_both_dnn_and_linear(self, fc_impl):
    opt = gradient_descent.GradientDescentOptimizer(1.)
    x_column = fc_impl.numeric_column('x')
    input_fn = numpy_io.numpy_input_fn(
        x={'x': np.array([[0.], [1.]])},
        y=np.array([[0.], [1.]]),
        batch_size=1,
        shuffle=False)
    est = dnn_linear_combined.DNNLinearCombinedClassifier(
        linear_feature_columns=[x_column],
        # verifies linear_optimizer is used only for linear part.
        linear_optimizer=self._mock_optimizer(opt, 'linear'),
        dnn_hidden_units=(2, 2),
        dnn_feature_columns=[x_column],
        # verifies dnn_optimizer is used only for linear part.
        dnn_optimizer=self._mock_optimizer(opt, 'dnn'),
        model_dir=self._model_dir)
    est.train(input_fn, steps=1)
    # verifies train_op fires linear minimize op
    self.assertEqual(100.,
                     checkpoint_utils.load_variable(
                         self._model_dir, 'linear_called'))
    # verifies train_op fires dnn minimize op
    self.assertEqual(100.,
                     checkpoint_utils.load_variable(
                         self._model_dir, 'dnn_called'))

  def test_dnn_and_linear_logits_are_added(self, fc_impl):
    with ops.Graph().as_default():
      variables_lib.Variable([[1.0]], name='linear/linear_model/x/weights')
      variables_lib.Variable([2.0], name='linear/linear_model/bias_weights')
      variables_lib.Variable([[3.0]], name='dnn/hiddenlayer_0/kernel')
      variables_lib.Variable([4.0], name='dnn/hiddenlayer_0/bias')
      variables_lib.Variable([[5.0]], name='dnn/logits/kernel')
      variables_lib.Variable([6.0], name='dnn/logits/bias')
      variables_lib.Variable(1, name='global_step', dtype=dtypes.int64)
      linear_testing_utils.save_variables_to_ckpt(self._model_dir)

    x_column = fc_impl.numeric_column('x')
    est = dnn_linear_combined.DNNLinearCombinedRegressor(
        linear_feature_columns=[x_column],
        dnn_hidden_units=[1],
        dnn_feature_columns=[x_column],
        model_dir=self._model_dir)
    input_fn = numpy_io.numpy_input_fn(
        x={'x': np.array([[10.]])}, batch_size=1, shuffle=False)
    # linear logits = 10*1 + 2 = 12
    # dnn logits = (10*3 + 4)*5 + 6 = 176
    # logits = dnn + linear = 176 + 12 = 188
    self.assertAllClose(
        {
            prediction_keys.PredictionKeys.PREDICTIONS: [188.],
        },
        next(est.predict(input_fn=input_fn)))

@parameterized.parameters((feature_column,), (feature_column_v2,))
class DNNLinearCombinedWarmStartingTest(test.TestCase):

  def setUp(self):
    # Create a directory to save our old checkpoint and vocabularies to.
    self._ckpt_and_vocab_dir = tempfile.mkdtemp()

    # Make a dummy input_fn.
    def _input_fn():
      features = {
          'age': [[23.], [31.]],
          'city': [['Palo Alto'], ['Mountain View']],
      }
      return features, [0, 1]

    self._input_fn = _input_fn

  def tearDown(self):
    # Clean up checkpoint / vocab dir.
    writer_cache.FileWriterCache.clear()
    shutil.rmtree(self._ckpt_and_vocab_dir)

  def test_classifier_basic_warm_starting(self, fc_impl):
    """Tests correctness of DNNLinearCombinedClassifier default warm-start."""
    age = fc_impl.numeric_column('age')
    city = fc_impl.embedding_column(
        fc_impl.categorical_column_with_vocabulary_list(
            'city', vocabulary_list=['Mountain View', 'Palo Alto']),
        dimension=5)

    # Create a DNNLinearCombinedClassifier and train to save a checkpoint.
    dnn_lc_classifier = dnn_linear_combined.DNNLinearCombinedClassifier(
        linear_feature_columns=[age],
        dnn_feature_columns=[city],
        dnn_hidden_units=[256, 128],
        model_dir=self._ckpt_and_vocab_dir,
        n_classes=4,
        linear_optimizer='SGD',
        dnn_optimizer='SGD')
    dnn_lc_classifier.train(input_fn=self._input_fn, max_steps=1)

    # Create a second DNNLinearCombinedClassifier, warm-started from the first.
    # Use a learning_rate = 0.0 optimizer to check values (use SGD so we don't
    # have accumulator values that change).
    warm_started_dnn_lc_classifier = (
        dnn_linear_combined.DNNLinearCombinedClassifier(
            linear_feature_columns=[age],
            dnn_feature_columns=[city],
            dnn_hidden_units=[256, 128],
            n_classes=4,
            linear_optimizer=gradient_descent.GradientDescentOptimizer(
                learning_rate=0.0),
            dnn_optimizer=gradient_descent.GradientDescentOptimizer(
                learning_rate=0.0),
            warm_start_from=dnn_lc_classifier.model_dir))

    warm_started_dnn_lc_classifier.train(input_fn=self._input_fn, max_steps=1)
    for variable_name in warm_started_dnn_lc_classifier.get_variable_names():
      self.assertAllClose(
          dnn_lc_classifier.get_variable_value(variable_name),
          warm_started_dnn_lc_classifier.get_variable_value(variable_name))

  def test_regressor_basic_warm_starting(self, fc_impl):
    """Tests correctness of DNNLinearCombinedRegressor default warm-start."""
    age = fc_impl.numeric_column('age')
    city = fc_impl.embedding_column(
        fc_impl.categorical_column_with_vocabulary_list(
            'city', vocabulary_list=['Mountain View', 'Palo Alto']),
        dimension=5)

    # Create a DNNLinearCombinedRegressor and train to save a checkpoint.
    dnn_lc_regressor = dnn_linear_combined.DNNLinearCombinedRegressor(
        linear_feature_columns=[age],
        dnn_feature_columns=[city],
        dnn_hidden_units=[256, 128],
        model_dir=self._ckpt_and_vocab_dir,
        linear_optimizer='SGD',
        dnn_optimizer='SGD')
    dnn_lc_regressor.train(input_fn=self._input_fn, max_steps=1)

    # Create a second DNNLinearCombinedRegressor, warm-started from the first.
    # Use a learning_rate = 0.0 optimizer to check values (use SGD so we don't
    # have accumulator values that change).
    warm_started_dnn_lc_regressor = (
        dnn_linear_combined.DNNLinearCombinedRegressor(
            linear_feature_columns=[age],
            dnn_feature_columns=[city],
            dnn_hidden_units=[256, 128],
            linear_optimizer=gradient_descent.GradientDescentOptimizer(
                learning_rate=0.0),
            dnn_optimizer=gradient_descent.GradientDescentOptimizer(
                learning_rate=0.0),
            warm_start_from=dnn_lc_regressor.model_dir))

    warm_started_dnn_lc_regressor.train(input_fn=self._input_fn, max_steps=1)
    for variable_name in warm_started_dnn_lc_regressor.get_variable_names():
      self.assertAllClose(
          dnn_lc_regressor.get_variable_value(variable_name),
          warm_started_dnn_lc_regressor.get_variable_value(variable_name))

  def test_warm_starting_selective_variables(self, fc_impl):
    """Tests selecting variables to warm-start."""
    age = fc_impl.numeric_column('age')
    city = fc_impl.embedding_column(
        fc_impl.categorical_column_with_vocabulary_list(
            'city', vocabulary_list=['Mountain View', 'Palo Alto']),
        dimension=5)

    # Create a DNNLinearCombinedClassifier and train to save a checkpoint.
    dnn_lc_classifier = dnn_linear_combined.DNNLinearCombinedClassifier(
        linear_feature_columns=[age],
        dnn_feature_columns=[city],
        dnn_hidden_units=[256, 128],
        model_dir=self._ckpt_and_vocab_dir,
        n_classes=4,
        linear_optimizer='SGD',
        dnn_optimizer='SGD')
    dnn_lc_classifier.train(input_fn=self._input_fn, max_steps=1)

    # Create a second DNNLinearCombinedClassifier, warm-started from the first.
    # Use a learning_rate = 0.0 optimizer to check values (use SGD so we don't
    # have accumulator values that change).
    warm_started_dnn_lc_classifier = (
        dnn_linear_combined.DNNLinearCombinedClassifier(
            linear_feature_columns=[age],
            dnn_feature_columns=[city],
            dnn_hidden_units=[256, 128],
            n_classes=4,
            linear_optimizer=gradient_descent.GradientDescentOptimizer(
                learning_rate=0.0),
            dnn_optimizer=gradient_descent.GradientDescentOptimizer(
                learning_rate=0.0),
            # The provided regular expression will only warm-start the deep
            # portion of the model.
            warm_start_from=estimator.WarmStartSettings(
                ckpt_to_initialize_from=dnn_lc_classifier.model_dir,
                vars_to_warm_start='.*(dnn).*')))

    warm_started_dnn_lc_classifier.train(input_fn=self._input_fn, max_steps=1)
    for variable_name in warm_started_dnn_lc_classifier.get_variable_names():
      if 'dnn' in variable_name:
        self.assertAllClose(
            dnn_lc_classifier.get_variable_value(variable_name),
            warm_started_dnn_lc_classifier.get_variable_value(variable_name))
      elif 'linear' in variable_name:
        linear_values = warm_started_dnn_lc_classifier.get_variable_value(
            variable_name)
        # Since they're not warm-started, the linear weights will be
        # zero-initialized.
        self.assertAllClose(np.zeros_like(linear_values), linear_values)

if __name__ == '__main__':
  test.main()


text:
package org.apache.derby.impl.store.replication.net;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import javax.net.SocketFactory;
import org.apache.derby.iapi.error.StandardException;
import org.apache.derby.iapi.reference.SQLState;
import org.apache.derby.iapi.util.InterruptStatus;
import org.apache.derby.shared.common.reference.MessageId;

/**
 * Used to send replication messages to the slave. Called by the
 * Master controller to transmit replication messages wrapped in
 * a ReplicationMessage object to a receiver. The
 * receiver is implemented by the ReplicationMessageReceive
 * class.
 */
public class ReplicationMessageTransmit {

    /** Number of millis to wait for a response message before timing out
     */
    private final int DEFAULT_MESSAGE_RESPONSE_TIMEOUT = 30000;

    /** Used to synchronize when waiting for a response message from the slave
     */
    private final Object receiveSemaphore = new Object();

    /** The message received from the slave as a response to sending a
     * message. */
    private ReplicationMessage receivedMsg = null;

    /** Whether or not to keep the message receiver thread alive. Set to true
     * to terminate the thread */
    private volatile boolean stopMessageReceiver = false;

    /**
     * Contains the address (hostname and port number) of the slave
     * to replicate to.
     */
    private final SlaveAddress slaveAddress;

    /**
     * Used to write/read message objects to/from a connection.
     */
    private SocketConnection socketConn;

    /**
     * The name of the replicated database
     */
    private String dbname;

    /**
     * Constructor initializes the slave address used in replication.
     *
     * @param slaveAddress contains the address (host name and port number)
     *                     of the slave to connect to.
     */
    public ReplicationMessageTransmit(SlaveAddress slaveAddress) {
        this.slaveAddress = slaveAddress;
    }

    /**
     * Used to create a Socket connection to the slave and
     * establish compatibility with the database version of the slave by
     * comparing the UID's of the ReplicationMessage classes
     * of the master and the slave.
     *
     * @param timeout the amount of time for which the connection should
     *                block before being established.
     *
     * @param synchOnInstant the master log instant, used to check
     * that the master and slave log files are in synch. If no chunks
     * of log records have been shipped to the slave yet, this is the
     * end position in the current log file. If a chunk of log has
     * been shipped, this is the instant of the log record shipped
     * last. Note that there is a difference!
     *
     * @throws IOException if an exception occurs while trying to create the
     *         SocketConnection class or open a connection.
     *
     * @throws StandardException If an error message is received from the
     *         server indicating incompatible software versions of master
     *         and slave.
     *
     * @throws ClassNotFoundException Class of a serialized object cannot
     *         be found.
     */
    public void initConnection(int timeout, long synchOnInstant) throws
        IOException,
        StandardException,
        ClassNotFoundException {

        Socket s = null;

        final int timeout_ = timeout;
        try {
            //create a connection to the slave.
            s = AccessController.doPrivileged(new PrivilegedExceptionAction<Socket>() {
                public Socket run() throws IOException {
                    SocketFactory sf = SocketFactory.getDefault();
                    InetSocketAddress sockAddr = new InetSocketAddress(
                            slaveAddress.getHostAddress(),
                            slaveAddress.getPortNumber());
                    Socket s_temp = sf.createSocket();
                    s_temp.connect(sockAddr, timeout_);
                    return s_temp;
                }
            });
        } catch(PrivilegedActionException pea) {
            throw (IOException) pea.getException();
        }

        // keep socket alive even if no log is shipped for a long time
        s.setKeepAlive(true);

        socketConn = new SocketConnection(s);

        // Start the thread that will listen for incoming messages.
        startMessageReceiverThread(dbname);

        // Verify that the master and slave have the same software version
        // and exactly equal log files.
        brokerConnection(synchOnInstant);
    }

    /**
     * Tear down the network connection established with the
     * other replication peer
     *
     * @throws IOException if an exception occurs while trying to tear
     *                     down the network connection
     */
    public void tearDown() throws IOException {
        stopMessageReceiver = true;
        if(socketConn != null) {
            socketConn.tearDown();
            socketConn = null;
        }
    }

    /**
     * Used to send a replication message to the slave.
     *
     * @param message a ReplicationMessage object that contains
     *                the message to be transmitted.
     *
     * @throws IOException 1) if an exception occurs while transmitting
     *                        the message.
     *                     2) if the connection handle is invalid.
     */
    public void sendMessage(ReplicationMessage message) throws IOException {
        checkSocketConnection();
        socketConn.writeMessage(message);
    }

    /**
     * Send a replication message to the slave and return the
     * message received as a response. Will only wait
     * DEFAULT_MESSAGE_RESPONSE_TIMEOUT millis for the response
     * message. If not received when the wait times out, no message is
     * returned. The method is synchronized to guarantee that only one
     * thread will be waiting for a response message at any time.
     *
     * @param message a ReplicationMessage object that contains the message to
     * be transmitted.
     *
     * @return the response message
     * @throws IOException 1) if an exception occurs while sending or receiving
     *                        a message.
     *                     2) if the connection handle is invalid.
     * @throws StandardException if the response message has not been received
     * after DEFAULT_MESSAGE_RESPONSE_TIMEOUT millis
     */
    public synchronized ReplicationMessage
        sendMessageWaitForReply(ReplicationMessage message)
        throws IOException, StandardException {
        receivedMsg = null;
        checkSocketConnection();
        socketConn.writeMessage(message);
        long startMillis = System.currentTimeMillis();
        long waited = 0L;

        while (receivedMsg == null &&
               waited < DEFAULT_MESSAGE_RESPONSE_TIMEOUT) {

            synchronized (receiveSemaphore) {
                try {
                    receiveSemaphore.wait(
                        DEFAULT_MESSAGE_RESPONSE_TIMEOUT - waited);
                } catch (InterruptedException ie) {
                    InterruptStatus.setInterrupted();
                    waited = System.currentTimeMillis() - startMillis;
                    continue;
                }
                break;
            }
        }

        if (receivedMsg == null) {
            throw StandardException.
                newException(SQLState.REPLICATION_CONNECTION_LOST, dbname);

        }
        return receivedMsg;
    }

    /**
     * Used to send initiator messages to the slave and receive
     * information about the compatibility of the slave with the
     * master. One message is used to check that the slave and master
     * have the same software versions. A second message is used to
     * check that the master and slave log files are in synch.
     *
     * @param synchOnInstant the master log instant, used to check
     * that the master and slave log files are in synch. If no chunks
     * of log records have been shipped to the slave yet, this is the
     * end position in the current log file. If a chunk of log has
     * been shipped, this is the instant of the log record shipped
     * last. Note that there is a difference!
     *
     * @throws IOException if an exception occurs during the sending or
     *                     reading of the message.
     *
     * @throws StandardException If an error message is received from the
     *                           server indicating a mis-match in
     *                           serialVersionUID or log files out of synch.
     *
     * @throws ClassNotFoundException Class of a serialized object cannot
     *                                be found.
     */
    private void brokerConnection(long synchOnInstant)
        throws IOException, StandardException, ClassNotFoundException {
        // Check that master and slave have the same serialVersionUID
        ReplicationMessage initiatorMsg =
            new ReplicationMessage(ReplicationMessage.TYPE_INITIATE_VERSION,
                                   new Long(ReplicationMessage.
                                            serialVersionUID));
        verifyMessageType(sendMessageWaitForReply(initiatorMsg),
                          ReplicationMessage.TYPE_ACK);

        // Check that master and slave log files are in synch
        initiatorMsg =
            new ReplicationMessage(ReplicationMessage.TYPE_INITIATE_INSTANT,
                                   new Long(synchOnInstant));
        verifyMessageType(sendMessageWaitForReply(initiatorMsg),
                          ReplicationMessage.TYPE_ACK);
    }

    /**
     * Used to parse a message received from the slave. If the message
     * is an ack of the last shipped message, this method terminates
     * quietly. Otherwise, it throws the exception received in the
     * message from the slave describing why the last message could
     * not be acked.
     *
     * @throws StandardException If an error message is received from
     *                           the server
     *
     * @throws ClassNotFoundException Class of a serialized object cannot
     *                                be found.
     */
    private boolean verifyMessageType(ReplicationMessage message,
                                      int expectedType)
        throws StandardException {
        //If the message is a TYPE_ACK the slave is capable
        //of handling the messages and is at a compatible database version.
        if (message.getType() == expectedType) {
            return true;
        } else if (message.getType() == ReplicationMessage.TYPE_ERROR) {
            // See ReplicationMessage#TYPE_ERROR
            String exception[] = (String[])message.getMessage();
            throw StandardException.
                newException(exception[exception.length - 1], exception);
        } else {
            //The message format was not recognized. Hence throw
            //an unexpected exception.
            throw StandardException.newException
                (SQLState.REPLICATION_UNEXPECTED_EXCEPTION);
        }
    }

    /**
     * Verifies if the SocketConnection is valid.
     *
     * @throws IOException If the socket connection object is not
     *                     valid (is null).
     */
    private void checkSocketConnection() throws IOException {
        if (socketConn == null) {
            throw new IOException
                    (MessageId.REPLICATION_INVALID_CONNECTION_HANDLE);
        }
    }

    private void startMessageReceiverThread(String dbname) {
        MasterReceiverThread msgReceiver = new MasterReceiverThread(dbname);
        msgReceiver.setDaemon(true);
        msgReceiver.start();
    }

    /////////////////
    // Inner Class //
    /////////////////

    /**
     * Thread that listens for messages from the slave. A separate thread
     * listening for messages from the slave is needed because the slave
     * may send messages to the master at any time, and these messages require
     * immediate action.
     */
    private class MasterReceiverThread extends Thread {

        private final ReplicationMessage pongMsg =
            new ReplicationMessage(ReplicationMessage.TYPE_PONG, null);

        MasterReceiverThread(String dbname) {
            super("derby.master.receiver-" + dbname);
        }

        public void run() {
            ReplicationMessage message;
            while (!stopMessageReceiver) {
                try {
                    message = readMessage();

                    switch (message.getType()) {
                    case ReplicationMessage.TYPE_PING:
                        sendMessage(pongMsg);
                        break;
                    case ReplicationMessage.TYPE_ACK:
                    case ReplicationMessage.TYPE_ERROR:
                        synchronized (receiveSemaphore) {
                            receivedMsg = message;
                            receiveSemaphore.notify();
                        }
                        break;
                    default:
                        // Handling of other messages (i.e., stop and failover)
                        // not implemented yet
                        break;
                    }
                } catch (SocketTimeoutException ste) {
                    // ignore socket timeout on reads
                } catch (ClassNotFoundException cnfe) {
                    // TODO: print problem to log
                } catch (IOException ex) {
                    // TODO: print problem to log
                    // If we get an exception for this socket, the log shipper
                    // will clean up. Stop this thread.
                    stopMessageReceiver = true;
                }
            }
        }

        /**
         * Used to read a replication message sent by the slave. Hangs until a
         * message is received from the slave
         *
         * @return the reply message.
         *
         * @throws ClassNotFoundException Class of a serialized object cannot
         *                                be found.
         *
         * @throws IOException 1) if an exception occurs while reading from the
         *                        stream.
         *                     2) if the connection handle is invalid.
         */
        private ReplicationMessage readMessage() throws
            ClassNotFoundException, IOException {
            checkSocketConnection();
            return (ReplicationMessage)socketConn.readMessage();
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// ** This file is automatically generated by gapic-generator-typescript. **
// ** https://github.com/googleapis/gapic-generator-typescript **
// ** All changes to this file may be overwritten. **

'use strict';

function main(parent, certificate) {
  // [START privateca_v1_generated_CertificateAuthorityService_CreateCertificate_async]
  /**
   * This snippet has been automatically generated and should be regarded as a code template only.
   * It will require modifications to work.
   * It may require correct/in-range values for request initialization.
   * TODO(developer): Uncomment these variables before running the sample.
   */
  /**
   *  Required. The resource name of the CaPool google.cloud.security.privateca.v1.CaPool associated with the Certificate google.cloud.security.privateca.v1.Certificate,
   *  in the format `projects/* /locations/* /caPools/*`.
   */
  // const parent = 'abc123'
  /**
   *  Optional. It must be unique within a location and match the regular
   *  expression `[a-zA-Z0-9_-]{1,63}`. This field is required when using a
   *  CertificateAuthority google.cloud.security.privateca.v1.CertificateAuthority in the Enterprise CertificateAuthority.Tier ,
   *  but is optional and its value is ignored otherwise.
   */
  // const certificateId = 'abc123'
  /**
   *  Required. A Certificate google.cloud.security.privateca.v1.Certificate  with initial field values.
   */
  // const certificate = {}
  /**
   *  Optional. An ID to identify requests. Specify a unique request ID so that if you must
   *  retry your request, the server will know to ignore the request if it has
   *  already been completed. The server will guarantee that for at least 60
   *  minutes since the first request.
   *  For example, consider a situation where you make an initial request and the
   *  request times out. If you make the request again with the same request ID,
   *  the server can check if original operation with the same request ID was
   *  received, and if so, will ignore the second request. This prevents clients
   *  from accidentally creating duplicate commitments.
   *  The request ID must be a valid UUID with the exception that zero UUID is
   *  not supported (00000000-0000-0000-0000-000000000000).
   */
  // const requestId = 'abc123'
  /**
   *  Optional. If this is true, no Certificate google.cloud.security.privateca.v1.Certificate  resource will be persisted regardless
   *  of the CaPool google.cloud.security.privateca.v1.CaPool's tier google.cloud.security.privateca.v1.CaPool.tier, and the returned Certificate google.cloud.security.privateca.v1.Certificate
   *  will not contain the pem_certificate google.cloud.security.privateca.v1.Certificate.pem_certificate  field.
   */
  // const validateOnly = true
  /**
   *  Optional. The resource ID of the CertificateAuthority google.cloud.security.privateca.v1.CertificateAuthority  that should issue the
   *  certificate.  This optional field will ignore the load-balancing scheme of
   *  the Pool and directly issue the certificate from the CA with the specified
   *  ID, contained in the same CaPool google.cloud.security.privateca.v1.CaPool  referenced by `parent`. Per-CA quota
   *  rules apply. If left empty, a CertificateAuthority google.cloud.security.privateca.v1.CertificateAuthority  will be chosen from
   *  the CaPool google.cloud.security.privateca.v1.CaPool  by the service. For example, to issue a Certificate google.cloud.security.privateca.v1.Certificate  from
   *  a Certificate Authority with resource name
   *  "projects/my-project/locations/us-central1/caPools/my-pool/certificateAuthorities/my-ca",
   *  you can set the parent google.cloud.security.privateca.v1.CreateCertificateRequest.parent  to
   *  "projects/my-project/locations/us-central1/caPools/my-pool" and the
   *  issuing_certificate_authority_id google.cloud.security.privateca.v1.CreateCertificateRequest.issuing_certificate_authority_id  to "my-ca".
   */
  // const issuingCertificateAuthorityId = 'abc123'

  // Imports the Privateca library
  const {CertificateAuthorityServiceClient} = require('@google-cloud/security-private-ca').v1;

  // Instantiates a client
  const privatecaClient = new CertificateAuthorityServiceClient();

  async function callCreateCertificate() {
    // Construct request
    const request = {
      parent,
      certificate,
    };

    // Run request
    const response = await privatecaClient.createCertificate(request);
    console.log(response);
  }

  callCreateCertificate();
  // [END privateca_v1_generated_CertificateAuthorityService_CreateCertificate_async]
}

process.on('unhandledRejection', err => {
  console.error(err.message);
  process.exitCode = 1;
});
main(...process.argv.slice(2));


text:
'use strict';

import type {GraphQLReporter} from './GraphQLReporter';

class GraphQLMultiReporter implements GraphQLReporter {
  _reporters: Array<GraphQLReporter>;

  constructor(...reporters: Array<GraphQLReporter>) {
    this._reporters = reporters;
  }

  reportMessage(message: string): void {
    this._reporters.forEach(reporter => {
      reporter.reportMessage(message);
    });
  }

  reportTime(name: string, ms: number): void {
    this._reporters.forEach(reporter => {
      reporter.reportTime(name, ms);
    });
  }

  reportError(caughtLocation: string, error: Error): void {
    this._reporters.forEach(reporter => {
      reporter.reportError(caughtLocation, error);
    });
  }
}

module.exports = GraphQLMultiReporter;


text:
Add or remove flags in a list of messages.

`POST {{ api_url }}/v1/messages/flags`

For updating the `read` flag on common collections of messages, see also
the
[special endpoints for marking message as read in bulk](/api/mark-as-read-bulk).

## Usage examples

{start_tabs}
{tab|python}

{generate_code_example(python)|/messages/flags:post|example}

{tab|js}

More examples and documentation can be found [here](https://github.com/zulip/zulip-js).
```js
const zulip = require('zulip-js');

// Download zuliprc-dev from your dev server
const config = {
    zuliprc: 'zuliprc-dev',
};

const flagParams = {
    messages: [4, 8, 15],
    flag: 'read',
};

zulip(config).then((client) => {
    // Add the "read" flag to messages with IDs 4, 8 and 15
    client.messages.flags.add(flagParams)
    .then(console.log)

    // Remove the "read" flag from said messages
    client.messages.flags.remove(flagParams)
    .then(console.log);
});
```

{tab|curl}

```
curl -X POST {{ api_url }}/v1/messages/flags \
    -u BOT_EMAIL_ADDRESS:BOT_API_KEY \
    -d "messages=[4,8,15]" \
    -d "op=add" \
    -d "flag=starred"
```

{end_tabs}

## Arguments

{generate_api_arguments_table|zulip.yaml|/messages/flags:post}

## Response

#### Return values

* `messages`: An array with the IDs of the modified messages.

#### Example response

A typical successful JSON response may look like:

{generate_code_example|/messages/flags:post|fixture(200)}


text:
<?php
namespace Cerad\Bundle\AppBundle\Controller\Results;

use Symfony\Component\HttpFoundation\Request;

use Cerad\Bundle\TournBundle\Controller\BaseController as MyBaseController;

/* ===========================================================
 * The main difference bewteen this and the TournBundle is that
 * this one does not use the session to store query information
 * I think because wordpress is used
 *
 * 08 Feb 2014 - Add program/gender/age search functionality
 */
class PoolPlayResultsController extends MyBaseController
{
    public function resultsAction(Request $request)
    {
        // Simple model
        $model = $this->createModel($request);
        if ($model['response']) return $model['response'];

        $tplData = array();

        $tplData['pools']   = $model['pools'];
        $tplData['project'] = $model['project'];

        return $this->render($request->get('_template'), $tplData);
    }
    /* ===============================================
     * Assorted report objects
     */
    protected function createModel(Request $request)
    {
        // Back and forth on this
        $model = array();
        $model['response'] = null;

        // Need current project
        $project = $this->getProject();
        $model['project'] = $project;

        // Levels come from div (aka level, program,gender,age
        $levels = $this->getLevels($request);

        // Pull the games
        $gameRepo = $this->get('cerad_game.game_repository');
        $criteria = array();
        $criteria['projects' ]  = $project->getId();
        $criteria['levels'   ]  = $levels;
        $criteria['groupTypes'] = 'PP';

        $games = $gameRepo->queryGameSchedule($criteria);

        $resultsServiceId = sprintf('cerad_tourn.%s_results',$project->getResults());
        $results = $this->get($resultsServiceId);

        $pools = $results->getPools($games);

        $model['pools'] = $pools;

        return $model;
    }
    protected function getLevels($request)
    {
        // Deal with program,age,gender
        $age     = $request->get('age');
        $gender  = $request->get('gender');
        $program = $request->get('program');

        $criteria = array
        (
            'ages'     => $age,
            'genders'  => $gender,
            'programs' => $program,
        );
        $levelRepo = $this->get('cerad_level.level_repository');
        $levelKeys = $levelRepo->queryKeys($criteria);

        // Add in individual levels (previously called div)
        // TODO: Make this part of queryKeys
        $level = $request->get('level');
        if ($level) array_merge($levelKeys,explode(',',$level));

        return $levelKeys;
    }
}


text:
all:: tests

# Currently, there are 5 sets of test programs, cf. tests/Makefile:
# set 1: stuff on R and R^d
# set 2: wavelet bases on the interval ([DS],[P],...)
# set 2a: periodic wavelet bases on the interval
# set 3: wavelet bases on general higher-dim. domains ((mapped) cube, tensor prod.)
# set 3a: wavelet bases on the ring domain
# set 3b: generic tensor product wavelet bases
# set 4: wavelet bases on the L-domain
# set 5: adaptive wavelet schemes for elliptic equations
# set 6: adaptive wavelet schemes for parabolic equations
# set 7: nonadaptive wavelet schemes for elliptic equations

tests::
	cd tests; $(MAKE)

tests1::
	cd tests; $(MAKE) tests1

tests2::
	cd tests; $(MAKE) tests2

tests2a::
	cd tests; $(MAKE) tests2a

tests3::
	cd tests; $(MAKE) tests3

tests3a::
	cd tests; $(MAKE) tests3a

tests3b::
	cd tests; $(MAKE) tests3b

tests4::
	cd tests; $(MAKE) tests4

tests5::
	cd tests; $(MAKE) tests5

tests6::
	cd tests; $(MAKE) tests6

tests7::
	cd tests; $(MAKE) tests7

doc::
	cd doc; $(MAKE)

clean::
	cd tests; $(MAKE) clean

veryclean:: clean
	cd tests; $(MAKE) veryclean

countlines::
	wc -l Rd/*.{h,cpp} interval/*.{h,cpp} galerkin/*.{h,cpp} adaptive/*.{h,cpp} generic/*.{h,cpp} cube/*.{h,cpp} Ldomain/*.{h,cpp} parabolic/*.{h,cpp} ring/*.{h,cpp} tests/*.cpp


text:
#!/usr/bin/env node
'use strict';

/**
 * Third-party requires
 */
const child_pty = require('child_pty').spawn;
const chalk     = require("chalk");

// Retrieving options from CLI
let env = process.argv.filter(el => el.indexOf("--env=") > -1).pop();
env     = env ? env.split("=").pop().toUpperCase() : 'development';

let debug = process.argv.filter(el => el.indexOf("--debug=") > -1).pop();
debug     = debug ? debug.split("=").pop() : '*,-express:*,-send';

// Setting up a fake TTY to get debug module display properly
let shell = process.platform === 'win32' ? 'cmd.exe' : '/bin/sh';

// Launching Express app as requested
let express = child_pty(shell, []);
express.stdout.pipe(process.stdout);
let commandExpress = `DEBUG=${debug} node ./www --env=${env}\n`;
express.stdin.write(commandExpress);

// Launching tslint, tsc and node-sass in watch mode if in development environment
if (env === 'development') {
    let watch = child_pty(shell, []);
    watch.stdout.pipe(process.stdout);
    let commandWatch = "npm run watch\n";
    watch.stdin.write(commandWatch);
}


text:
class Character < ActiveRecord::Base
  include WordMonger

  attr_accessible :type, :character_prototype, :skills, :energy

  serialize :skills

  belongs_to :character_prototype, :foreign_key => "character_code"
  belongs_to :game
  belongs_to :location

  has_many :tasks

  def name
    character_prototype.full_name
  end

  def gender
    character_prototype.gender
  end

end


text:
require_relative '../../../spec_helper'

module RgGen::RTL
  describe Component do
    def create_item(owner, &body)
      i = Class.new(Item, &body).new(owner)
      i.build
      i
    end

    let(:configuration) do
      RgGen::InputBase::Component.new(nil)
    end

    let(:register_map) do
      RgGen::InputBase::Component.new(nil)
    end

    let(:component) do
      Component.new(nil, configuration, register_map)
    end

    let(:children) do
      2.times.map do
        c = Component.new(component, configuration, register_map)
        component.add_child(c)
        c
      end
    end

    let(:foo_item) do
      create_item(component) do
        build do
          wire      :domain_a, :foo_wire
          reg       :domain_b, :foo_reg
          logic     :domain_a, :foo_logic
          input     :domain_b, :foo_input
          output    :domain_a, :foo_output
          parameter :domain_b, :foo_parameter
        end
      end
    end

    let(:bar_item) do
      create_item(children[0]) do
        build do
          wire      :domain_a, :bar_wire
          reg       :domain_b, :bar_reg
          logic     :domain_a, :bar_logic
          input     :domain_b, :bar_input
          output    :domain_a, :bar_output
          parameter :domain_b, :bar_parameter
        end
      end
    end

    let(:baz_item) do
      create_item(children[1]) do
        build do
          wire      :domain_a, :baz_reg
          logic     :domain_b, :baz_logic
          input     :domain_a, :baz_input
          output    :domain_b, :baz_output
          parameter :domain_a, :baz_parameter
        end
      end
    end

    describe "#build" do
      before do
        component.add_item(items[0])
        component.add_item(items[1])
        component.build
      end

      let(:items) do
        [
          create_item(component) {
            build do
              wire :domain_a, :foo
            end
          },
          create_item(component) {
            build do
              input   :domain_a, :bar
              output  :domain_a, :baz
            end
          }
        ]
      end

      it "アイテムオブジェクトが持つIdentifierオブジェクトへのアクセッサを定義する" do
        expect(component.foo).to eql items[0].foo
        expect(component.bar).to eql items[1].bar
        expect(component.baz).to eql items[1].baz
      end
    end

    describe "#signal_declarations" do
      before do
        component.add_item(foo_item)
        component.add_item(bar_item)
        children[0].add_item(baz_item)
      end

      let(:declarations) do
        Hash.new do |h, d|
          [foo_item, bar_item, baz_item].flat_map { |i| i.signal_declarations(d) }
        end
      end

      it "配下のアイテムオブジェクトが持つSignalDeclarationオブジェクトを返す" do
        expect(component.signal_declarations(:domain_a)).to match(declarations[:domain_a].map {|d| eql(d)})
        expect(component.signal_declarations(:domain_b)).to match(declarations[:domain_b].map {|d| eql(d)})
      end
    end

    describe "#port_declarations" do
      before do
        component.add_item(foo_item)
        component.add_item(bar_item)
        children[0].add_item(baz_item)
      end

      let(:declarations) do
        Hash.new do |h, d|
          [foo_item, bar_item, baz_item].flat_map { |i| i.port_declarations(d) }
        end
      end

      it "配下のアイテムオブジェクトが持つPortDeclarationオブジェクトを返す" do
        expect(component.port_declarations(:domain_a)).to match(declarations[:domain_a].map {|d| eql(d)})
        expect(component.port_declarations(:domain_b)).to match(declarations[:domain_b].map {|d| eql(d)})
      end
    end

    describe "#parameter_declarations" do
      before do
        component.add_item(foo_item)
        component.add_item(bar_item)
        children[0].add_item(baz_item)
      end

      let(:declarations) do
        Hash.new do |h, d|
          [foo_item, bar_item, baz_item].flat_map { |i| i.parameter_declarations(d) }
        end
      end

      it "配下のアイテムオブジェクトが持つParameterDeclarationオブジェクトを返す" do
        expect(component.parameter_declarations(:domain_a)).to match(declarations[:domain_a].map {|d| eql(d)})
        expect(component.parameter_declarations(:domain_b)).to match(declarations[:domain_b].map {|d| eql(d)})
      end
    end
  end
end


text:
<?php
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: google/cloud/datacatalog/v1/datacatalog.proto

namespace Google\Cloud\DataCatalog\V1;

use Google\Protobuf\Internal\GPBType;
use Google\Protobuf\Internal\RepeatedField;
use Google\Protobuf\Internal\GPBUtil;

/**
 * Request message for
 * [CreateEntryGroup][google.cloud.datacatalog.v1.DataCatalog.CreateEntryGroup].
 *
 * Generated from protobuf message google.cloud.datacatalog.v1.CreateEntryGroupRequest
 */
class CreateEntryGroupRequest extends \Google\Protobuf\Internal\Message
{
    /**
     * Required. The names of the project and location that the new entry group belongs to.
     * Note: The entry group itself and its child resources might not be
     * stored in the location specified in its name.
     *
     * Generated from protobuf field string parent = 1 [(.google.api.field_behavior) = REQUIRED, (.google.api.resource_reference) = {
     */
    private $parent = '';
    /**
     * Required. The ID of the entry group to create.
     * The ID must contain only letters (a-z, A-Z), numbers (0-9),
     * underscores (_), and must start with a letter or underscore.
     * The maximum size is 64 bytes when encoded in UTF-8.
     *
     * Generated from protobuf field string entry_group_id = 3 [(.google.api.field_behavior) = REQUIRED];
     */
    private $entry_group_id = '';
    /**
     * The entry group to create. Defaults to empty.
     *
     * Generated from protobuf field .google.cloud.datacatalog.v1.EntryGroup entry_group = 2;
     */
    private $entry_group = null;

    /**
     * Constructor.
     *
     * @param array $data {
     *     Optional. Data for populating the Message object.
     *
     *     @type string $parent
     *           Required. The names of the project and location that the new entry group belongs to.
     *           Note: The entry group itself and its child resources might not be
     *           stored in the location specified in its name.
     *     @type string $entry_group_id
     *           Required. The ID of the entry group to create.
     *           The ID must contain only letters (a-z, A-Z), numbers (0-9),
     *           underscores (_), and must start with a letter or underscore.
     *           The maximum size is 64 bytes when encoded in UTF-8.
     *     @type \Google\Cloud\DataCatalog\V1\EntryGroup $entry_group
     *           The entry group to create. Defaults to empty.
     * }
     */
    public function __construct($data = NULL) {
        \GPBMetadata\Google\Cloud\Datacatalog\V1\Datacatalog::initOnce();
        parent::__construct($data);
    }

    /**
     * Required. The names of the project and location that the new entry group belongs to.
     * Note: The entry group itself and its child resources might not be
     * stored in the location specified in its name.
     *
     * Generated from protobuf field string parent = 1 [(.google.api.field_behavior) = REQUIRED, (.google.api.resource_reference) = {
     * @return string
     */
    public function getParent()
    {
        return $this->parent;
    }

    /**
     * Required. The names of the project and location that the new entry group belongs to.
     * Note: The entry group itself and its child resources might not be
     * stored in the location specified in its name.
     *
     * Generated from protobuf field string parent = 1 [(.google.api.field_behavior) = REQUIRED, (.google.api.resource_reference) = {
     * @param string $var
     * @return $this
     */
    public function setParent($var)
    {
        GPBUtil::checkString($var, True);
        $this->parent = $var;

        return $this;
    }

    /**
     * Required. The ID of the entry group to create.
     * The ID must contain only letters (a-z, A-Z), numbers (0-9),
     * underscores (_), and must start with a letter or underscore.
     * The maximum size is 64 bytes when encoded in UTF-8.
     *
     * Generated from protobuf field string entry_group_id = 3 [(.google.api.field_behavior) = REQUIRED];
     * @return string
     */
    public function getEntryGroupId()
    {
        return $this->entry_group_id;
    }

    /**
     * Required. The ID of the entry group to create.
     * The ID must contain only letters (a-z, A-Z), numbers (0-9),
     * underscores (_), and must start with a letter or underscore.
     * The maximum size is 64 bytes when encoded in UTF-8.
     *
     * Generated from protobuf field string entry_group_id = 3 [(.google.api.field_behavior) = REQUIRED];
     * @param string $var
     * @return $this
     */
    public function setEntryGroupId($var)
    {
        GPBUtil::checkString($var, True);
        $this->entry_group_id = $var;

        return $this;
    }

    /**
     * The entry group to create. Defaults to empty.
     *
     * Generated from protobuf field .google.cloud.datacatalog.v1.EntryGroup entry_group = 2;
     * @return \Google\Cloud\DataCatalog\V1\EntryGroup|null
     */
    public function getEntryGroup()
    {
        return $this->entry_group;
    }

    public function hasEntryGroup()
    {
        return isset($this->entry_group);
    }

    public function clearEntryGroup()
    {
        unset($this->entry_group);
    }

    /**
     * The entry group to create. Defaults to empty.
     *
     * Generated from protobuf field .google.cloud.datacatalog.v1.EntryGroup entry_group = 2;
     * @param \Google\Cloud\DataCatalog\V1\EntryGroup $var
     * @return $this
     */
    public function setEntryGroup($var)
    {
        GPBUtil::checkMessage($var, \Google\Cloud\DataCatalog\V1\EntryGroup::class);
        $this->entry_group = $var;

        return $this;
    }

}