public static void Main(String[] args) { /* * USAGE:- * File adapter will be used to get the appropriate data from the different files and to provide it * in the format in which we can send to the ADK methods. * *************** PLEASE NOTE ALL DATA FILES TO BE KEPT INSIDE THE 'ConsoleApp2/bin/debug/' FOLDER for relative path purposes.*************** */ /* #################### For creating datastream and adding data ################ ####################The below code will create a datastream and post the historical data as a string to the ####################datastream. You will have to give the data as a string or give the fileName and pass it ####################to the getData() method of the file adapter. */ FileAdapter f = new FileAdapter(); ADKConnHist aDK = new ADKConnHist(); var datastreamId = aDK.CreateDataStream(); String fileName = "fileName"; var stream = f.GetData(fileName); String fileType = "csv"; // TODO: Uncomment the following line for json file. // fileType = "json"; aDK.IngestData(datastreamId, stream, fileType); // ############################################################################### /* #################### For creating datastream and adding data from a stream ################ ####################The below code will create a datastream and post the historical data as a stream from ####################the file to the datastream. You will have to give the fileName and pass it to the ####################getDataStream() method of the file adapter. */ FileAdapter f = new FileAdapter(); ADKConnHist aDK = new ADKConnHist(); var datastreamId = aDK.CreateDataStream(); String fileName = "fileName"; var stream = f.GetStream(fileName); String fileType = "csv"; // TODO: Uncomment the following line for json file. // fileType = "json"; aDK.IngestDataFromFile(datastreamId, stream, fileType); // ############################################################################################# /* #################### For live data input and output ################ ####################The below code will create a datastream and post the historical data as a stream from ####################the file to the datastream. You will have to give the fileName and pass it to the ####################getDataStream() method of the file adapter. ####################NOTE:- ####################1. Go on the Falkonry UI and build a model. ####################2. After building a model, run the code. ####################While using this method you can choose between IngestData(), IngestDataFromFile() and IngestDataFromFolder() ####################and set live parameter as true. */ FileAdapter f = new FileAdapter(); ADKConnLive aDK = new ADKConnLive(); String datastreamId = "datastreamId"; String assessmentId = "assessmentId"; String fileName = "fileName"; var stream = f.GetStream(fileName); String fileType = "csv"; // TODO: Uncomment the following line for json file. // fileType = "json"; Parallel.Invoke(() => { aDK.IngestDataFromFile(datastreamId, stream, fileType); }, () => { aDK.GetLiveOutput(assessmentId); }); // ######################################################################### /* #################### Example For creating datastream and adding data from a folder containing multiple files ################ #################### ####################The below code will create a datastream and post the historical data as a stream from ####################the folder containing multiple files to the datastream. You will have to give the folder path and pass it to the ####################addMoreHistoricalDataFromStream() in the adk connector */ ADKConnHist aDK = new ADKConnHist(); String folderPath = "folderPath"; String datastreamId = aDK.CreateDataStream(); aDK.IngestDataFromFolder(datastreamId, folderPath); // ######################################################################################################## /* #################### For adding facts data to an existing assessment. ################ ####################The code below will add facts to an existing assessment in the form of a string. ####################And for adding facts the model must be trained by the Falkonry UI. */ ADKConnHist aDK = new ADKConnHist(); FileAdapter f = new FileAdapter(); String fileName = "fileName"; String fileType = "csv"; String datastreamId = "datastreamID"; String assessmentId = "assessmentID"; // TODO: Uncomment the following line for json file. // fileType = "json"; String stream = f.GetData(fileName); aDK.IngestFactsData(datastreamId, assessmentId, stream, fileType); // ######################################################################################################## /* #################### For adding facts data from a stream to an existing assessment.################ ####################The code below will add facts to an existing assessment in the form of a stream. ####################And for adding facts the model must be trained by the Merlin. */ ADKConnHist aDK = new ADKConnHist(); FileAdapter f = new FileAdapter(); String fileName = "fileName"; String fileType = "csv"; String datastreamId = "datastreamID"; String assessmentId = "assessmentID"; // TODO: Uncomment the following line for json file. // fileType = "json"; byte[] stream = f.GetStream(fileName); aDK.IngestFactsFromFile(datastreamId, assessmentId, stream, fileType); // ######################################################################################################## /* #################### Exporting Facts ################ ####################This method will export the existing facts and then store them in a file named exportedFacts.json in the resources ####################folder. */ ADKConnHist aDK = new ADKConnHist(); String assessmentId = "assessmentID"; String datastreamId = "datastreamID"; aDK.ExportFacts(datastreamId, assessmentId); // ######################################################################################################## }
/* * This method is just an example to show how our ADK manages * multiple data files in a particular folder. It reads all * the files and passes each one of them as a stream to an existing * datastream. */ /// <summary> /// This method is just an example to show how our ADK manages /// multiple data files in a particular folder. It reads all the files /// and passes each one of them as a stream to an existing datastream. /// </summary> /// <exception cref="System.ArgumentNullException">Thrown when one parameter is missing /// in the function call.</exception> /// <exception cref="FalkonryClient.Service.FalkonryException">Thrown when there is a request timeout.</exception> /// <exception cref="System.Exception">Thrown when any other error causes /// the program to not add historical data.</exception> /// <param name="datastreamId">A String that contains the ID of existing datastream /// in which the historical data is supposed to be added.</param> /// <param name="folderPath">Complete folder path where the data files /// are stored</param> public void IngestDataFromFolder(String datastreamId, String folderPath) { if (datastreamId == null || folderPath == null) { throw new ArgumentNullException(nameof(datastreamId)); } String fileType; int i; FileAdapter fileAdapter = new FileAdapter(); Datastream datastream = falkonry.GetDatastream(datastreamId); var options = new SortedDictionary <string, string>(); var inputStatus = new InputStatus(); options.Add("streaming", "false"); options.Add("hasMoreData", "true"); options.Add("timeIdentifier", datastream.Field.Time.Identifier.ToString()); options.Add("timeZone", datastream.Field.Time.Zone.ToString()); options.Add("timeFormat", datastream.Field.Time.Format.ToString()); options.Add("entityIdentifier", datastream.Field.EntityIdentifier.ToString()); options.Add("fileFormat", "csv"); // TODO: Uncomment these 2 lines out for Narrow Datastream Format. //options.Add("signalIdentifier", datastream.Field.Signal.SignalIdentifier.ToString()); //options.Add("valueIdentifier", datastream.Field.Signal.ValueIdentifier.ToString()); // TODO: Uncomment this line out for Batch Datastream Format. //options.Add("batchIdentifier", datastream.Field.BatchIdentifier.ToString()); String[] Files = Directory.GetFiles(folderPath + "\\", "*.csv"); String[] Files2 = Directory.GetFiles(folderPath + "\\", "*.json"); Files = Files.Concat(Files2).ToArray(); var fileCount = Files.Length; foreach (String file in Files) { byte[] stream = fileAdapter.GetStream(file); fileType = System.IO.Path.GetExtension(file).ToString().Substring(1, System.IO.Path.GetExtension(file).ToString().Length - 1); options["fileFormat"] = fileType; if (fileCount == 1) { options["hasMoreData"] = "false"; } fileCount--; for (i = 0; i < 3; i++) { try { inputStatus = falkonry.AddInputStream(datastreamId, stream, options); if (CheckStatus(inputStatus.Id) == 1) { break; } } catch (FalkonryClient.Service.FalkonryException e) { log.Error(e.GetBaseException() + "\n"); Console.ReadKey(); Environment.Exit(1); } catch (Exception ex) { log.WarnFormat(ex.Message + "\n" + "Retry Attempt: {0}", i + 1); } } if (i == 3) { log.Error("Cannot add data to the datastream!"); Console.ReadKey(); Environment.Exit(1); } } }