예제 #1
0
 void AssertEquals <T>(T[] expected, T[] actual)
 {
     if (expected == null)
     {
         if (actual != null)
         {
             Assert.Fail("Expected null but got {0}", Sos.SerializeObject(actual));
         }
     }
     else if (actual == null)
     {
         Assert.Fail("Expected {0} but got null", Sos.SerializeObject(expected));
     }
     else
     {
         if (expected.Length != actual.Length)
         {
             Assert.Fail("Expected array of length {0} but actual was length {1} (expected {2}, actual {3})",
                         expected.Length, actual.Length, Sos.SerializeObject(expected), Sos.SerializeObject(actual));
         }
         for (int i = 0; i < expected.Length; i++)
         {
             if (!expected[i].Equals(actual[i]))
             {
                 Assert.Fail("Array mismatch at index {0} (expected {1}, actual {2})", i, Sos.SerializeObject(expected), Sos.SerializeObject(actual));
             }
         }
     }
 }
예제 #2
0
        public static void TestDeserializationToNull(StringBuilder builder, Type type, String str, Int32 expectedLengthUsed)
        {
            Object obj;
            Int32  offset = Sos.Deserialize(out obj, type, str, 0, str.Length);

            Console.WriteLine("String '{0}' Object After Deserialization '{1}'", str, obj);

            Assert.AreEqual(expectedLengthUsed, offset);
            Assert.IsNull(obj);
        }
예제 #3
0
        public static void TestSerializer(StringBuilder builder, Object obj)
        {
            builder.Length = 0;
            obj.SerializeObject(builder);
            String seralizationString = builder.ToString();

            //Console.WriteLine("SerializationString '{0}'", seralizationString);

            Object newObject = null;

            Int32 offset = -1;

            try
            {
                offset = Sos.Deserialize(out newObject, obj.GetType(), seralizationString, 0, seralizationString.Length);
            }
            catch (Exception e)
            {
                throw new Exception(String.Format("Deserialization failed: {0}. Serialized string was '{1}'", e, seralizationString), e);
            }


            /*
             * String objString;
             * try
             * {
             *  objString = obj.ToString();
             * }
             * catch (NullReferenceException)
             * {
             *  objString = "[NullReferenceException in ToString() Method]";
             * }
             * String newObjectString;
             * try
             * {
             *  newObjectString = newObject.ToString();
             * }
             * catch (NullReferenceException)
             * {
             *  newObjectString = "[NullReferenceException in ToString() Method]";
             * }
             * Console.WriteLine("Before Serialization '{0}' ({1}) SerializeString '{2}' After '{3}' ({4})",
             *  objString, obj.GetType(), seralizationString, newObjectString, newObject.GetType());
             */


            Assert.AreEqual(seralizationString.Length, offset);

            String diffMessage = obj.Diff(newObject);

            if (diffMessage != null)
            {
                Assert.Fail(diffMessage);
            }
        }
예제 #4
0
        public void TestValidFloatRegex(String floatString)
        {
            Console.WriteLine("ValidTestString '{0}'", floatString);

            //Match m = Sos.FloatNumberRegexBase10.Match(floatString);
            Int32  numberLength = Sos.FloatLength(floatString, 0, floatString.Length);
            String matchString  = floatString.Substring(0, numberLength);

            Console.WriteLine("Matched '{0}'", matchString);
            Assert.AreEqual(floatString.Length, matchString.Length);
        }
예제 #5
0
        public static void TestDeserializeFormatException(StringBuilder builder, Type type, String str)
        {
            Object obj;

            try
            {
                Int32 offset = Sos.Deserialize(out obj, type, str, 0, str.Length);
                Assert.Fail("Expected FormatException but didn't get one for string '{0}'", str);
            }
            catch (FormatException e)
            {
                Console.WriteLine("String '{0}' Produced FormatException '{1}'", str, e.Message);
            }
        }
예제 #6
0
        public void FullUTF16CharacterTestCoverage()
        {
            StringBuilder builder = new StringBuilder();

            Util.TestDeserialization(builder, '\0', @"""\0""");
            Util.TestDeserialization(builder, '\n', @"""\n""");
            Util.TestDeserialization(builder, '\r', @"""\r""");
            Util.TestDeserialization(builder, '\t', @"""\t""");
            Util.TestDeserialization(builder, '\\', @"""\\""");
            Util.TestDeserialization(builder, '\xab', @"""\xab""");
            Util.TestDeserialization(builder, '\x9F', @"""\x9F""");
            Util.TestDeserialization(builder, '\uabCe', @"""\uabCe""");
            Util.TestDeserialization(builder, '\u9F3C', @"""\u9F3C""");
            //Util.TestDeserialization(builder, '\a', @"""\a""");
            //Util.TestDeserialization(builder, '\b', @"""\b""");
            //Util.TestDeserialization(builder, '\f', @"""\f""");
            //Util.TestDeserialization(builder, '\v', @"""\v""");

            for (Int32 cAsInt = 0; cAsInt <= 65535; cAsInt++)
            {
                Char c = (Char)cAsInt;

                String seralizationString = c.SerializeChar();

                Object newObject = null;

                Int32 offset = -1;
                try
                {
                    offset = Sos.Deserialize(out newObject, typeof(Char), seralizationString, 0, seralizationString.Length);
                }
                catch (Exception e)
                {
                    throw new Exception(String.Format("Deserialization failed: {0}. Serialized string was '{1}'", e, seralizationString), e);
                }

                //Console.WriteLine("Before Serialization '{0}' ({1}) SerializeString '{2}' After '{3}' ({4})",
                //    c, typeof(Char), seralizationString, newObject, newObject.GetType());

                Assert.AreEqual(seralizationString.Length, offset);

                String diffMessage = c.Diff(newObject);
                if (diffMessage != null)
                {
                    Assert.Fail(diffMessage);
                }
            }
        }
