Skip to main content
Skip table of contents

Code Examples


Code Examples for Data Time Series & Attributes

C#
using System;
using System.Collections.Generic;
using System.Linq;
using HAKOM.Framework.Components2016;
using HAKOM.Framework.Components2016.Attribute;
using HAKOM.Framework.Components2016.TimeSeries;
using Microsoft.VisualStudio.TestTools.UnitTesting;
 
namespace Examples.DOTNET_API
{
	[TestClass]
	public class DotNetApiTest
	{
		private const string CONNECTION_NAME = "ZAMS";
		private ITimeSeriesRepository tsRepository = RepositoryFactory.GetTimeSeriesRepository(CONNECTION_NAME, attributesEnabled: false);
		private ITimeSeriesAttributeRepository tsAttributeRepository = RepositoryFactory.GetTimeSeriesAttributeRepository(CONNECTION_NAME);
 
		/// <summary>
		/// 1. Create time series with data for one day
		/// 2. Search time series by name
		/// 3. Get data as saved in DB
		/// 4. Get data with specific interval
		/// 5. Get data with specific interval and unit
		/// 6. Delete time series
		/// </summary>
		[TestMethod]
		public void BasicApiTest()
		{
			ITimeSeriesEntity timeSeriesEntity = EntityFactory.CreateTimeSeriesEntity(tsRepository);
			timeSeriesEntity.Definition.Name = Guid.NewGuid().ToString();
			timeSeriesEntity.Definition.Table = "FWT_TSDATA";
			timeSeriesEntity.Definition.Interval = new Interval(TimeSeriesInterval.Hour);
			timeSeriesEntity.Definition.Type = TimeSeriesType.Begin;
			timeSeriesEntity.Definition.Unit = "kW";
 
			const int hours = 24;
			DateTime from = DateTime.Today;
			DateTime to = DateTime.Today.AddDays(1);
 
			TimeSeriesData[] expected = new TimeSeriesData[hours];
			for(int i = 0; i < expected.Length; i++)
			{
				expected[i] = new TimeSeriesData(
					new TimeSeriesDateTime(from.AddHours(i)),
					new TimeSeriesDateTime(from.AddHours(i + 1)),
					i + 1,
					TimeSeriesFlag.Valid
				);
			}
 
			timeSeriesEntity.SetData(Operation.Set, expected);
			tsRepository.Save(timeSeriesEntity);
 
			try
			{
				var period = new Period(expected.First().FromDate, expected.Last().ToDate);
				bool loadDefinitionAndDataInOneStep = true;
				ITimeSeriesEntity tsLoaded;
 
				if(loadDefinitionAndDataInOneStep)
				{
					// Load data from DB into memory
					tsLoaded = tsRepository.Load(timeSeriesEntity.Definition.ID, period);
				}
				else
				{
					tsLoaded = tsRepository.Search(timeSeriesEntity.Definition.Name);
 
					// Load data from DB into memory
					tsRepository.Load(tsLoaded, period);
				}
 
				// Get data as saved in DB
				TimeSeriesData[] actual = tsLoaded.GetData(period).ToArray();
 
				CollectionAssert.AreEqual(expected, actual);
 
				expected = new TimeSeriesData[] {
					new TimeSeriesData(
						new TimeSeriesDateTime(from),
						new TimeSeriesDateTime(to),
						12.5,
						TimeSeriesFlag.Valid
					)
				};
 
				// Get data with specific interval (Aggregation depends on unit. Default: Average.")
				actual = tsLoaded.GetData(period, new Interval(TimeSeriesInterval.Day)).ToArray();
 
				CollectionAssert.AreEqual(expected, actual);
 
				expected = new TimeSeriesData[] {
					new TimeSeriesData(
						new TimeSeriesDateTime(from),
						new TimeSeriesDateTime(to),
						300,
						TimeSeriesFlag.Valid
					)
				};
 
				// Get data with specific interval and unit. Unit conversion according to rules defined in DB.
				actual = tsLoaded.GetData(period, new Interval(TimeSeriesInterval.Day), new ExchangeUnit("kWh")).ToArray();
 
				CollectionAssert.AreEqual(expected, actual);
			}
			finally
			{
				tsRepository.Delete(timeSeriesEntity.Definition.ID);
			}
		}
 
