# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# 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
#
# http://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.
# ==============================================================================
"""Generic training script that trains a model using a given dataset."""

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

import tensorflow as tf

from tensorflow.python.ops import control_flow_ops
from datasets import dataset_factory
from deployment import model_deploy
from nets import nets_factory
from preprocessing import preprocessing_factory

slim = tf.contrib.slim

tf.app.flags.DEFINE_string(
    'master', '', 'The address of the TensorFlow master to use.')

tf.app.flags.DEFINE_string(
    'train_dir', '/tmp/tfmodel/',
    'Directory where checkpoints and event logs are written to.')

tf.app.flags.DEFINE_integer('num_clones', 1,
                            'Number of model clones to deploy.')

tf.app.flags.DEFINE_boolean('clone_on_cpu', False,
                            'Use CPUs to deploy clones.')

tf.app.flags.DEFINE_integer('worker_replicas', 1, 'Number of worker replicas.')

tf.app.flags.DEFINE_integer(
    'num_ps_tasks', 0,
    'The number of parameter servers. If the value is 0, then the parameters '
    'are handled locally by the worker.')

tf.app.flags.DEFINE_integer(
    'num_readers', 4,
    'The number of parallel readers that read data from the dataset.')

tf.app.flags.DEFINE_integer(
    'num_preprocessing_threads', 4,
    'The number of threads used to create the batches.')

tf.app.flags.DEFINE_integer(
    'log_every_n_steps', 10,
    'The frequency with which logs are print.')

tf.app.flags.DEFINE_integer(
    'save_summaries_secs', 600,
    'The frequency with which summaries are saved, in seconds.')

tf.app.flags.DEFINE_integer(
    'save_interval_secs', 600,
    'The frequency with which the model is saved, in seconds.')

tf.app.flags.DEFINE_integer(
    'task', 0, 'Task id of the replica running the training.')

######################
# Optimization Flags #
######################

tf.app.flags.DEFINE_float(
    'weight_decay', 0.00004, 'The weight decay on the model weights.')

tf.app.flags.DEFINE_string(
    'optimizer', 'rmsprop',
    'The name of the optimizer, one of "adadelta", "adagrad", "adam",'
    '"ftrl", "momentum", "sgd" or "rmsprop".')

tf.app.flags.DEFINE_float(
    'adadelta_rho', 0.95,
    'The decay rate for adadelta.')

tf.app.flags.DEFINE_float(
    'adagrad_initial_accumulator_value', 0.1,
    'Starting value for the AdaGrad accumulators.')

tf.app.flags.DEFINE_float(
    'adam_beta1', 0.9,
    'The exponential decay rate for the 1st moment estimates.')

tf.app.flags.DEFINE_float(
    'adam_beta2', 0.999,
    'The exponential decay rate for the 2nd moment estimates.')

tf.app.flags.DEFINE_float('opt_epsilon', 1.0, 'Epsilon term for the optimizer.')

tf.app.flags.DEFINE_float('ftrl_learning_rate_power', -0.5,
                          'The learning rate power.')

tf.app.flags.DEFINE_float(
    'ftrl_initial_accumulator_value', 0.1,
    'Starting value for the FTRL accumulators.')

tf.app.flags.DEFINE_float(
    'ftrl_l1', 0.0, 'The FTRL l1 regularization strength.')

tf.app.flags.DEFINE_float(
    'ftrl_l2', 0.0, 'The FTRL l2 regularization strength.')

tf.app.flags.DEFINE_float(
    'momentum', 0.9,
    'The momentum for the MomentumOptimizer and RMSPropOptimizer.')

tf.app.flags.DEFINE_float('rmsprop_momentum', 0.9, 'Momentum.')

tf.app.flags.DEFINE_float('rmsprop_decay', 0.9, 'Decay term for RMSProp.')

#######################
# Learning Rate Flags #
#######################

tf.app.flags.DEFINE_string(
    'learning_rate_decay_type',
    'exponential',
    'Specifies how the learning rate is decayed. One of "fixed", "exponential",'
    ' or "polynomial"')

tf.app.flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')

tf.app.flags.DEFINE_float(
    'end_learning_rate', 0.0001,
    'The minimal end learning rate used by a polynomial decay learning rate.')

tf.app.flags.DEFINE_float(
    'label_smoothing', 0.0, 'The amount of label smoothing.')

tf.app.flags.DEFINE_float(
    'learning_rate_decay_factor', 0.94, 'Learning rate decay factor.')

tf.app.flags.DEFINE_float(
    'num_epochs_per_decay', 2.0,
    'Number of epochs after which learning rate decays.')

tf.app.flags.DEFINE_bool(
    'sync_replicas', False,
    'Whether or not to synchronize the replicas during training.')

tf.app.flags.DEFINE_integer(
    'replicas_to_aggregate', 1,
    'The Number of gradients to collect before updating params.')

tf.app.flags.DEFINE_float(
    'moving_average_decay', None,
    'The decay to use for the moving average.'
    'If left as None, then moving averages are not used.')

#######################
# Dataset Flags #
#######################

tf.app.flags.DEFINE_string(
    'dataset_name', 'imagenet', 'The name of the dataset to load.')

tf.app.flags.DEFINE_string(
    'dataset_split_name', 'train', 'The name of the train/test split.')

tf.app.flags.DEFINE_string(
    'dataset_dir', None, 'The directory where the dataset files are stored.')

tf.app.flags.DEFINE_integer(
    'labels_offset', 0,
    'An offset for the labels in the dataset. This flag is primarily used to '
    'evaluate the VGG and ResNet architectures which do not use a background '
    'class for the ImageNet dataset.')

tf.app.flags.DEFINE_string(
    'model_name', 'inception_v3', 'The name of the architecture to train.')

tf.app.flags.DEFINE_string(
    'preprocessing_name', None, 'The name of the preprocessing to use. If left '
    'as `None`, then the model_name flag is used.')

tf.app.flags.DEFINE_integer(
    'batch_size', 32, 'The number of samples in each batch.')

tf.app.flags.DEFINE_integer(
    'train_image_size', None, 'Train image size')

tf.app.flags.DEFINE_integer('max_number_of_steps', None,
                            'The maximum number of training steps.')

#####################
# Fine-Tuning Flags #
#####################

tf.app.flags.DEFINE_string(
    'checkpoint_path', None,
    'The path to a checkpoint from which to fine-tune.')

tf.app.flags.DEFINE_string(
    'checkpoint_exclude_scopes', None,
    'Comma-separated list of scopes of variables to exclude when restoring '
    'from a checkpoint.')

tf.app.flags.DEFINE_string(
    'trainable_scopes', None,
    'Comma-separated list of scopes to filter the set of variables to train.'
    'By default, None would train all the variables.')

tf.app.flags.DEFINE_boolean(
    'ignore_missing_vars', False,
    'When restoring a checkpoint would ignore missing variables.')

FLAGS = tf.app.flags.FLAGS

def _configure_learning_rate(num_samples_per_epoch, global_step):
  """Configures the learning rate.

  Args:
    num_samples_per_epoch: The number of samples in each epoch of training.
    global_step: The global_step tensor.

  Returns:
    A `Tensor` representing the learning rate.

  Raises:
    ValueError: if
  """
  decay_steps = int(num_samples_per_epoch / FLAGS.batch_size *
                    FLAGS.num_epochs_per_decay)
  if FLAGS.sync_replicas:
    decay_steps /= FLAGS.replicas_to_aggregate

  if FLAGS.learning_rate_decay_type == 'exponential':
    return tf.train.exponential_decay(FLAGS.learning_rate,
                                      global_step,
                                      decay_steps,
                                      FLAGS.learning_rate_decay_factor,
                                      staircase=True,
                                      name='exponential_decay_learning_rate')
  elif FLAGS.learning_rate_decay_type == 'fixed':
    return tf.constant(FLAGS.learning_rate, name='fixed_learning_rate')
  elif FLAGS.learning_rate_decay_type == 'polynomial':
    return tf.train.polynomial_decay(FLAGS.learning_rate,
                                     global_step,
                                     decay_steps,
                                     FLAGS.end_learning_rate,
                                     power=1.0,
                                     cycle=False,
                                     name='polynomial_decay_learning_rate')
  else:
    raise ValueError('learning_rate_decay_type [%s] was not recognized',
                     FLAGS.learning_rate_decay_type)