예제 #7
0
파일: SnmpTest.cs 프로젝트: blinds52/More
        public void TestOidParser(Byte[] expectedBytes, String oidString)
        {
            oidBytes.Clear();
            Console.WriteLine("Testing '{0}'", oidString);
            Snmp.ParseOid(oidString, oidBytes);

            Byte[] actual = oidBytes.ToArray();
            String diff   = Sos.Diff(expectedBytes, actual);

            if (diff != null)
            {
                Console.WriteLine("Expected: " + expectedBytes.SerializeObject());
                Console.WriteLine("Actual  : " + actual.SerializeObject());
                Assert.Fail(diff);
            }
        }
예제 #8
0
        public static void TestDeserialization(StringBuilder builder, Object expectedObject, String str, Int32 expectedLengthUsed)
        {
            Object newObject;
            Int32  offset = Sos.Deserialize(out newObject, expectedObject.GetType(), str, 0, str.Length);

            Console.WriteLine("String '{0}' Object After Deserialization '{1}'", str, newObject);

            String diffMessage = expectedObject.Diff(newObject);

            if (diffMessage != null)
            {
                Assert.Fail(diffMessage);
            }

            Assert.AreEqual(expectedLengthUsed, offset);
        }
예제 #9
0
        void ThrowExceptionFromCall(String methodName, NpcReturnLine returnLine)
        {
            Type exceptionType;

            try
            {
                exceptionType = staticClientTypeFinder.FindType(returnLine.sosTypeName);
            }
            catch (InvalidOperationException)
            {
                //Console.WriteLine("Could not find type '{0}'", returnLine.sosTypeName);
                goto INVALID_EXCEPTION;
            }

            Object exceptionObject;

            try
            {
                Int32 offset = Sos.Deserialize(out exceptionObject, exceptionType,
                                               returnLine.sosSerializationString, 0, returnLine.sosSerializationString.Length);
                if (offset != returnLine.sosSerializationString.Length)
                {
                    goto INVALID_EXCEPTION;
                }
            }
            catch (Exception)
            {
                //Console.WriteLine("Faild to deserialize exception '{0}'", returnLine.sosTypeName);
                goto INVALID_EXCEPTION;
            }

            try
            {
                Exception e = (Exception)exceptionObject;
                throw e;
            }
            catch (InvalidCastException)
            {
                //Console.WriteLine("Could not cast '{0}' to Exception", exceptionObject.GetType().Name);
                goto INVALID_EXCEPTION;
            }

INVALID_EXCEPTION:
            throw new Exception(String.Format("Method '{0}' threw exception {1}: '{2}'",
                                              methodName, returnLine.sosTypeName, returnLine.exceptionMessage));
        }
예제 #10
0
        public static void ParseParameters(String parametersString, List <String> parameterList)
        {
            if (parametersString == null)
            {
                return;
            }
            if (parametersString.Length <= 0)
            {
                return;
            }

            Int32 offset = 0;

            while (true)
            {
                while (true)
                {
                    Char c = parametersString[offset];
                    if (Sos.IsValidStartOfSosString(c))
                    {
                        break;
                    }
                    if (!Char.IsWhiteSpace(c))
                    {
                        throw new FormatException(String.Format(
                                                      "Every parameter string must start with 0-9,a-z,A-Z,\",[,{{ or ', but parameter {0} started with '{1}' (charcode={2})",
                                                      parameterList.Count, c, (UInt32)c));
                    }
                    offset++;
                    if (offset >= parametersString.Length)
                    {
                        return;
                    }
                }

                Int32 nextSpace = Sos.NextNonQuotedWhitespace(parametersString, offset);
                parameterList.Add(parametersString.Substring(offset, nextSpace - offset));

                offset = nextSpace + 1;
                if (offset >= parametersString.Length)
                {
                    return;
                }
            }
        }
예제 #11
0
        public void TestInvalidStringsForNextWhitespace()
        {
            String[] invalidStrings = new String[] {
                "\" ",
                "\"\\\\\\\" ",
            };

            for (int i = 0; i < invalidStrings.Length; i++)
            {
                String invalidString = invalidStrings[i];
                try
                {
                    Sos.NextNonQuotedWhitespace(invalidString, 0);
                    Assert.Fail("Expected format exception but didn't get one for '{0}'", invalidString);
                }
                catch (FormatException e)
                {
                    Console.WriteLine("Expected format exception for '{0}': {1}", invalidString, e.Message);
                }
            }
        }
예제 #12
0
        public void TestInvalidFloatRegex(String floatString)
        {
            Console.WriteLine("InvalidTestString '{0}'", floatString);
            //Match m = Sos.FloatNumberRegexBase10.Match(floatString);
            Int32  numberLength = Sos.FloatLength(floatString, 0, floatString.Length);
            String matchString  = floatString.Substring(0, numberLength);

            Console.WriteLine("Invalid '{0}' Matched '{1}'", floatString, matchString);

            if (numberLength > 0)
            {
                try
                {
                    Single.Parse(matchString);
                    Assert.Fail("Expected parse to fail but didn't");
                }
                catch (FormatException)
                {
                }
            }
        }
