text:
require 'hard_attn'
require 'memory'
require 'SparseMax'

function make_lstm(data, opt, model, use_chars)
   assert(model == 'enc' or model == 'dec' or model == 'bow_enc')
   local name = '_' .. model
   local dropout = opt.dropout or 0
   local n = opt.num_layers
   local rnn_size = opt.rnn_size
   local RnnD = {opt.rnn_size, opt.rnn_size}
   local input_size = opt.word_vec_size
   if model == 'bow_enc' then
     if opt.no_bow == 1 then
       input_size = opt.rnn_size
     else
       if opt.conv_bow == 1 then
         input_size = opt.num_kernels
       end
     end
     if opt.pos_embeds_sent == 1 then
       input_size = input_size + opt.pos_dim
     end
   end

   local batch_size
   if model == 'enc' and opt.all_lstm == 0 then
     batch_size = opt.max_batch_l*opt.max_sent_l
   else
     batch_size = opt.max_batch_l
   end

   local offset = 0
  -- there will be 2*n+3 inputs
   local inputs = {}
   table.insert(inputs, nn.Identity()()) -- x (batch_size x source_l)
   if model == 'dec' then
      if opt.input_feed == 1 then
        table.insert(inputs, nn.Identity()()) -- prev context_attn (batch_size x rnn_size)
        offset = offset + 1
      end
   elseif model == 'enc' then
      if opt.add_sent_context == 1 then
        table.insert(inputs, nn.Identity()()) -- batch_size x source_l x bow_size
        offset = offset + 1
      end
   end
   for L = 1,n do
      table.insert(inputs, nn.Identity()()) -- prev_c[L]
      table.insert(inputs, nn.Identity()()) -- prev_h[L]
   end

   local x, input_size_L
   local outputs = {}
  for L = 1,n do
    local nameL = model..'_L'..L..'_'
     -- c,h from previous timesteps
    local prev_c = inputs[L*2+offset]
    local prev_h = inputs[L*2+1+offset]
    -- the input to this layer
    if L == 1 then
       if use_chars == 0 then
         if model == 'bow_enc' then
           x = inputs[1] -- batch_size x bow_size
         else
           local word_vecs
           if model == 'enc' then
             word_vecs = nn.LookupTable(data.source_size, input_size)
           else
             word_vecs = nn.LookupTable(data.target_size, input_size)
           end
           word_vecs.name = 'word_vecs' .. name
           x = word_vecs(inputs[1]) -- batch_size x word_vec_size
         end
       else
	  local char_vecs = nn.LookupTable(data.char_size, opt.word_vec_size)
	  char_vecs.name = 'word_vecs' .. name
          x = nn.Reshape(-1, opt.word_vec_size, false)(char_vecs(inputs[1])) -- (batch_size*source_l) x word_vec_size
          --:usePrealloc(nameL..'reshape1', {{opt.max_batch_l, opt.max_sent_l, opt.word_vec_size}},
              --{{opt.max_batch_l*opt.max_sent_l, opt.word_vec_size}})(
	  --local charcnn = make_cnn(opt.char_vec_size,  opt.kernel_width, opt.num_kernels)
	  --charcnn.name = 'charcnn' .. name
	  --x = charcnn(char_vecs(inputs[1]))
	  --if opt.num_highway_layers > 0 then
	     --local mlp = make_highway(input_size, opt.num_highway_layers)
	     --mlp.name = 'mlp' .. name
	     --x = mlp(x)
	  --end
       end
       input_size_L = input_size
       if model == 'dec' then
          if opt.input_feed == 1 then
             x = nn.JoinTable(2):usePrealloc("dec_inputfeed_join",
                 {{opt.max_batch_l, opt.word_vec_size},{opt.max_batch_l, opt.rnn_size}})
                 ({x, inputs[1+offset]}) -- batch_size x (word_vec_size + rnn_size)
             input_size_L = input_size + rnn_size
          end
        elseif model == 'enc' then
          if opt.add_sent_context == 1 then
            local sent_context = nn.Reshape(-1, opt.bow_size, false)(inputs[1+offset])
            x = nn.JoinTable(2)({x, sent_context})
--:usePrealloc("enc_sent_context_join",
                --{{opt.max_batch_l*opt.max_sent_l, opt.word_vec_size}, {opt.max_batch_l*opt.max_sent_l, bow_size}})

            input_size_L = input_size + opt.bow_size
          end
       end
    else
       x = outputs[(L-1)*2]
       if opt.res_net == 1 and L > 2 then
	  x = nn.CAddTable()({x, outputs[(L-2)*2]})
       end
       input_size_L = rnn_size
       --if opt.multi_attn == L and model == 'dec' then
		--local multi_attn = make_decoder_attn(data, opt, 1)
		--multi_attn.name = 'multi_attn' .. L
		--x = multi_attn({x, inputs[2]})
       --end
       if dropout > 0 then
	  x = nn.Dropout(dropout, nil, false):usePrealloc(nameL.."dropout",
                                                          {{batch_size, input_size_L}})(x)
       end
    end
    -- evaluate the input sums at once for efficiency
    local i2h = nn.Linear(input_size_L, 4 * rnn_size):usePrealloc(nameL.."i2h-reuse",
                                                                  {{batch_size, input_size_L}},
                                                                  {{batch_size, 4 * rnn_size}})(x)
    local h2h = nn.LinearNoBias(rnn_size, 4 * rnn_size):usePrealloc(nameL.."h2h-reuse",
                                                                  {{batch_size, rnn_size}},
                                                                  {{batch_size, 4 * rnn_size}})(prev_h)
    local all_input_sums = nn.CAddTable():usePrealloc(nameL.."allinput",
            {{batch_size, 4*rnn_size}, {batch_size, 4*rnn_size}},
            {{batch_size, 4*rnn_size}})({i2h, h2h})

    local reshaped = nn.Reshape(4, rnn_size, true):usePrealloc(nameL.."reshape2",
            {{batch_size, 4*rnn_size}}, {{batch_size, 4, rnn_size}})
            (all_input_sums)
    local n1, n2, n3, n4 = nn.SplitTable(2):usePrealloc(nameL.."reshapesplit",
                                                        {{batch_size, 4, rnn_size}})(reshaped):split(4)
    -- decode the gates
    local in_gate = nn.Sigmoid():usePrealloc(nameL.."G1-reuse",{RnnD})(n1)
    local forget_gate = nn.Sigmoid():usePrealloc(nameL.."G2-reuse",{RnnD})(n2)
    local out_gate = nn.Sigmoid():usePrealloc(nameL.."G3-reuse",{RnnD})(n3)
    -- decode the write inputs
    local in_transform = nn.Tanh():usePrealloc(nameL.."G4-reuse",{RnnD})(n4)

    -- perform the LSTM update
    local next_c = nn.CAddTable():usePrealloc(nameL.."G5a",{RnnD,RnnD})({
         nn.CMulTable():usePrealloc(nameL.."G5b",{RnnD,RnnD})({forget_gate, prev_c}),
         nn.CMulTable():usePrealloc(nameL.."G5c",{RnnD,RnnD})({in_gate, in_transform})
      })

    -- gated cells form the output
    local next_h = nn.CMulTable():usePrealloc(nameL.."G5d",{RnnD,RnnD})
                                 ({out_gate, nn.Tanh():usePrealloc(nameL.."G6-reuse",{RnnD})(next_c)})

    table.insert(outputs, next_c)
    table.insert(outputs, next_h)
  end
  return nn.gModule(inputs, outputs)
end

function make_bow_encoder(data, opt)
  -- takes 3D tensor input: batch_l x source_l x source_char_l

  local input = nn.Identity()()
  local inputs = {input}
  local word_vecs = nn.LookupTable(data.char_size, opt.word_vec_size)
  word_vecs.name = 'word_vecs_bow'
  local embeds = nn.Bottle(word_vecs, 2, 3)(input) -- batch_l x source_l x source_char_l x word_vec_size

  local output
  if opt.conv_bow == 1 then
    local conv = make_cnn(opt.word_vec_size, opt.kernel_width, opt.num_kernels)
    local conv_output = nn.Bottle(conv, 3, 2)(embeds)
    if opt.num_highway_layers > 0 then
      local mlp = make_highway(opt.num_kernels, opt.num_highway_layers)
      conv_output = nn.Bottle(mlp)(conv_output)
    end
    if opt.batch_norm_conv == 1 then
      local batch_norm = nn.BatchNormalization(opt.num_kernels)
      conv_output = nn.Bottle(batch_norm, 2)(conv_output)
    end
    output = conv_output
  else
    -- bag of words
    output = nn.Sum(3)(embeds) -- batch_l x source_l x word_vec_size
    if opt.linear_bow == 1 then
      local linear_bow = nn.Linear(opt.word_vec_size, opt.linear_bow_size)
      output = nn.Bottle(linear_bow, 2)(output)
    end
  end
  if opt.bow_encoder_lstm == 1 then
    -- make output suitable for LSTM
    output = nn.Transpose({1,2})(output) -- source_l x batch_l x {word_vec_size,num_kernels}
  end
  if opt.pos_embeds_sent == 1 then
    local pos = nn.Identity()()
    table.insert(inputs, pos)
    local pos_embeddings = nn.LookupTable(opt.max_sent_l, opt.pos_dim)
    local pos_states = pos_embeddings(pos) -- batch_l x source_l x pos_dim
    output = nn.JoinTable(3)({output, pos_states}) -- batch_l x source_l x (word_vec_size + pos_dim)
  end
  if opt.bow_dropout > 0 then
    output = nn.Dropout(opt.bow_dropout, nil, false)(output)
  end

  return nn.gModule(inputs, {output})
end

function make_last_state_selecter(opt, batch_l, source_l)
  local selecter = nn.Sequential()
  selecter:add(nn.MaskedSelect())
  selecter:add(nn.Reshape(batch_l, source_l, opt.rnn_size, false))
  if opt.bow_encoder_lstm == 1 then
    selecter:add(nn.Transpose({1,2})) -- source_l x batch_l x rnn_size
  end
  return selecter:cuda()
end

function make_pos_embeds_sent(data, opt)
  local inputs = {}
  local rnn_states = nn.Identity()()
  local pos = nn.Identity()()
  table.insert(inputs, rnn_states)
  table.insert(inputs, pos)

  local pos_embeddings = nn.LookupTable(opt.max_sent_l, opt.pos_dim)
  local pos_states = pos_embeddings(pos) -- batch_l x source_l x pos_dim
  output = nn.JoinTable(3)({rnn_states, pos_states}) -- batch_l x source_l x (word_vec_size + pos_dim)
  return nn.gModule(inputs, {output})
end

function make_add_sent_context_init(data, opt)
    local inputs = {}
    local bow_context = nn.Identity()()
    table.insert(inputs, bow_context)

    local sz = opt.num_layers*opt.rnn_size*2
    local new_state = nn.Linear(opt.bow_size, sz)(nn.View(-1):setNumInputDims(1)(bow_context)) -- batch_l*source_l x sz
    local out = nn.ViewAs()({new_state, nn.Replicate(sz, 3)(nn.Sum(3)(bow_context))}) -- batch_l x source_l x sz
    return nn.gModule(inputs, {out})
end

function make_decoder_attn(data, opt, simple)
   -- 2D tensor target_t (batch_l x rnn_size) and
   -- 4D tensor for context (batch_l x source_l x source_char_l x rnn_size)

   local inputs = {}
   local outputs = {}
   table.insert(inputs, nn.Identity()())
   table.insert(inputs, nn.Identity()())
   local target_t = nn.LinearNoBias(opt.rnn_size, opt.rnn_size)(inputs[1])
   local context = nn.Reshape(-1, opt.rnn_size, true)(inputs[2]) -- batch_l x (source_l*source_char_l) x rnn_size
   simple = simple or 0
   local dropout = opt.dropout or 0
   -- get attention

   local attn = nn.MM()({context, nn.Replicate(1,3)(target_t)}) -- batch_l x (source_l*source_char_l) x 1
   -- :usePrealloc("dec_attn_mm1",
                                --{{opt.max_batch_l, opt.max_sent_l_src*opt.max_word_l, opt.rnn_size}, {opt.rnn_size,opt.rnn_size,1}},
                                --{{opt.max_batch_l, opt.max_sent_l_src*opt.max_word_l, 1}})
   attn = nn.Sum(3)(attn)
   local softmax_attn = nn.SoftMax()
   softmax_attn.name = 'softmax_attn'
   attn = softmax_attn(attn)
   attn = nn.Replicate(1,2)(attn) -- batch_l x  1 x (source_l*source_char_l)

   -- apply attention to context
   local context_combined = nn.MM()({attn, context}) -- batch_l x 1 x rnn_size
   -- :usePrealloc("dec_attn_mm2",
                                                --{{opt.max_batch_l, 1, opt.max_sent_l_src*opt.max_word_l},{opt.max_batch_l, opt.max_sent_l_src*opt.max_word_l, opt.rnn_size}},
                                                --{{opt.max_batch_l, 1, opt.rnn_size}})
   context_combined = nn.Sum(2)(context_combined) -- batch_l x rnn_size
             -- :usePrealloc("dec_attn_sum",
                                            --{{opt.max_batch_l, 1, opt.rnn_size}},
                                            --{{opt.max_batch_l, opt.rnn_size}})
   local context_output
   if simple == 0 then
      context_combined = nn.JoinTable(2)({context_combined, inputs[1]}) -- batch_l x rnn_size*2
      -- :usePrealloc("dec_attn_jointable",
                                                     --{{opt.max_batch_l,opt.rnn_size},{opt.max_batch_l, opt.rnn_size}})
      context_output = nn.Tanh()(nn.LinearNoBias(opt.rnn_size*2,opt.rnn_size)(context_combined))
      -- :usePrealloc("dec_noattn_tanh", {{opt.max_batch_l, opt.rnn_size}})
   else
      context_output = nn.CAddTable()({context_combined,inputs[1]})
   end

   if dropout > 0 then
      context_output = nn.Dropout(dropout, nil, false)(context_output)
      -- :usePrealloc("dec_attn_dropout",{{opt.rnn_size, opt.rnn_size}})
   end
   table.insert(outputs, context_output)
   --return nn.gModule(inputs, {context_output})
   return nn.gModule(inputs, outputs)
end

function make_hierarchical_decoder_attn(data, opt, simple)
   -- inputs:
   -- 2D tensor target_t (batch_l x rnn_size)
   -- 4D tensor for context (batch_l x source_l x source_char_l x rnn_size)
   -- 3D tensor for BOW context (batch_l x source_l x word_vec_size)
   --     or if using LSTM, (batch_l x source_l x rnn_size)

   local inputs = {}
   local outputs = {}
   table.insert(inputs, nn.Identity()())
   local target_t = inputs[1]
   local context, bow_context
   if opt.coarse_attn_only == 1 then
     table.insert(inputs, nn.Identity()())
     bow_context = inputs[2]
   else
     table.insert(inputs, nn.Identity()())
     table.insert(inputs, nn.Identity()())
     context = inputs[2]
     bow_context = inputs[3]
   end
   local inf_mask
   if opt.inf_mask == 1 then
      table.insert(inputs, nn.Identity()())
      inf_mask = inputs[4]
   end
   simple = simple or 0
   -- get attention

   local cur_target_t = target_t
   for hop = 1, opt.hop_attn do
     local hop_attn, inp
     if opt.coarse_attn_only == 1 then
       hop_attn = make_hier_hop_coarse_only(data, opt, simple, hop)
       inp = {cur_target_t, bow_context}
     else
       hop_attn = make_hier_hop(data, opt, simple, hop)
       inp = {cur_target_t, context, bow_context}
     end
     if opt.inf_mask == 1 then
       table.insert(inp, inf_mask)
     end
     cur_target_t = hop_attn(inp)
   end

   return nn.gModule(inputs, {cur_target_t})
end

function make_hier_hop(data, opt, simple, hop)
   local inputs = {}
   table.insert(inputs, nn.Identity()())
   table.insert(inputs, nn.Identity()())
   table.insert(inputs, nn.Identity()())
   local target_t = inputs[1] -- batch_l x rnn_size
   local context = inputs[2]
   local bow_context = nn.Contiguous()(inputs[3]) -- batch_l x source_l x bow_size
   local inf_mask
   if opt.inf_mask == 1 then
     table.insert(inputs, nn.Identity()())
     inf_mask = inputs[4] -- batch_l x source_l x source_char_l
   end
   simple = simple or 0
   local dropout = opt.dropout or 0
   -- get attention

   -- attention over sentences
   local attn1 -- raw scores, batch_l x source_l
   if opt.bahdanau_attn == 1 then
     local weighted_bow_context = nn.Bottle(nn.LinearNoBias(opt.bow_size, opt.hid_attn_size), 2)(bow_context) -- batch_l x source_l x hid_attn_size
     local cur_target_t = nn.LinearNoBias(opt.rnn_size, opt.hid_attn_size)(target_t) -- batch_l x hid_attn_size
     local hid = nn.Tanh()(nn.CAddTable(3)({weighted_bow_context,
                                            nn.ReplicateAs(2, 2)({cur_target_t, bow_context}) -- batch_l x source_l x hid_attn_size
                                          })) -- batch_l x source_l x hid_attn_size
     attn1 = nn.Bottle(nn.LinearNoBias(opt.hid_attn_size, 1), 2)(hid) -- batch_l x source_l x 1
     attn1 = nn.Sum(3)(attn1)
   else
     local cur_target_t = nn.LinearNoBias(opt.rnn_size, opt.bow_size)(target_t) -- :usePrealloc('dec_hier_attn_linear1_'..hop, {{opt.max_batch_l, opt.rnn_size}},
            --{{opt.max_batch_l, bow_size}})(target_t) -- batch_l x bow_size
     attn1 = nn.MM()({bow_context, nn.Replicate(1,3)(cur_target_t)}) -- batch_l x source_l x 1
     -- :usePrealloc("dec_hier_attn_mm1_" .. hop, {{opt.max_batch_l, opt.max_sent_l_src, bow_size},{bow_size,bow_size,1}}, {{opt.max_batch_l, opt.max_sent_l_src, 1}})
     attn1 = nn.Sum(3)(attn1)
   end

   -- trained temperature
   if opt.train_temp == 1 then
     local temp = nn.Sum(2)(nn.Linear(opt.rnn_size, 1)(target_t)) -- batch_l
     attn1 = nn.CMulTable()({nn.ReplicateAs(2, 2)({temp, attn1}), attn1})
   end

   local softmax_attn = nn.SoftMax() --:usePrealloc('dec_hier_attn_softmax1_'..hop, {{opt.max_batch_l, opt.max_sent_l_src}})
   if opt.use_sigmoid_sent == 1 then
     softmax_attn = nn.Sigmoid()
   end
   if opt.sparsemax == 1 then
     softmax_attn = nn.SparseMax()
   end
   softmax_attn.name = 'softmax_attn1_' .. hop
   attn1 = softmax_attn(attn1) -- batch_l x source_l
   if opt.attn_type == 'hard' then
     -- sample (hard attention)
     local sampler = nn.ReinforceCategorical(opt.semi_sampling_p, opt.entropy_scale, opt.multisampling,
                                            opt.with_replace, opt.uniform_attn)
                                            --:usePrealloc("sampler_" .. hop,
                                            --{{opt.max_batch_l, opt.max_sent_l_src}})
     sampler.name = 'sampler'
     attn1 = sampler(attn1) -- one hot
   else
     if opt.sent_ent > 0 then
       -- this module does nothing to input, but adds scale*(log(a) + 1) gradient
       -- on backward
       local ent_module = nn.EntropyBackward(opt.sent_ent)
       ent_module.name = 'entropy_backward'
       attn1 = ent_module(attn1)
     end
   end

   -- attention over words of each sentence
   --local attn2 = nn.MM():usePrealloc("dec_hier_attn_mm2_" .. hop,
                                     --{{opt.max_batch_l, opt.max_sent_l_src*opt.max_word_l, opt.rnn_size},{opt.rnn_size,opt.rnn_size,1}},
                                     --{{opt.max_batch_l, opt.max_sent_l_src*opt.max_word_l, 1}})
                         --({reshape_context,
                         --nn.Replicate(1,3)(nn.LinearNoBias(opt.rnn_size, opt.rnn_size):usePrealloc("dec_hier_attn_linear2_"..hop, {{opt.max_batch_l, opt.rnn_size}})
                         --(target_t))}) -- batch_l x (source_l*source_char_l) x 1
   local reshape_context = nn.Reshape(-1, opt.rnn_size, true)(context) -- batch_l x (source_l*source_char_l) x opt.rnn_size
   local attn2 = nn.MM()({reshape_context,
                         nn.Replicate(1,3)(nn.LinearNoBias(opt.rnn_size, opt.rnn_size)(target_t))}) -- batch_l x (source_l*source_char_l) x 1
   attn2 = nn.Sum(3)(attn2) -- batch_l x (source_l*source_char_l)
   --attn2 = nn.View(-1):setNumInputDims(1)(nn.ViewAs(3)({attn2, context})) -- (batch_l*source_l) x source_char_l
   attn2 = nn.ViewAs(3)({attn2, context}) -- batch_l x source_l x source_char_l

   if opt.inf_mask == 1 then
     local attn_mask = inf_mask -- batch_l x source_l x source_char_l
     attn2 = nn.CAddTable()({attn2, attn_mask})
   end

   local softmax_attn2 = nn.SoftMax() --:usePrealloc('dec_hier_attn_softmax2_'..hop, {{opt.max_batch_l*opt.max_sent_l_src, opt.max_word_l}})
   if opt.use_sigmoid_word == 1 then
     softmax_attn2 = nn.Sigmoid()
   end
   softmax_attn2.name = 'softmax_attn2_' .. hop
   attn2 = nn.Bottle(softmax_attn2)(attn2)
   --attn2 = nn.ViewAs(3)({attn2, context}) -- batch_l x source_l x source_char_l

   -- multiply attentions together
   local mul_attn = nn.CMulTable()({nn.ReplicateAs(3,3)({attn1, attn2}), attn2}) -- batch_l x source_l x source_char_l
   --:usePrealloc("dec_hier_attn_cmultable" .. hop,
                                               --{{opt.max_batch_l, opt.max_sent_l, opt.max_word_l}, {opt.max_batch_l, opt.max_sent_l, opt.max_word_l}})
   mul_attn = nn.Replicate(1,2)(nn.View(-1):setNumInputDims(2)(mul_attn)) -- batch_l x 1 x (source_l*source_char_l)
   --:usePrealloc("dec_hier_attn_rep12_" .. hop,
      --{{opt.max_batch_l, opt.max_sent_l_src*opt.max_word_l}}, {{opt.max_batch_l, 1, opt.max_sent_l_src*opt.max_word_l}})

   -- apply attention to context
   local context_combined = nn.MM()({mul_attn, reshape_context}) -- batch_l x 1 x rnn_size
   -- :usePrealloc("dec_hier_attn_mm3_" .. hop,
                                               --{{opt.max_batch_l, 1, opt.max_sent_l_src*opt.max_word_l},{opt.max_batch_l, opt.max_sent_l_src*opt.max_word_l, opt.rnn_size}},
                                                   --{{opt.max_batch_l, 1, opt.rnn_size}})
   context_combined = nn.Sum(2)(context_combined) -- batch_l x rnn_size
   local context_output
   if simple == 0 then
      context_combined = nn.JoinTable(2)({context_combined, inputs[1]}) -- batch_l x rnn_size*2
      -- :usePrealloc("dec_hier_attn_jointable"..hop,
                                                   --{{opt.max_batch_l,opt.rnn_size},{opt.max_batch_l, opt.rnn_size}})

      local dec_hier_linear = nn.LinearNoBias(opt.rnn_size*2, opt.rnn_size)
      -- :usePrealloc("dec_hier_noattn_linear" .. hop, {{opt.max_batch_l,2*opt.rnn_size}})
      context_output = nn.Tanh()(dec_hier_linear(context_combined))
      -- :usePrealloc("dec_hier_noattn_tanh"..hop, {{opt.max_batch_l,opt.rnn_size}})
   else
      context_output = nn.CAddTable()({context_combined,inputs[1]})
   end

   if dropout > 0 then
      context_output = nn.Dropout(dropout, nil, false)(context_output)
      --:usePrealloc("dec_hier_attn_dropout" .. hop,
                                                        --{{opt.rnn_size, opt.rnn_size}})(context_output)
   end

   -- finished hops
   local out = context_output
   return nn.gModule(inputs, {out})
end

function make_hier_hop_coarse_only(data, opt, simple, hop)
   local inputs = {}
   table.insert(inputs, nn.Identity()())
   table.insert(inputs, nn.Identity()())
   local target_t = inputs[1] -- batch_l x rnn_size
   local bow_context = nn.Contiguous()(inputs[2]) -- batch_l x source_l x bow_size
   simple = simple or 0
   local dropout = opt.dropout or 0
   -- get attention

   -- attention over sentences
   local attn1 -- raw scores, batch_l x source_l
   local cur_target_t = nn.LinearNoBias(opt.rnn_size, opt.bow_size)(target_t) -- batch_l x bow_size
   attn1 = nn.MM()({bow_context, nn.Replicate(1,3)(cur_target_t)}) -- batch_l x source_l x 1
   attn1 = nn.Sum(3)(attn1)

   local softmax_attn = nn.SoftMax()
   if opt.use_sigmoid_sent == 1 then
     softmax_attn = nn.Sigmoid()
   end
   softmax_attn.name = 'softmax_attn1_' .. hop
   attn1 = softmax_attn(attn1) -- batch_l x source_l
   if opt.attn_type == 'hard' then
     -- sample (hard attention)
     local sampler = nn.ReinforceCategorical(opt.semi_sampling_p, opt.entropy_scale, opt.multisampling,
                                            opt.with_replace, opt.uniform_attn)
     sampler.name = 'sampler'
     attn1 = sampler(attn1) -- one hot
   end

   -- apply attention to context
   local context_combined = nn.MM()({nn.Replicate(1,2)(attn1), bow_context}) -- batch_l x 1 x bow_vec_size
   context_combined = nn.Sum(2)(context_combined) -- batch_l x bow_vec_size
   local context_output
   if simple == 0 then
      context_combined = nn.JoinTable(2)({context_combined, inputs[1]}) -- batch_l x (bow_vec_size + rnn_size)

      local dec_hier_linear = nn.LinearNoBias(opt.bow_size + opt.rnn_size, opt.rnn_size)
      context_output = nn.Tanh()(dec_hier_linear(context_combined))
      -- :usePrealloc("dec_hier_noattn_tanh"..hop, {{opt.max_batch_l,opt.rnn_size}})
   else
      assert('failure')
      context_output = nn.CAddTable()({context_combined,inputs[1]})
   end

   if dropout > 0 then
      context_output = nn.Dropout(dropout, nil, false)(context_output)
      --:usePrealloc("dec_hier_attn_dropout" .. hop,
                                                        --{{opt.rnn_size, opt.rnn_size}})(context_output)
   end

   -- finished hops
   local out = context_output
   return nn.gModule(inputs, {out})

end

function make_pos_embeds(data, opt)
  local pos_embeds = nn.Sequential():add(nn.LookupTable(opt.max_sent_l_src, opt.num_layers*opt.rnn_size*2))
  return pos_embeds
end

function make_init_dec_module(opt, batch_l, source_l)
  local init_dec_module = nn.Sequential()
  init_dec_module:add(nn.View(batch_l, source_l, opt.rnn_size))
  init_dec_module:add(nn.Sum(2))
  if opt.gpuid >= 0 then
    init_dec_module:cuda()
  end

  return init_dec_module
end

function make_generator(data, opt)
   local model = nn.Sequential()
   model:add(nn.Linear(opt.rnn_size, data.target_size))
   model:add(nn.LogSoftMax())

   local w = torch.ones(data.target_size)
   w[1] = 0
   criterion = nn.ClassNLLCriterion(w)
   criterion.sizeAverage = false
   return model, criterion
end

function make_reinforce(data, opt)
  local baseline_m = nn.Linear(opt.rnn_size, 1)

  local reward_criterion = nn.ReinforceNLLCriterion()
  reward_criterion.zero_one = opt.zero_one

  return baseline_m, reward_criterion
end

-- cnn Unit
function make_cnn(input_size, kernel_width, num_kernels)
   local output
   local input = nn.Identity()()
   if opt.cudnn == 1 then
      local conv = cudnn.SpatialConvolution(1, num_kernels, input_size,
					    kernel_width, 1, 1, 0)
      local conv_layer = conv(nn.View(1, -1, input_size):setNumInputDims(2)(input))
      output = nn.Sum(3)(nn.Max(3)(nn.Tanh()(conv_layer)))
   else
      local conv = nn.TemporalConvolution(input_size, num_kernels, kernel_width)
      local conv_layer = conv(input)
      if opt.relu_conv == 1 then
        output = nn.Max(2)(nn.ReLU()(conv_layer))
      else
        output = nn.Max(2)(nn.Tanh()(conv_layer))
      end
   end
   return nn.gModule({input}, {output})
end

function make_highway(input_size, num_layers, output_size, bias, f)
    -- size = dimensionality of inputs
    -- num_layers = number of hidden layers (default = 1)
    -- bias = bias for transform gate (default = -2)
    -- f = non-linearity (default = ReLU)

    local num_layers = num_layers or 1
    local input_size = input_size
    local output_size = output_size or input_size
    local bias = bias or -2
    local f = f or nn.ReLU()
    local start = nn.Identity()()
    local transform_gate, carry_gate, input, output
    for i = 1, num_layers do
       if i > 1 then
	  input_size = output_size
       else
	  input = start
       end
       output = f(nn.Linear(input_size, output_size)(input))
       transform_gate = nn.Sigmoid()(nn.AddConstant(bias, true)(
					nn.Linear(input_size, output_size)(input)))
       carry_gate = nn.AddConstant(1, true)(nn.MulConstant(-1)(transform_gate))
       local proj
       if input_size==output_size then
	  proj = nn.Identity()
       else
	  proj = nn.LinearNoBias(input_size, output_size)
       end
       input = nn.CAddTable()({
	                     nn.CMulTable()({transform_gate, output}),
                             nn.CMulTable()({carry_gate, proj(input)})})
    end
    return nn.gModule({start},{input})
end


text:
package com.dianping.loader;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.dianping.app.MyActivity;
import com.dianping.app.MyApplication;
import com.dianping.loader.model.FileSpec;
import com.dianping.loader.model.SiteSpec;

/**
 * 主Activity容器,负责启动并装载Fragment
 *

 * 启动前所有依赖的资源必须加载完毕(由urlMapping和LoaderActivity负责)
 *

 * Intent参数:

 * _site:SiteSpec,指定的site地图

 * _code:String,ClassLoader所需要载入的FileID,如果为空则使用APK自带ClassLoader

 * _fragment:String,Fragment的类名

 *
 * @author Yimin
 *
 */
public class MainActivity extends MyActivity {
	private SiteSpec site;
	private FileSpec file;
	private String fragmentName;
	private boolean loaded;
	private MyClassLoader classLoader;
	private MyResources myResources;
	private AssetManager assetManager;
	private Resources resources;
	private Theme theme;
	private FrameLayout rootView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Intent intent = getIntent();

		int error = 0;
		// must be load at the first start
		do {
			site = intent.getParcelableExtra("_site");
			if (site == null) {
				error = 201; // #201
				break;
			}
			fragmentName = intent.getStringExtra("_fragment");
			if (TextUtils.isEmpty(fragmentName)) {
				error = 202; // #202
				break;
			}
			String code = intent.getStringExtra("_code");
			if (TextUtils.isEmpty(code)) {
				loaded = true;
				break;
			}
			file = site.getFile(code);
			if (file == null) {
				error = 205; // #205
				break;
			}
			classLoader = MyClassLoader.getClassLoader(site, file);
			loaded = classLoader != null;
			if (!loaded) {
				error = 210; // #210
				break;
			}
		} while (false);

		super.onCreate(savedInstanceState);

		rootView = new FrameLayout(this);
		rootView.setLayoutParams(new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));
		rootView.setId(android.R.id.primary);
		setContentView(rootView);

		if (!loaded) {
			TextView text = new TextView(this);
			text.setText("无法载入页面" + (error == 0 ? "" : " #" + error));
			text.setLayoutParams(new FrameLayout.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
			rootView.addView(text);

			return;
		}

		// the fragment will be restored by framework
		if (savedInstanceState != null)
			return;

		Fragment fragment = null;
		try {
			fragment = (Fragment) getClassLoader().loadClass(fragmentName)
					.newInstance();
		} catch (Exception e) {
			loaded = false;
			classLoader = null;
			error = 211; // #211
			TextView text = new TextView(this);
			text.setText("无法载入页面 #" + error);
			text.append("\n" + e);
			text.setLayoutParams(new FrameLayout.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER));
			rootView.addView(text);

			return;
		}

		FragmentManager fm = getFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		ft.replace(android.R.id.primary, fragment);
		ft.commit();
	}

	@Override
	public ClassLoader getClassLoader() {
		return classLoader == null ? super.getClassLoader() : classLoader;
	}

	public SiteSpec getSite() {
		return site;
	}

	public FileSpec getFile() {
		return file;
	}

	public String getFragmentName() {
		return fragmentName;
	}

	@Override
	public Intent urlMap(Intent intent) {
		do {
			// only process my scheme uri
			Uri uri = intent.getData();
			if (uri == null)
				break;
			if (uri.getScheme() == null)
				break;
			if (!(MyApplication.PRIMARY_SCHEME
					.equalsIgnoreCase(uri.getScheme())))
				break;

			if (!intent.hasExtra("_site")) {
				intent.putExtra("_site", site);
			}
		} while (false);
		return super.urlMap(intent);
	}

	@Override
	public AssetManager getAssets() {
		return assetManager == null ? super.getAssets() : assetManager;
	}

	@Override
	public Resources getResources() {
		return resources == null ? super.getResources() : resources;
	}

	@Override
	public Theme getTheme() {
		return theme == null ? super.getTheme() : theme;
	}

	public MyResources getOverrideResources() {
		return myResources;
	}

	void setOverrideResources(MyResources myres) {
		if (myres == null) {
			this.myResources = null;
			this.resources = null;
			this.assetManager = null;
			this.theme = null;
		} else {
			this.myResources = myres;
			this.resources = myres.getResources();
			this.assetManager = myres.getAssets();
			Theme t = myres.getResources().newTheme();
			t.setTo(getTheme());
			this.theme = t;
		}
	}
}

