Call us: +1 (716) 989 6531 or email at:

Forecasting Software for sales, demand and call volumes

RSS RSS

Navigation





Search the wiki
»

PoweredBy

Tutorial on Lokad SDK for .NET

RSS
Developers » SDK for.NET » here

Tutorial on Lokad SDK for .NET

Image
This is the tutorial on using Lokad SDK for .NET. We will create new .NET Console application that connects to Lokad forecasting services, uploads some mock data and retrieves the forecasts.


1. Register on the Lokad Sandbox server

Lokad SDK for .NET includes Lokad Sandbox (forecasting server that could be used for development and testing purposes). We'll use it in this tutorial.

You need to create account on this Sandbox server (it is fast and free, of course) in order to use Lokad API.

2. Download the latest SDK package

Latest release of SDK for .NET binaries could be downloaded from our integration server.

3. Create new Console Application project

Create Windows Console Application for .NET Framework 3.5. Then, you need to copy and reference Lokad.Sdk.dll from the downloaded archive in your project.

Tip: copying XML and PDB files along will supply documentation and debugging information for your IDE.

Note, that Lokad SDK is based on .NET 3.5. Yet, you can deliver solutions that work with .NET 2.0. In order to do that, just make sure that System.Core.dll from .NET 3.5 is in the same folder as Lokad libraries.

4. Create connection to Lokad APIv2

First you need to add Lokad.Api to the using declarations:

using Lokad.Api;

Then this code will obtain connector to the Sandbox server (you just need to provide credentials that you've got in step 1):

var login = "XXXXXX@lokad.com"; // your Sandbox login here
var pwd = "XXXXXX"; // your Sandbox password here

var service = ServiceFactory
  .ConnectToSandboxForTesting(login, pwd);

5. Upload Data

Further, we will create a couple of mock time series, populate them with some data and create a forecast for this data.

Note, that since Lokad needs every serie name to be unique, we will start our sample with cleaning up all our previously uploaded series:

var existingSeries = service.GetSeries();
service.DeleteSeries(existingSeries);

Then we'll create some mock time series and save them:

var serie1 = new SerieInfo { Name = "MySerie1" };
var serie2 = new SerieInfo { Name = "MySerie2" };

var mySeries = new[] {serie1, serie2};

Console.WriteLine("Saving series...");
service.AddSeries(mySeries);

Then we are creating some sample data and saving it to Lokad as well:

// add values
var value1 = new TimeValue
{
  Time = new DateTime(2008, 7, 1),
  Value = 10
};
var value2 = new TimeValue
{
  Time = new DateTime(2008, 7, 2),
  Value = 12
};
var value3 = new TimeValue
{
  Time = new DateTime(2008, 7, 3),
  Value = 11
};
// create association between serie1 and values 1,2,3
var segment1 = new SegmentForSerie(serie1, 
  new[] {value1, value2, value3});
// create association between serie2 and values 1,2
var segment2 = new SegmentForSerie(serie2, 
  new[] { value1, value2 });
			
Console.WriteLine("Saving values...");
service.UpdateSerieSegments(new[] {segment1, segment2});

Note, normally, instead of creating mock data, you would retrieve it from your database or any other data source that contains the data to be forecasted.

6. Create Forecasting task

Let's tell Lokad that we want to forecast the first serie:

// create new forecasting task
// to create 3 days forecast with daily interval
var task = new TaskInfo(serie1)
{
  FuturePeriods = 3,
  Period = Period.Day
};
			
Console.WriteLine("Saving Tasks...");
service.AddTasks(new[] { task });

7. Retrieve forecasts

In order to retrieve forecasts we just need to:

Console.WriteLine("Retrieving forecasts...");
var forecasts = service.GetForecasts(new [] { task});

And a bit more code to output the results to the console:

foreach (var forecast in forecasts)
{
  Console.WriteLine("Forecast for task {0}", forecast.TaskID);
  foreach (var value in forecast.Values)
  {
    Console.WriteLine("  {0} - {1}", 
      value.Time.ToShortDateString(), value.Value);
  }
}

Console.WriteLine("Press any key to continue");
Console.ReadKey(true);

8. End

Compile, build and run. You should see something like:

Console output

Congratulations! You've completed the tutorial.

What Next?

  • Check out Samples\Tutorial in the SDK package for source code for this tutorial
  • Lokad staff tries to answer all the questions raised in our forums.
  • You can check out our Lokad SDK project for advanced examples.

Note, that it is OK to have simple repeating numbers retrieved as forecast from the Sandbox server. That's because this server is designated for development and testing purposes and, thus, does not provide extensive data analysis.

After you are done with the app development and testing, you will need to switch to the Production server.

The procedure is similar to accessing the Sandbox server. You need to Create Account on the Lokad Production Server and then use the following code to retrieve the connector:

const string productionUrl = 
  "http://ws.lokad.com/TimeSeries2.asmx";
var lokadService = 
  ServiceFactory.GetConnector(login, pwd, productionUrl);

Remarks

Identifiers

Lokad APIv2 uses unique identifiers as keys for the objects during Create/Retrieve/Update/Delete operations. However, Lokad .NET SDK puts this behind the scenes and lets you manage higher level objects (i.e. SerieInfo) without worrying about the keys.

For example, when you add new SerieInfo objects to your Lokad account via the AddSeries method, all SerieID of each serie will be updated automatically. This will make it possible to use same objects in Update or Delete operations.

Lazy enumerators

There is an advanced feature in ILokadService hidden behind the Lazy property. It exposes interface containing methods that consume and/or return enumerators instead of the arrays. These enumerators are evaluated only when it is time to execute.

This gives you additional flexibility in creating applications that provide rich visual feedback while long-running operation are executed. For example, you can dynamically update your view with the aggregated forecast results as Lokad SDK for .NET retrieves pages of 10000 forecast results.

Note, that an additional care should be taken while handling any enumerators, since it is quite easy to get some undesired side effects. For example, in the code block below we will have two long-running requests to the database:

var series = service.Lazy.GetSeries();

foreach(var serie in series)
{
  // do something
}
foreach(var serie in series)
{
  // do something
}

It is recommended to use the regular methods, unless you specifically need to intercept the enumeration process.

Google Code

All add-ons developed by Lokad have been released as open source under the BSD license on Google Code.

The BSD license is compatible with closed-source commercial applications. Integrating a forecasting technology has never been easier.

What people say

From a developer standpoint, we found that the Lokad API was very well documented and easy to use. The Lokad team was also extremely responsive to any questions we had. Most importantly, we were able to develop a Lokad plugin for our data browser in very short order. Jeff Engel, Kirix Corporation