예제 #13
0
        public void TestHexStrings()
        {
            Byte[][] testArrays = new Byte[][] {
                new Byte[] {},
                new Byte[] { 0 },
                new Byte[] { 0, 1, 2, 3 },
                new Byte[] { 0xFF, 0xFE },
                new Byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 },
            };

            for (int i = 0; i < testArrays.Length; i++)
            {
                Byte[] testArray = testArrays[i];

                String hexString = testArray.ToHexString(0, testArray.Length);
                Console.WriteLine("Testing {0}", hexString);

                Byte[] deserializeCopy = new Byte[testArray.Length];
                deserializeCopy.ParseHex(0, hexString, 0, hexString.Length);

                Sos.Diff(testArray, deserializeCopy);
            }
        }
예제 #14
0
        public void NextWhitespaceTest()
        {
            String[] validStrings = new String[] {
                "0 ",
                "9 ",
                "a ",
                "z ",
                "A ",
                "Z ",
                "\"\" ",
                "\"\\\"\" ",
                "\"\\\\\\\"\" ",
                "[ ",
                "{ ",
            };

            for (int i = 0; i < validStrings.Length; i++)
            {
                String validString = validStrings[i];

                Int32 offset = Sos.NextNonQuotedWhitespace(validString, 0);
                Assert.AreEqual(validString.Length - 1, offset);
            }
        }
예제 #15
0
        public static List <Bitmap> Decode(MemoryStream stream)
        {
            var reader = new BinaryReader(stream);
            var images = new List <Bitmap>();

            stream.Seek(0, SeekOrigin.Begin);

            bool eof = false;

            for (int image = 1; ; image++)
            {
                try
                {
                    var imgInfo = new ImgInfo();

                    while (true)
                    {
                        while (reader.ReadByte() != 0xff)
                        {
                            ;
                        }
                        int markerId = reader.ReadByte();

                        switch ((Markers)markerId)
                        {
                        case Markers.App0:
                            App0.Read(reader, imgInfo);
                            break;

                        case Markers.App14:
                            App14.Read(reader, imgInfo);
                            break;

                        case Markers.Dqt:
                            Dqt.Read(reader, imgInfo);
                            break;

                        case Markers.Sof0:
                            Sof0.Read(reader, imgInfo);
                            break;

                        case Markers.Sof2:
                            Sof2.Read(reader, imgInfo);
                            break;

                        case Markers.Dht:
                            Dht.Read(reader, imgInfo);
                            break;

                        case Markers.Sos:
                            images.Add(Sos.Read(reader, imgInfo));
                            break;

                        case Markers.Soi:
                            imgInfo = new ImgInfo();
                            //Logger.Write("Start of Image " + image);
                            //Logger.WriteLine(" at: " + (reader.BaseStream.Position - 2).ToString("X"));
                            imgInfo.startOfImageFound = true;
                            break;

                        case Markers.Dri:
                            Dri.Read(reader, imgInfo);
                            break;

                        case Markers.Eoi:
                            //Logger.Write("End of Image " + image);
                            //Logger.WriteLine(" at: " + (reader.BaseStream.Position - 2).ToString("X"));
                            eof = true;
                            break;

                        // Unknown markers, or markers used outside of their specified area
                        default:
                            Default.Read(reader, imgInfo, (Markers)markerId);
                            break;
                        }

                        if (eof)
                        {
                            eof = false;
                            break;
                        }
                    }
                }
                catch (EndOfStreamException)
                {
                    break;
                }
                catch (Exception ex)
                {
                    //Logger.WriteLine(ex.Message);
                }
            }

            reader.Close();
            //Logger.Flush();

            return(images);
        }
예제 #16
0
        // expectedReturnType can be null, but providing the expected return type makes it unnecessary to search
        // each assembly for the type
        Object PerformCall(Type expectedReturnType, String methodName, String rawNpcLine)
        {
            if (threadSafe)
            {
                Monitor.Enter(serverEndPoint);
            }
            try
            {
                //
                // The reason for the retry logic is because if the underlying socket is disconnected, it may not
                // fail until after a send and a receive...so the socket should be reconnected and the request should
                // be repeated only once.
                //
                for (UInt32 attempt = 0; ; attempt++)
                {
                    try
                    {
                        Connect();

                        socketLineReader.socket.Send(Encoding.UTF8.GetBytes(rawNpcLine.ToString()));

                        String returnLineString = socketLineReader.ReadLine();
                        if (returnLineString == null)
                        {
                            if (attempt == 0)
                            {
                                Dispose();
                                continue; // Retry
                            }
                            throw UnexpectedClose();
                        }

                        NpcReturnLine returnLine = new NpcReturnLine(returnLineString);

                        if (returnLine.exceptionMessage != null)
                        {
                            ThrowExceptionFromCall(methodName, returnLine);
                        }

                        if (expectedReturnType == null)
                        {
                            if (returnLine.sosTypeName.Equals("Void"))
                            {
                                return(null);
                            }
                            expectedReturnType = GetTypeFromSosTypeName(returnLine.sosTypeName);
                        }
                        else
                        {
                            if (!returnLine.sosTypeName.Equals(expectedReturnType.SosTypeName()))
                            {
                                throw new InvalidOperationException(String.Format("Expected return type to be {0} but was {1}",
                                                                                  expectedReturnType.SosTypeName(), returnLine.sosTypeName));
                            }
                        }

                        if (expectedReturnType == typeof(void))
                        {
                            return(null);
                        }

                        Object returnObject;
                        Int32  valueStringOffset = Sos.Deserialize(out returnObject, expectedReturnType,
                                                                   returnLine.sosSerializationString, 0, returnLine.sosSerializationString.Length);

                        if (valueStringOffset != returnLine.sosSerializationString.Length)
                        {
                            throw new InvalidOperationException(String.Format(
                                                                    "Used {0} characters to deserialize object of type '{1}' but the serialization string had {2} characters",
                                                                    valueStringOffset, expectedReturnType.SosTypeName(), returnLine.sosSerializationString.Length));
                        }

                        return(returnObject);
                    }
                    catch (SocketException)
                    {
                        if (socketLineReader != null)
                        {
                            socketLineReader.Dispose();
                            socketLineReader = null;
                        }
                        if (attempt == 0)
                        {
                            continue; // Retry
                        }
                        throw;
                    }
                }
            }
            finally
            {
                if (threadSafe)
                {
                    Monitor.Exit(serverEndPoint);
                }
            }
        }
