text:
package com.opengamma.analytics.financial.provider.curve.inflationissuer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import com.google.common.collect.LinkedListMultimap;
import com.opengamma.analytics.financial.curve.interestrate.generator.GeneratorCurve;
import com.opengamma.analytics.financial.instrument.index.IborIndex;
import com.opengamma.analytics.financial.instrument.index.IndexON;
import com.opengamma.analytics.financial.instrument.index.IndexPrice;
import com.opengamma.analytics.financial.interestrate.InstrumentDerivative;
import com.opengamma.analytics.financial.interestrate.InstrumentDerivativeVisitor;
import com.opengamma.analytics.financial.legalentity.LegalEntity;
import com.opengamma.analytics.financial.legalentity.LegalEntityFilter;
import com.opengamma.analytics.financial.provider.curve.CurveBuildingBlock;
import com.opengamma.analytics.financial.provider.curve.CurveBuildingBlockBundle;
import com.opengamma.analytics.financial.provider.curve.MultiCurveBundle;
import com.opengamma.analytics.financial.provider.curve.SingleCurveBundle;
import com.opengamma.analytics.financial.provider.description.inflation.InflationIssuerProviderDiscount;
import com.opengamma.analytics.financial.provider.description.inflation.ParameterInflationIssuerProviderInterface;
import com.opengamma.analytics.financial.provider.sensitivity.inflation.InflationSensitivity;
import com.opengamma.analytics.financial.provider.sensitivity.inflationissuer.ParameterSensitivityInflationIssuerMatrixCalculator;
import com.opengamma.analytics.financial.provider.sensitivity.inflationissuer.ParameterSensitivityInflationIssuerUnderlyingMatrixCalculator;
import com.opengamma.analytics.math.function.Function1D;
import com.opengamma.analytics.math.linearalgebra.DecompositionFactory;
import com.opengamma.analytics.math.matrix.CommonsMatrixAlgebra;
import com.opengamma.analytics.math.matrix.DoubleMatrix1D;
import com.opengamma.analytics.math.matrix.DoubleMatrix2D;
import com.opengamma.analytics.math.matrix.MatrixAlgebra;
import com.opengamma.analytics.math.rootfinding.newton.BroydenVectorRootFinder;
import com.opengamma.util.ArgumentChecker;
import com.opengamma.util.money.Currency;
import com.opengamma.util.tuple.ObjectsPair;
import com.opengamma.util.tuple.Pair;
import com.opengamma.util.tuple.Pairs;

/**
 *
 */
public class InflationIssuerDiscountBuildingRepository {

  /**
   * The absolute tolerance for the root finder.
   */
  private final double _toleranceAbs;
  /**
   * The relative tolerance for the root finder.
   */
  private final double _toleranceRel;
  /**
   * The mximum number of steps for the root finder.
   */
  private final int _stepMaximum;

  /**
   * The root finder used for curve calibration.
   */
  private final BroydenVectorRootFinder _rootFinder;
  /**
   * The matrix algebra used for matrix inversion.
   */
  private static final MatrixAlgebra MATRIX_ALGEBRA = new CommonsMatrixAlgebra();

  /**.+-
   * Constructor.
   * @param toleranceAbs The absolute tolerance for the root finder.
   * @param toleranceRel The relative tolerance for the root finder.
   * @param stepMaximum The maximum number of step for the root finder.
   */
  public InflationIssuerDiscountBuildingRepository(final double toleranceAbs, final double toleranceRel, final int stepMaximum) {
    _toleranceAbs = toleranceAbs;
    _toleranceRel = toleranceRel;
    _stepMaximum = stepMaximum;
    _rootFinder = new BroydenVectorRootFinder(_toleranceAbs, _toleranceRel, _stepMaximum, DecompositionFactory.getDecomposition(DecompositionFactory.SV_COLT_NAME));
    // TODO: make the root finder flexible.
    // TODO: create a way to select the SensitivityMatrixMulticurve calculator (with underlying curve or not)
  }

  /**
   * Build a unit of curves without the discount curve.
   * @param instruments The instruments used for the unit calibration.
   * @param initGuess The initial parameters guess.
   * @param knownData The known data (fx rates, other curves, model parameters, ...)
   * @param inflationMap The inflation curves names map.
   * @param generatorsMap The generators map.
   * @param calculator The calculator of the value on which the calibration is done (usually ParSpreadMarketQuoteCalculator (recommended) or converted present value).
   * @param sensitivityCalculator The parameter sensitivity calculator.
   * @return The new curves and the calibrated parameters.
   */
  private InflationIssuerProviderDiscount makeUnit(final InstrumentDerivative[] instruments, final double[] initGuess,
      final InflationIssuerProviderDiscount knownData,
      LinkedHashMap<String, Currency> dscMap, LinkedHashMap<String, IndexON[]> fwdOnMap,
      LinkedHashMap<String, IborIndex[]> fwdIborMap, LinkedHashMap<String, IndexPrice[]> priceMap,
      LinkedListMultimap<String, Pair<Object, LegalEntityFilter<LegalEntity>>> issuerMap,
      final LinkedHashMap<String, GeneratorCurve> generatorsMap,
      final InstrumentDerivativeVisitor<ParameterInflationIssuerProviderInterface, Double> calculator,
      final InstrumentDerivativeVisitor<ParameterInflationIssuerProviderInterface, InflationSensitivity> sensitivityCalculator) {
    final GeneratorInflationIssuerProviderDiscount generator =
        new GeneratorInflationIssuerProviderDiscount(knownData, dscMap, fwdOnMap, priceMap, issuerMap, generatorsMap);
    final InflationIssuerDiscountBuildingData data = new InflationIssuerDiscountBuildingData(instruments, generator);
    final Function1D<DoubleMatrix1D, DoubleMatrix1D> curveCalculator =
        new InflationIssuerDiscountFinderFunction(calculator, data);
    final Function1D<DoubleMatrix1D, DoubleMatrix2D> jacobianCalculator =
        new InflationIssuerDiscountFinderJacobian(new ParameterSensitivityInflationIssuerMatrixCalculator(sensitivityCalculator),
        data);
    final double[] parameters = _rootFinder.getRoot(curveCalculator, jacobianCalculator, new DoubleMatrix1D(initGuess)).getData();
    final InflationIssuerProviderDiscount newCurves = data.getGeneratorMarket().evaluate(new DoubleMatrix1D(parameters));
    return newCurves;
  }