def _configure_optimizer(learning_rate):
  """Configures the optimizer used for training.

  Args:
    learning_rate: A scalar or `Tensor` learning rate.

  Returns:
    An instance of an optimizer.

  Raises:
    ValueError: if FLAGS.optimizer is not recognized.
  """
  if FLAGS.optimizer == 'adadelta':
    optimizer = tf.train.AdadeltaOptimizer(
        learning_rate,
        rho=FLAGS.adadelta_rho,
        epsilon=FLAGS.opt_epsilon)
  elif FLAGS.optimizer == 'adagrad':
    optimizer = tf.train.AdagradOptimizer(
        learning_rate,
        initial_accumulator_value=FLAGS.adagrad_initial_accumulator_value)
  elif FLAGS.optimizer == 'adam':
    optimizer = tf.train.AdamOptimizer(
        learning_rate,
        beta1=FLAGS.adam_beta1,
        beta2=FLAGS.adam_beta2,
        epsilon=FLAGS.opt_epsilon)
  elif FLAGS.optimizer == 'ftrl':
    optimizer = tf.train.FtrlOptimizer(
        learning_rate,
        learning_rate_power=FLAGS.ftrl_learning_rate_power,
        initial_accumulator_value=FLAGS.ftrl_initial_accumulator_value,
        l1_regularization_strength=FLAGS.ftrl_l1,
        l2_regularization_strength=FLAGS.ftrl_l2)
  elif FLAGS.optimizer == 'momentum':
    optimizer = tf.train.MomentumOptimizer(
        learning_rate,
        momentum=FLAGS.momentum,
        name='Momentum')
  elif FLAGS.optimizer == 'rmsprop':
    optimizer = tf.train.RMSPropOptimizer(
        learning_rate,
        decay=FLAGS.rmsprop_decay,
        momentum=FLAGS.rmsprop_momentum,
        epsilon=FLAGS.opt_epsilon)
  elif FLAGS.optimizer == 'sgd':
    optimizer = tf.train.GradientDescentOptimizer(learning_rate)
  else:
    raise ValueError('Optimizer [%s] was not recognized', FLAGS.optimizer)
  return optimizer

def _add_variables_summaries(learning_rate):
  summaries = []
  for variable in slim.get_model_variables():
    summaries.append(tf.histogram_summary(variable.op.name, variable))
  summaries.append(tf.scalar_summary('training/Learning Rate', learning_rate))
  return summaries

def _get_init_fn():
  """Returns a function run by the chief worker to warm-start the training.

  Note that the init_fn is only run when initializing the model during the very
  first global step.

  Returns:
    An init function run by the supervisor.
  """
  if FLAGS.checkpoint_path is None:
    return None

  # Warn the user if a checkpoint exists in the train_dir. Then we'll be
  # ignoring the checkpoint anyway.
  if tf.train.latest_checkpoint(FLAGS.train_dir):
    tf.logging.info(
        'Ignoring --checkpoint_path because a checkpoint already exists in %s'
        % FLAGS.train_dir)
    return None

  exclusions = []
  if FLAGS.checkpoint_exclude_scopes:
    exclusions = [scope.strip()
                  for scope in FLAGS.checkpoint_exclude_scopes.split(',')]

  # TODO(sguada) variables.filter_variables()
  variables_to_restore = []
  for var in slim.get_model_variables():
    excluded = False
    for exclusion in exclusions:
      if var.op.name.startswith(exclusion):
        excluded = True
        break
    if not excluded:
      variables_to_restore.append(var)

  if tf.gfile.IsDirectory(FLAGS.checkpoint_path):
    checkpoint_path = tf.train.latest_checkpoint(FLAGS.checkpoint_path)
  else:
    checkpoint_path = FLAGS.checkpoint_path

  tf.logging.info('Fine-tuning from %s' % checkpoint_path)

  return slim.assign_from_checkpoint_fn(
      checkpoint_path,
      variables_to_restore,
      ignore_missing_vars=FLAGS.ignore_missing_vars)

def _get_variables_to_train():
  """Returns a list of variables to train.

  Returns:
    A list of variables to train by the optimizer.
  """
  if FLAGS.trainable_scopes is None:
    return tf.trainable_variables()
  else:
    scopes = [scope.strip() for scope in FLAGS.trainable_scopes.split(',')]

  variables_to_train = []
  for scope in scopes:
    variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope)
    variables_to_train.extend(variables)
  return variables_to_train

def main(_):
  if not FLAGS.dataset_dir:
    raise ValueError('You must supply the dataset directory with --dataset_dir')

  tf.logging.set_verbosity(tf.logging.INFO)
  with tf.Graph().as_default():
    ######################
    # Config model_deploy#
    ######################
    deploy_config = model_deploy.DeploymentConfig(
        num_clones=FLAGS.num_clones,
        clone_on_cpu=FLAGS.clone_on_cpu,
        replica_id=FLAGS.task,
        num_replicas=FLAGS.worker_replicas,
        num_ps_tasks=FLAGS.num_ps_tasks)

    # Create global_step
    with tf.device(deploy_config.variables_device()):
      global_step = slim.create_global_step()

    ######################
    # Select the dataset #
    ######################
    dataset = dataset_factory.get_dataset(
        FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir)

    ####################
    # Select the network #
    ####################
    network_fn = nets_factory.get_network_fn(
        FLAGS.model_name,
        num_classes=(dataset.num_classes - FLAGS.labels_offset),
        weight_decay=FLAGS.weight_decay,
        is_training=True)

    #####################################
    # Select the preprocessing function #
    #####################################
    preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name
    image_preprocessing_fn = preprocessing_factory.get_preprocessing(
        preprocessing_name,
        is_training=True)

    ##############################################################
    # Create a dataset provider that loads data from the dataset #
    ##############################################################
    with tf.device(deploy_config.inputs_device()):
      provider = slim.dataset_data_provider.DatasetDataProvider(
          dataset,
          num_readers=FLAGS.num_readers,
          common_queue_capacity=20 * FLAGS.batch_size,
          common_queue_min=10 * FLAGS.batch_size)
      [image, label] = provider.get(['image', 'label'])
      label -= FLAGS.labels_offset

      train_image_size = FLAGS.train_image_size or network_fn.default_image_size

      image = image_preprocessing_fn(image, train_image_size, train_image_size)

      images, labels = tf.train.batch(
          [image, label],
          batch_size=FLAGS.batch_size,
          num_threads=FLAGS.num_preprocessing_threads,
          capacity=5 * FLAGS.batch_size)
      labels = slim.one_hot_encoding(
          labels, dataset.num_classes - FLAGS.labels_offset)
      batch_queue = slim.prefetch_queue.prefetch_queue(
          [images, labels], capacity=2 * deploy_config.num_clones)

    ####################
    # Define the model #
    ####################
    def clone_fn(batch_queue):
      """Allows data parallelism by creating multiple clones of network_fn."""
      images, labels = batch_queue.dequeue()
      logits, end_points = network_fn(images)

      #############################
      # Specify the loss function #
      #############################
      if 'AuxLogits' in end_points:
        slim.losses.softmax_cross_entropy(
            end_points['AuxLogits'], labels,
            label_smoothing=FLAGS.label_smoothing, weight=0.4, scope='aux_loss')
      slim.losses.softmax_cross_entropy(
          logits, labels, label_smoothing=FLAGS.label_smoothing, weight=1.0)
      return end_points

    # Gather initial summaries.
    summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES))

    clones = model_deploy.create_clones(deploy_config, clone_fn, [batch_queue])
    first_clone_scope = deploy_config.clone_scope(0)
    # Gather update_ops from the first clone. These contain, for example,
    # the updates for the batch_norm variables created by network_fn.
    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, first_clone_scope)

    # Add summaries for end_points.
    end_points = clones[0].outputs
    for end_point in end_points:
      x = end_points[end_point]
      summaries.add(tf.histogram_summary('activations/' + end_point, x))
      summaries.add(tf.scalar_summary('sparsity/' + end_point,
                                      tf.nn.zero_fraction(x)))

    # Add summaries for losses.
    for loss in tf.get_collection(tf.GraphKeys.LOSSES, first_clone_scope):
      summaries.add(tf.scalar_summary('losses/%s' % loss.op.name, loss))

    # Add summaries for variables.
    for variable in slim.get_model_variables():
      summaries.add(tf.histogram_summary(variable.op.name, variable))

    #################################
    # Configure the moving averages #
    #################################
    if FLAGS.moving_average_decay:
      moving_average_variables = slim.get_model_variables()
      variable_averages = tf.train.ExponentialMovingAverage(
          FLAGS.moving_average_decay, global_step)
    else:
      moving_average_variables, variable_averages = None, None

    #########################################
    # Configure the optimization procedure. #
    #########################################
    with tf.device(deploy_config.optimizer_device()):
      learning_rate = _configure_learning_rate(dataset.num_samples, global_step)
      optimizer = _configure_optimizer(learning_rate)
      summaries.add(tf.scalar_summary('learning_rate', learning_rate,
                                      name='learning_rate'))

    if FLAGS.sync_replicas:
      # If sync_replicas is enabled, the averaging will be done in the chief
      # queue runner.
      optimizer = tf.train.SyncReplicasOptimizer(
          opt=optimizer,
          replicas_to_aggregate=FLAGS.replicas_to_aggregate,
          variable_averages=variable_averages,
          variables_to_average=moving_average_variables,
          replica_id=tf.constant(FLAGS.task, tf.int32, shape=()),
          total_num_replicas=FLAGS.worker_replicas)
    elif FLAGS.moving_average_decay:
      # Update ops executed locally by trainer.
      update_ops.append(variable_averages.apply(moving_average_variables))

    # Variables to train.
    variables_to_train = _get_variables_to_train()

    #  and returns a train_tensor and summary_op
    total_loss, clones_gradients = model_deploy.optimize_clones(
        clones,
        optimizer,
        var_list=variables_to_train)
    # Add total_loss to summary.
    summaries.add(tf.scalar_summary('total_loss', total_loss,
                                    name='total_loss'))

    # Create gradient updates.
    grad_updates = optimizer.apply_gradients(clones_gradients,
                                             global_step=global_step)
    update_ops.append(grad_updates)

    update_op = tf.group(*update_ops)
    train_tensor = control_flow_ops.with_dependencies([update_op], total_loss,
                                                      name='train_op')

    # Add the summaries from the first clone. These contain the summaries
    # created by model_fn and either optimize_clones() or _gather_clone_loss().
    summaries |= set(tf.get_collection(tf.GraphKeys.SUMMARIES,
                                       first_clone_scope))

    # Merge all summaries together.
    summary_op = tf.merge_summary(list(summaries), name='summary_op')

    ###########################
    # Kicks off the training. #
    ###########################
    slim.learning.train(
        train_tensor,
        logdir=FLAGS.train_dir,
        master=FLAGS.master,
        is_chief=(FLAGS.task == 0),
        init_fn=_get_init_fn(),
        summary_op=summary_op,
        number_of_steps=FLAGS.max_number_of_steps,
        log_every_n_steps=FLAGS.log_every_n_steps,
        save_summaries_secs=FLAGS.save_summaries_secs,
        save_interval_secs=FLAGS.save_interval_secs,
        sync_optimizer=optimizer if FLAGS.sync_replicas else None)