		[TestMethod]
		public void AttributesTest()
		{
			// Create new text attribute
			IAttributeEntity attributeEntity = EntityFactory.CreateAttributeEntity(
				Guid.NewGuid().ToString(),
				AttributeType.Text
			);
 
			tsAttributeRepository.Save(attributeEntity);
 
			// Create new time series
			ITimeSeriesEntity timeSeriesEntity = EntityFactory.CreateTimeSeriesEntity(tsRepository);
			timeSeriesEntity.Definition.Name = Guid.NewGuid().ToString();
			timeSeriesEntity.Definition.Table = "FWT_TSDATA";
			timeSeriesEntity.Definition.Interval = new Interval(TimeSeriesInterval.Hour);
			timeSeriesEntity.Definition.Type = TimeSeriesType.Begin;
 
			tsRepository.Save(timeSeriesEntity);
 
			// Assign attribute to time series
			var assignedAttributes = new AttributeValuesEntity[] {
				new AttributeValuesEntity(
					attributeEntity.ID,
					new Dictionary<TimeSeriesDateTime, string>() {
						{ new TimeSeriesDateTime(2017, 1, 1), "foo" },
						{ new TimeSeriesDateTime(2018, 1, 1), "moo" }
					})
			};
 
			tsRepository.SaveAttributeValues(timeSeriesEntity.Definition.ID, assignedAttributes);
 
			try
			{
				// Search time series by attribute name
				var definitionFilter = new DefinitionFilter()
				{
					Attributes = new AttributeFilter[] {
						new AttributeFilter(attributeEntity.Name)
					}
				};
 
				var result = tsRepository.Search(definitionFilter).ToArray();
				Assert.AreEqual(1, result.Length);
 
				// Search time series by attribute name and value
				definitionFilter = new DefinitionFilter()
				{
					Attributes = new AttributeFilter[] {
						new AttributeFilter(attributeEntity.Name, "foo")
					}
				};
 
				result = tsRepository.Search(definitionFilter).ToArray();
				Assert.AreEqual(1, result.Length);
			}
			finally
			{
				tsRepository.Delete(timeSeriesEntity.Definition.ID);
				tsAttributeRepository.Delete(attributeEntity.Name);
			}
		}
 
