void SaveToXML()
        {
            RoboSimulation rs = new RoboSimulation();

            bool valid = ValidateRoboSimulation(rs);

            if (valid)
            {
                string filePath = Server.MapPath("~/resources/Generated/" + rs.simulation_name + " " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".xml");
                utilities.InsertRoboSimulationToXML(rs, filePath);
                //   LabelValidationError.Text = "Данните бяха добавени успешно в ХМЛ документ! :)";
                //   LabelValidationError.ForeColor = System.Drawing.ColorTranslator.FromHtml("#00CC00");
            }
        }
예제 #2
0
        /*inserts xml files' data into the DB*/
        public void InsertXmlDataToDB(string dir, ref string statusText)
        {
            XMLRoboSimulationProcessor processor = new XMLRoboSimulationProcessor();

            string[] files = Directory.GetFiles(dir, "*.xml", SearchOption.AllDirectories);
            foreach (string file in files)
            {
                try
                {
                    processor.validateDTDFile(file); // IF the file is not valid it throw exeption
                    processor.LoadRoboSimulationFromXMLUsingReader(file);
                    RoboSimulation roboSimulation = processor.RoboSimulation;
                    InsertRoboSimulationToDB(roboSimulation);
                }
                catch (XMLRoboSimulationProcessorException pe)
                {
                    statusText += pe.Message;
                }
            }
        }
        // If all fields are correct-> save the data to the database
        protected void ButtonSaveToDBAndXML_Click(object sender, EventArgs e)
        {
            RoboSimulation rs = new RoboSimulation();

            bool valid = ValidateRoboSimulation(rs);

            if (valid)
            {
                utilities.InsertRoboSimulationToDB(rs);
                LabelValidationStatus.Text      = "Данните бяха успешно записани! :)";
                LabelValidationStatus.ForeColor = System.Drawing.ColorTranslator.FromHtml("#00CC00");
            }
            else
            {
                LabelValidationStatus.Text      = "Грешка: Данните не са записани, има невалидно попълнени полета!";
                LabelValidationStatus.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF5050");
            }

            SaveToXML();
        }
예제 #4
0
 /*insetrs object's data into the DB*/
 public void InsertRoboSimulationToDB(RoboSimulation roboSimulation)
 {
     entities.RoboSimulations.AddObject(roboSimulation);
     entities.SaveChanges();
     // If here is some error - try to stop SQL server(from SQL manager) and start it again...
 }
