Esempio n. 1
0
        /// <summary>
        /// 論理式からIkarnoughMapを作成
        /// </summary>
        /// <param name="logicExp">論理式</param>
        /// <returns>map</returns>
        public IKarnoughMap solve(string logicExp)
        {
            string       json = SolveMap(logicExp);
            IKarnoughMap map  = deserializer(json);

            return(map);
        }
Esempio n. 2
0
        public void expTest()
        {
            KarnoughEngine en = new KarnoughEngine();

            en.script     = @"..\..\..\Karnaugh-Logic\pycode\jsontest.py";
            en.python_env = python;
            IKarnoughMap map = en.solve("x+a");

            maptest(map);
        }
Esempio n. 3
0
        public void generateTest()
        {
            KarnoughEngine en = new KarnoughEngine();

            en.script     = @"..\..\..\Karnaugh-Logic\pycode\jsontest.py";
            en.python_env = python;
            IKarnoughMap map = en.solve("x+a");
            string       exp = en.getExpress(map);

            Assert.AreEqual("not(value1)", exp);
        }
        /// <summary>
        /// カルノー図データから
        /// </summary>
        /// <param name="map"></param>
        internal void DrawGraph(IKarnoughMap map)
        {
            render.BeginDraw();
            render.Clear(new RawColor4(0.3f, 0.3f, 0.3f, 0f));

            switch (map.dimension)
            {
            case 2:
                break;
            }
        }
Esempio n. 5
0
        public void jsontest()
        {
            //KarnoughEngine en = new KarnoughEngine();
            string jsonpath = @"../../../TestJson.json";
            string json;

            using (FileStream fs = File.OpenRead(jsonpath))
            {
                using (StreamReader sr = new StreamReader(fs)){
                    json = sr.ReadToEnd();
                }
            }

            IKarnoughMap map = KarnoughEngine.deserializer(json);

            maptest(map);
        }
        private string getlabel(IKarnoughMap com, int count, bool isXlabel)
        {
            string str = "";

            if (isXlabel)
            {
                int loop = 0;
                for (int i = 0; i < count; i++)
                {
                    if (i % 2 == 0)
                    {
                        loop++;
                    }
                }
                for (int i = 0; i < loop; i++)
                {
                    str += com.valueNames[i];
                    if (i != (loop - 1))
                    {
                        str += "/";
                    }
                }
            }
            else
            {
                int loop = 0;
                for (int i = 0; i < count; i++)
                {
                    if (i % 2 == 1)
                    {
                        loop++;
                    }
                }
                int shift = com.valueNames.Count - loop;
                for (int i = 0; i < loop; i++)
                {
                    str += com.valueNames[shift + i];
                    if (i != (loop - 1))
                    {
                        str += "\n";
                    }
                }
            }

            return(str);
        }
Esempio n. 7
0
        private void maptest(IKarnoughMap map)
        {
            Assert.AreEqual("value1", map.valueNames[0]);
            Assert.AreEqual("value2", map.valueNames[1]);
            Assert.AreEqual("value3", map.valueNames[2]);

            Assert.AreEqual(TruthValue.True, map.getMapPoint(0, 0).values);
            Assert.AreEqual(TruthValue.True, map.getMapPoint(1, 0).values);
            Assert.AreEqual(TruthValue.True, map.getMapPoint(0, 1).values);
            Assert.AreEqual(TruthValue.True, map.getMapPoint(1, 1).values);
            Assert.AreEqual(TruthValue.False, map.getMapPoint(2, 0).values);
            Assert.AreEqual(TruthValue.False, map.getMapPoint(3, 0).values);
            Assert.AreEqual(TruthValue.False, map.getMapPoint(2, 1).values);
            Assert.AreEqual(TruthValue.False, map.getMapPoint(3, 1).values);

            Assert.AreEqual((byte)1, map.getMapPoint(0, 0).blockValue);
            Assert.AreEqual((byte)1, map.getMapPoint(1, 0).blockValue);
            Assert.AreEqual((byte)1, map.getMapPoint(0, 1).blockValue);
            Assert.AreEqual((byte)1, map.getMapPoint(1, 1).blockValue);
        }