		[TestMethod]
		public void AttributesSumTest()
		{
			// Create new text attribute
			IAttributeEntity attribute = EntityFactory.CreateAttributeEntity(
				Guid.NewGuid().ToString(),
				AttributeType.Text
			);
 
			tsAttributeRepository.Save(attribute);
 
			// Create new time series
			ITimeSeriesEntity timeSeries1 = EntityFactory.CreateTimeSeriesEntity(tsRepository);
			timeSeries1.Definition.Name = Guid.NewGuid().ToString();
			timeSeries1.Definition.Table = "FWT_TSDATA";
			timeSeries1.Definition.Interval = new Interval(TimeSeriesInterval.Hour);
			timeSeries1.Definition.Type = TimeSeriesType.Begin;
 
			const int hours = 24;
			DateTime from = DateTime.Today;
			DateTime to = DateTime.Today.AddDays(1);
 
			TimeSeriesData[] tsData = new TimeSeriesData[hours];
			for(int i = 0; i < tsData.Length; i++)
			{
				tsData[i] = new TimeSeriesData(
					new TimeSeriesDateTime(from.AddHours(i)),
					new TimeSeriesDateTime(from.AddHours(i + 1)),
					i + 1,
					TimeSeriesFlag.Valid
				);
			}
 
			timeSeries1.SetData(Operation.Set, tsData);
			tsRepository.Save(timeSeries1);
 
			// Create new time series
			ITimeSeriesEntity timeSeries2 = EntityFactory.CreateTimeSeriesEntity(tsRepository);
			timeSeries2.Definition.Name = Guid.NewGuid().ToString();
			timeSeries2.Definition.Table = "FWT_TSDATA";
			timeSeries2.Definition.Interval = new Interval(TimeSeriesInterval.Hour);
			timeSeries2.Definition.Type = TimeSeriesType.Begin;
 
			timeSeries2.SetData(Operation.Set, tsData);
			tsRepository.Save(timeSeries2);
 
			// Assign attribute to time series
			var assignedAttributes = new AttributeValuesEntity[] {
				new AttributeValuesEntity(
					attribute.ID,
					new Dictionary<TimeSeriesDateTime, string>() {
						{ TimeSeriesDateTime.MinValue, "foo" }
					})
			};
 
			tsRepository.SaveAttributeValues(timeSeries1.Definition.ID, assignedAttributes);
			tsRepository.SaveAttributeValues(timeSeries2.Definition.ID, assignedAttributes);
 
			try
			{
				var period = new Period(tsData.First().FromDate, tsData.Last().ToDate);
 
				var attributeFilter = new AttributeFilter[] {
					new AttributeFilter(attribute.Name)
				};
 
				var actual = tsRepository.Sum(attributeFilter, period, new Interval(TimeSeriesInterval.Hour)).ToArray();
				var expected = tsData.Select(d => new TimeSeriesData(d.FromDate, d.ToDate, d.Value * 2, d.Flag)).ToArray();
 
				CollectionAssert.AreEqual(expected, actual);
			}
			finally
			{
				tsRepository.Delete(timeSeries1.Definition.ID);
				tsRepository.Delete(timeSeries2.Definition.ID);
				tsAttributeRepository.Delete(attribute.Name);
			}
		}
 
		[TestMethod]
		public void UnitConversionTest()
		{
			ITimeSeriesEntity tsExchangeDollarToEuro = EntityFactory.CreateTimeSeriesEntity(tsRepository);
			ITimeSeriesEntity tsDollar = EntityFactory.CreateTimeSeriesEntity(tsRepository);
 
			try
			{
				tsExchangeDollarToEuro.Definition.Name = "USD_EUR_" + Guid.NewGuid().ToString();
				tsExchangeDollarToEuro.Definition.Table = "FWT_TSDATA";
				tsExchangeDollarToEuro.Definition.Interval = new Interval(TimeSeriesInterval.Day);
				tsExchangeDollarToEuro.Definition.Type = TimeSeriesType.Begin;
				tsExchangeDollarToEuro.Definition.Unit = "USD/EUR";
 
				TimeSeriesDateTime from = new TimeSeriesDateTime(DateTime.Today);
				TimeSeriesDateTime to = new TimeSeriesDateTime(DateTime.Today.AddDays(1));
				var period = new Period(from, to);
 
				tsExchangeDollarToEuro.SetData(Operation.Set, 1.1234, period);
				tsRepository.Save(tsExchangeDollarToEuro);
 
				tsDollar.Definition.Name = "USD_" + Guid.NewGuid().ToString();
				tsDollar.Definition.Table = "FWT_TSDATA";
				tsDollar.Definition.Interval = new Interval(TimeSeriesInterval.Day);
				tsDollar.Definition.Type = TimeSeriesType.Begin;
				tsDollar.Definition.Unit = "USD";
				tsDollar.Definition.ExchangeTimeSeries = new List<int>() { tsExchangeDollarToEuro.Definition.ID };
 
				tsDollar.SetData(Operation.Set, 10000, period);
				tsRepository.Save(tsDollar);
 
				TimeSeriesData[] actual = tsDollar.GetData(period, new Interval(TimeSeriesInterval.Day), new ExchangeUnit("EUR")).ToArray();
 
				TimeSeriesData[] expected = new TimeSeriesData[] {
					new TimeSeriesData(
						from,
						to,
						10000 / 1.1234,
						TimeSeriesFlag.Valid
					)
				};
 
				CollectionAssert.AreEqual(expected, actual);
			}
			finally
			{
				if(tsExchangeDollarToEuro.Definition.ID > 0)
					tsRepository.Delete(tsExchangeDollarToEuro.Definition.ID);
 
				if(tsDollar.Definition.ID > 0)
					tsRepository.Delete(tsDollar.Definition.ID);
			}
		}
	}
}

