Skip to content

Commit

Permalink
Refactor Parser interface names. (#18)
Browse files Browse the repository at this point in the history
* Renamed ILogFile to IParser and ILogFileParser to IParserFactory
PluginFramework loads factory instances at startup which improve performance.
Fixed spelling of Factories

* Cleaning of TestPlugin
  • Loading branch information
wolkesson authored Jan 12, 2018
1 parent d5c37b6 commit 0cd2849
Show file tree
Hide file tree
Showing 17 changed files with 292 additions and 219 deletions.
2 changes: 1 addition & 1 deletion PluginFramework/IAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace PluginFramework
{
public interface IAnalyzer
{
Dictionary<string, object> Analyze(ILogFile logfile);
Dictionary<string, object> Analyze(IParser logfile);
}
}
43 changes: 0 additions & 43 deletions PluginFramework/ILogFile.cs

This file was deleted.

23 changes: 0 additions & 23 deletions PluginFramework/ILogFileParser.cs

This file was deleted.

78 changes: 78 additions & 0 deletions PluginFramework/IParserFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;

namespace PluginFramework
{
/// <summary>
/// Interface for providing a log file parser.
/// </summary>
public interface IParserFactory
{
/// <summary>
/// Returns true if the specified file can be opened.
/// </summary>
/// Allows for further checks that just the filename, for example reading a header.
/// <param name="filename">the filename to check</param>
/// <returns>True if file can be loaded. False if loading is not possible.</returns>
bool CanOpen(string filename);

/// <summary>
/// Open the specified log file using the specified settings.
/// </summary>
/// <param name="filename">The filename to open</param>
/// <param name="settings">The setting to use when loading. Setting should be collected from user with the ShowSettings method.</param>
/// <returns>The loaded log file object.</returns>
IParser Open(string filename, ParserSettings settings = null);

/// <summary>
/// Show file load settings dialog to user
/// </summary>
/// <param name="filename">The filename to open</param>
/// <param name="settings">Key/value collection of settings. </param>
/// <returns>True if file should be loaded. False if loading should be aborted.</returns>
bool ShowSettingsDialog(string filename, ref ParserSettings settings);
}

/// <summary>
/// Parser for a log format
/// </summary>
public interface IParser
{
/// <summary>
/// List of signals provided by this parser
/// </summary>
SignalList Signals { get; }

/// <summary>
/// Read and return the provided signal
/// </summary>
/// <param name="signal">The signal to read</param>
/// <returns>The sample data points of the signal</returns>
Sample[] ReadSignal(Signal signal);

/// <summary>
/// The file origo time.
/// </summary>
DateTime Origo { get; }

/// <summary>
/// The duration of the data in the file
/// </summary>
TimeSpan Length { get; }
}

public struct Sample
{
public DateTime Time;
public double Value;
}

public struct Signal
{
public int UID;
public string Name;
public string FriendlyName;
public string Unit;
public string Description;
public string Tag;
}
}
2 changes: 1 addition & 1 deletion PluginFramework/IProjectViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface IProjectViewModel: INotifyPropertyChanged
event EventHandler FilesRealigned;

void AddLogFile(string filename);
void AddLogFile(string filename, ILogFileParser parser);
void AddLogFile(string filename, IParserFactory parser);
List<ISignalViewModel> FindSignalByTag(string tag);
ISignalViewModel GetSignal(string path);
IMarkerViewModel GetMarkerViewModel(IMarkerModel markerModel);
Expand Down
56 changes: 23 additions & 33 deletions PluginFramework/PluginFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public static class PluginFactory
static PluginFactory()
{
Info = new List<PluginInfo>();
Parsers = new List<Type>();
ParserFactories = new List<IParserFactory>();
Analyzers = new List<Type>();
PanelFactorys = new List<IPanelFactory>();
PanelFactories = new List<IPanelFactory>();
}

/// <summary>
Expand Down Expand Up @@ -48,9 +48,10 @@ public static List<Exception> LoadPlugins(string pluginPath)
{
foreach (var item in type.GetInterfaces())
{
if (item.Name == nameof(ILogFileParser) && !type.IsGenericType && !type.IsInterface)
if (item.Name == nameof(IParserFactory) && !type.IsGenericType && !type.IsInterface)
{
Parsers.Add(type);
IParserFactory factory = PluginFactory.CreateParserFactory(type);
ParserFactories.Add(factory);

ParserPluginAttribute attr = type.GetCustomAttribute<ParserPluginAttribute>(false);
if (attr != null)
Expand All @@ -67,7 +68,7 @@ public static List<Exception> LoadPlugins(string pluginPath)
else if (item.Name == nameof(IPanelFactory) && !type.IsGenericType && !type.IsInterface)
{
IPanelFactory factory = PluginFactory.CreatePanelFactory(type);
PanelFactorys.Add(factory);
PanelFactories.Add(factory);
info.AddItem(new PluginInfo.ItemInfo() { Name = factory.Title, Type = PluginInfo.ItemType.Display });
}
}
Expand All @@ -81,7 +82,7 @@ public static List<Exception> LoadPlugins(string pluginPath)
Exception exL = ex.LoaderExceptions[0];
if (exL is TypeLoadException)
{
exList.Add(new PluginLoadException("Plugin " + name + " could not load! Please contact plugin author.", exL));
exList.Add(new PluginLoadException("Plugin " + name + " could not load from " + pluginPath + "! Please contact plugin author.", exL));
}
else
{
Expand All @@ -107,15 +108,15 @@ public static List<Exception> LoadPlugins(string pluginPath)
public static void Reset()
{
Info.Clear();
Parsers.Clear();
ParserFactories.Clear();
Analyzers.Clear();
PanelFactorys.Clear();
PanelFactories.Clear();
}

private static List<Type> modelTypes = new List<Type>();
public static Type[] GetModelTypes()
{
var types = from item in PluginFactory.PanelFactorys select item.ModelType;
var types = from item in PluginFactory.PanelFactories select item.ModelType;
var list = types.ToList<Type>();
list.AddRange(modelTypes);
return list.ToArray<Type>();
Expand All @@ -127,18 +128,18 @@ public static void RegisterModelType(Type modelType)
}

public static List<PluginInfo> Info {get; private set;}
public static List<Type> Parsers { get; private set; }
public static List<IParserFactory> ParserFactories { get; private set; }
public static List<Type> Analyzers { get; private set; }
public static List<IPanelFactory> PanelFactorys { get; private set; }
public static List<IPanelFactory> PanelFactories { get; private set; }

public static string FileFilterString
{
get
{
List<string> fileFilter = new List<string>();
foreach (Type type in Parsers)
foreach (IParserFactory type in ParserFactories)
{
ParserPluginAttribute attr = type.GetCustomAttribute<ParserPluginAttribute>(false);
ParserPluginAttribute attr = type.GetType().GetCustomAttribute<ParserPluginAttribute>(false);
if (attr != null)
{
fileFilter.Add(attr.FileType);
Expand All @@ -151,9 +152,9 @@ public static string FileFilterString
}
}

public static ILogFileParser CreateLogFileParser(Type type)
public static IParserFactory CreateParserFactory(Type type)
{
ILogFileParser plugin = (ILogFileParser)Activator.CreateInstance(type);
IParserFactory plugin = (IParserFactory)Activator.CreateInstance(type);
return plugin;
}

Expand All @@ -169,23 +170,18 @@ public static IPanelFactory CreatePanelFactory(Type type)
return factory;
}

public static Type FindParser(string typename)
public static IParserFactory FindParser(string typename)
{
foreach (var item in Parsers)
{
if (item.FullName == typename)
return item;
}
var ret = from item in ParserFactories where item.GetType().FullName == typename select item;

return null;
return ret.FirstOrDefault();
}

public static ILogFileParser FindLogFileParser(string filename)
public static IParserFactory FindLogFileParser(string filename)
{
ILogFileParser suggestedLogFileParser = null;
foreach (Type pluginType in PluginFactory.Parsers)
IParserFactory suggestedLogFileParser = null;
foreach (IParserFactory tmpParser in PluginFactory.ParserFactories)
{
ILogFileParser tmpParser = PluginFactory.CreateLogFileParser(pluginType);
if (tmpParser.CanOpen(filename))
{
suggestedLogFileParser = tmpParser;
Expand Down Expand Up @@ -221,17 +217,11 @@ public static List<Type> FindAnalyzers(Type logfileParser)

public static IPanelFactory FindPanelFactory(IPanelModel model)
{
var ret = from item in PanelFactorys where item.GetType().FullName == model.FactoryReference select item;
var ret = from item in PanelFactories where item.GetType().FullName == model.FactoryReference select item;

return ret.FirstOrDefault();
}

public static ParserPluginAttribute GetPluginAttribute(string typename)
{
Type pluginType = FindParser(typename);
return pluginType.GetCustomAttribute<ParserPluginAttribute>(false);
}

private static Assembly MyInterceptMethod(object sender, ResolveEventArgs e)
{
string path = AppDomain.CurrentDomain.BaseDirectory;
Expand Down
4 changes: 2 additions & 2 deletions PluginFramework/PluginFramework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
<Compile Include="IDialogServiceExtcs.cs" />
<Compile Include="IFileModel.cs" />
<Compile Include="IFileViewModel.cs" />
<Compile Include="ILogFile.cs" />
<Compile Include="ILogFileParser.cs" />
<Compile Include="SignalList.cs" />
<Compile Include="IParserFactory.cs" />
<Compile Include="IMarkerModel.cs" />
<Compile Include="IMarkerViewModel.cs" />
<Compile Include="IPanel.cs" />
Expand Down
17 changes: 17 additions & 0 deletions PluginFramework/SignalList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace PluginFramework
{
using System.Collections.Generic;

public class SignalList:List<Signal>
{
public Signal GetSignalByName(string name)
{
return this.Find(p => p.Name == name);
}

public Signal GetSignalByUID(int uid)
{
return this.Find(p => p.UID == uid);
}
}
}
12 changes: 6 additions & 6 deletions Sample Crunch/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ private async void Window_Loaded(object sender, RoutedEventArgs e)
}

// Add local factories which will not be found because they are not in dll's.
PluginFactory.PanelFactorys.Add(PluginFactory.CreatePanelFactory(typeof(Factory.MarkerPanelFactory)));
PluginFactory.PanelFactorys.Add(PluginFactory.CreatePanelFactory(typeof(Factory.ProjectPanelFactory)));
PluginFactory.PanelFactories.Add(PluginFactory.CreatePanelFactory(typeof(Factory.MarkerPanelFactory)));
PluginFactory.PanelFactories.Add(PluginFactory.CreatePanelFactory(typeof(Factory.ProjectPanelFactory)));

MainViewModel.PropertyChanged += Main_PropertyChanged;

foreach (var item in PluginFactory.PanelFactorys)
foreach (var item in PluginFactory.PanelFactories)
{
// Add if it has no attribute set or if the attribute is set check the visible flag
PanelPluginAttribute attr = item.GetType().GetCustomAttribute<PanelPluginAttribute>(false);
Expand Down Expand Up @@ -139,9 +139,9 @@ private void SendConfigurationTelemetry()
StringBuilder sb = new StringBuilder();

// Log Parsers
foreach (Type parserType in PluginFactory.Parsers)
foreach (IParserFactory parser in PluginFactory.ParserFactories)
{
ParserPluginAttribute attr = parserType.GetCustomAttribute<ParserPluginAttribute>(false);
ParserPluginAttribute attr = parser.GetType().GetCustomAttribute<ParserPluginAttribute>(false);
if (attr != null)
{
sb.AppendFormat("{0} ({1}), ", attr.Title, attr.FileType);
Expand All @@ -151,7 +151,7 @@ private void SendConfigurationTelemetry()
sb.Clear();

// Log Panels
PluginFactory.PanelFactorys.ForEach((str) => { sb.AppendFormat("{0} ({1}), ", str.Title, str.ToString()); });
PluginFactory.PanelFactories.ForEach((str) => { sb.AppendFormat("{0} ({1}), ", str.Title, str.ToString()); });
props.Add("Panels", sb.ToString());
sb.Clear();

Expand Down
2 changes: 1 addition & 1 deletion Sample Crunch/ProjectPane.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private void treeView_DragOver(object sender, DragEventArgs e)
{
try
{
ILogFileParser lfp = PluginFactory.FindLogFileParser(filename);
IParserFactory lfp = PluginFactory.FindLogFileParser(filename);
if (lfp != null)
{
e.Effects = DragDropEffects.Copy;
Expand Down
Loading

0 comments on commit 0cd2849

Please sign in to comment.