text:
// This is the implementation of a very simple dbx import module which
// handles requests from the VM which come in over a socket. The
// higher-level Java wrapper for dbx starts the debugger, attaches to
// the process, imports this command, and runs it. After that, the SA
// writes commands to this agent via its own private communications
// channel. The intent is to move away from the text-based front-end
// completely in the near future (no more calling "debug" by printing
// text to dbx's stdin).

#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <stropts.h>
#include <netinet/in.h>
#include <netinet/tcp.h>

#include <proc_service.h>
#include <sys/procfs_isa.h>
#include <rtld_db.h>
#include "proc_service_2.h"
#include "svc_agent_dbx.hpp"

static ServiceabilityAgentDbxModule* module = NULL;
#define NEEDS_CLEANUP

// Useful for debugging
#define VERBOSE_DEBUGGING

#ifdef VERBOSE_DEBUGGING
# define debug_only(x) x
#else
# define debug_only(x)
#endif

// For profiling
//#define PROFILING

#ifdef PROFILING
#define PROFILE_COUNT 200
static Timer scanTimer;
static Timer workTimer;
static Timer writeTimer;
static int numRequests = 0;
#endif /* PROFILING */

const char* ServiceabilityAgentDbxModule::CMD_ADDRESS_SIZE   = "address_size";
const char* ServiceabilityAgentDbxModule::CMD_PEEK_FAIL_FAST = "peek_fail_fast";
const char* ServiceabilityAgentDbxModule::CMD_PEEK           = "peek";
const char* ServiceabilityAgentDbxModule::CMD_POKE           = "poke";
const char* ServiceabilityAgentDbxModule::CMD_MAPPED         = "mapped";
const char* ServiceabilityAgentDbxModule::CMD_LOOKUP         = "lookup";
const char* ServiceabilityAgentDbxModule::CMD_THR_GREGS      = "thr_gregs";
const char* ServiceabilityAgentDbxModule::CMD_EXIT           = "exit";

// The initialization routines must not have C++ name mangling
extern "C" {

/** This is the initialization routine called by dbx upon importing of
    this module. Returns 0 upon successful initialization, -1 upon
    failure. */
int shell_imp_init(int major, int minor,
                   shell_imp_interp_t interp, int argc, char *argv[])
{
  // Ensure shell interpreter data structure is laid out the way we
  // expect
  if (major != SHELL_IMP_MAJOR) {
    debug_only(fprintf(stderr, "Serviceability agent: unexpected value for SHELL_IMP_MAJOR (got %d, expected %d)\n", major, SHELL_IMP_MAJOR);)
    return -1;
  }
  if (minor < SHELL_IMP_MINOR) {
    debug_only(fprintf(stderr, "Serviceability agent: unexpected value for SHELL_IMP_MINOR (got %d, expected >= %d)\n", minor, SHELL_IMP_MINOR);)
    return -1;
  }

  if (module != NULL) {
    debug_only(fprintf(stderr, "Serviceability agent: module appears to already be initialized (should not happen)\n");)
    // Already initialized. Should not happen.
    return -1;
  }

  module = new ServiceabilityAgentDbxModule(major, minor, interp, argc, argv);
  if (!module->install()) {
    debug_only(fprintf(stderr, "Serviceability agent: error installing import module\n");)
    delete module;
    module = NULL;
    return -1;
  }

  // Installation was successful. Next step will be for the user to
  // enter the appropriate command on the command line, which will
  // make the SA's dbx module wait for commands to come in over the
  // socket.
  return 0;
}

/** This is the routine called by dbx upon unloading of this module.
    Returns 0 upon success, -1 upon failure. */
int
shell_imp_fini(shell_imp_interp_t)
{
  if (module == NULL) {
    return -1;
  }

  bool res = module->uninstall();
  delete module;
  module = NULL;
  if (!res) {
    return -1;
  }
  return 0;
}

} // extern "C"

/** This is the routine which is called by the dbx shell when the user
    requests the serviceability agent module to run. This delegates to
    ServiceabilityAgentDbxModule::run. This routine's signature must
    match that of shell_imp_fun_t. */
extern "C" {
static int
svc_agent_run(shell_imp_interp_t, int, char **, void *) {
  if (module == NULL) {
    return -1;
  }

  module->run();
  return 0;
}
}

/*
 * Implementation of ServiceabilityAgentDbxModule class
 */

// NOTE: we need to forward declare the special "ps_get_prochandle2"
// function which allows examination of core files as well. It isn't
// currently in proc_service_2.h. Note also that it has name mangling
// because it isn't declared extern "C".
//const struct ps_prochandle *ps_get_prochandle2(int cores_too);

ServiceabilityAgentDbxModule::ServiceabilityAgentDbxModule(int, int, shell_imp_interp_t interp,
                                                           int argc, char *argv[])
  :myComm(32768, 131072)
{
  _interp = interp;
  _argc = argc;
  _argv = argv;
  _tdb_agent = NULL;
  peek_fail_fast = false;
  libThreadName = NULL;
}

ServiceabilityAgentDbxModule::~ServiceabilityAgentDbxModule() {
  if (_command != NULL) {
    uninstall();
  }
}

char*
readCStringFromProcess(psaddr_t addr) {
  char c;
  int num = 0;
  ps_prochandle* cur_proc = (ps_prochandle*) ps_get_prochandle2(1);

  // Search for null terminator
  do {
    if (ps_pread(cur_proc, addr + num, &c, 1) != PS_OK) {
      return NULL;
    }
    ++num;
  } while (c != 0);

  // Allocate string
  char* res = new char[num];
  if (ps_pread(cur_proc, addr, res, num) != PS_OK) {
    delete[] res;
    return NULL;
  }
  return res;
}

int
findLibThreadCB(const rd_loadobj_t* lo, void* data) {
  ServiceabilityAgentDbxModule* module = (ServiceabilityAgentDbxModule*) data;
  char* name = readCStringFromProcess(lo->rl_nameaddr);
  if (strstr(name, "libthread.so") != NULL) {
    module->libThreadName = name;
    return 0;
  } else {
    delete[] name;
    return 1;
  }
}

bool
ServiceabilityAgentDbxModule::install() {
  // NOTE interdependency between here and Java side wrapper
  // FIXME: casts of string literal to char * to match prototype
  _command = shell_imp_define_command((char *) "svc_agent_run",
                                      &svc_agent_run,
                                      0,
                                      NULL,
                                      (char *) "Run the serviceability agent's dbx module.\n"
                                      "This routine causes the module to listen on a socket for requests.\n"
                                      "It does not return until the Java-side code tells it to exit, at\n"
                                      "which point control is returned to the dbx shell.");
  if (_command == NULL) {
    debug_only(fprintf(stderr, "Serviceability agent: Failed to install svc_agent_run command\n"));
    return false;
  }

  // This is fairly painful. Since dbx doesn't currently load
  // libthread_db with RTLD_GLOBAL, we can't just use RTLD_DEFAULT for
  // the argument to dlsym. Instead, we have to use rtld_db to search
  // through the loaded objects in the target process for libthread.so and

  // Try rtld_db
  if (rd_init(RD_VERSION) != RD_OK) {
    debug_only(fprintf(stderr, "Serviceability agent: Unable to init rtld_db\n"));
    return false;
  }

  rd_agent_t* rda = rd_new((struct ps_prochandle*) ps_get_prochandle2(1));
  if (rda == NULL) {
    debug_only(fprintf(stderr, "Serviceability agent: Unable to allocate rtld_db agent\n"));
    return false;
  }

  if (rd_loadobj_iter(rda, (rl_iter_f*) findLibThreadCB, this) != RD_OK) {
    debug_only(fprintf(stderr, "Serviceability agent: Loadobject iteration failed\n"));
    return false;
  }

  if (libThreadName == NULL) {
    debug_only(fprintf(stderr, "Serviceability agent: Failed to find pathname to libthread.so in target process\n"));
    return false;
  }

  // Find and open libthread_db.so
  char* slash = strrchr(libThreadName, '/');
  if (slash == NULL) {
    debug_only(fprintf(stderr, "Serviceability agent: can't parse path to libthread.so \"%s\"\n"));
    return false;
  }

  int slashPos = slash - libThreadName;
  char* buf = new char[slashPos + strlen("libthread_db.so") + 20]; // slop
  if (buf == NULL) {
    debug_only(fprintf(stderr, "Serviceability agent: error allocating libthread_db.so pathname\n"));
    return false;
  }
  strncpy(buf, libThreadName, slashPos + 1);

  // Check dbx's data model; use sparcv9/ subdirectory if 64-bit and
  // if target process is 32-bit
  if ((sizeof(void*) == 8) &&
      (strstr(libThreadName, "sparcv9") == NULL)) {
    strcpy(buf + slashPos + 1, "sparcv9/");
    slashPos += strlen("sparcv9/");
  }

  strcpy(buf + slashPos + 1, "libthread_db.so");

  libThreadDB = dlopen(buf, RTLD_LAZY);
  void* tmpDB = libThreadDB;
  if (libThreadDB == NULL) {
    debug_only(fprintf(stderr, "Serviceability agent: Warning: unable to find libthread_db.so at \"%s\"\n", buf));
    // Would like to handle this case as well. Maybe dbx has a better
    // idea of where libthread_db.so lies. If the problem with dbx
    // loading libthread_db without RTLD_GLOBAL specified ever gets
    // fixed, we could run this code all the time.
    tmpDB = RTLD_DEFAULT;
  }

  delete[] buf;

  // Initialize access to libthread_db
  td_init_fn          = (td_init_fn_t*)          dlsym(tmpDB, "td_init");
  td_ta_new_fn        = (td_ta_new_fn_t*)        dlsym(tmpDB, "td_ta_new");
  td_ta_delete_fn     = (td_ta_delete_fn_t*)     dlsym(tmpDB, "td_ta_delete");
  td_ta_map_id2thr_fn = (td_ta_map_id2thr_fn_t*) dlsym(tmpDB, "td_ta_map_id2thr");
  td_thr_getgregs_fn  = (td_thr_getgregs_fn_t*)  dlsym(tmpDB, "td_thr_getgregs");

  if (td_init_fn == NULL ||
      td_ta_new_fn == NULL ||
      td_ta_delete_fn == NULL ||
      td_ta_map_id2thr_fn == NULL ||
      td_thr_getgregs_fn == NULL) {
    debug_only(fprintf(stderr, "Serviceability agent: Failed to find one or more libthread_db symbols:\n"));
    debug_only(if (td_init_fn == NULL)          fprintf(stderr, "  td_init\n"));
    debug_only(if (td_ta_new_fn == NULL)        fprintf(stderr, "  td_ta_new\n"));
    debug_only(if (td_ta_delete_fn == NULL)     fprintf(stderr, "  td_ta_delete\n"));
    debug_only(if (td_ta_map_id2thr_fn == NULL) fprintf(stderr, "  td_ta_map_id2thr\n"));
    debug_only(if (td_thr_getgregs_fn == NULL)  fprintf(stderr, "  td_thr_getgregs\n"));
    return false;
  }

  if ((*td_init_fn)() != TD_OK) {
    debug_only(fprintf(stderr, "Serviceability agent: Failed to initialize libthread_db\n"));
    return false;
  }

  return true;
}

bool
ServiceabilityAgentDbxModule::uninstall() {
  if (_command == NULL) {
    return false;
  }

  if (libThreadDB != NULL) {
    dlclose(libThreadDB);
    libThreadDB = NULL;
  }

  int res = shell_imp_undefine_command(_command);

  if (res != 0) {
    return false;
  }

  return true;
}