  /**
   * Construct the CurveBuildingBlock associated to all the curve built so far and updates the CurveBuildingBlockBundle.
   * @param instruments The instruments used for the block calibration.
   * @param multicurves The known curves including the current unit.
   * @param currentCurvesArray
   * @param blockBundle
   * @param sensitivityCalculator The parameter sensitivity calculator for the value on which the calibration is done
  (usually ParSpreadMarketQuoteDiscountingProviderCalculator (recommended) or converted present value).
   * @return The part of the inverse Jacobian matrix associated to each curve. Only the part for the curve in the current unit (not the previous units).
   * The Jacobian matrix is the transition matrix between the curve parameters and the par spread.
   */
  private void updateBlockBundle(final InstrumentDerivative[] instruments, final InflationIssuerProviderDiscount multicurves, final List<String> currentCurvesList,
      final CurveBuildingBlockBundle blockBundle, final InstrumentDerivativeVisitor<ParameterInflationIssuerProviderInterface, InflationSensitivity> sensitivityCalculator) {
    // Sensitivity calculator
    final ParameterSensitivityInflationIssuerUnderlyingMatrixCalculator parameterSensitivityCalculator =
        new ParameterSensitivityInflationIssuerUnderlyingMatrixCalculator(sensitivityCalculator);
    int loopc;
    final LinkedHashMap<String, Pair<Integer, Integer>> mapBlockOut = new LinkedHashMap<>();
    // Curve names manipulation
    final Set<String> allCurveName1 = multicurves.getAllNames();
    final int nbCurrentCurves = currentCurvesList.size();
    final LinkedHashSet<String> currentCurves = new LinkedHashSet<>(currentCurvesList);
    final LinkedHashSet<String> beforeCurveName = new LinkedHashSet<>(allCurveName1);
    beforeCurveName.removeAll(currentCurves);
    final LinkedHashSet<String> allCurveName = new LinkedHashSet<>(beforeCurveName);
    allCurveName.addAll(currentCurves); // Manipulation to ensure that the new curves are at the end.
    //Implementation note : if blockBundle don't contain a block for a specific curve then we remove this curve from  beforeCurveName.
    //Because we can't compute the total bundle without the block for each curve.
    final LinkedHashSet<String> withoutBlockCurveName = new LinkedHashSet<>();
    for (final String name : beforeCurveName) {
      if (!(blockBundle.getData().containsKey(name))) {
        withoutBlockCurveName.add(name);
      }
    }
    beforeCurveName.removeAll(withoutBlockCurveName);
    final int nbAllCurve = allCurveName.size();
    final int nbBeforeCurves = nbAllCurve - nbCurrentCurves;
    // Current curves size and nb parameters
    int nbParametersCurrentTotal = 0;
    final int[] nbParametersCurrent = new int[nbCurrentCurves];
    final int[] startIndexCurrent = new int[nbCurrentCurves];
    loopc = 0;
    final LinkedHashSet<String> currentBeforeCurveNames = new LinkedHashSet<>(beforeCurveName);
    for (final String name : currentCurves) {
      startIndexCurrent[loopc] = nbParametersCurrentTotal;
      nbParametersCurrent[loopc] = multicurves.getNumberOfIntrinsicParameters(name, currentBeforeCurveNames);
//      nbParametersCurrent[loopc] = multicurves.getInflationProvider().getCurve(name).getNumberOfIntrinsicParameters(currentBeforeCurveNames);
      currentBeforeCurveNames.add(name);
      nbParametersCurrentTotal += nbParametersCurrent[loopc];
      loopc++;
    }
    // Sensitivity to parameters
    final int nbIns = instruments.length;
    final double[][] res = new double[nbParametersCurrentTotal][];
    for (int loopinstrument = 0; loopinstrument < nbIns; loopinstrument++) {
      res[loopinstrument] = parameterSensitivityCalculator.calculateSensitivity(instruments[loopinstrument], multicurves, allCurveName).getData();
      // The sensitivity is to all parameters in the order provided by the allCurveName
    }

    final int nbParametersAllCurvesTotal = res[0].length;
    // Jacobian direct
    final int nbParametersBeforeTotal = nbParametersAllCurvesTotal - nbParametersCurrentTotal;
    final double[][] direct = new double[nbParametersCurrentTotal][nbParametersCurrentTotal];
    for (int loopp = 0; loopp < nbIns; loopp++) {
      System.arraycopy(res[loopp], nbParametersBeforeTotal, direct[loopp], 0, nbParametersCurrentTotal);
    }
    final DoubleMatrix2D pDmCurrentMatrix = MATRIX_ALGEBRA.getInverse(new DoubleMatrix2D(direct));
    // Jacobian indirect: when nbBefor
    double[][] pDmBeforeArray = new double[0][0];
    if (nbParametersBeforeTotal > 0) {
      final double[][] nonDirect = new double[nbParametersCurrentTotal][nbParametersBeforeTotal];
      for (int loopp = 0; loopp < nbIns; loopp++) {
        System.arraycopy(res[loopp], 0, nonDirect[loopp], 0, nbParametersBeforeTotal);
      }
      final DoubleMatrix2D pDpBeforeMatrix;
      pDpBeforeMatrix = (DoubleMatrix2D) MATRIX_ALGEBRA.scale(MATRIX_ALGEBRA.multiply(pDmCurrentMatrix, new DoubleMatrix2D(nonDirect)), -1.0);
      // All curves: order and size
      final int[] nbParametersBefore = new int[nbBeforeCurves];
      final int[] startIndexBefore = new int[nbBeforeCurves];
      int tempNbParam = 0;
      loopc = 0;
      for (final String name : beforeCurveName) {
        nbParametersBefore[loopc] = multicurves.getNumberOfParameters(name);
        startIndexBefore[loopc] = tempNbParam;
        tempNbParam += nbParametersBefore[loopc];
        loopc++;
      }
      // Transition Matrix: all curves before current
      final double[][] transition = new double[nbParametersBeforeTotal][nbParametersBeforeTotal];
      loopc = 0;
      int loopc2 = 0;
      for (final String name : beforeCurveName) { // l
        final Pair<CurveBuildingBlock, DoubleMatrix2D> thisPair = blockBundle.getBlock(name);
        final CurveBuildingBlock thisBlock = thisPair.getFirst();
        final Set<String> thisBlockCurves = thisBlock.getAllNames();
        final double[][] thisMatrix = thisPair.getSecond().getData();
        loopc2 = 0;
        for (final String name2 : beforeCurveName) { // k
          if (thisBlockCurves.contains(name2)) { // If not, the matrix stay with 0
            final Integer start = thisBlock.getStart(name2);
            for (int loopp = 0; loopp < nbParametersBefore[loopc]; loopp++) {
              System.arraycopy(thisMatrix[loopp], start, transition[startIndexBefore[loopc] + loopp], startIndexBefore[loopc2], thisBlock.getNbParameters(name2));
            }
          }
          loopc2++;
        }
        loopc++;
      }
      final DoubleMatrix2D transitionMatrix = new DoubleMatrix2D(transition);
      DoubleMatrix2D pDmBeforeMatrix;
      pDmBeforeMatrix = (DoubleMatrix2D) MATRIX_ALGEBRA.multiply(pDpBeforeMatrix, transitionMatrix);
      pDmBeforeArray = pDmBeforeMatrix.getData();
      loopc = 0;
      for (final String name : beforeCurveName) {
        mapBlockOut.put(name, ObjectsPair.of((Integer) startIndexBefore[loopc], (Integer) nbParametersBefore[loopc]));
        loopc++;
      }
    }
    loopc = 0;
    for (final String name : currentCurves) {
      mapBlockOut.put(name, ObjectsPair.of((Integer) (nbParametersBeforeTotal + startIndexCurrent[loopc]), (Integer) nbParametersCurrent[loopc]));
      loopc++;
    }
    final CurveBuildingBlock blockOut = new CurveBuildingBlock(mapBlockOut);
    final double[][] pDmCurrentArray = pDmCurrentMatrix.getData();
    loopc = 0;
    for (final String name : currentCurves) {
      final double[][] pDmCurveArray = new double[nbParametersCurrent[loopc]][nbParametersAllCurvesTotal];
      for (int loopp = 0; loopp < nbParametersCurrent[loopc]; loopp++) {
        System.arraycopy(pDmCurrentArray[startIndexCurrent[loopc] + loopp], 0, pDmCurveArray[loopp], nbParametersBeforeTotal, nbParametersCurrentTotal);
      }
      if (nbParametersBeforeTotal > 0) {
        for (int loopp = 0; loopp < nbParametersCurrent[loopc]; loopp++) {
          System.arraycopy(pDmBeforeArray[startIndexCurrent[loopc] + loopp], 0, pDmCurveArray[loopp], 0, nbParametersBeforeTotal);
        }
      }
      final DoubleMatrix2D pDmCurveMatrix = new DoubleMatrix2D(pDmCurveArray);
      blockBundle.add(name, blockOut, pDmCurveMatrix);
      loopc++;
    }
  }

  /**
   * Build a block of curves without the discount curve.
   * @param curveBundles The curve bundles, not null
   * @param knownData The known data (fx rates, other curves, model parameters, ...)
   * @param priceMap The inflation curves names map.
   * @param calculator The calculator of the value on which the calibration is done (usually ParSpreadInflationMarketQuoteDiscountingCalculator (recommended) or converted present value).
   * @param sensitivityCalculator The parameter sensitivity calculator.
   * @return A pair with the calibrated yield curve bundle (including the known data) and the CurveBuildingBlckBundle with the relevant inverse Jacobian Matrix.
   */
  public Pair<InflationIssuerProviderDiscount, CurveBuildingBlockBundle> makeCurvesFromDerivatives(
      final MultiCurveBundle<GeneratorCurve>[] curveBundles,
      final InflationIssuerProviderDiscount knownData,
      LinkedHashMap<String, IndexPrice[]> priceMap,
      final InstrumentDerivativeVisitor<ParameterInflationIssuerProviderInterface, Double> calculator,
      final InstrumentDerivativeVisitor<ParameterInflationIssuerProviderInterface, InflationSensitivity> sensitivityCalculator) {
    LinkedListMultimap<String, Pair<Object, LegalEntityFilter<LegalEntity>>> issuerMap = LinkedListMultimap.create();
    return makeCurvesFromDerivatives(curveBundles, knownData, new LinkedHashMap<String, Currency>(),
        new LinkedHashMap<String, IndexON[]>(), new LinkedHashMap<String, IborIndex[]>(), priceMap,
        issuerMap, calculator, sensitivityCalculator);
  }

  /**
   * Build a block of curves without the discount curve.
   * @param curveBundles The curve bundles, not null
   * @param knownData The known data (fx rates, other curves, model parameters, ...)
   * @param dscMap The discounting
   * @param fwdOnMap Forward ON
   * @param fwdIborMap Forward Ibor
   * @param priceMap The inflation curves names map.
   * @param issuerMap Issuer
   * @param calculator The calculator of the value on which the calibration is done (usually ParSpreadInflationMarketQuoteDiscountingCalculator (recommended) or converted present value).
   * @param sensitivityCalculator The parameter sensitivity calculator.
   * @return A pair with the calibrated yield curve bundle (including the known data) and the CurveBuildingBlckBundle with the relevant inverse Jacobian Matrix.
   */
  public Pair<InflationIssuerProviderDiscount, CurveBuildingBlockBundle> makeCurvesFromDerivatives(
      final MultiCurveBundle<GeneratorCurve>[] curveBundles,
      final InflationIssuerProviderDiscount knownData,
      LinkedHashMap<String, Currency> dscMap, LinkedHashMap<String, IndexON[]> fwdOnMap,
      LinkedHashMap<String, IborIndex[]> fwdIborMap, LinkedHashMap<String, IndexPrice[]> priceMap,
      LinkedListMultimap<String, Pair<Object, LegalEntityFilter<LegalEntity>>> issuerMap,
      final InstrumentDerivativeVisitor<ParameterInflationIssuerProviderInterface, Double> calculator,
      final InstrumentDerivativeVisitor<ParameterInflationIssuerProviderInterface, InflationSensitivity> sensitivityCalculator) {
    return makeCurvesFromDerivatives(curveBundles, knownData, new CurveBuildingBlockBundle(), dscMap, fwdOnMap,
        fwdIborMap, priceMap, issuerMap, calculator, sensitivityCalculator);
  }

  public Pair<InflationIssuerProviderDiscount, CurveBuildingBlockBundle> makeCurvesFromDerivatives(
      final MultiCurveBundle<GeneratorCurve>[] curveBundles,
      final InflationIssuerProviderDiscount knownData, final CurveBuildingBlockBundle knownBlockBundle,
      LinkedHashMap<String, IndexPrice[]> priceMap,
      final InstrumentDerivativeVisitor<ParameterInflationIssuerProviderInterface, Double> calculator,
      final InstrumentDerivativeVisitor<ParameterInflationIssuerProviderInterface, InflationSensitivity> sensitivityCalculator) {
    LinkedListMultimap<String, Pair<Object, LegalEntityFilter<LegalEntity>>> issuerMap = LinkedListMultimap.create();
    return makeCurvesFromDerivatives(curveBundles, knownData, knownBlockBundle, new LinkedHashMap<String, Currency>(),
        new LinkedHashMap<String, IndexON[]>(), new LinkedHashMap<String, IborIndex[]>(), priceMap, issuerMap,
        calculator, sensitivityCalculator);
  }