if __name__ == '__main__':
  tf.app.run()

{'repo_name': 'coderSkyChen/Action_Recognition_Zoo', 'stars': '211', 'repo_language': 'Python', 'file_name': 'pytorch_load.py', 'mime_type': 'text/x-python', 'hash': 6808955611497721122, 'source_dataset': 'data'}


Chapter 10

3D Graphics with WebGL and ThreeJS

Overview

WebGL is 3D for the web. And as the name implies, it is related to OpenGL,
the industry standard API for hardware accelerated 3D graphics. 3D is a lot more
complicated than 2D. Not only do we have to deal with a full three dimensional
coordinate system and all of the math that goes with it, but we have to worry a
lot more about the state of the graphics context. Far, far more than the basic
colors and transforms of the 2D context.

In 2D we draw shapes with paths then fill them with fill styles. It's very
simple. 3D on the other hand, involves a very complex multi-stage process:

First, we have shapes in the form of geometry, lists of points in 3D space
called "vectors". Next, we may have additional information for the shapes.
Surface normals, for example, describe the direction that light bounces off of
the shape. Then, we must set up lights and a camera. The camera defines point of
view. The lights are just what they sound like, points in space that specify
where the light is coming from. With all of this set up, we apply shaders.

Shaders take the camera, light, normals, and geometry as inputs to draw the actual pixels.  (I realize this is a very simplified explanation of OpenGL, but please bear with me.)  There are two kinds of shaders, one used to modify the vectors to create final light reflections and another that draws the actual pixels. This latter shader is known as a pixel shader, for obvious reasons.

Shaders are essentially tiny programs written in a special OpenGL language
that looks like a form of C. This code is not very easy to write because it must
be massively parallel. A modern graphics processor is essentially a special
super parallel multi-core chip that does one thing very efficiently: render lots
of pixels very fast.

Shaders are the power behind modern graphics but they are not easy to work
with. On the plus side your app can install its own shaders to do lots of
amazing things, but the minus side is your app has to install its own
shaders. There are no shaders pre-built into the WebGL standard. You must bring
your own.

The above is a simplified version how OpenGL ES 2.0 and OpenGL 3 work (older
versions of OpenGL did not have shaders.) It is a complex but flexible system.
WebGL is essentially the same, just with a JavaScript API instead of C.

We simply don't have time for me to teach you OpenGL. We could easily fill up
an entire week-long conference learning OpenGL. Even if we did have the time,
you probably wouldn't write code this way. It would take you thousands of lines
of to make a fairly simple game. Instead, you would use a library or graphics
engine to do the low-level stuff for you, letting you concentrate on what
your app actually does. In the WebGL world, the most popular such library is an
open source project called ThreeJS. It greatly simplifies
building interactive 3D apps and comes with its own set of reusable shaders. That is what I'm going to teach you today: ThreeJS.

Examples

First a few examples.

This is a simple game called Zombies vs Cow where you use the arrow keys to
make the cow avoid getting eaten by the zombies. It is completely 3D and
hardware accelerated. It looks much like a professional game that you might see
on the Wii, but it is done entirely in a web browser.

Here is another example that gives you a Google Earth like experience without
installing a separate app.

Here is another example that does interesting
visualizations of audio with 3D.

All of these were created with Three.JS and WebGL

Browser Support

Before we dive in, a word on browser support. Opera, FireFox and all of the
desktop WebKit based browsers support WebGL. Typically they map down to the native
OpenGL stack. The big hole here is Internet Explorer. While IE 10 has excellent
support for 2D canvas, it does not support WebGL. Furthermore, Microsoft has not
announced any plans to support it in the future. It's unclear what effect this will have in the Windows 8 world where 3rd party browsers and plugins are disallowed.

On the mobile side there is virtually, no support for WebGL. iOS supports it
but only as part of iAd, not in the regular browser. This suggests that Apple
may add it in the future, however. Some Android phones support WebGL, but
usually only if an alternate browser like FireFox or Opera is installed. Since
desktop Chrome supports WebGL, and Google is making Chrome the Android
default, hopefully we will get WebGL as standard on Android as well. The only
mobile device that ships with good WebGL support out of the box is actually the
BlackBerry Playbook. So while support isn't great on mobile it will probably get
better over the next year or so. WebGL will be a part of the future web
standards and has some big names behind it, so now is a good time to get
started.

A ThreeJS Template

ThreeJS is an open source library created by creative coder extraordinaire, Mr. Doob. His real name is Ricardo Cabello, but if you search for Mr. Doob you will find his cool graphics hacks going back at least a decade.  ThreeJS is a library that sits on top of WebGL. It automates the annoying things so you can focus on your app.  To make it even easier to work with Jerome Etienne, has created a boiler plate builder that will give you a headstart. It fills in all of the common things like the camera, mouse input, and rendering, so that you can start with a working ThreeJS application. The template builder has several options, but for these projects you can just leave the defaults.

Let's see how easy it can be. Go to the ThreeJS Boiler Plate Builder and download a new template. Unzip it and open the index.html page in your browser to ensure it works. You should see something like this:

Now open up the index.html file in your text editor. Notice that the template is pretty well documented. Let's start with the init function.

