by David Pallmann via Fire and Ice: David Pallmann's Web and Cloud Blog on 4/25/2009 8:16:00 AM
#region Application-Specific Loader Code/// /// Your applications' Loader code goes here. /// Responsibilities:/// 1. Read input data necessary to create tasks with parameters from local resources./// 2. For each task generated, create a Task object with input parameters./// 3. Return an array of Task objects./// /// Job id/// Task[] arraypublic Task[] GenerateTasks(string jobId){ List tasks = new List(); Dictionary parameters = new Dictionary(); // TODO: implement Loader // Example task creation: parameters["Param1"] = "Value1"; parameters["Param2"] = "Value2"; parameters["Param3"] = "Value3"; tasks.Add(new Task(ProjectName, jobId, 1, "GridTask1", Task.Status.Pending, parameters, null)); return tasks.ToArray();}#endregion
using (TextReader reader = File.OpenText("FraudInput.csv")){ string[] names = null; string[] values = null; string line; int lineCount = 0; int nextTaskId = 1; // Read lines from CSV file until empty. Line 1 contains parameter names. while ((line = reader.ReadLine()) != null) { lineCount++; if (lineCount == 1) { // Reader header row of parameter names. names = line.Split(','); int n = 0; foreach (string name in names) names[n++] = name; } else { if (!String.IsNullOrEmpty(line)) { // Load latest values for this row and generate a task values = line.Split(','); parameters = new Dictionary(); for (int i = 0; i < names.Length; i++) parameters[names[i]] = String.Empty; for (int i = 0; i < values.Length; i++) parameters[names[i]] = values[i]; tasks.Add(new Task(ProjectName, jobId, nextTaskId++, "FraudScore", Task.Status.Pending, parameters, null)); } } }}
#region Application-specific Aggregator Code/// /// You application's Aggregator code goes here./// Responsibilities:/// 1. OpenStorage - open local storage./// 2. StoreResult - store a result./// 2. CloseStorage - close local storage./// /// protected override void OpenStorage(){ // TODO: open storage}protected override void StoreResult(string parametersXml, string resultsXml){ // TODO: store result}protected override void CloseStorage(){ // TODO: close storage}#endregion
<Parameters>=> <Parameter name="LastName" value="Bach"/> <Parameter name="FirstName" value="J.S."/> ...</Parameters><Results> <Result name="Score" value="700"/> <Result name="Approved" value="1"/> <Result name="Notes" value=" "/></Result>
#region Application-specific AggregatorCode/// /// You application's Aggregator code goes here./// Responsibilities:/// 1. OpenStorage - open local storage./// 2. StoreResult - store a result./// 2. CloseStorage - close local storage./// /// TextWriter tw = null;protected override void OpenStorage(){ tw = File.CreateText("FraudOutput.csv"); tw.WriteLine("Last Name,First Name,Score,Accepted,Notes"); tw.Flush();}protected override void StoreResult(string parametersXml, string resultsXml){ XElement parameters = XElement.Parse(parametersXml); XElement results = XElement.Parse(resultsXml); // Write values tw.WriteLine("{0},{1},{2},{3},\"{4}\"", parameters.XPathSelectElement("/Parameter[@name='LastName']").Attribute("value").Value, parameters.XPathSelectElement("/Parameter[@name='FirstName']").Attribute("value").Value, results.XPathSelectElement("/Result[@name='Score']").Attribute("value").Value, results.XPathSelectElement("/Result[@name='Accepted']").Attribute("value").Value, results.XPathSelectElement("/Result[@name='Notes']").Attribute("value").Value); tw.Flush();}protected override void CloseStorage(){ if (tw != null) tw.Close();}#endregion
#region Application Code/// /// Application code to execute a task. The switch statement uses the task's TaskType string to determine the appropriate code to execute./// /// The task to execute.public override void Execute(Task task){ switch (task.TaskType) { case "Task1": Task1(task); break; case "Task2": Task2(task); break; default: // Ignore unknown task. break; }}private void Task1(Task task){ // TODO: implement task1}private void Task2(Task task){ // TODO: implement task 2}#endregion End Application Code
public override void Execute(Task task){ switch (task.TaskType) case "FraudScore": FraudScore(task); break; default: // Ignore unknown task. break; }}
private void FraudScore(Task task){ StringBuilder notes = new StringBuilder(); bool rejected = false; int score = 1000; string firstName = task.Parameters["FirstName"]; string lastName = task.Parameters["LastName"]; string state = task.Parameters["State"]; string country = task.Parameters["Country"]; int age = Convert.ToInt32(task.Parameters["Age"]); string ssn = task.Parameters["SSN"]; string relation = task.Parameters["Relation"]; int monthsEmployed = Convert.ToInt32(task.Parameters["MonthsEmployed"]);
// Rule: if age < 18 or age > 100, automatic rejection if (age < 18 || age > 100) { score = 0; notes.Append("Age out of range, automatic rejection. "); rejected = true; } // Rule: is SSN missing, reduce score by 300. if (string.IsNullOrEmpty(ssn)) { score -= 300; notes.Append("SSN missing. "); }... // Check score. If below 500, reject application. if (score < 0) score = 0; if (!rejected && score < 500) { rejected = true; notes.Append("Score below 500, rejection. "); }
// Store task results. task.Results["Score"] = score.ToString(); task.Results["Notes"] = notes.ToString(); if (rejected) task.Results["Accepted"] = "0"; else task.Results["Accepted"] = "1";}
Original Post: Grid Computing on the Azure Cloud Computing Platform, Part 2: Developing a Grid Application
The content of the postings is owned by the respective author. AzureFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on AzureFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.