예제 #17
0
파일: PixzDecode.cs 프로젝트: 0-v-0/test
        public static List <Image> Decode(Stream stream)
        {
            var reader = new BinaryReader(stream);
            var images = new List <Image>();

            stream.Seek(0, SeekOrigin.Begin);

            var imgInfo = new ImgInfo();

            for (long length = stream.Length; ;)
            {
                int markerId;
                do
                {
                    if (stream.Position == length)
                    {
                        goto end;
                    }
                } while (reader.ReadByte() != 0xff);

                markerId = reader.ReadByte();

                switch ((Markers)markerId)
                {
                case Markers.App0:
                    App0.Read(reader, imgInfo);
                    break;

                case Markers.App14:
                    App14.Read(reader, imgInfo);
                    break;

                case Markers.Dqt:
                    Dqt.Read(reader, imgInfo);
                    break;

                case Markers.Sof0:
                    Sof0.Read(reader, imgInfo);
                    break;

                case Markers.Sof2:
                    Sof2.Read(reader, imgInfo);
                    break;

                case Markers.Dht:
                    Dht.Read(reader, imgInfo);
                    break;

                case Markers.Sos:
                    images.Add(Sos.Read(reader, imgInfo));
                    break;

                case Markers.Soi:
                    //Logger.Write("Start of Image " + image);
                    //Logger.WriteLine(" at: " + (reader.BaseStream.Position - 2).ToString("X"));
                    imgInfo = new ImgInfo
                    {
                        startOfImageFound = true
                    };
                    break;

                case Markers.Dri:
                    Dri.Read(reader, imgInfo);
                    break;

                case Markers.Eoi:
                    //Logger.Write("End of Image " + image);
                    //Logger.WriteLine(" at: " + (reader.BaseStream.Position - 2).ToString("X"));
                    //eof = true;
                    break;

                // Unknown markers, or markers used outside of their specified area
                default:
                    Default.Read(reader, imgInfo, (Markers)markerId);
                    break;
                }
            }
end:
            reader.Dispose();
            return(images);
        }
예제 #18
0
        //
        // Throws NpcErrorException for an npc error
        //
        public NpcReturnLine(String returnLine)
        {
            Boolean success = returnLine.StartsWith(NpcReturnObject.NpcReturnLineSuccessPrefix);

            if (success || returnLine.StartsWith(NpcReturnObject.NpcReturnLineExceptionPrefix))
            {
                String noPrefixReturnLine;
                if (success)
                {
                    noPrefixReturnLine = returnLine.Substring(NpcReturnLineSuccessPrefix.Length);

                    if (noPrefixReturnLine.Equals("Void"))
                    {
                        this.exceptionMessage       = null;
                        this.sosTypeName            = "Void";
                        this.sosSerializationString = null;
                        return;
                    }
                }
                else
                {
                    noPrefixReturnLine = returnLine.Substring(NpcReturnLineExceptionPrefix.Length);

                    //
                    // Exception Message
                    //
                    Object exceptionMessageObject;
                    Int32  offset = Sos.Deserialize(out exceptionMessageObject, typeof(String),
                                                    noPrefixReturnLine, 0, noPrefixReturnLine.Length);
                    this.exceptionMessage = (String)exceptionMessageObject;

                    if (offset >= noPrefixReturnLine.Length - 1)
                    {
                        InvalidReturnLine(returnLine, "Missing exception type and serialized exception");
                    }
                    noPrefixReturnLine = noPrefixReturnLine.Substring(offset + 1);
                }

                //
                // Get the return type
                //
                Int32 spaceIndex = noPrefixReturnLine.IndexOf(' ');
                if (spaceIndex < 0 || spaceIndex >= noPrefixReturnLine.Length - 1)
                {
                    InvalidReturnLine(returnLine, "missing the return value");
                }
                if (spaceIndex == 0)
                {
                    InvalidReturnLine(returnLine, "After 'Success' prefix there were 2 spaces in a row");
                }

                this.sosTypeName            = noPrefixReturnLine.Remove(spaceIndex);
                this.sosSerializationString = noPrefixReturnLine.Substring(spaceIndex + 1);

                return;
            }

            if (returnLine.StartsWith(NpcReturnObject.NpcReturnLineNpcErrorPrefix))
            {
                String errorCode;
                returnLine.Peel(out errorCode);
                errorCode = errorCode.Peel(out returnLine);
                throw new NpcErrorException((NpcErrorCode)Enum.Parse(typeof(NpcErrorCode), errorCode), returnLine);
            }

            InvalidReturnLine(returnLine, String.Format("does not start with '{0}','{1}' or '{2}'",
                                                        NpcReturnObject.NpcReturnLineSuccessPrefix, NpcReturnObject.NpcReturnLineNpcErrorPrefix,
                                                        NpcReturnObject.NpcReturnLineExceptionPrefix));
        }
