Esempio n. 1
0
CXTPReportRecords* CXTPReportRecord::GetChilds()
{
	if (m_pChildren == NULL)
		m_pChildren = new CXTPReportRecords(this);

	if (GetRecords())
	{
		BOOL bCase = GetRecords()->IsCaseSensitive();
		m_pChildren->SetCaseSensitive(bCase);
	}
	return m_pChildren;
}
Esempio n. 2
0
CXTPReportRecord* CReportCtrl::TransferAddRecord(void)
{
	ASSERT (m_pAddOpRecord != NULL);

	CXTPReportRecord *pRec = new CXTPReportRecord();
	for (int i=0; i<m_nColCount; ++i)
	{
		const ColInfo &col = m_cols[i];
		if (col.fmt != FmtIdOnly)
		{
			CXTPReportRecordItem *pItem = new CXTPReportRecordItem();
			if (!col.check)
			{
				pItem->SetCaption(m_pAddOpRecord->GetItem(i)->GetCaption(NULL));
			}
			else
			{
				pItem->SetEditable(col.editable);
				pItem->HasCheckbox(col.check); 
				pItem->SetChecked(m_pAddOpRecord->GetItem(i)->IsChecked());
			}
			pRec->AddItem(pItem);
		}
	}

	PutRecordAux(pRec);

	GetRecords()->InsertAt(m_pAddOpRecord->GetIndex(),pRec);
	ClearAddRecord();
	
	Populate();

	return pRec;
 
}
Esempio n. 3
0
void CSimpleReport::RowDelete()
{
	int idx = RowGetFocused();
	GetRecords()->RemoveAt(idx);
	Populate();
	SetFocusedRow(GetRows()->GetAt(idx));
	SetFocus();
}
Esempio n. 4
0
void CSimpleReport::ColReplace(int r, int c, double n)
{
	CXTPReportRecord* pRec = GetRecords()->GetAt(r);
	if (!pRec)
		return;

	CXTPReportRecordItemNumber* p = (CXTPReportRecordItemNumber*)pRec->GetItem(c);
	if (p) 
		p->SetValue(n);
}
Esempio n. 5
0
void CSimpleReport::ColSetBold(int r, int c, BOOL x)
{
	CXTPReportRecord* pRec = GetRecords()->GetAt(r);
	if (!pRec)
		return;

	CXTPReportRecordItem* p = pRec->GetItem(c);
	if (p)
		p->SetBold(x);
}
Esempio n. 6
0
int BuscaPagina(int id,char szComando[],char szRuta[])
{
	Tipo_XML *xml=NULL;
	int i,sts;
	char szAux[512];
	char szComando1[256];
	FILE *f;
	//printf("Busca Pagina\n\r");
	if (global.xml_paginas==NULL)
	{
		char szSql[1024];
		sprintf(szSql,"select * from paginas");
		global.xml_paginas=GetRecords(id,szSql,&global.nTotalPaginas,global.xml_paginas,&sts);
		if (!sts)
		{
			WriteLog(id,"No puede leer tabla paginas");
			WriteLog(id,szSql);
			printf("Falla leer paginas");
			exit(0);
		}
	}
	//printf("Total Paginas %i\n\r",global.nTotalPaginas);

	for(i=0;i<global.nTotalPaginas;i++)
	{
		xml=global.xml_paginas;
		memset(szComando1,0,sizeof(szComando1));
		GetNivelStrXML(xml,"COMANDO",szComando1,sizeof(szComando1),i);
		printf("TAG=%s(%i)  %s(%i)\n\r",szComando1,strlen(szComando1),szComando,strlen(szComando));
		if ((strlen(szComando1)==strlen(szComando)) &&
		    (memcmp(szComando,szComando1,strlen(szComando))==0))	
		{
		    GetNivelStrXML(xml,"RUTA",szAux,sizeof(szAux),i);
	            sprintf(szRuta,"%s",szAux);
		    return 1;
		}
	}
	
        WriteLog(0,szComando);
	//Si viene un .. me estan tratando de hackear
	if (strstr(szComando,".."))
	{
        	WriteLog(0,"INTENTO DE HACKEO");
		sprintf(szRuta,"/home/switch/paginas_web/index.htm");
	}
	else
	{
		//27-03-2009 cambio para operar como servidor web tradicional
		//si no esta la pagina en la tabla paginas enviamos como si fuera el archivo directo
		sprintf(szRuta,"/home/switch/paginas_web/%s",szComando);
        	WriteLog(0,szRuta);
	}
	
	return 1;
}
Esempio n. 7
0
void CSimpleReport::ColReplace(int r, int c, const char* s)
{
	CXTPReportRecord* pRec = GetRecords()->GetAt(r);
	if (!pRec)
		return;

	CXTPReportRecordItem* p = pRec->GetItem(c);
	if (p)
		p->SetCaption(s);

}
Esempio n. 8
0
void CSimpleReport::ColSetColor(int r, int c, COLORREF fore, COLORREF back)
{
	CXTPReportRecord* pRec = GetRecords()->GetAt(r);
	if (!pRec)
		return;

	CXTPReportRecordItem* p = pRec->GetItem(c);
	if (p) {
		p->SetBackgroundColor(back);
		p->SetTextColor(fore);
	}
}
Esempio n. 9
0
void CSimpleReport::RowInsert(CXTPReportRecord* pRec, bool bAdd)
{
	if (!pRec)
		return;

	CXTPReportRow* pRow = GetFocusedRow();
	if (bAdd) {
		pRec = GetRecords()->Add(pRec);	// 맨 뒤에 추가
	}
	else {
		if (pRow != NULL) {		
			int n = pRow->GetRecord()->GetIndex();
			GetRecords()->InsertAt(n, pRec);
		}
		else { // 데이터 처음으로 넣음
			pRec = GetRecords()->Add(pRec);
		}
	}
	Populate();
	RowSetFocused(pRec);
}
Esempio n. 10
0
static void PackData(uint16_t record_id, SnifferResponse *response){
	uint8_t interrupt_flag;
	SnifferResponse_RSSIRecord *record;

	int count=GetRecords(record_id,rssi_buf,id_buf,&interrupt_flag);
	DEBUG_PRINT("Pack: record count=%d\n",count);
	for(int i=0;i<count;++i){
		record=response->add_rssi_data();
		record->set_mac(rssi_buf[i].mac,MAC_LEN);
		record->set_rssi(rssi_buf[i].rssi);
		record->set_id(id_buf[i]);
	}
	response->set_interrupted((bool)interrupt_flag);
}
Esempio n. 11
0
int BuscaFormatoSms(int id,Tabla_FormatoSms *p)
{
	Tipo_XML *xml=NULL;
	int i,sts;
	char szAux[1024];
	if (global.xml_formatosms==NULL)
	{
		char szSql[1024];
		sprintf(szSql,"select * from sms_formato_msg");
		global.xml_formatosms=GetRecords(id,szSql,&global.nTotalFormatoSms,global.xml_formatosms,&sts);
		if (!sts)
		{
			WriteLog(id,"No puede leer tabla tablas");
			printf("Falla leer tablas");
			exit(0);
		}
	}
	
	xml=global.xml_formatosms;
	for(i=0;i<global.nTotalFormatoSms;i++)
	{
		char szCliente[5];
		char szTmp[100];
		char chTipoTx,chProtocolo;
		GetNivelStrXML(xml,"CLIENTE",szAux,sizeof(szAux),i);
		sprintf(szCliente,"%s",szAux);
		GetNivelStrXML(xml,"TIPO_MSG",szAux,sizeof(szAux),i);
		chTipoTx=szAux[0];
		GetNivelStrXML(xml,"PROTOCOLO",szAux,sizeof(szAux),i);
		chProtocolo=szAux[0];
		//sprintf(szTmp,"PROTO=%c CLIENTE=%s TX=%c",chProtocolo,szCliente,chTipoTx);
		//WriteLog(id,szTmp);
            	if ((memcmp(szCliente,p->szCliente,strlen(szCliente))==0) &&
		    (chTipoTx==p->chTipoTx) &&
		    (chProtocolo==p->chProtocolo))
		{
		    GetNivelIntXML(xml,"FORMATO_ENTRADA",&p->nFormatoIn,i);
		    GetNivelIntXML(xml,"FORMATO_SALIDA",&p->nFormatoOut,i);
		    GetNivelIntXML(xml,"FORMATO_SALIDA_TEXTO",&p->nFormatoOutTexto,i);
		    GetNivelIntXML(xml,"FORMATO_IN_TRANS",&p->nFormatoInTrans,i);
		    GetNivelIntXML(xml,"FORMATO_OUT_TRANS",&p->nFormatoOutTrans,i);
		    GetNivelIntXML(xml,"POSICION_DATOS",&p->nPosicionDatos,i);
		    GetNivelStrXML(xml,"TX_BACKOFFICE",szAux,sizeof(szAux),i);
		    sprintf(p->szTxBack,"%s",szAux);
		    return 1;
		}
	}
	return 0;
}
Esempio n. 12
0
TEST_F(CheckpointTests, CheckpointScanTest) {
  logging::LoggingUtil::RemoveDirectory("pl_checkpoint", false);

  auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
  txn_manager.BeginTransaction();

  // Create a table and wrap it in logical tile
  size_t tile_group_size = TESTS_TUPLES_PER_TILEGROUP;
  size_t table_tile_group_count = 3;

  // table has 3 tile groups
  std::unique_ptr<storage::DataTable> target_table(
      ExecutorTestsUtil::CreateTable(tile_group_size));
  ExecutorTestsUtil::PopulateTable(target_table.get(),
                                   tile_group_size * table_tile_group_count,
                                   false, false, false);
  txn_manager.CommitTransaction();

  auto cid = txn_manager.GetNextCommitId() - 1;
  LOG_INFO("Scan with cid = %d. MaxCommittedCid = %d", (int) cid, (int) txn_manager.GetMaxCommittedCid());
  auto schema = target_table->GetSchema();
  std::vector<oid_t> column_ids;
  column_ids.resize(schema->GetColumnCount());
  std::iota(column_ids.begin(), column_ids.end(), 0);

  // create checkpoint
  auto &checkpoint_manager = logging::CheckpointManager::GetInstance();
  checkpoint_manager.Configure(CHECKPOINT_TYPE_NORMAL, true, 1);
  checkpoint_manager.DestroyCheckpointers();
  checkpoint_manager.InitCheckpointers();
  auto checkpointer = checkpoint_manager.GetCheckpointer(0);

  auto simple_checkpointer =
      reinterpret_cast<logging::SimpleCheckpoint *>(checkpointer);

  simple_checkpointer->SetLogger(new logging::WriteAheadBackendLogger());
  simple_checkpointer->SetStartCommitId(cid);
  simple_checkpointer->Scan(target_table.get(), DEFAULT_DB_ID);

  // verify results
  auto records = simple_checkpointer->GetRecords();
  EXPECT_EQ(records.size(),
            TESTS_TUPLES_PER_TILEGROUP * table_tile_group_count);
  for (unsigned int i = 0; i < records.size(); i++) {
    EXPECT_EQ(records[i]->GetType(), LOGRECORD_TYPE_WAL_TUPLE_INSERT);
  }
}
Esempio n. 13
0
int LeeTablaXML(int id,char szSql[],Tipo_Tabla_XML *p)
{
	int sts;
	WriteLog(id,"Lock Proceso");
	pthread_mutex_lock(&p->mutex);
	if (p->xml!=NULL) goto fin;
	p->xml=GetRecords(id,szSql,&p->nTotal,p->xml,&sts);
	if (sts==0)
	{
		WriteLog(id,"Falla lectura de tabla");
		WriteLog(id,szSql);
		exit(1);
		//Si falla la lectura de la tabla dejo el puntero en nulo para que se vuelva a leer
		p->xml=CierraXML(p->xml);
		sts=-1;
	}
fin:
	WriteLog(id,"UnLock Proceso");
	pthread_mutex_unlock(&p->mutex);
	return sts;
}
Esempio n. 14
0
void CSimpleReport::RowDown( bool (*copyFunc)(CSimpleReport*, CXTPReportRecord*, CXTPReportRecord*) )
{
	int rowidx = GetFocusedRow()->GetIndex();
	int recidx = GetFocusedRow()->GetRecord()->GetIndex();
	CXTPReportRecords* pRecs = GetRecords();
	if (!pRecs || recidx >= pRecs->GetCount() - 1)
		return;

	CXTPReportRecord* pRec = newRec();
	CXTPReportRecord* src = pRecs->GetAt(recidx);
	if (!pRec || !src)
		return;

	copyFunc(this, src, pRec);

	pRecs->RemoveAt(recidx);
	pRecs->InsertAt(recidx+1, pRec);

	Populate();
	SetFocusedRow(GetRows()->GetAt(rowidx+1));
	SetFocus();
}
Esempio n. 15
0
int BuscaTabla(int id,int nTx,Tipo_Tabla *p)
{
	Tipo_XML *xml=NULL;
	int i,sts;
	char szAux[512];
	if (global.xml_tablas==NULL)
	{
		char szSql[1024];
		sprintf(szSql,"select * from tablas");
		global.xml_tablas=GetRecords(id,szSql,&global.nTotalTablas,global.xml_tablas,&sts);
		if (!sts)
		{
			WriteLog(id,"No puede leer tabla tablas");
			printf("Falla leer tablas");
			exit(0);
		}
	}
	
	for(i=0;i<global.nTotalTablas;i++)
	{
		xml=global.xml_tablas;
		GetNivelIntXML(xml,"TX",&p->nTx,i);
            	if (p->nTx==nTx)
		{
		    GetNivelStrXML(xml,"QUERY",szAux,sizeof(szAux),i);
	            sprintf(p->szQuery,"%s",szAux);
		    GetNivelStrXML(xml,"LIMITE",szAux,sizeof(szAux),i);
	            p->nLimit=atoi(szAux);
		    GetNivelStrXML(xml,"TITULO",szAux,sizeof(szAux),i);
	            sprintf(p->szTitulo,"%s",szAux);
		    GetNivelIntXML(xml,"BASEDATOS",&p->nBaseDatos,i);
		    return 1;
		}
	}
	return 0;
}
Esempio n. 16
0
int main(int argc, char *argv[]) {
  int i, j;
  int record_count = 400;
  int range = record_count/10;
  uint8_t len = 5;
  AttributeType type[len];
  const char hans[] = "hans";
  Index *index;
  Iterator **iterator = (Iterator**) malloc(sizeof(Iterator*));
  Transaction *t;
  Record recs[record_count];
  Key keys[record_count];
  Key minkey;
  Key maxkey;
  Record *currentResult;
  Attribute attr;
  Block blocks[record_count];
  time_t tm;
  
  for (j = 0; j < record_count; j++) {
    blocks[j].size = 5;
    blocks[j].data = malloc(5);
    sprintf((char*)blocks[j].data, "%i", j);
    attr.type = kInt;
    keys[j].value = (Attribute**) malloc(len*sizeof(Attribute*));
    keys[j].attribute_count = len;
//     printf("Key number %i: \n", j);
    for (i = 0; i < len; i++) {
      type[i] = kInt;
      keys[j].value[i] = (Attribute*) malloc(sizeof(Attribute));
      keys[j].value[i]->type = kInt;
      keys[j].value[i]->int_value = randInt(range);
//       printf("key.value[%i] = %ld \n", i, keys[j].value[i]->int_value);
    }
//     printf("\n");
    recs[j].key = keys[j];
    recs[j].payload = blocks[j];
  }
  // generate min and max keys
  minkey.value = (Attribute**) malloc(len*sizeof(Attribute*));
  minkey.attribute_count = len;
  maxkey.value = (Attribute**) malloc(len*sizeof(Attribute*));
  maxkey.attribute_count = len;
  for (i = 0; i < len; i++) {
    minkey.value[i] = (Attribute*) malloc(sizeof(Attribute));
    minkey.value[i]->type = kInt;
    minkey.value[i]->int_value = 0.3*range;
    maxkey.value[i] = (Attribute*) malloc(sizeof(Attribute));
    maxkey.value[i]->type = kInt;
    maxkey.value[i]->int_value = 0.8*range;
  }
  printf("Done with generating data. \n");
  
  tm = time(NULL);
  CreateIndex(hans, len, type); // len
  OpenIndex(hans, &index);
  BeginTransaction(&t);
  for (i = 0; i < record_count; i++) { //record_count
    InsertRecord(t, index, recs+i);
    if (i % 1000 == 0) 
      printf("i = %i / %i \n", i, record_count);
  }
  printf("finished inserting, took %is \n", time(NULL)-tm);
  tm = time(NULL);
//   for (i = 0; i < record_count; i+=2) { // record_count
//     if (i == 398) {
//       printf("deleting record %i \n", i);
//     }
    DeleteRecord(t, index, recs+4, 0);
//   }
//   if (DeleteRecord(t, index, recs+398, 0) != kErrorNotFound) {
//     printf(";_; \n ");
//   }
  printf("deleted half of the records, took %is \n", time(NULL)-tm);
  GetRecords(t, index, minkey, maxkey, iterator);
  while (GetNext(*iterator, &currentResult) != kErrorNotFound) {
    printf("%i \t(%i\t%i\t%i\t%i\t%i)\t%s \n", currentResult->key.value[0]->int_value, currentResult->key.value[0]->int_value, currentResult->key.value[1]->int_value, currentResult->key.value[2]->int_value, currentResult->key.value[3]->int_value, currentResult->key.value[4]->int_value, currentResult->payload.data);
  }
  printf("just to be sure: \n");
  for (i = 0; i < record_count; i++) {
    for (j = 0; j < len; j++) { // len
//       if (!(compareKeys(minkey, keys[i], j, true) <= 0 && compareKeys(keys[i], maxkey, j, true) < 0)) {
	if (!((minkey.value[j] == 0 || compareKeys(minkey, keys[i], j, true) <= 0) && (maxkey.value[j] == 0 || compareKeys(keys[i], maxkey, j, true) < 0))) {
	break;
      }
    }
    if (j == len) { // len
      printf("so: %i \n", keys[i].value[0]->int_value);
    }
  }
//   printf("committing \n");
  CommitTransaction(&t);
//   printf("closing \n");
  CloseIndex(&index);
//   printf("deleting \n");
  DeleteIndex(hans);
//   printf("should be zero: %i \n", all_indices["hans"]);
  return 0;
}
Esempio n. 17
0
void CSimpleReport::Paste()
{
	if (IsVirtualMode())
		return;

	CWaitCursor _WC;

	CLIPFORMAT uCF_Records = (CLIPFORMAT)::RegisterClipboardFormat(XTPREPORTCTRL_CF_RECORDS);

	CXTPReportRecords arRecords;
	BOOL bTryPasteFromText = TRUE;

	// Retrieve text from the clipboard
	if (!OpenClipboard())
		return;

	if (::IsClipboardFormatAvailable(uCF_Records))
	{
		HGLOBAL hPasteData = ::GetClipboardData(uCF_Records);

		if (hPasteData)
		{
			bTryPasteFromText = FALSE;

			const int cErrTextSize = 1024;
			TCHAR szErrText[cErrTextSize + 1];

			CSharedFile fileSahred;
			fileSahred.SetHandle(hPasteData, FALSE);
			CArchive ar(&fileSahred, CArchive::load);

			try
			{
				CXTPPropExchangeArchive px(ar);
				BOOL bread = FALSE;
			#if _XTPLIB_VERSION_PREFIX == 1502
				bread = _ReadRecordsFromData(&px, arRecords);
			#else
				bread = _ReadRecodsFromData(&px, arRecords);
			#endif
				if (!bread)
				{
					arRecords.RemoveAll();
				}
			}
			catch(CArchiveException* pE)
			{
				if (pE->GetErrorMessage(szErrText, cErrTextSize))
				{
					TRACE(_T("EXCEPTION: CXTPReportControl::Paste() - %s\n"), szErrText);
				}
				pE->Delete();
			}
			catch(CFileException* pE)
			{
				if (pE->GetErrorMessage(szErrText, cErrTextSize))
				{
					TRACE(_T("EXCEPTION: CXTPReportControl::Paste() - %s\n"), szErrText);
				}
				pE->Delete();
			}
			catch(...)
			{
				TRACE(_T("EXCEPTION: CXTPReportControl::Paste() - Unhandled Exception!\n"));
			}

			//*********
			ar.Close(); // detach from file
			fileSahred.Detach(); //detach from data
			::GlobalUnlock(hPasteData); // unlock data
		}
	}

	UINT uCF_TText = sizeof(TCHAR) == 2 ? CF_UNICODETEXT : CF_TEXT;

	if (bTryPasteFromText && ::IsClipboardFormatAvailable(uCF_TText))
	{
		// Try to get text data from the clipboard
		HGLOBAL hglbPaste = ::GetClipboardData(uCF_TText);

		// Import Text data into the control
		if (hglbPaste != NULL)
		{
			TCHAR* lpszClipboard = (TCHAR*)GlobalLock(hglbPaste);
			BOOL bRead = FALSE;
		#if _XTPLIB_VERSION_PREFIX == 1502	 
			bRead = _ReadRecordsFromText(lpszClipboard, arRecords);
		#else
			bRead = _ReadRecodsFromText(lpszClipboard, arRecords);
		#endif
			if (!bRead)
			{
				arRecords.RemoveAll();
			}
			::GlobalUnlock(hglbPaste);
		}
	}

	::CloseClipboard();
	//////////////////////////////////////////////////////////////////////////

	CUpdateContext updateContext(this);

	// Add and Populate records
	CXTPReportRow* pRow = GetFocusedRow();
	if (pRow == NULL)
		return;

	int n = pRow->GetRecord()->GetIndex();
	int nRecordsCount = arRecords.GetCount();
	if (nRecordsCount > 0) {
		for (int i = nRecordsCount - 1; i >= 0; i--) {
			CXTPReportRecord* pRecord = arRecords.GetAt(i);
			if (pRecord) {
				CMDTARGET_ADDREF(pRecord);
				if (m_bAdd)
					GetRecords()->Add(arRecords.GetAt(i));
				else
					GetRecords()->InsertAt(n, arRecords.GetAt(i));
			}
		}
		Populate();
		_SelectRows(&arRecords);
	}
	m_bAdd = false;
}
Esempio n. 18
0
bool CSimpleReport::doCutPaste(CPoint pt, int op)
{
	CXTPReportSelectedRows* pRows = GetSelectedRows();
	if (pRows == NULL)
		return false;

	CXTPReportRow* pRow = pRows->GetAt(0);
	int nRow = 0;
	if (pRow)
		nRow = pRows->GetAt(0)->GetIndex();

	int n;
	CMenu menu;
	if (!menu.CreatePopupMenu())
		return false;

	if ((op & DO_ADD) != 0)	{
		menu.AppendMenu(MF_STRING, 10, "Add New");
		menu.AppendMenu(MF_STRING, 13, "Insert");
	}
	if (CanCopy() && (op & DO_COPY) != 0)
		menu.AppendMenu(MF_STRING, 11, "Copy");
	if (CanCut() && (op & DO_CUT) != 0)
		menu.AppendMenu(MF_STRING, 12, "Delete");

	n = menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RETURNCMD, pt.x, pt.y, this);
	menu.DestroyMenu();

	switch (n) {
		case 10:	// add
		case 13:	// insert
			{
				CXTPReportRecord* pRec = newRec();
				if (callback_add) {
					if (!callback_add(this, pRec)) {
						delete pRec;
						return false;
					}
				}
				RowInsert(pRec, (n == 10));
			}
			break;

		case 11:	// copy
			{
				pRow = GetFocusedRow();
				if (!pRow)
					return false;

				CXTPReportRecord* pSource = pRow->GetRecord();
				CXTPReportRecord* pTarget  = newRec();
		
				if (callback_copy) {
					if (!callback_copy(this, pSource, pTarget)) {
						delete pTarget;
						return false;
					}
				}
				RowInsert(pTarget);
			}
			break;
		case 12:	// delete
			{
				n = pRows->GetCount();
				for (int i = 0; i < n; i++) {
					if (callback_del) {
						CXTPReportRecord* pRec = pRows->GetAt(i)->GetRecord();
						if (!callback_del(this, pRec)) {
							return false;
						}
					}
				}

				for (int i = n - 1; i >= 0; i--) {
					pRow = pRows->GetAt(i);
					GetRecords()->RemoveAt(pRow->GetRecord()->GetIndex());
				}
				Populate();
				RowSetFocused(nRow);
			}
			break;
		default:
			return false;
	}
	return true;
}
Esempio n. 19
0
void CReportCtrl::DeleteRecord(CXTPReportRecord *pRec)
{
	GetRecords()->RemoveRecord(pRec);
	Populate();
}
Esempio n. 20
0
int Ptron_edam( int argc, char** argv, ostream& errMsg){
  errMsg << "Run E-DaM\n";
  //errMsg << "[mode]: training(0), testing(1), both(2), Anomaly Detection(3), Error-D(4), Error-DaM(5)\n";
  //errMsg << "[norm]: unnormalized(0)/normalized(1) data\n";
  errMsg << "[datafile]: Name of file containing data. \n";
  errMsg << "[tilt_time] :  (0) Do NOT use tilted time (1) use tilted time.\n";
  errMsg << "[nvar]: Number of variables in datafile. \n";
  errMsg << "[tgtIdx]: Column of target variable (first column = 0).\n";
  errMsg << "[delay1]: delay before starting time-series of first variable\n";
  errMsg << "[nlag1]: number of lags to compose the time-series of the first variable from\n";
  errMsg << "[delay2]: delay before starting time-series of secon variable\n";
  errMsg << "[nlag2]: number of lags to compose the time-series of the second variable from\n";
  errMsg << "\n\n\n";
  if ( argc != 9+2*(atoi(argv[7])) ) return(0);
  //
  string prefix = argv[2];
  cout << "# I/O prefix: " << prefix << endl;
  //
  int norm = atoi(argv[4]);
  cout << "# Using normalized params: " << norm << endl;
  if( norm!=0 && norm!=1 ){
    errMsg << "Error: Invalid variable [norm]\n";
    return(0);
  }
  //
  cout << "# Inputfile is: " << argv[5] << endl;
  ifstream ifile( argv[5] );
  if( !ifile ) {
    cerr << "ERROR: cannot open \"" << argv[5] << "\"... aborting\n";
    return( 0 );
  }
  //
  int mitigation;
  if( atoi( argv[3] ) == 4 ) mitigation=0;
  if( atoi( argv[3] ) == 5 ) mitigation=1;
  if( mitigation == 0 ) cout << "# Mitigation off.\n";
  if( mitigation == 1 ) cout << "# Mitigation on.\n";
  int tilt_time = atoi( argv[6] );
  if( tilt_time == 0 ) cout << "# Using linear time window.\n";
  if( tilt_time == 1 ) cout << "# Using tilted time window\n";
    //
  int nvar = atoi(argv[7]);
  cout << "# nvar is: " << nvar << endl;
  //
  int tgtIdx = atoi(argv[8]);
  cout << "# Target index is: " << tgtIdx << endl;
  if( tgtIdx > nvar-1 ){
    errMsg << "ERROR: Invalid target index\n";
    return( 0 );
  }
  //
  vector<int> delay(nvar);
  vector<int> nlags(nvar);
  cout << "# ( delay , lag )\n";
  for(int i=0; i< nvar; i++){
    int argvIdx = 9+2*i;
    delay[i] = atoi(argv[argvIdx]);
    nlags[i] = atoi(argv[argvIdx+1]);
    if( i==tgtIdx && delay[i] <= 0){
      delay[i]==1;
    }
    cout << "# ( " << delay[i] << "," << nlags[i] << " )" << endl;
  }
  int nAtt = 0;
  for( int i=0; i< nvar; i++)
  {
    nAtt += nlags[i];
  }
  //
  cout << "# Reading Data" << endl;
  vector< ts_record > Records;
  if( GetRecords( ifile, Records ) != 1 ){
  errMsg << "ERROR: could not read records from " << argv[5] << "... aborting.\n";
    return( 0 );
  }
  ifile.close();
  cout << "# Read " << Records.size() << " records\n";
  //
  vector< vector< float > > normParam;
  string npfile = prefix + "-norm_param.dat";
  GetNormParam( npfile, normParam );
  //
  string pfile_name = prefix + "-ptron_predictor.out";
  adet_ptron model( pfile_name );
  //
  int last_written_idx = 0;
  bool write = false;
  int inc = 30;
  int timer=-2;
  int ErrCnt = 0;
  int ExampCnt = 0;
  timestamp lastOutput;
  for( int i=0; i < Records.size(); i++ ){
    ts_record newEx;
    int secDiff;
    if( i==0) lastOutput = Records[i].TS();
    if( MakeLinearTimeExample( Records, i, tgtIdx-1, delay, nlags, newEx) == 1 ){
      //
      //vector< float > Example = newEx.Data();
      if( norm == 1 ) NormalizeExample( newEx.Data(), normParam );
      //
      vector< float > result = model.TestHelper( newEx.Data() );
      //
      if( norm == 1 ) UnnormalizeResult( result, normParam );
      //
      if( result[4] == 0 ) ErrCnt++;
      ExampCnt++;
     /********************************
     
     *********************************/
      newEx.TS().PrintTimestamp(cout);
      newEx.TS().PrintJulianDate(cout);
      for( int k=0; k<result.size(); k++ ){
        cout << setw(15) << result[k];
      }
      //cout << " write = true ";
      cout << endl;
     /**********************************
      if( write ){
       secDiff = newEx.TS().DifferenceSec(lastOutput);
       lastOutput = newEx.TS();
       if( secDiff < inc ){
         newEx.TS().PrintTimestamp(cout);
         newEx.TS().PrintJulianDate(cout);
          for( int k=0; k<result.size(); k++ ){
            cout << setw(15) << result[k];
          }
          //cout << " write = true ";
          cout << endl;
        }
        else{
          timer = -2;
          write = false;
        }
        if( result[4]== 0 ) timer = inc;
      }
      if(result[4] == 0 && !write ){
        //cout << "RESULT == 0 " << endl;
        if( ErrCnt != 1 ) cout << endl << endl << endl;
        for( int j=i-inc; j<=i; j++ ){
          if( j>=0 && j<Records.size() && Records[j].TS()>=Records[i].TS().NextIntervalSec(-inc) ){
//            if( j>=0 && j<Records.size() ){
            ts_record tmpEx;
            if( MakeLinearTimeExample( Records, j, tgtIdx-1, delay, nlags, tmpEx) == 1 ){
              if( norm == 1 ) NormalizeExample( tmpEx.Data(), normParam );
              //
              vector< float > tmpResult = model.TestHelper( tmpEx.Data() );
              //
              if( norm == 1 ) UnnormalizeResult( tmpResult, normParam );
              tmpEx.TS().PrintTimestamp(cout);
              tmpEx.TS().PrintJulianDate(cout);
              for( int k=0; k<tmpResult.size(); k++ ){
                cout << setw(15) << tmpResult[k];
              }
              secDiff = tmpEx.TS().DifferenceSec(lastOutput);
              lastOutput = tmpEx.TS();
            }
            else{
              Records[j].TS().PrintTimestamp(cout);
              Records[j].TS().PrintJulianDate(cout);
              cout << setw(15) << Records[j].Data()[0];
              secDiff = Records[j].TS().DifferenceSec(lastOutput);
              lastOutput = Records[j].TS();
            }
            //cout << " other loop";
            cout << endl;
          }
        }
        timer = inc;
      }
      if( timer > 0 ) write = true;
      else write = false;
      
      timer--;      
     **********************************/
      if( mitigation && result[4]==0  ){
        Records[i].Data()[tgtIdx-1] = result[2];
      }
    }
  }
  cout << "# Number of Examples: " << ExampCnt << endl;
  cout << "# Number of Errors: " << ErrCnt << endl;
  return( 1 );
}
Esempio n. 21
0
int Ptron_clean( int argc, char** argv, ostream& errMsg){
  errMsg << "Clean file\n";
  //errMsg << "[mode]: training(0), testing(1), both(2), Anomaly Detection(3), Error-D(4), Error-DaM(5)\n";
  //errMsg << "[norm]: unnormalized(0)/normalized(1) data\n";
  errMsg << "[datafile]: Name of file containing data. \n";
  errMsg << "[tilt_time] :  (0) Do NOT use tilted time (1) use tilted time.\n";
  errMsg << "[nvar]: Number of variables in datafile. \n";
  errMsg << "[tgtIdx]: Column of target variable (first column = 0).\n";
  errMsg << "[delay1]: delay before starting time-series of first variable\n";
  errMsg << "[nlag1]: number of lags to compose the time-series of the first variable from\n";
  errMsg << "[delay2]: delay before starting time-series of secon variable\n";
  errMsg << "[nlag2]: number of lags to compose the time-series of the second variable from\n";
  errMsg << "\n\n\n";
  if ( argc != 9+2*(atoi(argv[7])) ) return(0);
  //
  string prefix = argv[2];
  cout << "# I/O prefix: " << prefix << endl;
  //
  int norm = atoi(argv[4]);
  cout << "# Using normalized params: " << norm << endl;
  if( norm!=0 && norm!=1 ){
    errMsg << "Error: Invalid variable [norm]\n";
    return(0);
  }
  //
  cout << "# Inputfile is: " << argv[5] << endl;
  ifstream ifile( argv[5] );
  if( !ifile ) {
    cerr << "ERROR: cannot open \"" << argv[5] << "\"... aborting\n";
    return( 0 );
  }
  //
  int mitigation;
  if( atoi( argv[3] ) == 6 ) mitigation=0;
  if( atoi( argv[3] ) == 7 ) mitigation=1;
  if( mitigation == 0 ) cout << "# Excising anomalies.\n";
  if( mitigation == 1 ) cout << "# Correcting anomalies.\n";
  int tilt_time = atoi( argv[6] );
  if( tilt_time == 0 ) cout << "# Using linear time window.\n";
  if( tilt_time == 1 ) cout << "# Using tilted time window\n";
    //
  int nvar = atoi(argv[7]);
  cout << "# nvar is: " << nvar << endl;
  //
  int tgtIdx = atoi(argv[8]);
  cout << "# Target index is: " << tgtIdx << endl;
  if( tgtIdx > nvar-1 ){
    errMsg << "ERROR: Invalid target index\n";
    return( 0 );
  }
  //
  vector<int> delay(nvar);
  vector<int> nlags(nvar);
  cout << "# ( delay , lag )\n";
  for(int i=0; i< nvar; i++){
    int argvIdx = 9+2*i;
    delay[i] = atoi(argv[argvIdx]);
    nlags[i] = atoi(argv[argvIdx+1]);
    if( i==tgtIdx && delay[i] <= 0){
      delay[i]==1;
    }
    cout << "# ( " << delay[i] << "," << nlags[i] << " )" << endl;
  }
  int nAtt = 0;
  for( int i=0; i< nvar; i++){
    nAtt += nlags[i];
  }
  //
  cout << "# Reading Data" << endl;
  vector< ts_record > Records;
  if( GetRecords( ifile, Records ) != 1 ){
  errMsg << "ERROR: could not read records from " << argv[5] << "... aborting.\n";
    return( 0 );
  }
  ifile.close();
  cout << "# Read " << Records.size() << " records\n";
  //
  vector< vector< float > > normParam;
  string npfile = prefix + "-norm_param.dat";
  GetNormParam( npfile, normParam );
  //
  string pfile_name = prefix + "-ptron_predictor.out";
  adet_ptron model( pfile_name );
  //
  int last_written_idx = 0;
  bool write = false;
  int inc = 30;
  int timer=-2;
  int ErrCnt = 0;
  int ExampCnt = 0;
  timestamp lastOutput;
  for( int i=0; i < Records.size(); i++ ){
    ts_record newEx;
    int secDiff;
    if( i==0) lastOutput = Records[i].TS();
    // if record can be cleaned -- do so
    if( MakeLinearTimeExample( Records, i, tgtIdx-1, delay, nlags, newEx) == 1 ){
      //
      //vector< float > Example = newEx.Data();
      if( norm == 1 ) NormalizeExample( newEx.Data(), normParam );
      //
      vector< float > result = model.TestHelper( newEx.Data() );
      //
      if( norm == 1 ) UnnormalizeResult( result, normParam );
      //
      ExampCnt++;
      ts_record outputVal( Records[i] );
      if( result[4] == 0 ){
        ErrCnt++;
        Records[i].Data()[tgtIdx-1] = result[2];
        if( mitigation==1 ){
          outputVal.Data()[tgtIdx-1] = result[2];
        }
        else{
          outputVal.Data()[tgtIdx-1] = outputVal.NAFlag();
        }
      }
      outputVal.PrintCSV(1, cout );
    }
    //  otherwise just leave record uncleaned
    else{
      Records[i].PrintCSV(1, cout );
    }
  }
  cout << "# Number of Examples: " << ExampCnt << endl;
  cout << "# Number of Errors: " << ErrCnt << endl;
  return( 1 );
}