  /**
   * Build a block of curves without the discount curve.
   * @param curveBundles The curve bundles, not null
   * @param knownData The known data (fx rates, other curves, model parameters, ...)
   * @param knownBlockBundle The already build CurveBuildingBlockBundle. This should contain all the bundles corresponding to the curves in the knownData.
   * @param dscMap The discounting
   * @param fwdOnMap Forward ON
   * @param fwdIborMap Forward Ibor
   * @param priceMap The inflation curves names map.
   * @param issuerMap Issuer
   * @param calculator The calculator of the value on which the calibration is done (usually ParSpreadInflationMarketQuoteDiscountingCalculator (recommended) or converted present value).
   * @param sensitivityCalculator The parameter sensitivity calculator.
   * @return A pair with the calibrated yield curve bundle (including the known data) and the CurveBuildingBlckBundle with the relevant inverse Jacobian Matrix.
   */
  public Pair<InflationIssuerProviderDiscount, CurveBuildingBlockBundle> makeCurvesFromDerivatives(
      final MultiCurveBundle<GeneratorCurve>[] curveBundles,
      final InflationIssuerProviderDiscount knownData, final CurveBuildingBlockBundle knownBlockBundle,
      LinkedHashMap<String, Currency> dscMap, LinkedHashMap<String, IndexON[]> fwdOnMap,
      LinkedHashMap<String, IborIndex[]> fwdIborMap, LinkedHashMap<String, IndexPrice[]> priceMap,
      LinkedListMultimap<String, Pair<Object, LegalEntityFilter<LegalEntity>>> issuerMap,
      final InstrumentDerivativeVisitor<ParameterInflationIssuerProviderInterface, Double> calculator,
      final InstrumentDerivativeVisitor<ParameterInflationIssuerProviderInterface, InflationSensitivity> sensitivityCalculator) {
    ArgumentChecker.notNull(curveBundles, "curve bundles");
    ArgumentChecker.notNull(knownData, "known data");
    ArgumentChecker.notNull(priceMap, "inflation map");
    ArgumentChecker.notNull(calculator, "calculator");
    ArgumentChecker.notNull(sensitivityCalculator, "sensitivity calculator");
    final int nbUnits = curveBundles.length;
    InflationIssuerProviderDiscount knownSoFarData = knownData.copy();
    final CurveBuildingBlockBundle totalBundle = new CurveBuildingBlockBundle();
    totalBundle.addAll(knownBlockBundle);

    final List<InstrumentDerivative> instrumentsSoFar = new ArrayList<>();
    final LinkedHashMap<String, GeneratorCurve> generatorsSoFar = new LinkedHashMap<>();
    final LinkedHashMap<String, Pair<Integer, Integer>> unitMap = new LinkedHashMap<>();
    int startUnit = 0;

    for (int iUnits = 0; iUnits < nbUnits; iUnits++) {
      final MultiCurveBundle<GeneratorCurve> curveBundle = curveBundles[iUnits];
      final int nbCurve = curveBundle.size();
      final int[] startCurve = new int[nbCurve]; // First parameter index of the curve in the unit.
      final LinkedHashMap<String, GeneratorCurve> generators = new LinkedHashMap<>();
      final int[] nbIns = new int[curveBundle.getNumberOfInstruments()];
      int nbInsUnit = 0; // Number of instruments in the unit.
      for (int iCurve = 0; iCurve < nbCurve; iCurve++) {
        final SingleCurveBundle<GeneratorCurve> singleCurve = curveBundle.getCurveBundle(iCurve);
        startCurve[iCurve] = nbInsUnit;
        nbIns[iCurve] = singleCurve.size();
        nbInsUnit += nbIns[iCurve];
        instrumentsSoFar.addAll(Arrays.asList(singleCurve.getDerivatives()));
      }
      final InstrumentDerivative[] instrumentsUnit = new InstrumentDerivative[nbInsUnit];
      final double[] parametersGuess = new double[nbInsUnit];
      for (int iCurve = 0; iCurve < nbCurve; iCurve++) {
        final SingleCurveBundle<GeneratorCurve> singleCurve = curveBundle.getCurveBundle(iCurve);
        final InstrumentDerivative[] derivatives = singleCurve.getDerivatives();
        System.arraycopy(derivatives, 0, instrumentsUnit, startCurve[iCurve], nbIns[iCurve]);
        System.arraycopy(singleCurve.getStartingPoint(), 0, parametersGuess, startCurve[iCurve], nbIns[iCurve]);
        final GeneratorCurve tmp = singleCurve.getCurveGenerator().finalGenerator(derivatives);
        final String curveName = singleCurve.getCurveName();
        generators.put(curveName, tmp);
        generatorsSoFar.put(curveName, tmp);
        unitMap.put(curveName, Pairs.of(startUnit + startCurve[iCurve], nbIns[iCurve]));
      }
      knownSoFarData = makeUnit(instrumentsUnit, parametersGuess, knownSoFarData, dscMap, fwdOnMap, fwdIborMap,
          priceMap, issuerMap, generators, calculator,
          sensitivityCalculator);
      updateBlockBundle(instrumentsUnit, knownSoFarData, curveBundle.getNames(), totalBundle, sensitivityCalculator);
      startUnit = startUnit + nbInsUnit;
    }

    return Pairs.of(knownSoFarData, totalBundle);
  }
}


text:
apEvent::~apEvent()
{
}

// ---------------------------------------------------------------------------
// Name:        apEvent::isEventObject
// Description: Returns true iff this is a sub-class of apEvent.
// Author:  AMD Developer Tools Team
// Date:        12/8/2009
// Implementation notes:
//   By implementing it in apEvent, all sub-classes inherit this implementation
//   that answers "true".
// ---------------------------------------------------------------------------
bool apEvent::isEventObject() const
{
    return true;
}

// ---------------------------------------------------------------------------
// Name:        apEvent::writeSelfIntoChannel
// Description: Write into the channel all the members of apEvent. Note that subclasses
//              must still override this function (and use this function in the
//              overriding function).
// Return Val: bool  - Success / failure.
// Author:  AMD Developer Tools Team
// Date:        16/8/2009
// ---------------------------------------------------------------------------
bool apEvent::writeSelfIntoChannel(osChannel& ipcChannel) const
{
    ipcChannel << (gtUInt64)_triggeringThreadId;

    return true;
}

// ---------------------------------------------------------------------------
// Name:        apEvent::readSelfFromChannel
// Description: Read from the channel all the members of apEvent. Note that subclasses
//              must still override this function (and use this function in the
//              overriding function).
// Return Val: bool  - Success / failure.
// Author:  AMD Developer Tools Team
// Date:        16/8/2009
// ---------------------------------------------------------------------------
bool apEvent::readSelfFromChannel(osChannel& ipcChannel)
{
    gtUInt64 triggeringThreadIdAsUInt64 = (gtUInt64)OS_NO_THREAD_ID;
    ipcChannel >> triggeringThreadIdAsUInt64;
    _triggeringThreadId = (osThreadId)triggeringThreadIdAsUInt64;

    return true;
}

redpajama_set_name: RedPajamaGithub


text:
module.exports = function(grunt) {
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-release');

    grunt.initConfig({
        jshint: {
            all: ['index.js'],
            options: {
                jshintrc: '.jshintrc'
            }
        },

        watch: {
            all: {
                files: ['index.js'],
                tasks: ['build']
            }
        }
    });

    grunt.registerTask('default', ['build']);
    grunt.registerTask('build', ['jshint']);
    grunt.registerTask('package', ['build', 'release']);
};


text:
package de.intarsys.tools.tag;

import de.intarsys.tools.string.StringTools;

/**
 * A simple type/key/value pair that can be attached to POJO's.
 *

 * Tags can be created using strings of the format

 *
 * [type#]key[=value]?[;[type#]key[=value]?]*
 *  or
 * [type#]key[:value]?[;[type#]key[:value]?]*
 *
 *
 */
public class TypedTag extends Tag {

	public static TypedTag[] create(String tagString) {
		String[] tagStrings = tagString.split("\\;"); //$NON-NLS-1$
		TypedTag[] tags = new TypedTag[tagStrings.length];
		for (int i = 0; i < tagStrings.length; i++) {
			String[] split = tagStrings[i].split("[\\:\\=]", 2); //$NON-NLS-1$
			String[] keysplit = split[0].split("\\#"); //$NON-NLS-1$
			if (keysplit.length > 1) {
				if (split.length > 1) {
					tags[i] = new TypedTag(keysplit[0], keysplit[1], split[1]);
				} else {
					tags[i] = new TypedTag(keysplit[0], keysplit[1], ""); //$NON-NLS-1$
				}
			} else {
				if (split.length > 1) {
					tags[i] = new TypedTag(split[0], split[1]);
				} else {
					tags[i] = new TypedTag(split[0]);
				}
			}
		}
		return tags;
	}

	final private String type;

	public TypedTag(String key) {
		super(key);
		this.type = ""; //$NON-NLS-1$
	}

	public TypedTag(String key, String value) {
		super(key, value);
		this.type = ""; //$NON-NLS-1$
	}

	public TypedTag(String type, String key, String value) {
		super(key, value);
		this.type = type;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof TypedTag) {
			TypedTag in = (TypedTag) obj;
			return in.type.equals(this.type)
					&& in.getValue().equals(this.getValue())
					&& in.getKey().equals(this.getKey());
		}
		return false;
	}

	public String getType() {
		return type;
	}

	@Override
	public int hashCode() {
		return type.hashCode() + super.hashCode();
	}

	@Override
	public String toString() {
		if (StringTools.isEmpty(type)) {
			return super.toString();
		} else {
			if (StringTools.isEmpty(getValue())) {
				return type + '#' + getKey();
			} else {
				return type + '#' + getKey() + '=' + getValue();
			}
		}
	}

}


text:
local rectangle = require("src.math.rectangle")
local timer = require("src.logic.timer")

local entity = {}

local draw = function (self, view)
  if self.visible then self.sprite:draw(
      view,
      self.position.drawX,
      self.position.drawY
    )
  end
  if DEBUG then
    view:inContext(function ()
      love.graphics.print(
        self.position:toString(),
        self.position.drawX,
        self.position.drawY)
      if self.debugColor then love.graphics.setColor(self.debugColor) end
      love.graphics.polygon("line", self.boundingBox:getPoints())
    end)
  end
end

local update = function (self, game)
  if self.iframes and game:modulate() then
    self.visible = false
  else
    self.visible = true
  end
  if self.sprite.update then self.sprite:update(game) end
  if self.timer then self.timer:tick(self, game) end
  if self.movement then self.movement.update(self, game) end
  self.boundingBox:update(self.position.x, self.position.z)
end

local collisionCheck = function (self, ent, game)
  if self == ent then return end
  if self.boundingBox:overlaps(ent.boundingBox) then
    self.debugColor = {255, 0, 255}
    if self.collision then self:collision(ent, game) end
  end
end

local done = function (self)
  self.finished = true
end

local addTimer = function (self, timer)
  self.timer = timer
end

local removeTimer = function (self)
  self.timer = nil
end

local takeDamage = function (self, damage)
  if self.vulnerable then
    self.hp = self.hp - damage
    if self.hp <= 0 then
      self:done()
    else
      self.vulnerable = false
      self.iframes = true
      self:addTimer(timer.create(timer.ticks(20), function (_, ent, game)
        ent.vulnerable = true
        ent.iframes = false
      end))
    end
  end
end

entity.create = function (sprite, position, speed, movement, collision)
  local inst = {}

  inst.finished = false
  inst.sprite = sprite
  inst.position = position
  inst.speed = speed
  inst.movement = movement
  inst.collision = collision
  inst.boundingBox = rectangle.create(
    inst.position.x,
    inst.position.z,
    sprite.image:getWidth(),
    sprite.image:getHeight()
  )
  inst.interuptMovement = false
  inst.vulnerable = true
  inst.hp = 5
  inst.iframes = false
  inst.visible = true

  inst.draw = draw
  inst.update = update
  inst.toPosition = toPosition
  inst.collisionCheck = collisionCheck
  inst.done = done
  inst.addTimer = addTimer
  inst.removeTimer = removeTimer
  inst.takeDamage = takeDamage

  return inst
end

return entity


text:
/* eslint-disable import/no-commonjs */
module.exports = function(grunt) {
    grunt.config.set('shell', {
        cleanNodeModules: {
            command: () => 'rm -rf node_modules',
        },
    });
};


text:
var generators = require('../../../..');
var util = require('util');