// init the scene
function init(){

if( Detector.webgl ){
    renderer = new THREE.WebGLRenderer({
        antialias : true,	// to get smoother output
        preserveDrawingBuffer : true	// to allow screenshot
    });
    renderer.setClearColorHex( 0xBBBBBB, 1 );
// uncomment if webgl is required
//}else{
//	Detector.addGetWebGLMessage();
//	return true;
}else{
    renderer	= new THREE.CanvasRenderer();
}
renderer.setSize( window.innerWidth, window.innerHeight );
document.getElementById('container').appendChild(renderer.domElement);

First, the template initializes the system. It tries to create a WebGL renderer because ThreeJS actually supports some other backends like 2D canvas. For this we only want WebGL. If it can't create a WebGLRenderer it will fall back to 2D canvas. Though canvas will be much slower it might be better than showing nothing. It's up to you.

Then, it sets the size of the canvas and adds it to the page as a child of container (a DIV declared in the document.)

// add Stats.js - https://github.com/mrdoob/stats.js
stats = new Stats();
stats.domElement.style.position	= 'absolute';
stats.domElement.style.bottom	= '0px';
document.body.appendChild( stats.domElement );

Next, it creates a Stats object and adds it to the scene. This will show us how fast our code is running.

// create a scene
scene = new THREE.Scene();

Finally, it creates a Scene.  ThreeJS uses a tree structure called a scene graph. The scene is the root of this tree. Everything we create within the scene will be a child node in the scene tree.

// put a camera in the scene
camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set(0, 0, 5);
scene.add(camera);

Next comes the camera. This is a perspective camera. Generally you can leave these values alone, but it is possible to change the position of the camera if you wish.

// create a camera contol
cameraControls	= new THREEx.DragPanControls(camera)

DragPanControls is a utility object which will move the camera around as you drag the mouse. You can remove it if you want some other kind of control.

// transparently support window resize
THREEx.WindowResize.bind(renderer, camera);
// allow 'p' to make screenshot
THREEx.Screenshot.bindKey(renderer);
// allow 'f' to go fullscreen where this feature is supported
if( THREEx.FullScreen.available() ){
    THREEx.FullScreen.bindKey();
    document.getElementById('inlineDoc').innerHTML	+= "- f for fullscreen";
}

Normally we have to handle window resizing manually, but the Threex.WindowResize object (provided by the template, not ThreeJS) will handle it for us. It will resize the scene to fit the window.
The next lines add a fullscreen mode using the 'f' key and a screenshot using the 'p' key.

Okay, now that we are past the boiler plate, we can add a shape to the scene. We will start with a torus, which is a donut shape. ThreeJS has support for several standard shapes including the torus.

// here you add your objects
// - you will most likely replace this part by your own
var geometry	= new THREE.TorusGeometry( 1, 0.42 );
var material	= new THREE.MeshNormalMaterial();
var mesh	= new THREE.Mesh( geometry, material );
scene.add( mesh );

An object in the scene is called a mesh. A mesh is composed of two parts: the geometry and the material. The template uses torus geometry and standard normal material, which always reflects light perpedicularly to the surface of the geometry.  It reflects light but doesn't have a set color.  This is how the template creates the mesh and adds it to the scene.

// animation loop
function animate() {

    // loop on request animation loop
    // - it has to be at the begining of the function
    // - see details at http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
    requestAnimationFrame( animate );

    // do the render
    render();

    // update stats
    stats.update();
}

Now let's move down to the animate function. animate calls itself with requestAnimationFrame (which we learned about in the animation chapter,) invokes render() and updates the stats.

// render the scene
function render() {

    // update camera controls
    cameraControls.update();

    // actually render the scene
    renderer.render( scene, camera );
}

The render function is called for every frame of animation. First, it calls update on the camera controls to enable camera movement in response to mouse and keyboard input. Then, it calls renderer.render to actually draw the scene on the screen.

That's it. Here's what it looks like:

Customizing the Template

Now let's customize it a bit. Every object in the scene is capable of basic scale, rotate, and position transformations. Let's rotate the torus with mesh.rotation.y = Math.PI/2.  Note that rotations are in radians, not degrees.  Math.PI/2 is 90 degrees.

var geometry	= new THREE.TorusGeometry( 1, 0.42 );
var material	= new THREE.MeshNormalMaterial();
var mesh	= new THREE.Mesh( geometry, material );
mesh.rotation.y = Math.PI/2; //90 degrees

Now let's comment out the torus and replace it with something more complex.
ThreeJS can use pre-fab models as well as generated ones like the torus. The Utah Teapot is the "Hello
World" of the graphics world, so let's start with that. The teapot geometry is
encoded as a JSON file. We download teapot.js from the examples
repo and place it in the same directory as index.html. Next, we
load it with THREE.JSONLoader().load(). When it finishes loading, we
add it to the scene as a new mesh model, again employing a standard normal material.
(teapot.js originally came from Jerome's repo.)

//scene.add( mesh );

new THREE.JSONLoader().load('teapot.js', function(geometry) {
    var material	= new THREE.MeshNormalMaterial();
    var mesh	= new THREE.Mesh( geometry, material );
    scene.add( mesh );
    teapot = mesh;
});

Now let's add some animation and make the teapot rotate on each frame. We simply set a teapot variable and adjust its rotation by 0.01 on each frame.

// update camera controls
cameraControls.update();
teapot.rotation.y += 0.01;

Shader Effects

Finally, we will add some post-processing effects. They are called post-processing because they happen after the main rendering phase. These parts of the ThreeJS API are somewhat experimental and not documented well, but I'm going to show them to you anyway because they are very powerful. Post-processing requires adding more scripts to our page. We'll need ShaderExtras.js, RenderPass.js, BloomPass.js, ShaderPass.js, EffectComposer.js, DotScreenPass.js, and MaskPass.js.

<script src="vendor/three.js/ShaderExtras.js"></script>

<script src="vendor/three.js/postprocessing/RenderPass.js"></script>
<script src="vendor/three.js/postprocessing/BloomPass.js"></script>
<script src="vendor/three.js/postprocessing/ShaderPass.js"></script>
<script src="vendor/three.js/postprocessing/EffectComposer.js"></script>
<script src="vendor/three.js/postprocessing/DotScreenPass.js"></script>
<script src="vendor/three.js/postprocessing/MaskPass.js"></script>

We begin by creating a new function called initPostProcessing(). Inside it we will create an effect composer.

function initPostProcessing() {

    composer = new THREE.EffectComposer(renderer);

Next, we will add a render pass which will render the entire scene into a texture image. We have to tell it that it won't be rendering to the screen, then add it to the composer.

renderModel = new THREE.RenderPass(scene,camera);
renderModel.renderToScreen = false;
composer.addPass(renderModel);

Next, we will create a dot screen pass.  These are some good default values but you can adjust them to get different effects.  This pass will go to the screen so we will set renderToScreen to true and add it to the composer.

var effectDotScreen = new THREE.DotScreenPass(
    new THREE.Vector2(0,0), 0.5, 0.8);
effectDotScreen.renderToScreen = true;
composer.addPass(effectDotScreen);

Now, we need to update the render function. Instead of calling renderer.render() we will call renderer.clear() and composer.render();

// actually render the scene
//renderer.render( scene, camera );

//alt form
renderer.clear();
composer.render();

We also have to call initPostProcessing as the last line of the init function.

initPostProcessing();

Here's what it looks like. Crazy huh!

demo Teapot (run)

Shader code creates the dot screen effect

Just for curiosity, if we open up ShaderExtras.js we can see the actual shader math, which creates the dot pattern and generates the final color for each pixel.

fragmentShader: [

    "uniform vec2 center;",
    "uniform float angle;",
    "uniform float scale;",
    "uniform vec2 tSize;",

    "uniform sampler2D tDiffuse;",

    "varying vec2 vUv;",

    "float pattern() {",

        "float s = sin( angle ), c = cos( angle );",

        "vec2 tex = vUv * tSize - center;",
        "vec2 point = vec2( c * tex.x - s * tex.y, s * tex.x + c * tex.y ) * scale;",

        "return ( sin( point.x ) * sin( point.y ) ) * 4.0;",

    "}",

    "void main() {",

        "vec4 color = texture2D( tDiffuse, vUv );",

        "float average = ( color.r + color.g + color.b ) / 3.0;",

        "gl_FragColor = vec4( vec3( average * 10.0 - 5.0 + pattern() ), color.a );",

    "}"

].join("\n")

A Few More Details

Much like OpenGL, WebGL doesn't support text directly. Instead, you must draw text using a 2D canvas, then add it as a texture onto a plane. (see WebGL Factor's explanation.)

There is a library for building quick GUIs called dat-gui. The project page is here.

There are model loaders for a lot of formats. You will probably use the Collada or JSON loaders. (DAE files are for Collada). Some are just geometry, some include textures and animation, like the monster loader.  Loaders are important because most complex geometry won't be created in code, instead you would use geometry created by someone else, probably using a 3D modeler tool like Blender or Maya.

For the most part, any general performance tips for OpenGL apply to WebGL. For example, you should always cache geometry and materials on the GPU.

CreativeJS has lots of good examples of 2D Canvas and WebGL.

In the next chapter, you will do a hands on lab in which you will create a new app with a car that drives around on a large grassy plain under a starry sky.

{'repo_name': 'joshmarinacci/canvasdeepdive-book', 'stars': '181', 'repo_language': 'HTML', 'file_name': 'amino.js', 'mime_type': 'text/html', 'hash': -451956772663986646, 'source_dataset': 'data'}


<!DOCTYPE html>

<html class="no-js">

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title></title>
  <meta name="description" content="" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <script src="https://cdn.staticfile.org/axios/0.19.0-beta.1/axios.js"></script>
  <script src="https://cdn.staticfile.org/vue/2.5.17/vue.js"></script>
  <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.13.0/theme-chalk/index.css" />
  <script src="https://cdn.staticfile.org/element-ui/2.13.0/index.js"></script>

  <link rel="stylesheet" href="../../../lib/index.css" />
  <script src="../../../lib/avue.js"></script>
</head>

<body>
  <div id="app">
    <avue-form :option="option" v-model="obj">
    </avue-form>
  </div>
</body>
<script>
  new Vue({
    el: '#app',
    data() {
      return {
        obj: {},
        option: {
          column: [
            {
              label: '文本框',
              prop: 'textarea',
              maxlength: 5,
              span: 24,
              showWordLimit: true,
            },
            {
              label: '文本框',
              prop: 'textarea',
              type: 'textarea',
              maxlength: 5,
              minRows: 10,
              span: 24,
              showWordLimit: true,
            }
          ]
        }
      }
    },
    mounted() {

    },
    methods: {
    }
  })
</script>

</html>

{'repo_name': 'nmxiaowei/avue', 'stars': '1335', 'repo_language': 'JavaScript', 'file_name': 'config.js', 'mime_type': 'text/plain', 'hash': -7164359532611958656, 'source_dataset': 'data'}


//
//     Generated by class-dump 3.5 (64 bit) (Debug version compiled Jun  9 2015 22:53:21).
//
//     class-dump is Copyright (C) 1997-1998, 2000-2001, 2004-2014 by Steve Nygard.
//

#import "SubViewAutoPairDevice.h"

#import "AutoPairDeviceProtocol-Protocol.h"

@interface SubViewKeyboardSetup : SubViewAutoPairDevice <AutoPairDeviceProtocol>
{
}

- (void)automaticDeviceSetupComplete:(id)arg1 device:(id)arg2;
- (void)automaticDeviceSetupBluetoothKeyboardConnected:(id)arg1;
- (void)automaticDeviceSetupUSBKeyboardConnected:(id)arg1;
- (void)automaticDeviceSetupPairingDeviceUserPasskeyNotification:(id)arg1 deviceNumber:(int)arg2 passkey:(unsigned int)arg3 device:(id)arg4;
- (void)automaticDeviceSetupPairingDeviceConnected:(id)arg1 deviceNumber:(int)arg2 device:(id)arg3;
- (id)messageConnectedToDevice:(id)arg1 deviceNumber:(int)arg2;
- (id)textWaitingForAckWithDevice:(id)arg1 timeRemaining:(int)arg2;
- (id)textPairingFailedWithDevice:(id)arg1;
- (id)textPairingRetriesRemaining:(int)arg1;
- (id)textPairingErrorWithDevice:(id)arg1;
- (id)textPairingTimeoutFailureWithDevice:(id)arg1;
- (id)textPairingAuthenticationFailureWithDevice:(id)arg1;
- (id)textPairedToDevice:(id)arg1;
- (id)textErrorConnectingToDevice:(id)arg1 error:(int)arg2;
- (id)textConnectedToDevice:(id)arg1 deviceNumber:(int)arg2;
- (id)textConnectingToPrepairedDevice:(id)arg1 deviceNumber:(int)arg2;
- (id)textConnectingToDevice:(id)arg1 deviceNumber:(int)arg2;
- (id)textSearchingError:(int)arg1;
- (id)textFoundDevice:(id)arg1 totalFoundDevices:(int)arg2;
- (id)textUpdatingDeviceNames:(id)arg1 remainingDevices:(int)arg2;
- (id)textSearchingForDevices;
- (id)textNoDevicesFound;
- (id)textNoDeviceConnected;
- (id)textPrepairedDeviceNotConnected;
- (unsigned int)minorClass;
- (int)ackTimeLimit;
- (BOOL)computerHasDeviceAttached;
- (void)initiateDeviceSetup;
- (id)getNextSubView;
- (id)getName;

@end

{'repo_name': 'w0lfschild/macOS_headers', 'stars': '304', 'repo_language': 'None', 'file_name': 'NSTextFinderBarContainer-Protocol.h', 'mime_type': 'text/x-objective-c', 'hash': -4418333070180801458, 'source_dataset': 'data'}


fileFormatVersion: 2
guid: 91b21fd4e41edd841b9a734e4362f07d
timeCreated: 1465283417
licenseType: Store
NativeFormatImporter:
  userData:
  assetBundleName:
  assetBundleVariant:

{'repo_name': 'Yecats/dotGAME', 'stars': '106', 'repo_language': 'C#', 'file_name': 'ProjectVersion.txt', 'mime_type': 'text/plain', 'hash': -7332322972071658262, 'source_dataset': 'data'}


// Created by cgo -godefs - DO NOT EDIT
// cgo -godefs types_openbsd.go

package disk

const (
	sizeofPtr        = 0x8
	sizeofShort      = 0x2
	sizeofInt        = 0x4
	sizeofLong       = 0x8
	sizeofLongLong   = 0x8
	sizeofLongDouble = 0x8

	DEVSTAT_NO_DATA = 0x00
	DEVSTAT_READ    = 0x01
	DEVSTAT_WRITE   = 0x02
	DEVSTAT_FREE    = 0x03

	MNT_RDONLY      = 0x00000001
	MNT_SYNCHRONOUS = 0x00000002
	MNT_NOEXEC      = 0x00000004
	MNT_NOSUID      = 0x00000008
	MNT_NODEV       = 0x00000010
	MNT_ASYNC       = 0x00000040

	MNT_WAIT   = 1
	MNT_NOWAIT = 2
	MNT_LAZY   = 3
)

const (
	sizeOfDiskstats = 0x70
)

type (
	_C_short       int16
	_C_int         int32
	_C_long        int64
	_C_long_long   int64
	_C_long_double int64
)

type Statfs struct {
	F_flags       uint32
	F_bsize       uint32
	F_iosize      uint32
	Pad_cgo_0     [4]byte
	F_blocks      uint64
	F_bfree       uint64
	F_bavail      int64
	F_files       uint64
	F_ffree       uint64
	F_favail      int64
	F_syncwrites  uint64
	F_syncreads   uint64
	F_asyncwrites uint64
	F_asyncreads  uint64
	F_fsid        Fsid
	F_namemax     uint32
	F_owner       uint32
	F_ctime       uint64
	F_fstypename  [16]int8
	F_mntonname   [90]int8
	F_mntfromname [90]int8
	F_mntfromspec [90]int8
	Pad_cgo_1     [2]byte
	Mount_info    [160]byte
}
type Diskstats struct {
	Name       [16]int8
	Busy       int32
	Pad_cgo_0  [4]byte
	Rxfer      uint64
	Wxfer      uint64
	Seek       uint64
	Rbytes     uint64
	Wbytes     uint64
	Attachtime Timeval
	Timestamp  Timeval
	Time       Timeval
}
type Fsid struct {
	Val [2]int32
}
type Timeval struct {
	Sec  int64
	Usec int64
}

type Diskstat struct{}
type Bintime struct{}

{'repo_name': 'micro-plat/hydra', 'stars': '268', 'repo_language': 'Go', 'file_name': 'dlock.go', 'mime_type': 'text/plain', 'hash': -1109268047580752312, 'source_dataset': 'data'}


// stylelint-disable declaration-no-important, selector-no-qualifying-type

// Source: https://github.com/h5bp/main.css/blob/master/src/_print.css

// ==========================================================================
// Print styles.
// Inlined to avoid the additional HTTP request:
// https://www.phpied.com/delay-loading-your-print-css/
// ==========================================================================

@if $enable-print-styles {
  @media print {
    *,
    *::before,
    *::after {
      // Bootstrap specific; comment out `color` and `background`
      //color: $black !important; // Black prints faster
      text-shadow: none !important;
      //background: transparent !important;
      box-shadow: none !important;
    }

    a {
      &:not(.btn) {
        text-decoration: underline;
      }
    }

    // Bootstrap specific; comment the following selector out
    //a[href]::after {
    //  content: " (" attr(href) ")";
    //}

    abbr[title]::after {
      content: " (" attr(title) ")";
    }

    // Bootstrap specific; comment the following selector out
    //
    // Don't show links that are fragment identifiers,
    // or use the `javascript:` pseudo protocol
    //

    //a[href^="#"]::after,
    //a[href^="javascript:"]::after {
    // content: "";
    //}

    pre {
      white-space: pre-wrap !important;
    }
    pre,
    blockquote {
      border: $border-width solid $gray-500; // Bootstrap custom code; using `$border-width` instead of 1px
      page-break-inside: avoid;
    }

    //
    // Printing Tables:
    // https://web.archive.org/web/20180815150934/http://css-discuss.incutio.com/wiki/Printing_Tables
    //

    thead {
      display: table-header-group;
    }

    tr,
    img {
      page-break-inside: avoid;
    }

    p,
    h2,
    h3 {
      orphans: 3;
      widows: 3;
    }

    h2,
    h3 {
      page-break-after: avoid;
    }

    // Bootstrap specific changes start

    // Specify a size and min-width to make printing closer across browsers.
    // We don't set margin here because it breaks `size` in Chrome. We also
    // don't use `!important` on `size` as it breaks in Chrome.
    @page {
      size: $print-page-size;
    }
    body {
      min-width: $print-body-min-width !important;
    }
    .container {
      min-width: $print-body-min-width !important;
    }

    // Bootstrap components
    .navbar {
      display: none;
    }
    .badge {
      border: $border-width solid $black;
    }

    .table {
      border-collapse: collapse !important;

      td,
      th {
        background-color: $white !important;
      }
    }

    .table-bordered {
      th,
      td {
        border: 1px solid $gray-300 !important;
      }
    }

    .table-dark {
      color: inherit;

      th,
      td,
      thead th,
      tbody + tbody {
        border-color: $table-border-color;
      }
    }

    .table .thead-dark th {
      color: inherit;
      border-color: $table-border-color;
    }

    // Bootstrap specific changes end
  }
}

{'repo_name': 'IdentityServer/IdentityServer4', 'stars': '6755', 'repo_language': 'C#', 'file_name': 'site.js', 'mime_type': 'text/plain', 'hash': 594114533615905966, 'source_dataset': 'data'}


from ctypes import *

class PyObject(Structure):
    _fields_ = [('ob_refcnt', c_size_t),
                ('ob_type', py_object)]

class PseudoTypeType(object):
    def __getattribute__(self, name):
        if name == '__repr__':
            raise Exception()
        elif name == '__name__':
            return 'PseudoType'

class PseudoType(object):
    def __repr__(self):
        return 'pseudo'

PseudoType_ptr = cast(id(PseudoType), POINTER(PyObject))
obj = PseudoType()
PseudoType_ptr.contents.ob_type = py_object(PseudoTypeType)

print()

{'repo_name': 'microsoft/PTVS', 'stars': '2276', 'repo_language': 'C#', 'file_name': 'LaunchConditions.wxs', 'mime_type': 'text/xml', 'hash': -8988372819235388040, 'source_dataset': 'data'}


// Copyright 2016 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package builder

import "errors"

var (
	// ErrNotSupportType not supported SQL type error
	ErrNotSupportType = errors.New("Not supported SQL type")
	// ErrNoNotInConditions no NOT IN params error
	ErrNoNotInConditions = errors.New("No NOT IN conditions")
	// ErrNoInConditions no IN params error
	ErrNoInConditions = errors.New("No IN conditions")
	// ErrNeedMoreArguments need more arguments
	ErrNeedMoreArguments = errors.New("Need more sql arguments")
	// ErrNoTableName no table name
	ErrNoTableName = errors.New("No table indicated")
	// ErrNoColumnToInsert no column to update
	ErrNoColumnToUpdate = errors.New("No column(s) to update")
	// ErrNoColumnToInsert no column to update
	ErrNoColumnToInsert = errors.New("No column(s) to insert")
	// ErrNotSupportDialectType not supported dialect type error
	ErrNotSupportDialectType = errors.New("Not supported dialect type")
	// ErrNotUnexpectedUnionConditions using union in a wrong way
	ErrNotUnexpectedUnionConditions = errors.New("Unexpected conditional fields in UNION query")
	// ErrUnsupportedUnionMembers unexpected members in UNION query
	ErrUnsupportedUnionMembers = errors.New("Unexpected members in UNION query")
	// ErrUnexpectedSubQuery Unexpected sub-query in SELECT query
	ErrUnexpectedSubQuery = errors.New("Unexpected sub-query in SELECT query")
	// ErrDialectNotSetUp dialect is not setup yet
	ErrDialectNotSetUp = errors.New("Dialect is not setup yet, try to use `Dialect(dbType)` at first")
	// ErrInvalidLimitation offset or limit is not correct
	ErrInvalidLimitation = errors.New("Offset or limit is not correct")
	// ErrUnnamedDerivedTable Every derived table must have its own alias
	ErrUnnamedDerivedTable = errors.New("Every derived table must have its own alias")
	// ErrInconsistentDialect Inconsistent dialect in same builder
	ErrInconsistentDialect = errors.New("Inconsistent dialect in same builder")
)

{'repo_name': 'WeBankPartners/open-monitor', 'stars': '184', 'repo_language': 'Go', 'file_name': 'index.html', 'mime_type': 'text/html', 'hash': -7544817459431182181, 'source_dataset': 'data'}


package log

import (
	"golang.org/x/sys/windows"
	"syscall"
)

// EnableColoredConsoleOutput sets flag that enables VT escape sequences
// in windows console. See link for Win API documentation.
//
// https://docs.microsoft.com/en-us/windows/console/setconsolemode
//
func EnableColoredConsoleOutput() {
	var console_handle syscall.Handle
	var err error

	console_handle, err = syscall.Open("CONOUT$", syscall.O_RDWR, 0)
	if err != nil {
		Errorf("Error getting console handle: %v", err)
		return
	}

	var mode uint32
	err = windows.GetConsoleMode(windows.Handle(console_handle), &mode)
	if err != nil {
		Errorf("Error getting console mode: %v", err)
		return
	}
	err = windows.SetConsoleMode(windows.Handle(console_handle), mode | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
	if err != nil {
		Errorf("Error setting console mode: %v", err)
		return
	}
}

{'repo_name': 'go-flutter-desktop/hover', 'stars': '548', 'repo_language': 'Go', 'file_name': 'linux-pkg.go', 'mime_type': 'text/plain', 'hash': 8532265549385210278, 'source_dataset': 'data'}


/*
Copyright (C) 2014, Youssef Touil <youssef@airspy.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifndef IQCONVERTER_FLOAT_H
#define IQCONVERTER_FLOAT_H

#include <stdint.h>

#define IQCONVERTER_NZEROS 2
#define IQCONVERTER_NPOLES 2

typedef struct {
	float avg;
	float hbc;
	int len;
	int fir_index;
	int delay_index;
	float *fir_kernel;
	float *fir_queue;
	float *delay_line;
} iqconverter_float_t;

iqconverter_float_t *iqconverter_float_create(const float *hb_kernel, int len);
void iqconverter_float_free(iqconverter_float_t *cnv);
void iqconverter_float_reset(iqconverter_float_t *cnv);
void iqconverter_float_process(iqconverter_float_t *cnv, float *samples, int len);

#endif // IQCONVERTER_FLOAT_H

{'repo_name': 'JvanKatwijk/qt-dab', 'stars': '127', 'repo_language': 'C++', 'file_name': 'machine_type.h', 'mime_type': 'text/x-c', 'hash': -4650766392922706918, 'source_dataset': 'data'}


@ val t = new Trie(); t.add("mango"); t.add("mandarin"); t.add("map"); t.add("man")

@ t.stringsMatchingPrefix("man")
res1: Set[String] = Set("man", "mandarin", "mango")

@ t.stringsMatchingPrefix("ma")
res2: Set[String] = Set("map", "man", "mandarin", "mango")

@ t.stringsMatchingPrefix("map")
res3: Set[String] = Set("map")

@ t.stringsMatchingPrefix("mand")
res4: Set[String] = Set("mandarin")

@ t.stringsMatchingPrefix("mando")
res5: Set[String] = Set()

{'repo_name': 'handsonscala/handsonscala', 'stars': '259', 'repo_language': 'TSQL', 'file_name': '20.58.error', 'mime_type': 'text/plain', 'hash': 6792472854478375940, 'source_dataset': 'data'}


// -----------------------------------------------------------------------
//  <copyright file="SignInManager.cs" company="OSharp开源团队">
//      Copyright (c) 2014-2015 OSharp. All rights reserved.
//  </copyright>
//  <site>http://www.osharp.org</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2015-11-05 18:47</last-date>
// -----------------------------------------------------------------------

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;

using OSharp.Core.Dependency;
using OSharp.Demo.Models.Identity;

namespace OSharp.Demo.Identity
{
    /// <summary>
    /// 用户登录管理器
    /// </summary>
    public class SignInManager : SignInManager<User, int>, IScopeDependency
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="userManager"/><param name="authenticationManager"/>
        public SignInManager(UserManager<User, int> userManager, IAuthenticationManager authenticationManager)
            : base(userManager, authenticationManager)
        { }
    }
}

{'repo_name': 'i66soft/osharp-v4', 'stars': '472', 'repo_language': 'C#', 'file_name': 'pack-osharp.bat', 'mime_type': 'text/plain', 'hash': -8763344603582074146, 'source_dataset': 'data'}


<!DOCTYPE html>

	<meta charset="utf-8">
	<link rel="shortcut icon" type="image/ico" href="http://www.datatables.net/favicon.ico">
	<meta name="viewport" content="initial-scale=1.0, maximum-scale=2.0">

	<title>Responsive example - Ajax data</title>
	<link rel="stylesheet" type="text/css" href="../../../../media/css/jquery.dataTables.css">
	<link rel="stylesheet" type="text/css" href="../../css/dataTables.responsive.css">
	<link rel="stylesheet" type="text/css" href="../../../../examples/resources/syntax/shCore.css">
	<link rel="stylesheet" type="text/css" href="../../../../examples/resources/demo.css">

Responsive example Ajax data

This example shows the Responsive extension working with Ajax sourced data in the DataTable. Note that no special
				initialisation is required. Responsive is enabled by adding the responsive class to the table element.

						Name
						Position
						Office
						Extn.
						Start date
						Salary

						Name
						Position
						Office
						Extn.
						Start date
						Salary

Javascript

HTML

CSS

Ajax

Server-side script

The Javascript shown below is used to initialise the table shown in this example:
$(document).ready(function() {
	$('#example').DataTable( {
		"ajax": "../../../../examples/ajax/data/objects.txt",
		"columns": [
			{ "data": "name" },
			{ "data": "position" },
			{ "data": "office" },
			{ "data": "extn" },
			{ "data": "start_date" },
			{ "data": "salary" }
		]
	} );
} );

In addition to the above code, the following Javascript library files are loaded for use in this example:

../../../../media/js/jquery.js

../../../../media/js/jquery.dataTables.js

../../js/dataTables.responsive.js

The HTML shown below is the raw HTML table element, before it has been enhanced by DataTables:

This example uses a little bit of additional CSS beyond what is loaded from the library files (below), in order to correctly display the table. The
						additional CSS used is shown below:
div.container { max-width: 1200px }

The following CSS library files are loaded for use in this example to provide the styling of the table:

../../../../media/css/jquery.dataTables.css

../../css/dataTables.responsive.css

This table loads data by Ajax. The latest data that has been loaded is shown below. This data will update automatically as any additional data is
					loaded.

The script used to perform the server-side processing for this table is shown below. Please note that this is just an example script using PHP. Server-side
					processing scripts can be written in any language, using the protocol described in the DataTables
					documentation.

Other examples

Basic initialisation

Class name

Configuration option

`new` constructor

Ajax data

Default initialisation

Styling

Bootstrap styling

Foundation styling

Vertical scrolling

Compact styling

Display control

Automatic column hiding

Class control

Assigned class control

With FixedHeader

Complex headers (rowspan / colspan)

Child rows

Disable child rows

Column controlled child rows

Column control - right

Whole row child row control

Custom child row renderer

Please refer to the DataTables documentation for full information about its API properties and methods.

					Additionally, there are a wide range of extras and plug-ins
					which extend the capabilities of DataTables.

DataTables designed and created by SpryMedia Ltd © 2007-2015

					DataTables is licensed under the MIT license.

{'repo_name': 'AKSHAYUBHAT/ComputationalHealthcare', 'stars': '179', 'repo_language': 'Python', 'file_name': '0009_auto_20161027_0609.py', 'mime_type': 'text/x-python', 'hash': 9136034172649564102, 'source_dataset': 'data'}


// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System;
using System.Collections.Immutable;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.LanguageServices.Implementation.CodeModel.Interop;

namespace Microsoft.VisualStudio.LanguageServices.Implementation.CodeModel.Collections
{
    internal class CodeElementSnapshot : Snapshot
    {
        private readonly ImmutableArray<EnvDTE.CodeElement> _elements;

        public CodeElementSnapshot(ICodeElements codeElements)
        {
            var count = codeElements.Count;
            var elementsBuilder = ImmutableArray.CreateBuilder<EnvDTE.CodeElement>(count);

            for (int i = 0; i < count; i++)
            {
                // We use "i + 1" since CodeModel indices are 1-based
                EnvDTE.CodeElement element;
                if (ErrorHandler.Succeeded(codeElements.Item(i + 1, out element)))
                {
                    elementsBuilder.Add(element);
                }
            }

            _elements = elementsBuilder.ToImmutable();
        }

        public CodeElementSnapshot(ImmutableArray<EnvDTE.CodeElement> elements)
        {
            _elements = elements;
        }

        public override int Count
        {
            get { return _elements.Length; }
        }

        public override EnvDTE.CodeElement this[int index]
        {
            get
            {
                if (index < 0 || index >= _elements.Length)
                {
                    throw new ArgumentOutOfRangeException("index");
                }

                return _elements[index];
            }
        }
    }
}

{'repo_name': 'mono/roslyn', 'stars': '187', 'repo_language': 'C#', 'file_name': 'VSL.Imports.Closed.targets', 'mime_type': 'text/plain', 'hash': 7582189805682504634, 'source_dataset': 'data'}


/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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
 *
 *     http://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.
 */
package org.apache.servicecomb.core.definition;

import java.util.Map;

import org.apache.servicecomb.core.SCBEngine;
import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;

public class MicroserviceVersionsMeta {
  protected SCBEngine scbEngine;

  protected MicroserviceConfig microserviceConfig;

  // key is operationMeta.getMicroserviceQualifiedName()
  private Map<String, OperationConfig> configs = new ConcurrentHashMapEx<>();

  public MicroserviceVersionsMeta(SCBEngine scbEngine, String microserviceName) {
    this.scbEngine = scbEngine;
    this.microserviceConfig = scbEngine.getPriorityPropertyManager()
        .createConfigObject(MicroserviceConfig.class, "service", microserviceName);
  }

  public MicroserviceConfig getMicroserviceConfig() {
    return microserviceConfig;
  }

  public OperationConfig getOrCreateOperationConfig(OperationMeta operationMeta) {
    return configs.computeIfAbsent(operationMeta.getMicroserviceQualifiedName(),
        name -> createOperationConfig(operationMeta));
  }

  private OperationConfig createOperationConfig(OperationMeta operationMeta) {
    boolean consumer = operationMeta.getMicroserviceMeta().isConsumer();
    return scbEngine.getPriorityPropertyManager().createConfigObject(
        OperationConfig.class,
        "op-any-priority", consumer ?
            OperationConfig.CONSUMER_OP_ANY_PRIORITY : OperationConfig.PRODUCER_OP_ANY_PRIORITY,
        "consumer-op-any_priority", OperationConfig.CONSUMER_OP_ANY_PRIORITY,
        "producer-op-any_priority", OperationConfig.PRODUCER_OP_ANY_PRIORITY,

        "op-priority", consumer ?
            OperationConfig.CONSUMER_OP_PRIORITY : OperationConfig.PRODUCER_OP_PRIORITY,
        "consumer-op-priority", OperationConfig.CONSUMER_OP_PRIORITY,
        "producer-op-priority", OperationConfig.PRODUCER_OP_PRIORITY,

        "consumer-producer", consumer ? "Consumer" : "Provider",
        "consumer-provider", consumer ? "Consumer" : "Provider",

        "service", operationMeta.getMicroserviceName(),
        "schema", operationMeta.getSchemaId(),
        "operation", operationMeta.getOperationId());
  }
}

{'repo_name': 'apache/servicecomb-java-chassis', 'stars': '1582', 'repo_language': 'Java', 'file_name': 'archetype-metadata.xml', 'mime_type': 'text/plain', 'hash': 86189669562513818, 'source_dataset': 'data'}


// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Microsoft.Bot.Schema
{
    /// <summary>
    /// A bot was installed or removed from a channel.
    /// </summary>
    public interface IInstallationUpdateActivity : IActivity
    {
        /// <summary>
        /// Gets or Sets add|remove.
        /// </summary>
        /// <value>Action.</value>
        string Action { get; set; }
    }
}

{'repo_name': 'microsoft/botbuilder-dotnet', 'stars': '553', 'repo_language': 'C#', 'file_name': 'readme.md', 'mime_type': 'text/html', 'hash': -2945767595201254615, 'source_dataset': 'data'}


var isKeyable = require('./_isKeyable');

/** Used to stand-in for `undefined` hash values. */
var HASH_UNDEFINED = '__lodash_hash_undefined__';

/**
 * Checks if `value` is in `cache`.
 *
 * @private
 * @param {Object} cache The set cache to search.
 * @param {*} value The value to search for.
 * @returns {number} Returns `true` if `value` is found, else `false`.
 */
function cacheHas(cache, value) {
  var map = cache.__data__;
  if (isKeyable(value)) {
    var data = map.__data__,
        hash = typeof value == 'string' ? data.string : data.hash;

    return hash[value] === HASH_UNDEFINED;
  }
  return map.has(value);
}

module.exports = cacheHas;

{'repo_name': 'houdunwang/video', 'stars': '323', 'repo_language': 'PHP', 'file_name': 'phpcms.sql', 'mime_type': 'text/plain', 'hash': 2539926004230988438, 'source_dataset': 'data'}


<?php

/*
 * This file is part of the Goteo Package.
 *
 * (c) Platoniq y Fundación Goteo <fundacion@goteo.org>
 *
 * For the full copyright and license information, please view the README.md
 * and LICENSE files that was distributed with this source code.
 */

namespace Goteo\Util\Form\Type;

use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 *
 * This class creates overides Date to show always as the single_text option is activated
 *
 */
class TypeaheadType extends TextType
{

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        parent::configureOptions($resolver);
        $resolver->setDefault('sources', ''); // Sources: 'channel', 'call', 'matcher', 'project', 'user', 'consultant'
        // $resolver->setDefault('engines', []); // Url for autocomplete
        // $resolver->setDefault('defaults', []); // Url for autocomplete
        $resolver->setDefault('row_class', '');
        $resolver->setDefault('text', ''); // If exists, the text shown instead of the value
        $resolver->setDefault('fake_id', ''); // Created automatically, the id of the typeahead input field (real data is placed in a hidden field)
        $resolver->setDefault('value_field', 'id'); // Field where to extract the Value from API calls, placed in the hidden field
        $resolver->setDefault('type','simple'); // Field to change between typeahead with only one input or with multiple ['simple', 'multiple']
    }

    /**
     * {@inheritdoc}
     */
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        parent::buildView($view, $form, $options);
        if(is_array($options['attr']['class'])) $view->vars['attr']['class'] = implode(' ', $view->vars['attr']['class']);
        if(empty($view->vars['fake_id'])) $view->vars['fake_id'] = 'typeahead_' . $view->vars['id'];
        $view->vars['attr']['class'] .= 'form-control typeahead';
        $view->vars['attr']['autocomplete'] = 'off';
        $view->vars['sources'] = $options['sources'];
        $view->vars['text'] = $options['text'] ? $options['text'] : $view->vars['value'];
        $view->vars['row_class'] = $options['row_class'];
        $view->vars['value_field'] = $options['value_field'];
        $view->vars['type'] = $options['type'];
    }

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

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'typeahead';
    }
}

