//--------------------------------------------------------------------------------
//
// Copyright © The University of Queensland, 2012-2014. All rights reserved.
//
// License:
//--------------------------------------------------------------------------------
namespace Analyses
{
using System;
using System.Collections.Generic;
using System.Linq;
using Genomics;
using Shared;
///
/// Analysis executor interface
///
/// Type of the analysis to be executed.
/// Type of the command line arguments to be used.
public abstract class IAnalysisExecutor : ITaskExecutor
{
///
/// Gets the command line options
///
override public Dictionary Options
{
get
{
return this.OptionsData.ToDictionary(x => x.Key.ToString(), x => x.Value);
}
}
///
/// Gets the name of the analysis.
///
override public string Name
{
get
{
return typeof(TAnalysis).Name;
}
}
///
/// Gets the command line option descriptions.
///
protected abstract Dictionary OptionsData { get; }
///
/// Gets the optional string argument.
///
/// The optional string argument.
/// Arguments.
/// Argument.
protected string GetOptionalStringArg(Args args, TArgEnum arg)
{
return args.StringEnumArgs.ContainsKey(arg.ToString()) ?
args.StringEnumArgs[arg.ToString()] :
null;
}
///
/// Gets the optional int argument.
///
/// true, if optional int argument was gotten, false otherwise.
/// Arguments.
/// Argument.
/// The index.
protected bool GetOptionalIntArg(Args args, TArgEnum arg, ref int i)
{
if (args.StringEnumArgs.ContainsKey(arg.ToString()))
{
i = int.Parse(args.StringEnumArgs[arg.ToString()]);
return true;
}
return false;
}
///
/// Gets the enum argument.
///
/// The enum argument.
/// Arguments.
/// Argument.
/// The 1st type parameter.
protected TEnum GetEnumArg(Args args, TArgEnum arg)
{
return (TEnum)Enum.Parse(typeof(TEnum), args.StringEnumArgs[arg.ToString()]);
}
///
/// Reflects the string.
///
/// O.
/// Argument.
protected void ReflectString(object o, TArgEnum arg)
{
this.GetProperty(o, arg).SetValue(o, CommandArgs.StringEnumArgs[arg.ToString()]);
}
///
/// Reflects the string arguments.
///
/// Analysis.
/// Arguments.
protected void ReflectStringArgs(BaseAnalysis analysis, TArgEnum[] args)
{
var argEnumAsString = args.Select(x => x.ToString()).ToList();
var registeredArgs = analysis.ElementArgRegistry
.Where(x => argEnumAsString.Contains(x.Key)).ToList();
foreach (var arg in registeredArgs)
{
arg.Value(this.CommandArgs.StringArgs[arg.Key]);
}
var unregisteredArgs = args.Where(x => !analysis.ElementArgRegistry.ContainsKey(x.ToString())).ToArray();
this.ReflectStringArgs((object)analysis, unregisteredArgs);
}
///
/// Reflects the string arguments.
///
/// Analysis.
/// Arguments.
protected void ReflectOptionalStringArgs(BaseAnalysis analysis, TArgEnum[] args)
{
var argEnumAsString = args.Select(x => x.ToString()).ToList();
var registeredArgs = analysis.ElementArgRegistry
.Where(x => argEnumAsString.Contains(x.Key)).ToList();
foreach (var arg in registeredArgs)
{
if (this.CommandArgs.StringArgs.ContainsKey(arg.Key))
{
arg.Value(this.CommandArgs.StringArgs[arg.Key]);
}
}
var unregisteredArgs = args.Where(x => !analysis.ElementArgRegistry.ContainsKey(x.ToString())).ToArray();
this.ReflectOptionalStringArgs((object)analysis, unregisteredArgs);
}
///
/// Reflects the string arguments.
///
/// O.
/// Arguments.
protected void ReflectStringArgs(object o, TArgEnum[] args)
{
foreach (var arg in args)
{
this.ReflectString(o, arg);
}
}
///
/// Reflects the optional string.
///
/// O.
/// Argument.
/// optional default value.
protected void ReflectOptionalString(object o, TArgEnum arg, string defaultValue)
{
string value = GetOptionalStringArg(this.CommandArgs, arg);
if (value != null)
{
this.GetProperty(o, arg).SetValue(o, value);
}
else if (defaultValue != null)
{
this.GetProperty(o, arg).SetValue(o, defaultValue);
}
}
///
/// Reflects the optional int.
///
/// O.
/// Argument.
/// Default value.
protected void ReflectOptionalInt(object o, TArgEnum arg, int defaultValue)
{
int value = this.CommandArgs.StringEnumArgs.ContainsKey(arg.ToString()) ?
int.Parse(this.CommandArgs.StringEnumArgs[arg.ToString()]) :
defaultValue;
this.GetProperty(o, arg).SetValue(o, value);
}
///
/// Reflects the flag.
///
/// O.
/// Argument.
protected void ReflectFlag(object o, TArgEnum arg)
{
if (this.CommandArgs.Flags.Contains(arg.ToString()))
{
this.GetProperty(o, arg).SetValue(o, true);
}
}
///
/// Reflects the optional string arguments.
///
/// O.
/// Arguments.
protected void ReflectOptionalStringArgs(object o, TArgEnum[] args)
{
foreach (var arg in args)
{
this.ReflectOptionalString(o, arg, null);
}
}
///
/// Gets the property.
///
/// The property.
/// O.
/// Argument.
protected System.Reflection.PropertyInfo GetProperty(object o, TArgEnum arg)
{
System.Reflection.PropertyInfo prop = o.GetType().GetProperty(arg.ToString());
if (prop == null)
{
throw new Exception(string.Format("Missing property for argument {0}", arg.ToString()));
}
return prop;
}
}
}