redpajama_set_name: RedPajamaGithub


text:
namespace Snippets6.Sagas.Reply
{
    using System.Threading.Tasks;
    using NServiceBus;

    #region saga-with-reply

    public class MySaga : Saga<MySagaData>,
        IAmStartedByMessages<StartMessage>
    {

        public async Task Handle(StartMessage message, IMessageHandlerContext context)
        {
            Data.SomeID = message.SomeID;

            await ReplyToOriginator(context, new AlmostDoneMessage
            {
                SomeID = Data.SomeID
            });
        }

        #endregion

        protected override void ConfigureHowToFindSaga(SagaPropertyMapper<MySagaData> mapper)
        {
        }

    }

}


text:
//Write a program that reads two integer arrays from the console and compares them element by element.
using System;

namespace Compare_arrays
{
    class CompareArrays
    {
        static void Main()
        {
            Console.Write("Enter N : ");
            int n = int.Parse(Console.ReadLine());
            int[] firstArray = new int[n];
            for (int i = 0; i < firstArray.Length; i++)
            {
                firstArray[i] = int.Parse(Console.ReadLine());
            }
            Console.Write("Enter L : ");
            int l = int.Parse(Console.ReadLine());
            int[] secondArray = new int[l];
            for (int i = 0; i < secondArray.Length; i++)
            {
                secondArray[i] = int.Parse(Console.ReadLine());
            }
            if (firstArray.Length != secondArray.Length)
            {
                Console.WriteLine("False");
            }
            else
            {
                int trueCount = 0;
                int falseCount = 0;
                for (int i = 0; i < firstArray.Length; i++)
                {
                    if (firstArray[i] == secondArray[i])
                    {
                        trueCount++;
                    }
                    else
                    {
                        falseCount++;
                    }
                }
                Console.WriteLine("The arrays have {0} same elements at the same index and {1} different", trueCount, falseCount);
            }
        }
    }
}