{'repo_name': 'GoteoFoundation/goteo', 'stars': '110', 'repo_language': 'PHP', 'file_name': 'clean.js', 'mime_type': 'text/plain', 'hash': 129353721258388972, 'source_dataset': 'data'}


Dialogs
=======

.. toctree::
   :maxdepth: 1
   :glob:

   ../action_dialog
   ../action_dialog_open
   ../action_dialog_close
   ../action_overlay_open
   ../action_overlay_close

{'repo_name': 'zotonic/zotonic', 'stars': '651', 'repo_language': 'Erlang', 'file_name': 'push-connect.dot', 'mime_type': 'text/plain', 'hash': 8007434700881052164, 'source_dataset': 'data'}


/*******************************************************************************
 * Copyright (c) 2013, 2014 Lectorius, Inc.
 * Authors:
 * Vijay Pandurangan (vijayp@mitro.co)
 * Evan Jones (ej@mitro.co)
 * Adam Hilss (ahilss@mitro.co)
 *
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *     You can contact the authors at inbound@mitro.co.
 *******************************************************************************/
package co.mitro.core.servlets;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.junit.Test;

import co.mitro.core.exceptions.MitroServletException;
import co.mitro.core.server.data.DBAcl.AccessLevelType;
import co.mitro.core.server.data.DBGroup;
import co.mitro.core.server.data.DBIdentity;
import co.mitro.core.server.data.DBServerVisibleSecret;
import co.mitro.core.server.data.RPC;
import co.mitro.core.server.data.RPC.GetOrganizationStateResponse;
import co.mitro.core.server.data.RPC.ListMySecretsAndGroupKeysResponse.GroupInfo;
import co.mitro.core.servlets.MitroServlet.MitroRequestContext;