Code Examples for Formula Time Series

C#
using System;
using System.Collections.Generic;
using System.Linq;
using HAKOM.Framework.Components2016;
using HAKOM.Framework.Components2016.TimeSeries;
using Microsoft.VisualStudio.TestTools.UnitTesting;
 
namespace EvalComponentTest
{
	[TestClass]
	public class EvalComponentTest
	{
		private const string CONNECTION_NAME = "ZAMS";
		private ITimeSeriesRepository tsRepository = RepositoryFactory.GetTimeSeriesRepository(CONNECTION_NAME, attributesEnabled: false);
 
		[TestMethod]
		public void UnitConversionTest()
		{
			ITimeSeriesEntity dataTS = EntityFactory.CreateTimeSeriesEntity(tsRepository);
			dataTS.Definition.Name = Guid.NewGuid().ToString();
			dataTS.Definition.Table = "FWT_TSDATA";
			dataTS.Definition.Interval = new Interval(TimeSeriesInterval.Hour);
			dataTS.Definition.Type = TimeSeriesType.Begin;
			dataTS.Definition.Unit = "KW";
 
			DateTime from = DateTime.Today;
			DateTime to = DateTime.Today.AddDays(1);
 
			TimeSeriesData[] data = new TimeSeriesData[24];
			for(int i = 0; i < data.Length; i++)
			{
				data[i] = new TimeSeriesData(
					new TimeSeriesDateTime(from.AddHours(i)),
					new TimeSeriesDateTime(from.AddHours(i + 1)),
					i + 1,
					TimeSeriesFlag.Valid
				);
			}
 
			dataTS.SetData(Operation.Set, data);
			tsRepository.Save(dataTS);
 
			try
			{
				ITimeSeriesEntity formulaTS = EntityFactory.CreateTimeSeriesEntity(tsRepository);
				formulaTS.Definition.Formula = new Dictionary<TimeSeriesDateTime, string>() {
					{ TimeSeriesDateTime.MinValue, string.Format("[{0}]", dataTS.Definition.ID) }
				};
				formulaTS.Definition.Interval = new Interval(TimeSeriesInterval.Day);
				formulaTS.Definition.Type = TimeSeriesType.Begin;
				formulaTS.Definition.Unit = "KWh";
 
				var period = new Period(new TimeSeriesDateTime(from), new TimeSeriesDateTime(to));
				tsRepository.Load(formulaTS, period);
 
				// Default-Aggregation: Average
				TimeSeriesData[] actual = formulaTS.GetData(period).ToArray();
				TimeSeriesData[] expected = new TimeSeriesData[] {
					new TimeSeriesData(
						new TimeSeriesDateTime(from),
						new TimeSeriesDateTime(to),
						12.5,
						TimeSeriesFlag.Valid
					)
				};
 
				CollectionAssert.AreEqual(expected, actual);
 
				// Force unit conversion: KWh
				formulaTS.Definition.Formula = new Dictionary<TimeSeriesDateTime, string>() {
					{ TimeSeriesDateTime.MinValue, string.Format("[{0},,KWh]", dataTS.Definition.ID) }
				};
				tsRepository.Load(formulaTS, period);
				actual = formulaTS.GetData(period).ToArray();
 
				expected = new TimeSeriesData[] {
					new TimeSeriesData(
						new TimeSeriesDateTime(from),
						new TimeSeriesDateTime(to),
						300,
						TimeSeriesFlag.Valid
					)
				};
 
				CollectionAssert.AreEqual(expected, actual);
			}
			finally
			{
				tsRepository.Delete(dataTS.Definition.ID);
			}
		}
 