text:
+++
Title = "Piotr Gaczkowski"
image = "piotr-gaczkowski.jpg"
Linkedin = "https://www.linkedin.com/in/piotrgaczkowski/"
type = "speaker"
linktitle = "piotr-gaczkowski"
Twitter = "DoomHammerNG"

+++

Music and automation enthusiast that enjoys building simple solutions to human problems.


text:
import { Component } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs/Rx';

import { BaseForm } from '../../common/base/baseForm';

import { Album } from './album';
import { AlbumService } from './album.service';

import { Artist } from '../artist/artist';
import { ArtistService } from '../artist/artist.service';

import {GlobalState} from "../../global.state";
import { LocalStorageService } from 'angular-2-local-storage';
import {Environment} from "../../common/environment";

@Component({
    moduleId: 'album',
    selector: 'album-form',
    templateUrl: './form.html',
    styleUrls: ['./../../common/styles/form.scss'],
    providers: [
        ArtistService,
    ],
})

export class AlbumFormComponent extends BaseForm {

    public listUrl = '/albums';
    public title = 'Albums';

    public model = new Album();

    public artists: Artist[];

    constructor(
        public router: Router,
        public route: ActivatedRoute,
        protected _state: GlobalState,
        protected localStorageService: LocalStorageService,
        private artistService: ArtistService,
        public service: AlbumService) {
        super();
    }