Esempio n. 8
0
        private async void RunButton_Click(object sender, EventArgs e)
        {
            if (File.Exists(PythonPathBox.Text) == false)
            {
                MessageBox.Show("指定したPython実行環境が存在しません。", "Pythonエラー");
                return;
            }
            statusLabel.Text = "簡略化中";

            KarnoughEngine eng = new KarnoughEngine();

            eng.python_env = PythonPathBox.Text;
            eng.script     = @"write_json.py";
            IKarnoughMap map = await Task.Run(() => genMap(eng, LogicTexBox.Text));

            string exp = genExp(eng, map);

            afterExpBox.Text = exp;
            karnaughCnt.testDraw(map);

            statusLabel.Text = "簡略化完了";
        }
 public void testDraw(IKarnoughMap map)
 {
     drawer.Value2Map(map);
 }
Esempio n. 10
0
        private string genExp(KarnoughEngine eng, IKarnoughMap map)
        {
            IKarnoughLogic log = eng.getExp(map);

            return(log.genLogicExpression());
        }
        //ここからカルノー図描画メソッド

        public void Value2Map(IKarnoughMap map)
        {
            int valueCount = map.valueNames.Count();

            //int valueCount = 3;
            render.BeginDraw();

            render.Clear(background);

            int margin      = 10;
            int ColumHeight = 30;
            int RowWidth    = 100;

            //外枠
            render.DrawLine(new RawVector2(margin, margin), new RawVector2(margin, height - margin), LineColor);
            render.DrawLine(new RawVector2(margin, margin), new RawVector2(width - margin, margin), LineColor);
            render.DrawLine(new RawVector2(width - margin, height - margin), new RawVector2(width - margin, margin), LineColor);
            render.DrawLine(new RawVector2(width - margin, height - margin), new RawVector2(margin, height - margin), LineColor);

            render.DrawLine(new RawVector2(margin, margin + (ColumHeight * 2)), new RawVector2(width - margin, margin + (ColumHeight * 2)), LineColor);
            render.DrawLine(new RawVector2(margin + (RowWidth * 2), margin), new RawVector2(margin + (RowWidth * 2), height - margin), LineColor);

            render.DrawLine(new RawVector2(margin + (RowWidth * 2), margin + ColumHeight), new RawVector2(width - margin, margin + ColumHeight), LineColor);
            render.DrawLine(new RawVector2(margin + RowWidth, margin + (ColumHeight * 2)), new RawVector2(margin + RowWidth, height - margin), LineColor);

            //行と列の数の設定
            int columCount = 2;
            int rowCount   = 2;

            for (int i = 3; i <= valueCount; i++)
            {
                if (i % 2 == 1)
                {
                    columCount = columCount * 2;
                }
                else
                {
                    rowCount = rowCount * 2;
                }
            }

            //セルの大きさ
            int valueWidth  = ((width - margin) - (margin + (RowWidth * 2))) / columCount;
            int valueHeight = ((height - margin) - (margin + (ColumHeight * 2))) / rowCount;

            //縦の罫線
            for (int i = 1; i < columCount; i++)
            {
                //int valueWidth = ((width - margin) - (margin + (RowWidth * 2)))/columCount;
                int x       = margin + (RowWidth * 2) + (valueWidth * i);
                int y_start = margin + ColumHeight;
                int y_end   = height - margin;

                render.DrawLine(new RawVector2(x, y_start), new RawVector2(x, y_end), LineColor);
            }

            //横の罫線
            for (int i = 1; i < rowCount; i++)
            {
                //int valueHeight = ((height - margin) - (margin + (ColumHeight * 2)))/rowCount;
                int y       = margin + (ColumHeight * 2) + (valueHeight * i);
                int x_start = margin + RowWidth;
                int x_end   = width - margin;

                render.DrawLine(new RawVector2(x_start, y), new RawVector2(x_end, y), LineColor);
            }

            //文字の描画
            var fontFactory = new SharpDX.DirectWrite.Factory();
            var textFormat  = new TextFormat(fontFactory, "メイリオ", 24.0f);

            textFormat.TextAlignment = TextAlignment.Center;
            var textBrush = new SharpDX.Direct2D1.SolidColorBrush(render, new RawColor4(1f, 1f, 1f, 1f));

            //列のインデックス
            string[] columStr = valueStr(columCount);
            for (int i = 0; i < columCount; i++)
            {
                int x_shift = margin + (RowWidth * 2);
                int x_start = x_shift + (valueWidth * i);
                int x_end   = x_shift + (valueWidth * (i + 1));

                int y_start = margin + ColumHeight;
                int y_end   = margin + (ColumHeight * 2);

                TextFormat textTmpFormat;
                if (columCount >= 8)
                {
                    textTmpFormat = new TextFormat(fontFactory, "メイリオ", 20.0f);
                    textTmpFormat.TextAlignment = TextAlignment.Center;
                }
                else
                {
                    textTmpFormat = textFormat;
                }
                render.DrawText(columStr[i], textTmpFormat, new RawRectangleF(x_start, y_start, x_end, y_end), textBrush, DrawTextOptions.None);
            }

            //行のインデックス
            string[] rowStr = valueStr(rowCount);
            for (int i = 0; i < rowCount; i++)
            {
                int x_start = margin + RowWidth;
                int x_end   = x_start + RowWidth;

                int y_shift = margin + (ColumHeight * 2);
                int y_start = y_shift + (valueHeight * i) + (valueHeight / 2 - 10);
                int y_end   = y_start + (valueHeight * (i + 1));

                render.DrawText(rowStr[i], textFormat, new RawRectangleF(x_start, y_start, x_end, y_end), textBrush, DrawTextOptions.None);
            }

            //mapを描画
            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < columCount; j++)
                {
                    int xStartCell = margin + (RowWidth * 2) + (valueWidth * j);
                    int yStartCell = margin + (ColumHeight * 2) + (valueHeight * i) + (valueHeight / 2 - 10);
                    int xEndCell   = xStartCell + valueWidth;
                    int yEndCell   = yStartCell + valueHeight;


                    TruthValue v = map.getMapPoint(j, i).values;
                    string     s = "";
                    switch (v)
                    {
                    case TruthValue.False:
                        s = "0";
                        break;

                    case TruthValue.True:
                        s = "1";
                        break;

                    case TruthValue.Null:
                        s = "d";
                        break;
                    }

                    var MaptextFormat = new TextFormat(fontFactory, "メイリオ", 30.0f);
                    var mapBrush      = indexFontColor(0);
                    MaptextFormat.TextAlignment = TextAlignment.Center;
                    render.DrawText(s, MaptextFormat, new RawRectangleF(xStartCell, yStartCell, xEndCell, yEndCell), mapBrush, DrawTextOptions.None);
                }
            }

            //囲む(blockid=10まで)
            for (byte i = 1; i < 10; i++)
            {
                List <IAxisKarnoughComponent> com = map.getBlockIDList(i);
                if (com.Count == 0)
                {
                    break;
                }

                int maxX = 0;
                int maxY = 0;
                int minX = 16;
                int minY = 16;

                foreach (IAxisKarnoughComponent c in com)
                {
                    if (maxX < c.x)
                    {
                        maxX = c.x;
                    }
                    if (minX > c.x)
                    {
                        minX = c.x;
                    }
                    if (maxY < c.y)
                    {
                        maxY = c.y;
                    }
                    if (minY > c.y)
                    {
                        minY = c.y;
                    }
                }

                var color      = indexFontColor(i);
                int rectMargin = 10;
                int startX     = (margin + (RowWidth * 2) + (valueWidth * minX)) + rectMargin;
                int endX       = (margin + (RowWidth * 2) + (valueWidth * (maxX + 1))) - rectMargin;
                int startY     = (margin + (ColumHeight * 2) + (valueHeight * minY)) + rectMargin;
                int endY       = (margin + (ColumHeight * 2) + (valueHeight * (maxY + 1))) - rectMargin;
                render.DrawRectangle(new RawRectangleF(startX, startY, endX, endY), color);
            }

            //列の変数名
            int x_label_Xstart = margin + (RowWidth * 2);
            int x_label_Xend   = width - margin;
            int x_label_Ystart = margin;
            int x_label_Yend   = margin + ColumHeight;

            render.DrawText(getlabel(map, valueCount, true), textFormat, new RawRectangleF(x_label_Xstart, x_label_Ystart, x_label_Xend, x_label_Yend), textBrush, DrawTextOptions.None);

            //行の変数名
            int y_label_Xstart = margin;
            int y_label_Xend   = margin + RowWidth;
            int y_label_Ystart = margin + (ColumHeight * 2) + ((valueHeight * rowCount) / 2) - (10 * rowCount / 4);
            int y_label_Yend   = height - margin;

            render.DrawText(getlabel(map, valueCount, false), textFormat, new RawRectangleF(y_label_Xstart, y_label_Ystart, y_label_Xend, y_label_Yend), textBrush, DrawTextOptions.None);

            render.EndDraw();
        }
