text:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="section_map">Map</string>
    <string name="section_rules">Knigge</string>
    <string name="section_twitter">Twitter</string>
    <string name="section_about">Info</string>
    <string name="section_chat">Chat</string>

    <string name="close">App wirklich beenden?</string>
    <string name="yes">Ja</string>
    <string name="no">Nein</string>

    <string name="tracking">Tracking</string>
    <string name="close_button">Schließen</string>
    <string name="feedback">Feedback</string>
    <string name="datenschutz">Datenschutzerklärung</string>
    <string name="take_picture">Foto aufnehmen</string>
    <string name="rate_the_app">Bewerte die App</string>

    <string name="map_searching_for_location">Warte auf GPS-Signal</string>
    <string name="map_overlay_tracking_off">Tracking ist deaktiviert. \n
        Aktiviere Tracking, um die anderen Fahrer*innen sehen zu können. \n\n
        Tappe einfach hier drauf!
    </string>

    <string name="prerequisites_no_internet_enabled_text">Bitte aktivieren deine Internet-Verbindung und starte die App erneut</string>
    <string name="prerequisites_no_internet_enabled_title">Deine Internet-Verbindung ist deaktiviert oder geblockt</string>
    <string name="prerequisites_no_gps_provider_enabled_title">GPS ist deaktiviert oder geblockt</string>
    <string name="prerequisites_no_gps_provider_enabled_text">Bitte aktivieren GPS und starte die App erneut</string>

    <string name="notification_tracking_title">Critical Maps Tracking</string>
    <string name="notification_tracking_text">Die App läuft im Hintergrund und zeichnet deine Location-Daten auf. Bitte vergiss nicht, die App auszuschalten, wenn du nicht mehr getrackt werden willst. </string>

    <string name="rules_corken">Corken!?</string>
    <string name="rules_corken_explain">Schütze Autofahrer vor sich selbst durch corken!</string>
    <string name="rules_oncoming_traffic">Gegenverkehr</string>
    <string name="rules_oncoming_traffic_explain">Verzichte darauf auf der Gegenfahrbahn zu fahren.</string>
    <string name="rules_steady">Sachte, Keule!</string>
    <string name="rules_steady_explain">Vorne: nicht rasen! Hinten: Lücken zufahren!</string>
    <string name="rules_brake">Keine Vollbremsungen!</string>
    <string name="rules_brake_explain">Wenn\'s mal nicht anders geht, versuche die anderen per Handzeichen zu warnen.
    </string>
    <string name="rules_green">Vorne nur bei Grün</string>
    <string name="rules_green_explain">Wenn du an der Spitze der Mass fährst, musst du warten bis die Ampel auf Grün
        schaltet.
    </string>
    <string name="rules_friendly">Locker bleiben</string>
    <string name="rules_friendly_explain">Lass dich nicht provozieren. Sei freundlich zur Polizei, Autofahrern und allen
        Anderen, auch wenn die\'s nicht sind.
    </string>
    <string name="rules_fun">Hab Spaß!</string>
    <string name="rules_fun_explain">Genieße autofreie Straßen. Fahr ein bisschen mit dem Sound-Rädern mit. Check
        die Bikes deiner Mitfahrer aus. Quatsch Autofahrer, Passanten, Mitfahrer an. Hab Spaß!
    </string>
    <string name="rules_image_source">Bilder von criticalmass-hh.de (CC BY-NC-ND 3.0 DE)</string>

    <string name="twitter_heading">Alle Tweets die \"CriticalMaps\" enthalten</string>
    <string name="twitter_refreshed">Tweets aktualisiert!</string>
    <string name="twitter_try_again">Versuch\'s nochmal!</string>

    <string name="chat_heading">Nachrichten werden nach 30 Minuten wieder gelöscht.</string>
    <string name="chat_send">Senden</string>
    <string name="chat_text">Deine Nachicht …</string>
    <string name="chat_sending">Sende …</string>

    <string name="about_heading_social">social krams</string>
    <string name="about_heading_website">website</string>
    <string name="about_heading_contributions">contributions</string>
    <string name="about_heading_code">code</string>

    <string name="notification_alarm_title">Nicht vergessen:</string>
    <string name="notification_alarm_text">Critical Mass heute 20 Uhr Heinrichplatz! Viva la velorution!</string>

    <string name="timetoword_justnow">gerade eben</string>
    <string name="timetoword_aminuteago">vor einer Minute</string>
    <string name="timetoword_minutesago">vor %s Minuten</string>
    <string name="timetoword_anhourago">vor einer Stunde</string>
    <string name="timetoword_hoursago">vor %s Stunden</string>
    <string name="timetoword_yesterday">gestern</string>
    <string name="timetoword_daysago">vor %s Tagen</string>

    <string name="camera_confirm_image_upload">
        <![CDATA[
        Willst du das Foto auf criticalmaps.net hochladen? \nWir veröffentlichen die Bilder dort auf der Karte unter der CC BY-SA 4.0 Lizenz.
        ]]>
    </string>
    <string name="camera_upload">Foto hochladen!</string>
    <string name="camera_discard">Och nööö :-(</string>
    <string name="camera_uploading_progress">Lade hoch …</string>
    <string name="camera_upload_failed_title">Hochladen hat nicht funktioniert</string>
    <string name="camera_upload_failed_message">Bitte versuch es später nochmal</string>
    <string name="camera_processing_image">Foto wird verarbeitet</string>
    <string name="camera_no_location_no_camera">Ohne GPS-Signal kannst du keine Bilder hochladen</string>
    <string name="camera_image_upload_succeeded_title">Bilder-Upload erfolgreich!</string>
    <string name="camera_image_upload_succeeded_message">Das Bild steht jetzt in der Galerie auf criticalmaps.net</string>
    <string name="camera_no_camera">Keine Kamera vorhanden.</string>
    <string name="camera_no_camera_no_camera">Du kannst keine Bilder aufnehmen, wenn dein Smartfone keine Kamera hat.</string>

    <string name="something_went_wrong">Eine Fehler ist aufgetreten</string>
    <string name="cancel">Abbrechen</string>
    <string name="ok">OK</string>
    <string name="go_to_settings">zu den Einstellungen</string>
    <string name="notification_tracking_close">Beenden</string>
    <string name="notification_tracking_open">Öffnen</string>

    <string name="introduction_heading">Lies dir bitte kurz folgende Infos durch, damit du weißt, was hier abgeht.</string>
    <string name="introduction_own_icon">So siehst du aus:\u0020</string>
    <string name="introduction_other_icon">So sehen alle anderen User aus:\u0020</string>
    <string name="introduction_close_app">Wenn du nicht mehr getrackt werden willst, schließe die App einfach wieder.</string>
    <string name="introduction_careful">Sei bitte vorsichtig und achte lieber auf den Verkehr, als auf die App.</string>
    <string name="introduction_bugs">Wenn irgendwas nicht richtig funktioniert, sag uns bitte bescheid, indem du auf den Feedback-Button in den Settings klickst.</string>
    <string name="introduction_requisites">Damit alles richtig funktioniert brauchst du eine halbwegs stabile mobile Internet-Verbindung und aktiviertes GPS.</string>

</resources>


text:
using System.Threading.Tasks;
using CheetahTesting;
using LabLog.WriteTests.Steps;
using Xunit;

namespace LabLog.WriteTests.EntityTests
{
    public class SchoolTests
    {
        [Fact]
        public async Task SchoolCanBeCreated()
        {
            await CTest<WriteSchoolContext>
                .Given(c => { })
                .When(i => i.CreateASchool("Test School Name"))
                .Then(t => t.SchoolIsCreated())
                .And(t => t.IdIsSet())
                .And(t => t.SchoolCreatedEventRaised("Test School Name"))
                .And(t => t.EventHasSchoolId())
                .ExecuteAsync();
        }

        [Fact]
        public async Task SchoolCanBeNamed()
        {
            await CTest<WriteSchoolContext>
                .Given(a => a.School())
                .When(i => i.NameASchool())
                .Then(t => t.SchoolNameChangedEventRaised())
                .And(t => t.EventHasSchoolName())
                .ExecuteAsync();
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
laughing-adventure
==================


text:
package com.googleinterns.smb.common;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

import androidx.core.content.ContextCompat;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.JointType;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.maps.android.PolyUtil;
import com.googleinterns.smb.MainActivity;
import com.googleinterns.smb.R;

import java.util.List;

/**
 * Map handler to display navigation path from merchant to customer location.
 * See {@link com.googleinterns.smb.NewOrderDisplayActivity} and {@link com.googleinterns.smb.OngoingOrderDisplayActivity}
 */
public class NavigationMapViewHandler implements OnMapReadyCallback {

    private GoogleMap mGoogleMap;
    private LatLng mSource;
    private LatLng mDestination;
    private String mEncodedPath;
    private boolean mIsMapLoaded = false;

    // Maps view constants
    private static final int PATH_STROKE_WIDTH_PX = 16;
    private static final int PATH_COLOR = 0xff6199f5;
    private static final int MAP_PADDING = 50;

    public NavigationMapViewHandler(SupportMapFragment mapFragment) {
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        this.mGoogleMap = googleMap;
        googleMap.getUiSettings().setZoomControlsEnabled(true);
        if (mSource != null) {
            loadMap();
        }
    }

    public void setMapAttributes(LatLng source, LatLng destination, String encodedPath) {
        this.mSource = source;
        this.mDestination = destination;
        this.mEncodedPath = encodedPath;
        if (mGoogleMap != null) {
            loadMap();
        }
    }

    private synchronized void loadMap() {
        if (mIsMapLoaded)
            return;
        // Add merchant and customer location markers
        mGoogleMap.addMarker(new MarkerOptions().position(mDestination).title("Customer"));
        mGoogleMap.addMarker(new MarkerOptions()
                .position(mSource)
                .icon(bitmapDescriptorFromVector(MainActivity.getContext()))
                .title("You")
                .flat(true));
        // Create bounding box for setting camera zoom
        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        builder.include(mSource);
        builder.include(mDestination);
        List<LatLng> path = PolyUtil.decode(mEncodedPath);
        for (int i = 0; i < path.size(); i++) {
            builder.include(path.get(i));
        }
        LatLngBounds bounds = builder.build();
        // Draw route from merchant to customer
        Polyline polyline = mGoogleMap.addPolyline(new PolylineOptions().addAll(path));
        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, MAP_PADDING));
        polyline.setColor(PATH_COLOR);
        polyline.setWidth(PATH_STROKE_WIDTH_PX);
        polyline.setJointType(JointType.ROUND);
        mIsMapLoaded = true;
    }

    private BitmapDescriptor bitmapDescriptorFromVector(Context context) {
        Drawable vectorDrawable = ContextCompat.getDrawable(context, R.drawable.ic_merchant_store);
        vectorDrawable.setBounds(0, 0, vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight());
        Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        vectorDrawable.draw(canvas);
        return BitmapDescriptorFactory.fromBitmap(bitmap);
    }
}


text:
package org.innovateuk.ifs.form.security;

import org.innovateuk.ifs.commons.security.PermissionRule;
import org.innovateuk.ifs.commons.security.PermissionRules;
import org.innovateuk.ifs.form.resource.SectionResource;
import org.innovateuk.ifs.security.BasePermissionRules;
import org.innovateuk.ifs.user.resource.UserResource;
import org.springframework.stereotype.Component;

@Component
@PermissionRules
public class SectionPermissionRules extends BasePermissionRules {
    @PermissionRule(value = "READ", description = "everyone can read sections")
    public boolean userCanReadSection(SectionResource section, UserResource user) {
        return true;
    }

    @PermissionRule(value = "UPDATE", description = "no one can update sections yet")
    public boolean userCanUpdateSection(SectionResource section, UserResource user) {
        return false;
    }

}


text:
#include <algorithm>
#include <vector>
#include <unordered_set>
#include <boost/foreach.hpp>
#include <boost/functional/hash.hpp>
#include "fast_lexical_cast.hpp"
#include "hash.h"
#include "translator.h"
#include "hg.h"
#include "grammar.h"
#include "bottom_up_parser.h"
#include "sentence_metadata.h"
#include "stringlib.h"
#include "tdict.h"
#include "viterbi.h"
#include "verbose.h"

#define foreach         BOOST_FOREACH
#define reverse_foreach BOOST_REVERSE_FOREACH

using namespace std;
static bool printGrammarsUsed = false;

struct GlueGrammar : public TextGrammar {
  // read glue grammar from file
  explicit GlueGrammar(const std::string& file);
  GlueGrammar(const std::string& goal_nt, const std::string& default_nt, const unsigned int ctf_level=0);  // "S", "X"
  virtual bool HasRuleForSpan(int i, int j, int distance) const;
};

struct PassThroughGrammar : public TextGrammar {
  PassThroughGrammar(const Lattice& input, const std::string& cat, const unsigned int ctf_level=0, const unsigned int num_pt_features=0);
  virtual bool HasRuleForSpan(int i, int j, int distance) const;
};

GlueGrammar::GlueGrammar(const string& file) : TextGrammar(file) {}

static void RefineRule(TRulePtr pt, const unsigned int ctf_level){
  for (unsigned int i=0; i<ctf_level; ++i){
    TRulePtr r(new TRule(*pt));
    pt->fine_rules_.reset(new vector<TRulePtr>);
    pt->fine_rules_->push_back(r);
    pt = r;
  }
}

GlueGrammar::GlueGrammar(const string& goal_nt, const string& default_nt, const unsigned int ctf_level) {
  TRulePtr stop_glue(new TRule("[" + goal_nt + "] ||| [" + default_nt + ",1] ||| [1]"));
  AddRule(stop_glue);
  RefineRule(stop_glue, ctf_level);
  TRulePtr glue(new TRule("[" + goal_nt + "] ||| [" + goal_nt + "] ["+ default_nt + "] ||| [1] [2] ||| Glue=1"));
  AddRule(glue);
  RefineRule(glue, ctf_level);
}

bool GlueGrammar::HasRuleForSpan(int i, int /* j */, int /* distance */) const {
  return (i == 0);
}

PassThroughGrammar::PassThroughGrammar(const Lattice& input, const string& cat, const unsigned int ctf_level, const unsigned num_pt_features) {
  unordered_set<WordID> ss;
  for (int i = 0; i < input.size(); ++i) {
    const vector<LatticeArc>& alts = input[i];
    for (int k = 0; k < alts.size(); ++k) {
      const int j = alts[k].dist2next + i;
      const string& src = TD::Convert(alts[k].label);
      if (ss.count(alts[k].label) == 0) {
        if (num_pt_features > 0) {
          int length = static_cast<int>(log(UTF8StringLen(src)) / log(1.6)) + 1;
          if (length > num_pt_features) length = num_pt_features;
          string len_feat = "PassThrough_0=1";
          len_feat[12] += length;
          TRulePtr pt(new TRule("[" + cat + "] ||| " + src + " ||| " + src + " ||| PassThrough=1 " + len_feat));
          pt->a_.push_back(AlignmentPoint(0,0));
          AddRule(pt);
          RefineRule(pt, ctf_level);
        } else {
          TRulePtr pt(new TRule("[" + cat + "] ||| " + src + " ||| " + src + " ||| PassThrough=1 "));
          pt->a_.push_back(AlignmentPoint(0,0));
          AddRule(pt);
          RefineRule(pt, ctf_level);
        }
        ss.insert(alts[k].label);
      }
    }
  }
}

bool PassThroughGrammar::HasRuleForSpan(int, int, int distance) const {
  return (distance < 4);  // TODO this isn't great, but helps with EPS lattices
}

struct SCFGTranslatorImpl {
  SCFGTranslatorImpl(const boost::program_options::variables_map& conf) :
      max_span_limit(conf["scfg_max_span_limit"].as<int>()),
      add_pass_through_rules(conf.count("add_pass_through_rules")),
      num_pt_features(conf["add_extra_pass_through_features"].as<unsigned int>()),
      goal(conf["goal"].as<string>()),
      default_nt(conf["scfg_default_nt"].as<string>()),
      use_ctf_(conf.count("coarse_to_fine_beam_prune"))
  {
    if(conf.count("grammar")){
      vector<string> gfiles = conf["grammar"].as<vector<string> >();
      for (unsigned i = 0; i < gfiles.size(); ++i) {
        if (!SILENT) cerr << "Reading SCFG grammar from " << gfiles[i] << endl;
        TextGrammar* g = new TextGrammar(gfiles[i]);
        g->SetMaxSpan(max_span_limit);
        g->SetGrammarName(gfiles[i]);
        grammars.push_back(GrammarPtr(g));
      }
      if (!SILENT) cerr << endl;
    }
    if (conf.count("scfg_extra_glue_grammar")) {
      GlueGrammar* g = new GlueGrammar(conf["scfg_extra_glue_grammar"].as<string>());
      g->SetGrammarName("ExtraGlueGrammar");
      grammars.push_back(GrammarPtr(g));
      if (!SILENT) cerr << "Adding glue grammar from file " << conf["scfg_extra_glue_grammar"].as<string>() << endl;
    }
    ctf_iterations_=0;
    if (use_ctf_){
      ctf_alpha_ = conf["coarse_to_fine_beam_prune"].as<double>();
      foreach(GrammarPtr& gp, grammars){
        ctf_iterations_ = std::max(gp->GetCTFLevels(), ctf_iterations_);
      }
      foreach(GrammarPtr& gp, grammars){
        if (gp->GetCTFLevels() != ctf_iterations_){
          cerr << "Grammar " << gp->GetGrammarName() << " has CTF level " << gp->GetCTFLevels() <<
            " but overall number of CTF levels is " << ctf_iterations_ << endl <<
            "Mixing coarse-to-fine grammars of different granularities is not supported" << endl;
          abort();
        }
      }
      show_tree_structure_ = conf.count("show_tree_structure");
      ctf_wide_alpha_ = conf["ctf_beam_widen"].as<double>();
      ctf_num_widenings_ = conf["ctf_num_widenings"].as<int>();
      ctf_exhaustive_ = (conf.count("ctf_no_exhaustive") == 0);
      assert(ctf_wide_alpha_ > 1.0);
      cerr << "Using coarse-to-fine pruning with " << ctf_iterations_ << " grammar projection(s) and alpha=" << ctf_alpha_ << endl;
      cerr << "  Coarse beam will be widened " << ctf_num_widenings_ << " times by a factor of " << ctf_wide_alpha_ << " if fine parse fails" << endl;
    }
    if (!conf.count("scfg_no_hiero_glue_grammar")){
      GlueGrammar* g = new GlueGrammar(goal, default_nt, ctf_iterations_);
      g->SetGrammarName("GlueGrammar");
      grammars.push_back(GrammarPtr(g));
      if (!SILENT) cerr << "Adding glue grammar for default nonterminal " << default_nt <<
        " and goal nonterminal " << goal << endl;
    }
 }

  const int max_span_limit;
  const bool add_pass_through_rules;
  const unsigned int num_pt_features;
  const string goal;
  const string default_nt;
  const bool use_ctf_;
  double ctf_alpha_;
  double ctf_wide_alpha_;
  int ctf_num_widenings_;
  bool ctf_exhaustive_;
  bool show_tree_structure_;
  unsigned int ctf_iterations_;
  vector<GrammarPtr> grammars;
  set<GrammarPtr> sup_grammars_;

  struct ContainedIn {
    ContainedIn(const set<GrammarPtr>& gs) : gs_(gs) {}
    bool operator()(const GrammarPtr& x) const { return gs_.find(x) != gs_.end(); }
    const set<GrammarPtr>& gs_;
  };

  void AddSupplementalGrammarFromString(const std::string& grammar_string) {
    grammars.erase(remove_if(grammars.begin(), grammars.end(), ContainedIn(sup_grammars_)), grammars.end());
    istringstream in(grammar_string);
    TextGrammar* sent_grammar = new TextGrammar(&in);
    sent_grammar->SetMaxSpan(max_span_limit);
    sent_grammar->SetGrammarName("SupFromString");
    AddSupplementalGrammar(GrammarPtr(sent_grammar));
  }

  void AddSupplementalGrammar(GrammarPtr gp) {
    sup_grammars_.insert(gp);
    grammars.push_back(gp);
  }

  void RemoveSupplementalGrammars() {
    grammars.erase(remove_if(grammars.begin(), grammars.end(), ContainedIn(sup_grammars_)), grammars.end());
    sup_grammars_.clear();
  }

  bool Translate(const string& input,
                 SentenceMetadata* smeta,
                 const vector<double>& weights,
                 Hypergraph* forest) {
    vector<GrammarPtr> glist = grammars;
    Lattice& lattice = smeta->src_lattice_;
    LatticeTools::ConvertTextOrPLF(input, &lattice);
    smeta->SetSourceLength(lattice.size());
    smeta->ComputeInputLatticeType();
    if (add_pass_through_rules){
      if (!SILENT) cerr << "Adding pass through grammar" << endl;
      PassThroughGrammar* g = new PassThroughGrammar(lattice, default_nt, ctf_iterations_, num_pt_features);
      g->SetGrammarName("PassThrough");
      glist.push_back(GrammarPtr(g));
    }
    for (unsigned gi = 0; gi < glist.size(); ++gi) {
      if(printGrammarsUsed)
        cerr << "Using grammar::" << glist[gi]->GetGrammarName() << endl;
    }
    if (!SILENT) cerr << "First pass parse... " << endl;
    ExhaustiveBottomUpParser parser(goal, glist);
    if (!parser.Parse(lattice, forest)){
      if (!SILENT) cerr << "  parse failed." << endl;
      return false;
    } else {
      // if (!SILENT) cerr << "  parse succeeded." << endl;
    }
    forest->Reweight(weights);
    if (use_ctf_) {
      Hypergraph::Node& goal_node = *(forest->nodes_.end()-1);
      foreach(unsigned edge_id, goal_node.in_edges_)
        RefineRule(forest->edges_[edge_id].rule_, ctf_iterations_);
      double alpha = ctf_alpha_;
      bool found_parse=false;
      for (int i=-1; i < ctf_num_widenings_; ++i) {
        cerr << "Coarse-to-fine source parse, alpha=" << alpha << endl;
        found_parse = true;
        Hypergraph refined_forest = *forest;
        for (unsigned j=0; j < ctf_iterations_; ++j) {
          cerr << viterbi_stats(refined_forest,"  Coarse forest",true,show_tree_structure_);
          cerr << "  Iteration " << (j+1) << ": Pruning forest... ";
          refined_forest.BeamPruneInsideOutside(1.0, false, alpha, NULL);
          cerr << "Refining forest...";
          if (RefineForest(&refined_forest)) {
            cerr << "  Refinement succeeded." << endl;
            refined_forest.Reweight(weights);
          } else {
            cerr << "  Refinement failed. Widening beam." << endl;
            found_parse = false;
            break;
          }
        }
        if (found_parse) {
          forest->swap(refined_forest);
          break;
        }
        alpha *= ctf_wide_alpha_;
      }
      if (!found_parse){
        if (ctf_exhaustive_){
          cerr << "Last resort: refining coarse forest without pruning...";
          for (unsigned j=0; j < ctf_iterations_; ++j) {
            if (RefineForest(forest)){
              cerr << "  Refinement succeeded." << endl;
              forest->Reweight(weights);
            } else {
              cerr << "  Refinement failed.  No parse found for this sentence." << endl;
              return false;
            }
          }
        } else
          return false;
      }
    }
    return true;
  }

  typedef std::pair<int, WordID> StateSplit;
  typedef std::pair<StateSplit, int> StateSplitPair;
  typedef HASH_MAP<StateSplit, int, boost::hash<StateSplit> > Split2Node;
  typedef HASH_MAP<int, vector<WordID> > Splits;

  bool RefineForest(Hypergraph* forest) {
    Hypergraph refined_forest;
    Split2Node s2n;
    HASH_MAP_RESERVED(s2n,StateSplit(-1,-1),StateSplit(-2,-2));
    Splits splits;
    HASH_MAP_RESERVED(splits,-1,-2);
    Hypergraph::Node& coarse_goal_node = *(forest->nodes_.end()-1);
    bool refined_goal_node = false;
    foreach(Hypergraph::Node& node, forest->nodes_){
      cerr << ".";
      foreach(int edge_id, node.in_edges_) {
        Hypergraph::Edge& edge = forest->edges_[edge_id];
        std::vector<int> nt_positions;
        TRulePtr& coarse_rule_ptr = edge.rule_;
        for(unsigned i=0; i< coarse_rule_ptr->f_.size(); ++i){
          if (coarse_rule_ptr->f_[i] < 0)
            nt_positions.push_back(i);
        }
        if (coarse_rule_ptr->fine_rules_ == 0) {
          cerr << "Parsing with mixed levels of coarse-to-fine granularity is currently unsupported." <<
            endl << "Could not find refinement for: " << coarse_rule_ptr->AsString() << " on edge " << edge_id << " spanning " << edge.i_ << "," << edge.j_ << endl;
          abort();
        }
        // fine rules apply only if state splits on tail nodes match fine rule nonterminals
        foreach(TRulePtr& fine_rule_ptr, *(coarse_rule_ptr->fine_rules_)) {
          Hypergraph::TailNodeVector tail;
          for (unsigned pos_i=0; pos_i<nt_positions.size(); ++pos_i){
            WordID fine_cat = fine_rule_ptr->f_[nt_positions[pos_i]];
            Split2Node::iterator it =
              s2n.find(StateSplit(edge.tail_nodes_[pos_i], fine_cat));
            if (it == s2n.end())
              break;
            else
              tail.push_back(it->second);
          }
          if (tail.size() == nt_positions.size()) {
            WordID cat = fine_rule_ptr->lhs_;
            Hypergraph::Edge* new_edge = refined_forest.AddEdge(fine_rule_ptr, tail);
            new_edge->i_ = edge.i_;
            new_edge->j_ = edge.j_;
            new_edge->feature_values_ = fine_rule_ptr->GetFeatureValues();
            new_edge->feature_values_.set_value(FD::Convert("LatticeCost"),
              edge.feature_values_.value(FD::Convert("LatticeCost")));
            Hypergraph::Node* head_node;
            Split2Node::iterator it = s2n.find(StateSplit(node.id_, cat));
            if (it == s2n.end()){
              head_node = refined_forest.AddNode(cat);
              s2n.insert(StateSplitPair(StateSplit(node.id_, cat), head_node->id_));
              splits[node.id_].push_back(cat);
              if (&node == &coarse_goal_node)
                refined_goal_node = true;
            } else
              head_node = &(refined_forest.nodes_[it->second]);
            refined_forest.ConnectEdgeToHeadNode(new_edge, head_node);
          }
        }
      }
    }
    cerr << endl;
    forest->swap(refined_forest);
    if (!refined_goal_node)
      return false;
    return true;
  }
  void OutputForest(Hypergraph* h) {
    foreach(Hypergraph::Node& n, h->nodes_){
      if (n.in_edges_.size() == 0){
        cerr << "<" << TD::Convert(-n.cat_) << ", ?, ?>" << endl;
      } else {
        cerr << "<" << TD::Convert(-n.cat_) << ", " << h->edges_[n.in_edges_[0]].i_ << ", " << h->edges_[n.in_edges_[0]].j_ << ">" << endl;
      }
      foreach(int edge_id, n.in_edges_){
        cerr << "    " << h->edges_[edge_id].rule_->AsString() << endl;
      }
    }
  }
};

/*
Called once from cdec.cc to setup the initial SCFG translation structure backend
*/
SCFGTranslator::SCFGTranslator(const boost::program_options::variables_map& conf) :
  pimpl_(new SCFGTranslatorImpl(conf)) {}

/*
Called for each sentence to perform translation using the SCFG backend
*/
bool SCFGTranslator::TranslateImpl(const string& input,
                               SentenceMetadata* smeta,
                               const vector<double>& weights,
                               Hypergraph* minus_lm_forest) {

  return pimpl_->Translate(input, smeta, weights, minus_lm_forest);
}

//
// Check for extra grammars in the sentence markup, for use with sentence specific grammars
//
void SCFGTranslator::ProcessMarkupHintsImpl(const map<string, string>& kv) {
  if (kv.find("grammar0") != kv.end()) {
    cerr << "SGML tag grammar0 is not expected (order is: grammar, grammar1, grammar2, ...)\n";
    abort();
  }
  unsigned gc = 0;
  set<string> loaded;
  while(true) {
    string gkey = "grammar";
    if (gc > 0) gkey += boost::lexical_cast<string>(gc);
    ++gc;
    map<string,string>::const_iterator it = kv.find(gkey);
    if (it == kv.end()) break;
    const string& gfile = it->second;
    if (loaded.count(gfile) == 1) {
      cerr << "Attempting to load " << gfile << " twice!\n";
      abort();
    }
    loaded.insert(gfile);
    TextGrammar* sentGrammar = new TextGrammar(gfile);
    sentGrammar->SetMaxSpan(pimpl_->max_span_limit);
    sentGrammar->SetGrammarName(gfile);
    pimpl_->AddSupplementalGrammar(GrammarPtr(sentGrammar));
  }
}

void SCFGTranslator::AddSupplementalGrammarFromString(const std::string& grammar) {
  pimpl_->AddSupplementalGrammarFromString(grammar);
}

void SCFGTranslator::AddSupplementalGrammar(GrammarPtr grammar) {
  pimpl_->AddSupplementalGrammar(grammar);
}

void SCFGTranslator::SentenceCompleteImpl() {
  pimpl_->RemoveSupplementalGrammars();
}

std::string SCFGTranslator::GetDecoderType() const {
  return "SCFG";
}


text:
package org.springframework.web.socket.server.support;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;

import static org.junit.Assert.*;
import org.junit.Test;
import org.mockito.Mockito;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.web.socket.AbstractHttpRequestTests;
import org.springframework.web.socket.WebSocketHandler;

/**
 * Test fixture for {@link OriginHandshakeInterceptor}.
 *
 * @author Sebastien Deleuze
 */
public class OriginHandshakeInterceptorTests extends AbstractHttpRequestTests {

	@Test(expected = IllegalArgumentException.class)
	public void nullAllowedOriginList() {
		new OriginHandshakeInterceptor(null);
	}

	@Test(expected = IllegalArgumentException.class)
	public void invalidAllowedOrigin() {
		new OriginHandshakeInterceptor(Arrays.asList("domain.com"));
	}

	@Test
	public void emtpyAllowedOriginList() {
		new OriginHandshakeInterceptor(Arrays.asList());
	}

	@Test
	public void validAllowedOrigins() {
		new OriginHandshakeInterceptor(Arrays.asList("http://domain.com", "https://domain.com", "*"));
	}

	@Test
	public void originValueMatch() throws Exception {
		Map<String, Object> attributes = new HashMap<String, Object>();
		WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
		this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain1.com");
		OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(Arrays.asList("http://mydomain1.com"));
		assertTrue(interceptor.beforeHandshake(request, response, wsHandler, attributes));
		assertNotEquals(servletResponse.getStatus(), HttpStatus.FORBIDDEN.value());
	}

	@Test
	public void originValueNoMatch() throws Exception {
		Map<String, Object> attributes = new HashMap<String, Object>();
		WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
		this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain1.com");
		OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(Arrays.asList("http://mydomain2.com"));
		assertFalse(interceptor.beforeHandshake(request, response, wsHandler, attributes));
		assertEquals(servletResponse.getStatus(), HttpStatus.FORBIDDEN.value());
	}

	@Test
	public void originListMatch() throws Exception {
		Map<String, Object> attributes = new HashMap<String, Object>();
		WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
		this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain2.com");
		OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(Arrays.asList("http://mydomain1.com", "http://mydomain2.com", "http://mydomain3.com"));
		assertTrue(interceptor.beforeHandshake(request, response, wsHandler, attributes));
		assertNotEquals(servletResponse.getStatus(), HttpStatus.FORBIDDEN.value());
	}

	@Test
	public void originListNoMatch() throws Exception {
		Map<String, Object> attributes = new HashMap<String, Object>();
		WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
		this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain4.com");
		OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(Arrays.asList("http://mydomain1.com", "http://mydomain2.com", "http://mydomain3.com"));
		assertFalse(interceptor.beforeHandshake(request, response, wsHandler, attributes));
		assertEquals(servletResponse.getStatus(), HttpStatus.FORBIDDEN.value());
	}

	@Test
	public void originNoMatchWithNullHostileCollection() throws Exception {
		Map<String, Object> attributes = new HashMap<String, Object>();
		WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
		this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain4.com");
		OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor();
		Set<String> allowedOrigins = new ConcurrentSkipListSet<String>();
		allowedOrigins.add("http://mydomain1.com");
		interceptor.setAllowedOrigins(allowedOrigins);
		assertFalse(interceptor.beforeHandshake(request, response, wsHandler, attributes));
		assertEquals(servletResponse.getStatus(), HttpStatus.FORBIDDEN.value());
	}

	@Test
	public void originMatchAll() throws Exception {
		Map<String, Object> attributes = new HashMap<String, Object>();
		WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
		this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain1.com");
		OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor();
		interceptor.setAllowedOrigins(Arrays.asList("*"));
		assertTrue(interceptor.beforeHandshake(request, response, wsHandler, attributes));
		assertNotEquals(servletResponse.getStatus(), HttpStatus.FORBIDDEN.value());
	}

	@Test
	public void sameOriginMatch() throws Exception {
		Map<String, Object> attributes = new HashMap<String, Object>();
		WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
		this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain2.com");
		this.servletRequest.setServerName("mydomain2.com");
		OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(Arrays.asList());
		assertTrue(interceptor.beforeHandshake(request, response, wsHandler, attributes));
		assertNotEquals(servletResponse.getStatus(), HttpStatus.FORBIDDEN.value());
	}

	@Test
	public void sameOriginNoMatch() throws Exception {
		Map<String, Object> attributes = new HashMap<String, Object>();
		WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
		this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain3.com");
		this.servletRequest.setServerName("mydomain2.com");
		OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(Arrays.asList());
		assertFalse(interceptor.beforeHandshake(request, response, wsHandler, attributes));
		assertEquals(servletResponse.getStatus(), HttpStatus.FORBIDDEN.value());
	}

}


text:
'use strict';

/**
 * The functionality behind the behaviors field in the API, supporting post-processing responses
 * @module
 */

const os = require('os'),
    fsExtra = require('fs-extra'),
    childProcess = require('child_process'),
    safeRegex = require('safe-regex'),
    csvParse = require('csv-parse'),
    buffer = require('buffer'),
    prometheus = require('prom-client'),
    xPath = require('./xpath'),
    jsonPath = require('./jsonpath'),
    helpers = require('../util/helpers.js'),
    exceptions = require('../util/errors.js'),
    behaviorsValidator = require('./behaviorsValidator.js'),
    compatibility = require('./compatibility.js');

const metrics = {
    behaviorDuration: new prometheus.Histogram({
        name: 'mb_behavior_duration_seconds',
        help: 'Time it takes to run all the behaviors',
        buckets: [0.05, 0.1, 0.2, 0.5, 1, 3],
        labelNames: ['imposter']
    })
};

// The following schemas are used by both the lookup and copy behaviors and should be kept consistent
const fromSchema = {
        _required: true,
        _allowedTypes: {
            string: {},
            object: { singleKeyOnly: true }
        },
        _additionalContext: 'the request field to select from'
    },
    intoSchema = {
        _required: true,
        _allowedTypes: { string: {} },
        _additionalContext: 'the token to replace in response fields'
    },
    usingSchema = {
        _required: true,
        _allowedTypes: { object: {} },
        method: {
            _required: true,
            _allowedTypes: { string: { enum: ['regex', 'xpath', 'jsonpath'] } }
        },
        selector: {
            _required: true,
            _allowedTypes: { string: {} }
        }
    },
    validations = {
        wait: {
            _required: true,
            _allowedTypes: { string: {}, number: { nonNegativeInteger: true } }
        },
        copy: {
            from: fromSchema,
            into: intoSchema,
            using: usingSchema
        },
        lookup: {
            key: {
                _required: true,
                _allowedTypes: { object: {} },
                from: fromSchema,
                using: usingSchema
            },
            fromDataSource: {
                _required: true,
                _allowedTypes: { object: { singleKeyOnly: true, enum: ['csv'] } },
                csv: {
                    _required: false,
                    _allowedTypes: { object: {} },
                    path: {
                        _required: true,
                        _allowedTypes: { string: {} },
                        _additionalContext: 'the path to the CSV file'
                    },
                    delimiter: {
                        _required: false,
                        _allowedTypes: { string: {} },
                        _additionalContext: 'the delimiter separator values'
                    },
                    keyColumn: {
                        _required: true,
                        _allowedTypes: { string: {} },
                        _additionalContext: 'the column header to select against the "key" field'
                    }
                }
            },
            into: intoSchema
        },
        shellTransform: {
            _required: true,
            _allowedTypes: { string: {} },
            _additionalContext: 'the path to a command line application'
        },
        decorate: {
            _required: true,
            _allowedTypes: { string: {} },
            _additionalContext: 'a JavaScript function'
        }
    };

/**
 * Validates the behavior configuration and returns all errors
 * @param {Object} config - The behavior configuration
 * @returns {Object} The array of errors
 */
function validate (config) {
    const validator = behaviorsValidator.create();
    return validator.validate(config, validations);
}

/**
 * Waits a specified number of milliseconds before sending the response.  Due to the approximate
 * nature of the timer, there is no guarantee that it will wait the given amount, but it will be close.
 * @param {Object} request - The request object
 * @param {Object} response - The response
 * @param {number} millisecondsOrFn - The number of milliseconds to wait before returning, or a function returning milliseconds
 * @param {Object} logger - The mountebank logger, useful for debugging
 * @returns {Object} A promise resolving to the response
 */
async function wait (request, response, millisecondsOrFn, logger) {
    const fn = `(${millisecondsOrFn})()`;

    let milliseconds = parseInt(millisecondsOrFn);

    if (isNaN(milliseconds)) {
        try {
            milliseconds = eval(fn);
        }
        catch (error) {
            logger.error('injection X=> ' + error);
            logger.error('    full source: ' + JSON.stringify(fn));
            return Promise.reject(exceptions.InjectionError('invalid wait injection',
                { source: millisecondsOrFn, data: error.message }));
        }
    }

    logger.debug('Waiting %s ms...', milliseconds);
    return new Promise(resolve => {
        setTimeout(() => resolve(response), milliseconds);
    });
}

function quoteForShell (obj) {
    const json = JSON.stringify(obj),
        isWindows = os.platform().indexOf('win') === 0;

    if (isWindows) {
        // Confused? Me too. All other approaches I tried were spectacular failures
        // in both 1) keeping the JSON as a single CLI arg, and 2) maintaining the inner quotes
        return `"${json.replace(/"/g, '\\"')}"`;
    }
    else {
        return `'${json}'`;
    }
}

function execShell (command, request, response, logger) {
    const exec = childProcess.exec,
        env = helpers.clone(process.env),
        maxBuffer = buffer.constants.MAX_STRING_LENGTH,
        maxShellCommandLength = 2048;

    logger.debug(`Shelling out to ${command}`);

    // Switched to environment variables because of inconsistencies in Windows shell quoting
    // Leaving the CLI args for backwards compatibility
    env.MB_REQUEST = JSON.stringify(request);
    env.MB_RESPONSE = JSON.stringify(response);

    // Windows has a pretty low character limit to the command line. When we're in danger
    // of the character limit, we'll remove the command line arguments under the assumption
    // that backwards compatibility doesn't matter when it never would have worked to begin with
    let fullCommand = `${command} ${quoteForShell(request)} ${quoteForShell(response)}`;
    if (fullCommand.length >= maxShellCommandLength) {
        fullCommand = command;
    }

    return new Promise((resolve, reject) => {
        exec(fullCommand, { env, maxBuffer }, (error, stdout, stderr) => {
            if (error) {
                if (stderr) {
                    logger.error(stderr);
                }
                reject(error.message);
            }
            else {
                logger.debug(`Shell returned '${stdout}'`);
                try {
                    resolve(JSON.parse(stdout));
                }
                catch (err) {
                    reject(`Shell command returned invalid JSON: '${stdout}'`);
                }
            }
        });
    });
}

/**
 * Runs the response through a shell function, passing the JSON in as stdin and using
 * stdout as the new response
 * @param {Object} request - The request
 * @param {Object} response - The response
 * @param {string} command - The shell command to execute
 * @param {Object} logger - The mountebank logger, useful in debugging
 * @returns {Object}
 */
function shellTransform (request, response, command, logger) {
    return execShell(command, request, response, logger);
}

/**
 * Runs the response through a post-processing function provided by the user
 * @param {Object} originalRequest - The request object, in case post-processing depends on it
 * @param {Object} response - The response
 * @param {Function} fn - The function that performs the post-processing
 * @param {Object} logger - The mountebank logger, useful in debugging
 * @param {Object} imposterState - The user controlled state variable
 * @returns {Object}
 */
function decorate (originalRequest, response, fn, logger, imposterState) {
    const config = {
            request: helpers.clone(originalRequest),
            response,
            logger,
            state: imposterState
        },
        injected = `(${fn})(config, response, logger);`; // backwards compatibility

    compatibility.downcastInjectionConfig(config);

    try {
        // Support functions that mutate response in place and those
        // that return a new response
        let result = eval(injected);
        if (!result) {
            result = response;
        }
        return Promise.resolve(result);
    }
    catch (error) {
        logger.error('injection X=> ' + error);
        logger.error('    full source: ' + JSON.stringify(injected));
        logger.error('    config: ' + JSON.stringify(config));
        return Promise.reject(exceptions.InjectionError('invalid decorator injection', { source: injected, data: error.message }));
    }
}

function getKeyIgnoringCase (obj, expectedKey) {
    return Object.keys(obj).find(key => {
        if (key.toLowerCase() === expectedKey.toLowerCase()) {
            return key;
        }
        else {
            return undefined;
        }
    });
}

function getFrom (obj, from) {
    const isObject = helpers.isObject;

    if (typeof obj === 'undefined') {
        return undefined;
    }
    else if (isObject(from)) {
        const keys = Object.keys(from);
        return getFrom(obj[keys[0]], from[keys[0]]);
    }
    else {
        const result = obj[getKeyIgnoringCase(obj, from)];

        // Some request fields, like query parameters, can be multi-valued
        if (Array.isArray(result)) {
            return result[0];
        }
        else {
            return result;
        }
    }
}

function regexFlags (options) {
    let result = '';
    if (options && options.ignoreCase) {
        result += 'i';
    }
    if (options && options.multiline) {
        result += 'm';
    }
    return result;
}

function getMatches (selectionFn, selector, logger) {
    const matches = selectionFn();

    if (matches && matches.length > 0) {
        return matches;
    }
    else {
        logger.debug('No match for "%s"', selector);
        return [];
    }
}

function regexValue (from, config, logger) {
    const regex = new RegExp(config.using.selector, regexFlags(config.using.options)),
        selectionFn = () => regex.exec(from);

    if (!safeRegex(regex)) {
        logger.warn(`If mountebank becomes unresponsive, it is because of this unsafe regular expression: ${config.using.selector}`);
    }
    return getMatches(selectionFn, regex, logger);
}

function xpathValue (from, config, logger) {
    const selectionFn = () => {
        return xPath.select(config.using.selector, config.using.ns, from, logger);
    };
    return getMatches(selectionFn, config.using.selector, logger);
}

function jsonpathValue (from, config, logger) {
    const selectionFn = () => {
        return jsonPath.select(config.using.selector, from, logger);
    };
    return getMatches(selectionFn, config.using.selector, logger);
}

function globalStringReplace (str, substring, newSubstring, logger) {
    if (substring !== newSubstring) {
        logger.debug('Replacing %s with %s', JSON.stringify(substring), JSON.stringify(newSubstring));
        return str.split(substring).join(newSubstring);
    }
    else {
        return str;
    }
}

function globalObjectReplace (obj, replacer) {
    const isObject = helpers.isObject,
        renames = {};

    Object.keys(obj).forEach(key => {
        if (typeof obj[key] === 'string') {
            obj[key] = replacer(obj[key]);
        }
        else if (isObject(obj[key])) {
            globalObjectReplace(obj[key], replacer);
        }
        var newKey = replacer(key);
        if (newKey !== key) {
            renames[key] = newKey;
        }
    });
    Object.keys(renames).forEach(key => {
        obj[renames[key]] = obj[key];
        delete obj[key];
    });
}

function replaceArrayValuesIn (response, token, values, logger) {
    const replacer = field => {
        values.forEach(function (replacement, index) {
            // replace ${TOKEN}[1] with indexed element
            const indexedToken = `${token}[${index}]`;
            field = globalStringReplace(field, indexedToken, replacement, logger);
        });
        if (values.length > 0) {
            // replace ${TOKEN} with first element
            field = globalStringReplace(field, token, values[0], logger);
        }
        return field;
    };

    globalObjectReplace(response, replacer);
}

/**
 * Copies a value from the request and replaces response tokens with that value
 * @param {Object} originalRequest - The request object, in case post-processing depends on it
 * @param {Object} response - The response
 * @param {Function} copyConfig - The config to copy
 * @param {Object} logger - The mountebank logger, useful in debugging
 * @returns {Object}
 */
function copy (originalRequest, response, copyConfig, logger) {
    const from = getFrom(originalRequest, copyConfig.from),
        using = copyConfig.using || {},
        fnMap = { regex: regexValue, xpath: xpathValue, jsonpath: jsonpathValue },
        values = fnMap[using.method](from, copyConfig, logger);

    replaceArrayValuesIn(response, copyConfig.into, values, logger);
    return response;
}

function containsKey (headers, keyColumn) {
    const key = Object.values(headers).find(value => value === keyColumn);

    return helpers.defined(key);
}

function createRowObject (headers, rowArray) {
    const row = {};
    rowArray.forEach(function (value, index) {
        row[headers[index]] = value;
    });
    return row;
}

function selectRowFromCSV (csvConfig, keyValue, logger) {
    const delimiter = csvConfig.delimiter || ',',
        inputStream = fsExtra.createReadStream(csvConfig.path),
        parser = csvParse.parse({ delimiter: delimiter }),
        pipe = inputStream.pipe(parser);
    let headers;

    return new Promise(resolve => {
        inputStream.on('error', e => {
            logger.error('Cannot read ' + csvConfig.path + ': ' + e);
            resolve({});
        });

        pipe.on('data', function (rowArray) {
            if (!helpers.defined(headers)) {
                headers = rowArray;
                const keyOnHeader = containsKey(headers, csvConfig.keyColumn);
                if (!keyOnHeader) {
                    logger.error('CSV headers "' + headers + '" with delimiter "' + delimiter + '" does not contain keyColumn:"' + csvConfig.keyColumn + '"');
                    resolve({});
                }
            }
            else {
                const row = createRowObject(headers, rowArray);
                if (helpers.defined(row[csvConfig.keyColumn]) && row[csvConfig.keyColumn].localeCompare(keyValue) === 0) {
                    resolve(row);
                }
            }
        });

        pipe.on('error', e => {
            logger.debug('Error: ' + e);
            resolve({});
        });

        pipe.on('end', () => {
            resolve({});
        });
    });
}

function lookupRow (lookupConfig, originalRequest, logger) {
    const from = getFrom(originalRequest, lookupConfig.key.from),
        fnMap = { regex: regexValue, xpath: xpathValue, jsonpath: jsonpathValue },
        keyValues = fnMap[lookupConfig.key.using.method](from, lookupConfig.key, logger),
        index = lookupConfig.key.index || 0;

    if (lookupConfig.fromDataSource.csv) {
        return selectRowFromCSV(lookupConfig.fromDataSource.csv, keyValues[index], logger);
    }
    else {
        return Promise.resolve({});
    }
}

function replaceObjectValuesIn (response, token, values, logger) {
    const replacer = field => {
        Object.keys(values).forEach(key => {
            // replace ${TOKEN}["key"] and ${TOKEN}['key'] and ${TOKEN}[key]
            ['"', "'", ''].forEach(function (quoteChar) {
                const quoted = `${token}[${quoteChar}${key}${quoteChar}]`;
                field = globalStringReplace(field, quoted, values[key], logger);
            });
        });
        return field;
    };

    globalObjectReplace(response, replacer);
}

/**
 * Looks up request values from a data source and replaces response tokens with the resulting data
 * @param {Object} originalRequest - The request object
 * @param {Object} response - The response
 * @param {Function} lookupConfig - The lookup configurations
 * @param {Object} logger - The mountebank logger, useful in debugging
 * @returns {Object}
 */
async function lookup (originalRequest, response, lookupConfig, logger) {
    try {
        const row = await lookupRow(lookupConfig, originalRequest, logger);
        replaceObjectValuesIn(response, lookupConfig.into, row, logger);
    }
    catch (error) {
        logger.error(error);
    }
    return response;
}

/**
 * The entry point to execute all behaviors provided in the API
 * @param {Object} request - The request object
 * @param {Object} response - The response generated from the stubs
 * @param {Object} behaviors - The behaviors specified in the API
 * @param {Object} logger - The mountebank logger, useful for debugging
 * @param {Object} imposterState - the user-controlled state variable
 * @returns {Object}
 */
async function execute (request, response, behaviors, logger, imposterState) {
    const fnMap = {
        wait: wait,
        copy: copy,
        lookup: lookup,
        shellTransform: shellTransform,
        decorate: decorate
    };
    let result = Promise.resolve(response);

    if (!behaviors || behaviors.length === 0 || request.isDryRun) {
        return result;
    }

    logger.debug('using stub response behavior ' + JSON.stringify(behaviors));
    behaviors.forEach(behavior => {
        Object.keys(behavior).forEach(key => {
            if (fnMap[key]) {
                result = result.then(newResponse => fnMap[key](request, newResponse, behavior[key], logger, imposterState));
            }
        });
    });

    const observeBehaviorDuration = metrics.behaviorDuration.startTimer(),
        transformed = await result;
    observeBehaviorDuration({ imposter: logger.scopePrefix });
    return transformed;
}

module.exports = {
    validate,
    execute
};


text:
<?xml version="1.0" encoding="UTF-8"?>
<ldswebml type="image" uri="/media-library/images/seasons/spring#melting-snow-902169" xml:lang="eng" locale="eng" status="publish" xmlns:its="http://www.w3.org/2005/11/its">
  <search-meta>
      <uri-title its:translate="no">melting-snow-902169</uri-title>
      <source>melting-snow-902169</source>
      <publication-type its:translate="no">image</publication-type>
      <media-type its:translate="no">image</media-type>
      <publication value="ldsorg">LDS.org</publication>
      <publication-date value="2012-10-23">2012-10-23</publication-date>
      <title/>
      <description/>
      <collections>
          <collection its:translate="no">global-search</collection>
          <collection its:translate="no">media</collection>
          <collection its:translate="no">images</collection>
      </collections>
      <subjects>
          <subject>Bushes</subject>
          <subject>Plants</subject>
          <subject>Snow</subject>
          <subject>Spring (Season)</subject>
          <subject>Winter</subject>
      </subjects>
  </search-meta>
  <no-search>
      <path>http://media.ldscdn.org/images/media-library/seasons/spring/melting-snow-902169-gallery.jpg</path>
      <images>
          http://media.ldscdn.org/images/media-library/seasons/spring/melting-snow-902169-thumbnail.jpg
      </images>
          <download-urls>
              <download-url>
                  Mobile
                  <size>237 KB</size>
                  <path>http://media.ldscdn.org/images/media-library/seasons/spring/melting-snow-902169-mobile.jpg</path>
              </download-url>
              <download-url>
                  Tablet
                  <size>324 KB</size>
                  <path>http://media.ldscdn.org/images/media-library/seasons/spring/melting-snow-902169-tablet.jpg</path>
              </download-url>
              <download-url>
                  Print
                  <size>687 KB</size>
                  <path>http://media.ldscdn.org/images/media-library/seasons/spring/melting-snow-902169-print.jpg</path>
              </download-url>
              <download-url>
                  Wallpaper
                  <size>1.1 MB</size>
                  <path>http://media.ldscdn.org/images/media-library/seasons/spring/melting-snow-902169-wallpaper.jpg</path>
              </download-url>
          </download-urls>
  </no-search>
</ldswebml>


text:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2014 The Chromium Authors. All rights reserved.
     Use of this source code is governed by a BSD-style license that can be
     found in the LICENSE file. -->

<resources>
    <!-- Preferences -->
    <style name="PreferencesTheme" parent="ThemeWithActionBar">
        <item name="android:textColorLink">@color/pref_accent_color</item>
        <item name="android:buttonStyle">@style/PreferenceButtonStyle</item>
        <item name="android:alertDialogTheme">@style/PreferencesDialogTheme</item>
        <item name="android:colorButtonNormal">@color/btn_default_preferences</item>
        <item name="android:editTextBackground">@drawable/abc_edit_text_material</item>
        <item name="android:editTextStyle">@style/PreferenceEditTextStyle</item>
    </style>
    <style name="PreferencesDialogTheme" parent="@android:style/Theme.Material.Light.Dialog.Alert">
        <item name="android:colorAccent">@color/pref_accent_color</item>
    </style>
    <style name="PreferenceButtonStyle" parent="@android:style/Widget.Material.Light.Button">
        <item name="android:textColor">#fff</item>
    </style>
    <style name="PreferenceCategoryWithButtonStyle">
        <item name="android:paddingStart">?android:attr/listPreferredItemPaddingStart</item>
        <item name="android:paddingEnd">4dp</item>
    </style>
    <style name="PreferenceCategoryTextStyle">
        <item name="android:fontFamily">sans-serif-medium</item>
        <item name="android:textColor">@color/pref_accent_color</item>
        <item name="android:textSize">14sp</item>
    </style>
    <style name="PreferenceEditTextStyle" parent="Widget.AppCompat.EditText">
        <item name="android:backgroundTint">@color/edit_text_tint</item>
    </style>
    <style name="PreferenceTitle">
        <item name="android:ellipsize">end</item>
        <item name="android:singleLine">true</item>
        <item name="android:textAppearance">?android:attr/textAppearanceListItem</item>
    </style>
    <style name="PreferenceSummary">
        <item name="android:textAppearance">?android:attr/textAppearanceListItemSecondary</item>
        <item name="android:textColor">?android:attr/textColorSecondary</item>
    </style>
    <style name="PreferenceLayoutBase">
        <item name="android:background">?android:attr/activatedBackgroundIndicator</item>
        <item name="android:minHeight">?android:attr/listPreferredItemHeightSmall</item>
        <item name="android:paddingTop">16dp</item>
        <item name="android:paddingBottom">16dp</item>
        <item name="android:paddingStart">?android:attr/listPreferredItemPaddingStart</item>
        <item name="android:paddingEnd">?android:attr/listPreferredItemPaddingEnd</item>
    </style>
    <style name="PreferenceLayout" parent="PreferenceLayoutBase" />

</resources>


text:
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// 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.
// ----------------------------------------------------------------------------------

using Hyak.Common;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.Azure.Common.OData;
using Microsoft.Azure.Gallery;
using Microsoft.Azure.Gallery.Models;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;
using ProjectResources = Microsoft.Azure.Commands.Resources.Properties.Resources;

namespace Microsoft.Azure.Commands.Resources.Models
{
    public class GalleryTemplatesClient
    {
        public IGalleryClient GalleryClient { get; set; }

        public GalleryTemplatesClient(IAzureContext context)
            : this(AzureSession.Instance.ClientFactory.CreateClient<GalleryClient>(context, AzureEnvironment.Endpoint.Gallery))
        {

        }

        public GalleryTemplatesClient(IGalleryClient galleryClient)
        {
            GalleryClient = galleryClient;
        }

        /// <summary>
        /// Parameterless constructor for mocking
        /// </summary>
        public GalleryTemplatesClient()
        {

        }
    }
}


text:
function os::test::junit::declare_suite_start() {
    local suite_name=$1
    local num_suites=${NUM_OS_JUNIT_SUITES_IN_FLIGHT:-0}

    echo "=== BEGIN TEST SUITE github.com/openshift/origin/test/${suite_name} ===" >> "${JUNIT_REPORT_OUTPUT:-/dev/null}"
    NUM_OS_JUNIT_SUITES_IN_FLIGHT=$(( ${num_suites} + 1 ))
    export NUM_OS_JUNIT_SUITES_IN_FLIGHT
}
readonly -f os::test::junit::declare_suite_start

# os::test::junit::declare_suite_end prints a message declaring the end of a test suite
# If there aren't any suites in flight, this function will fail.
#
# Globals:
#  - JUNIT_REPORT_OUTPUT
#  - NUM_OS_JUNIT_SUITES_IN_FLIGHT
# Arguments:
#  - 1: the suite name that is starting
# Returns:
#  - export/decrement NUM_OS_JUNIT_SUITES_IN_FLIGHT
function os::test::junit::declare_suite_end() {
    local num_suites=${NUM_OS_JUNIT_SUITES_IN_FLIGHT:-0}
    if [[ "${num_suites}" -lt "1" ]]; then
        # we can't end a suite if none have been started yet
        echo "[ERROR] jUnit suite marker could not be placed, expected suites in flight, got ${num_suites}"
        return 1
    fi

    echo "=== END TEST SUITE ===" >> "${JUNIT_REPORT_OUTPUT:-/dev/null}"
    NUM_OS_JUNIT_SUITES_IN_FLIGHT=$(( ${num_suites} - 1 ))
    export NUM_OS_JUNIT_SUITES_IN_FLIGHT
}
readonly -f os::test::junit::declare_suite_end

# os::test::junit::declare_test_start prints a message declaring the start of a test case
# If there is already a test marked as being in flight, this function will fail.
#
# Globals:
#  - JUNIT_REPORT_OUTPUT
#  - NUM_OS_JUNIT_TESTS_IN_FLIGHT
# Arguments:
#  None
# Returns:
#  - increment NUM_OS_JUNIT_TESTS_IN_FLIGHT
function os::test::junit::declare_test_start() {
    local num_tests=${NUM_OS_JUNIT_TESTS_IN_FLIGHT:-0}
    if [[ "${num_tests}" -ne "0" ]]; then
        # someone's declaring the starting of a test when a test is already in flight
        echo "[ERROR] jUnit test marker could not be placed, expected no tests in flight, got ${num_tests}"
        return 1
    fi

    local num_suites=${NUM_OS_JUNIT_SUITES_IN_FLIGHT:-0}
    if [[ "${num_suites}" -lt "1" ]]; then
        # we can't end a test if no suites are in flight
        echo "[ERROR] jUnit test marker could not be placed, expected suites in flight, got ${num_suites}"
        return 1
    fi

    echo "=== BEGIN TEST CASE ===" >> "${JUNIT_REPORT_OUTPUT:-/dev/null}"
    NUM_OS_JUNIT_TESTS_IN_FLIGHT=$(( ${num_tests} + 1 ))
    export NUM_OS_JUNIT_TESTS_IN_FLIGHT
}
readonly -f os::test::junit::declare_test_start

# os::test::junit::declare_test_end prints a message declaring the end of a test case
# If there is no test marked as being in flight, this function will fail.
#
# Globals:
#  - JUNIT_REPORT_OUTPUT
#  - NUM_OS_JUNIT_TESTS_IN_FLIGHT
# Arguments:
#  None
# Returns:
#  - decrement NUM_OS_JUNIT_TESTS_IN_FLIGHT
function os::test::junit::declare_test_end() {
    local num_tests=${NUM_OS_JUNIT_TESTS_IN_FLIGHT:-0}
    if [[ "${num_tests}" -ne "1" ]]; then
        # someone's declaring the end of a test when a test is not in flight
        echo "[ERROR] jUnit test marker could not be placed, expected one test in flight, got ${num_tests}"
        return 1
    fi

    echo "=== END TEST CASE ===" >> "${JUNIT_REPORT_OUTPUT:-/dev/null}"
    NUM_OS_JUNIT_TESTS_IN_FLIGHT=$(( ${num_tests} - 1 ))
    export NUM_OS_JUNIT_TESTS_IN_FLIGHT
}
readonly -f os::test::junit::declare_test_end

# os::test::junit::check_test_counters checks that we do not have any test suites or test cases in flight
# This function should be called at the very end of any test script using jUnit markers to make sure no error in
# marking has occurred.
#
# Globals:
#  - NUM_OS_JUNIT_SUITES_IN_FLIGHT
#  - NUM_OS_JUNIT_TESTS_IN_FLIGHT
# Arguments:
#  None
# Returns:
#  None
function os::test::junit::check_test_counters() {
    if [[ "${NUM_OS_JUNIT_SUITES_IN_FLIGHT-}" -ne "0" ]]; then
        echo "[ERROR] Expected no test suites to be marked as in-flight at the end of testing, got ${NUM_OS_JUNIT_SUITES_IN_FLIGHT-}"
        return 1
    elif [[ "${NUM_OS_JUNIT_TESTS_IN_FLIGHT-}" -ne "0" ]]; then
        echo "[ERROR] Expected no test cases to be marked as in-flight at the end of testing, got ${NUM_OS_JUNIT_TESTS_IN_FLIGHT-}"
        return 1
    fi
}
readonly -f os::test::junit::check_test_counters

# os::test::junit::reconcile_output appends the necessary suite and test end statements to the jUnit output file
# in order to ensure that the file is in a consistent state to allow for parsing
#
# Globals:
#  - NUM_OS_JUNIT_SUITES_IN_FLIGHT
#  - NUM_OS_JUNIT_TESTS_IN_FLIGHT
# Arguments:
#  None
# Returns:
#  None
function os::test::junit::reconcile_output() {
    if [[ "${NUM_OS_JUNIT_TESTS_IN_FLIGHT:-0}" = "1" ]]; then
        os::test::junit::declare_test_end
    fi

    for (( i = 0; i < ${NUM_OS_JUNIT_SUITES_IN_FLIGHT:-0}; i++ )); do
        os::test::junit::declare_suite_end
    done
}
readonly -f os::test::junit::reconcile_output

# os::test::junit::generate_report determines which type of report is to
# be generated and does so from the raw output of the tests.
#
# Globals:
#  - JUNIT_REPORT_OUTPUT
#  - ARTIFACT_DIR
# Arguments:
#  None
# Returns:
#  None
function os::test::junit::generate_report() {
    if [[ -z "${JUNIT_REPORT_OUTPUT:-}" ||
          -n "${JUNIT_REPORT_OUTPUT:-}" && ! -s "${JUNIT_REPORT_OUTPUT:-}" ]]; then
        # we can't generate a report
        return
    fi

    if grep -q "=== END TEST CASE ===" "${JUNIT_REPORT_OUTPUT}"; then
        os::test::junit::reconcile_output
        os::test::junit::check_test_counters
        os::test::junit::internal::generate_report "oscmd"
    fi
}

# os::test::junit::internal::generate_report generats an XML jUnit
# report for either `os::cmd` or `go test`, based on the passed
# argument. If the `junitreport` binary is not present, it will be built.
#
# Globals:
#  - JUNIT_REPORT_OUTPUT
#  - ARTIFACT_DIR
# Arguments:
#  - 1: specify which type of tests command output should junitreport read
# Returns:
#  export JUNIT_REPORT_NUM_FAILED
function os::test::junit::internal::generate_report() {
    local report_type="$1"
    os::util::ensure::built_binary_exists 'junitreport'

    local report_file
    report_file="$( mktemp "${ARTIFACT_DIR}/${report_type}_report_XXXXX" ).xml"
    os::log::info "jUnit XML report placed at $( os::util::repository_relative_path ${report_file} )"
    junitreport --type "${report_type}"             \
                --suites nested                     \
                --roots github.com/openshift/origin \
                --output "${report_file}"           \
                <"${JUNIT_REPORT_OUTPUT}"

    local summary
    summary=$( junitreport summarize <"${report_file}" )

    JUNIT_REPORT_NUM_FAILED="$( grep -oE "[0-9]+ failed" <<<"${summary}" )"
    export JUNIT_REPORT_NUM_FAILED

    echo "${summary}"
}


text:
using Hyperboliq.Dialects;
using Hyperboliq.Domain;
using NUnit.Framework;

namespace Hyperboliq.Tests.TokenGeneration
{
    [TestFixture]
    public class TokenGeneration_SetOperationTests
    {

        [Test]
        public void ItCanDoASimpleUnion()
        {
            var expr =
                SetOperations.Union(
                    Select.Star<Person>()
                          .From<Person>()
                          .Where<Person>(p => p.Age > 42),
                    Select.Star<Person>()
                          .From<Person>()
                          .Where<Person>(p => p.Name == "Kalle")
                    );
            var result = expr.ToSqlExpression();
            Assert.That(result, Is.EqualTo(TokenGeneration_SetOperationTests_Results.simpleUnionExpression));
        }

        [Test]
        public void ItCanDoASimpleUnionAll()
        {
            var expr =
                SetOperations.UnionAll(
                    Select.Star<Person>()
                          .From<Person>()
                          .Where<Person>(p => p.Age > 42),
                    Select.Star<Person>()
                          .From<Person>()
                          .Where<Person>(p => p.Name == "Kalle")
                    );
            var result = expr.ToSqlExpression();
            Assert.That(result, Is.EqualTo(TokenGeneration_SetOperationTests_Results.simpleUnionAllExpression));
        }

        [Test]
        public void ItCanDoASimpleIntersect()
        {
            var expr = SetOperations.Intersect(
                Select.Star<Person>()
                      .From<Person>(),
                Select.Star<Person>()
                      .From<Person>()
                      .Where<Person>(p => p.Age > 42));
            var result = expr.ToSqlExpression();
            Assert.That(result, Is.EqualTo(TokenGeneration_SetOperationTests_Results.simpleIntersectExpression));
        }

        [Test]
        public void ItCanDoASimpleMinus()
        {
            var expr = SetOperations.Minus(
                Select.Star<Person>()
                      .From<Person>(),
                Select.Star<Person>()
                      .From<Person>()
                      .Where<Person>(p => p.Age > 42));
            var result = expr.ToSqlExpression();
            Assert.That(result, Is.EqualTo(TokenGeneration_SetOperationTests_Results.simpleMinusExpression));
        }

        [Test]
        public void ItCanDoAUnionsWithCommonTableExpressions()
        {
            var identifier = Table<Person>.WithTableAlias("cte");
            var expr =
                With.Table(
                        identifier,
                        SetOperations.Union(
                            Select.Star<Person>().From<Person>().Where<Person>(p => p.Age > 42),
                            Select.Star<Person>().From<Person>().Where<Person>(p => p.Name == "Kalle")))
                    .Query(
                        SetOperations.Union(
                            Select.Star(identifier).From(identifier).Where<Person>(identifier, p => p.Age == 50),
                            Select.Star(identifier).From(identifier).Where<Person>(identifier, p => p.Name == "Kalle")));

            var result = expr.ToSqlExpression();
            var sql = SqlGen.SqlifyExpression(AnsiSql.Dialect, result);
            var sql2 = SqlGen.SqlifyExpression(AnsiSql.Dialect, TokenGeneration_SetOperationTests_Results.unionsInACommonTableExpression);
            Assert.That(result, Is.EqualTo(TokenGeneration_SetOperationTests_Results.unionsInACommonTableExpression));
        }
    }
}


text:
"use strict";

var arrays  = require("./utils/arrays"),
    objects = require("./utils/objects");

var PEG = {
  /* PEG.js version (uses semantic versioning). */
  VERSION: "0.9.0",

  GrammarError: require("./grammar-error"),
  parser:       require("./parser"),
  compiler:     require("./compiler"),

  /*
   * Generates a parser from a specified grammar and returns it.
   *
   * The grammar must be a string in the format described by the metagramar in
   * the parser.pegjs file.
   *
   * Throws |PEG.parser.SyntaxError| if the grammar contains a syntax error or
   * |PEG.GrammarError| if it contains a semantic error. Note that not all
   * errors are detected during the generation and some may protrude to the
   * generated parser and cause its malfunction.
   */
  buildParser: function(grammar) {
    function convertPasses(passes) {
      var converted = {}, stage;

      for (stage in passes) {
        if (passes.hasOwnProperty(stage)) {
          converted[stage] = objects.values(passes[stage]);
        }
      }

      return converted;
    }

    var options = arguments.length > 1 ? objects.clone(arguments[1]) : {},
        plugins = "plugins" in options ? options.plugins : [],
        config  = {
          parser: this.parser,
          passes: convertPasses(this.compiler.passes)
        };

    arrays.each(plugins, function(p) { p.use(config, options); });

    return this.compiler.compile(
      config.parser.parse(grammar),
      config.passes,
      options
    );
  }
};

module.exports = PEG;


text:
layout: post
title: Recording Brain Waves to MongoDB
desription: A system to record EEG samples, store them on a remote server, and exposing the server to business intelligence platforms.
categories: data
excerpt:
tags: [Electroencephalography, EEG, NeuroSky, iOS, NodeJS, MongoDB, Tableau]
series: MindWave Mobile 2+
image:
    feature: darius-bashar-529461-unsplash.jpg
    credit: Darius Bashar
    thumbnail: mindwave-journaler-thumbnail.png
comments: true
custom_css:
custom_js:
---

# Description
This project takes brain wave readings from a MindWave Mobile 2+, transmits them to an iOS app via Bluetooth LE.  The iOS app makes  calls to a remote Node server, which is a minimal REST API, passing off the brain wave sample.  The Node server stores the data on a MongoDB server.  The MongoDB server is then exposed to business intelligence applications use with MongoDB BI Connector.  Lastly, using Tableau Professional Desktop, the data is accessed and visualizations created.

Whew.

To recap:
* [MindWave Mobile 2+](https://www.sparkfun.com/products/14758)
* [iOS App](https://github.com/Ladvien/MindWaveJournaler) (tentatively named Mind Wave Journaler; Swift)
* [REST Server](https://github.com/Ladvien/mind-wave-journal-server) (mind-wave-journaler; NodeJS)
* [MongoDB BI Connector Server](https://www.mongodb.com/products/bi-connector)
* [Tableau Desktop Professional](https://www.tableau.com/products/desktop)

The end result is a system which could allow a remote EEG analyst to examine samples nearly in real time.

![eeg-visualization](/images/eeg_poc_tableau_viz.png)

Below, I'm going to show how I was able to setup the system.  But, before that a few words of warning.

## Gotchas

### Hacker Haters
This isn't a hacker friendly project.  It relies on several paid licenses, an Apple Developer License ($99) and Tableau Desktop Professional ($10,000,000,000 or something).  Of course, the central piece of hardware, the MindWave Mobile, is also $99, but I think that one is fair.  Oh! Let's not forget, even though you bought an Apple Developer license, you still need a Mac (or Hackintosh) to compile the app.

However, as a proof-of-concept, I think it's solid.  Hopefully a good hacker will be able to see how several tweaks in the system could make it _dirt_ cheap to deploy.

### Mimimum Viable Hack..er, Product
The source code provided here is a [minimally viable](https://en.wikipedia.org/wiki/Minimum_viable_product).  Fancy words meaning, only _base_ functionality was implemented.  There many other things which could be done to improve each piece of the system.

Not to be a douche, but please don't point them out.  That's the only thing I ask for providing this _free_ information.

There are many improvements I know can be made.  The reason they were not made had nothing to do with my ignorance (well, at least a majority of them), but rather my time constraints.

### I Hate Tableau
That's it.  I hate Tableau.

## Getting Started

Let's make a list of what's needed before beginning this project.

* Mac (to compile the iOS app)
* [MindWave Mobile 2+](https://www.sparkfun.com/products/14758)
* A remote server running Ubuntu 16.04 (theoretically a local server will work, but you'll be on your own)
* [Apple Developer License](https://developer.apple.com/programs/)
* [Xcode](https://developer.apple.com/xcode/), with Swift 4.1
* [CocoaPods](https://cocoapods.org/)
* [MongoDB](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/) Running on Ubuntu
* [MongoDB BI Connector Server](https://www.mongodb.com/products/bi-connector)
* A business intelligence platform with remote connection ability

Regarding the business intelligence platform--if anyone has a _free_ suggestions, please leave them in the comments below.  The first improvement I'd like to the entire system is to get away from Tableau.  Have I mentioned I hate it?

Ok, let's get started!


text:
/* ###
 * IP: GHIDRA
 *
 * 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.util.bin.format.dwarf4.next;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.List;

import org.junit.Test;

import ghidra.app.util.NamespaceUtils;
import ghidra.app.util.bin.format.dwarf4.*;
import ghidra.app.util.bin.format.dwarf4.encoding.DWARFSourceLanguage;
import ghidra.app.util.bin.format.dwarf4.expression.DWARFExpressionOpCodes;
import ghidra.program.model.data.*;
import ghidra.program.model.listing.*;
import ghidra.program.model.symbol.Namespace;
import ghidra.util.exception.CancelledException;

public class DWARFFunctionImporterTest extends DWARFTestBase {

	@Test
	public void testRustMethod_HasParamDefs()
			throws CancelledException, IOException, DWARFException {
		// test that Ghidra functions in a Rust compilation unit do have their info set
		// if they look like they have normal param info

		cu = new MockDWARFCompilationUnit(dwarfProg, 0x1000, 0x2000, 0,
			DWARFCompilationUnit.DWARF_32, (short) 4, 0, (byte) 8, 0,
			DWARFSourceLanguage.DW_LANG_Rust);
		cu2 = null;
		setMockCompilationUnits(cu);

		DebugInfoEntry intDIE = addInt(cu);
		DebugInfoEntry fooDIE = newSubprogram("foo", intDIE, 0x410, 10).create(cu);
		newFormalParam(fooDIE, "param1", intDIE, DWARFExpressionOpCodes.DW_OP_fbreg, 0x6c)
				.create(cu);

		importFunctions();

		Function fooFunc = program.getListing().getFunctionAt(addr(0x410));
		assertNotNull(fooFunc);

		assertEquals("foo", fooFunc.getName());
		DataType returnType = fooFunc.getReturnType();
		assertNotNull(returnType);
		assertEquals("int", returnType.getName());

		Parameter[] fooParams = fooFunc.getParameters();
		assertEquals(fooParams.length, 1);
		assertEquals("param1", fooParams[0].getName());
		assertEquals("int", fooParams[0].getDataType().getName());
	}

	@Test
	public void testRustMethod_NoParamDefs()
			throws CancelledException, IOException, DWARFException {
		// test that Ghidra functions in a Rust compilation unit don't have their info set
		// if they look like they are one of the stub DIE entries that Rust creates
		cu = new MockDWARFCompilationUnit(dwarfProg, 0x1000, 0x2000, 0,
			DWARFCompilationUnit.DWARF_32, (short) 4, 0, (byte) 8, 0,
			DWARFSourceLanguage.DW_LANG_Rust);
		cu2 = null;
		setMockCompilationUnits(cu);

		DebugInfoEntry intDIE = addInt(cu);
		newSubprogram("foo", intDIE, 0x410, 10).create(cu);

		importFunctions();

		Function fooFunc = program.getListing().getFunctionAt(addr(0x410));
		assertNotNull(fooFunc);

		assertEquals("foo", fooFunc.getName());
		DataType returnType = fooFunc.getReturnType();
		assertNotNull(returnType);
		assertEquals("undefined", returnType.getName());
	}

	@Test
	public void testNotRustMethod_NoParamDefs()
			throws CancelledException, IOException, DWARFException {
		// test that Ghidra functions in a non-Rust compilation unit do have their info set
		// even if their param info is empty.
		cu = new MockDWARFCompilationUnit(dwarfProg, 0x1000, 0x2000, 0,
			DWARFCompilationUnit.DWARF_32, (short) 4, 0, (byte) 8, 0,
			DWARFSourceLanguage.DW_LANG_C);
		cu2 = null;
		setMockCompilationUnits(cu);

		DebugInfoEntry intDIE = addInt(cu);
		newSubprogram("foo", intDIE, 0x410, 10).create(cu);

		importFunctions();

		Function fooFunc = program.getListing().getFunctionAt(addr(0x410));
		assertNotNull(fooFunc);

		assertEquals("foo", fooFunc.getName());
		DataType returnType = fooFunc.getReturnType();
		assertNotNull(returnType);
		assertEquals("int", returnType.getName());
	}

	@Test
	public void testNamespace_with_reserved_chars()
			throws CancelledException, IOException, DWARFException {
		// simulate what happens when a C++ operator/ or a templated classname
		// that contains a namespace template argument (templateclass<ns1::ns2::argclass>)
		// is encountered and a Ghidra namespace is created

		DebugInfoEntry intDIE = addInt(cu);
		DebugInfoEntry floatDIE = addFloat(cu);
		DebugInfoEntry struct1DIE = newStruct("mystruct::operator/()", 100).create(cu);
		DebugInfoEntry nestedStructPtrDIE = addFwdPtr(cu, 1);
		DebugInfoEntry nestedStructDIE =
			newStruct("nested_struct", 10).setParent(struct1DIE).create(cu);
		newMember(nestedStructDIE, "blah1", intDIE, 0).create(cu);
		DebugInfoEntry fooDIE =
			newSubprogram("foo", intDIE, 0x410, 10).setParent(nestedStructDIE).create(cu);
		newFormalParam(fooDIE, "this", nestedStructPtrDIE, DWARFExpressionOpCodes.DW_OP_fbreg, 0x6c)
				.create(cu);

		newMember(struct1DIE, "f1", intDIE, 0).create(cu);
		newMember(struct1DIE, "f2", floatDIE, 10).create(cu);
		newMember(struct1DIE, "f3", nestedStructDIE, 20).create(cu);

		importFunctions();

		Function fooFunc = program.getListing().getFunctionAt(addr(0x410));
		List<Namespace> nsParts = NamespaceUtils.getNamespaceParts(fooFunc.getParentNamespace());

		assertEquals("foo", fooFunc.getName());
		assertEquals("nested_struct",
			((Pointer) fooFunc.getParameter(0).getDataType()).getDataType().getName());
		assertEquals("__thiscall", fooFunc.getCallingConventionName());
		assertEquals("nested_struct", nsParts.get(nsParts.size() - 1).getName());
		assertEquals("mystruct::operator/()", nsParts.get(nsParts.size() - 2).getName());

		// Test that VariableUtilities can find the structure for the this* pointer
		Structure nestedStructDT1 = (Structure) dataMgr
				.getDataType(new CategoryPath(uncatCP, "mystruct::operator/()"), "nested_struct");
		Structure nestedStructDT2 = VariableUtilities.findExistingClassStruct(fooFunc);
		assertTrue(nestedStructDT1 == nestedStructDT2);
	}
}


text:
package test

import org.specs2.mutable._

import play.api.test._
import play.api.test.Helpers._
import play.api.libs.json.Json

/**
 * Add your spec here.
 * You can mock out a whole application including requests, plugins etc.
 * For more information, consult the wiki.
 */
class JsonApplicationSpec extends Specification {

  "Application" should {

    "render the index page" in new WithApplication {

      val kitty = Json.obj("name" -> "Scarlett", "color" -> "Black & White")
      val postRequest = FakeRequest(
                  method = "POST",
                  uri = "/json/insert",
                  headers = FakeHeaders(
                    Seq("Content-type"->Seq("application/json"))
                  ),
                  body =  kitty
                )
      val Some(result) = route(postRequest)
      status(result) must equalTo(OK)

      val home = route(FakeRequest(GET, "/json/all")).get

      status(home) must equalTo(OK)
      contentType(home) must beSome.which(_ == "application/json")
      contentAsString(home) must contain ("Scarlett")
    }
  }
}


text:
'use strict';
const r = require('../services/reddit');
if (!r) {
  return;
}
const usernoteHelper = require('../services/usernote-helper');
const SUBREDDIT = 'pokemontrades';

const SPAM_COOLDOWN = 60 * 60 * 6; // The number of seconds allowed between threads
const TRADE_TYPE_FLAIRS = new Set(['SWSH', 'LGPE', 'SMUSUM', 'XYORAS', 'Home', 'Tradeback', 'Redeem']);
const GIVEAWAY_TYPE_FLAIRS = new Set(['Giveaway']);

const threadsByUser = new Map();
const handledThreadIds = new Set();

const getSpamReply = (newThread, oldThread) => `Hello /u/${newThread.author.name},

Sorry, but this thread has been removed because it violates [rule 7](/r/pokemontrades/wiki/rules). In the future, please do not post trade threads more often than once per 6 hours. (Your [other thread](${oldThread.url}) was posted more recently than 6 hours ago.)

*This action was performed by a bot. If you think there was some mistake, please click the "report" button next to this comment, and a human will take a look at your post shortly.*`;

module.exports = {
  period: 60,
  onStart: false,
  task ({bot: {nick}}) {
    return r.getSubreddit(SUBREDDIT).getNew()
      .filter(thread => !handledThreadIds.has(thread.id))
      .then(threads => threads.reverse())
      .each(thread => handledThreadIds.add(thread.id))
      .each(thread => {
        if (!threadsByUser.has(thread.author.name)) {
          threadsByUser.set(thread.author.name, []);
        }
      }).mapSeries(thread => {
        const oldThread = threadsByUser.get(thread.author.name).find(oldThread => threadBreaksRules(thread, oldThread));

        if (oldThread && thread.approved_by === null) {
          return thread.remove().lock().reply(getSpamReply(thread, oldThread)).distinguish().return(
            `[Flood notification]: The thread <${thread.url}> by /u/${thread.author.name} breaks the flood rule, and has been removed. (Old thread: <${oldThread.url}> )`
          ).tap(() => usernoteHelper.addNote({mod: nick, user: thread.author.name, subreddit: thread.subreddit.display_name, note: '6 hour rule', link: `l,${thread.id}`, index: 0, fromChannel: null}));
        } else {
          threadsByUser.set(
            thread.author.name,
            threadsByUser.get(thread.author.name)
              .filter(oldThread => oldThread.created_utc + SPAM_COOLDOWN > Date.now() / 1000)
              .concat({url: thread.url, created_utc: thread.created_utc, link_flair_text: thread.link_flair_text})
          );
        }
      }).filter(response => response);
  }
};

function threadBreaksRules (thread, oldThread) {
  return oldThread.created_utc + SPAM_COOLDOWN > thread.created_utc && (
    TRADE_TYPE_FLAIRS.has(thread.link_flair_text) && TRADE_TYPE_FLAIRS.has(oldThread.link_flair_text) ||
    GIVEAWAY_TYPE_FLAIRS.has(thread.link_flair_text) && GIVEAWAY_TYPE_FLAIRS.has(oldThread.link_flair_text)
  );
}


text:
// 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.

#ifndef __HEALTH_CHECKER_HPP__
#define __HEALTH_CHECKER_HPP__

#include <string>
#include <tuple>
#include <vector>

#include <mesos/mesos.hpp>

#include <process/future.hpp>
#include <process/owned.hpp>
#include <process/pid.hpp>
#include <process/process.hpp>
#include <process/protobuf.hpp>
#include <process/time.hpp>

#include <stout/duration.hpp>
#include <stout/lambda.hpp>
#include <stout/nothing.hpp>
#include <stout/stopwatch.hpp>

#include "messages/messages.hpp"

namespace mesos {
namespace internal {
namespace checks {

// Forward declarations.
class HealthCheckerProcess;

class HealthChecker
{
public:
  /**
   * Attempts to create a `HealthChecker` object. In case of success, health
   * checking starts immediately after initialization.
   *
   * @param check The protobuf message definition of health check.
   * @param launcherDir A directory where Mesos helper binaries are located.
   * @param callback A callback HealthChecker uses to send health status
   *     updates to its owner (usually an executor).
   * @param taskId The TaskID of the target task.
   * @param taskPid The target task's pid used to enter the specified
   *     namespaces.
   * @param namespaces The namespaces to enter prior to performing the health
   *     check.
   * @return A `HealthChecker` object or an error if `create` fails.
   *
   * @todo A better approach would be to return a stream of updates, e.g.,
   * `process::Stream<TaskHealthStatus>` rather than invoking a callback.
   *
   * @todo Consider leveraging `checks::Checker` for checking functionality.
   * This class will then focus on interpreting and acting on the result.
   */
  static Try<process::Owned<HealthChecker>> create(
      const HealthCheck& check,
      const std::string& launcherDir,
      const lambda::function<void(const TaskHealthStatus&)>& callback,
      const TaskID& taskId,
      const Option<pid_t>& taskPid,
      const std::vector<std::string>& namespaces);

  ~HealthChecker();

  /**
   * Immediately stops health checking. Any in-flight health checks are dropped.
   */
  void stop();

private:
  explicit HealthChecker(process::Owned<HealthCheckerProcess> process);

  process::Owned<HealthCheckerProcess> process;
};

class HealthCheckerProcess : public ProtobufProcess<HealthCheckerProcess>
{
public:
  HealthCheckerProcess(
      const HealthCheck& _check,
      const std::string& _launcherDir,
      const lambda::function<void(const TaskHealthStatus&)>& _callback,
      const TaskID& _taskId,
      const Option<pid_t>& _taskPid,
      const std::vector<std::string>& _namespaces);

  virtual ~HealthCheckerProcess() {}

protected:
  virtual void initialize() override;

private:
  void failure(const std::string& message);
  void success();

  void performSingleCheck();
  void processCheckResult(
      const Stopwatch& stopwatch,
      const process::Future<Nothing>& future);

  process::Future<Nothing> commandHealthCheck();

  process::Future<Nothing> httpHealthCheck();

  process::Future<Nothing> _httpHealthCheck(
      const std::tuple<
          process::Future<Option<int>>,
          process::Future<std::string>,
          process::Future<std::string>>& t);

  process::Future<Nothing> tcpHealthCheck();

  process::Future<Nothing> _tcpHealthCheck(
      const std::tuple<
          process::Future<Option<int>>,
          process::Future<std::string>,
          process::Future<std::string>>& t);

  void scheduleNext(const Duration& duration);

  HealthCheck check;
  Duration checkDelay;
  Duration checkInterval;
  Duration checkGracePeriod;
  Duration checkTimeout;

  // Contains a binary for TCP health checks.
  const std::string launcherDir;

  const lambda::function<void(const TaskHealthStatus&)> healthUpdateCallback;
  const TaskID taskId;
  const Option<pid_t> taskPid;
  const std::vector<std::string> namespaces;
  Option<lambda::function<pid_t(const lambda::function<int()>&)>> clone;

  uint32_t consecutiveFailures;
  process::Time startTime;
  bool initializing;
};

namespace validation {

// TODO(alexr): A better place for this function would be something like
// "mesos_validation.cpp", since it validates API protobuf which is not
// solely related to the health checking library.
Option<Error> healthCheck(const HealthCheck& check);

} // namespace validation {

} // namespace checks {
} // namespace internal {
} // namespace mesos {

#endif // __HEALTH_CHECKER_HPP__


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EntityFrameworkRepository
{
    class Constants
    {
        public class AppSettings
        {
            public const string ConnectionString = "RepositoryConnectionStringName";
            public const string DefaultConnectionStringName = "DefaultConnection";
        }
    }
}


text:
package org.elasticsearch.search.aggregations.metrics.weighted_avg;

import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation;

/**
 * An aggregation that computes the average of the values in the current bucket.
 */
public interface WeightedAvg extends NumericMetricsAggregation.SingleValue {

    /**
     * The average value.
     */
    double getValue();
}