    initCreateForm() {
        Observable.forkJoin(
            this.artistService.allWithoutLimit()
        ).subscribe(
            data => {
                this.active = true;

                this.artists = data[0] as Artist[];

                this.setBreadCrumbs();
            },
            error => this.errorMessage = <any>error);
    }

    initEditForm(id) {
        this.editMode = true;

        Observable.forkJoin(
            this.service.find(id),
            this.artistService.allWithoutLimit()
        ).subscribe(
            data => {
                this.active = true;

                this.model = data[0] as Album;
                this.artists = data[1] as Artist[];

                this.setBreadCrumbs();
            },
            error => this.errorMessage = <any>error);
    }

    getBreadCrumbTitle(): string {
        return this.editMode ? 'Album: ' + this.model.title : 'Create new album';
    }

    deleteImage(event): void {
        this.model.image_base_64 = 'remove';
        this.service.update(this.model, this.model.id)
            .subscribe(
                data => {
                    if (data.id) {
                        let target = event.currentTarget || event.target || event.srcElement;
                        jQuery(target).parent().remove();
                        this.model.image = null;
                    } else {
                        console.log('error delete image', data);
                    }
                },
                error => this.errorMessage = <any>error);
    }

    onChangeImage(event: EventTarget) {
        let eventObj: MSInputMethodContext = <MSInputMethodContext>event;
        let target: HTMLInputElement = <HTMLInputElement>eventObj.target;
        let files: FileList = target.files;

        let reader = new FileReader();
        reader.readAsDataURL(files[0]);
        reader.onload = function () {
            jQuery(document.getElementById('image')).text(reader.result);
        };
        reader.onerror = function (error) {
            jQuery(document.getElementById('image')).text('');
        };
    }

    fillModel(): void {
        let imageBase64 = jQuery(document.getElementById('image')).text();
        this.model.image_base_64 = imageBase64 ? imageBase64 : null;
    }

    get image(): string {
        return this.model.image ? Environment.AWS_S3_BUCKET_URL + 'static/albums/images/' +
            this.getThumbPath(this.model.image, 100) : '';
    }

    getThumbPath (image: string, size: number): string {
        return image.substr(0, image.indexOf('.')) + `/${size}_` + image;
    }

}


text:
import '../../../test/common-test-setup';
import './gr-apply-fix-dialog';
import {navigationToken} from '../../core/gr-navigation/gr-navigation';
import {queryAndAssert, stubRestApi} from '../../../test/test-utils';
import {GrApplyFixDialog} from './gr-apply-fix-dialog';
import {PatchSetNum} from '../../../types/common';
import {
  createFixSuggestionInfo,
  createParsedChange,
  createRevisions,
  getCurrentRevision,
} from '../../../test/test-data-generators';
import {createDefaultDiffPrefs} from '../../../constants/constants';
import {DiffInfo} from '../../../types/diff';
import {
  CloseFixPreviewEventDetail,
  EventType,
  OpenFixPreviewEventDetail,
} from '../../../types/events';
import {GrButton} from '../../shared/gr-button/gr-button';
import {fixture, html, assert} from '@open-wc/testing';
import {SinonStub} from 'sinon';
import {testResolver} from '../../../test/common-test-setup';

suite('gr-apply-fix-dialog tests', () => {
  let element: GrApplyFixDialog;
  let setUrlStub: SinonStub;

  const TWO_FIXES: OpenFixPreviewEventDetail = {
    patchNum: 2 as PatchSetNum,
    fixSuggestions: [
      createFixSuggestionInfo('fix_1'),
      createFixSuggestionInfo('fix_2'),
    ],
  };

  const ONE_FIX: OpenFixPreviewEventDetail = {
    patchNum: 2 as PatchSetNum,
    fixSuggestions: [createFixSuggestionInfo('fix_1')],
  };

  function getConfirmButton(): GrButton {
    return queryAndAssert(
      queryAndAssert(element, '#applyFixDialog'),
      '#confirm'
    );
  }

  async function open(detail: OpenFixPreviewEventDetail) {
    element.open(
      new CustomEvent<OpenFixPreviewEventDetail>(EventType.OPEN_FIX_PREVIEW, {
        detail,
      })
    );
    await element.updateComplete;
  }

  setup(async () => {
    setUrlStub = sinon.stub(testResolver(navigationToken), 'setUrl');
    element = await fixture<GrApplyFixDialog>(
      html`<gr-apply-fix-dialog></gr-apply-fix-dialog>`
    );
    const change = {
      ...createParsedChange(),
      revisions: createRevisions(2),
      current_revision: getCurrentRevision(1),
    };
    element.changeNum = change._number;
    element.patchNum = change.revisions[change.current_revision]._number;
    element.change = change;
    element.diffPrefs = {
      ...createDefaultDiffPrefs(),
      font_size: 12,
      line_length: 100,
      tab_size: 4,
    };
    await element.updateComplete;
  });

  suite('dialog open', () => {
    setup(() => {
      const diffInfo1: DiffInfo = {
        meta_a: {
          name: 'f1',
          content_type: 'text',
          lines: 10,
        },
        meta_b: {
          name: 'f1',
          content_type: 'text',
          lines: 12,
        },
        content: [
          {
            ab: ['loqlwkqll'],
          },
          {
            b: ['qwqqsqw'],
          },
          {
            ab: ['qwqqsqw', 'qweqeqweqeq', 'qweqweq'],
          },
        ],
        change_type: 'MODIFIED',
        intraline_status: 'OK',
      };

      const diffInfo2: DiffInfo = {
        meta_a: {
          name: 'f2',
          content_type: 'text',
          lines: 10,
        },
        meta_b: {
          name: 'f2',
          content_type: 'text',
          lines: 12,
        },
        content: [
          {
            ab: ['eqweqweqwex'],
          },
          {
            b: ['zassdasd'],
          },
          {
            ab: ['zassdasd', 'dasdasda', 'asdasdad'],
          },
        ],
        change_type: 'MODIFIED',
        intraline_status: 'OK',
      };

      stubRestApi('getRobotCommentFixPreview').returns(
        Promise.resolve({
          f1: diffInfo1,
          f2: diffInfo2,
        })
      );
      sinon.stub(element.applyFixModal!, 'showModal');
    });

    test('dialog opens fetch and sets previews', async () => {
      await open(TWO_FIXES);
      assert.equal(element.currentFix!.fix_id, 'fix_1');
      assert.equal(element.currentPreviews.length, 2);
      const button = getConfirmButton();
      assert.isFalse(button.hasAttribute('disabled'));
      assert.equal(button.getAttribute('title'), '');
    });

    test('tooltip is hidden if apply fix is loading', async () => {
      element.isApplyFixLoading = true;
      await open(TWO_FIXES);
      const button = getConfirmButton();
      assert.isTrue(button.hasAttribute('disabled'));
      assert.equal(button.getAttribute('title'), '');
    });

    test('apply fix button is disabled on older patchset', async () => {
      element.change = element.change = {
        ...createParsedChange(),
        revisions: createRevisions(2),
        current_revision: getCurrentRevision(0),
      };
      await open(TWO_FIXES);
      const button = getConfirmButton();
      assert.isTrue(button.hasAttribute('disabled'));
      assert.equal(
        button.getAttribute('title'),
        'Fix can only be applied to the latest patchset'
      );
    });
  });

  test('renders', async () => {
    await open(TWO_FIXES);
    assert.shadowDom.equal(
      element,
      /* HTML */ `
        <dialog id="applyFixModal" tabindex="-1" open="">
          <gr-dialog id="applyFixDialog" role="dialog">

Fix fix_1

              Suggested fix 1 of 2
              <gr-button
                aria-disabled="true"
                disabled=""
                id="prevFix"
                role="button"
                tabindex="-1"
              >
                <gr-icon icon="chevron_left"></gr-icon>
              </gr-button>
              <gr-button
                aria-disabled="false"
                id="nextFix"
                role="button"
                tabindex="0"
              >
                <gr-icon icon="chevron_right"></gr-icon>
              </gr-button>

          </gr-dialog>
        </dialog>
      `,
      {ignoreAttributes: ['style']}
    );
  });

  test('next button state updated when suggestions changed', async () => {
    stubRestApi('getRobotCommentFixPreview').returns(Promise.resolve({}));

    await open(ONE_FIX);
    await element.updateComplete;
    assert.notOk(element.nextFix);
    element.applyFixModal?.close();

    await open(TWO_FIXES);
    assert.ok(element.nextFix);
    assert.notOk(element.nextFix!.disabled);
  });

  test('preview endpoint throws error should reset dialog', async () => {
    stubRestApi('getRobotCommentFixPreview').returns(
      Promise.reject(new Error('backend error'))
    );
    try {
      await open(TWO_FIXES);
    } catch (error) {
      // expected
    }
    assert.equal(element.currentFix, undefined);
  });

  test('apply fix button should call apply, navigate to change view and fire close', async () => {
    const applyRobotFixSuggestionStub = stubRestApi(
      'applyRobotFixSuggestion'
    ).returns(Promise.resolve(new Response(null, {status: 200})));
    element.currentFix = createFixSuggestionInfo('123');

    const closeFixPreviewEventSpy = sinon.spy();
    // Element is recreated after each test, removeEventListener isn't required
    element.addEventListener(
      EventType.CLOSE_FIX_PREVIEW,
      closeFixPreviewEventSpy
    );

    await element.handleApplyFix(new CustomEvent('confirm'));

    sinon.assert.calledOnceWithExactly(
      applyRobotFixSuggestionStub,
      element.change!._number,
      2 as PatchSetNum,
      '123'
    );
    assert.isTrue(setUrlStub.called);
    assert.equal(setUrlStub.lastCall.firstArg, '/c/test-project/+/42/2..edit');

    sinon.assert.calledOnceWithExactly(
      closeFixPreviewEventSpy,
      new CustomEvent<CloseFixPreviewEventDetail>(EventType.CLOSE_FIX_PREVIEW, {
        detail: {
          fixApplied: true,
        },
      })
    );
    // reset gr-apply-fix-dialog and close
    assert.equal(element.currentFix, undefined);
    assert.equal(element.currentPreviews.length, 0);
  });

  test('should not navigate to change view if incorect reponse', async () => {
    const applyRobotFixSuggestionStub = stubRestApi(
      'applyRobotFixSuggestion'
    ).returns(Promise.resolve(new Response(null, {status: 500})));
    element.currentFix = createFixSuggestionInfo('fix_123');

    await element.handleApplyFix(new CustomEvent('confirm'));

    sinon.assert.calledWithExactly(
      applyRobotFixSuggestionStub,
      element.change!._number,
      2 as PatchSetNum,
      'fix_123'
    );
    assert.isFalse(setUrlStub.called);
    assert.equal(element.isApplyFixLoading, false);
  });

  test('select fix forward and back of multiple suggested fixes', async () => {
    sinon.stub(element.applyFixModal!, 'showModal');

    await open(TWO_FIXES);
    element.onNextFixClick(new CustomEvent('click'));
    assert.equal(element.currentFix!.fix_id, 'fix_2');
    element.onPrevFixClick(new CustomEvent('click'));
    assert.equal(element.currentFix!.fix_id, 'fix_1');
  });

  test('server-error should throw for failed apply call', async () => {
    stubRestApi('applyRobotFixSuggestion').returns(
      Promise.reject(new Error('backend error'))
    );
    element.currentFix = createFixSuggestionInfo('fix_123');

    const closeFixPreviewEventSpy = sinon.spy();
    // Element is recreated after each test, removeEventListener isn't required
    element.addEventListener(
      EventType.CLOSE_FIX_PREVIEW,
      closeFixPreviewEventSpy
    );

    let expectedError;
    await element.handleApplyFix(new CustomEvent('click')).catch(e => {
      expectedError = e;
    });
    assert.isOk(expectedError);
    assert.isFalse(setUrlStub.called);
    sinon.assert.notCalled(closeFixPreviewEventSpy);
  });

  test('onCancel fires close with correct parameters', () => {
    const closeFixPreviewEventSpy = sinon.spy();
    // Element is recreated after each test, removeEventListener isn't required
    element.addEventListener(
      EventType.CLOSE_FIX_PREVIEW,
      closeFixPreviewEventSpy
    );
    element.onCancel(new CustomEvent('cancel'));
    sinon.assert.calledOnceWithExactly(
      closeFixPreviewEventSpy,
      new CustomEvent<CloseFixPreviewEventDetail>(EventType.CLOSE_FIX_PREVIEW, {
        detail: {
          fixApplied: false,
        },
      })
    );
  });
});