bool
ServiceabilityAgentDbxModule::run() {
  // This is where most of the work gets done.
  // The command processor loop looks like the following:
  //  - create listening socket
  //  - accept a connection (only one for now)
  //  - while that connection is open and the "exit" command has not
  //    been received:
  //    - read command
  //    - if it's the exit command, cleanup and return
  //    - otherwise, process command and write result

  int listening_socket = socket(AF_INET, SOCK_STREAM, 0);
  if (listening_socket < 0) {
    return false;
  }

  // Set the SO_REUSEADDR property on the listening socket. This
  // prevents problems with calls to bind() to the same port failing
  // after this process exits. This seems to work on all platforms.
  int reuse_address = 1;
  if (setsockopt(listening_socket, SOL_SOCKET, SO_REUSEADDR,
                 (char *)&reuse_address, sizeof(reuse_address)) < 0) {
    close(listening_socket);
    return false;
  }

  sockaddr_in server_address;
  // Build the server address. We can bind the listening socket to the
  // INADDR_ANY internet address.
  memset((char*)&server_address, 0, sizeof(server_address));
  server_address.sin_family = AF_INET;
  server_address.sin_addr.s_addr = (unsigned long)htonl(INADDR_ANY);
  server_address.sin_port = htons((short)PORT);

  // Bind socket to port
  if (bind(listening_socket, (sockaddr*) &server_address,
           sizeof(server_address)) < 0) {
    close(listening_socket);
    return false;
  }

  // Arbitrarily chosen backlog of 5 (shouldn't matter since we expect
  // at most one connection)
  if (listen(listening_socket, 5) < 0) {
    close(listening_socket);
    return false;
  }

  // OK, now ready to wait for a data connection. This call to
  // accept() will block.
  struct sockaddr_in client_address;
  int address_len   = sizeof(client_address);
  int client_socket = accept(listening_socket, (sockaddr*) &client_address,
                         &address_len);
  // Close listening socket regardless of whether accept() succeeded.
  // (FIXME: this may be annoying, especially during debugging, but I
  // really feel that robustness and multiple connections should be
  // handled higher up, e.g., at the Java level -- multiple clients
  // could conceivably connect to the SA via RMI, and that would be a
  // more robust solution than implementing multiple connections at
  // this level)
  NEEDS_CLEANUP;

  // NOTE: the call to shutdown() usually fails, so don't panic if this happens
  shutdown(listening_socket, 2);

  if (close(listening_socket) < 0) {
    debug_only(fprintf(stderr, "Serviceability agent: Error closing listening socket\n"));
    return false;
  }

  if (client_socket < 0) {
    debug_only(fprintf(stderr, "Serviceability agent: Failed to open client socket\n"));
    // No more cleanup necessary
    return false;
  }

  // Attempt to disable TCP buffering on this socket. We send small
  // amounts of data back and forth and don't want buffering.
  int buffer_val = 1;
  if (setsockopt(client_socket, IPPROTO_IP, TCP_NODELAY, (char *) &buffer_val, sizeof(buffer_val)) < 0) {
    debug_only(fprintf(stderr, "Serviceability agent: Failed to set TCP_NODELAY option on client socket\n"));
    cleanup(client_socket);
    return false;
  }

  // OK, we have the data socket through which we will communicate
  // with the Java side. Wait for commands or until reading or writing
  // caused an error.

  bool should_continue = true;

  myComm.setSocket(client_socket);

#ifdef PROFILING
  scanTimer.reset();
  workTimer.reset();
  writeTimer.reset();
#endif

  // Allocate a new thread agent for libthread_db
  if ((*td_ta_new_fn)((ps_prochandle*) ps_get_prochandle2(1), &_tdb_agent) !=
      TD_OK) {
    debug_only(fprintf(stderr, "Serviceability agent: Failed to allocate thread agent\n"));
    cleanup(client_socket);
    return false;
  }

  do {
    // Decided to use text to communicate between these processes.
    // Probably will make debugging easier -- could telnet in if
    // necessary. Will make scanning harder, but probably doesn't
    // matter.

    // Why not just do what workshop does and parse dbx's console?
    // Probably could do that, but at least this way we are in control
    // of the text format on both ends.

    // FIXME: should have some way of synchronizing these commands
    // between the C and Java sources.

    NEEDS_CLEANUP;

    // Do a blocking read of a line from the socket.
    char *input_buffer = myComm.readLine();
    if (input_buffer == NULL) {
      debug_only(fprintf(stderr, "Serviceability agent: error during read: errno = %d\n", errno));
      debug_only(perror("Serviceability agent"));
      // Error occurred during read.
      // FIXME: should guard against SIGPIPE
      cleanup(client_socket);
      return false;
    }

    // OK, now ready to scan. See README-commands.txt for syntax
    // descriptions.

    bool res = false;
    if (!strncmp(input_buffer, CMD_ADDRESS_SIZE, strlen(CMD_ADDRESS_SIZE))) {
      res = handleAddressSize(input_buffer + strlen(CMD_ADDRESS_SIZE));
    } else if (!strncmp(input_buffer, CMD_PEEK_FAIL_FAST, strlen(CMD_PEEK_FAIL_FAST))) {
      res = handlePeekFailFast(input_buffer + strlen(CMD_PEEK_FAIL_FAST));
    } else if (!strncmp(input_buffer, CMD_PEEK, strlen(CMD_PEEK))) {
      res = handlePeek(input_buffer + strlen(CMD_PEEK));
    } else if (!strncmp(input_buffer, CMD_POKE, strlen(CMD_POKE))) {
      res = handlePoke(input_buffer + strlen(CMD_POKE));
    } else if (!strncmp(input_buffer, CMD_MAPPED, strlen(CMD_MAPPED))) {
      res = handleMapped(input_buffer + strlen(CMD_MAPPED));
    } else if (!strncmp(input_buffer, CMD_LOOKUP, strlen(CMD_LOOKUP))) {
      res = handleLookup(input_buffer + strlen(CMD_LOOKUP));
    } else if (!strncmp(input_buffer, CMD_THR_GREGS, strlen(CMD_THR_GREGS))) {
      res = handleThrGRegs(input_buffer + strlen(CMD_THR_GREGS));
    } else if (!strncmp(input_buffer, CMD_EXIT, strlen(CMD_EXIT))) {
      should_continue = false;
    }

    if (should_continue) {
      if (!res) {
        cleanup(client_socket);
        return false;
      }
    }

#ifdef PROFILING
    if (++numRequests == PROFILE_COUNT) {
      fprintf(stderr, "%d requests: %d ms scanning, %d ms work, %d ms writing\n",
              PROFILE_COUNT, scanTimer.total(), workTimer.total(), writeTimer.total());
      fflush(stderr);
      scanTimer.reset();
      workTimer.reset();
      writeTimer.reset();
      numRequests = 0;
    }
#endif

  } while (should_continue);

  // Successful exit
  cleanup(client_socket);
  return true;
}

void
ServiceabilityAgentDbxModule::cleanup(int client_socket) {
  shutdown(client_socket, 2);
  close(client_socket);
  if (_tdb_agent != NULL) {
    (*td_ta_delete_fn)(_tdb_agent);
  }
}

bool
ServiceabilityAgentDbxModule::handleAddressSize(char* data) {
  int data_model;
  ps_err_e result = ps_pdmodel((ps_prochandle*) ps_get_prochandle2(1),
                               &data_model);
  if (result != PS_OK) {
    myComm.writeString("0");
    myComm.flush();
    return false;
  }

  int val;
  switch (data_model) {
  case PR_MODEL_ILP32:
    val = 32;
    break;
  case PR_MODEL_LP64:
    val = 64;
    break;
  default:
    val = 0;
    break;
  }

  if (!myComm.writeInt(val)) {
    return false;
  }
  if (!myComm.writeEOL()) {
    return false;
  }
  return myComm.flush();
}

bool
ServiceabilityAgentDbxModule::handlePeekFailFast(char* data) {
  unsigned int val;
  if (!scanUnsignedInt(&data, &val)) {
    return false;
  }
  peek_fail_fast = (val ? true : false);
  return true;
}

bool
ServiceabilityAgentDbxModule::handlePeek(char* data) {
  // Scan hex address, return false if failed
  psaddr_t addr;
#ifdef PROFILING
  scanTimer.start();
#endif /* PROFILING */
  if (!scanAddress(&data, &addr)) {
    return false;
  }
  unsigned int num;
  if (!scanUnsignedInt(&data, &num)) {
    return false;
  }
  if (num == 0) {
#ifdef PROFILING
    writeTimer.start();
#endif /* PROFILING */
    myComm.writeBinChar('B');
    myComm.writeBinChar(1);
    myComm.writeBinUnsignedInt(0);
    myComm.writeBinChar(0);
#ifdef PROFILING
    writeTimer.stop();
#endif /* PROFILING */
    return true;
  }
#ifdef PROFILING
  scanTimer.stop();
  workTimer.start();
#endif /* PROFILING */
  char* buf = new char[num];
  ps_prochandle* cur_proc = (ps_prochandle*) ps_get_prochandle2(1);
  ps_err_e result = ps_pread(cur_proc, addr, buf, num);
  if (result == PS_OK) {
    // Fast case; entire read succeeded.
#ifdef PROFILING
    workTimer.stop();
    writeTimer.start();
#endif /* PROFILING */
    myComm.writeBinChar('B');
    myComm.writeBinChar(1);
    myComm.writeBinUnsignedInt(num);
    myComm.writeBinChar(1);
    myComm.writeBinBuf(buf, num);
#ifdef PROFILING
    writeTimer.stop();
#endif /* PROFILING */
  } else {
#ifdef PROFILING
    workTimer.stop();
#endif /* PROFILING */

    if (peek_fail_fast) {
#ifdef PROFILING
    writeTimer.start();
#endif /* PROFILING */
      // Fail fast
      myComm.writeBinChar('B');
      myComm.writeBinChar(1);
      myComm.writeBinUnsignedInt(num);
      myComm.writeBinChar(0);
#ifdef PROFILING
    writeTimer.stop();
#endif /* PROFILING */
    } else {
      // Slow case: try to read one byte at a time
      // FIXME: need better way of handling this, a la VirtualQuery

      unsigned int  strideLen      = 0;
      int           bufIdx         = 0;
      bool          lastByteMapped = (ps_pread(cur_proc, addr, buf, 1) == PS_OK ? true : false);

#ifdef PROFILING
      writeTimer.start();
#endif /* PROFILING */
      myComm.writeBinChar('B');
      myComm.writeBinChar(1);
#ifdef PROFILING
      writeTimer.stop();
#endif /* PROFILING */

      for (int i = 0; i < num; ++i, ++addr) {
#ifdef PROFILING
        workTimer.start();
#endif /* PROFILING */
        result = ps_pread(cur_proc, addr, &buf[bufIdx], 1);
#ifdef PROFILING
        workTimer.stop();
#endif /* PROFILING */
        bool tmpMapped = (result == PS_OK ? true : false);
#ifdef PROFILING
        writeTimer.start();
#endif /* PROFILING */
        if (tmpMapped != lastByteMapped) {
          // State change. Write the length of the last stride.
          myComm.writeBinUnsignedInt(strideLen);
          if (lastByteMapped) {
            // Stop gathering data. Write the data of the last stride.
            myComm.writeBinChar(1);
            myComm.writeBinBuf(buf, strideLen);
            bufIdx = 0;
          } else {
            // Start gathering data to write.
            myComm.writeBinChar(0);
          }
          strideLen = 0;
          lastByteMapped = tmpMapped;
        }
#ifdef PROFILING
        writeTimer.stop();
#endif /* PROFILING */
        if (lastByteMapped) {
          ++bufIdx;
        }
        ++strideLen;
      }

      // Write last stride (must be at least one byte long by definition)
#ifdef PROFILING
      writeTimer.start();
#endif /* PROFILING */
      myComm.writeBinUnsignedInt(strideLen);
      if (lastByteMapped) {
        myComm.writeBinChar(1);
        myComm.writeBinBuf(buf, strideLen);
      } else {
        myComm.writeBinChar(0);
      }
#ifdef PROFILING
      writeTimer.stop();
#endif /* PROFILING */
    }
  }
  delete[] buf;
  myComm.flush();
  return true;
}

bool
ServiceabilityAgentDbxModule::handlePoke(char* data) {
  // FIXME: not yet implemented
  NEEDS_CLEANUP;
  bool res = myComm.writeBoolAsInt(false);
  myComm.flush();
  return res;
}