예제 #19
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (cmbAlgorithms.Text == "" || cmbFunction.Text == "" || txtExperiments.Text == "")
            {
                MessageBox.Show("Lütfen bilgileri eksiksiz giriniz.");
            }
            else
            {
                table.Columns.Clear();
                table.Rows.Clear();
                label3.Text  = "";
                label4.Text  = "";
                label5.Text  = "";
                label11.Text = "";



                table.Columns.Add("En iyi çözüm", typeof(double));
                for (int i = 0; i < nd; i++)
                {
                    table.Columns.Add("Çözüm adayı " + (i + 1), typeof(double));
                }
                table.Columns.Add("İşlem süresi", typeof(double));
                table.Columns.Add("Deney sayısı", typeof(int));
                table.Columns.Add("Min en iyi çözüm", typeof(double));
                table.Columns.Add("Max en iyi çözüm", typeof(double));
                table.Columns.Add("En iyi çözümlerin ortalaması", typeof(double));
                table.Columns.Add("Standart sapma", typeof(double));
                dgwFitnessInf.DataSource = table;

                NumofExp = int.Parse(txtExperiments.Text);
                sw.Start();
                switch (cmbAlgorithms.Text)
                {
                case "Genetic Algorithm":

                    for (int i = 0; i < NumofExp; i++)
                    {
                        Genetic gen       = new Genetic();
                        double  globalMin = gen.geneticStart(cmbFunction.Text);
                        if (gen.getDeger() == -1)
                        {
                            MessageBox.Show("Fonksiyonun parametre sayısı ile değişkenlerinizin sayısı uyuşmuyor!"); break;
                        }
                        else
                        {
                            double[][] randPop  = gen.getRandPop();
                            MinBul     min      = new TekMinBul(gen.getFitness());
                            int        minindex = min.minBul();
                            sw.Stop();

                            row = table.NewRow();

                            row["En iyi çözüm"] = globalMin;
                            for (int h = 0; h < nd; h++)
                            {
                                row["Çözüm adayı " + (h + 1)] = randPop[minindex][h];
                            }
                            row["İşlem süresi"] = sw.ElapsedMilliseconds;

                            table.Rows.Add(row);
                            dgwFitnessInf.DataSource = table;
                        }
                    }
                    break;

                case "SOS Algorithm":

                    for (int i = 0; i < NumofExp; i++)
                    {
                        Sos    sos       = new Sos();
                        double globalMin = sos.sosStart(cmbFunction.Text);

                        if (sos.getDeger() == -1)
                        {
                            MessageBox.Show("Fonksiyonun parametre sayısı ile değişkenlerinizin sayısı uyuşmuyor!"); break;
                        }
                        else
                        {
                            double[][] randPop  = sos.getRandPop();
                            MinBul     min      = new TekMinBul(sos.getFitness());
                            int        minindex = min.minBul();
                            sw.Stop();

                            row = table.NewRow();

                            row["En iyi çözüm"] = globalMin;
                            for (int h = 0; h < nd; h++)
                            {
                                row["Çözüm adayı " + (h + 1)] = randPop[minindex][h];
                            }
                            row["İşlem süresi"] = sw.ElapsedMilliseconds;

                            table.Rows.Add(row);
                            dgwFitnessInf.DataSource = table;
                        }
                    }
                    break;
                }

                try
                {
                    double[] bfs = new double[dgwFitnessInf.Rows.Count - 1];
                    foreach (DataGridViewRow row in dgwFitnessInf.Rows)
                    {
                        ort += Convert.ToDouble(row.Cells[0].Value);
                        bf   = Convert.ToDouble(row.Cells[0].Value);
                        if (m == dgwFitnessInf.Rows.Count - 1)
                        {
                            break;
                        }
                        bfs[m] = bf;
                        m++;
                    }
                    average = ort / dgwFitnessInf.Rows.Count;
                    for (int i = 0; i < dgwFitnessInf.Rows.Count - 1; i++)
                    {
                        ss += Math.Pow((bfs[i] - average), 2);
                    }
                    ss = ss / (dgwFitnessInf.Rows.Count - 2);

                    label3.Text  = bfs.Min() + "";
                    label4.Text  = bfs.Max() + "";
                    label5.Text  = (average) + "";
                    label11.Text = ss + "";

                    row["Deney sayısı"]                 = txtExperiments.Text;
                    row["Min en iyi çözüm"]             = bfs.Min();
                    row["Max en iyi çözüm"]             = bfs.Max();
                    row["En iyi çözümlerin ortalaması"] = average;
                    row["Standart sapma"]               = ss;
                    pnlInfo.Visible  = true;
                    btnStart.Enabled = false;
                }
                catch (Exception)
                {
                    MessageBox.Show("Lütfen probleminize uygun bir fonksiyon seçiniz.");;
                }
            }
        }