예제 #5
0
        public void InsertRoboSimulationToXML(RoboSimulation rs, string filePath)
        {
            XDocument roboSimulationXml = new XDocument(new XDocumentType("RoboSimulation", null, "RoboSim.dtd", null));
            XElement  xRoot             = new XElement("RoboSimulation");

            // Simulation attributes and elements(not nested once)
            xRoot.SetAttributeValue("id", "S" + rs.id.ToString());
            xRoot.SetAttributeValue("name", rs.simulation_name);
            xRoot.SetAttributeValue("owner", rs.simulation_owner);
            XElement xRootChildElement = new XElement("SimulationOwnerEmail", rs.simulation_owner_email);

            xRoot.Add(xRootChildElement);
            xRootChildElement = new XElement("SimulationDescription", rs.simulation_description);
            xRoot.Add(xRootChildElement);
            xRootChildElement = new XElement("SimulationRating", rs.simulation_rating);
            xRoot.Add(xRootChildElement);

            // Environments
            xRootChildElement = new XElement("Environments", "");
            foreach (Environment en in rs.Environments)
            {
                XElement xEnvironment = new XElement("Environment");
                xEnvironment.SetAttributeValue("id", "I" + en.id.ToString());
                xEnvironment.SetAttributeValue("name", en.name);

                XElement xEnvironmetChildElement = new XElement("TravelCostEnter", en.travel_cost_enter);
                xEnvironment.Add(xEnvironmetChildElement);
                xEnvironmetChildElement = new XElement("TravelCostIn", en.travel_cost_in);
                xEnvironment.Add(xEnvironmetChildElement);
                xEnvironmetChildElement = new XElement("TravelCostExit", en.travel_cost_exit);
                xEnvironment.Add(xEnvironmetChildElement);
                xEnvironmetChildElement = new XElement("Damage", en.damage);
                xEnvironment.Add(xEnvironmetChildElement);

                xRootChildElement.Add(xEnvironment);
            }
            xRoot.Add(xRootChildElement);

            // Robots
            xRootChildElement = new XElement("Robots", "");
            foreach (Robot r in rs.Robots)
            {
                XElement xRobot = new XElement("Robot");
                xRobot.SetAttributeValue("id", "R" + r.id.ToString());
                xRobot.SetAttributeValue("name", r.name);
                xRobot.SetAttributeValue("owner", r.owner);

                XElement xRobotChildElement = new XElement("RobotMeshGrid", r.robot_mesh_grid);
                xRobot.Add(xRobotChildElement);
                xRobotChildElement = new XElement("Speed", r.speed);
                xRobot.Add(xRobotChildElement);
                xRobotChildElement = new XElement("SpeedBack", r.speed_back);
                xRobot.Add(xRobotChildElement);
                xRobotChildElement = new XElement("TurningSpeed", r.turning_speed);
                xRobot.Add(xRobotChildElement);
                xRobotChildElement = new XElement("TurningSpeedBack", r.turning_speed_back);
                xRobot.Add(xRobotChildElement);

                // Robot wheels
                XElement xRobotWheels = new XElement("Wheels", "");
                foreach (Wheel w in r.Wheels)
                {
                    XElement xWheel = new XElement("Wheel");
                    xWheel.SetAttributeValue("driving", w.driving);

                    XElement xWheelChildElement = new XElement("WheelMeshGrid", w.wheel_mesh_grid);
                    xWheel.Add(xWheelChildElement);
                    xWheelChildElement = new XElement("WheelDiameter", w.wheel_diameter);
                    xWheel.Add(xWheelChildElement);
                    xWheelChildElement = new XElement("WheelWidth", w.wheel_width);
                    xWheel.Add(xWheelChildElement);

                    xRobotWheels.Add(xWheel);
                }
                xRobot.Add(xRobotWheels);

                // Robot sensors
                XElement xRobotSensors = new XElement("Sensors", "");
                foreach (Sensor s in r.Sensors)
                {
                    XElement xSensor = new XElement("Sensor");
                    xSensor.SetAttributeValue("name", s.name);
                    xSensor.SetAttributeValue("valueType", s.value_type);

                    XElement xSensorChildElement = new XElement("SensorMeshGrid", s.sensor_mesh_grid);
                    xSensor.Add(xSensorChildElement);
                    xSensorChildElement = new XElement("NumberOfValusPerSecond", s.number_of_values_per_second);
                    xSensor.Add(xSensorChildElement);

                    xRobotSensors.Add(xSensor);
                }
                xRobot.Add(xRobotSensors);

                // Robot rotor
                XElement xRobotRotors = new XElement("Rotors", "");
                foreach (Rotor rotor in r.Rotors)
                {
                    XElement xRotor = new XElement("Rotor");

                    XElement xRotorChildElement = new XElement("RotorMeshGrid", rotor.rotor_mesh_grid);
                    xRotor.Add(xRotorChildElement);
                    xRotorChildElement = new XElement("RotorLiftingPower", rotor.rotor_lifting_power);
                    xRotor.Add(xRotorChildElement);

                    xRobotRotors.Add(xRotor);
                }
                xRobot.Add(xRobotRotors);


                xRootChildElement.Add(xRobot);
            }
            xRoot.Add(xRootChildElement);

            // Maps
            xRootChildElement = new XElement("Maps", "");
            foreach (Map m in rs.Maps)
            {
                XElement xMap = new XElement("Map");
                xMap.SetAttributeValue("id", "M" + m.id.ToString());
                xMap.SetAttributeValue("name", m.map_name);

                XElement xMapChildElement = new XElement("MapData", m.map_data);
                xMap.Add(xMapChildElement);
                xMapChildElement = new XElement("Denivelation", m.denivelation);
                xMap.Add(xMapChildElement);

                xRootChildElement.Add(xMap);
            }
            xRoot.Add(xRootChildElement);

            // Algorithms
            xRootChildElement = new XElement("Algorithms", "");
            foreach (Algorithm a in rs.Algorithms)
            {
                XElement xAlgorithm = new XElement("Algorithm");
                xAlgorithm.SetAttributeValue("id", "A" + a.id.ToString());
                xAlgorithm.SetAttributeValue("name", a.name);
                xAlgorithm.SetAttributeValue("diffEnvironments", a.diffEnvironments);
                xAlgorithm.SetAttributeValue("multipleDestPoints", a.multipleDestPoints);

                XElement xAlgorithmChildElement = new XElement("Complexity", a.complexity);
                xAlgorithm.Add(xAlgorithmChildElement);
                xAlgorithmChildElement = new XElement("Depth", a.depth);
                xAlgorithm.Add(xAlgorithmChildElement);

                xRootChildElement.Add(xAlgorithm);
            }
            xRoot.Add(xRootChildElement);

            // Add the root element to the document
            roboSimulationXml.Add(xRoot);
            // Save the document in the given path(Maybe needed virtual server or such stuff to be the given path, be carefull!)
            roboSimulationXml.Save(filePath);


            //try
            //{
            //    roboSimulationXml.Save(filePath);
            //}
            //catch (Exception e)
            //{
            //    string msg = e.Message; ???
            //}
        }
        //reader using
        public bool LoadRoboSimulationFromXMLUsingReader(string path)
        {
            this.path = path;

            XmlReaderSettings settings = new XmlReaderSettings();

            settings.ValidationType = ValidationType.DTD;
            settings.DtdProcessing  = DtdProcessing.Parse;


            RoboSimulation rs  = null;
            Environment    en  = null;
            Robot          r   = null;
            Wheel          w   = null;
            Sensor         s   = null;
            Rotor          rot = null;
            Map            m   = null;
            Algorithm      a   = null;

            try
            {
                XmlReader reader = XmlReader.Create(path, settings);
                while (reader.Read())
                {
                    if (reader.IsStartElement())
                    {
                        switch (reader.Name)
                        {
                        case "RoboSimulation":
                            rs = new RoboSimulation();
                            rs.simulation_name  = reader.GetAttribute(RoboSimulationElements.ROBOSIMULATION_NAME);
                            rs.simulation_owner = reader.GetAttribute(RoboSimulationElements.ROBOSIMULATION_OWNER);
                            break;

                        case "SimulationOwnerEmail":
                            rs.simulation_owner_email = (string)reader.ReadElementContentAs(typeof(string), null);
                            break;

                        case "SimulationDescription":
                            rs.simulation_description = (string)reader.ReadElementContentAs(typeof(string), null);
                            break;

                        case "SimulationRating":
                            rs.simulation_rating = Decimal.Parse((string)reader.ReadElementContentAs(typeof(string), null));
                            break;

                        /* Environment */
                        case "Environment":
                            en      = new Environment();
                            en.name = reader.GetAttribute(RoboSimulationElements.ROBOSIMULATION_ENVIRONMENT_NAME);
                            rs.Environments.Add(en);
                            break;

                        case "TravelCostEnter":
                            en.travel_cost_enter = Decimal.Parse((string)reader.ReadElementContentAs(typeof(string), null));
                            break;

                        case "TravelCostIn":
                            en.travel_cost_in = Decimal.Parse((string)reader.ReadElementContentAs(typeof(string), null));
                            break;

                        case "TravelCostExit":
                            en.travel_cost_exit = Decimal.Parse((string)reader.ReadElementContentAs(typeof(string), null));
                            break;

                        case "Damage":
                            en.damage = Decimal.Parse((string)reader.ReadElementContentAs(typeof(string), null));
                            break;

                        /* Robot */
                        case "Robot":
                            r       = new Robot();
                            r.name  = reader.GetAttribute(RoboSimulationElements.ROBOSIMULATION_ROBOT_NAME);
                            r.owner = reader.GetAttribute(RoboSimulationElements.ROBOSIMULATION_ROBOT_OWNER);
                            rs.Robots.Add(r);
                            break;

                        case "RobotMeshGrid":
                            r.robot_mesh_grid = (string)reader.ReadElementContentAs(typeof(string), null);
                            break;

                        case "Speed":
                            r.speed = Decimal.Parse((string)reader.ReadElementContentAs(typeof(string), null));
                            break;

                        case "SpeedBack":
                            r.speed_back = Decimal.Parse((string)reader.ReadElementContentAs(typeof(string), null));
                            break;

                        case "TurningSpeed":
                            r.turning_speed = Decimal.Parse((string)reader.ReadElementContentAs(typeof(string), null));
                            break;

                        case "TurningSpeedBack":
                            r.turning_speed_back = Decimal.Parse((string)reader.ReadElementContentAs(typeof(string), null));
                            break;

                        /* Robot Wheels */
                        case "Wheel":
                            w         = new Wheel();
                            w.driving = reader.GetAttribute(RoboSimulationElements.ROBOSIMULATION_WHEEL_DIRIVING);
                            r.Wheels.Add(w);
                            break;

                        case "WheelMeshGrid":
                            w.wheel_mesh_grid = (string)reader.ReadElementContentAs(typeof(string), null);
                            break;

                        case "WheelDiameter":
                            w.wheel_diameter = Decimal.Parse((string)reader.ReadElementContentAs(typeof(string), null));
                            break;

                        case "WheelWidth":
                            w.wheel_width = Decimal.Parse((string)reader.ReadElementContentAs(typeof(string), null));
                            break;

                        /* Robot Sensors */
                        case "Sensor":
                            s            = new Sensor();
                            s.name       = reader.GetAttribute(RoboSimulationElements.ROBOSIMULATION_SENSOR_NAME);
                            s.value_type = reader.GetAttribute(RoboSimulationElements.ROBOSIMULATION_SENSOR_VALUETYPE);
                            r.Sensors.Add(s);
                            break;

                        case "SensorMeshGrid":
                            s.sensor_mesh_grid = (string)reader.ReadElementContentAs(typeof(string), null);
                            break;

                        case "NumberOfValusPerSecond":
                            s.number_of_values_per_second = Decimal.Parse((string)reader.ReadElementContentAs(typeof(string), null));
                            break;

                        /* Robot Rotors */
                        case "Rotor":
                            rot = new Rotor();
                            r.Rotors.Add(rot);
                            break;

                        case "RotorMeshGrid":
                            rot.rotor_mesh_grid = (string)reader.ReadElementContentAs(typeof(string), null);
                            break;

                        case "RotorLiftingPower":
                            rot.rotor_lifting_power = Decimal.Parse((string)reader.ReadElementContentAs(typeof(string), null));
                            break;

                        /* Map */
                        case "Map":
                            m          = new Map();
                            m.map_name = reader.GetAttribute(RoboSimulationElements.ROBOSIMULATION_MAP_NAME);
                            rs.Maps.Add(m);
                            break;

                        case "MapData":
                            m.map_data = (string)reader.ReadElementContentAs(typeof(string), null);
                            break;

                        case "Denivelation":
                            m.denivelation = Decimal.Parse((string)reader.ReadElementContentAs(typeof(string), null));
                            break;

                        /* Algorithm */
                        case "Algorithm":
                            a      = new Algorithm();
                            a.name = reader.GetAttribute(RoboSimulationElements.ROBOSIMULATION_ALGORITHM_NAME);
                            a.multipleDestPoints = reader.GetAttribute(RoboSimulationElements.ROBOSIMULATION_ALGORITHM_MULTIPLEDESTPOINTS);
                            a.diffEnvironments   = reader.GetAttribute(RoboSimulationElements.ROBOSIMULATION_ALGORITHM_DIFFENVIRONMENTS);
                            rs.Algorithms.Add(a);
                            break;

                        case "Complexity":
                            a.complexity = (string)reader.ReadElementContentAs(typeof(string), null);
                            break;

                        case "Depth":
                            a.depth = (string)reader.ReadElementContentAs(typeof(string), null);
                            break;
                        }
                    }
                }
                reader.Close();
            }
            catch (Exception e)
            {
                throw new XMLRoboSimulationProcessorException(e.Message);
            }
            this.roboSimulation = rs;
            return(true);
        }
        // Validates every field in the robo simulation and if everything is correct returns true (and the data is in the given RoboSimulation object)
        bool ValidateRoboSimulation(RoboSimulation rs)
        {
            bool valid     = true;
            int  idCounter = 0;

            // Validate simulation elements(not nested)
            if (!Validate())
            {
                valid = false;
            }
            else
            {
                rs.simulation_name        = string.Copy(TextBoxSimulationName.Text);
                rs.simulation_owner       = string.Copy(TextBoxSimulationOwner.Text);
                rs.simulation_owner_email = string.Copy(TextBoxSimulationOwnerEmail.Text);
                rs.simulation_description = string.Copy(TextBoxSimulationDescription.Text);
                rs.simulation_rating      = Decimal.Parse(TextBoxSimulationRating.Text);
            }

            // Validate environments
            idCounter = 0;
            foreach (WebUserControlEnvironments en in PlaceHolderEnvironments.Controls)
            {
                Environment environment = null;

                // If the fields are wrong, the environment object will be null
                en.WriteToEnvironment(ref environment);

                if (environment == null)
                {
                    valid = false;
                }
                else
                {
                    environment.id = idCounter++;
                    rs.Environments.Add(environment);
                }
            }

            // Validate environments
            idCounter = 0;
            foreach (WebUserControlRobots r in PlaceHolderRobots.Controls)
            {
                Robot robot = null;

                // If the fields are wrong, the environment object will be null
                r.WriteToRobot(ref robot);

                if (robot == null)
                {
                    valid = false;
                }
                else
                {
                    robot.id = idCounter++;
                    rs.Robots.Add(robot);
                }
            }

            // Validate maps
            idCounter = 0;
            foreach (WebUserControlMaps m in PlaceHolderMaps.Controls)
            {
                Map map = null;

                // If the fields are wrong, the environment object will be null
                m.WriteToMap(ref map);

                if (map == null)
                {
                    valid = false;
                }
                else
                {
                    map.id = idCounter++;
                    rs.Maps.Add(map);
                }
            }

            // Validate algorithms
            idCounter = 0;
            foreach (WebUserControlAlgorithms a in PlaceHolderAlgorithms.Controls)
            {
                Algorithm algorithm = null;

                // If the fields are wrong, the environment object will be null
                a.WriteToAlgorithm(ref algorithm);

                if (algorithm == null)
                {
                    valid = false;
                }
                else
                {
                    algorithm.id = idCounter++;
                    rs.Algorithms.Add(algorithm);
                }
            }

            return(valid);
        }