bool
ServiceabilityAgentDbxModule::handleMapped(char* data) {
  // Scan address
  psaddr_t addr;
  if (!scanAddress(&data, &addr)) {
    return false;
  }
  unsigned int num;
  if (!scanUnsignedInt(&data, &num)) {
    return false;
  }
  unsigned char val;
  ps_prochandle* cur_proc = (ps_prochandle*) ps_get_prochandle2(1);
  char* buf = new char[num];
  if (ps_pread(cur_proc, addr, buf, num) == PS_OK) {
    myComm.writeBoolAsInt(true);
  } else {
    myComm.writeBoolAsInt(false);
  }
  delete[] buf;
  myComm.writeEOL();
  myComm.flush();
  return true;
}

extern "C"
int loadobj_iterator(const rd_loadobj_t* loadobj, void *) {
  if (loadobj != NULL) {
    fprintf(stderr, "loadobj_iterator: visited loadobj \"%p\"\n", (void*) loadobj->rl_nameaddr);
    return 1;
  }

  fprintf(stderr, "loadobj_iterator: NULL loadobj\n");
  return 0;
}

bool
ServiceabilityAgentDbxModule::handleLookup(char* data) {
  // Debugging: iterate over loadobjs
  /*
  rd_agent_t* rld_agent = rd_new((ps_prochandle*) ps_get_prochandle2(1));
  rd_loadobj_iter(rld_agent, &loadobj_iterator, NULL);
  rd_delete(rld_agent);
  */

#ifdef PROFILING
  scanTimer.start();
#endif /* PROFILING */

  char* object_name = scanSymbol(&data);
  if (object_name == NULL) {
    return false;
  }
  char* symbol_name = scanSymbol(&data);
  if (symbol_name == NULL) {
    delete[] object_name;
    return false;
  }

#ifdef PROFILING
  scanTimer.stop();
  workTimer.start();
#endif /* PROFILING */

  ps_sym_t sym;
  // FIXME: check return values from write routines
  ps_prochandle* process = (ps_prochandle*) ps_get_prochandle2(1);
  ps_err_e lookup_res = ps_pglobal_sym(process,
                                       object_name, symbol_name, &sym);
#ifdef PROFILING
  workTimer.stop();
  writeTimer.start();
#endif /* PROFILING */

  delete[] object_name;
  delete[] symbol_name;
  if (lookup_res != PS_OK) {
    // This is too noisy
    //    debug_only(fprintf(stderr, "ServiceabilityAgentDbxModule::handleLookup: error %d\n", lookup_res));
    myComm.writeString("0x0");
  } else {
    myComm.writeAddress((void *)sym.st_value);
  }
  myComm.writeEOL();
  myComm.flush();

#ifdef PROFILING
  writeTimer.stop();
#endif /* PROFILING */

  return true;
}

bool
ServiceabilityAgentDbxModule::handleThrGRegs(char* data) {
#ifdef PROFILING
  scanTimer.start();
#endif /* PROFILING */

  unsigned int num;
  // Get the thread ID
  if (!scanUnsignedInt(&data, &num)) {
    return false;
  }

#ifdef PROFILING
  scanTimer.stop();
  workTimer.start();
#endif /* PROFILING */

  // Map tid to thread handle
  td_thrhandle_t thread_handle;
  if ((*td_ta_map_id2thr_fn)(_tdb_agent, num, &thread_handle) != TD_OK) {
    //    fprintf(stderr, "Error mapping thread ID %d to thread handle\n", num);
    return false;
  }

  // Fetch register set
  prgregset_t reg_set;
  memset(reg_set, 0, sizeof(reg_set));
  td_err_e result = (*td_thr_getgregs_fn)(&thread_handle, reg_set);
  if ((result != TD_OK) && (result != TD_PARTIALREG)) {
    //    fprintf(stderr, "Error fetching registers for thread handle %d: error = %d\n", num, result);
    return false;
  }

#ifdef PROFILING
  workTimer.stop();
  writeTimer.start();
#endif /* PROFILING */

#if (defined(__sparc) || defined(__i386))
  myComm.writeInt(NPRGREG);
  myComm.writeSpace();
  for (int i = 0; i < NPRGREG; i++) {
    myComm.writeAddress((void *)reg_set[i]);
    if (i == NPRGREG - 1) {
      myComm.writeEOL();
    } else {
      myComm.writeSpace();
    }
  }
#else
#error  Please port ServiceabilityAgentDbxModule::handleThrGRegs to your current platform
#endif

  myComm.flush();

#ifdef PROFILING
  writeTimer.stop();
#endif /* PROFILING */

  return true;
}

//
// Input routines
//

bool
ServiceabilityAgentDbxModule::scanAddress(char** data, psaddr_t* addr) {
  *addr = 0;

  // Skip whitespace
  while ((**data != 0) && (isspace(**data))) {
    ++*data;
  }

  if (**data == 0) {
    return false;
  }

  if (strncmp(*data, "0x", 2) != 0) {
    return false;
  }

  *data += 2;

  while ((**data != 0) && (!isspace(**data))) {
    int val;
    bool res = charToNibble(**data, &val);
    if (!res) {
      return false;
    }
    *addr <<= 4;
    *addr |= val;
    ++*data;
  }

  return true;
}

bool
ServiceabilityAgentDbxModule::scanUnsignedInt(char** data, unsigned int* num) {
  *num = 0;

  // Skip whitespace
  while ((**data != 0) && (isspace(**data))) {
    ++*data;
  }

  if (**data == 0) {
    return false;
  }

  while ((**data != 0) && (!isspace(**data))) {
    char cur = **data;
    if ((cur < '0') || (cur > '9')) {
      return false;
    }
    *num *= 10;
    *num += cur - '0';
    ++*data;
  }

  return true;
}

char*
ServiceabilityAgentDbxModule::scanSymbol(char** data) {
  // Skip whitespace
  while ((**data != 0) && (isspace(**data))) {
    ++*data;
  }

  if (**data == 0) {
    return NULL;
  }

  // First count length
  int len = 1; // Null terminator
  char* tmpData = *data;
  while ((*tmpData != 0) && (!isspace(*tmpData))) {
    ++tmpData;
    ++len;
  }
  char* buf = new char[len];
  strncpy(buf, *data, len - 1);
  buf[len - 1] = 0;
  *data += len - 1;
  return buf;
}

bool
ServiceabilityAgentDbxModule::charToNibble(char ascii, int* value) {
  if (ascii >= '0' && ascii <= '9') {
    *value = ascii - '0';
    return true;
  } else if (ascii >= 'A' && ascii <= 'F') {
    *value = 10 + ascii - 'A';
    return true;
  } else if (ascii >= 'a' && ascii <= 'f') {
    *value = 10 + ascii - 'a';
    return true;
  }

  return false;
}

char*
ServiceabilityAgentDbxModule::readCStringFromProcess(psaddr_t addr) {
  char c;
  int num = 0;
  ps_prochandle* cur_proc = (ps_prochandle*) ps_get_prochandle2(1);

  // Search for null terminator
  do {
    if (ps_pread(cur_proc, addr + num, &c, 1) != PS_OK) {
      return NULL;
    }
    ++num;
  } while (c != 0);

  // Allocate string
  char* res = new char[num];
  if (ps_pread(cur_proc, addr, res, num) != PS_OK) {
    delete[] res;
    return NULL;
  }
  return res;
}

//--------------------------------------------------------------------------------
// Class Timer
//

Timer::Timer() {
  reset();
}

Timer::~Timer() {
}

void
Timer::start() {
  gettimeofday(&startTime, NULL);
}

void
Timer::stop() {
  struct timeval endTime;
  gettimeofday(&endTime, NULL);
  totalMicroseconds += timevalDiff(&startTime, &endTime);
  ++counter;
}

long
Timer::total() {
  return (totalMicroseconds / 1000);
}

long
Timer::average() {
  return (long) ((double) total() / (double) counter);
}

void
Timer::reset() {
  totalMicroseconds = 0;
  counter = 0;
}

long long
Timer::timevalDiff(struct timeval* start, struct timeval* end) {
  long long secs = end->tv_sec - start->tv_sec;
  secs *= 1000000;
  long long usecs = end->tv_usec - start->tv_usec;
  return (secs + usecs);
}


text:
using System;

