private XmlElement DetermineBestModelForSelectedSignal(string sourceSignalName)
{
XmlElement bestMatch = null;
double bestRate = 0d;
List<string> sourceAttributes = new List<string>();
foreach (DataGridViewRow row in mappedSignalAttributes.Rows)
{
object val = row.Cells[2].Value;
if (val != null)
{
string source = val.ToString().ToLower().Replace( "-", " " ).Replace( "_", " " );
if ("voltage".Equals( source ))
{
if (mappedSignals.SelectedRows.Count > 0)
{
if (sourceSignalName.Contains( "DC" ))
source = "dc ampl";
else if (sourceSignalName.Contains( "AC" ))
source = "ac ampl";
else
source = "ampl";
}
}
sourceAttributes.Add( source );
}
}
XmlDocument model = signalModelLibrary.TreeModel;
XmlElement tsf = model.DocumentElement;
foreach (XmlNode tsfNode in tsf.ChildNodes)
{
XmlElement tsfLibrary = tsfNode as XmlElement;
if (tsfLibrary != null)
{
foreach (XmlNode signalNode in tsfLibrary.ChildNodes)
{
XmlElement signal = signalNode as XmlElement;
if (signal != null)
{
List<string> targetAttributes = new List<string>();
foreach (XmlAttribute attribute in signal.Attributes)
{
string name = attribute.Name.ToLower().Replace( "-", " " ).Replace( "_", " " );
if (!name.Equals( "id" ) && !name.Equals( "xmlns" ) && !name.Equals( "uuid" ))
targetAttributes.Add( name );
}
int totalMatches = 0;
foreach (string sourceAttribute in sourceAttributes)
{
if (targetAttributes.Contains( sourceAttribute ))
totalMatches += 1;
}
//Console.WriteLine( @"Library: {0} Signal: {1} matches {2} out of {3}", tsfLibrary.Name, signal.Name, totalMatches, sourceAttributes.Count );
if (sourceAttributes.Count > 0)
{
double rate = (double) totalMatches/(double) sourceAttributes.Count;
if (rate > bestRate)
{
bestRate = rate;
bestMatch = signal;
}
}
}
}
}
}
return bestMatch;
}