Esempio n. 12
0
        /// <summary>
        /// カルノー図から論理式を生成
        /// </summary>
        /// <param name="map">カルノー図</param>
        /// <returns>論理式</returns>
        public string getExpress(IKarnoughMap map)
        {
            IKarnoughLogic logi = getExp(map);

            return(logi.genLogicExpression());
        }
Esempio n. 13
0
        public IKarnoughLogic getExp(IKarnoughMap map)
        {
            IKarnoughLogic outputVal = new KarnoughLogic();

            outputVal.valueNames = map.valueNames;

            //List<List<bool>> valuelists = new List<List<bool>>();

            //とりあえずBlockIDが10まで
            for (byte i = 1; i < 10; i++)
            {
                List <List <bool> >           valuelists = new List <List <bool> >();
                List <IAxisKarnoughComponent> com        = map.getBlockIDList(i);
                List <TruthValue>             truthval   = new List <TruthValue>();;
                if (com.Count == 0)
                {
                    break;
                }

                foreach (IAxisKarnoughComponent c in com)
                {
                    List <bool> booleanLST = axisX(map.valueNames.Count(), c.x);
                    booleanLST.AddRange(axisY(map.valueNames.Count(), c.y));
                    valuelists.Add(booleanLST);
                }

                for (int j = 0; j < map.valueNames.Count(); j++)
                {
                    List <bool> Xlist = new List <bool>();
                    foreach (List <bool> lst in valuelists)
                    {
                        Xlist.Add(lst[j]);
                    }

                    bool       prevLogic = Xlist[0];
                    TruthValue result;
                    if (prevLogic == true)
                    {
                        result = TruthValue.True;
                    }
                    else
                    {
                        result = TruthValue.False;
                    }
                    foreach (bool b in Xlist)
                    {
                        if (prevLogic != b)
                        {
                            result = TruthValue.Null;
                            break;
                        }
                    }
                    truthval.Add(result);
                }
                outputVal.values.Add(truthval);
            }

            //未定義の部分(blockid=0)
            List <IAxisKarnoughComponent> nodefinedBool = map.getBlockIDList(0);

            foreach (IAxisKarnoughComponent c in nodefinedBool)
            {
                if (c.values == TruthValue.True)
                {
                    List <bool> valList = axisX(map.valueNames.Count(), c.x);
                    valList.AddRange(axisY(map.valueNames.Count(), c.y));
                    List <TruthValue> val = new List <TruthValue>();
                    foreach (bool b in valList)
                    {
                        if (b == true)
                        {
                            val.Add(TruthValue.True);
                        }
                        else
                        {
                            val.Add(TruthValue.False);
                        }
                    }
                    outputVal.values.Add(val);
                }
            }

            return(outputVal);
        }