text:
#ifndef AI_H_
#define AI_H_

/** Contains the state of the AI */
struct ai_state {
    /** Random seed for AI */
    unsigned int seed;
};

/**
 * Seed the AI with a random value.
 *
 * @param state The AI state to seed
 */
void seed_ai(struct ai_state *state);

#endif /* AI_H_ */


text:
package com.github.lg198.codefray.api.math;

/**
 * Represents a two-dimensional point (x, y) of a tile on the map.
 */
public class Point {

    private int x,y;

    /**
     * Creates a two-dimensional point.
     * @param x
     * @param y
     */
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    /**
     * Translates the point by the specified x and y vectors.
     * @param xd The horizontal translation
     * @param yd The vertical translation
     * @return The translated point.
     */
    public Point translate(int xd, int yd) {
        return new Point(x + xd, y + yd);
    }

    public Point in(Direction d) {
        return new Point(x + d.getXChange(), y + d.getYChange());
    }

    public Point in(Direction d, int am) {
        return new Point(x + d.getXChange()*am, y + d.getYChange()*am);
    }

    public boolean inBounds(int xmin, int xmax, int ymin, int ymax) {
        return x >= xmin && x < xmax && y >= ymin && y < ymax;
    }

    @Override
    public String toString() {
        return String.format("(%d, %d)", x, y);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Point point = (Point) o;

        if (x != point.x) return false;
        return y == point.y;

    }

    @Override
    public int hashCode() {
        int result = x;
        result = 31 * result + y;
        return result;
    }
}


text:
module.exports = function (models, config) {

	var admin_group= {
        		name: "Administrators",
        		p_cont_m:true,
				    o_cont_m:true,
				    manage_users:true,
				    manage_groups:true,
				    p_recp_m:true,
				    o_recp_m:true,
				    account_delete:false,
				    deletable: false
        	};

	models.collections.group.findOrCreate({where:{id:"1"}}, admin_group, function(err, group) {
  	if(err) return console.log(err);

  	var admin = {
      	group: group.id,
      	name: config.admin_name,
      	email: config.admin_email,
      	password: config.admin_password
  	};
  	models.collections.user.findOrCreate({where:{id:"1"}}, admin, function(err, user) {
  		if(err) return console.log(err);

  		console.log("Admin created or already exists ! Email : " + user.email)
  	});
  });
}


text:
		<form action="/restaurantes" method="get">
			<input type="text" name="s" class="search__input" placeholder="Digite seu CEP ou endereço">
			<button type="submit" class="search__submit"></button>
		</form>


text:
require 'spec_helper'

describe "included matchers" do
  describe "include_text" do
    it { is_expected.to include_text("inc").before_text("tex")}
  end
end


text:
import Npc from './npc';
import ShipPart from './ship/ship-part.js';
import Sprites from './ship/sprites.js';

class Front extends ShipPart {
    constructor(state, position, next, previous) {
        super(state, position, next, previous);
        this.sprite = Sprites.whaleFront;
    }
}

class Middle extends ShipPart {
    constructor(state, position, next, previous) {
        super(state, position, next, previous);
        this.sprite = Sprites.whaleBody;
    }
}

class Back extends ShipPart {
    constructor(state, position, next, previous) {
        super(state, position, next, previous);
        this.sprite = Sprites.whaleBack;
    }
}

export default class Whale extends Npc {
    constructor(state, entrance, direction) {
        super(state, entrance, direction, Front);
        this.middle = Middle;
        this.back = Back;
    }
}


text:
.. _quickstart:

=====================
Developer Quick-Start
=====================

This is a quick walkthrough to get you started developing code for magnum.
This assumes you are already familiar with submitting code reviews to an
OpenStack project.

.. seealso::

   https://docs.openstack.org/infra/manual/developers.html

Setup Dev Environment
=====================

Install OS-specific prerequisites::

    # Ubuntu Xenial:
    sudo apt update
    sudo apt install python-dev libssl-dev libxml2-dev curl \
                     libmysqlclient-dev libxslt-dev libpq-dev git \
                     libffi-dev gettext build-essential python3-dev

    # CentOS 7:
    sudo yum install -y python-devel openssl-devel mariadb-devel curl \
                        libxml2-devel libxslt-devel postgresql-devel git \
                        libffi-devel gettext gcc

    # Fedora/RHEL:
    sudo yum install python-devel openssl-devel mysql-devel curl \
                     libxml2-devel libxslt-devel postgresql-devel git \
                     libffi-devel gettext gcc

    # openSUSE/SLE 12:
    sudo zypper install git libffi-devel curl \
                        libmysqlclient-devel libopenssl-devel libxml2-devel \
                        libxslt-devel postgresql-devel python-devel \
                        gettext-runtime

Install pip::

    curl -s https://bootstrap.pypa.io/get-pip.py | sudo python

Install common prerequisites::

    sudo pip install virtualenv flake8 tox testrepository git-review

You may need to explicitly upgrade virtualenv if you've installed the one
from your OS distribution and it is too old (tox will complain). You can
upgrade it individually, if you need to::

    sudo pip install -U virtualenv

Magnum source code should be pulled directly from git::

    # from your home or source directory
    cd ~
    git clone https://opendev.org/openstack/magnum
    cd magnum

All unit tests should be run using tox. To run magnum's entire test suite::

    # run all tests (unit and pep8)
    tox

To run a specific test, use a positional argument for the unit tests::

    # run a specific test for Python 3.7
    tox -epy37 -- test_conductor

You may pass options to the test programs using positional arguments::

    # run all the Python 3.7 unit tests (in parallel!)
    tox -epy37 -- --parallel

To run only the pep8/flake8 syntax and style checks::

    tox -epep8

To run unit test coverage and check percentage of code covered::

    tox -e cover

To discover and interact with templates, please refer to
:doc:`/user/cluster-type-definition`.

Exercising the Services Using DevStack
======================================

DevStack can be configured to enable magnum support. It is easy to develop
magnum with the DevStack environment. Magnum depends on nova, glance, heat and
neutron to create and schedule virtual machines to simulate bare-metal (full
bare-metal support is under active development).

Minimum System Requirements
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Magnum running in DevStack requires at least: 10 GB RAM, 8 CPU and 100 GB disk
storage.

**NOTE:** Running DevStack within a virtual machine with magnum enabled is not
recommended at this time.

This session has only been tested on Ubuntu 16.04 (Xenial) and Fedora 20/21.
We recommend users to select one of them if it is possible.

Set-up Environment and Create a Magnum Session
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Clone devstack::

    # Create a root directory for devstack if needed
    sudo mkdir -p /opt/stack
    sudo chown $USER /opt/stack

    git clone https://opendev.org/openstack/devstack /opt/stack/devstack

We will run devstack with minimal local.conf settings required to enable
magnum, heat, and neutron (neutron is enabled by default in devstack since
Kilo, and heat must be enabled by yourself)::

    $ cat > /opt/stack/devstack/local.conf << END
    [[local|localrc]]
    DATABASE_PASSWORD=password
    RABBIT_PASSWORD=password
    SERVICE_TOKEN=password
    SERVICE_PASSWORD=password
    ADMIN_PASSWORD=password
    # magnum requires the following to be set correctly
    PUBLIC_INTERFACE=eth1

    # Enable barbican service and use it to store TLS certificates
    # For details https://docs.openstack.org/magnum/latest/user/index.html#transport-layer-security
    enable_plugin barbican https://opendev.org/openstack/barbican

    enable_plugin heat https://opendev.org/openstack/heat

    # Enable magnum plugin after dependent plugins
    enable_plugin magnum https://opendev.org/openstack/magnum

    # Optional:  uncomment to enable the Magnum UI plugin in Horizon
    #enable_plugin magnum-ui https://opendev.org/openstack/magnum-ui

    VOLUME_BACKING_FILE_SIZE=20G
    END

**NOTE:** Update PUBLIC_INTERFACE as appropriate for your system.

**NOTE:** Enable heat plugin is necessary.