import com.google.common.collect.Sets;
import com.j256.ormlite.support.DatabaseConnection;

public class GetOrganizationStateTest extends OrganizationsFixture {

  private void insertAddSecretAuditLog(DBServerVisibleSecret secret, DBIdentity user, int timestamp)
      throws SQLException {
    manager.auditDao.getConnectionSource().getReadWriteConnection().executeStatement(
        "INSERT into audit (uid, target_sv_secret, \"timestampMs\", action) VALUES("
        + user.getId() + ", " + secret.getId() + ", " + timestamp + ", 'ADD_SECRET');",
        DatabaseConnection.DEFAULT_RESULT_FLAGS);
  }

  @Test
  public void testGetOrgData() throws SQLException, MitroServletException, IOException {
    // Must be a multiple of 1000:
    final int orphanedSecretTimestamp  = 2000;
    insertAddSecretAuditLog(orgSecret, testIdentity, 0);
    insertAddSecretAuditLog(orphanedOrgSecret, testIdentity2, orphanedSecretTimestamp);
    getOrgStateResponse(testIdentity);

    assertEquals(admins.size() + members.size(), gosResponse.members.size());
    Set<String> emails = Sets.newHashSet();
    final Set<String> adminEmails = Sets.newHashSet();
    for (DBIdentity i : members) {
      emails.add(i.getName());
    }
    for (DBIdentity i : admins) {
      emails.add(i.getName());
      adminEmails.add(i.getName());
    }
    List<String> sortedEmails = new ArrayList<>(emails);
    Collections.sort(sortedEmails);
    Collections.sort(gosResponse.members);
    assertEquals(sortedEmails, gosResponse.members);
    assertEquals(emails.size() + 1, gosResponse.groups.size());
    assertEquals(1, gosResponse.organizations.size());
    for (GroupInfo g: gosResponse.groups.values()) {
      assertFalse(g.autoDelete);
      assertFalse(g.isNonOrgPrivateGroup);
      if (g.isTopLevelOrg) {
        assertFalse(g.isOrgPrivateGroup);
        assertEquals(adminEmails, Sets.newHashSet(g.users));
        assertEquals(gosRequest.orgId, g.groupId);
      } else {
        assertTrue(g.isOrgPrivateGroup);
        assertEquals(org.getId(), g.owningOrgId.intValue());
        assertEquals(1, g.users.size());
        // make sure each user appears exactly once
        assertTrue(sortedEmails.remove(g.users.get(0)));
        // TODO: check that ONLY the requestor's private group has encryptedPrivateKey.
      }
    }
    assertTrue(sortedEmails.isEmpty());
    assertEquals(2, gosResponse.orgSecretsToPath.size());
    assertNotNull(gosResponse.orgSecretsToPath.get(orgSecret.getId()));
    assertEquals(0, gosResponse.orphanedSecretsToPath.size());

    for (RPC.Secret s : gosResponse.orgSecretsToPath.values()) {
      // TODO: Disabled due to slowness. See comment in GetOrganizationState
//      assertNotNull(s.lastModified);
//      assertEquals(testIdentity.getName(), s.lastModified.userName);
//      assertEquals(0, s.lastModified.timestampSec);
      assertNull(s.lastModified);
    }
    for (RPC.Secret s : gosResponse.orphanedSecretsToPath.values()) {
//      assertNotNull(s.lastModified);
//      assertEquals(orphanedSecretTimestamp/1000, s.lastModified.timestampSec);
//      assertEquals(testIdentity2.getName(), s.lastModified.userName);
      assertNull(s.lastModified);
    }
  }