namespace Spect.Net.VsPackage.Vsx
{
    /// <summary>
    /// This class represents a tool window
    /// </summary>
    public class ToolWindowAttribute: Attribute
    {
        /// <summary>
        /// The type of the tool window
        /// </summary>
        public Type Type { get; }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.Attribute" /> class.
        /// </summary>
        public ToolWindowAttribute(Type type)
        {
            Type = type;
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
layout: publi
title: Changes in gene expression control by DNA methylation in cancer
categories: publi-journal
journal: Submitted to BMC Genomics
date: May, 2015
keywords: Epigenetic regulation, cancer, DNA methylation, Gene expression.
link:
authors: Matahi Moarii, Valentina Boeva, Jean-Philippe Vert, Fabien Reyal
---

Available soon.


text:
using System;

namespace MultithreadedSingleton
{
	public class Singleton
	{
		private static volatile Singleton _instance;
		private static readonly object SyncRoot = new Object();

		private Singleton()
		{
			Console.WriteLine("Creating Singleton");
		}

		public static Singleton Instance
		{
			get
			{
				if (_instance == null)
				{
					lock (SyncRoot)
					{
						if (_instance == null)
						{
							_instance = new Singleton();
						}
					}
				}
				return _instance;
			}
		}
	}
}


text:
package net.metanotion.toml;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Set;

import org.joda.time.ReadableInstant;

import net.metanotion.util.DictionaryIndex;

final class Properties implements DictionaryIndex<String,Value>, Value<DictionaryIndex> {
	private final Map<String,Value> data;

	public Properties(final Map<String,Value> data) {
		this.data = data;
	}

	@Override public Value get(final String key) { return data.get(key); }
	@Override public Set<String> keySet() { return data.keySet(); }

	@Override public Class<DictionaryIndex> type() { return DictionaryIndex.class; }
	@Override public DictionaryIndex<String,Value> child() { return this; }
	@Override public Iterable<Value> array() { throw new WrongTypeException(); }
	@Override public Long integer() { throw new WrongTypeException(); }
	@Override public BigDecimal decimal() { throw new WrongTypeException(); }
	@Override public String string() { throw new WrongTypeException(); }
	@Override public boolean bool() { throw new WrongTypeException(); }
	@Override public ReadableInstant instant() { throw new WrongTypeException(); }
}


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<TITLE>
Ft (SuanShu 1.3.1 API Documentation)
</TITLE>

<META NAME="date" CONTENT="2011-04-26">

<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../../../../stylesheet.css" TITLE="Style">

      Overview
      Package
    Class
      Tree
      Index
      Help

                SuanShu, a Java numerical and statistical library

 PREV CLASS
 NEXT CLASS

  FRAMES
 NO FRAMES

  All Classes

  SUMMARY: NESTED | FIELD | CONSTR | METHOD

DETAIL: FIELD | CONSTR | METHOD

com.numericalmethod.suanshu.stats.stochasticprocess.univariate.sde

Class Ft

java.lang.Object
  com.numericalmethod.suanshu.stats.stochasticprocess.univariate.sde.Ft

All Implemented Interfaces:
DeepCopyable

Direct Known Subclasses:
FtWt

public class Ft
extends java.lang.Object
implements DeepCopyable

This represents the concept 'Filtration', the information available at time t.

 The information can include, for example,

time

value of the stochastic process

values of the driving Brownian motion(s)

See Also:
"Fima C. Klebaner. Introduction to Stochastic Calculus with Applications. 2nd ed. pp.23. Imperial College Press. 2006."

Constructor Summary

Ft()

          Construct an empty filtration (no information).

Ft(Ft that)

          Copy constructor.

Method Summary

 Ft
deepCopy()

          The implementation can return an instance created from this by the copy
 constructor of the class, or just this if the instance itself is
 immutable.

 double
dt()

          Get the current time differential.

 double
dWt()

          Get the increment of the driving Brownian motion during the time differential.

 void
setDt(double dt)

          Set the current time differential.

 void
setXt(double Xt)

          Set the current value of the stochastic process.

 void
setZt(double Zt)

          Set the value of the Gaussian distribution innovation.

 double
Xt()

          Get the current value of the stochastic process.

 double
Zt()

          Get the current value of the Gaussian distribution innovation.

Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail

Ft

public Ft()

Construct an empty filtration (no information).

Ft

public Ft(Ft that)

Copy constructor.

Parameters:
that - another <tt>Ft</tt>

Method Detail

deepCopy

public Ft deepCopy()

Description copied from interface: DeepCopyable

The implementation can return an instance created from this by the copy
 constructor of the class, or just this if the instance itself is
 immutable.

Specified by:
deepCopy in interface DeepCopyable

Returns:
an independent (deep) copy of the instance

setDt

public void setDt(double dt)

Set the current time differential.

Parameters:
dt - the time differential

dt

public double dt()

Get the current time differential.

Returns:
the time differential

setXt

public void setXt(double Xt)

Set the current value of the stochastic process.

Parameters:
Xt - the current value of the stochastic process

Xt

public double Xt()

Get the current value of the stochastic process.

Returns:
the current value of the stochastic process

setZt

public void setZt(double Zt)

Set the value of the Gaussian distribution innovation.

Parameters:
Zt - the Gaussian distribution innovation

Zt

public double Zt()

Get the current value of the Gaussian distribution innovation.

Returns:
the current Gaussian distribution innovation

dWt

public double dWt()

Get the increment of the driving Brownian motion during the time differential.

 This is the product of the Gaussian distribution innovation and the square root of the time differential.

Returns:
the increment of the driving Brownian motion during the time differential

      Overview
      Package
    Class
      Tree
      Index
      Help

                SuanShu, a Java numerical and statistical library

 PREV CLASS
 NEXT CLASS

  FRAMES
 NO FRAMES

  All Classes

  SUMMARY: NESTED | FIELD | CONSTR | METHOD

DETAIL: FIELD | CONSTR | METHOD

                Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.


text:
#region License

// Copyright (C) Tomáš Pažourek, 2014
// All rights reserved.
//
// Distributed under MIT license as a part of project Colourful.
// https://github.com/tompazourek/Colourful

#endregion License

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using Colourful.Implementation.Conversion;

namespace Colourful.Conversion
{
    public partial class ColourfulConverter
    {
        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        public LinearRGBColor ToLinearRGB(RGBColor color)
        {
            if (color == null) throw new ArgumentNullException("color");

            // conversion
            var converter = new RGBToLinearRGBConverter();
            LinearRGBColor result = converter.Convert(color);
            return result;
        }

        public LinearRGBColor ToLinearRGB(XYZColor color)
        {
            if (color == null) throw new ArgumentNullException("color");

            // adaptation
            XYZColor adapted = TargetRGBWorkingSpace.WhitePoint.Equals(WhitePoint) || !IsChromaticAdaptationPerformed
                ? color
                : ChromaticAdaptation.Transform(color, WhitePoint, TargetRGBWorkingSpace.WhitePoint);

            // conversion to linear RGB
            var xyzConverter = GetXYZToLinearRGBConverter(TargetRGBWorkingSpace);
            LinearRGBColor result = xyzConverter.Convert(adapted);
            return result;
        }

        public LinearRGBColor ToLinearRGB(xyYColor color)
        {
            if (color == null) throw new ArgumentNullException("color");

            XYZColor xyzColor = ToXYZ(color);
            LinearRGBColor result = ToLinearRGB(xyzColor);
            return result;
        }

        public LinearRGBColor ToLinearRGB(LabColor color)
        {
            if (color == null) throw new ArgumentNullException("color");

            XYZColor xyzColor = ToXYZ(color);
            LinearRGBColor result = ToLinearRGB(xyzColor);
            return result;
        }

        public LinearRGBColor ToLinearRGB(LChabColor color)
        {
            if (color == null) throw new ArgumentNullException("color");

            XYZColor xyzColor = ToXYZ(color);
            LinearRGBColor result = ToLinearRGB(xyzColor);
            return result;
        }

        public LinearRGBColor ToLinearRGB(HunterLabColor color)
        {
            if (color == null) throw new ArgumentNullException("color");

            XYZColor xyzColor = ToXYZ(color);
            LinearRGBColor result = ToLinearRGB(xyzColor);
            return result;
        }

        public LinearRGBColor ToLinearRGB(LuvColor color)
        {
            if (color == null) throw new ArgumentNullException("color");

            XYZColor xyzColor = ToXYZ(color);
            LinearRGBColor result = ToLinearRGB(xyzColor);
            return result;
        }

        public LinearRGBColor ToLinearRGB(LChuvColor color)
        {
            if (color == null) throw new ArgumentNullException("color");

            XYZColor xyzColor = ToXYZ(color);
            LinearRGBColor result = ToLinearRGB(xyzColor);
            return result;
        }

        public LinearRGBColor ToLinearRGB(LMSColor color)
        {
            if (color == null) throw new ArgumentNullException("color");

            XYZColor xyzColor = ToXYZ(color);
            LinearRGBColor result = ToLinearRGB(xyzColor);
            return result;
        }

#if (NET35)
#else
        public LinearRGBColor ToLinearRGB<T>(T color) where T : IColorVector
        {
            if (color == null) throw new ArgumentNullException("color");

            LinearRGBColor converted = color as LinearRGBColor;

            if (converted != null)
            {
                return converted;
            }
            else
            {
                dynamic source = color;

                return ToLinearRGB(source);
            }
        }
#endif
    }
}


text:
package org.decaf.science
import scala.util.control.NonFatal

object Experiment {
  def apply[Storage](storageStrategy: StorageStrategy[Storage]) = new ExperimentBuilder(storageStrategy)
}

class ExperimentBuilder[Storage](storage: StorageStrategy[Storage]) {
  def apply[Control, Exp](control: => Control, candidate: => Exp)(implicit
                                                                  controlSerializer: Serialization[Control, Storage],
                                                                  candidateSerializer: Serialization[Exp, Storage],
                                                                  throwableSerializer: Serialization.ThrowableSerialization[Storage],
                                                                  experimentStrategy: ExperimentStrategy[Exp] = ExperimentStrategy.default): Control =
    new Experiment(control, candidate, controlSerializer, candidateSerializer, throwableSerializer, storage, experimentStrategy).run()

  def apply[Exp](strategy: ExperimentStrategy[Exp]) = new ExperimentFromStorageAndStrategyBuilder(storage, strategy)
}

class ExperimentFromStorageAndStrategyBuilder[Storage, Exp](val storage: StorageStrategy[Storage], val experimentStrategy: ExperimentStrategy[Exp]) {
  def apply[Control](control: => Control, candidate: => Exp)(implicit
                                                             controlSerializer: Serialization[Control, Storage],
                                                             candidateSerializer: Serialization[Exp, Storage],
                                                             throwableSerializer: Serialization.ThrowableSerialization[Storage]): Control =
    new Experiment(control, candidate, controlSerializer, candidateSerializer, throwableSerializer, storage, experimentStrategy).run()
}

class Experiment[Control, Exp, Storage](
  control: => Control,
  candidate: => Exp,
  controlSerializer: Serialization[Control, Storage],
  candidateSerializer: Serialization[Exp, Storage],
  throwableSerializer: Serialization.ThrowableSerialization[Storage],
  storageStrategy: StorageStrategy[Storage],
  experimentStrategy: ExperimentStrategy[Exp],
  duringExperiment: Option[DuringExperiment[Control, Exp]] = Option.empty,
  afterExperiments: Seq[AfterExperiment] = Seq.empty
) {
  def withDuringExperiment(during: DuringExperiment[Control, Exp]): Experiment[Control, Exp, Storage] =
    new Experiment(control, candidate, controlSerializer, candidateSerializer, throwableSerializer, storageStrategy, experimentStrategy, Some(during), afterExperiments)

  def withAfterExperiment(after: AfterExperiment): Experiment[Control, Exp, Storage] =
    new Experiment(control, candidate, controlSerializer, candidateSerializer, throwableSerializer, storageStrategy, experimentStrategy, duringExperiment, afterExperiments :+ after)

  def run(): Control = {
    val during = duringExperiment getOrElse DuringExperiment.empty
    val evaluatedControl = during.duringControl(control)

    val controlSerialized = controlSerializer.serialize(evaluatedControl)
    afterExperiments.foreach(_.afterControl())

    try {
      val candidateSerialized =
        for {
          candidate <- experimentStrategy.experiment(during.duringExperiment(candidate))
        } yield {
          val result = candidateSerializer.serialize(candidate)
          afterExperiments.foreach(_.afterExperiment())
          result
        }

      storageStrategy.store(controlSerialized, candidateSerialized)
    } catch {
      case NonFatal(err) =>
        storageStrategy.failed(controlSerializer.serialize(evaluatedControl), err)(throwableSerializer)
        afterExperiments.foreach(_.afterExceptionInExperiment())
    }

    evaluatedControl
  }
}

object DuringExperiment {
  def empty[C, E] = new DuringExperiment[C, E] {
    def duringControl(c: => C): C = c
    def duringExperiment(e: => E): E = e
  }
}

trait DuringExperiment[C, E] {
  def duringControl(c: => C): C
  def duringExperiment(e: => E): E
}

trait AfterExperiment {
  def afterControl(): Unit
  def afterExperiment(): Unit
  def afterExceptionInExperiment(): Unit
}

object AfterExperiment {
  lazy val default: AfterExperiment = new AfterExperiment {
    def afterControl(): Unit = {}
    def afterExperiment(): Unit = {}
    def afterExceptionInExperiment(): Unit = {}
  }
}


text:
*,
*:before,
*:after {
	box-sizing: inherit;
}

body {
	font: 14px/20px ui-monospace, "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
	margin: 20px;
}

.users {
	margin-bottom: 20px;
}

.realtime .line {
	margin-bottom: 6px;
}

.realtime .time {
	display: inline-block;
	text-align: center;
	width: 75px;
	margin-right: 10px;
	font-variant-numeric: tabular-nums;
}


text:
#include "defs.h"

#include DEF_MPERS_TYPE(struct_mmsghdr)

#include "msghdr.h"
typedef struct mmsghdr struct_mmsghdr;

#include MPERS_DEFS

/*
 * On success, return the number of fetched bytes.
 * On error, return 0;
 *
 * This function cannot use umove_or_printaddr because
 * it is called by dumpio and therefore cannot print anything.
 */

MPERS_PRINTER_DECL(int, fetch_struct_mmsghdr,
		   struct tcb *tcp, const unsigned long addr, void *p)
{
	struct mmsghdr *p_native = p;
	struct_mmsghdr v_compat;

	if (sizeof(*p_native) == sizeof(v_compat))
		return umove(tcp, addr, p_native) ? 0 : sizeof(*p_native);

	if (umove(tcp, addr, &v_compat))
		return 0;

	p_native->msg_hdr.msg_name = (void *) (unsigned long)
	 v_compat.msg_hdr.msg_name;

	p_native->msg_hdr.msg_namelen =
	 v_compat.msg_hdr.msg_namelen;

	p_native->msg_hdr.msg_iov = (void *) (unsigned long)
	 v_compat.msg_hdr.msg_iov;

	p_native->msg_hdr.msg_iovlen =
	 v_compat.msg_hdr.msg_iovlen;

	p_native->msg_hdr.msg_control = (void *) (unsigned long)
	 v_compat.msg_hdr.msg_control;

	p_native->msg_hdr.msg_controllen =
	 v_compat.msg_hdr.msg_controllen;

	p_native->msg_hdr.msg_flags =
	 v_compat.msg_hdr.msg_flags;

	p_native->msg_len =
	 v_compat.msg_len;

	return sizeof(v_compat);
}

MPERS_PRINTER_DECL(unsigned int, sizeof_struct_mmsghdr, void)
{
	return sizeof(struct_mmsghdr);
}


text:
import numpy as np
from scipy import sparse

from sklearn.cross_validation import train_test_split
from scipy.sparse.csgraph import minimum_spanning_tree

from pystruct.learners import SubgradientSSVM
from pystruct.models import GraphCRF

def make_random_trees(n_samples=50, n_nodes=100, n_states=7, n_features=10):
    crf = GraphCRF(inference_method='max-product', n_states=n_states,
                   n_features=n_features)
    weights = np.random.randn(crf.size_joint_feature)
    X, y = [], []
    for i in range(n_samples):
        distances = np.random.randn(n_nodes, n_nodes)
        features = np.random.randn(n_nodes, n_features)
        tree = minimum_spanning_tree(sparse.csr_matrix(distances))
        edges = np.c_[tree.nonzero()]
        X.append((features, edges))
        y.append(crf.inference(X[-1], weights))

    return X, y, weights

X, y, weights = make_random_trees(n_nodes=1000)

X_train, X_test, y_train, y_test = train_test_split(X, y)

#tree_model = MultiLabelClf(edges=tree, inference_method=('ogm', {'alg': 'dyn'}))
tree_model = GraphCRF(inference_method='max-product')

tree_ssvm = SubgradientSSVM(tree_model, max_iter=4, C=1, verbose=10)

print("fitting tree model...")
tree_ssvm.fit(X_train, y_train)

print("Training loss tree model: %f" % tree_ssvm.score(X_train, y_train))
print("Test loss tree model: %f" % tree_ssvm.score(X_test, y_test))


text:
import json
import os
import flask
from flask import Flask, Response, render_template, send_from_directory, send_file

# Needed for the demo, but perhaps not in actual use
import itertools
import random
import time

# Server sent events
class ServerSentEvent:
    FIELDS = ('event', 'data', 'id')
    def __init__(self, data, event=None, event_id=None):
        self.data = data
        self.event = event
        self.id = event_id

    def encode(self):
        if not self.data:
            return ""
        ret = []
        for field in self.FIELDS:
            entry = getattr(self, field)
            if entry is not None:
                ret.extend(["%s: %s" % (field, line) for line in entry.split("\n")])
        return "\n".join(ret) + "\n\n"

# The Flask application
app = Flask(__name__)

@app.route('/static/<path:path>')
def send_js(path):
    return send_from_directory('static', path)

@app.route("/", methods=['GET'])
def get_index():
    return render_template('index.html')

@app.route("/stream")
def stream():
    def gen():
        count = itertools.count()
        while True:
            data = json.dumps({"series": {"mySeries": random.random()}, 'x': next(count)})
            ev = ServerSentEvent(data)
            print(ev.encode())
            yield ev.encode()
            time.sleep(0.05)

    return Response(gen(), mimetype="text/event-stream")

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8000, debug=True)


text:
module Spree
  class Payment
    class GatewayOptions
      def initialize(payment)
        @payment = payment
      end