Optionally, you can enable neutron/lbaas v2 with octavia to create load
balancers for multi master clusters::

    $ cat >> /opt/stack/devstack/local.conf << END
    enable_plugin neutron-lbaas https://opendev.org/openstack/neutron-lbaas
    enable_plugin octavia https://opendev.org/openstack/octavia

    # Disable LBaaS(v1) service
    disable_service q-lbaas
    # Enable LBaaS(v2) services
    enable_service q-lbaasv2
    enable_service octavia
    enable_service o-cw
    enable_service o-hk
    enable_service o-hm
    enable_service o-api
    END

Optionally, you can enable ceilometer in devstack. If ceilometer is enabled,
magnum will periodically send metrics to ceilometer::

    $ cat >> /opt/stack/devstack/local.conf << END
    enable_plugin ceilometer https://opendev.org/openstack/ceilometer
    END

If you want to deploy Docker Registry 2.0 in your cluster, you should enable
swift in devstack::

    $ cat >> /opt/stack/devstack/local.conf << END
    enable_service s-proxy
    enable_service s-object
    enable_service s-container
    enable_service s-account
    END

More devstack configuration information can be found at
https://docs.openstack.org/devstack/latest/configuration.html

More neutron configuration information can be found at
https://docs.openstack.org/devstack/latest/guides/neutron.html

Run devstack::

    cd /opt/stack/devstack
    ./stack.sh

**NOTE:** This will take a little extra time when the Fedora Atomic micro-OS
image is downloaded for the first time.

At this point, two magnum process (magnum-api and magnum-conductor) will be
running on devstack screens. If you make some code changes and want to
test their effects, just stop and restart magnum-api and/or magnum-conductor.

Prepare your session to be able to use the various openstack clients including
magnum, neutron, and glance. Create a new shell, and source the devstack openrc
script::

    . /opt/stack/devstack/openrc admin admin

Magnum has been tested with the Fedora Atomic micro-OS and CoreOS. Magnum will
likely work with other micro-OS platforms, but each requires individual
support in the heat template.

The Fedora Atomic micro-OS image will automatically be added to glance.  You
can add additional images manually through glance. To verify the image created
when installing devstack use::

    $ openstack image list

    +--------------------------------------+------------------------------------+--------+
    | ID                                   | Name                               | Status |
    +--------------------------------------+------------------------------------+--------+
    | 0bc132b1-ee91-4bd8-b0fd-19deb57fb39f | Fedora-Atomic-27-20180419.0.x86_64 | active |
    | 7537bbf2-f1c3-47da-97bb-38c09007e146 | cirros-0.3.5-x86_64-disk           | active |
    +--------------------------------------+------------------------------------+--------+

To list the available commands and resources for magnum, use::

    openstack help coe

To list out the health of the internal services, namely conductor, of magnum,
use::

    $ openstack coe service list

    +----+---------------------------------------+------------------+-------+----------+-----------------+---------------------------+---------------------------+
    | id | host                                  | binary           | state | disabled | disabled_reason | created_at                | updated_at                |
    +----+---------------------------------------+------------------+-------+----------+-----------------+---------------------------+---------------------------+
    | 1  | oxy-dev.hq1-0a5a3c02.hq1.abcde.com    | magnum-conductor | up    |          | -               | 2016-08-31T10:03:36+00:00 | 2016-08-31T10:11:41+00:00 |
    +----+---------------------------------------+------------------+-------+----------+-----------------+---------------------------+---------------------------+

Create a keypair for use with the ClusterTemplate::

    test -f ~/.ssh/id_rsa.pub || ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
    openstack keypair create --public-key ~/.ssh/id_rsa.pub testkey

Check a dns server can resolve a host name properly::

    dig <server name> @<dns server> +short

For example::

    $ dig www.openstack.org @8.8.8.8 +short
    www.openstack.org.cdn.cloudflare.net.
    104.20.64.68
    104.20.65.68

Building a Kubernetes Cluster - Based on Fedora CoreOS
======================================================

Create a cluster template. This is similar in nature to a flavor and describes
to magnum how to construct the cluster. The ClusterTemplate specifies a Fedora
CoreOS image so the clusters which use this ClusterTemplate will be based on
Fedora CoreOS
\
::

    openstack coe cluster template create k8s-cluster-template \
        --image fedora-coreos-35.20220116.3.0-openstack.x86_64 \
        --keypair testkey \
        --external-network public \
        --dns-nameserver 8.8.8.8 \
        --flavor ds1G \
        --master-flavor ds2G \
        --docker-volume-size 5 \
        --network-driver flannel \
        --docker-storage-driver overlay2 \
        --coe kubernetes

Create a cluster. Use the ClusterTemplate name as a template for cluster
creation. This cluster will result in one master kubernetes node and one minion
node
::

    openstack coe cluster create k8s-cluster \
                          --cluster-template k8s-cluster-template \
                          --node-count 1

Clusters will have an initial status of CREATE_IN_PROGRESS.  Magnum will update
the status to CREATE_COMPLETE when it is done creating the cluster.  Do not
create containers, pods, services, or replication controllers before magnum
finishes creating the cluster. They will likely not be created, and may cause
magnum to become confused.

The existing clusters can be listed as follows::

    $ openstack coe cluster list

    +--------------------------------------+-------------+------------+--------------+-----------------+
    | uuid                                 | name        | node_count | master_count | status          |
    +--------------------------------------+-------------+------------+--------------------------------+
    | 9dccb1e6-02dc-4e2b-b897-10656c5339ce | k8s-cluster | 1          | 1            | CREATE_COMPLETE |
    +--------------------------------------+-------------+------------+--------------+-----------------+

More detailed information for a given cluster is obtained via::

    openstack coe cluster show k8s-cluster

After a cluster is created, you can dynamically add/remove node(s) to/from the
cluster by updating the node_count attribute. For example, to add one more
node::

    openstack coe cluster update k8s-cluster replace node_count=2

Clusters in the process of updating will have a status of UPDATE_IN_PROGRESS.
Magnum will update the status to UPDATE_COMPLETE when it is done updating
the cluster.

**NOTE:** Reducing node_count will remove all the existing pods on the nodes
that are deleted. If you choose to reduce the node_count, magnum will first
try to remove empty nodes with no pods running on them. If you reduce
node_count by more than the number of empty nodes, magnum must remove nodes
that have running pods on them. This action will delete those pods. We
strongly recommend using a replication controller before reducing the
node_count so any removed pods can be automatically recovered on your
remaining nodes.

Heat can be used to see detailed information on the status of a stack or
specific cluster:

To check the list of all cluster stacks::

    openstack stack list

To check an individual cluster's stack::

    openstack stack show <stack-name or stack_id>

Monitoring cluster status in detail (e.g., creating, updating)::

    CLUSTER_HEAT_NAME=$(openstack stack list | \
                        awk "/\sk8s-cluster-/{print \$4}")
    echo ${CLUSTER_HEAT_NAME}
    openstack stack resource list ${CLUSTER_HEAT_NAME}

Building a Kubernetes Cluster - Based on Fedora Atomic [DEPRECATED]
===================================================================

`Fedora Atomic Deprecation Notice <https://docs.fedoraproject.org/en-US/fedora-coreos/faq/#_does_fedora_coreos_replace_fedora_atomic_host_what_happens_to_fedora_atomic_host_and_centos_atomic_host>`_:

    Fedora CoreOS is the official successor to Fedora Atomic Host. The last
    Fedora Atomic Host release was version 29, which has now reached
    end-of-life.

When building devstack from master, the Fedora atomic image is no longer
created for us by default. We will need to create an image ourselves.
::

    wget https://dl.fedoraproject.org/pub/alt/atomic/stable/Fedora-Atomic-27-20180419.0/CloudImages/x86_64/images/Fedora-Atomic-27-20180419.0.x86_64.qcow2

    openstack image create Fedora-Atomic-27-20180419.0.x86_64  \
                           --public \
                           --disk-format=qcow2 \
                           --container-format=bare \
                           --property os_distro=fedora-atomic \
                           --file=Fedora-Atomic-27-20180419.0.x86_64.qcow2

Create a ClusterTemplate. This is similar in nature to a flavor and describes
to magnum how to construct the cluster. The ClusterTemplate specifies a Fedora
Atomic image so the clusters which use this ClusterTemplate will be based on
Fedora Atomic
::

    openstack coe cluster template create k8s-cluster-template \
                           --image Fedora-Atomic-27-20180419.0.x86_64 \
                           --keypair testkey \
                           --external-network public \
                           --dns-nameserver 8.8.8.8 \
                           --flavor m1.small \
                           --docker-volume-size 5 \
                           --network-driver flannel \
                           --coe kubernetes

Create a cluster. Use the ClusterTemplate name as a template for cluster
creation. This cluster will result in one master kubernetes node and one minion
node::

    openstack coe cluster create k8s-cluster \
                          --cluster-template k8s-cluster-template \
                          --node-count 1

Building a Kubernetes Cluster - Based on CoreOS [DEPRECATED]
============================================================

`End-of-life announcement for CoreOS Container Linux <https://coreos.com/os/eol/>`_:

    On May 26, 2020, CoreOS Container Linux will reach its end of life and will
    no longer receive updates. We strongly recommend that users begin migrating
    their workloads to another operating system as soon as possible. [...]
    Fedora CoreOS is the official successor to CoreOS Container Linux

You can create a Kubernetes cluster based on CoreOS as an alternative to
Atomic or Fedora CoreOS. First, download the official CoreOS image::

    wget http://beta.release.core-os.net/amd64-usr/current/coreos_production_openstack_image.img.bz2
    bunzip2 coreos_production_openstack_image.img.bz2

Upload the image to glance::

    openstack image create CoreOS  \
                        --public \
                        --disk-format=qcow2 \
                        --container-format=bare \
                        --property os_distro=coreos \
                        --file=coreos_production_openstack_image.img

Create a CoreOS Kubernetes ClusterTemplate, which is similar to the Atomic
Kubernetes ClusterTemplate, except for pointing to a different image::

    openstack coe cluster template create k8s-cluster-template-coreos \
                           --image CoreOS \
                           --keypair testkey \
                           --external-network public \
                           --dns-nameserver 8.8.8.8 \
                           --flavor m1.small \
                           --network-driver flannel \
                           --coe kubernetes

Create a CoreOS Kubernetes cluster. Use the CoreOS ClusterTemplate as a
template for cluster creation::

    openstack coe cluster create k8s-cluster \
                      --cluster-template k8s-cluster-template-coreos \
                      --node-count 2

Using a Kubernetes Cluster
==========================

**NOTE:** For the following examples, only one minion node is required in the
k8s cluster created previously.

Kubernetes provides a number of examples you can use to check that things are
working. You may need to download kubectl binary for interacting with k8s
cluster using::

    curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.2.0/bin/linux/amd64/kubectl
    chmod +x ./kubectl
    sudo mv ./kubectl /usr/local/bin/kubectl

We first need to setup the certs to allow Kubernetes to authenticate our
connection.   Please refer to :ref:`transport_layer_security`
for more info on using TLS keys/certs which are setup below.

To generate an RSA key, you will use the 'genrsa' command of the 'openssl'
tool.::

    openssl genrsa -out client.key 4096

To generate a CSR for client authentication, openssl requires a config file
that specifies a few values.::

    $ cat > client.conf << END
    [req]
    distinguished_name = req_distinguished_name
    req_extensions     = req_ext
    prompt = no
    [req_distinguished_name]
    CN = admin
    O = system:masters
    OU=OpenStack/Magnum
    C=US
    ST=TX
    L=Austin
    [req_ext]
    extendedKeyUsage = clientAuth
    END

Once you have client.conf, you can run the openssl 'req' command to generate
the CSR.::

    openssl req -new -days 365 \
        -config client.conf \
        -key client.key \
        -out client.csr