  @Test
  public void getAsMember() throws Exception {
    // Originally getOrgState was forbidden to regular users.
    // Now used to get list of members and groups.
    DBIdentity member = members.iterator().next();
    getOrgStateResponse(member);

    // doesn't return secrets for non-admins (not needed)
    assertNull(gosResponse.orgSecretsToPath);
    assertNull(gosResponse.orphanedSecretsToPath);
    assertNull(gosResponse.organizations);

    assertEquals(8, gosResponse.admins.size());
    assertTrue(gosResponse.admins.contains(testIdentity.getName()));
    assertFalse(gosResponse.admins.contains(member.getName()));

    // members contains both admins and regular members
    assertEquals(15, gosResponse.members.size());
    assertTrue(gosResponse.members.contains(testIdentity.getName()));
    assertTrue(gosResponse.members.contains(member.getName()));

    assertEquals(16, gosResponse.groups.size());
    for (GroupInfo group : gosResponse.groups.values()) {
      assertNull(group.users);
      assertNull(group.encryptedPrivateKey);
      assertFalse(group.isNonOrgPrivateGroup);
      assertFalse(group.isRequestorAnOrgAdmin);
    }
  }

  @Test
  public void orgGroupDontMakeMembers() throws Exception {
    // Add a named group to the organization. The members are not automatically part of the org
    DBGroup namedGroup = createGroupContainingIdentity(testIdentity2);
    addOrgToGroup(manager, org, namedGroup, AccessLevelType.ADMIN);

    // testIdentity2 still can't call getorgstate
    expectException(testIdentity2);

    getOrgStateResponse(testIdentity);
    assertFalse(gosResponse.members.contains(testIdentity2.getName()));
  }