예제 #20
0
        public static Object[] CreateParameterObjects(ParameterInfo[] parameterInfos, params String[] parameterStrings)
        {
            int parameterStringsLength = (parameterStrings == null) ? 0 : parameterStrings.Length;
            int parameterInfosLength   = (parameterInfos == null) ? 0 : parameterInfos.Length;

            if (parameterInfosLength != parameterStringsLength)
            {
                throw new InvalidOperationException(String.Format("Expected {0} arguments but got {1}",
                                                                  parameterInfosLength, parameterStringsLength));
            }

            if (parameterStringsLength <= 0)
            {
                return(null);
            }

            Object[] parameterObjects = new Object[parameterStringsLength];

            for (int i = 0; i < parameterStringsLength; i++)
            {
                String parameterString = parameterStrings[i];

                ParameterInfo parameterInfo = parameterInfos[i];
                Type          parameterType = parameterInfos[i].ParameterType;

                //Console.WriteLine("Parameter {0} Type={1}", parameterInfo.Name, parameterType);

                //
                // Add quotes if parameter is string type and it is missing quotes
                //
                if (parameterType == typeof(String) && !parameterString.Equals("null"))
                {
                    if (parameterString.Length <= 0)
                    {
                        parameterString = "null"; // default to null for empty string
                    }
                    else
                    {
                        if (parameterString[0] != '"')
                        {
                            //
                            // make sure the string does not contain any whitespace or quotes
                            //
                            if (parameterString.IndexOfAny(InvalidCharsForNonQuotedStrings) >= 0)
                            {
                                throw new InvalidOperationException(String.Format(
                                                                        "You provided a non-quoted string with invalid characters '{0}' (invalid characters include whitespace, backslashes or quotes)", parameterString));
                            }
                            parameterString = "\"" + parameterString + "\"";
                        }
                    }
                }

                Int32 deserialationIndex;
                try
                {
                    deserialationIndex = Sos.Deserialize(
                        out parameterObjects[i], parameterType, parameterString, 0, parameterString.Length);
                }
                catch (Exception e)
                {
                    throw new InvalidOperationException(String.Format("Failed to deserialize argument {0} ({1}) of type {2}: {3}",
                                                                      i + 1, parameterInfo.Name, parameterType.SosTypeName(), e.Message), e);
                }
                if (deserialationIndex != parameterString.Length)
                {
                    throw new InvalidOperationException(String.Format(
                                                            "Argument {0} (type={1}) has {2} characters but deserializtion only used {3} characters",
                                                            i, parameterType, parameterString.Length, deserialationIndex));
                }
            }

            return(parameterObjects);
        }
예제 #21
0
        private void ExportTimeSeries(bool clearExportedData,
                                      TimeSeriesChangeEvent detectedChange,
                                      TimeSeriesDescription timeSeriesDescription)
        {
            var locationInfo = GetLocationInfo(timeSeriesDescription.LocationIdentifier);

            var(exportDuration, exportLabel) = GetExportDuration(timeSeriesDescription);

            var dataRequest = new TimeSeriesDataCorrectedServiceRequest
            {
                TimeSeriesUniqueId = timeSeriesDescription.UniqueId,
                QueryFrom          = GetInitialQueryFrom(detectedChange),
                ApplyRounding      = Context.ApplyRounding,
            };

            var existingSensor       = Sos.FindExistingSensor(timeSeriesDescription);
            var deleteExistingSensor = clearExportedData && existingSensor != null;
            var assignedOffering     = existingSensor?.Identifier;

            var lastSensorTime = GetLastSensorTime(existingSensor);

            if (HaveExistingSosPointsChanged(dataRequest, lastSensorTime, detectedChange, timeSeriesDescription))
            {
                Log.Warn($"FirstPointChanged={detectedChange.FirstPointChanged:O} AttributeChange={detectedChange.HasAttributeChange} of '{timeSeriesDescription.Identifier}' precedes LastSensorTime={lastSensorTime:O} of '{existingSensor?.Identifier}'. Forcing delete of existing sensor.");

                // A point has changed before the last known observation, so we'll need to throw out the entire sensor
                deleteExistingSensor = true;

                // We'll also need to fetch more data again
                dataRequest.QueryFrom = null;
            }

            if (dataRequest.QueryFrom == null)
            {
                // Get the full extraction
                var endPoint     = dataRequest.QueryTo ?? DateTimeOffset.Now;
                var startOfToday = new DateTimeOffset(endPoint.Year, endPoint.Month, endPoint.Day, 0, 0, 0,
                                                      timeSeriesDescription.UtcOffsetIsoDuration.ToTimeSpan());

                dataRequest.QueryFrom = startOfToday - exportDuration;
            }

            Log.Info($"Fetching changes from '{timeSeriesDescription.Identifier}' FirstPointChanged={detectedChange.FirstPointChanged:O} HasAttributeChanged={detectedChange.HasAttributeChange} QueryFrom={dataRequest.QueryFrom:O} ...");

            var timeSeries = Aquarius.Publish.Get(dataRequest);

            TrimExcludedPoints(timeSeriesDescription, timeSeries);

            var createSensor = existingSensor == null || deleteExistingSensor;

            TimeSeriesPointFilter.FilterTimeSeriesPoints(timeSeries);

            var exportSummary = $"{timeSeries.NumPoints} points [{timeSeries.Points.FirstOrDefault()?.Timestamp.DateTimeOffset:O} to {timeSeries.Points.LastOrDefault()?.Timestamp.DateTimeOffset:O}] from '{timeSeriesDescription.Identifier}' with ExportDuration={exportLabel}";

            ExportedTimeSeriesCount += 1;
            ExportedPointCount      += timeSeries.NumPoints ?? 0;

            if (Context.DryRun)
            {
                if (deleteExistingSensor)
                {
                    LogDryRun($"Would delete existing sensor '{existingSensor?.Identifier}'");
                }

                if (createSensor)
                {
                    LogDryRun($"Would create new sensor for '{timeSeriesDescription.Identifier}'");
                }

                LogDryRun($"Would export {exportSummary}.");
                return;
            }

            Log.Info($"Exporting {exportSummary} ...");

            if (deleteExistingSensor)
            {
                Sos.DeleteSensor(timeSeries);
                Sos.DeleteDeletedObservations();
            }

            if (createSensor)
            {
                var sensor = Sos.InsertSensor(timeSeries);

                assignedOffering = sensor.AssignedOffering;
            }

            Sos.InsertObservation(assignedOffering, locationInfo.LocationData, locationInfo.LocationDescription, timeSeries);
        }