		[TestMethod]
		public void AggregationTest()
		{
			ITimeSeriesEntity dataTS = EntityFactory.CreateTimeSeriesEntity(tsRepository);
			dataTS.Definition.Name = Guid.NewGuid().ToString();
			dataTS.Definition.Table = "FWT_TSDATA";
			dataTS.Definition.Interval = new Interval(TimeSeriesInterval.Hour);
			dataTS.Definition.Type = TimeSeriesType.Begin;
 
			DateTime from = DateTime.Today;
			DateTime to = DateTime.Today.AddDays(1);
 
			TimeSeriesData[] data = new TimeSeriesData[24];
			for(int i = 0; i < data.Length; i++)
			{
				data[i] = new TimeSeriesData(
					new TimeSeriesDateTime(from.AddHours(i)),
					new TimeSeriesDateTime(from.AddHours(i + 1)),
					i + 1,
					TimeSeriesFlag.Valid
				);
			}
 
			dataTS.SetData(Operation.Set, data);
			tsRepository.Save(dataTS);
 
			try
			{
				ITimeSeriesEntity formulaTS = EntityFactory.CreateTimeSeriesEntity(tsRepository);
				formulaTS.Definition.Formula = new Dictionary<TimeSeriesDateTime, string>() {
					{ TimeSeriesDateTime.MinValue, string.Format("[{0}]", dataTS.Definition.ID) }
				};
				formulaTS.Definition.Interval = new Interval(TimeSeriesInterval.Day);
				formulaTS.Definition.Type = TimeSeriesType.Begin;
 
				var period = new Period(new TimeSeriesDateTime(from), new TimeSeriesDateTime(to));
				tsRepository.Load(formulaTS, period);
 
				// Default-Aggregation: Average
				TimeSeriesData[] actual = formulaTS.GetData(period).ToArray();
				TimeSeriesData[] expected = new TimeSeriesData[] {
					new TimeSeriesData(
						new TimeSeriesDateTime(from),
						new TimeSeriesDateTime(to),
						12.5,
						TimeSeriesFlag.Valid
					)
				};
 
				CollectionAssert.AreEqual(expected, actual);
 
				// Force Aggregation: Sum
				formulaTS.Definition.Formula = new Dictionary<TimeSeriesDateTime, string>() {
					{ TimeSeriesDateTime.MinValue, string.Format("[{0},,,Sum]", dataTS.Definition.ID) }
				};
 
				tsRepository.Load(formulaTS, period);
				actual = formulaTS.GetData(period).ToArray();
 
				expected = new TimeSeriesData[] {
					new TimeSeriesData(
						new TimeSeriesDateTime(from),
						new TimeSeriesDateTime(to),
						300,
						TimeSeriesFlag.Valid
					)
				};
 
				CollectionAssert.AreEqual(expected, actual);
			}
			finally
			{
				tsRepository.Delete(dataTS.Definition.ID);
			}
		}
 