  @Test
  public void invalidGets() throws Exception {
    // non-member
    expectException(testIdentity2);

    // get non-org group
    gosRequest.orgId = testGroup.getId();
    expectException(testIdentity2);

    // get non-existent group
    gosRequest.orgId = -1;
    expectException(testIdentity2);
  }

  private void expectException(DBIdentity identity) throws IOException, SQLException {
    try {
      getOrgStateResponse(identity);
      fail("expected exception");
    } catch (MitroServletException expected) {
      assertThat(expected.getMessage(), containsString("no access"));
    }
  }

  private void getOrgStateResponse(DBIdentity identity) throws IOException, SQLException,
      MitroServletException {
    gosResponse = (GetOrganizationStateResponse) gosServlet.processCommand(
        new MitroRequestContext(identity, gson.toJson(gosRequest), manager, null));
  }
}

{'repo_name': 'mitro-co/mitro', 'stars': '1688', 'repo_language': 'HTML', 'file_name': 'pkg_info', 'mime_type': 'text/plain', 'hash': -1916249811870509939, 'source_dataset': 'data'}


// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright 2014, Michael Ellerman, IBM Corp.
 */

#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>

#include "ebb.h"

/*
 * Test that tries to trigger CPU_FTR_PMAO_BUG. Which is a hardware defect
 * where an exception triggers but we context switch before it is delivered and
 * lose the exception.
 */

static int test_body(void)
{
	int i, orig_period, max_period;
	struct event event;

	SKIP_IF(!ebb_is_supported());

	/* We use PMC4 to make sure the kernel switches all counters correctly */
	event_init_named(&event, 0x40002, "instructions");
	event_leader_ebb_init(&event);

	event.attr.exclude_kernel = 1;
	event.attr.exclude_hv = 1;
	event.attr.exclude_idle = 1;

	FAIL_IF(event_open(&event));

	ebb_enable_pmc_counting(4);
	setup_ebb_handler(standard_ebb_callee);
	ebb_global_enable();
	FAIL_IF(ebb_event_enable(&event));

	/*
	 * We want a low sample period, but we also want to get out of the EBB
	 * handler without tripping up again.
	 *
	 * This value picked after much experimentation.
	 */
	orig_period = max_period = sample_period = 400;

	mtspr(SPRN_PMC4, pmc_sample_period(sample_period));

	while (ebb_state.stats.ebb_count < 1000000) {
		/*
		 * We are trying to get the EBB exception to race exactly with
		 * us entering the kernel to do the syscall. We then need the
		 * kernel to decide our timeslice is up and context switch to
		 * the other thread. When we come back our EBB will have been
		 * lost and we'll spin in this while loop forever.
		 */

		for (i = 0; i < 100000; i++)
			sched_yield();

		/* Change the sample period slightly to try and hit the race */
		if (sample_period >= (orig_period + 200))
			sample_period = orig_period;
		else
			sample_period++;

		if (sample_period > max_period)
			max_period = sample_period;
	}

	ebb_freeze_pmcs();
	ebb_global_disable();

	count_pmc(4, sample_period);
	mtspr(SPRN_PMC4, 0xdead);

	dump_summary_ebb_state();
	dump_ebb_hw_state();

	event_close(&event);

	FAIL_IF(ebb_state.stats.ebb_count == 0);

	/* We vary our sample period so we need extra fudge here */
	FAIL_IF(!ebb_check_count(4, orig_period, 2 * (max_period - orig_period)));

	return 0;
}

static int lost_exception(void)
{
	return eat_cpu(test_body);
}

int main(void)
{
	test_harness_set_timeout(300);
	return test_harness(lost_exception, "lost_exception");
}

{'repo_name': 'endlessm/linux', 'stars': '241', 'repo_language': 'C', 'file_name': 'intel_pipe_crc.h', 'mime_type': 'text/x-c', 'hash': -3363059388166758753, 'source_dataset': 'data'}