예제 #22
0
        private bool HaveExistingSosPointsChanged(
            TimeSeriesDataCorrectedServiceRequest dataRequest,
            DateTimeOffset?lastSensorTime,
            TimeSeriesChangeEvent detectedChange,
            TimeSeriesDescription timeSeriesDescription)
        {
            if (detectedChange.HasAttributeChange ?? false)
            {
                return(true);
            }

            if (!detectedChange.FirstPointChanged.HasValue || !lastSensorTime.HasValue)
            {
                return(false);
            }

            if (lastSensorTime < detectedChange.FirstPointChanged)
            {
                return(false);
            }

            var timeSeriesIdentifier = timeSeriesDescription.Identifier;

            var sosPoints  = new Queue <TimeSeriesPoint>(Sos.GetObservations(timeSeriesDescription, AddMilliseconds(detectedChange.FirstPointChanged.Value, -1), AddMilliseconds(lastSensorTime.Value, 1)));
            var aqtsPoints = new Queue <TimeSeriesPoint>(Aquarius.Publish.Get(dataRequest).Points);

            var sosCount  = sosPoints.Count;
            var aqtsCount = aqtsPoints.Count;

            Log.Info($"Fetched {sosCount} SOS points and {aqtsCount} AQUARIUS points for '{timeSeriesIdentifier}' from {dataRequest.QueryFrom:O} ...");

            while (sosPoints.Any() || aqtsPoints.Any())
            {
                var sosPoint  = sosPoints.FirstOrDefault();
                var aqtsPoint = aqtsPoints.FirstOrDefault();

                if (aqtsPoint == null)
                {
                    Log.Warn($"'{timeSeriesIdentifier}': AQUARIUS now has fewer points than SOS@{sosPoint?.Timestamp.DateTimeOffset:O}");
                    return(true);
                }

                if (sosPoint == null)
                {
                    break;
                }

                var aqtsValue = (dataRequest.ApplyRounding ?? false)
                    ? double.Parse(aqtsPoint.Value.Display)
                    : aqtsPoint.Value.Numeric;

                var sosValue = sosPoint.Value.Numeric;

                if (sosPoint.Timestamp.DateTimeOffset != aqtsPoint.Timestamp.DateTimeOffset)
                {
                    Log.Warn($"'{timeSeriesIdentifier}': Different timestamps: AQUARIUS={aqtsValue}@{aqtsPoint.Timestamp.DateTimeOffset:O} vs SOS={sosValue}@{sosPoint.Timestamp.DateTimeOffset:O}");
                    return(true);
                }

                if (!DoubleHelper.AreSame(aqtsValue, sosValue))
                {
                    Log.Warn($"'{timeSeriesIdentifier}': Different values @ {aqtsPoint.Timestamp.DateTimeOffset:O}: AQUARIUS={aqtsValue} vs SOS={sosValue}");
                    return(true);
                }

                sosPoints.Dequeue();
                aqtsPoints.Dequeue();
            }

            Log.Info($"'{timeSeriesDescription.Identifier}': All {sosCount} SOS points match between SOS and AQUARIUS.");
            dataRequest.QueryFrom = lastSensorTime.Value.AddTicks(1);

            return(false);
        }