      def email
        order.email
      end

      def customer
        order.email
      end

      def customer_id
        order.user_id
      end

      def ip
        order.last_ip_address
      end

      def order_id
        "#{order.number}-#{@payment.number}"
      end

      def shipping
        order.ship_total * exchange_multiplier
      end

      def tax
        order.additional_tax_total * exchange_multiplier
      end

      def subtotal
        order.item_total * exchange_multiplier
      end

      def discount
        order.promo_total * exchange_multiplier
      end

      def currency
        @payment.currency
      end

      def billing_address
        order.bill_address.try(:active_merchant_hash)
      end

      def shipping_address
        order.ship_address.try(:active_merchant_hash)
      end

      def hash_methods
        [
          :email,
          :customer,
          :customer_id,
          :ip,
          :order_id,
          :shipping,
          :tax,
          :subtotal,
          :discount,
          :currency,
          :billing_address,
          :shipping_address
        ]
      end

      def to_hash
        Hash[hash_methods.map do |method|
          [method, send(method)]
        end]
      end

      private

      def order
        @payment.order
      end

      def exchange_multiplier
        @payment.payment_method.try(:exchange_multiplier) || 1.0
      end
    end
  end
end


text:
UPSTREAM_REPO="kubernetes-incubator"
CLI="kompose"

usage() {
  echo "This will prepare $CLI for release!"
  echo ""
  echo "Requirements:"
  echo " git"
  echo " tar - you should already have this on your system"
  echo " hub"
  echo " github-release"
  echo " GITHUB_TOKEN in your env variable"
  echo " "
  echo "Not only that, but you must have permission for:"
  echo " Tagging releases within Github"
  echo ""
}

requirements() {
  if [ ! -f /usr/bin/git ] && [ ! -f /usr/local/bin/git ]; then
    echo "No git."
    return 1
  fi

  if [ ! -f $GOPATH/bin/github-release ]; then
    echo "No $GOPATH/bin/github-release. Please run 'go get -v github.com/aktau/github-release'"
    return 1
  fi

  if [ ! -f /usr/bin/hub ]; then
    echo "Hub needed in order to create the relevant PR's. Please install hub @ https://github.com/github/hub"
    return 1
  fi

  if [[ -z "$GITHUB_TOKEN" ]]; then
    echo "export GITHUB_TOKEN=yourtoken needed for using github-release"
  fi
}

# Clone and then change to user's upstream repo for pushing to master / opening PR's :)
clone() {
  git clone ssh://git@github.com/$UPSTREAM_REPO/$CLI.git
  if [ $? -eq 0 ]; then
        echo OK
  else
        echo FAIL
        exit
  fi
  cd $CLI
  git remote remove origin
  git remote add origin git@github.com:$ORIGIN_REPO/$CLI.git
  git checkout -b release-$1
  cd ..
}

replaceversion() {
  cd $CLI

  echo "1. Replaced version in version.go"
  find . -name 'version.go' -maxdepth 3 -type f -exec sed -i "s/$1/$2/g" {} \;

  echo "2. Replaced README.md versioning"
  sed -i "s/$1/$2/g" README.md

  cd ..
}

changelog() {
  cd $CLI
  echo "Getting commit changes. Writing to ../changes.txt"
  LOG=`git shortlog --email --no-merges --pretty=%s v${1}..`
  echo -e "\`\`\`\n$LOG\n\`\`\`" > ../changes.txt
  echo "Changelog has been written to changes.txt"
  echo "!!PLEASE REVIEW BEFORE CONTINUING!!"
  echo "Open changes.txt and add the release information"
  echo "to the beginning of the file before the git shortlog"
  cd ..
}

changelog_md() {
  echo "Generating CHANGELOG.md"
  CHANGES=$(cat changes.txt)
  cd $CLI
  DATE=$(date +"%m-%d-%Y")
  CHANGELOG=$(cat CHANGELOG.md)
  HEADER="## $CLI $1 ($DATE)"
  echo -e "$HEADER\n\n$CHANGES\n\n$CHANGELOG" >CHANGELOG.md
  echo "Changes have been written to CHANGELOG.md"
  cd ..
}

git_commit() {
  cd $CLI

  BRANCH=`git symbolic-ref --short HEAD`
  if [ -z "$BRANCH" ]; then
    echo "Unable to get branch name, is this even a git repo?"
    return 1
  fi
  echo "Branch: " $BRANCH

  git add .
  git commit -m "$1 Release"
  git push origin $BRANCH
  hub pull-request -b $UPSTREAM_REPO/$CLI:master -h $ORIGIN_REPO/$CLI:$BRANCH

  cd ..
  echo ""
  echo "PR opened against master to update version"
  echo "MERGE THIS BEFORE CONTINUING"
  echo ""
}

git_pull() {
  cd $CLI
  git pull
  cd ..
}

push() {
  CHANGES=$(cat changes.txt)
  # Release it!
  github-release release \
      --user $UPSTREAM_REPO \
      --repo $CLI \
      --tag v$1 \
      --name "v$1" \
      --description "$CHANGES"
  if [ $? -eq 0 ]; then
        echo RELEASE UPLOAD OK
  else
        echo RELEASE UPLOAD FAIL
        exit
  fi

  github-release upload \
      --user $UPSTREAM_REPO \
      --repo $CLI \
      --tag $1 \
      --name "$CLI-$1.tar.gz" \
      --file $CLI-$1.tar.gz
  if [ $? -eq 0 ]; then
        echo TARBALL UPLOAD OK
  else
        echo TARBALL UPLOAD FAIL
        exit
  fi

  echo "DONE"
  echo "DOUBLE CHECK IT:"
  echo "!!!"
  echo "https://github.com/$UPSTREAM_REPO/$CLI/releases/edit/$1"
  echo "!!!"
}

clean() {
  rm -rf $CLI $CLI-$1 $CLI-$1.tar.gz changes.txt
}

main() {
  local cmd=$1
  usage

  echo "What is your Github username? (location of your $CLI fork)"
  read ORIGIN_REPO
  echo "You entered: $ORIGIN_REPO"
  echo ""

  echo ""
  echo "First, please enter the version of the NEW release: "
  read VERSION
  echo "You entered: $VERSION"
  echo ""

  echo ""
  echo "Second, please enter the version of the LAST release: "
  read PREV_VERSION
  echo "You entered: $PREV_VERSION"
  echo ""

  clear

  echo "Now! It's time to go through each step of releasing $CLI!"
  echo "If one of these steps fails / does not work, simply re-run ./release.sh"
  echo "Re-enter the information at the beginning and continue on the failed step"
  echo ""

  PS3='Please enter your choice: '
  options=(
  "Git clone master"
  "Replace version number"
  "Generate changelog"
  "Generate changelog for release"
  "Create PR"
  "Git pull most recent changes"
  "Upload the tarball and push to Github release page"
  "Clean"
  "Quit")
  select opt in "${options[@]}"
  do
      echo ""
      case $opt in
          "Git clone master")
              clone $VERSION
              ;;
          "Replace version number")
              replaceversion $PREV_VERSION $VERSION
              ;;
          "Generate changelog")
              changelog $PREV_VERSION
              ;;
          "Generate changelog for release")
              changelog_md $VERSION
              ;;
          "Create PR")
              git_commit $VERSION
              ;;
          "Git pull most recent changes")
              git_pull
              ;;
          "Upload the tarball and push to Github release page")
              push $VERSION
              ;;
          "Clean")
              clean $VERSION
              ;;
          "Quit")
              clear
              break
              ;;
          *) echo invalid option;;
      esac
      echo ""
  done
}

main "$@"


text:
using System;
using Telerik.Data.Core.Layouts;
using Telerik.UI.Xaml.Controls.Data.ContainerGeneration;
using Telerik.UI.Xaml.Controls.Data.ListView;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;

namespace Telerik.UI.Xaml.Controls.Data
{
    public partial class RadListView
    {
       /// <summary>
        /// Identifies the <see cref="CurrentItem"/> dependency property.
        /// </summary>
        public static readonly DependencyProperty CurrentItemProperty =
            DependencyProperty.Register(nameof(CurrentItem), typeof(object), typeof(RadListView), new PropertyMetadata(null, OnCurrentItemChanged));

        /// <summary>
        /// Identifies the <see cref="EnsureCurrentItemIntoView"/> dependency property.
        /// </summary>
        public static readonly DependencyProperty EnsureCurrentItemIntoViewProperty =
            DependencyProperty.Register(nameof(EnsureCurrentItemIntoView), typeof(bool), typeof(RadListView), new PropertyMetadata(true, OnEnsureCurrentItemIntoViewChanged));

        /// <summary>
        /// Identifies the <see cref="IsSynchronizedWithCurrentItem"/> dependency property.
        /// </summary>
        public static readonly DependencyProperty IsSynchronizedWithCurrentItemProperty =
            DependencyProperty.Register(nameof(IsSynchronizedWithCurrentItem), typeof(bool), typeof(RadListView), new PropertyMetadata(false, OnIsSynchronizedWithCurrentItemChanged));

        internal readonly ListViewCurrencyService currencyService;
        internal int currentLogicalIndex;

