/// <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_atom1 = new Mock <IChemObject>(); IChemObject atom1 = m_atom1.Object; var m_atom2 = new Mock <IChemObject>(); IChemObject atom2 = m_atom2.Object; m_atom1.SetupGet(n => n.IsPlaced).Returns(false); m_atom1.SetupGet(n => n.IsVisited).Returns(false); m_atom2.SetupGet(n => n.IsPlaced).Returns(false); m_atom2.SetupGet(n => n.IsVisited).Returns(true); IDifference difference = ChemObjectDiff.Difference(atom1, atom2); Assert.IsNotNull(difference); }