/// <summary> /// Reiter’s HS-Tree Algorithm 3 /// </summary> /// <param name="paths">Already explored paths</param> /// <param name="newPathLabel">the newPathLabel to be explored</param> /// <returns>Flag indicating successful addition</returns> private bool CheckAndAddPath(List <HSTreePath> paths, HSTreePath newPathLabel) { if (!paths.Contains(newPathLabel)) { paths.Add(newPathLabel); return(true); } return(false); }
/// <summary> /// Reiter’s HS-Tree Algorithm 2 /// </summary> /// <param name="existingNode"></param> /// <param name="c"></param> /// <param name="diagnosisSet"></param> /// <param name="paths"></param> /// <param name="conflicts"></param> /// <param name="newNodes"></param> private void Expand(HSTreeNode existingNode, Gate c, DiagnosisSet diagnosisSet, List <HSTreePath> paths, ConflictSet conflicts, List <HSTreeNode> newNodes) { HSTreePath newPathLabel = new HSTreePath(existingNode.PathLabel, c); if (!HSHelper.IsSubsetOfPathDoesExitInDiagnosis(newPathLabel, diagnosisSet) && CheckAndAddPath(paths, newPathLabel)) { HSTreeNode node = new HSTreeNode(newPathLabel); Conflict S = HSHelper.IsAConflictExistConjWithPathLabelIsEmpty(conflicts, newPathLabel); if (S != null) { node.Conflict = S; } else { //consistency checker, which tests if the new node is a diagnosis or returns a minimal conflict otherwise. bool IsDiagnosis = ConstraintSystemSolver.Instance.CheckConsistensy(_observation, node.PathLabel.Path); //If its not a diagnosis we add it as a conflict if (!IsDiagnosis) { node.Conflict = new Conflict(node.PathLabel.Path); } } lock (_expendLock) { if (node.Conflict != null && node.Conflict.TheConflict.Count > 0) { // Add if not exist if (!newNodes.Any(e => e.CompareTo(node) == 0)) { newNodes.Add(node); } // Add if not exist if (!conflicts.Conflicts.Contains(node.Conflict)) { conflicts.Conflicts.Add(node.Conflict); } } else if (!HSHelper.IsSubsetOfPathDoesExitInDiagnosis(newPathLabel, diagnosisSet)) { Diagnosis diagnosis = new Diagnosis(node.PathLabel.Path); diagnosisSet.AddDiagnosis(diagnosis); } } } }
/// <summary> /// Reiter’s HS-Tree Algorithm 3 /// </summary> /// <param name="paths">Already explored paths</param> /// <param name="newPathLabel">the newPathLabel to be explored</param> /// <returns>Flag indicating successful addition</returns> private bool CheckAndAddPath(List <HSTreePath> paths, HSTreePath newPathLabel) { try { if (!paths.Contains(newPathLabel)) { paths.Add(newPathLabel); return(true); } } catch (Exception) { //Getiing index out of rangle when running multiple threads return(false); } return(false); }
/// <summary> /// Is Subset Of Path Does Exit In Diagnosis /// For example: /// path = {'a', 'b'} diagnosis.TheDiagnosis = {'a','b','c'} ===> Return true /// path = {'a', 'b'} diagnosis.TheDiagnosis = {'a','c','d'} ===> Return false /// </summary> /// <param name="newPathLabel"></param> /// <param name="diagnosisSet"></param> /// <returns></returns> public static bool IsSubsetOfPathDoesExitInDiagnosis(HSTreePath newPathLabel, DiagnosisSet diagnosisSet) { //Check if in any diagnosis: for (int j = 0; j < diagnosisSet.Diagnoses.Count; j++) { Diagnosis diagnosis = diagnosisSet.Diagnoses[j]; List <Gate> path = newPathLabel.Path; //Check if path is in diagnosis: if (!diagnosis.TheDiagnosis.Except(path).Any()) { return(true); } } return(false); }
/// <summary> /// Is A Conflict Exist Conj With "newPathLabel" Is Empty /// </summary> /// <param name="conflicts"></param> /// <param name="newPathLabel"></param> /// <returns> Return the conflict if found. If not found return null </returns> public static Conflict IsAConflictExistConjWithPathLabelIsEmpty(ConflictSet conflicts, HSTreePath newPathLabel) { List <Gate> pathLabelsGates = newPathLabel.Path; //Check if in any conflict: for (int j = 0; j < conflicts.Conflicts.Count; j++) { Conflict conflict = conflicts.Conflicts[j]; List <Gate> conflictGats = conflict.TheConflict; //Check if pathLabelsGates Conj (CHITOCH) conflictGats is empty bool intersect = conflictGats.Intersect(pathLabelsGates).Any(); if (!intersect) { return(conflict); } } return(null); }
/// <summary> /// Reiter’s HS-Tree Algorithm 2 /// </summary> /// <param name="existingNode"></param> /// <param name="c"></param> /// <param name="diagnosisSet"></param> /// <param name="paths"></param> /// <param name="conflicts"></param> /// <param name="newNodes"></param> private void Expand(HSTreeNode existingNode, Gate c, DiagnosisSet diagnosisSet, List <HSTreePath> paths, ConflictSet conflicts, List <HSTreeNode> newNodes) { HSTreePath newPathLabel = new HSTreePath(existingNode.PathLabel, c); if (!HSHelper.IsSubsetOfPathDoesExitInDiagnosis(newPathLabel, diagnosisSet) && CheckAndAddPath(paths, newPathLabel)) { HSTreeNode node = new HSTreeNode(newPathLabel); Conflict S = HSHelper.IsAConflictExistConjWithPathLabelIsEmpty(conflicts, newPathLabel); if (S != null) { node.Conflict = S; } else { //consistency checker, which tests if the new node is a diagnosis or returns a minimal conflict otherwise. bool IsDiagnosis = ConstraintSystemSolver.Instance.CheckConsistensy(_observation, node.PathLabel.Path); //If its not a diagnosis we add it as a conflict if (!IsDiagnosis) { node.Conflict = new Conflict(node.PathLabel.Path); } } lock (_expendLock) { if (node.Conflict != null && node.Conflict.TheConflict.Count > 0) { // Add if not exist if (!newNodes.Any(e => e.CompareTo(node) == 0)) { newNodes.Add(node); } // Add if not exist if (!conflicts.Conflicts.Contains(node.Conflict)) { conflicts.Conflicts.Add(node.Conflict); } } else if (!HSHelper.IsSubsetOfPathDoesExitInDiagnosis(newPathLabel, diagnosisSet)) { Diagnosis diagnosis = new Diagnosis(node.PathLabel.Path); diagnosisSet.AddDiagnosis(diagnosis); for (int i = 0; i < diagnosisSet.Diagnoses.Count; i++) { Diagnosis d = diagnosisSet.Diagnoses[i]; /// path = {'a', 'b'} diagnosis.TheDiagnosis = {'a','b','c'} ===> Return true /// path = {'a', 'b'} diagnosis.TheDiagnosis = {'a','c','d'} ===> Return false if (d.TheDiagnosis.Count <= newPathLabel.Path.Count) { continue; } if (d.TheDiagnosis.Except(newPathLabel.Path).Any()) { diagnosisSet.Diagnoses.RemoveAt(i); i--; } } } } } _waitEvent.Set(); }
public HSTreeNode(HSTreePath pathLabel) { PathLabel = pathLabel; }
public HSTreeNode(Conflict conflict) { Conflict = conflict; PathLabel = new HSTreePath(); }