예제 #1
0
void StatsAnalyzer::WriteOpcodeMarkov(std::ostream& out) {
  if (stats_.opcode_markov_hist.empty()) return;

  const std::unordered_map<uint32_t, std::unordered_map<uint32_t, uint32_t>>&
      cue_to_hist = stats_.opcode_markov_hist[0];

  // Sort by prevalence of the opcodes in opcode_freq_ (descending).
  std::vector<std::pair<uint32_t, std::unordered_map<uint32_t, uint32_t>>>
      sorted_cue_to_hist(cue_to_hist.begin(), cue_to_hist.end());
  std::sort(
      sorted_cue_to_hist.begin(), sorted_cue_to_hist.end(),
      [this](const std::pair<uint32_t, std::unordered_map<uint32_t, uint32_t>>&
                 left,
             const std::pair<uint32_t, std::unordered_map<uint32_t, uint32_t>>&
                 right) {
        const double lf = opcode_freq_[left.first];
        const double rf = opcode_freq_[right.first];
        if (lf == rf) return right.first > left.first;
        return lf > rf;
      });

  for (const auto& kv : sorted_cue_to_hist) {
    const uint32_t cue = kv.first;
    const double kFrequentEnoughToAnalyze = 0.0001;
    if (opcode_freq_[cue] < kFrequentEnoughToAnalyze) continue;

    const std::unordered_map<uint32_t, uint32_t>& hist = kv.second;

    uint32_t total = 0;
    for (const auto& pair : hist) {
      total += pair.second;
    }

    std::vector<std::pair<uint32_t, uint32_t>> sorted_hist(hist.begin(),
                                                           hist.end());
    std::sort(sorted_hist.begin(), sorted_hist.end(),
              [](const std::pair<uint32_t, uint32_t>& left,
                 const std::pair<uint32_t, uint32_t>& right) {
                if (left.second == right.second)
                  return right.first > left.first;
                return left.second > right.second;
              });

    for (const auto& pair : sorted_hist) {
      const double prior = opcode_freq_[pair.first];
      const double posterior =
          static_cast<double>(pair.second) / static_cast<double>(total);
      out << GetOpcodeString(cue) << " -> " << GetOpcodeString(pair.first)
          << " " << posterior * 100 << "% (base rate " << prior * 100
          << "%, pair occurrences " << pair.second << ")" << std::endl;
    }
  }
}
예제 #2
0
void CSiemensSR::Response(RB1 *rb)
{
	WORD response;
	SR_RBH *rb_H;
	static int pp = 0;

	if (rb == NULL)
		return;

	rb_H = (SR_RBH *) rb;
	
	/* check success/failure */
	response = rb_H->rb_response;
	if ((response % 2) == 0 || response == REQ_WAITING)
	{
		/* print out opcode and response */
		MSGERROR3 ("Error in \"%s\"\nrb_response = %s (%d)", 
			GetOpcodeString(rb_H->rb_opcode), GetResponseString(response), 
			response);

		CString strTemp;
		strTemp.Format (IDS_ERRSIEMENSSR, 
			GetOpcodeString(rb_H->rb_opcode),
			GetResponseString(response),
			response);

		CAlarma * ptrAlarma = new CAlarma;
		ptrAlarma->AvisarAlarma (6,2,0,0,0,0,0,0,0,strTemp);

		switch (response)
		{
		case REM_ABORT:
		case ILLEGAL_REQ:
		// Michael 08.07.2008
		case UNKNOWN_REFERENCE:
		// Michael 08.07.2008 fin
			MSGFATAL ("Cerrando comunicacion con el PLC por Error");
			m_fConnected = FALSE;	// Michael 27.11.2001
		}

		// Michael 29.01.2002
		switch (rb_H->rb_opcode)
		{
		case SEND_CONN_REQ:
		case OPEN_REQ:
			// Si ha dado error en la conexión, no va más
			MSGFATAL ("Fatal Siemens Error");
			g_Container.m_fSiemensFatalError = true;
		case RECEIVE_DATA:
			MSGFATAL ("Response () : Anulando receive por error en recepción");
			m_fReceivePend = false; // para que se inicie otro cuando se envíe algo
		}


		return;
	}

	switch (rb_H->rb_opcode) 
	{	
		case OPEN_REQ: 
			/* save reference to the opened connection */
			if (rb_H->rb_response == OK_RESP)
			{
				m_OpenRef = rb->rb.open.open_reference;
				Process(DO_CONNECT);
			}
			return;
		case SEND_CONN_REQ: 
			m_fConnected = TRUE;
			m_fConnecting = FALSE;
			Process(DO_SEND_EOM); // PC es activo - iniciar send al CP
			return;
		case SEND_EOM_DATA: 
			MSGTRACE ("CSiemensSR::Response (): rb_opcode = SEND_EOM_DATA");
			// Michael 29.01.2002 Solo si no hay recieve pendiente
			if (!m_fReceivePend)
				Process(DO_RECEIVE);	
		
			return;
		case RECEIVE_DATA:
			MSGTRACE ("CSiemensSR::Response (): rb_opcode = RECEIVE_DATA");
			VerMensajeRecibido ();
			Process(DO_RECEIVE); // Preparar siguiente lectura
			MSGAVISO ("CSiemensSR::Response (): Siguiente Recepcion pedido");

			return; 
		case CLOSE_REQ: 
			m_OpenRef = 0;
			m_fConnected = FALSE;
			// Michael 22.11.2001 SendMessage(m_hWnd, WM_DESTROY, 0, 0);
			return;
	}

}