		[TestMethod]
		public void OffsetTest_Lead()
		{
			ITimeSeriesEntity dataTS = EntityFactory.CreateTimeSeriesEntity(tsRepository);
			dataTS.Definition.Name = Guid.NewGuid().ToString();
			dataTS.Definition.Table = "FWT_TSDATA";
			dataTS.Definition.Interval = new Interval(TimeSeriesInterval.Day);
			dataTS.Definition.Type = TimeSeriesType.Begin;
 
			DateTime today = DateTime.Today;
 
			TimeSeriesData[] data = new TimeSeriesData[5];
			for(int i = 0; i < data.Length; i++)
			{
				data[i] = new TimeSeriesData(
					new TimeSeriesDateTime(today.AddDays(i)),
					new TimeSeriesDateTime(today.AddDays(i + 1)),
					i + 1,
					TimeSeriesFlag.Valid
				);
			}
 
			dataTS.SetData(Operation.Set, data);
			tsRepository.Save(dataTS);
 
			try
			{
				ITimeSeriesEntity formulaTS = EntityFactory.CreateTimeSeriesEntity(tsRepository);
				formulaTS.Definition.Formula = new Dictionary<TimeSeriesDateTime, string>() {
					{ TimeSeriesDateTime.MinValue, string.Format("Lead([{0}, 1], 1)", dataTS.Definition.ID) }
				};
 
				formulaTS.Definition.Interval = new Interval(TimeSeriesInterval.Day);
				formulaTS.Definition.Type = TimeSeriesType.Begin;
 
				DateTime from = today.AddDays(1);
				DateTime to = today.AddDays(4);
				var period = new Period(new TimeSeriesDateTime(from), new TimeSeriesDateTime(to));
				tsRepository.Load(formulaTS, period);
 
				TimeSeriesData[] actual = formulaTS.GetData(period).ToArray();
				TimeSeriesData[] expected = data
					.Skip(1)
					.Take(3)
					.Select(d => new TimeSeriesData(d.FromDate, d.ToDate, d.Value + 1, d.Flag))
					.ToArray();
 
				CollectionAssert.AreEqual(expected, actual);
			}
			finally
			{
				tsRepository.Delete(dataTS.Definition.ID);
			}
		}
 
		[TestMethod]
		public void OffsetTest_Lag()
		{
			ITimeSeriesEntity dataTS = EntityFactory.CreateTimeSeriesEntity(tsRepository);
			dataTS.Definition.Name = Guid.NewGuid().ToString();
			dataTS.Definition.Table = "FWT_TSDATA";
			dataTS.Definition.Interval = new Interval(TimeSeriesInterval.Day);
			dataTS.Definition.Type = TimeSeriesType.Begin;
 
			DateTime today = DateTime.Today;
 
			TimeSeriesData[] data = new TimeSeriesData[5];
			for(int i = 0; i < data.Length; i++)
			{
				data[i] = new TimeSeriesData(
					new TimeSeriesDateTime(today.AddDays(i)),
					new TimeSeriesDateTime(today.AddDays(i + 1)),
					i + 1,
					TimeSeriesFlag.Valid
				);
			}
 
			dataTS.SetData(Operation.Set, data);
			tsRepository.Save(dataTS);
 
			try
			{
				ITimeSeriesEntity formulaTS = EntityFactory.CreateTimeSeriesEntity(tsRepository);
				formulaTS.Definition.Formula = new Dictionary<TimeSeriesDateTime, string>() {
					{ TimeSeriesDateTime.MinValue, string.Format("Lag([{0}, 1], 1)", dataTS.Definition.ID) }
				};
				formulaTS.Definition.Interval = new Interval(TimeSeriesInterval.Day);
				formulaTS.Definition.Type = TimeSeriesType.Begin;
 
				DateTime from = today.AddDays(1);
				DateTime to = today.AddDays(4);
				var period = new Period(new TimeSeriesDateTime(from), new TimeSeriesDateTime(to));
				tsRepository.Load(formulaTS, period);
 
				TimeSeriesData[] actual = formulaTS.GetData(period).ToArray();
				TimeSeriesData[] expected = data
					.Skip(1)
					.Take(3)
					.Select(d => new TimeSeriesData(d.FromDate, d.ToDate, d.Value - 1, d.Flag))
					.ToArray();
 
				CollectionAssert.AreEqual(expected, actual);
			}
			finally
			{
				tsRepository.Delete(dataTS.Definition.ID);
			}
		}
	}
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.