276 lines
8.0 KiB
C#
Executable File
276 lines
8.0 KiB
C#
Executable File
/* LaurisTimePoint.cs
|
|
* part of zaaReloaded2
|
|
*
|
|
* Copyright 2015-2017 Daniel Kraus
|
|
*
|
|
* 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;
|
|
using zaaReloaded2.LabModel;
|
|
|
|
namespace zaaReloaded2.Importer.ZaaImporter
|
|
{
|
|
/// <summary>
|
|
/// Holds all laboratory items for a given time point.
|
|
/// </summary>
|
|
class LaurisTimePoint : TimePoint
|
|
{
|
|
#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
|
|
|
|
#region Properties
|
|
|
|
/// <summary>
|
|
/// Gets an array of paragraphs in this LaurisText.
|
|
/// </summary>
|
|
public IList<String> Paragraphs
|
|
{
|
|
[DebuggerStepThrough]
|
|
get
|
|
{
|
|
if (_paragraphs == null)
|
|
{
|
|
_paragraphs = new List<String>();
|
|
}
|
|
return _paragraphs;
|
|
}
|
|
set
|
|
{
|
|
_paragraphs = value;
|
|
ParseParagraphs();
|
|
}
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
}
|
|
|
|
/// <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 },
|
|
StringSplitOptions.None).ToList();
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Constructors
|
|
|
|
public LaurisTimePoint() { }
|
|
|
|
public LaurisTimePoint(
|
|
string laurisText,
|
|
Parameters parameterDictionary,
|
|
Units unitDictionary)
|
|
: this()
|
|
{
|
|
_parameterDictionary = parameterDictionary;
|
|
_unitDictionary = unitDictionary;
|
|
LaurisText = laurisText;
|
|
}
|
|
|
|
public LaurisTimePoint(string laurisText)
|
|
: this(laurisText, null, null)
|
|
{ }
|
|
|
|
public LaurisTimePoint(
|
|
IList<String> paragraphs,
|
|
Parameters parameterDictionary,
|
|
Units unitDictionary)
|
|
: this(parameterDictionary, unitDictionary)
|
|
{
|
|
Paragraphs = paragraphs;
|
|
}
|
|
|
|
public LaurisTimePoint(IList<String> paragraphs)
|
|
: this(paragraphs, null, null)
|
|
{
|
|
}
|
|
|
|
public LaurisTimePoint(
|
|
Parameters parameterDictionary,
|
|
Units unitDictionary)
|
|
: this()
|
|
{
|
|
_parameterDictionary = parameterDictionary;
|
|
_unitDictionary = unitDictionary;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#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
|
|
|
|
#region Private methods
|
|
|
|
/// <summary>
|
|
/// Analyzes each Lauris paragraph in this time point, sets the date
|
|
/// and time, and collects LabItem data.
|
|
/// </summary>
|
|
void ParseParagraphs()
|
|
{
|
|
if (Paragraphs != null)
|
|
{
|
|
foreach (string paragraph in Paragraphs)
|
|
{
|
|
ParseParagraph(paragraph);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ParseParagraph(string paragraph)
|
|
{
|
|
Match m = _timeStampRegex.Match(paragraph);
|
|
if (m.Success)
|
|
{
|
|
Logger.Info("ParseParagraph: Matches time stamp");
|
|
DateTime dt;
|
|
if (DateTime.TryParseExact(
|
|
m.Groups["datetime"].Value,
|
|
"dd.MM.yyyy HH:mm",
|
|
CultureInfo.InvariantCulture,
|
|
DateTimeStyles.AllowWhiteSpaces,
|
|
out dt))
|
|
{
|
|
TimeStamp = dt;
|
|
}
|
|
|
|
// Put the remainder of the line back to the
|
|
// 'paragraph' parameter to deal with exceptional
|
|
// lines such as
|
|
// "(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)
|
|
{
|
|
Logger.Info("ParseParagraph: Time stamp has a tail -- putting it back with a dummy caption");
|
|
paragraph = "DUMMY CAPTION: " + m.Groups["tail"].Value;
|
|
}
|
|
else
|
|
{
|
|
paragraph = null;
|
|
}
|
|
}
|
|
|
|
if (!String.IsNullOrEmpty(paragraph))
|
|
{
|
|
Logger.Info("ParseParagraph: Not a time stamp");
|
|
LaurisParagraph lp = new LaurisParagraph(
|
|
paragraph,
|
|
_parameterDictionary,
|
|
_unitDictionary);
|
|
if (lp.IsLaurisParagraph)
|
|
{
|
|
Logger.Debug("ParseParagraph: Merging Lauris paragraph");
|
|
Items.Merge(lp.Items);
|
|
}
|
|
}
|
|
}
|
|
|
|
void AddItems(IItemDictionary items)
|
|
{
|
|
Items.Merge(items);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Private fields
|
|
|
|
/// <summary>
|
|
/// A regular expression that matches the time stamp in the first
|
|
/// paragraph of a LaurisText.
|
|
/// </summary>
|
|
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>.+)?)?$");
|
|
IList<String> _paragraphs;
|
|
Parameters _parameterDictionary;
|
|
Units _unitDictionary;
|
|
|
|
#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
|
|
}
|
|
}
|