        /// <summary>
        /// Occurs when the <see cref="CurrentItem"/> property has changed.
        /// </summary>
        public event EventHandler CurrentItemChanged
        {
            add
            {
                this.currencyService.CurrentChanged += value;
            }
            remove
            {
                this.currencyService.CurrentChanged -= value;
            }
        }

        /// <summary>
        /// Occurs when the <see cref="CurrentItem"/> property is about to change. The event may be canceled to prevent the actual change.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1009:DeclareEventHandlersCorrectly")]
        public event CurrentChangingEventHandler CurrentItemChanging
        {
            add
            {
                this.currencyService.CurrentChanging += value;
            }
            remove
            {
                this.currencyService.CurrentChanging -= value;
            }
        }

        /// <summary>
        /// Gets the object instance from the <see cref="ItemsSource"/> that is currently considered as Current.
        /// </summary>
        public object CurrentItem
        {
            get
            {
                return this.currencyService.CurrentItem;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the <see cref="CurrentItem"/> should always be automatically scrolled into view.
        /// </summary>
        public bool EnsureCurrentItemIntoView
        {
            get
            {
                return (bool)this.GetValue(EnsureCurrentItemIntoViewProperty);
            }
            set
            {
                this.SetValue(EnsureCurrentItemIntoViewProperty, value);
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the <see cref="SelectedItem"/> and <see cref="CurrentItem"/> are synchronized.
        /// </summary>
        public bool IsSynchronizedWithCurrentItem
        {
            get
            {
                return this.currencyService.isSynchronizedWithCurrent;
            }
            set
            {
                this.SetValue(IsSynchronizedWithCurrentItemProperty, value);
            }
        }

        ListViewCurrencyService ICurrencyService.CurrencyService
        {
            get { return this.currencyService; }
        }

        /// <summary>
        /// Tries to change the <see cref="CurrentItem"/> to the specified object. Returns value indicating if the operation is successful.
        /// </summary>
        public bool MoveCurrentTo(object item)
        {
            return this.currencyService.MoveCurrentTo(item);
        }

        internal void FocusCurrentContainer()
        {
            RadListViewItem container = this.GetCurrentContainer();
            if (container != null)
            {
                container.Focus(FocusState.Programmatic);
            }
        }

        private RadListViewItem GetCurrentContainer()
        {
            if (this.CurrentItem == null || this.currencyService.CurrentItemInfo == null || !this.currencyService.CurrentItemInfo.HasValue)
                return null;

            ItemInfo currentInfo = this.currencyService.CurrentItemInfo.Value;
            GeneratedItemModel generatedModel = this.Model.GetDisplayedElement(currentInfo.Slot, currentInfo.Id);

            RadListViewItem result = null;
            if (generatedModel != null)
            {
                result = generatedModel.Container as RadListViewItem;
            }
            return result;
        }

        private static void OnCurrentItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var listView = d as RadListView;
            if (!listView.IsInternalPropertyChange)
            {
                throw new InvalidOperationException("CurrentItem is read-only property.");
            }
        }

        private static void OnEnsureCurrentItemIntoViewChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var listView = d as RadListView;
            listView.currencyService.ensureCurrentIntoView = (bool)e.NewValue;
        }

        private static void OnIsSynchronizedWithCurrentItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var listView = d as RadListView;
            listView.currencyService.UpdateIsSynchronizedWithCurrent((bool)e.NewValue);
        }
    }
}


text:
//
// Copyright (C) 2016 OpenSim Ltd.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, see http://www.gnu.org/licenses/.
//

#include "OriginatorQoSAckPolicy.h"
#include <tuple>

namespace inet {
namespace ieee80211 {

Define_Module(OriginatorQoSAckPolicy);

void OriginatorQoSAckPolicy::initialize(int stage)
{
    ModeSetListener::initialize(stage);
    if (stage == INITSTAGE_LOCAL) {
        rateSelection = check_and_cast<IQoSRateSelection*>(getModuleByPath(par("rateSelectionModule")));
        maxBlockAckPolicyFrameLength = par("maxBlockAckPolicyFrameLength");
        blockAckReqTreshold = par("blockAckReqTreshold");
        blockAckTimeout = par("blockAckTimeout");
        ackTimeout = par("ackTimeout");
    }
}

bool OriginatorQoSAckPolicy::isAckNeeded(Ieee80211ManagementFrame* frame) const
{
    return !frame->getReceiverAddress().isMulticast();
}

std::map<MACAddress, std::vector<Ieee80211DataFrame*>> OriginatorQoSAckPolicy::getOutstandingFramesPerReceiver(InProgressFrames *inProgressFrames) const
{
    auto outstandingFrames = inProgressFrames->getOutstandingFrames();
    std::map<MACAddress, std::vector<Ieee80211DataFrame*>> outstandingFramesPerReceiver;
    for (auto frame : outstandingFrames)
        outstandingFramesPerReceiver[frame->getReceiverAddress()].push_back(frame);
    return outstandingFramesPerReceiver;
}

int OriginatorQoSAckPolicy::computeStartingSequenceNumber(const std::vector<Ieee80211DataFrame*>& outstandingFrames) const
{
    ASSERT(outstandingFrames.size() > 0);
    int startingSequenceNumber = outstandingFrames[0]->getSequenceNumber();
    for (int i = 1; i < (int)outstandingFrames.size(); i++) {
        int seqNum = outstandingFrames[i]->getSequenceNumber();
        if (seqNum < startingSequenceNumber)
            startingSequenceNumber = seqNum;
    }
    return startingSequenceNumber;
}

bool OriginatorQoSAckPolicy::isCompressedBlockAckReq(const std::vector<Ieee80211DataFrame*>& outstandingFrames, int startingSequenceNumber) const
{
    // The Compressed Bitmap subfield of the BA Control field or BAR Control field shall be set to 1 in all
    // BlockAck and BlockAckReq frames sent from one HT STA to another HT STA and shall be set to 0 otherwise.
    return false; // non-HT STA
//    for (auto frame : outstandingFrames)
//        if (frame->getSequenceNumber() >= startingSequenceNumber && frame->getFragmentNumber() > 0)
//            return false;
//    return true;
}

// FIXME
bool OriginatorQoSAckPolicy::isBlockAckReqNeeded(InProgressFrames* inProgressFrames, TxopProcedure* txopProcedure) const
{
    auto outstandingFramesPerReceiver = getOutstandingFramesPerReceiver(inProgressFrames);
    for (auto outstandingFrames : outstandingFramesPerReceiver) {
        if ((int)outstandingFrames.second.size() >= blockAckReqTreshold)
            return true;
    }
    return false;
}

// FIXME
std::tuple<MACAddress, SequenceNumber, Tid> OriginatorQoSAckPolicy::computeBlockAckReqParameters(InProgressFrames *inProgressFrames, TxopProcedure* txopProcedure) const
{
    auto outstandingFramesPerReceiver = getOutstandingFramesPerReceiver(inProgressFrames);
    for (auto outstandingFrames : outstandingFramesPerReceiver) {
        if ((int)outstandingFrames.second.size() >= blockAckReqTreshold) {
            auto largestOutstandingFrames = outstandingFramesPerReceiver.begin();
            for (auto it = outstandingFramesPerReceiver.begin(); it != outstandingFramesPerReceiver.end(); it++) {
                if (it->second.size() > largestOutstandingFrames->second.size())
                    largestOutstandingFrames = it;
            }
            MACAddress receiverAddress = largestOutstandingFrames->first;
            SequenceNumber startingSequenceNumber = computeStartingSequenceNumber(largestOutstandingFrames->second);
            Tid tid = largestOutstandingFrames->second.at(0)->getTid();
            return std::make_tuple(receiverAddress, startingSequenceNumber, tid);
        }
    }
    return std::make_tuple(MACAddress::UNSPECIFIED_ADDRESS, -1, -1);
}

AckPolicy OriginatorQoSAckPolicy::computeAckPolicy(Ieee80211DataFrame* frame, OriginatorBlockAckAgreement *agreement) const
{
    if (agreement == nullptr)
        return AckPolicy::NORMAL_ACK;
    if (agreement->getIsAddbaResponseReceived() && isBlockAckPolicyEligibleFrame(frame)) {
        if (checkAgreementPolicy(frame, agreement))
            return AckPolicy::BLOCK_ACK;
        else
            return AckPolicy::NORMAL_ACK;
    }
    else
        return AckPolicy::NORMAL_ACK;
}

bool OriginatorQoSAckPolicy::isBlockAckPolicyEligibleFrame(Ieee80211DataFrame* frame) const
{
    return frame->getType() == ST_DATA_WITH_QOS && frame->getByteLength() < maxBlockAckPolicyFrameLength;
}

bool OriginatorQoSAckPolicy::checkAgreementPolicy(Ieee80211DataFrame* frame, OriginatorBlockAckAgreement *agreement) const
{
    bool bufferFull = agreement->getBufferSize() == agreement->getNumSentBaPolicyFrames();
    bool aMsduOk = agreement->getIsAMsduSupported() || !frame->getAMsduPresent();
    // TODO: bool baPolicy = agreement->getIsDelayedBlockAckPolicySupported() || !frame->getAckPolicy();
    return !bufferFull && aMsduOk && (frame->getSequenceNumber() >= agreement->getStartingSequenceNumber()); // TODO: && baPolicy
}

//
// After transmitting an MPDU that requires an ACK frame as a response (see Annex G), the STA shall wait for an
// ACKTimeout interval, with a value of aSIFSTime + aSlotTime + aPHY-RX-START-Delay, starting at the
// PHY-TXEND.confirm primitive. If a PHY-RXSTART.indication primitive does not occur during the
// ACKTimeout interval, the STA concludes that the transmission of the MPDU has failed, and this STA shall
// invoke its backoff procedure upon expiration of the ACKTimeout interval.
//
simtime_t OriginatorQoSAckPolicy::getAckTimeout(Ieee80211DataOrMgmtFrame* dataOrMgmtFrame) const
{
    return ackTimeout == -1 ? modeSet->getSifsTime() + modeSet->getSlotTime() + rateSelection->computeResponseAckFrameMode(dataOrMgmtFrame)->getPhyRxStartDelay() : ackTimeout;
}

simtime_t OriginatorQoSAckPolicy::getBlockAckTimeout(Ieee80211BlockAckReq* blockAckReq) const
{
    return blockAckTimeout == -1 ? modeSet->getSifsTime() + modeSet->getSlotTime() + rateSelection->computeResponseBlockAckFrameMode(blockAckReq)->getPhyRxStartDelay() : blockAckTimeout;
}

} /* namespace ieee80211 */
} /* namespace inet */


text:
ACCEPTED

#### According to
Index Fungorum

#### Published in
Sydowia 61(1): 32 (2009)

#### Original name
Psilocybe deconicoides E. Horak, Guzmán & Desjardin

### Remarks
null


text:
title: "PuppetDB 4.3: API query tutorial"
layout: default
canonical: "/puppetdb/latest/api/query/tutorial.html"
---

[array]: ./v4/ast.html#array
[curl]: ./curl.html
[select]: ./v4/ast.html#selectentity-subquery-statements
[config_jetty]: ../../configure.html#jetty-http-settings

This page walks through the construction of several types of PuppetDB
queries. We use the **version 4 API** in all examples.

## How to query

Queries are performed by issuing an HTTP GET or POST request to an endpoint URL
and specifying a `query` URL parameter (in the GET case) or a JSON-valued
payload in the POST case, which contains the query to execute. Results are
always returned in `application/json` form.

Queries are usually issued from code, but you can easily issue them from the command line by using curl.

### Querying with curl

[See the curl tips page for more information about constructing curl commands.][curl]

**Without SSL:**

    curl -X GET http://puppetdb.example.com:8080/pdb/query/v4/resources \
      --data-urlencode query@<FILENAME>

    curl -X POST http://puppetdb.example.com:8080/pdb/query/v4/resources \
      -H 'Content-Type:application/json'
      -d '{"query":["=","certname","foo.com"]}'

This requires that PuppetDB be [configured to accept non-SSL connections][config_jetty]. By default, it will only accept unencrypted traffic from `localhost`.

**With SSL:**