예제 #23
0
        public void TestKnownHashes()
        {
            TestClass[] tests = new TestClass[] {
                new TestClass("abc", 0xA9993E36, 0x4706816A, 0xBA3E2571, 0x7850C26C, 0x9Cd0d89D),
                new TestClass("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
                              0x84983E44, 0x1C3BD26E, 0xBAAE4AA1, 0xF95129E5, 0xE54670F1),
                new TestClass("12345678901234567890123456789012345678901234567890123456789012345678901234567890",
                              0x50ABF570, 0x6A150990, 0xA08B2C5E, 0xA40FA0E5, 0x85554732),
                new TestClass("jafdnznjkl89fn4q3poiunqn8vrnaru8apr8umpau8rfpnu312--1-0-139-110un45paiouwepiourpoqiwrud0-ur238901unmxd-0r1u-rdu0-12u3rm-u-uqfoprufquwioperupauwperuq2cfurq2urduq;w3uirmparuw390peuaf;wuir;oui;avuwao; aro aruawrv au;ru ;aweuriafuwer23f0quprmpuqpuqurq[0q5tau=53una54fion[5cnuq30m5uq903uqncf4",
                              0xEEC53E5E, 0x78191154, 0x0A073AE1, 0x39743E68, 0x8A6CD077),
            };

            Sha1Builder reusedShaBuilder = new Sha1Builder();

            for (int i = 0; i < tests.Length; i++)
            {
                TestClass test = tests[i];

                //
                // Test using 1 call
                //
                {
                    Sha1Builder newShaBuilder = new Sha1Builder();

                    newShaBuilder.Add(test.contentBytes, 0, test.contentBytes.Length);
                    reusedShaBuilder.Add(test.contentBytes, 0, test.contentBytes.Length);

                    var newFinished    = newShaBuilder.Finish(false);
                    var reusedFinished = reusedShaBuilder.Finish(true);

                    Console.WriteLine("Content '{0}'", test.contentString);
                    Console.WriteLine("    Expected {0}", test.expectedHash);
                    Console.WriteLine("    Actual   {0}", newFinished);
                    Console.WriteLine("    Reused   {0}", reusedFinished);

                    Assert.AreEqual(test.expectedHash, newFinished);
                    Assert.AreEqual(test.expectedHash, reusedFinished);
                    //String sosDiff = Sos.Diff(test.expectedHash, finished);
                    //Assert.IsNull(sosDiff, sosDiff);
                }

                //
                // Test using multiple calls
                //
                for (int addLength = 1; addLength < test.contentBytes.Length; addLength++)
                {
                    Console.WriteLine("Test AddLength {0}", addLength);
                    Sha1Builder shaBuilder = new Sha1Builder();

                    // Add the bytes
                    Int32 bytesToWrite       = test.contentBytes.Length;
                    Int32 contentBytesOffset = 0;
                    while (bytesToWrite > 0)
                    {
                        Int32 writeLength = Math.Min(bytesToWrite, addLength);
                        shaBuilder.Add(test.contentBytes, contentBytesOffset, writeLength);
                        reusedShaBuilder.Add(test.contentBytes, contentBytesOffset, writeLength);
                        contentBytesOffset += writeLength;
                        bytesToWrite       -= writeLength;
                    }

                    var shaFinished       = shaBuilder.Finish(false);
                    var reusedShaFinished = reusedShaBuilder.Finish(true);

                    var sosDiff = Sos.Diff(test.expectedHash, shaFinished);
                    if (sosDiff != null)
                    {
                        Console.WriteLine("Content '{0}'", test.contentString);
                        Console.WriteLine("    Expected {0}", test.expectedHash);
                        Console.WriteLine("    Actual   {0}", shaFinished);
                        Assert.Fail();
                    }
                    Assert.AreEqual(test.expectedHash, shaFinished);
                    Assert.AreEqual(test.expectedHash, reusedShaFinished);
                }
            }
        }
예제 #24
0
        private void ExportTimeSeries(
            bool clearExportedData,
            TimeSeriesChangeEvent detectedChange,
            TimeSeriesDescription timeSeriesDescription)
        {
            Log.Info($"Fetching changes from '{timeSeriesDescription.Identifier}' FirstPointChanged={detectedChange.FirstPointChanged:O} HasAttributeChanged={detectedChange.HasAttributeChange} ...");

            var locationInfo = GetLocationInfo(timeSeriesDescription.LocationIdentifier);

            var period = GetTimeSeriesPeriod(timeSeriesDescription);

            var dataRequest = new TimeSeriesDataCorrectedServiceRequest
            {
                TimeSeriesUniqueId = timeSeriesDescription.UniqueId,
                QueryFrom          = GetInitialQueryFrom(detectedChange),
                ApplyRounding      = Context.ApplyRounding,
            };

            var existingSensor       = Sos.FindExistingSensor(timeSeriesDescription);
            var deleteExistingSensor = clearExportedData && existingSensor != null;
            var assignedOffering     = existingSensor?.Identifier;

            var timeSeries = FetchMinimumTimeSeries(detectedChange, timeSeriesDescription, existingSensor, dataRequest, ref deleteExistingSensor, ref period);

            var createSensor = existingSensor == null || deleteExistingSensor;

            TimeSeriesPointFilter.FilterTimeSeriesPoints(timeSeries);

            var exportSummary = $"{timeSeries.NumPoints} points [{timeSeries.Points.FirstOrDefault()?.Timestamp.DateTimeOffset:O} to {timeSeries.Points.LastOrDefault()?.Timestamp.DateTimeOffset:O}] from '{timeSeriesDescription.Identifier}' with Frequency={period}";

            ExportedTimeSeriesCount += 1;
            ExportedPointCount      += timeSeries.NumPoints ?? 0;

            if (Context.DryRun)
            {
                if (deleteExistingSensor)
                {
                    LogDryRun($"Would delete existing sensor '{existingSensor?.Identifier}'");
                }

                if (createSensor)
                {
                    LogDryRun($"Would create new sensor for '{timeSeriesDescription.Identifier}'");
                }

                LogDryRun($"Would export {exportSummary}.");
                return;
            }

            Log.Info($"Exporting {exportSummary} ...");

            if (deleteExistingSensor)
            {
                Sos.DeleteSensor(timeSeries);
                Sos.DeleteDeletedObservations();
            }

            if (createSensor)
            {
                var sensor = Sos.InsertSensor(timeSeries);

                assignedOffering = sensor.AssignedOffering;
            }

            Sos.InsertObservation(assignedOffering, locationInfo.LocationData, locationInfo.LocationDescription, timeSeries, timeSeriesDescription);
        }