Ejemplo n.º 1
0
 public void Probing(Observation observation, DiagnosisSet diagnoses, List <Gate> RealFaulty)
 {
 }
Ejemplo n.º 2
0
        /// <summary>
        ///  If true its a diagnosis
        //   If false its a conflict
        /// </summary>
        /// <param name="observation"></param>
        /// <param name="posibleConflict"></param>
        /// <returns></returns>
        public bool CheckConsistensy(Observation observation, List <Gate> posibleConflict)
        {
            lock (Locker)
            {
                //Debug.WriteLine("SAT CheckConsistensy IN!");

                // Set broken gates
                foreach (Gate gate in posibleConflict)
                {
                    gate.IsNotHealthy = true;
                }

                /*
                 * // Add input constrain
                 * List<Wire> allInputWires = observation.TheModel.Input;
                 * foreach (Wire wire in allInputWires)
                 * {
                 * Solver.AddConstraints(wire.GetTerm());
                 * }
                 *
                 *
                 * // Add output constrain
                 * List<Wire> allOutputWires = observation.TheModel.Output;
                 * foreach (Wire wire in allOutputWires)
                 * {
                 * Solver.AddConstraints(wire.GetTerm());
                 * }
                 */

                // Add components constrain
                List <Gate> allSystemGates = observation.TheModel.Components;
                foreach (Gate gate in allSystemGates)
                {
                    gate.AddConstaint();
                }


                ConstraintSolverSolution solution;
                try
                {
                    solution = Solver.Solve();
                }
                catch (Exception)
                {
                    Reset();
                    solution = Solver.Solve();
                }



                // If true its a diagnosis
                // If false its a conflict
                bool explainOutput = solution.HasFoundSolution;

                if (!explainOutput)
                {
                    Debug.WriteLine("SAT Doesn't found a solution. The new node is N-O-T a diagnosis!");
                }

                //Reset
                instance = null;
                Solver.ResetSolver();
                ConstraintSystemSolver newSolver = ConstraintSystemSolver.Instance;
                wireTermsDictionary.Clear();

                //Revert broken
                foreach (Gate gate in posibleConflict)
                {
                    gate.IsNotHealthy = false;
                }

                //Debug.WriteLine("SAT CheckConsistensy OUT!");
                return(explainOutput);
            }
        }
Ejemplo n.º 3
0
        public List <Observation> ReadObsModelFiles(string fileModel, string fileObs) //path?
        {
            //reading model file
            List <Observation> observationsList = new List <Observation>();
            // if (observationsList.Count > 0)
            //   observationsList.Clear();
            FileStream   fs            = new FileStream(fileModel, FileMode.Open, FileAccess.Read);
            StreamReader reader        = new StreamReader(fs);
            string       model_allText = reader.ReadToEnd();

            fs.Close();
            reader.Close();
            fs     = null;
            reader = null;

            char[] delrow = new char[2];
            delrow[0] = '\n';
            delrow[1] = '\r';
            List <string> rows = model_allText.Split(delrow, StringSplitOptions.RemoveEmptyEntries).ToList();

            if (rows == null || rows.Count < 4)
            {
                return(null); // throw
            }
            //build Model
            SystemModel theModel;
            string      modelID       = rows[0].Substring(0, rows[0].Length - 1);
            List <Wire> inputs        = new List <Wire>();
            List <Wire> outputs       = new List <Wire>();
            List <Wire> internalWires = new List <Wire>();
            Dictionary <Wire.WireType, Dictionary <int, Wire> > wiresDictionary = new Dictionary <Wire.WireType, Dictionary <int, Wire> >();

            wiresDictionary.Add(Wire.WireType.i, new Dictionary <int, Wire>());
            wiresDictionary.Add(Wire.WireType.o, new Dictionary <int, Wire>());
            wiresDictionary.Add(Wire.WireType.z, new Dictionary <int, Wire>());
            //model id
            // if (!Int32.TryParse(rows[0].Substring(0, rows[0].Length - 1), out modelID))
            //   return; //throw

            char[] del = new char[6];
            del[0] = '.';
            del[1] = ',';
            del[2] = '[';
            del[3] = ']';
            del[4] = '(';
            del[5] = ')';

            //Wire.WiresDictionary = new Dictionary<string, Wire>();

            //input & output
            string[] inputArr  = rows[1].Split(del, StringSplitOptions.RemoveEmptyEntries);
            string[] outputArr = rows[2].Split(del, StringSplitOptions.RemoveEmptyEntries);


            for (int i = 0; i < inputArr.Length; i++)
            {
                //need to check if the Value is Valid? 2<=len<=3, starts with i/o/z, end with a number
                //need to check if theres as similar wire exist
                string wireName = inputArr[i];
                int    wid;
                if (Int32.TryParse(wireName.Substring(1), out wid))
                {
                    if (wireName.StartsWith("i"))
                    {
                        Wire w = new Wire(wid, Wire.WireType.i);
                        inputs.Add(w);
                        wiresDictionary[Wire.WireType.i].Add(wid, w);
                    }
                }
                //else --
            }
            for (int j = 0; j < outputArr.Length; j++)
            {
                string wireName = outputArr[j];
                int    wid;
                if (Int32.TryParse(wireName.Substring(1), out wid))
                {
                    if (wireName.StartsWith("o"))
                    {
                        Wire w = new Wire(wid, Wire.WireType.o);
                        outputs.Add(w);
                        wiresDictionary[Wire.WireType.o].Add(wid, w);
                    }
                }
            }
            theModel = new SystemModel(modelID, inputs, outputs);

            //creating components
            for (int i = 3; i < rows.Count; i++)
            {
                if (!String.IsNullOrEmpty(rows[i]))
                {
                    theModel.AddComponent(CreateComponent(rows[i].Split(del, StringSplitOptions.RemoveEmptyEntries), theModel, wiresDictionary));
                }
            }

            //sort model
            theModel.SortComponents();

            //reading observation fila
            delrow    = new char[1];
            delrow[0] = '.';
            del       = new char[7];
            del[0]    = '\r';
            del[1]    = ',';
            del[2]    = '[';
            del[3]    = ']';
            del[4]    = '(';
            del[5]    = ')';
            del[6]    = '\n';
            rows      = null;
            fs        = new FileStream(fileObs, FileMode.Open, FileAccess.Read);
            reader    = new StreamReader(fs);
            string ob_allText = reader.ReadToEnd();

            rows = ob_allText.Split(delrow, StringSplitOptions.RemoveEmptyEntries).ToList();
            fs.Close();
            reader.Close();
            fs     = null;
            reader = null;

            if (rows == null || rows.Count == 0)
            {
                return(null); // throw
            }
            //build observation
            // List<Observation> obList = new List<Observation>();
            for (int i = 0; i < rows.Count; i++)
            {
                string[] obArr = rows[i].Split(del, StringSplitOptions.RemoveEmptyEntries);
                if (obArr == null || obArr.Length == 0 && i != rows.Count - 1)
                {
                    return(null); //throw
                }
                if (obArr.Length == 2 + inputs.Count + outputs.Count)
                {
                    if (!obArr[0].Equals(modelID))
                    {
                        return(null); //throw
                    }
                    Observation toAdd = CreateObservation(obArr);
                    if (toAdd != null)
                    {
                        toAdd.TheModel = theModel; //try catch
                        observationsList.Add(toAdd);
                    }
                }
                //else return/throw?
            }
            return(observationsList);
        }