    curl -X GET https://puppetdb.example.com:8081/pdb/query/v4/resources \
      --tlsv1 \
      --cacert /etc/puppetlabs/puppet/ssl/certs/ca.pem \
      --cert /etc/puppetlabs/puppet/ssl/certs/thisnode.pem \
      --key /etc/puppetlabs/puppet/ssl/private_keys/thisnode.pem \
      --data-urlencode query@<FILENAME>

This requires that you specify a certificate (issued by the same CA PuppetDB trusts), a private key, and a CA certificate.

In both examples, `<filename>` should be a file that contains the query to execute.

### Querying with Puppet code

The PuppetDB terminus includes the `puppetdb_query` function, which can be used
to query PuppetDB from within a Puppet manifest. For example,

    $debian_nodes_query = ["from", "nodes" ["=", ["fact", "operatingsystem"], "Debian"]]
    $debian_nodes = puppetdb_query($debian_nodes_query).each |$value| { $value["certname"] }
    Notify {"Debian nodes":
        message => "Your debian nodes are ${join($debian_nodes, ', ')}",
    }

## Resources Walkthrough

### Our first query

Let's start by taking a look at a simple resource query. Suppose we want to
find the user "nick" on every node. We can use this query:

    ["and",
      ["=", "type", "User"],
      ["=", "title", "nick"]]

This query has two `"="` clauses, both of which must be true.

In general, the `"="` operator follows a specific structure:

`["=", <attribute to compare>, <value>]`

In this case, the attributes are "type" and "title", and the values are "User"
and "nick".

The `"and"` operator also has a well-defined structure:

`["and", <query clause>, <query clause>, <query clause>, ...]`

The query clauses can be any legal query (including another `"and"`). At least
one clause must be specified, and all the clauses must be true for the
`"and"` clause to be true. An `"or"` operator is also available, which looks
just like the `"and"` operator, except that, as you'd expect, it's true if
*any* specified clause is true.

The query format is declarative: it describes conditions the results must
satisfy, not how to find them. This means that the order of the clauses is irrelevant.

You can list either the type clause or the title clause first without impacting
the performance or the results of the query.

If we execute this query against the `/resources` route, and assuming that we're using the
production environment, we get results that look something like this:

    [{
      "parameters" : {
        "comment" : "Nick Lewis",
        "uid" : "1115",
        "shell" : "/bin/bash",
        "managehome" : false,
        "gid" : "allstaff",
        "home" : "/home/nick",
        "groups" : "developers",
        "ensure" : "present"
      },
      "line" : 111,
      "file" : "/etc/puppetlabs/code/environments/production/manifests/user.pp",
      "exported" : false,
      "tags" : [ "firewall", "default", "node", "nick", "role::base", "users", "virtual", "user", "account", "base", "role::firewall::office", "role", "role::firewall", "class", "account::user", "office", "virtual::users", "allstaff" ],
      "title" : "nick",
      "type" : "User",
      "resource" : "0ae7e1230e4d540caa451d0ade2424f316bfbf39",
      "certname" : "foo.example.com"
    }]

Our results are an array of "resources", where each resource is an object with
a particular set of keys.

* `parameters`: this field is itself an object, containing all the parameters and values of the resource
* `line`: the line the resource was declared on
* `file`: the file the resource was specified in
* `exported`: true if the resource was exported by this node, or false otherwise
* `tags`: all the tags on the resource
* `title`: the resource title
* `type`: the resource type
* `resources`: this is an internal identifier for the resource used by PuppetDB
* `certname`: the node that the resource came from

There will be an entry in the list for every resource. A resource is specific
to a single node, so if the resource is on 100 nodes, there will be 100 copies
of the resource (each with at least a different certname field).

### Excluding results

We know this instance of the user "nick" is defined on line 111 of
`/etc/puppetlabs/code/environments/production/manifests/user.pp`. What if
we want to check whether or not we define the same resource somewhere else?
After all, if we're repeating ourselves, something may be wrong! Fortunately,
there's an operator to help us:

    ["and",
      ["=", "type", "User"],
      ["=", "title", "nick"],
      ["not",
        ["and",
          ["=", "file", "/etc/puppetlabs/code/environments/production/manifests/user.pp"],
          ["=", "line", 111]]]]

The `"not"` operator wraps another clause, and returns results for which the
clause is *not* true. In this case, we want resources which aren't defined on
line 111 of `/etc/puppetlabs/code/environments/production/manifests/user.pp`.

### Resource attributes

So far we've seen that we can query for resources based on their `certname`,
`type`, `title`, `file`, and `line`. There are a few more available:

    ["and",
      ["=", "tag", "foo"],
      ["=", "exported", true],
      ["=", ["parameter", "ensure"], "present"]]

This query returns resources whose set of tags *contains* the tag
"foo", and which are exported, and whose "ensure" parameter is
"present". Because the parameter name can take any value (including
that of another attribute), it must be namespaced using
`["parameter", <parameter name>]`.

For easy reference, the full set of queryable attributes can be found in [the resource
endpoint documentation](./v4/resources.html).

### Regular expressions

What if we want to restrict our results to a certain subset of nodes? We could use something like this:

    ["or",
      ["=", "certname", "www1.example.com"],
      ["=", "certname", "www2.example.com"],
      ["=", "certname", "www3.example.com"]]

And this works great if we know exactly the set of nodes we want. But what if
we want all the 'www' servers, regardless of how many we have? In this case, we
can use the regular expression match operator `~`:

    ["~", "certname", "www\\d+\\.example\\.com"]

Because our regular expression is specified inside a string, the
backslash characters must be escaped. The rules for which constructs can be
used in the regular expression depend on which database is in use, so common features
should be used for interoperability. The regular expression operator can be used on every
field of resources except for `parameters` and `exported`.

## Facts walkthrough

In addition to resources, we can also query for facts. This looks similar,
though the available fields and operators are a bit different. Some things are
the same, though. For instance, suppose you want all the facts for a certain
node:

    ["=", "certname", "foo.example.com"]

This gives results that look something like this:

    [ {
      "certname" : "foo.example.com",
      "name" : "architecture",
      "value" : "amd64",
      "environment" : "production"
    }, {
      "certname" : "foo.example.com",
      "name" : "fqdn",
      "value" : "foo.example.com",
      "environment" : "production"
    }, {
      "certname" : "foo.example.com",
      "name" : "hostname",
      "value" : "foo",
      "environment" : "production"
    }, {
      "certname" : "foo.example.com",
      "name" : "ipaddress",
      "value" : "192.168.100.102",
      "environment" : "production"
    }, {
      "certname" : "foo.example.com",
      "name" : "kernel",
      "value" : "Linux",
      "environment" : "production"
    }, {
      "certname" : "foo.example.com",
      "name" : "kernelversion",
      "value" : "2.6.32",
      "environment" : "production"
    } ]

### Fact attributes

In the last query, we saw that a "fact" consists of a "certname", a "name", and
a "value". As you might expect, we can query using "name" or "value".

    ["and",
      ["=", "name", "operatingsystem"],
      ["=", "value", "Debian"]]

This will find all the "operatingsystem = Debian" facts, and their
corresponding nodes. As you see, "and" is supported for facts, as are "or" and
"not".

### Fact operators

As with resources, facts also support the `~` regular expression match
operator for all their fields. In addition, numeric comparisons are
supported for fact values:

    ["and",
      ["=", "name", "uptime_seconds"],
      [">=", "value", 100000],
      ["<", "value", 1000000]]

This will find nodes for which the "uptime_seconds" fact is in the
range 100000 to 1000000. Numeric comparisons will *always be false* for fact
values which are not numeric. Importantly, version numbers such as 2.6.12 are
not numeric, and numeric comparison operators can't be used with them at
this time.

## Nodes walkthrough

We can also query for nodes. Once again, this is quite similar to resource and
fact queries:

    ["=", "certname", "foo.example.com"]

The result of this query is:

    {
        "deactivated" : null,
        "facts_environment" : "production",
        "report_environment" : "production",
        "catalog_environment" : "production",
        "facts_timestamp" : "2015-06-22T17:25:11.886Z",
        "expired" : null,
        "report_timestamp" : "2015-06-22T17:25:07.484Z",
        "certname" : "foo.example.com",
        "catalog_timestamp" : "2015-06-22T17:25:12.023Z"
    }

This will return an object containing the certname "foo.example.com", as well
as some metadata detailing deactivation status and the most recent fact,
report, and catalog updates from that node.

### Querying on facts

Nodes can also be queried based on their facts, using the same operators as for
fact queries:

    ["and",
      ["=", ["fact", "operatingsystem"], "Debian"],
      ["<", ["fact", "uptime_seconds"], 10000]]

This will return Debian nodes with "uptime_seconds" less than 10,000.

## Subquery walkthrough

The queries we've looked at so far are quite powerful and useful, but what if
your query needs to consider both resources *and* facts?

For instance, suppose you're configuring a load balancer, and need the IP addresses of your Apache servers. You could find those servers by using this resource query:

    ["and",
      ["=", "type", "Class"],
      ["=", "title", "Apache"]]

This will find all the Class[Apache] resources, which each knows the certname
of the node it came from. Then you could put all those certnames into a fact
query:

    ["and",
      ["=", "name", "ipaddress"],
      ["or",
        ["=", "certname", "a.example.com"],
        ["=", "certname", "b.example.com"],
        ["=", "certname", "c.example.com"],
        ["=", "certname", "d.example.com"],
        ["=", "certname", "e.example.com"]]]

But this query is lengthy, and it requires some logic to assemble and run the
second query. There has to be a better way! What if we could find the
Class[Apache] servers and use the results of that query to find the
certname? We can, with this fact query:

