public void StatefulOperator_Versioning_NotSupportedByDefault()
        {
            var o = Observer.Create <int>(_ => { }, _ => Assert.Fail(), () => Assert.Fail());

            var state         = new MockOperatorStateContainer();
            var writerFactory = state.CreateWriter();
            var readerFactory = state.CreateReader();

            var mo = new MyOperator(o)
            {
                State = 42
            };

            mo.Start();

            var writer = writerFactory.Create(mo);

            Assert.ThrowsException <NotSupportedException>(() => mo.SaveState(writer, new Version(2, 0, 0, 0)));

            var mor = new MyOperator(o);

            var reader = readerFactory.Create(mor);

            Assert.ThrowsException <NotSupportedException>(() => mor.LoadState(reader, new Version(2, 0, 0, 0)));
        }
示例#2
0
 private void processingExeption(string message)
 {
     buttonLock();
     textBox_Enter.Text = message;
     textBox_Enter.Tag  = false;
     enterOperator      = null;
     executableOperator = new KeyVal <bool, MyOperator>(false, enterOperator);
 }
示例#3
0
 // действия, при нажатии кнопак, соответствующим бинарным арифметическим операциям
 private void buttBinaryOperator_Click(object sender)
 {
     registrEnter = Convert.ToDouble(textBox_Enter.Text);                // записываем число в регистр
     arithmeticProcessing();
     textBox_Calculating.Text = textBuff + ((Button)sender).Text + " ";
     enterOperator            = (MyOperator)(((Button)sender).Tag);      // выбираем операцию в очередь (в временный буфер)
     clickButtEqually         = false;
 }
        public void StatefulUnaryOperator_NoSubscriptionsByDefault()
        {
            var o  = Observer.Create <int>(_ => { }, _ => Assert.Fail(), () => Assert.Fail());
            var mo = new MyOperator(o)
            {
                State = 42
            };

            // Seems a bit inconsistent that the subscription has zero inputs
            // but it returns a nop subscription from its unary property..
            Assert.AreEqual(0, ((IOperator)mo).Inputs.Count());
            Assert.IsNotNull(mo.Input);
        }
示例#5
0
        public void AndDatetimeNull(string iFieldName, DateTime iValue, MyOperator iOperator)
        {
            var ex1 = Expression.Property(c, iFieldName);
            var ex2 = Expression.Convert(Expression.Constant(iValue), typeof(T).GetProperty(iFieldName).PropertyType);

            Expression con2 = null;

            switch (iOperator)
            {
            case MyOperator.GreaterThanOrEqual:
                con2 = Expression.GreaterThanOrEqual(ex1, ex2);
                break;

            case MyOperator.GreaterThan:
                con2 = Expression.GreaterThan(ex1, ex2);
                break;

            case MyOperator.LessThan:
                con2 = Expression.LessThan(ex1, ex2);
                break;

            case MyOperator.LessThanOrEqual:
                con2 = Expression.LessThanOrEqual(ex1, ex2);
                break;

            case MyOperator.Like:
                throw new MyException("不支持");

            case MyOperator.Equal:
                con2 = Expression.Equal(ex1, ex2);
                break;

            case MyOperator.NotEqual:
                con2 = Expression.NotEqual(ex1, ex2);
                break;

            case MyOperator.ListIntContains:
                throw new MyException("不支持");

            default:
                break;
            }

            if (con2 != null)
            {
                this.mConditionList.Add(new MyExpression(MyExpressionType.And, con2));
            }
        }
        public void StatefulOperator_Basics()
        {
            var o = Observer.Create <int>(_ => { }, _ => Assert.Fail(), () => Assert.Fail());

            {
                var mo = new MyOperator(o);

                mo.Start();
                Assert.IsTrue(mo.Started);
                //Assert.IsFalse(mo.StateChanged); // TODO: Hmm, can we do this?

                mo.OnNext(42);
                mo.OnNext(43);
                mo.OnCompleted();

                Assert.IsTrue(new[] { 42, 43 }.SequenceEqual(mo.Values));
                Assert.IsTrue(mo.Done);

                mo.Dispose();
                Assert.IsTrue(mo.Disposed);

                mo.Dispose();
                Assert.IsTrue(mo.Disposed);
            }

            {
                var mo = new MyOperator(o);

                mo.Start();
                Assert.IsTrue(mo.Started);
                //Assert.IsFalse(mo.StateChanged); // TODO: Hmm, can we do this?

                var ex = new Exception();

                mo.OnNext(42);
                mo.OnNext(43);
                mo.OnError(ex);

                Assert.IsTrue(new[] { 42, 43 }.SequenceEqual(mo.Values));
                Assert.AreSame(ex, mo.Error);

                mo.Dispose();
                Assert.IsTrue(mo.Disposed);

                mo.Dispose();
                Assert.IsTrue(mo.Disposed);
            }
        }