Now that you have your client CSR, you can use the Magnum CLI to send it off
to Magnum to get it signed and also download the signing cert.::

    magnum ca-sign --cluster k8s-cluster --csr client.csr > client.crt
    magnum ca-show --cluster k8s-cluster > ca.crt

Here's how to set up the replicated redis example. Now we create a pod for the
redis-master::

    # Using cluster-config command for faster configuration
    eval $(openstack coe cluster config k8s-cluster)

    # Test the cert and connection works
    kubectl version

    cd kubernetes/examples/redis
    kubectl create -f ./redis-master.yaml

Now create a service to provide a discoverable endpoint for the redis
sentinels in the cluster::

    kubectl create -f ./redis-sentinel-service.yaml

To make it a replicated redis cluster create replication controllers for the
redis slaves and sentinels::

    sed -i 's/\(replicas: \)1/\1 2/' redis-controller.yaml
    kubectl create -f ./redis-controller.yaml

    sed -i 's/\(replicas: \)1/\1 2/' redis-sentinel-controller.yaml
    kubectl create -f ./redis-sentinel-controller.yaml

Full lifecycle and introspection operations for each object are supported.
For example, openstack coe cluster create, openstack coe cluster template
delete.

Now there are four redis instances (one master and three slaves) running
across the cluster, replicating data between one another.

Run the openstack coe cluster show command to get the IP of the cluster host on
which the redis-master is running::

    $ openstack coe cluster show k8s-cluster

    +--------------------+------------------------------------------------------------+
    | Property           | Value                                                      |
    +--------------------+------------------------------------------------------------+
    | status             | CREATE_COMPLETE                                            |
    | uuid               | cff82cd0-189c-4ede-a9cb-2c0af6997709                       |
    | stack_id           | 7947844a-8e18-4c79-b591-ecf0f6067641                       |
    | status_reason      | Stack CREATE completed successfully                        |
    | created_at         | 2016-05-26T17:45:57+00:00                                  |
    | updated_at         | 2016-05-26T17:50:02+00:00                                  |
    | create_timeout     | 60                                                         |
    | api_address        | https://172.24.4.4:6443                                    |
    | coe_version        | v1.2.0                                                     |
    | cluster_template_id| e73298e7-e621-4d42-b35b-7a1952b97158                       |
    | master_addresses   | ['172.24.4.6']                                             |
    | node_count         | 1                                                          |
    | node_addresses     | ['172.24.4.5']                                             |
    | master_count       | 1                                                          |
    | container_version  | 1.9.1                                                      |
    | discovery_url      | https://discovery.etcd.io/4caaa65f297d4d49ef0a085a7aecf8e0 |
    | name               | k8s-cluster                                                |
    +--------------------+------------------------------------------------------------+

The output here indicates the redis-master is running on the cluster host with
IP address 172.24.4.5. To access the redis master::

    $ ssh fedora@172.24.4.5
    $ REDIS_ID=$(sudo docker ps | grep redis:v1 | grep k8s_master | awk '{print $1}')
    $ sudo docker exec -i -t $REDIS_ID redis-cli

    127.0.0.1:6379> set replication:test true
    OK
    ^D

    $ exit  # Log out of the host

Log into one of the other container hosts and access a redis slave from it.
You can use `nova list` to enumerate the kube-minions. For this example we
will use the same host as above::

    $ ssh fedora@172.24.4.5
    $ REDIS_ID=$(sudo docker ps | grep redis:v1 | grep k8s_redis | awk '{print $1}')
    $ sudo docker exec -i -t $REDIS_ID redis-cli

    127.0.0.1:6379> get replication:test
    "true"
    ^D

    $ exit  # Log out of the host

Additional useful commands from a given minion::

    sudo docker ps  # View Docker containers on this minion
    kubectl get pods  # Get pods
    kubectl get rc  # Get replication controllers
    kubectl get svc  # Get services
    kubectl get nodes  # Get nodes

After you finish using the cluster, you want to delete it. A cluster can be
deleted as follows::

    openstack coe cluster delete k8s-cluster

Building and Using a Swarm Cluster
==================================

Create a ClusterTemplate. It is very similar to the Kubernetes ClusterTemplate,
except for the absence of some Kubernetes-specific arguments and the use of
'swarm' as the COE::

    openstack coe cluster template create swarm-cluster-template \
                           --image Fedora-Atomic-27-20180419.0.x86_64 \
                           --keypair testkey \
                           --external-network public \
                           --dns-nameserver 8.8.8.8 \
                           --flavor m1.small \
                           --docker-volume-size 5 \
                           --coe swarm-mode

**NOTE:** If you are using Magnum behind a firewall then refer
to :doc:`/admin/magnum-proxy`.

Finally, create the cluster. Use the ClusterTemplate 'swarm-cluster-template'
as a template for cluster creation. This cluster will result in one swarm
manager node and two extra agent nodes::

    openstack coe cluster create swarm-cluster \
                          --cluster-template swarm-cluster-template \
                          --node-count 2

Now that we have a swarm cluster we can start interacting with it::

    $ openstack coe cluster show swarm-cluster

    +--------------------+------------------------------------------------------------+
    | Property           | Value                                                      |
    +--------------------+------------------------------------------------------------+
    | status             | CREATE_COMPLETE                                            |
    | uuid               | eda91c1e-6103-45d4-ab09-3f316310fa8e                       |
    | stack_id           | 7947844a-8e18-4c79-b591-ecf0f6067641                       |
    | status_reason      | Stack CREATE completed successfully                        |
    | created_at         | 2015-04-20T19:05:27+00:00                                  |
    | updated_at         | 2015-04-20T19:06:08+00:00                                  |
    | create_timeout     | 60                                                         |
    | api_address        | https://172.24.4.4:6443                                    |
    | coe_version        | 1.2.5                                                      |
    | cluster_template_id| e73298e7-e621-4d42-b35b-7a1952b97158                       |
    | master_addresses   | ['172.24.4.6']                                             |
    | node_count         | 2                                                          |
    | node_addresses     | ['172.24.4.5']                                             |
    | master_count       | 1                                                          |
    | container_version  | 1.9.1                                                      |
    | discovery_url      | https://discovery.etcd.io/4caaa65f297d4d49ef0a085a7aecf8e0 |
    | name               | swarm-cluster                                              |
    +--------------------+------------------------------------------------------------+

We now need to setup the docker CLI to use the swarm cluster we have created
with the appropriate credentials.

Create a dir to store certs and cd into it. The `DOCKER_CERT_PATH` env variable
is consumed by docker which expects ca.pem, key.pem and cert.pem to be in that
directory.::

    export DOCKER_CERT_PATH=~/.docker
    mkdir -p ${DOCKER_CERT_PATH}
    cd ${DOCKER_CERT_PATH}

Generate an RSA key.::

    openssl genrsa -out key.pem 4096

Create openssl config to help generated a CSR.::

    $ cat > client.conf << END
    [req]
    distinguished_name = req_distinguished_name
    req_extensions     = req_ext
    prompt = no
    [req_distinguished_name]
    CN = Your Name
    [req_ext]
    extendedKeyUsage = clientAuth
    END

Run the openssl 'req' command to generate the CSR.::

    openssl req -new -days 365 \
        -config client.conf \
        -key key.pem \
        -out client.csr

Now that you have your client CSR use the Magnum CLI to get it signed and also
download the signing cert.::

    magnum ca-sign --cluster swarm-cluster --csr client.csr > cert.pem
    magnum ca-show --cluster swarm-cluster > ca.pem

Set the CLI to use TLS . This env var is consumed by docker.::

    export DOCKER_TLS_VERIFY="1"

Set the correct host to use which is the public ip address of swarm API server
endpoint. This env var is consumed by docker.::

    export DOCKER_HOST=$(openstack coe cluster show swarm-cluster | awk '/ api_address /{print substr($4,7)}')

Next we will create a container in this swarm cluster. This container will ping
the address 8.8.8.8 four times::

    docker run --rm -it cirros:latest ping -c 4 8.8.8.8

You should see a similar output to::

    PING 8.8.8.8 (8.8.8.8): 56 data bytes
    64 bytes from 8.8.8.8: seq=0 ttl=40 time=25.513 ms
    64 bytes from 8.8.8.8: seq=1 ttl=40 time=25.348 ms
    64 bytes from 8.8.8.8: seq=2 ttl=40 time=25.226 ms
    64 bytes from 8.8.8.8: seq=3 ttl=40 time=25.275 ms

    --- 8.8.8.8 ping statistics ---
    4 packets transmitted, 4 packets received, 0% packet loss
    round-trip min/avg/max = 25.226/25.340/25.513 ms

Building Developer Documentation
================================

To build the documentation locally (e.g., to test documentation changes
before uploading them for review) chdir to the magnum root folder and
run tox::

    tox -edocs

**NOTE:** The first time you run this will take some extra time as it
creates a virtual environment to run in.

When complete, the documentation can be accessed from::

    doc/build/html/index.html


text:
export default function Categories({ categories }) {
  return (

      under
      {categories.edges.length > 0 ? (
        categories.edges.map((category, index) => (

            {category.node.name}

        ))
      ) : (
        {categories.edges.node.name}
      )}

  )
}


text:
Cereal Visualization by Amber Feng

Comparison of Nutritional Facts for the 7 major cereal manufacturers

                Calories (cal)
                Protein (g)
                Fat (g)
                Carbohydrates (g)

What's this?

Recommended nutritional intake for breakfast (assuming breakfast is 1/5 of daily intake) with the average nutritional information from top cereal manufacturers.

                A
                G
                K
                N
                Q
                P
                R

            A  AAHF Products
            G  General Mill
            K  Kellogs
            N  Nabisco
            Q  Post
            P  Quaker Oats
            R  Ralston Purina

Hierarchy of Cereal Brands

What's this?

Percentage of cereals offered by the top cereal manufacturers.

Visual Heatmap of Cereal Nutrition

What's this?

Visual "heatmap" of relative nutritional values for various cereal brands. Darker colors mean high positive deviation from the mean for that particular category, while lighter colors mean high negative deviation from the mean.

Design by Amber Feng, Graph API by Protovis, Data set by CS194-16 Spring 2011, UC Berkeley