var Generator = module.exports = function Generator(args, options) {
  generators.NamedBase.apply(this, arguments);
};

util.inherits(Generator, generators.NamedBase);


text:
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "GameFramework/PlayerController.h"
#include "Rapid_2PlayerController.generated.h"

UCLASS()
class ARapid_2PlayerController : public APlayerController
{
	GENERATED_BODY()

public:
	ARapid_2PlayerController();

protected:
	/** True if the controlled character should navigate to the mouse cursor. */
	uint32 bMoveToMouseCursor : 1;

	// Begin PlayerController interface
	virtual void PlayerTick(float DeltaTime) override;
	virtual void SetupInputComponent() override;
	// End PlayerController interface

	/** Navigate player to the current mouse cursor location. */
	void MoveToMouseCursor();

	/** Navigate player to the current touch location. */
	void MoveToTouchLocation(const ETouchIndex::Type FingerIndex, const FVector Location);

	/** Navigate player to the given world location. */
	void SetNewMoveDestination(const FVector DestLocation);

	/** Input handlers for SetDestination action. */
	void OnSetDestinationPressed();
	void OnSetDestinationReleased();
};


text:
"use strict";

const { expect } = require("chai");

const { LanguageDef } = $language;

const { createDummyParser } = $testutil.helpers;

describe("create", () => {
  it("should create a new language definition object", () => {
    // use default parameter
    {
      const def = LanguageDef.create();
      expect(def.commentStart).to.be.undefined;
      expect(def.commentEnd).to.be.undefined;
      expect(def.commentLine).to.be.undefined;
      expect(def.nestedComments).to.be.true;
      expect(def.idStart).to.be.undefined;
      expect(def.idLetter).to.be.undefined;
      expect(def.opStart).to.be.undefined;
      expect(def.opLetter).to.be.undefined;
      expect(def.reservedIds).to.be.undefined;
      expect(def.reservedOps).to.be.undefined;
      expect(def.caseSensitive).to.be.true;
    }
    // empty object
    {
      const def = LanguageDef.create({});
      expect(def.commentStart).to.be.undefined;
      expect(def.commentEnd).to.be.undefined;
      expect(def.commentLine).to.be.undefined;
      expect(def.nestedComments).to.be.true;
      expect(def.idStart).to.be.undefined;
      expect(def.idLetter).to.be.undefined;
      expect(def.opStart).to.be.undefined;
      expect(def.opLetter).to.be.undefined;
      expect(def.reservedIds).to.be.undefined;
      expect(def.reservedOps).to.be.undefined;
      expect(def.caseSensitive).to.be.true;
    }
    // fully specified object
    {
      const idStart  = createDummyParser();
      const idLetter = createDummyParser();
      const opStart  = createDummyParser();
      const opLetter = createDummyParser();
      const def = LanguageDef.create({
        commentStart  : "/*",
        commentEnd    : "*/",
        commentLine   : "//",
        nestedComments: false,
        idStart       : idStart,
        idLetter      : idLetter,
        opStart       : opStart,
        opLetter      : opLetter,
        reservedIds   : ["var", "if", "else", "while"],
        reservedOps   : ["+", "-", "*", "/"],
        caseSensitive : false,
      });
      expect(def.commentStart).to.equal("/*");
      expect(def.commentEnd).to.equal("*/");
      expect(def.commentLine).to.equal("//");
      expect(def.nestedComments).to.be.false;
      expect(def.idStart).to.equal(idStart);
      expect(def.idLetter).to.equal(idLetter);
      expect(def.opStart).to.equal(opStart);
      expect(def.opLetter).to.equal(opLetter);
      expect(def.reservedIds).to.deep.equal(["var", "if", "else", "while"]);
      expect(def.reservedOps).to.deep.equal(["+", "-", "*", "/"]);
      expect(def.caseSensitive).to.be.false;
    }
  });
});


text:
package org.apache.hadoop.mapred;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.v2.MiniMRYarnCluster;
import org.apache.hadoop.util.JarFinder;

/**
 * A MiniMRCluster factory. In MR2, it provides a wrapper MiniMRClientCluster
 * interface around the MiniMRYarnCluster. While in MR1, it provides such
 * wrapper around MiniMRCluster. This factory should be used in tests to provide
 * an easy migration of tests across MR1 and MR2.
 */
public class MiniMRClientClusterFactory {

  public static MiniMRClientCluster create(Class<?> caller, int noOfNMs,
      Configuration conf) throws IOException {

    if (conf == null) {
      conf = new Configuration();
    }

    FileSystem fs = FileSystem.get(conf);

    Path testRootDir = new Path("target", caller.getName() + "-tmpDir")
        .makeQualified(fs);
    Path appJar = new Path(testRootDir, "MRAppJar.jar");

    // Copy MRAppJar and make it private.
    Path appMasterJar = new Path(MiniMRYarnCluster.APPJAR);

    fs.copyFromLocalFile(appMasterJar, appJar);
    fs.setPermission(appJar, new FsPermission("744"));

    Job job = Job.getInstance(conf);

    job.addFileToClassPath(appJar);
    String callerJar = JarFinder.getJar(caller);
    job.setJar(callerJar);

    MiniMRYarnCluster miniMRYarnCluster = new MiniMRYarnCluster(caller
        .getName(), noOfNMs);
    miniMRYarnCluster.init(job.getConfiguration());
    miniMRYarnCluster.start();

    return new MiniMRYarnClusterAdapter(miniMRYarnCluster);
  }

}


text:
import functools
import logging
import os
import Queue as queue
import socket
import subprocess
import sys
import threading
import time
import traceback
import uuid
from concurrent import futures

import grpc
from google.protobuf import text_format

from apache_beam import coders
from apache_beam.internal import pickler
from apache_beam.portability.api import beam_fn_api_pb2_grpc
from apache_beam.portability.api import beam_job_api_pb2
from apache_beam.portability.api import beam_job_api_pb2_grpc
from apache_beam.portability.api import endpoints_pb2
from apache_beam.runners import pipeline_context
from apache_beam.runners import runner
from apache_beam.runners.portability import fn_api_runner

TERMINAL_STATES = [
    beam_job_api_pb2.JobState.DONE,
    beam_job_api_pb2.JobState.STOPPED,
    beam_job_api_pb2.JobState.FAILED,
    beam_job_api_pb2.JobState.CANCELLED,
]

class UniversalLocalRunner(runner.PipelineRunner):
  """A BeamRunner that executes Python pipelines via the Beam Job API.

  By default, this runner executes in process but still uses GRPC to communicate
  pipeline and worker state.  It can also be configured to use inline calls
  rather than GRPC (for speed) or launch completely separate subprocesses for
  the runner and worker(s).
  """

  def __init__(
      self,
      use_grpc=True,
      use_subprocesses=False,
      runner_api_address=None,
      docker_image=None):
    if use_subprocesses and not use_grpc:
      raise ValueError("GRPC must be used with subprocesses")
    super(UniversalLocalRunner, self).__init__()
    self._use_grpc = use_grpc
    self._use_subprocesses = use_subprocesses

    self._job_service = None
    self._job_service_lock = threading.Lock()
    self._subprocess = None
    self._runner_api_address = runner_api_address
    self._docker_image = docker_image or self.default_docker_image()

  def __del__(self):
    # Best effort to not leave any dangling processes around.
    self.cleanup()

  def cleanup(self):
    if self._subprocess:
      self._subprocess.kill()
      time.sleep(0.1)
    self._subprocess = None

  @staticmethod
  def default_docker_image():
    if 'USER' in os.environ:
      # Perhaps also test if this was built?
      logging.info('Using latest locally built Python SDK docker image.')
      return os.environ['USER'] + '-docker.apache.bintray.io/beam/python:latest'
    else:
      logging.warning('Could not find a Python SDK docker image.')
      return 'unknown'

  def _get_job_service(self):
    with self._job_service_lock:
      if not self._job_service:
        if self._runner_api_address:
          self._job_service = beam_job_api_pb2_grpc.JobServiceStub(
              grpc.insecure_channel(self._runner_api_address))
        elif self._use_subprocesses:
          self._job_service = self._start_local_runner_subprocess_job_service()

        elif self._use_grpc:
          self._servicer = JobServicer(use_grpc=True)
          self._job_service = beam_job_api_pb2_grpc.JobServiceStub(
              grpc.insecure_channel(
                  'localhost:%d' % self._servicer.start_grpc()))

        else:
          self._job_service = JobServicer(use_grpc=False)

    return self._job_service

  def _start_local_runner_subprocess_job_service(self):
    if self._subprocess:
      # Kill the old one if it exists.
      self._subprocess.kill()
    # TODO(robertwb): Consider letting the subprocess pick one and
    # communicate it back...
    port = _pick_unused_port()
    logging.info("Starting server on port %d.", port)
    self._subprocess = subprocess.Popen([
        sys.executable,
        '-m',
        'apache_beam.runners.portability.universal_local_runner_main',
        '-p',
        str(port),
        '--worker_command_line',
        '%s -m apache_beam.runners.worker.sdk_worker_main' % sys.executable
    ])
    job_service = beam_job_api_pb2_grpc.JobServiceStub(
        grpc.insecure_channel('localhost:%d' % port))
    logging.info("Waiting for server to be ready...")
    start = time.time()
    timeout = 30
    while True:
      time.sleep(0.1)
      if self._subprocess.poll() is not None:
        raise RuntimeError(
            "Subprocess terminated unexpectedly with exit code %d." %
            self._subprocess.returncode)
      elif time.time() - start > timeout:
        raise RuntimeError(
            "Pipeline timed out waiting for job service subprocess.")
      else:
        try:
          job_service.GetState(
              beam_job_api_pb2.GetJobStateRequest(job_id='[fake]'))
          break
        except grpc.RpcError as exn:
          if exn.code != grpc.StatusCode.UNAVAILABLE:
            # We were able to contact the service for our fake state request.
            break
    logging.info("Server ready.")
    return job_service

  def run_pipeline(self, pipeline):
    # Java has different expectations about coders
    # (windowed in Fn API, but *un*windowed in runner API), whereas the
    # FnApiRunner treats them consistently, so we must guard this.
    # See also BEAM-2717.
    proto_context = pipeline_context.PipelineContext(
        default_environment_url=self._docker_image)
    proto_pipeline = pipeline.to_runner_api(context=proto_context)
    if self._runner_api_address:
      for pcoll in proto_pipeline.components.pcollections.values():
        if pcoll.coder_id not in proto_context.coders:
          coder = coders.registry.get_coder(pickler.loads(pcoll.coder_id))
          pcoll.coder_id = proto_context.coders.get_id(coder)
      proto_context.coders.populate_map(proto_pipeline.components.coders)

    job_service = self._get_job_service()
    prepare_response = job_service.Prepare(
        beam_job_api_pb2.PrepareJobRequest(
            job_name='job',
            pipeline=proto_pipeline))
    run_response = job_service.Run(beam_job_api_pb2.RunJobRequest(
        preparation_id=prepare_response.preparation_id))
    return PipelineResult(job_service, run_response.job_id)