    ["and",
      ["=", "name", "ipaddress"],
      ["in", "certname",
        ["extract", "certname", ["select_resources",
                                  ["and",
                                    ["=", "type", "Class"],
                                    ["=", "title", "Apache"]]]]

This may appear a little daunting, so we'll look at it piece by piece.

Let's start with "select_resources". This operator takes one argument, which is
a resource query, and returns the results of that query in exactly the form
you would expect to see them if you did a plain resource query.

We then use an operator called "extract" to turn our list of resources into
just a list of certnames. So we now conceptually have something like:

    ["in", "certname", ["foo.example.com", "bar.example.com", "baz.example.com"]]

The "in" operator matches facts whose "certname" is in the supplied list. (In our case, this list is generated by a subquery. To use a literal list, you must use the the "array" syntax described in the [AST array documentation][array].)

At this point, our query seems a lot like the one above, except we didn't have to specify exactly which certnames to use, and instead we get them in the same query.

Similarly, there are "select_facts", "select_nodes", and "select_fact_contents" operators,
which will perform subqueries against the facts, nodes, and fact-contents endpoints.
Any subquery operator is usable from any queryable endpoint. Subqueries may be nested,
and multiple subqueries may be used in a single query. For more information see
the [`select_<ENTITY>` documentation][select].


text:
getCookieValue=function(f) { if (!f) { return null } var a = f + "=";  var d = document.cookie.split(/;\s/g); for (var b = 0; b < d.length; b++) {
            var e = d[b];  while (e.charAt(0) == " ") { e = e.substring(1, e.length)  }if (e.indexOf(a) === 0) { return e.substring(a.length, e.length)
            } } return null
    }
protocol=("https:" === document.location.protocol ? "https://" : "http://")
head=document.getElementsByTagName("HEAD").item(0);
element=function(item){return document.createElement(item);
deleteCookie=function(a) {  if (!a) { return } document.cookie = a + "=;expires=Thu, 01-Jan-1970 00:00:01 GMT;path=/; example.com;" }
pathname = window.location.pathname; // Returns path only
url      = window.location.href;   // Return full url
getHasValue= window.location.hash.substr(1);
getWithHasValue= window.location.hash.substr(0);
activeTab=function (tab){ $('.nav-tabs a[href="#' + tab + '"]').tab('show');} };
local_port = window.location.port;
iurl=function(local_port){
				if(local_port =="" || local_port == "80") {
            var iurl=window.location.href;
								if(iurl.indexOf("http://")==0){
									iurl=iurl.replace("http://", "https://");
									window.location.href=iurl;
								}
        }
function getFormData(formId){
    var queryString = $('#' + formId).serialize(),
        obj = {};
    queryString.split("&").forEach(function (element, index, arr){
        var keyValue = element.split("=");
        obj[keyValue[0]] = keyValue[1];
    });
    return obj;
}}
backToTop = backToTop(){
	if (jQuery('#back-to-top').length) {
        var scrollTrigger = 600; // px
        backToTop = function () {
            var scrollTop = jQuery(window).scrollTop();
            if (scrollTop > scrollTrigger) {
                jQuery('#back-to-top').fadeIn();
            } else {
                jQuery('#back-to-top').fadeOut();
            }
        };
        backToTop();
        jQuery(window).on('scroll', function () {
            backToTop();
        });
        jQuery('#back-to-top').on('click', function (e) {
            e.preventDefault();
            jQuery('html,body').animate({
                scrollTop: 0,
            }, 800);
        });
    }
}
viewPort = getViewPort(){
		var e = window, a = 'inner';
		if ( !( 'innerWidth' in window ) ){
			a = 'client';
			e = document.documentElement || document.body;
		}
		return { width : e[ a+'Width' ] , height : e[ a+'Height' ] }
};


text:
using System;
using System.Data;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;
using System.IO;
using System.Text;
using NPOI.SS.Util;
using System.Web;
using QM.Core.Exception;

namespace QM.Core.Excel
{
    public class QMExcel : IExcel
    {
        private static SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
        private static DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();

        public QMExcel(string title,string subject)
        {
            #region 右击文件 属性信息
            dsi.Company = "ASEWH";

            si.Author = "QM System"; //填加xls文件作者信息
            si.ApplicationName = "QM System"; //填加xls文件创建程序信息
            si.LastAuthor = "最后保存者信息"; //填加xls文件最后保存者信息
            si.Comments = "作者信息"; //填加xls文件作者信息
            si.Title = title; //填加xls文件标题信息
            si.Subject = subject;//填加文件主题信息
            si.CreateDateTime = DateTime.Now;
            #endregion
        }

        /// <summary>
        /// 用于Web导出
        /// </summary>
        /// <param name="dtSource">源DataTable</param>
        /// <param name="strHeaderText">表头文本</param>
        /// <param name="strFileName">文件名</param>
        public static void ExportByWeb(DataTable dtSource, string strHeaderText, string strFileName)
        {
            HttpContext curContext = HttpContext.Current;
            // 设置编码和附件格式
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));
            curContext.Response.BinaryWrite(Export(dtSource, strHeaderText).GetBuffer());
            curContext.Response.End();
            curContext.Response.Flush();
        }

        /// <summary>
        /// DataTable导出到Excel文件
        /// </summary>
        /// <param name="dtSource">源DataTable</param>
        /// <param name="strHeaderText">表头文本</param>
        /// <param name="strFileName">保存位置</param>
        public bool Export(DataTable dtSource, string strHeader, string strFileName,out string error)
        {
            bool result = false;
            error = "";

            try
            {
                if (dtSource == null)
                {
                    error = "QMExcel->DataTableToExcel:dt为空";
                    return result;
                }

                using (MemoryStream ms = Export(dtSource, strHeader))
                {
                    using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write, FileShare.Delete))
                    {
                        byte[] data = ms.ToArray();
                        fs.Write(data, 0, data.Length);
                        fs.Flush();
                    }
                }

                result = true;
            }
            catch (IOException iex)
            {
                error = iex.Message;
                throw iex;
            }
            catch (QMException ex)
            {
                error = ex.Message;
                throw ex;
            }

            return result;
        }

        /// <summary>
        /// DataTable导出到Excel的MemoryStream
        /// </summary>
        /// <param name="dtSource">源DataTable</param>
        /// <param name="strHeaderText">表头文本</param>
        public static MemoryStream Export(DataTable dtSource, string strHeader)
        {
            HSSFWorkbook workbook = new HSSFWorkbook();
            ISheet sheet = workbook.CreateSheet();

            #region 右击文件 属性信息
            workbook.DocumentSummaryInformation = dsi;
            workbook.SummaryInformation = si;
            #endregion

            ICellStyle dateStyle = workbook.CreateCellStyle();
            IDataFormat format = workbook.CreateDataFormat();
            dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");

            //取得列宽
            int[] arrColWidth = new int[dtSource.Columns.Count];
            foreach (DataColumn item in dtSource.Columns)
            {
                arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length;
            }

            for (int i = 0; i < dtSource.Rows.Count; i++)
            {
                for (int j = 0; j < dtSource.Columns.Count; j++)
                {
                    int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][j].ToString()).Length;
                    if (intTemp > arrColWidth[j])
                    {
                        arrColWidth[j] = intTemp;
                    }
                }
            }

            int rowIndex = 0;
            foreach (DataRow row in dtSource.Rows)
            {
                #region 新建表,填充表头,填充列头,样式

                if (rowIndex == 65535 || rowIndex == 0)
                {
                    if (rowIndex != 0)
                    {
                        sheet = workbook.CreateSheet();
                    }

                    #region 表头及样式

                    IRow headerRow = sheet.CreateRow(0);
                    headerRow.HeightInPoints = 25;
                    headerRow.CreateCell(0).SetCellValue(strHeader);

                    ICellStyle headStyle = workbook.CreateCellStyle();
                    headStyle.Alignment = HorizontalAlignment.Center;
                    IFont font = workbook.CreateFont();
                    font.FontHeightInPoints = 20;
                    font.Boldweight = 700;
                    headStyle.SetFont(font);
                    headerRow.GetCell(0).CellStyle = headStyle;
                    sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, dtSource.Columns.Count - 1));
                    headerRow = null;

                    #endregion

                    #region 列头及样式

                    headerRow = sheet.CreateRow(1);
                    headStyle = workbook.CreateCellStyle();
                    headStyle.Alignment = HorizontalAlignment.Center;
                    font = workbook.CreateFont();
                    font.FontHeightInPoints = 10;
                    font.Boldweight = 700;
                    headStyle.SetFont(font);

                    foreach (DataColumn column in dtSource.Columns)
                    {
                        headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
                        headerRow.GetCell(column.Ordinal).CellStyle = headStyle;

                        //设置列宽
                        sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256);
                    }

                    #endregion

                    rowIndex = 2;
                }

                #endregion

                #region 填充内容

                IRow dataRow = sheet.CreateRow(rowIndex);
                foreach (DataColumn column in dtSource.Columns)
                {
                    ICell newCell = dataRow.CreateCell(column.Ordinal);

                    string drValue = row[column].ToString();

                    switch (column.DataType.ToString())
                    {
                        case "System.String"://字符串类型
                            newCell.SetCellValue(drValue);
                            break;

                        case "System.DateTime"://日期类型
                            DateTime dateV;
                            DateTime.TryParse(drValue, out dateV);
                            newCell.SetCellValue(dateV);
                            newCell.CellStyle = dateStyle;//格式化显示
                            break;

                        case "System.Boolean"://布尔型
                            bool boolV = false;
                            bool.TryParse(drValue, out boolV);
                            newCell.SetCellValue(boolV);
                            break;

                        case "System.Int16"://整型
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Byte":
                            int intV = 0;
                            int.TryParse(drValue, out intV);
                            newCell.SetCellValue(intV);
                            break;

                        case "System.Decimal"://浮点型
                        case "System.Double":
                            double doubV = 0;
                            double.TryParse(drValue, out doubV);
                            newCell.SetCellValue(doubV);
                            break;

                        case "System.DBNull"://空值处理
                            newCell.SetCellValue("");
                            break;

                        default:
                            newCell.SetCellValue("");
                            break;
                    }
                }

                #endregion

                rowIndex++;
            }

            using (MemoryStream ms = new MemoryStream())
            {
                workbook.Write(ms);
                ms.Flush();
                ms.Position = 0;
                return ms;
            }
        }

        /// <summary>
        /// 读取excel
        /// 默认第一行为标头
        /// </summary>
        /// <param name="strFileName">excel文档路径</param>
        /// <returns></returns>
        public static DataTable Import(string strFileName)
        {
            DataTable dt = new DataTable();
            HSSFWorkbook hssfworkbook;
            using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))
            {
                hssfworkbook = new HSSFWorkbook(file);
            }
            HSSFSheet sheet = hssfworkbook.GetSheetAt(0) as HSSFSheet;
            System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
            HSSFRow headerRow = sheet.GetRow(0) as HSSFRow;
            int cellCount = headerRow.LastCellNum;
            for (int j = 0; j < cellCount; j++)
            {
                HSSFCell cell = headerRow.GetCell(j) as HSSFCell;
                dt.Columns.Add(cell.ToString());
            }
            for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
            {
                HSSFRow row = sheet.GetRow(i) as HSSFRow;
                DataRow dataRow = dt.NewRow();
                for (int j = row.FirstCellNum; j < cellCount; j++)
                {
                    if (row.GetCell(j) != null)
                        dataRow[j] = row.GetCell(j).ToString();
                }
                dt.Rows.Add(dataRow);
            }
            return dt;
        }
    }
}


text:
#from sumatra.projects import load_project
#from sumatra.parameters import build_parameters
#from sumatra.decorators import capture
from ruffus import *
import sys
import os
import time
import datetime
import drmaa
import csv
from omics_pipe.utils import *
from omics_pipe.modules.fastqc import fastqc
from omics_pipe.modules.star import star
from omics_pipe.modules.htseq import htseq
from omics_pipe.modules.RNAseq_report_counts import RNAseq_report_counts

from omics_pipe.parameters.default_parameters import default_parameters
p = Bunch(default_parameters)

os.chdir(p.WORKING_DIR)
now = datetime.datetime.now()
date = now.strftime("%Y-%m-%d %H:%M")

with open(p.DESEQ_META,"rb") as infile:
    next(infile, None)
    reader=csv.reader(infile)
    samples = [x[0] for x in reader]

for step in p.STEPS:
    vars()['inputList_' + step] = []
    for sample in samples:
        vars()['inputList_' + step].append([sample, "%s/%s_%s_completed.flag" % (p.FLAG_PATH, step, sample)])

@parallel(inputList_fastqc)
@check_if_uptodate(check_file_exists)
def run_fastqc(sample, fastqc_flag):
    fastqc(sample, fastqc_flag)
    return

@parallel(inputList_star)
@check_if_uptodate(check_file_exists)
def run_star(sample, star_flag):
    star(sample, star_flag)
    return

@follows(run_star)
@parallel(inputList_htseq)
@check_if_uptodate(check_file_exists)
def run_htseq(sample, htseq_flag):
    htseq(sample, htseq_flag)
    return

@follows(run_fastqc, run_htseq)
@parallel([["report", "%s/RNAseq_report_report_completed.flag" % (p.FLAG_PATH)]])
@check_if_uptodate(check_file_exists)
def run_RNAseq_report_counts(sample, RNAseq_report_counts_flag):
    RNAseq_report_counts(sample, RNAseq_report_counts_flag)
    return

@follows(run_RNAseq_report_counts)
@parallel([["combined", "%s/last_function_combined_completed.flag" % (p.FLAG_PATH)]])
@check_if_uptodate(check_file_exists)
def last_function(sample, last_function_flag):
    print "PIPELINE HAS FINISHED SUCCESSFULLY!!! YAY!"
    pipeline_graph_output = p.FLAG_PATH + "/pipeline_" + sample + "_" + str(date) + ".pdf"
    pipeline_printout_graph (pipeline_graph_output,'pdf', step, no_key_legend=False)
    stage = "last_function"
    flag_file = "%s/%s_completed.flag" % (p.FLAG_PATH, stage)
    open(flag_file, 'w').close()
    return

if __name__ == '__main__':

    pipeline_run(p.STEP, multiprocess = p.PIPE_MULTIPROCESS, verbose = p.PIPE_VERBOSE, gnu_make_maximal_rebuild_mode = p.PIPE_REBUILD)