text:
var utils = require("utils/utils");
var view = require("ui/core/view");
var common = require("./absolute-layout-common");
global.moduleMerge(common, exports);
function setNativeProperty(data, setter) {
    var uiView = data.object;
    if (uiView instanceof view.View) {
        var nativeView = uiView._nativeView;
        var lp = nativeView.getLayoutParams();
        if (!(lp instanceof org.nativescript.widgets.CommonLayoutParams)) {
            lp = new org.nativescript.widgets.CommonLayoutParams();
        }
        setter(lp);
        nativeView.setLayoutParams(lp);
    }
}
function setNativeLeftProperty(data) {
    setNativeProperty(data, function (lp) { lp.left = data.newValue * utils.layout.getDisplayDensity(); });
}
function setNativeTopProperty(data) {
    setNativeProperty(data, function (lp) { lp.top = data.newValue * utils.layout.getDisplayDensity(); });
}
common.AbsoluteLayout.leftProperty.metadata.onSetNativeValue = setNativeLeftProperty;
common.AbsoluteLayout.topProperty.metadata.onSetNativeValue = setNativeTopProperty;
var AbsoluteLayout = (function (_super) {
    __extends(AbsoluteLayout, _super);
    function AbsoluteLayout() {
        _super.apply(this, arguments);
    }
    Object.defineProperty(AbsoluteLayout.prototype, "android", {
        get: function () {
            return this._layout;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(AbsoluteLayout.prototype, "_nativeView", {
        get: function () {
            return this._layout;
        },
        enumerable: true,
        configurable: true
    });
    AbsoluteLayout.prototype._createUI = function () {
        this._layout = new org.nativescript.widgets.AbsoluteLayout(this._context);
    };
    return AbsoluteLayout;
})(common.AbsoluteLayout);
exports.AbsoluteLayout = AbsoluteLayout;


text:
package edu.Groove9.TunesMaster.statistics;

import edu.Groove9.TunesMaster.BasePresenter;
import edu.Groove9.TunesMaster.BaseView;

/**
 * This specifies the contract between the view and the presenter.
 */
public interface StatisticsContract {

    interface View extends BaseView<Presenter> {

        void showUserSessionStatus();
    }

    interface Presenter extends BasePresenter {

    }
}


text:
require File.expand_path('../../spec_helper', File.dirname(__FILE__))

describe WillFilter::Filter do
  describe 'creating new filter' do
    context 'when filter extensions are enabled' do
      context 'creating base filter' do
        it 'should raise an exception' do
          WillFilter::Config.stub!(:require_filter_extensions?).and_return(true)
        	expect { WillFilter::Filter.new(User) }.should raise_error
        end
      end
      context 'creating extended filter' do
        it 'should be fine' do
          WillFilter::Config.stub!(:require_filter_extensions?).and_return(true)
          filter = UserFilter.new
          filter.should be_a(UserFilter)
          filter.model_class.should eq(User)
        end
      end
    end

    context 'when filter extensions are disabled' do
      it "should create a filter" do
        WillFilter::Config.stub!(:require_filter_extensions?).and_return(false)
        filter = WillFilter::Filter.new(User)
        filter.should be_a(WillFilter::Filter)
        filter.model_class_name.should eq('User')
        filter.model_class.should eq(User)
        filter.table_name.should eq('users')
      end
    end
  end

  describe 'filter methods' do
    before :all do
      @filter = WillFilter::Filter.new(User)
    end

    it 'definition should contain the right attributes' do
      @filter.definition.keys.should eq([:id, :first_name, :last_name, :birthday, :sex, :created_at, :updated_at])
    end

    context 'container_by_sql_type' do
      it 'should map the right containers' do
        ['bigint', 'numeric', 'smallint', 'integer', 'int'].each do |type|
          @filter.container_by_sql_type(type).should eq(["nil", "numeric", "numeric_range", "numeric_delimited"])
        end

        ['float', 'double'].each do |type|
          @filter.container_by_sql_type(type).should eq(["nil", "double", "double_range", "double_delimited"])
        end

        ['timestamp', 'datetime'].each do |type|
          @filter.container_by_sql_type(type).should eq(["nil", "date_time", "date_time_range", "single_date"])
        end

        ['date'].each do |type|
          @filter.container_by_sql_type(type).should eq(["nil", "date", "date_range"])
        end

        ['char', 'character', 'varchar', 'text', 'text[]', 'bytea'].each do |type|
          @filter.container_by_sql_type(type).should eq(["nil", "text", "text_delimited"])
        end

        ['boolean', 'tinyint'].each do |type|
          @filter.container_by_sql_type(type).should eq(["nil", "boolean"])
        end
      end
    end

    context 'default_condition_definition_for' do
      it 'should map the right containers' do
        @filter.default_condition_definition_for('id', 'int').should eq({
             :is_provided=>"nil",
             :is_not_provided=>"nil",
             :is=>"numeric",
             :is_not=>"numeric",
             :is_less_than=>"numeric",
             :is_greater_than=>"numeric",
             :is_in_the_range=>"numeric_range",
             :is_in=>"numeric_delimited",
             :is_filtered_by=>:filter_list
        })

        @filter.default_condition_definition_for('first_name', 'varchar').should eq({
             :is_provided=>"nil",
             :is_not_provided=>"nil",
             :is=>"text",
             :is_not=>"text",
             :contains=>"text",
             :does_not_contain=>"text",
             :starts_with=>"text",
             :ends_with=>"text",
             :is_in=>"text_delimited",
             :is_not_in=>"text_delimited"
       })
      end
    end

    context 'operator sorting' do
      it 'should sort the operators according to default sort' do
        WillFilter::Config.operator_order.should eq([
           "is",
           "is_not",
           "is_on",
           "is_in",
           "is_provided",
           "is_not_provided",
           "is_after",
           "is_before",
           "is_in_the_range",
           "contains",
           "does_not_contain",
           "starts_with",
           "ends_with",
           "is_greater_than",
           "is_less_than",
           "is_filtered_by"
        ])

        @filter.sorted_operators({
           :is_provided=>"nil",
           :is_not_provided=>"nil",
           :is=>"numeric",
           :is_not=>"numeric",
           :is_less_than=>"numeric",
           :is_greater_than=>"numeric",
           :is_in_the_range=>"numeric_range",
           :is_in=>"numeric_delimited",
           :is_filtered_by=>:filter_list
        }).should eq([
           "is",
           "is_not",
           "is_in",
           "is_provided",
           "is_not_provided",
           "is_in_the_range",
           "is_greater_than",
           "is_less_than",
           "is_filtered_by"
        ])
      end
    end
  end

  describe 'serialization' do
    context 'serializing a default filter with no params' do
      before :all do
        @filter = WillFilter::Filter.new(User)
      end

      it 'should create a default filter hash' do
        @filter.to_params.should eq({
           "wf_type"=>"WillFilter::Filter",
           "wf_match"=>:all,
           "wf_model"=>"User",
           "wf_order"=>"id",
           "wf_order_type"=>"desc",
           "wf_per_page"=>30,
           "wf_export_fields"=>"",
           "wf_export_format"=>:html
        })
      end
    end

    context 'serializing a filter with conditions' do
      before :all do
        @filter = WillFilter::Filter.new(User)
        @filter.add_condition(:first_name, :is, "Alex")
        @filter.add_condition(:sex, :is, "male")
      end

      it 'should create a default filter hash' do
        @filter.to_params.should eq({
           "wf_type"=>"WillFilter::Filter",
           "wf_match"=>:all,
           "wf_model"=>"User",
           "wf_order"=>"id",
           "wf_order_type"=>"desc",
           "wf_per_page"=>30,
           "wf_export_fields"=>"",
           "wf_export_format"=>:html,
           "wf_c0"=>:first_name,
           "wf_o0"=>:is,
           "wf_v0_0"=>"Alex",
           "wf_c1"=>:sex,
           "wf_o1"=>:is,
           "wf_v1_0"=>"male"
        })
      end
    end

    context 'serializing a default filter with params' do
      before :all do
        @filter = WillFilter::Filter.new(User)
      end

      it 'should create a default filter hash' do
        @filter.to_params(:extra => 'value').should eq({
                                                           "wf_type"=>"WillFilter::Filter",
                                                           "wf_match"=>:all,
                                                           "wf_model"=>"User",
                                                           "wf_order"=>"id",
                                                           "wf_order_type"=>"desc",
                                                           "wf_per_page"=>30,
                                                           "wf_export_fields"=>"",
                                                           "wf_export_format"=>:html,
                                                           "extra"=>"value"
                                                       })
      end
    end
  end

  describe '#add_condition' do
    let(:filter){ WillFilter::Filter.new(User) }

    context 'when adding a condition with a key that is a string' do
      it 'adds the condition with the key as a symbol' do
        filter.add_condition("sex", :is, "male")

        filter.conditions.size.should eq(1)
        filter.conditions.first.key.should eq(:sex)
      end
    end

    context 'when adding a condition with an operator key that is a string' do
      it 'adds the condition with the operator key as a symbol' do
        filter.add_condition(:sex, "is", "male")

        filter.conditions.size.should eq(1)
        filter.conditions.first.operator.should eq(:is)
      end
    end

    context 'when adding a condition over an inexistent attribute' do
      it 'does not add the condition' do
        filter.add_condition(:some_attribute, :is, "male")

        filter.conditions.size.should eq(0)
      end
    end
  end

  describe 'deserialization' do

    context 'deserializing a filter with conditions' do
      before :all do
        @filter = WillFilter::Filter.new(User)
      end

      it 'should create a default filter hash' do
        @filter.from_params({
           "wf_type"=>"WillFilter::Filter",
           "wf_match"=>:all,
           "wf_model"=>"User",
           "wf_order"=>"first_name",
           "wf_order_type"=>"asc",
           "wf_per_page"=>30,
           "wf_export_fields"=>"",
           "wf_export_format"=>:html,
           "wf_c0"=>:first_name,
           "wf_o0"=>:is,
           "wf_v0_0"=>"Alex",
           "wf_c1"=>:sex,
           "wf_o1"=>:is,
           "wf_v1_0"=>"male"
        })

        @filter.order.should eq('first_name')
        @filter.order_type.should eq('asc')
        @filter.conditions.size.should eq(2)
        @filter.conditions.first.key.should eq(:first_name)
        @filter.conditions.first.operator.should eq(:is)
      end
    end

    context 'When the serialized data has a condition over an inexistent attribute' do
      before :all do
        @filter = WillFilter::Filter.new(User)
      end

      it 'ignores the condition' do
        @filter.from_params({
                                "wf_type"=>"WillFilter::Filter",
                                "wf_match"=>:all,
                                "wf_model"=>"User",
                                "wf_order"=>"first_name",
                                "wf_order_type"=>"asc",
                                "wf_per_page"=>30,
                                "wf_export_fields"=>"",
                                "wf_export_format"=>:html,
                                "wf_c0"=>:inexistent_attribute,
                                "wf_o0"=>:is,
                                "wf_v0_0"=>"Alex",
                                "wf_c1"=>:sex,
                                "wf_o1"=>:is,
                                "wf_v1_0"=>"male"
                            })

        @filter.order.should eq('first_name')
        @filter.order_type.should eq('asc')
        @filter.conditions.size.should eq(1)
        @filter.conditions.first.key.should eq(:sex)
        @filter.conditions.first.operator.should eq(:is)
      end
    end

  end

  describe '#merge_params' do

    context 'When a filter exists with some conditions' do
      let!(:existent_filter) do
        WillFilter::Filter.new(User).from_params({
                                "wf_type"=>"WillFilter::Filter",
                                "wf_match"=>:all,
                                "wf_model"=>"User",
                                "wf_order"=>"first_name",
                                "wf_order_type"=>"asc",
                                "wf_per_page"=>30,
                                "wf_export_fields"=>"",
                                "wf_export_format"=>:html,
                                "wf_c0"=>:first_name,
                                "wf_o0"=>:is,
                                "wf_v0_0"=>"Alex",
                                "wf_c1"=>:sex,
                                "wf_o1"=>:is,
                                "wf_v1_0"=>"male"
                            })
      end

      context "and override some params" do
        let(:new_params){ {"wf_order" => "last_name"} }

        it "takes the new params over the existing ones" do
          existent_filter.merge_params(new_params).order.should eq('last_name')
        end

        it "preserve unchanged params" do
          existent_filter.order_type.should eq('asc')
          existent_filter.conditions.size.should eq(2)
          existent_filter.conditions.first.key.should eq(:first_name)
          existent_filter.conditions.first.operator.should eq(:is)
        end
      end

      context "and add some params" do
        let(:new_params){ {"page" => 2, "wf_c2"=>:last_name, "wf_o2"=>:is, "wf_v2_0"=>"Foley",} }
        let(:merged_filter){ existent_filter.merge_params(new_params) }

        it "adds the new params" do
          merged_filter.page.should eq(2)
          merged_filter.conditions.size.should eq(3)
          merged_filter.conditions.last.key.should eq(:last_name)
          merged_filter.conditions.last.operator.should eq(:is)
        end

        it "preserve original params" do
          merged_filter.order_type.should eq('asc')
          merged_filter.order.should eq('first_name')
          merged_filter.conditions.first.key.should eq(:first_name)
          merged_filter.conditions.first.operator.should eq(:is)
        end
      end
    end

  end

end