class PipelineResult(runner.PipelineResult):
  def __init__(self, job_service, job_id):
    super(PipelineResult, self).__init__(beam_job_api_pb2.JobState.UNSPECIFIED)
    self._job_service = job_service
    self._job_id = job_id
    self._messages = []

  def cancel(self):
    self._job_service.Cancel()

  @property
  def state(self):
    runner_api_state = self._job_service.GetState(
        beam_job_api_pb2.GetJobStateRequest(job_id=self._job_id)).state
    self._state = self._runner_api_state_to_pipeline_state(runner_api_state)
    return self._state

  @staticmethod
  def _runner_api_state_to_pipeline_state(runner_api_state):
    return getattr(
        runner.PipelineState,
        beam_job_api_pb2.JobState.Enum.Name(runner_api_state))

  @staticmethod
  def _pipeline_state_to_runner_api_state(pipeline_state):
    return beam_job_api_pb2.JobState.Enum.Value(pipeline_state)

  def wait_until_finish(self):
    def read_messages():
      for message in self._job_service.GetMessageStream(
          beam_job_api_pb2.JobMessagesRequest(job_id=self._job_id)):
        self._messages.append(message)
    t = threading.Thread(target=read_messages, name='wait_until_finish_read')
    t.daemon = True
    t.start()

    for state_response in self._job_service.GetStateStream(
        beam_job_api_pb2.GetJobStateRequest(job_id=self._job_id)):
      self._state = self._runner_api_state_to_pipeline_state(
          state_response.state)
      if state_response.state in TERMINAL_STATES:
        break
    if self._state != runner.PipelineState.DONE:
      raise RuntimeError(
          "Pipeline %s failed in state %s." % (self._job_id, self._state))
    return self._state

class BeamJob(threading.Thread):
  """This class handles running and managing a single pipeline.

  The current state of the pipeline is available as self.state.
  """
  def __init__(self, job_id, pipeline_options, pipeline_proto,
               use_grpc=True, sdk_harness_factory=None):
    super(BeamJob, self).__init__()
    self._job_id = job_id
    self._pipeline_options = pipeline_options
    self._pipeline_proto = pipeline_proto
    self._use_grpc = use_grpc
    self._sdk_harness_factory = sdk_harness_factory
    self._log_queue = queue.Queue()
    self._state_change_callbacks = [
        lambda new_state: self._log_queue.put(
            beam_job_api_pb2.JobMessagesResponse(
                state_response=
                beam_job_api_pb2.GetJobStateResponse(state=new_state)))
    ]
    self._state = None
    self.state = beam_job_api_pb2.JobState.STARTING
    self.daemon = True

  def add_state_change_callback(self, f):
    self._state_change_callbacks.append(f)

  @property
  def log_queue(self):
    return self._log_queue

  @property
  def state(self):
    return self._state

  @state.setter
  def state(self, new_state):
    for state_change_callback in self._state_change_callbacks:
      state_change_callback(new_state)
    self._state = new_state

  def run(self):
    with JobLogHandler(self._log_queue):
      try:
        fn_api_runner.FnApiRunner(
            use_grpc=self._use_grpc,
            sdk_harness_factory=self._sdk_harness_factory
        ).run_via_runner_api(self._pipeline_proto)
        logging.info("Successfully completed job.")
        self.state = beam_job_api_pb2.JobState.DONE
      except:  # pylint: disable=bare-except
        logging.exception("Error running pipeline.")
        traceback.print_exc()
        self.state = beam_job_api_pb2.JobState.FAILED
        raise

  def cancel(self):
    if self.state not in TERMINAL_STATES:
      self.state = beam_job_api_pb2.JobState.CANCELLING
      # TODO(robertwb): Actually cancel...
      self.state = beam_job_api_pb2.JobState.CANCELLED

class JobServicer(beam_job_api_pb2_grpc.JobServiceServicer):
  """Servicer for the Beam Job API.

  Manages one or more pipelines, possibly concurrently.
  """
  def __init__(
      self, worker_command_line=None, use_grpc=True):
    self._worker_command_line = worker_command_line
    self._use_grpc = use_grpc or bool(worker_command_line)
    self._jobs = {}

  def start_grpc(self, port=0):
    self._server = grpc.server(futures.ThreadPoolExecutor(max_workers=3))
    port = self._server.add_insecure_port('localhost:%d' % port)
    beam_job_api_pb2_grpc.add_JobServiceServicer_to_server(self, self._server)
    self._server.start()
    logging.info("Grpc server started on port %s", port)
    return port

  def Prepare(self, request, context=None):
    # For now, just use the job name as the job id.
    logging.debug("Got Prepare request.")
    preparation_id = "%s-%s" % (request.job_name, uuid.uuid4())
    if self._worker_command_line:
      sdk_harness_factory = functools.partial(
          SubprocessSdkWorker, self._worker_command_line)
    else:
      sdk_harness_factory = None
    self._jobs[preparation_id] = BeamJob(
        preparation_id, request.pipeline_options, request.pipeline,
        use_grpc=self._use_grpc, sdk_harness_factory=sdk_harness_factory)
    logging.debug("Prepared job '%s' as '%s'", request.job_name, preparation_id)
    return beam_job_api_pb2.PrepareJobResponse(preparation_id=preparation_id)

  def Run(self, request, context=None):
    job_id = request.preparation_id
    logging.debug("Runing job '%s'", job_id)
    self._jobs[job_id].start()
    return beam_job_api_pb2.RunJobResponse(job_id=job_id)

  def GetState(self, request, context=None):
    return beam_job_api_pb2.GetJobStateResponse(
        state=self._jobs[request.job_id].state)

  def Cancel(self, request, context=None):
    self._jobs[request.job_id].cancel()
    return beam_job_api_pb2.CancelJobRequest(
        state=self._jobs[request.job_id].state)

  def GetStateStream(self, request, context=None):
    job = self._jobs[request.job_id]
    state_queue = queue.Queue()
    job.add_state_change_callback(lambda state: state_queue.put(state))
    try:
      current_state = state_queue.get()
    except queue.Empty:
      current_state = job.state
    yield beam_job_api_pb2.GetJobStateResponse(
        state=current_state)
    while current_state not in TERMINAL_STATES:
      current_state = state_queue.get(block=True)
      yield beam_job_api_pb2.GetJobStateResponse(
          state=current_state)

  def GetMessageStream(self, request, context=None):
    job = self._jobs[request.job_id]
    current_state = job.state
    while current_state not in TERMINAL_STATES:
      msg = job.log_queue.get(block=True)
      yield msg
      if msg.HasField('state_response'):
        current_state = msg.state_response.state
    try:
      while True:
        yield job.log_queue.get(block=False)
    except queue.Empty:
      pass

class BeamFnLoggingServicer(beam_fn_api_pb2_grpc.BeamFnLoggingServicer):
  def Logging(self, log_bundles, context=None):
    for log_bundle in log_bundles:
      for log_entry in log_bundle.log_entries:
        logging.info('Worker: %s', str(log_entry).replace('\n', ' '))
    return iter([])

class SubprocessSdkWorker(object):
  """Manages a SDK worker implemented as a subprocess communicating over grpc.
  """

  def __init__(self, worker_command_line, control_address):
    self._worker_command_line = worker_command_line
    self._control_address = control_address

  def run(self):
    logging_server = grpc.server(
        futures.ThreadPoolExecutor(max_workers=10))
    logging_port = logging_server.add_insecure_port('[::]:0')
    logging_server.start()
    logging_servicer = BeamFnLoggingServicer()
    beam_fn_api_pb2_grpc.add_BeamFnLoggingServicer_to_server(
        logging_servicer, logging_server)
    logging_descriptor = text_format.MessageToString(
        endpoints_pb2.ApiServiceDescriptor(url='localhost:%s' % logging_port))

    control_descriptor = text_format.MessageToString(
        endpoints_pb2.ApiServiceDescriptor(url=self._control_address))

    p = subprocess.Popen(
        self._worker_command_line,
        shell=True,
        env=dict(os.environ,
                 CONTROL_API_SERVICE_DESCRIPTOR=control_descriptor,
                 LOGGING_API_SERVICE_DESCRIPTOR=logging_descriptor))
    try:
      p.wait()
      if p.returncode:
        raise RuntimeError(
            "Worker subprocess exited with return code %s" % p.returncode)
    finally:
      if p.poll() is None:
        p.kill()
      logging_server.stop(0)

class JobLogHandler(logging.Handler):
  """Captures logs to be returned via the Beam Job API.

  Enabled via the with statement."""

  # Mapping from logging levels to LogEntry levels.
  LOG_LEVEL_MAP = {
      logging.FATAL: beam_job_api_pb2.JobMessage.JOB_MESSAGE_ERROR,
      logging.ERROR: beam_job_api_pb2.JobMessage.JOB_MESSAGE_ERROR,
      logging.WARNING: beam_job_api_pb2.JobMessage.JOB_MESSAGE_WARNING,
      logging.INFO: beam_job_api_pb2.JobMessage.JOB_MESSAGE_BASIC,
      logging.DEBUG: beam_job_api_pb2.JobMessage.JOB_MESSAGE_DEBUG,
  }

  def __init__(self, message_queue):
    super(JobLogHandler, self).__init__()
    self._message_queue = message_queue
    self._last_id = 0
    self._logged_thread = None

  def __enter__(self):
    # Remember the current thread to demultiplex the logs of concurrently
    # running pipelines (as Python log handlers are global).
    self._logged_thread = threading.current_thread()
    logging.getLogger().addHandler(self)

  def __exit__(self, *args):
    self._logged_thread = None
    self.close()

  def _next_id(self):
    self._last_id += 1
    return str(self._last_id)

  def emit(self, record):
    if self._logged_thread is threading.current_thread():
      self._message_queue.put(beam_job_api_pb2.JobMessagesResponse(
          message_response=beam_job_api_pb2.JobMessage(
              message_id=self._next_id(),
              time=time.strftime(
                  '%Y-%m-%d %H:%M:%S.', time.localtime(record.created)),
              importance=self.LOG_LEVEL_MAP[record.levelno],
              message_text=self.format(record))))

def _pick_unused_port():
  """Not perfect, but we have to provide a port to the subprocess."""
  # TODO(robertwb): Consider letting the subprocess communicate a choice of
  # port back.
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.bind(('localhost', 0))
  _, port = s.getsockname()
  s.close()
  return port


text:
using System;
using System.Collections.Generic;
using System.Threading;
using System.Text;
using System.Linq;
using M2SA.AppGenome;
using M2SA.AppGenome.Threading;

