zaaReloaded2/zaaReloaded2/Importer/ZaaImporter/LaurisTimePoint.cs

276 lines
8.0 KiB
C#
Raw Normal View History

2015-07-06 13:48:43 +00:00
/* LaurisTimePoint.cs
2015-07-04 10:09:39 +00:00
* part of zaaReloaded2
*
2017-02-23 15:44:07 +00:00
* Copyright 2015-2017 Daniel Kraus
2015-07-04 10:09:39 +00:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using zaaReloaded2.Thesaurus;
2015-07-06 13:48:43 +00:00
using zaaReloaded2.LabModel;
2015-07-04 10:09:39 +00:00
2015-07-06 13:48:43 +00:00
namespace zaaReloaded2.Importer.ZaaImporter
2015-07-04 10:09:39 +00:00
{
/// <summary>
2015-07-06 13:48:43 +00:00
/// Holds all laboratory items for a given time point.
2015-07-04 10:09:39 +00:00
/// </summary>
2015-07-06 13:48:43 +00:00
class LaurisTimePoint : TimePoint
2015-07-04 10:09:39 +00:00
{
2015-07-11 14:51:02 +00:00
#region Static methods
/// <summary>
/// Examines a string and returns true if it resembles
/// a time stamp line in the Lauris output.
/// </summary>
/// <param name="line">Line to examine.</param>
/// <returns>True if line resembles a time stamp line
/// in the Lauris output.</returns>
static public bool IsTimeStampLine(string line)
{
return _timeStampRegex.IsMatch(line);
}
/// <summary>
/// Gets a Regex object that matches a Lauris time stamp
/// line.
/// </summary>
static public Regex TimeStampRegex
{
get
{
return _timeStampRegex;
}
}
#endregion
2015-07-04 10:09:39 +00:00
#region Properties
2015-07-06 13:48:43 +00:00
/// <summary>
/// Gets an array of paragraphs in this LaurisText.
/// </summary>
2015-08-04 21:21:38 +00:00
public IList<String> Paragraphs
2015-07-11 14:51:02 +00:00
{
[DebuggerStepThrough]
get
{
2015-08-04 21:21:38 +00:00
if (_paragraphs == null)
{
_paragraphs = new List<String>();
}
2015-07-11 14:51:02 +00:00
return _paragraphs;
}
set
{
_paragraphs = value;
ParseParagraphs();
}
}
2015-07-06 13:48:43 +00:00
/// <summary>
/// Is true if the LaurisText has time stamp in the first
/// paragraph and <see cref="LabItem"/>s in the others.
/// </summary>
public bool IsValidTimePoint
{
get
{
return Items.Count > 0;
}
}
2015-07-06 13:48:43 +00:00
2015-07-04 10:09:39 +00:00
/// <summary>
/// Gets or sets the original Lauris text for this timepoint.
/// </summary>
public string LaurisText
{
[DebuggerStepThrough]
get
{
return String.Join(Environment.NewLine, Paragraphs);
}
set
{
if (!String.IsNullOrEmpty(value))
{
Paragraphs = value.Split(
new string[] { Environment.NewLine },
2015-08-04 21:21:38 +00:00
StringSplitOptions.None).ToList();
2015-07-04 10:09:39 +00:00
}
}
}
#endregion
#region Constructors
2015-07-06 13:48:43 +00:00
public LaurisTimePoint() { }
2015-07-04 10:09:39 +00:00
2015-07-11 14:51:02 +00:00
public LaurisTimePoint(
string laurisText,
Parameters parameterDictionary,
Units unitDictionary)
2015-07-04 10:09:39 +00:00
: this()
{
2015-07-11 14:51:02 +00:00
_parameterDictionary = parameterDictionary;
_unitDictionary = unitDictionary;
LaurisText = laurisText;
2015-07-04 10:09:39 +00:00
}
2015-07-11 14:51:02 +00:00
public LaurisTimePoint(string laurisText)
: this(laurisText, null, null)
{ }
2015-07-06 13:48:43 +00:00
public LaurisTimePoint(
2015-08-04 21:21:38 +00:00
IList<String> paragraphs,
Parameters parameterDictionary,
Units unitDictionary)
: this(parameterDictionary, unitDictionary)
2015-07-04 10:09:39 +00:00
{
2015-07-11 14:51:02 +00:00
Paragraphs = paragraphs;
}
2015-08-04 21:21:38 +00:00
public LaurisTimePoint(IList<String> paragraphs)
2015-07-11 14:51:02 +00:00
: this(paragraphs, null, null)
{
2015-07-04 10:09:39 +00:00
}
public LaurisTimePoint(
Parameters parameterDictionary,
Units unitDictionary)
: this()
{
_parameterDictionary = parameterDictionary;
_unitDictionary = unitDictionary;
}
2015-07-04 10:09:39 +00:00
#endregion
2015-08-04 21:21:38 +00:00
#region Public methods
/// <summary>
/// Adds a new paragraph to this time point by parsing
/// the paragraph for laboratory items.
/// </summary>
/// <param name="paragraph">Paragraph to add.</param>
public void AddParagraph(string paragraph)
{
Paragraphs.Add(paragraph);
ParseParagraph(paragraph);
}
#endregion
2015-07-04 10:09:39 +00:00
#region Private methods
/// <summary>
/// Analyzes each Lauris paragraph in this time point, sets the date
/// and time, and collects LabItem data.
/// </summary>
2015-08-04 21:21:38 +00:00
void ParseParagraphs()
2015-07-04 10:09:39 +00:00
{
2015-08-04 21:21:38 +00:00
if (Paragraphs != null)
2015-07-04 10:09:39 +00:00
{
2015-08-04 21:21:38 +00:00
foreach (string paragraph in Paragraphs)
2015-07-04 10:09:39 +00:00
{
2015-08-04 21:21:38 +00:00
ParseParagraph(paragraph);
2015-07-04 10:09:39 +00:00
}
}
}
2015-08-04 21:21:38 +00:00
void ParseParagraph(string paragraph)
2015-07-04 10:09:39 +00:00
{
Match m = _timeStampRegex.Match(paragraph);
if (m.Success)
2015-07-04 10:09:39 +00:00
{
Logger.Info("ParseParagraph: Matches time stamp");
2015-07-04 10:09:39 +00:00
DateTime dt;
2015-08-04 21:21:38 +00:00
if (DateTime.TryParseExact(
m.Groups["datetime"].Value,
2015-07-04 10:09:39 +00:00
"dd.MM.yyyy HH:mm",
CultureInfo.InvariantCulture,
DateTimeStyles.AllowWhiteSpaces,
2015-08-04 21:21:38 +00:00
out dt))
{
TimeStamp = dt;
}
// Put the remainder of the line back to the
// 'paragraph' parameter to deal with exceptional
// lines such as
2017-02-23 15:44:07 +00:00
// "(17.09.2015-201710:44:00) Cyclosporin-A vor Gabe: 130 µg/l;"
// We need to add a dummy caption because this is
// the normal format of a paragraph generated from Lauris.
if (m.Groups["tail"].Success)
{
2016-10-03 15:12:47 +00:00
Logger.Info("ParseParagraph: Time stamp has a tail -- putting it back with a dummy caption");
paragraph = "DUMMY CAPTION: " + m.Groups["tail"].Value;
}
else
{
2016-10-03 15:12:47 +00:00
paragraph = null;
}
2015-08-04 21:21:38 +00:00
}
if (!String.IsNullOrEmpty(paragraph))
2015-08-04 21:21:38 +00:00
{
Logger.Info("ParseParagraph: Not a time stamp");
2015-08-04 21:21:38 +00:00
LaurisParagraph lp = new LaurisParagraph(
paragraph,
_parameterDictionary,
_unitDictionary);
if (lp.IsLaurisParagraph)
{
Logger.Debug("ParseParagraph: Merging Lauris paragraph");
2015-08-04 21:21:38 +00:00
Items.Merge(lp.Items);
}
2015-07-04 10:09:39 +00:00
}
}
void AddItems(IItemDictionary items)
{
2015-07-11 14:51:02 +00:00
Items.Merge(items);
2015-07-04 10:09:39 +00:00
}
#endregion
#region Private fields
/// <summary>
/// A regular expression that matches the time stamp in the first
/// paragraph of a LaurisText.
/// </summary>
2015-07-11 14:51:02 +00:00
static readonly Regex _timeStampRegex = new Regex(
@"^(Labor:?)?\s*\(?\s*(?<datetime>\d\d\.\d\d\.\d\d\d\d\s+\d\d:\d\d)(:00\)\s*(?<tail>.+)?)?$");
2015-08-04 21:21:38 +00:00
IList<String> _paragraphs;
Parameters _parameterDictionary;
Units _unitDictionary;
2015-07-04 10:09:39 +00:00
#endregion
#region Class logger
private static NLog.Logger Logger { get { return _logger.Value; } }
private static readonly Lazy<NLog.Logger> _logger = new Lazy<NLog.Logger>(() => NLog.LogManager.GetCurrentClassLogger());
#endregion
2015-07-04 10:09:39 +00:00
}
}