/// <summary> /// Compare two <see cref="IChemObject"/> classes and return the difference as an <see cref="IDifference"/>. /// </summary> /// <param name="first">the first of the two classes to compare</param> /// <param name="second">the second of the two classes to compare</param> /// <returns>an <see cref="IDifference"/> representation of the difference between the first and second <see cref="IChemObject"/>.</returns> public static IDifference Difference(IChemObject first, IChemObject second) { if (!(first is IAtomContainer && second is IAtomContainer)) { return(null); } var firstAC = (IAtomContainer)first; var secondAC = (IAtomContainer)second; var totalDiff = new ChemObjectDifference("AtomContainerDiff"); totalDiff.AddChild(IntegerDifference.Construct("atomCount", firstAC.Atoms.Count, secondAC.Atoms.Count)); if (firstAC.Atoms.Count == secondAC.Atoms.Count) { for (int i = 0; i < firstAC.Atoms.Count; i++) { totalDiff.AddChild(AtomDiff.Difference(firstAC.Atoms[i], secondAC.Atoms[i])); } } totalDiff.AddChild(IntegerDifference.Construct("electronContainerCount", firstAC.GetElectronContainers().Count(), secondAC.GetElectronContainers().Count())); if (firstAC.GetElectronContainers().Count() == secondAC.GetElectronContainers().Count()) { for (int i = 0; i < firstAC.GetElectronContainers().Count(); i++) { if (firstAC.GetElectronContainers().ElementAt(i) is IBond && secondAC.GetElectronContainers().ElementAt(i) is IBond) { totalDiff.AddChild(BondDiff.Difference(firstAC.GetElectronContainers().ElementAt(i), secondAC.GetElectronContainers().ElementAt(i))); } else if (firstAC.GetElectronContainers().ElementAt(i) is ILonePair && secondAC.GetElectronContainers().ElementAt(i) is ILonePair) { totalDiff.AddChild(LonePairDiff.Difference(firstAC.GetElectronContainers().ElementAt(i), secondAC.GetElectronContainers().ElementAt(i))); } else if (firstAC.GetElectronContainers().ElementAt(i) is ISingleElectron && secondAC.GetElectronContainers().ElementAt(i) is ISingleElectron) { totalDiff.AddChild(SingleElectronDiff.Difference(firstAC.GetElectronContainers().ElementAt(i), secondAC.GetElectronContainers().ElementAt(i))); } else { totalDiff.AddChild(ElectronContainerDiff.Difference(firstAC.GetElectronContainers().ElementAt(i), secondAC.GetElectronContainers().ElementAt(i))); } } } totalDiff.AddChild(ChemObjectDiff.Difference(first, second)); if (totalDiff.ChildCount() > 0) { return(totalDiff); } else { return(null); } }
public void TestDifference() { var m_carbon = new Mock <IAtom>(); IAtom carbon = m_carbon.Object; var m_oxygen = new Mock <IAtom>(); IAtom oxygen = m_oxygen.Object; m_carbon.SetupGet(n => n.Symbol).Returns("C"); m_oxygen.SetupGet(n => n.Symbol).Returns("O"); var m_bond1 = new Mock <ILonePair>(); ILonePair bond1 = m_bond1.Object; var m_bond2 = new Mock <ILonePair>(); ILonePair bond2 = m_bond2.Object; m_bond1.SetupGet(n => n.Atom).Returns(carbon); m_bond2.SetupGet(n => n.Atom).Returns(oxygen); IDifference difference = LonePairDiff.Difference(bond1, bond2); Assert.IsNotNull(difference); }