namespace M2SA.AppGenome.Threading
{
    /// <summary>
    ///
    /// </summary>
    public class TimeAction : ITaskAction
    {
        CronExpression cronExpression = null;
        TimeSpan waitSpan = TimeSpan.Zero;
        DateTime nextFireTime = DateTime.MaxValue;

        readonly IWorkItemsGroup threadPool = null;

        /// <summary>
        /// 是否可以被取消
        /// </summary>
        public bool CanCancel
        {
            get;
            private set;
        }

        /// <summary>
        ///
        /// </summary>
        public string Name
        {
            get;
            set;
        }

        /// <summary>
        ///
        /// </summary>
        public Action<DateTime> Action
        {
            get;
            set;
        }

        /// <summary>
        ///
        /// </summary>
        public int Concurrency
        {
            get;
            private set;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <param name="cronExpressionValue"></param>
        /// <param name="action"></param>
        /// <param name="concurrency"></param>
        /// <param name="canCancel"></param>
        public TimeAction(string name, string cronExpressionValue, Action<DateTime> action, int concurrency, bool canCancel)
        {
            this.Name = name;
            this.cronExpression = new CronExpression(cronExpressionValue);
            this.Action = action;
            this.CanCancel = canCancel;
            this.ResetNextFireTime();

            this.Concurrency = concurrency;
            this.threadPool = AppInstance.GetThreadPool().CreateWorkItemsGroup(this.Concurrency);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <param name="cronExpressionValue"></param>
        /// <param name="action"></param>
        public TimeAction(string name, string cronExpressionValue, Action<DateTime> action)
            : this(name, cronExpressionValue, action, 1, true)
        {
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <param name="cronExpressionValue"></param>
        /// <param name="action"></param>
        /// <param name="concurrency"></param>
        public TimeAction(string name, string cronExpressionValue, Action<DateTime> action, int concurrency)
            : this(name, cronExpressionValue, action, concurrency, true)
        {
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pocessInterval"></param>
        /// <param name="action"></param>
        /// <param name="concurrency"></param>
        /// <param name="canCancel"></param>
        public TimeAction(string name, TimeSpan pocessInterval, Action<DateTime> action, int concurrency, bool canCancel)
        {
            this.Name = name;
            this.waitSpan = pocessInterval;
            this.Action = action;
            this.CanCancel = canCancel;
            this.ResetNextFireTime();

            this.Concurrency = concurrency;
            this.threadPool = AppInstance.GetThreadPool().CreateWorkItemsGroup(this.Concurrency);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pocessInterval"></param>
        /// <param name="action"></param>
        public TimeAction(string name, TimeSpan pocessInterval, Action<DateTime> action)
            : this(name, pocessInterval, action, 1, true)
        {
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pocessInterval"></param>
        /// <param name="action"></param>
        /// <param name="concurrency"></param>
        public TimeAction(string name, TimeSpan pocessInterval, Action<DateTime> action, int concurrency)
            : this(name, pocessInterval, action, concurrency, true)
        {
        }

        #region ITaskAction 成员

        /// <summary>
        ///
        /// </summary>
        public void Invoke()
        {
            if (AppInstance.GetThreadPool().IsShuttingdown)
                return;

            this.threadPool.QueueWorkItem<DateTime>(this.Action, DateTime.Now);
            this.ResetNextFireTime();
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="waitInterval"></param>
        /// <returns></returns>
        public bool Trigger(TimeSpan waitInterval)
        {
            return this.nextFireTime <= DateTime.Now;
        }

        #endregion

        void ResetNextFireTime()
        {
            if (this.cronExpression == null)
            {
                if (this.nextFireTime == DateTime.MaxValue)
                    this.nextFireTime = DateTime.Now + this.waitSpan;
                else
                    this.nextFireTime = this.nextFireTime + this.waitSpan;
            }
            else
            {
                if (this.nextFireTime == DateTime.MaxValue)
                    this.nextFireTime = this.cronExpression.GetNextValidTimeAfter(DateTime.Now).Value;
                else
                    this.nextFireTime = this.cronExpression.GetNextValidTimeAfter(this.nextFireTime).Value;
            }
        }
    }
}


text:
package org.apache.hadoop.hdfs.server.federation.store.protocol;

import java.io.IOException;

import org.apache.hadoop.classification.InterfaceAudience.Private;
import org.apache.hadoop.classification.InterfaceStability.Unstable;
import org.apache.hadoop.hdfs.server.federation.store.driver.StateStoreSerializer;

/**
 * API response for registering a namenode with the state store.
 */
public abstract class NamenodeHeartbeatResponse {

  public static NamenodeHeartbeatResponse newInstance() throws IOException {
    return StateStoreSerializer.newRecord(NamenodeHeartbeatResponse.class);
  }

  public static NamenodeHeartbeatResponse newInstance(boolean status)
      throws IOException {
    NamenodeHeartbeatResponse response = newInstance();
    response.setResult(status);
    return response;
  }

  @Private
  @Unstable
  public abstract boolean getResult();

  @Private
  @Unstable
  public abstract void setResult(boolean result);
}


text:
php-lorem-ipsum
===============

Creates random fake text for testing using Lorem Ipsum

Installation
---------------
To install add the following to your `composer.json`:
```
"require": {
	"isuttell/php-lorem-ipsum" : "dev-master"
},
"repositories": [
	{
		"type": "vcs",
		"url" : "https://github.com/isuttell/php-lorem-ipsum.git"
	}
]
```

And then run `composer update`

Documentation
---------------
The following example creates five random paragraphs

```
use \Lorem\Ipsum;
echo Ipsum::paragraphs();
```

## Available Methods

`Ipsum::paragraphs($count = 5);`

`Ipsum::sentences($count = 5);`

`Ipsum::words($count = 5);`

`Ipsum::title($count = 5);`

`Ipsum::email($count = 1);`

## CLI app

You can generate random Lorem Ipsum text from command line outside web server context using CLI script find into `bin/lorem`.

An help is available by calling `bin/lorem --help`. But it is easy to use it, for example, getting 5 paragraphs is `bin/lorem --paragraph=5` or `bin/lorem -p=5`.

Here is the full CLI app help:

```
Usage: lorem [OPTIONS]…
Generate random Lorem Ipsum texts as paragraphs, sentences, words, title or
email.
  -p, --paragraph[=N]        Generates N paragraphs.
  -s, --sentence[=N]         Generates N sentences.
  -w, --word[=N]             Generates N words.
  -t, --title[=N]            Generates title having N words.
  -e, --email[=N]            Generates N comma separated emails.

  -h, --help                 Display this help message and exit
      --version              Display version information and exit
```


text:
/* ###
 * IP: GHIDRA
 * REVIEWED: YES
 *
 * 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.
 */
package ghidra.app.merge.listing;

import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Variable;
import ghidra.program.util.VariableStorageConflicts;
import ghidra.util.exception.CancelledException;
import ghidra.util.task.TaskMonitor;

import java.util.Arrays;

class FunctionVariableStorageConflicts extends VariableStorageConflicts {

	private Function function1;
	private Function function2;

	/**
	 * Construct a VariableStorageConflicts object for the variables contained within two
	 * functions.
	 * @param function1
	 * @param function2
	 * @param ignoreParamToParamConflicts if true param-to-param overlaps will be ignored unless
	 * a param-to-local overlap occurs in which case all params will be pulled in to the
	 * overlap.  If true, it is assumed that the current overlap iteration was initiated by
	 * a parameter overlap check.
	 * @param monitor
	 * @throws CancelledException
	 */
	FunctionVariableStorageConflicts(Function function1, Function function2,
			boolean ignoreParamToParamConflicts, TaskMonitor monitor) throws CancelledException {
		super(Arrays.asList(function1.getAllVariables()),
			Arrays.asList(function2.getAllVariables()), ignoreParamToParamConflicts, monitor);
		this.function1 = function1;
		this.function2 = function2;
	}

	@Override
	public boolean isConflicted(Variable var1, Variable var2) {
		if (var1 != null) {
			if (var1.getFunction() != function1) {
				throw new IllegalArgumentException("var1 does not correspond to function1");
			}
		}
		if (var2 != null) {
			if (var2.getFunction() != function2) {
				throw new IllegalArgumentException("var2 does not correspond to function2");
			}
		}
		return super.isConflicted(var1, var2);
	}
}


text:
package org.apache.ofbiz.workeffort.workeffort;

import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.UtilGenerics;
import org.apache.ofbiz.base.util.UtilHttp;
import org.apache.ofbiz.base.util.UtilMisc;
import org.apache.ofbiz.base.util.UtilValidate;
import org.apache.ofbiz.entity.Delegator;
import org.apache.ofbiz.workeffort.workeffort.WorkEffortSearch.ResultSortOrder;
import org.apache.ofbiz.workeffort.workeffort.WorkEffortSearch.SortKeywordRelevancy;
import org.apache.ofbiz.workeffort.workeffort.WorkEffortSearch.WorkEffortSearchConstraint;

public class WorkEffortSearchSession {

    public static final String module = WorkEffortSearchSession.class.getName();

    @SuppressWarnings("serial")
    public static class WorkEffortSearchOptions implements java.io.Serializable {
        protected List<WorkEffortSearchConstraint> constraintList = null;
        protected ResultSortOrder resultSortOrder = null;
        protected Integer viewIndex = null;
        protected Integer viewSize = null;
        protected boolean changed = false;
        public WorkEffortSearchOptions() { }

        /** Basic copy constructor */
        public WorkEffortSearchOptions(WorkEffortSearchOptions workEffortSearchOptions) {
            this.constraintList = UtilMisc.makeListWritable(workEffortSearchOptions.constraintList);
            this.resultSortOrder = workEffortSearchOptions.resultSortOrder;
            this.viewIndex = workEffortSearchOptions.viewIndex;
            this.viewSize = workEffortSearchOptions.viewSize;
            this.changed = workEffortSearchOptions.changed;
        }

        public List<WorkEffortSearchConstraint> getConstraintList() {
            return this.constraintList;
        }
        public static List<WorkEffortSearchConstraint> getConstraintList(HttpSession session) {
            return getWorkEffortSearchOptions(session).constraintList;
        }
        public static void addConstraint(WorkEffortSearchConstraint workEffortSearchConstraint, HttpSession session) {
            WorkEffortSearchOptions workEffortSearchOptions = getWorkEffortSearchOptions(session);
            if (workEffortSearchOptions.constraintList == null) {
                workEffortSearchOptions.constraintList = new LinkedList<>();
            }
            if (!workEffortSearchOptions.constraintList.contains(workEffortSearchConstraint)) {
                workEffortSearchOptions.constraintList.add(workEffortSearchConstraint);
                workEffortSearchOptions.changed = true;
            }
        }

        public ResultSortOrder getResultSortOrder() {
            if (this.resultSortOrder == null) {
                this.resultSortOrder = new SortKeywordRelevancy();
                this.changed = true;
            }
            return this.resultSortOrder;
        }
        public static ResultSortOrder getResultSortOrder(HttpServletRequest request) {
            WorkEffortSearchOptions workEffortSearchOptions = getWorkEffortSearchOptions(request.getSession());
            return workEffortSearchOptions.getResultSortOrder();
        }
        public static void setResultSortOrder(ResultSortOrder resultSortOrder, HttpSession session) {
            WorkEffortSearchOptions workEffortSearchOptions = getWorkEffortSearchOptions(session);
            workEffortSearchOptions.resultSortOrder = resultSortOrder;
            workEffortSearchOptions.changed = true;
        }

        public static void clearSearchOptions(HttpSession session) {
            WorkEffortSearchOptions workEffortSearchOptions = getWorkEffortSearchOptions(session);
            workEffortSearchOptions.constraintList = null;
            workEffortSearchOptions.resultSortOrder = null;
        }

        public void clearViewInfo() {
            this.viewIndex = null;
            this.viewSize = null;
        }

        /**
         * @return Returns the viewIndex.
         */
        public Integer getViewIndex() {
            return viewIndex;
        }
        /**
         * @param viewIndex The viewIndex to set.
         */
        public void setViewIndex(Integer viewIndex) {
            this.viewIndex = viewIndex;
        }
        /**
         * @return Returns the viewSize.
         */
        public Integer getViewSize() {
            return viewSize;
        }
        /**
         * @param viewSize The viewSize to set.
         */
        public void setViewSize(Integer viewSize) {
            this.viewSize = viewSize;
        }

        public List<String> searchGetConstraintStrings(boolean detailed, Delegator delegator, Locale locale) {
            List<WorkEffortSearchConstraint> workEffortSearchConstraintList = this.getConstraintList();
            List<String> constraintStrings = new LinkedList<>();
            if (workEffortSearchConstraintList == null) {
                return constraintStrings;
            }
            for (WorkEffortSearchConstraint workEffortSearchConstraint: workEffortSearchConstraintList) {
                if (workEffortSearchConstraint == null) {
                    continue;
                }
                String constraintString = workEffortSearchConstraint.prettyPrintConstraint(delegator, detailed, locale);
                if (UtilValidate.isNotEmpty(constraintString)) {
                    constraintStrings.add(constraintString);
                } else {
                    constraintStrings.add("Description not available");
                }
            }
            return constraintStrings;
        }
    }

    public static WorkEffortSearchOptions getWorkEffortSearchOptions(HttpSession session) {
        WorkEffortSearchOptions workEffortSearchOptions = (WorkEffortSearchOptions) session.getAttribute("_WORK_EFFORT_SEARCH_OPTIONS_CURRENT_");
        if (workEffortSearchOptions == null) {
            workEffortSearchOptions = new WorkEffortSearchOptions();
            session.setAttribute("_WORK_EFFORT_SEARCH_OPTIONS_CURRENT_", workEffortSearchOptions);
        }
        return workEffortSearchOptions;
    }

    public static void processSearchParameters(Map<String, Object> parameters, HttpServletRequest request) {
        Boolean alreadyRun = (Boolean) request.getAttribute("processSearchParametersAlreadyRun");
        if (Boolean.TRUE.equals(alreadyRun)) {
            return;
        }
        request.setAttribute("processSearchParametersAlreadyRun", Boolean.TRUE);
        HttpSession session = request.getSession();
        boolean constraintsChanged = false;

        // clear search? by default yes, but if the clearSearch parameter is N then don't
        String clearSearchString = (String) parameters.get("clearSearch");
        if (!"N".equals(clearSearchString)) {
            searchClear(session);
            constraintsChanged = true;
        } else {
            String removeConstraint = (String) parameters.get("removeConstraint");
            if (UtilValidate.isNotEmpty(removeConstraint)) {
                try {
                    searchRemoveConstraint(Integer.parseInt(removeConstraint), session);
                    constraintsChanged = true;
                } catch (Exception e) {
                    Debug.logError(e, "Error removing constraint [" + removeConstraint + "]", module);
                }
            }
        }

//      add a Work Effort Review to the search
        if (UtilValidate.isNotEmpty(parameters.get("SEARCH_STRING_REVIEW_TEXT"))) {
            String reviewText = (String) parameters.get("SEARCH_STRING_REVIEW_TEXT");
            searchAddConstraint(new WorkEffortSearch.WorkEffortReviewConstraint(reviewText), session);
            constraintsChanged = true;
        }
//      add a Work Effort Assoc Type to the search
        if (UtilValidate.isNotEmpty(parameters.get("SEARCH_WORK_EFFORT_ID"))) {
            String workEffortId=(String) parameters.get("SEARCH_WORK_EFFORT_ID");
            String workEffortAssocTypeId=(String) parameters.get("workEffortAssocTypeId");
            boolean includeAllSubWorkEfforts =!"N".equalsIgnoreCase((String) parameters.get("SEARCH_SUB_WORK_EFFORTS"));
            searchAddConstraint(new WorkEffortSearch.WorkEffortAssocConstraint(workEffortId,workEffortAssocTypeId,includeAllSubWorkEfforts), session);
            constraintsChanged = true;
        }
//      add a Work Effort Party Assignment to the search
        if (UtilValidate.isNotEmpty(parameters.get("partyId"))) {
            String partyId=(String) parameters.get("partyId");
            String roleTypeId=(String) parameters.get("roleTypeId");
            searchAddConstraint(new WorkEffortSearch.PartyAssignmentConstraint(partyId,roleTypeId), session);
            constraintsChanged = true;
        }

//      add a Product Set to the search
        if (UtilValidate.isNotEmpty(parameters.get("productId_1"))) {
            List<String> productSet = new LinkedList<>();
            productSet.add((String) parameters.get("productId_1"));
            if (UtilValidate.isNotEmpty(parameters.get("productId_2"))) {
                productSet.add((String) parameters.get("productId_2"));
            }
            searchAddConstraint(new WorkEffortSearch.ProductSetConstraint(productSet), session);
            constraintsChanged = true;
        }

//      add a WorkEfort fromDate thruDate  to the search
        if (UtilValidate.isNotEmpty(parameters.get("fromDate")) || UtilValidate.isNotEmpty(parameters.get("thruDate"))) {
            Timestamp fromDate =null;
            if (UtilValidate.isNotEmpty(parameters.get("fromDate"))) {
                fromDate=Timestamp.valueOf((String) parameters.get("fromDate"));
            }

            Timestamp thruDate = null;
            if (UtilValidate.isNotEmpty(parameters.get("thruDate"))) {
                thruDate = Timestamp.valueOf((String) parameters.get("thruDate"));
            }
            searchAddConstraint(new WorkEffortSearch.LastUpdatedRangeConstraint(fromDate,thruDate), session);
            constraintsChanged = true;
        }

        // if keywords were specified, add a constraint for them
        if (UtilValidate.isNotEmpty(parameters.get("SEARCH_STRING"))) {
            String keywordString = (String) parameters.get("SEARCH_STRING");
            String searchOperator = (String) parameters.get("SEARCH_OPERATOR");
            // defaults to true/Y, ie anything but N is true/Y
            boolean anyPrefixSuffix = !"N".equals(parameters.get("SEARCH_ANYPRESUF"));
            searchAddConstraint(new WorkEffortSearch.KeywordConstraint(keywordString, anyPrefixSuffix, anyPrefixSuffix, null, "AND".equals(searchOperator)), session);
            constraintsChanged = true;
        }
        // set the sort order
        String sortOrder = (String) parameters.get("sortOrder");
        String sortAscending = (String) parameters.get("sortAscending");
        boolean ascending = !"N".equals(sortAscending);
        if (sortOrder != null) {
            if ("SortKeywordRelevancy".equals(sortOrder)) {
                searchSetSortOrder(new WorkEffortSearch.SortKeywordRelevancy(), session);
            } else if (sortOrder.startsWith("SortWorkEffortField:")) {
                String fieldName = sortOrder.substring("SortWorkEffortField:".length());
                searchSetSortOrder(new WorkEffortSearch.SortWorkEffortField(fieldName, ascending), session);
            }
        }

        WorkEffortSearchOptions workEffortSearchOptions = getWorkEffortSearchOptions(session);
        if (constraintsChanged) {
            // query changed, clear out the VIEW_INDEX & VIEW_SIZE
            workEffortSearchOptions.clearViewInfo();
        }

        String viewIndexStr = (String) parameters.get("VIEW_INDEX");
        if (UtilValidate.isNotEmpty(viewIndexStr)) {
            try {
                workEffortSearchOptions.setViewIndex(Integer.valueOf(viewIndexStr));
            } catch (Exception e) {
                Debug.logError(e, "Error formatting VIEW_INDEX, setting to 0", module);
                // we could just do nothing here, but we know something was specified so we don't want to use the previous value from the session
                workEffortSearchOptions.setViewIndex(Integer.valueOf(0));
            }
        }

        String viewSizeStr = (String) parameters.get("VIEW_SIZE");
        if (UtilValidate.isNotEmpty(viewSizeStr)) {
            try {
                workEffortSearchOptions.setViewSize(Integer.valueOf(viewSizeStr));
            } catch (Exception e) {
                Debug.logError(e, "Error formatting VIEW_SIZE, setting to 20", module);
                workEffortSearchOptions.setViewSize(Integer.valueOf(20));
            }
        }
    }

    public static void searchAddConstraint(WorkEffortSearchConstraint workEffortSearchConstraint, HttpSession session) {
        WorkEffortSearchOptions.addConstraint(workEffortSearchConstraint, session);
    }
    public static void searchSetSortOrder(ResultSortOrder resultSortOrder, HttpSession session) {
        WorkEffortSearchOptions.setResultSortOrder(resultSortOrder, session);
    }
    public static List<WorkEffortSearchOptions> getSearchOptionsHistoryList(HttpSession session) {
        List<WorkEffortSearchOptions> optionsHistoryList = UtilGenerics.checkList(session.getAttribute("_WORK_EFFORT_SEARCH_OPTIONS_HISTORY_"));
        if (optionsHistoryList == null) {
            optionsHistoryList = new LinkedList<>();
            session.setAttribute("_WORK_EFFORT_SEARCH_OPTIONS_HISTORY_", optionsHistoryList);
        }
        return optionsHistoryList;
    }

    public static List<String> searchGetConstraintStrings(boolean detailed, HttpSession session, Delegator delegator) {
        Locale locale = UtilHttp.getLocale(session);
        WorkEffortSearchOptions workEffortSearchOptions = getWorkEffortSearchOptions(session);
        return workEffortSearchOptions.searchGetConstraintStrings(detailed, delegator, locale);
    }
    public static String searchGetSortOrderString(boolean detailed, HttpServletRequest request) {
        Locale locale = UtilHttp.getLocale(request);
        ResultSortOrder resultSortOrder = WorkEffortSearchOptions.getResultSortOrder(request);
        return resultSortOrder.prettyPrintSortOrder(detailed, locale);
    }
    public static void checkSaveSearchOptionsHistory(HttpSession session) {
        WorkEffortSearchOptions workEffortSearchOptions = WorkEffortSearchSession.getWorkEffortSearchOptions(session);
        // if the options have changed since the last search, add it to the beginning of the search options history
        if (workEffortSearchOptions.changed) {
            List<WorkEffortSearchOptions> optionsHistoryList = WorkEffortSearchSession.getSearchOptionsHistoryList(session);
            optionsHistoryList.add(0, new WorkEffortSearchOptions(workEffortSearchOptions));
            workEffortSearchOptions.changed = false;
        }
    }
    public static void searchRemoveConstraint(int index, HttpSession session) {
        List<WorkEffortSearchConstraint> workEffortSearchConstraintList = WorkEffortSearchOptions.getConstraintList(session);
        if (workEffortSearchConstraintList == null) {
            return;
        } else if (index >= workEffortSearchConstraintList.size()) {
            return;
        } else {
            workEffortSearchConstraintList.remove(index);
        }
    }
    public static void searchClear(HttpSession session) {
        WorkEffortSearchOptions.clearSearchOptions(session);
    }
}


text:
<?php

// Must include code to stop this file being access directly
/* -------------------------------------------------------- */
if(defined('WB_PATH') == false)
{
	// Stop this file being access directly
	die('<title>Access denied</title>
Cannot access this file directly
');
}
/* -------------------------------------------------------- */

if(defined('WB_URL'))
{
	// Drop Table
	$database->query("DROP TABLE IF EXISTS `".TABLE_PREFIX."mod_mdLV".$page_id."-".$section_id."`");
	$database->query("DELETE FROM `".TABLE_PREFIX."mod_mdLagerVerwaltung` WHERE `page_id` = ".$page_id." && `section_id` = ".$section_id);
}
?>


text:
// XLA-specific reverse Op.

#include "tensorflow/compiler/tf2xla/type_util.h"
#include "tensorflow/compiler/tf2xla/xla_compilation_device.h"
#include "tensorflow/compiler/tf2xla/xla_helpers.h"
#include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
#include "tensorflow/compiler/tf2xla/xla_op_registry.h"
#include "tensorflow/compiler/xla/literal_util.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/register_types.h"
#include "tensorflow/core/framework/tensor.h"

namespace tensorflow {
namespace {

class ReverseOp : public XlaOpKernel {
 public:
  explicit ReverseOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}

  void Compile(XlaOpKernelContext* ctx) override {
    // r = tf.reverse(x, revdims)
    const TensorShape x_shape = ctx->InputShape(0);
    const TensorShape revd_shape = ctx->InputShape(1);
    // Validate input sizes.
    OP_REQUIRES(ctx, TensorShapeUtils::IsVector(revd_shape),
                errors::InvalidArgument("axes must be a vector, not shape ",
                                        revd_shape.DebugString()));
    OP_REQUIRES(ctx, revd_shape.num_elements() == x_shape.dims(),
                errors::InvalidArgument("axes ", revd_shape.DebugString(),
                                        " must have same number of elements as"
                                        " than input tensor has dimensions ",
                                        x_shape.DebugString(), "."));
    if (revd_shape.num_elements() == 0) {
      ctx->SetOutput(0, ctx->Input(0));
      return;
    }
    // ComputationBuilder::Rev() requires concrete values for dimensions arg.
    xla::Literal lax;
    OP_REQUIRES_OK(ctx, ctx->ConstantInputReshaped(1, {x_shape.dims()}, &lax));
    std::vector<bool> revdims(x_shape.dims());
    std::copy(lax.preds().begin(), lax.preds().end(), revdims.begin());
    std::vector<int64> dimensions;

    for (int d = 0; d < x_shape.dims(); ++d) {
      if (revdims[d]) {
        dimensions.push_back(d);
      }
    }

    ctx->SetOutput(0, ctx->builder()->Rev(ctx->Input(0), dimensions));
  }
};

REGISTER_XLA_OP("Reverse", ReverseOp);

class ReverseV2Op : public XlaOpKernel {
 public:
  explicit ReverseV2Op(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}

  void Compile(XlaOpKernelContext* ctx) override {
    // r = tf.reverse(x, axes)
    const TensorShape x_shape = ctx->InputShape(0);
    const TensorShape axes_shape = ctx->InputShape(1);
    // Validate input sizes.
    OP_REQUIRES(ctx, TensorShapeUtils::IsVector(axes_shape),
                errors::InvalidArgument("axes must be a vector, not shape ",
                                        axes_shape.DebugString()));
    OP_REQUIRES(ctx, axes_shape.num_elements() <= x_shape.dims(),
                errors::InvalidArgument("axes ", axes_shape.DebugString(),
                                        " can not have more elements"
                                        " than input tensor has dimensions ",
                                        x_shape.DebugString(), "."));
    // Reverse is a no-op if axes argument is empty.
    if (axes_shape.num_elements() == 0) {
      ctx->SetOutput(0, ctx->Input(0));
      return;
    }
    // ComputationBuilder::Rev() requires concrete values for dimensions arg.
    std::vector<int64> axes;
    OP_REQUIRES_OK(ctx, ctx->ConstantInputAsIntVector(1, &axes));

    for (int d = 0; d < axes.size(); ++d) {
      OP_REQUIRES(ctx, (0 <= axes[d]) && (axes[d] < x_shape.dims()),
                  errors::InvalidArgument(axes[d], " is out of range [0, ",
                                          x_shape.dims(), ")."));
    }

    ctx->SetOutput(0, ctx->builder()->Rev(ctx->Input(0), axes));
  }
};

REGISTER_XLA_OP("ReverseV2", ReverseV2Op);

}  // namespace
}  // namespace tensorflow


text:
.. only:: not (epub or latex or html)

    WARNING: You are looking at unreleased Cilium documentation.
    Please use the official rendered version released here:
    http://docs.cilium.io

.. _intro:

######################
Introduction to Cilium
######################

What is Cilium?
===============

Cilium is open source software for transparently securing the network
connectivity between application services deployed using Linux container
management platforms like Docker and Kubernetes.

At the foundation of Cilium is a new Linux kernel technology called BPF, which
enables the dynamic insertion of powerful security visibility and control logic
within Linux itself.  Because BPF runs inside the Linux kernel, Cilium
security policies can be applied and updated without any changes to the
application code or container configuration.

Why Cilium?
===========

The development of modern datacenter applications has shifted to a
service-oriented architecture often referred to as *microservices*, wherein a
large application is split into small independent services that communicate
with each other via APIs using lightweight protocols like HTTP.  Microservices
applications tend to be highly dynamic, with individual containers getting
started or destroyed as the application scales out / in to adapt to load changes
and during rolling updates that are deployed as part of continuous delivery.

This shift toward highly dynamic microservices presents both a challenge and an
opportunity in terms of securing connectivity between microservices.
Traditional Linux network security approaches (e.g., iptables) filter on IP
address and TCP/UDP ports, but IP addresses frequently churn in dynamic
microservices environments. The highly volatile life cycle of containers causes
these approaches to struggle to scale side by side with the application as load
balancing tables and access control lists carrying hundreds of thousands of
rules that need to be updated with a continuously growing frequency. Protocol
ports (e.g. TCP port 80 for HTTP traffic) can no longer be used to
differentiate between application traffic for security purposes as the port is
utilized for a wide range of messages across services.

An additional challenge is the ability to provide accurate visibility as
traditional systems are using IP addresses as primary identification vehicle
which may have a drastically reduced lifetime of just a few seconds in
microservices architectures.

By leveraging Linux BPF, Cilium retains the ability to transparently insert
security visibility + enforcement, but does so in a way that is based on
service / pod / container identity (in contrast to IP address identification in
traditional systems) and can filter on application-layer (e.g. HTTP).  As a
result, Cilium not only makes it simple to apply security policies in a highly
dynamic environment by decoupling security from addressing, but can also
provide stronger security isolation by operating at the HTTP-layer in addition
to providing traditional Layer 3 and Layer 4 segmentation.

The use of BPF enables Cilium to achieve all of this in a way that is highly
scalable even for large-scale environments.

Functionality Overview
======================

.. include:: ../README.rst
     :start-after: begin-functionality-overview
     :end-before: end-functionality-overview


text:
'use strict';

var path   = require('path')
  , logger = require('../../../../lib/logger')
  ;

/**
 * This is a c9/architect configuration file.
 */
module.exports = [
  {packagePath : path.join(__dirname, '../../../../test/integration/benchmarkr/services/mysqld'),
   dbpath      : path.join(__dirname, '../db'),
   logger      : logger.child({component : 'mysqld'})
  },
  {packagePath : path.join(__dirname, '../../../../test/lib/bootstrap/mysql'),
   db          : {
     user  : 'test_user',
     name  : 'express_sequelize',
     table : 'test'
   },
   logger      : logger.child({component : 'mysql_bootstrap'})
  },
  {packagePath : path.join(__dirname, 'sequelize-dal'),
   db          : {
     user  : 'test_user',
     name  : 'express_sequelize'
   },
   logger      : logger.child({component : 'sequelize_dal'})
  },
];


text:
using namespace std;

 struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  };

class Solution {
public:
    void recur(vector<vector<int> > *ans, TreeNode *current, int target, vector<int> *path)
    {
      if (current == NULL)
        return;
      path->push_back(current->val);
      target = target - current->val;
      if (target == 0)
      {
        if ((current->left == NULL) && (current->right == NULL))
          ans->push_back(*path);
      }
      recur(ans, current->left, target, path);
      recur(ans, current->right, target, path);
      path->pop_back();
      return;
    }

    vector<vector<int> > pathSum(TreeNode *root, int sum)
    {
      vector<vector<int> >ans;
      vector<int> path;
      recur(&ans, root, sum, &path);
      return ans;
    }
};

int main()
{
  TreeNode single(1);
  Solution obj;
  vector<vector<int> > ans = obj.pathSum(&single, 1);
  cout << "The size of returned ans is " << ans.size() << endl;
  cout << "The first element is " << ans[0][0] << endl;
  return 0;
}


text:
import java.rmi.Naming;

public class ChatServerMain {
    public static void main(String[] args) {
        try {
            ChatServerImpl server = new ChatServerImpl();
            Naming.rebind("rmi://localhost/ChatServer", server);
        } catch (Exception e) {
        }
    }
}