示例#7
0
        private void Form1_Load(object sender, EventArgs e)
        {
            enterOperator      = null;                                                  // показываем, что метод оператора не выбран
            executableOperator = new KeyVal <bool, MyOperator>(false, enterOperator);   // начальная инициализация пары значений, в которой хранятся делигат выполняемого метода и информация о его выполнении
            textBox_Enter.Text = "0";
            textBox_Enter.Tag  = false;                                                 // показываем, что ввод цифр еще не производился
            // инициализация поля Tag кнопок арифметических бинарных операций объектами делигата хранящими соответствующие методы
            butt_sum.Tag   = new MyOperator(sum);
            butt_sub.Tag   = new MyOperator(substaction);
            butt_multi.Tag = new MyOperator(multiplication);
            butt_div.Tag   = new MyOperator(division);

            butt_MR.Enabled     = false;
            butt_MC.Enabled     = false;
            activateButton      = true;
            activateButtonVC_MR = false;
        }
示例#8
0
        public void Or(string iFieldName, object iValue, MyOperator iOperator)
        {
            Expression con2 = null;

            switch (iOperator)
            {
            case MyOperator.GreaterThanOrEqual:
                con2 = Expression.GreaterThanOrEqual(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            case MyOperator.GreaterThan:
                con2 = Expression.GreaterThan(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            case MyOperator.LessThan:
                con2 = Expression.LessThan(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            case MyOperator.LessThanOrEqual:
                con2 = Expression.LessThanOrEqual(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            case MyOperator.Like:
                con2 = Expression.Call(Expression.Property(c, typeof(T).GetProperty(iFieldName)), typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), Expression.Constant(iValue));
                break;

            case MyOperator.NotEqual:
                con2 = Expression.NotEqual(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            case MyOperator.Equal:
                con2 = Expression.Equal(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            default:
                break;
            }

            if (con2 != null)
            {
                this.mConditionList.Add(new MyExpression(MyExpressionType.Or, con2));
            }
        }
示例#9
0
        public void SubscriptionVisitor_Simple()
        {
            var g1 = new MySpecialOperator("g1");
            var c1 = new MyOperator("c1", g1);
            var c2 = new MySpecialOperator("c2");
            var c3 = new MyOperator("c3");
            var p  = new MyOperator("p", c1, c2, c3);

            var log  = new List <int>();
            var lst1 = new List <string>();
            var lst2 = new List <string>();

            SubscriptionVisitor
            .Do <IMyOperator>(op => { log.Add(1); lst1.Add(op.Name); })
            .Do <IMySpecialOperator>(op => { log.Add(2); lst2.Add(op.Name); })
            .Apply(p);

            Assert.IsTrue(new[] { 1, 1, 1, 2, 1, 2, 1 }.SequenceEqual(log));
            Assert.IsTrue(new[] { "p", "c1", "g1", "c2", "c3" }.SequenceEqual(lst1));
            Assert.IsTrue(new[] { "g1", "c2" }.SequenceEqual(lst2));
        }
示例#10
0
        public Expression CreateAnd(string iFieldName, object iValue, MyOperator iOperator)
        {
            Expression con2 = null;

            switch (iOperator)
            {
            case MyOperator.GreaterThanOrEqual:
                con2 = Expression.GreaterThanOrEqual(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            case MyOperator.GreaterThan:
                con2 = Expression.GreaterThan(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            case MyOperator.LessThan:
                con2 = Expression.LessThan(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            case MyOperator.LessThanOrEqual:
                con2 = Expression.LessThanOrEqual(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            case MyOperator.Like:
                con2 = Expression.Call(Expression.Property(c, typeof(T).GetProperty(iFieldName)), typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), Expression.Constant(iValue));
                break;

            case MyOperator.Equal:
                con2 = Expression.Equal(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            case MyOperator.NotEqual:
                con2 = Expression.NotEqual(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            default:
                break;
            }

            return(con2);
        }
示例#11
0
        private void OperationButton_Click(object sender, RoutedEventArgs e)
        {
            if (double.TryParse(resultLabel.Content.ToString(), out lastNumber))
            {
                resultLabel.Content = "0";
            }

            if (sender == addButton)
            {
                myOp = MyOperator.Addition;
            }
            if (sender == minusButton)
            {
                myOp = MyOperator.Subtraction;
            }
            if (sender == mulButton)
            {
                myOp = MyOperator.Multiplication;
            }
            if (sender == divButton)
            {
                myOp = MyOperator.Division;
            }
        }
示例#12
0
    // permutate operation
    private bool CalculateOperation(MyOperator o)
    {
        if (!IsValidOperator(o.matrix))
        {
            return(false);
        }
        int qsize = o.qubits.Length;

        if (Math.Pow(2, qsize) != o.size)
        {
            return(false);
        }
        curOpSize = o.size;

        double[] newAmplitudes = new Double[(int)Math.Pow(2, nQubits)];

        for (int i = 0; i < (int)Math.Pow(2, nQubits); i++)
        {
            newAmplitudes [i] = 0;
        }
        for (int a = 0; a < statesAmplitudes.Length; a++)
        {
            double ampl = statesAmplitudes [a];
            string res  = "";
            for (int i = 0; i < statesAmplitudes.Length; i++)
            {
                res = res + " " + statesAmplitudes [i];
            }
            print(res + " first " + a);
            print(ampl);

            // choose only nessasary bits from state for calculation
            int[] bits = new Int32[nQubits];
            Dec2Bin(a, ref bits);

            int[] nbits = new Int32[qsize];             //nbits length - qsize
            for (int i = 0; i < qsize; i++)
            {
                nbits[i] = bits [o.qubits [i]];
            }

            // transform state to vector
            double[] vector = new Double[o.size];
            for (int i = 0; i < o.size; i++)
            {
                vector [i] = 0.0;
            }
            int k = 0;
            Bin2Dec(ref k, nbits);
            vector [k] = 1.0;

            // apply operator
            vector = MultMatrixVector(o.matrix, vector);

            // change them
            for (int i = 0; i < o.size; i++)
            {
                // if vector [i] != 0, so binary decomposition of i need to be setup for some places in bits
                if (vector [i] > 0.00001 || vector[i] < -0.00001)
                {
                    Dec2Bin(i, ref nbits);

                    // change bits in decomposition
                    for (int j = 0; j < qsize; j++)
                    {
                        bits[o.qubits [j]] = nbits[j];
                    }

                    // set up amplitudes
                    Bin2Dec(ref k, bits);
                    newAmplitudes [k] += vector[i] * ampl;
                }
            }
            res = "";
            for (int i = 0; i < statesAmplitudes.Length; i++)
            {
                res = res + " " + newAmplitudes [i];
            }
            print(res + " second " + a);
        }
        statesAmplitudes = newAmplitudes;
        return(true);
    }
示例#13
0
    /*private bool AddAlgoFile(){
     *      path = EditorUtility.OpenFilePanel("Choose algorithm description", "", "txt");
     *      if (path != "")
     *              return true;
     *      return false;
     * }
     */
    private bool ReadAlgoFromFile()
    {
        try {
            //Read operator's matrix size, read matrix
            path = "test.txt";
            int op = 0;
            operatorsList = new List <MyOperator> ();
            string[] Algo = System.IO.File.ReadAllLines(path);

            // file need to contain qubit quantity as first line!
            nQubits = int.Parse(Algo [op]);
            if (nQubits > MAX_QUBITS)
            {
                message.text = "Too much qubits! No more than " + MAX_QUBITS + " is allowed!";
                throw new UnityException("Too much qubits");
            }
            op++;
            // read start amplitudes
            string   stateString = Algo [op];
            string[] states      = stateString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            statesAmplitudes = new Double[(int)Math.Pow(2, nQubits)];
            for (int i = 0; i < (int)Math.Pow(2, nQubits); i++)
            {
                statesAmplitudes [i] = Single.Parse(states [i]);
            }
            op++;
            for (; op < Algo.Length;)
            {
                MyOperator mOperator = new MyOperator();

                // parse operator matrix size
                mOperator.size = int.Parse(Algo [op]);
                op++;
                // parse operator matrix
                mOperator.matrix = new double[mOperator.size][];
                for (int i = 0; i < mOperator.size; i++)
                {
                    mOperator.matrix [i] = new Double[mOperator.size];
                    string[] Operator = Algo [op + i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int j = 0; j < Operator.Length; j++)
                    {
                        mOperator.matrix [i] [j] = Single.Parse(Operator [j]);
                    }
                }
                op += mOperator.size;

                string[] Arguments = Algo [op].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                mOperator.qubits = new int[Arguments.Length];
                for (int i = 0; i < Arguments.Length; i++)
                {
                    mOperator.qubits [i] = int.Parse(Arguments [i]);
                }

                operatorsList.Add(mOperator);
                op++;
            }
        } catch (Exception e) {
            print(e.Message);
            print("ваще не ок");
            return(false);
        }
        return(true);
    }
        public void StatefulOperator_State()
        {
            var o = Observer.Create <int>(_ => { }, _ => Assert.Fail(), () => Assert.Fail());

            var state         = new MockOperatorStateContainer();
            var writerFactory = state.CreateWriter();
            var readerFactory = state.CreateReader();

            var mo = new MyOperator(o)
            {
                State = 42
            };

            mo.Start();
            Assert.IsTrue(mo.Started);
            Assert.IsTrue(mo.StateChanged);

            Assert.ThrowsException <ArgumentNullException>(() => mo.SaveState(null, mo.Version));

            var writer = writerFactory.Create(mo);

            mo.SaveState(writer, mo.Version);

            Assert.IsTrue(mo.StateChanged);
            mo.OnStateSaved();
            Assert.IsFalse(mo.StateChanged);

            var mor = new MyOperator(o);

            var reader = readerFactory.Create(mor);

            Assert.ThrowsException <ArgumentNullException>(() => mor.LoadState(null, mor.Version));

            mor.LoadState(reader, mor.Version);

            mor.Start();
            Assert.IsTrue(mor.Started);

            Assert.AreEqual(42, mor.State);

            mo.State = 43;
            Assert.IsTrue(mo.StateChanged);

            writer = writerFactory.Create(mo);
            mo.SaveState(writer, mo.Version);
            mo.OnStateSaved();
            Assert.IsFalse(mo.StateChanged);

            var moq = new MyOperator(o);

            reader = readerFactory.Create(moq);
            moq.LoadState(reader, moq.Version);

            moq.Start();
            Assert.IsTrue(moq.Started);

            Assert.AreEqual(43, moq.State);

            mo.Dispose();

            Assert.IsTrue(mo.Disposed);

            writer = writerFactory.Create(mo);
            mo.SaveState(writer, mo.Version);
            mo.OnStateSaved();

            var mop = new MyOperator(o);

            reader = readerFactory.Create(mop);
            mop.LoadState(reader, mop.Version);
            mop.Start();

            Assert.IsTrue(mop.Disposed);
            Assert.IsFalse(mop.Started);
        }
示例#15
0
 public void Operator_Test()
 {
     MyOperator.Operator_Test();
 }
示例#16
0
        public void And(string iFieldName, object iValue, MyOperator iOperator)
        {
            Expression con2 = null;

            switch (iOperator)
            {
            case MyOperator.GreaterThanOrEqual:
                con2 = Expression.GreaterThanOrEqual(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            case MyOperator.GreaterThan:
                con2 = Expression.GreaterThan(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            case MyOperator.LessThan:
                con2 = Expression.LessThan(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            case MyOperator.LessThanOrEqual:
                con2 = Expression.LessThanOrEqual(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            case MyOperator.Like:
                con2 = Expression.Call(Expression.Property(c, typeof(T).GetProperty(iFieldName)), typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), Expression.Constant(iValue));
                break;

            case MyOperator.Equal:
                con2 = Expression.Equal(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            case MyOperator.NotEqual:
                con2 = Expression.NotEqual(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(iValue));
                break;

            case MyOperator.ListIntContains:
                if (iValue is List <int> && (iValue as List <int>).Count > 0)
                {
                    List <int>        values  = iValue as List <int>;
                    List <Expression> conList = new List <Expression>();
                    foreach (var item in values)
                    {
                        var con = Expression.Equal(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(item));

                        conList.Add(con);
                    }

                    if (conList.Count > 1)
                    {
                        con2 = Expression.Or(conList[0], conList[1]);

                        for (int i = 2; i < conList.Count; i++)
                        {
                            con2 = Expression.Or(con2, conList[i]);
                        }
                    }
                    else
                    {
                        con2 = conList[0];
                    }
                }

                break;

            case MyOperator.ListIntNotIn:
                if (iValue is List <int> && (iValue as List <int>).Count > 0)
                {
                    List <int>        values  = iValue as List <int>;
                    List <Expression> conList = new List <Expression>();
                    foreach (var item in values)
                    {
                        var con = Expression.NotEqual(Expression.Property(c, typeof(T).GetProperty(iFieldName)), Expression.Constant(item));

                        conList.Add(con);
                    }

                    if (conList.Count > 1)
                    {
                        con2 = Expression.And(conList[0], conList[1]);

                        for (int i = 2; i < conList.Count; i++)
                        {
                            con2 = Expression.And(con2, conList[i]);
                        }
                    }
                    else
                    {
                        con2 = conList[0];
                    }
                }

                break;

            default:
                break;
            }

            if (con2 != null)
            {
                this.mConditionList.Add(new MyExpression(MyExpressionType.And, con2));
            }
        }
 public TestClass(MyOperator op, Object operand) {
     this.Operator = op;
     this.Operand = operand;
 }