Initially I intended to call a webservice, but it seems that all free weather webservice calls is something from the past. Yahoo offers weather information as an RSS feed. So a simple XPath query can select the information that I'm interested in:
Here is the code (note the FunctionAttribute decoration on the method)
1using System.IO;
2using System.Net;
3using System.Xml;
4using AcumenBusiness.IModel;
5usingType=AcumenBusiness.IModel.Type;
6
7namespace TestCallWebService
8 {
9 [Concept("Weather info", "Try to get weather forecast from different services")]
10publicclassWeatherInfoProvider
11 {
12privatestaticreadonlystring YahooWeatherURLFormat = "http://weather.yahooapis.com/forecastrss?p={0}";
13
14 [Term("workaround", "import function requires one field. (bug)")]
15privatestring dummyField;
16publicstring DummyField
17 {
18get { return dummyField; }
19set { dummyField = value; }
20 }
21
22 [Function("Get temperature", "Get the weather info from Yahoo",
23 ReturnType = Type.text,
24 ArgumentTypes = newType[] { Type.integer })]
25publicstaticstring GetWeatherInfoAsFeed(int zipCode)
26 {
27HttpWebRequest request = (HttpWebRequest) WebRequest.Create(string.Format(YahooWeatherURLFormat, zipCode));
28// execute the request
29HttpWebResponse response = (HttpWebResponse) request.GetResponse();
30
31// we will read data via the response stream
32Stream resStream = response.GetResponseStream();
33XmlDocument rssFeed = newXmlDocument();
34 rssFeed.Load(resStream);
35
36//set an XmlNamespaceManager since we have to make explicit namespace searches
37XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(rssFeed.NameTable);
38//Add the namespaces used in the xml doc to the XmlNamespaceManager.
39 xmlnsManager.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
40
41XmlNode weatherCondition = rssFeed.SelectSingleNode("/rss/channel/item/yweather:condition/@temp", xmlnsManager);
42if (weatherCondition != null) {
43return weatherCondition.Value;
44 }
45returnnull;
46 }
47 }
48 }
With the Import Schema adapter I can import this function and call it from my business rules. The coolest part is that you can invoke this from the Interactive Rule Map. So you can see how your rules are executing with this external data. It seems to be 76 fahrenheid in L.A.
Nice weather to jump on the bike and cycle to work...