Exemple #1
0
        public object doMLVForPoint(GameCell gameCell)
        {
            log();
            object result="Клетка пуста.";
            if (gameCell.FrameExample != null && gameCell.FrameExample.BaseFrame != null && gameCell.FrameExample.BaseFrame.FrameId != -1)
            {
                situations = gatherSituationsList();

                situationExample = new FrameExample();
                Slot slotAgent = new Slot();
                slotAgent.SlotName = "agent";
                slotAgent.SlotType = SlotType.Frame;
                slotAgent.SlotInheritance = SlotInherit.Override;
                slotAgent.IsSystem = false;
                situationExample.AddSlot(slotAgent);
                situationExample.SetValue("agent", gameCell.FrameExample);
                Frame situationToCheck = this.getNextFrameToCheck();

                while (!situationExample.ContainsSlot("action") && situationToCheck != null) //пока не определили целевой слот "действие"
                {
                    checkedFramesIDs.Add(situationToCheck.FrameId);
                    if (checkSituation(situationToCheck, situationExample))
                    {
                        Slot actionSlot = situationToCheck.GetSlotByName("action");
                        situationExample.AddSlot(actionSlot);
                        situationExample.SetValue(actionSlot.SlotName, actionSlot.SlotDefault);
                    }
                    situationToCheck = this.getNextFrameToCheck();
                }
                result = null;
                if (situationExample.ContainsSlot("action"))
                    result = situationExample.Value("action");
            }
            return result;
        }
Exemple #2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="gameField">Игровое поле, которому принадлежит ячейка.</param>
        /// <param name="x">Индекс ряда ячейки на игровом поле.</param>
        /// <param name="y">Индекс столбца ячейки на игровом поле.</param>
        /// <param name="size">Размер ячейки (в пикселях).</param>
        public GameCell(GameField gameField, int row, int column, int size, FrameExample frameExample = null)
            : base()
        {
            this.FrameExample = frameExample;

            pictureBox = new PictureBox();
            pictureBox.Dock = DockStyle.Fill;
            this.Controls.Add(pictureBox);
            this.AllowDrop = true;

            this.gameField = gameField;
            this.Size = size;
            pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;

            String imageFile;
            if (frameExample != null)
            {
                //Фрейм-прототип этого экземпляра должен быть унаследован от фрейма-объекта, который должен содержать слот "image".
                imageFile = (string)frameExample.Value("image");
                if (imageFile == null)
                    throw new NullReferenceException("slot 'image' not found for frame " + frameExample.BaseFrame.FrameName + " with id " + frameExample.BaseFrame.FrameId.ToString());
            }
            else
            {
                imageFile = "Images\\grass.jpg";
            }
            pictureBox.Image = Image.FromFile(imageFile);

            this.Row = row;
            this.Column = column;
            this.DragEnter += new DragEventHandler(GameCell_DragEnter);
            this.DragDrop += new DragEventHandler(GameCell_DragDrop);
            pictureBox.Click += new EventHandler(pictureBox_Click);
        }
Exemple #3
0
        /// <summary>
        /// Вычисляет выражение, стоящее в левой или правой части condition'а. 
        /// </summary>
        /// <param name="frameContext"></param>
        /// <param name="expression"></param>
        /// <param name="expressionType"></param>
        /// <returns></returns>
        private object evaluateExpression(Frame frameContext, Slot testedSlot, FrameExample frameMemory, FrameExample testedSubFrame, string expression, out SlotType expressionType)
        {
            //object result = null;
            //expressionType = SlotType.String;

            object result = frameMemory;
            expressionType = SlotType.Frame;

            expression = expression.Trim().ToLower();
            int currentIndex = 0;
            string currentPart="";
            while (currentIndex < expression.Length)
            {
                switch (expression[currentIndex])
                {
                    case '[':
                        while (expression[++currentIndex] != ']')
                            currentPart += expression[currentIndex];
                        ++currentIndex;
                        break;
                    case '.':
                        ++currentIndex;
                        continue;
                        break;
                    default:
                        currentPart = expression.Substring(currentIndex);
                        currentPart = currentPart.Replace(" ", "");
                        currentIndex = expression.Length;
                        //обработка всяких там +-1 и т.д.
                        break;
                }
                Slot tmpSlot;
                int tmpInt;
                switch (expressionType)
                {
                    case SlotType.Frame:
                        //tmpSlot = ((FrameExample)result).BaseFrame.GetSlotByName(currentPart);
                        //TODO: костыль ИМХО :(
                        if (currentPart == testedSlot.SlotNameTrimmed)
                            result = testedSubFrame;
                        else
                        {
                            tmpSlot = ((FrameExample)result).GetSlotByName(currentPart);
                            if (tmpSlot.SlotType != SlotType.FramePrototype)
                                result = ((FrameExample)result).Value(currentPart);
                            else
                                result = KnowLedgeBase.getFrameByID(tmpSlot.frameId);
                            expressionType = tmpSlot.SlotType;
                        }
                        break;
                    case SlotType.FramePrototype:
                        //sl = ((Frame)result).GetSlotByName(currentPart);
                        //ничего не надо обрабатывать, ссылка может быть только на сам фрейм-прототип. На его слоты ссылаться мы не можем.
                        break;
                    case SlotType.Integer:
                        tmpInt = Int32.Parse(currentPart);
                        result = ((int)result) + tmpInt;
                        break;
                    case SlotType.Boolean:
                    case SlotType.String:
                        //тоже ничего не будем делать.
                        break;
                }
                currentPart = "";
            }
            return result;
        }
Exemple #4
0
        private bool doesFrameFitCondition(Frame frameContext, FrameExample frameToTest, FrameExample frameMemory, Slot checkedSlot, string condition)
        {
            bool result = false;
            string leftPart=null;
            string rightPart=null;
            Operations operation;
            int operationIndexInText;
            string[] differentOperations = { "#isa", "==", "!=", "#morethan", "#lessthan", "#moreequal", "#lessequal" };
            int operationIndexInArray = -1;
            //foreach (string operationToLookFor in differentOperations)
            for (int i=0; i<differentOperations.Length; ++i)
            {
                string operationToLookFor = differentOperations[i];
                if (condition.Contains(operationToLookFor))
                {
                    operationIndexInText = condition.IndexOf(operationToLookFor);
                    operationIndexInArray = i;
                    leftPart = condition.Substring(0, operationIndexInText);
                    rightPart = condition.Substring(operationIndexInText + operationToLookFor.Length);
                    break;
                }
            }
            switch (operationIndexInArray)
            {
                case 0:
                    operation = Operations.isa;
                    break;
                case 1:
                    operation = Operations.equal;
                    break;
                case 2:
                    operation = Operations.notequal;
                    break;
                case 3:
                    operation = Operations.morethan;
                    break;
                case 4:
                    operation = Operations.lessthan;
                    break;
                case 5:
                    operation = Operations.moreequal;
                    break;
                case 6:
                    operation = Operations.lessequal;
                    break;
                default:
                    throw new IndexOutOfRangeException();
            }
            SlotType leftType, rightType;
            object leftPartResult = evaluateExpression(frameContext, checkedSlot, frameMemory, frameToTest, leftPart, out leftType);
            object rightPartResult = evaluateExpression(frameContext, checkedSlot, frameMemory, frameToTest, rightPart, out rightType);

            Exception typeComparisonException = new Exception("Can not compare different types!");

            switch (operation)
            {
                case Operations.isa:
                    if (leftType == SlotType.Frame && rightType == SlotType.FramePrototype)
                    {
                        result = ((FrameExample)leftPartResult).CheckIsA((Frame)rightPartResult);
                    }
                    else
                        throw new Exception("Incorrect frame types for 'is a' relation!");
                    break;
                case Operations.equal:
                    if (leftType == rightType)
                    {
                        switch (leftType)
                        {
                            //TODO: возможно, тут стоит добавить более изящное сравнение.
                            case SlotType.Boolean:
                                result = leftPartResult.ToString() == rightPartResult.ToString();
                                break;
                            case SlotType.Integer:
                                result = ((int)leftPartResult) == ((int)rightPartResult);
                                break;
                            case SlotType.String:
                                result = leftPartResult.ToString() == rightPartResult.ToString();
                                break;
                            default:
                                throw new Exception("Only boolean, integer and string slots can be compared.");
                        }
                    }
                    else
                        throw typeComparisonException;
                    break;
                case Operations.notequal:
                    if (leftType == rightType)
                    {
                        result = leftPartResult.ToString() == rightPartResult.ToString();
                    }
                    else
                        throw typeComparisonException;
                    break;
                case Operations.morethan:
                    if (leftType == rightType && rightType == SlotType.Integer)
                    {
                        result = ((int)leftPartResult) > ((int)rightPartResult);
                    }
                    else
                        throw typeComparisonException;
                    break;
                case Operations.lessthan:
                    if (leftType == rightType && rightType == SlotType.Integer)
                    {
                        result = ((int)leftPartResult) < ((int)rightPartResult);
                    }
                    else
                        throw typeComparisonException;
                    break;
                case Operations.moreequal:
                    if (leftType == rightType && rightType == SlotType.Integer)
                    {
                        result = ((int)leftPartResult) >= ((int)rightPartResult);
                    }
                    else
                        throw typeComparisonException;
                    break;
                case Operations.lessequal:
                    if (leftType == rightType && rightType == SlotType.Integer)
                    {
                        result = ((int)leftPartResult) <= ((int)rightPartResult);
                    }
                    else
                        throw typeComparisonException;
                    break;
            }
            return result;
        }
Exemple #5
0
        /// <summary>
        /// Попробовать привязаться к конкретной ситуации
        /// </summary>
        /// <param name="situationPrototype">Ситуация, к которой мы хотим привязаться (которую мы проверяем)</param>
        /// <param name="situationExample">Текущий фрейм-экземпляр</param>
        /// <returns>true - если привязалась. false - в противном случае.</returns>
        private bool checkSituation(Frame situationPrototype, FrameExample situationExample, int recursionLevel=0)
        {
            log("Проверяем ситуацию: " + situationPrototype.FrameName, recursionLevel);
            bool result = true;
            if (passedSituationsIds.Contains(situationPrototype.FrameId))
                result = true;
            else
            {
                if (failedSituationsIds.Contains(situationPrototype.FrameId))
                    result = false;
                else
                {

                    checkedFramesIDs.Add(situationPrototype.FrameId);
                    List<Slot> boundedSlots = new List<Slot>(); //привязавшиеся слоты
                    Dictionary<string, object> slotValues = new Dictionary<string, object>(); //значения привязавшихся слотов

                    foreach (Slot slot in situationPrototype.FrameSlots)
                    {
                        if (slot.SlotMarker != null && !situationExample.ContainsSlot(slot.SlotName))
                        {
                            object slotValue;

                            //                    if (situationExample.ContainsSlot(slot.SlotName))
                            //                        slotValue = situationExample.Value(slot.SlotName);
                            //                    else
                            if (!situationExample.ContainsSlot(slot.SlotName))
                            {
                                slotValue = boundSlot(slot, situationPrototype, situationExample);

                                if (slotValue == null)
                                {
                                    result = false;
                                    break;
                                }
                                else
                                {
                                    boundedSlots.Add(slot);
                                    slotValues.Add(slot.SlotNameTrimmed, slotValue);
                                }
                            }
                        }
                    }
                    if (result)
                    {
                        log("+ Ситуация " + situationPrototype.FrameName + " прошла проверку (исключая родителей).", recursionLevel);

                        passedSituationsIds.Add(situationPrototype.FrameId);
                        foreach (Slot boundedSlot in boundedSlots)
                        {
                            if (!situationExample.ContainsSlot(boundedSlot.SlotName))
                            {
                                situationExample.AddSlot(boundedSlot);
                                situationExample.SetValue(boundedSlot.SlotName, slotValues[boundedSlot.SlotNameTrimmed]);
                            }
                        }

                        if (situationPrototype.GetParentFrame() != null)
                        {
                            log("Привязка родителей ситуации " + situationPrototype.FrameName + "...", recursionLevel);
                            result = checkSituation(situationPrototype.GetParentFrame(), situationExample, recursionLevel+1);
                        }
                    }
                    else
                    {
                        failedSituationsIds.Add(situationPrototype.FrameId);
                        log("- Ситуация " + situationPrototype.FrameName + " не привязалась.", recursionLevel);
                    }

                    if (result)
                    {
                        log("Ситуация " + situationPrototype.FrameName + " Привязалась полностью.", recursionLevel);
                        //TODO:
                        //тут еще как-то, думаю, будут использоваться задания отсутствия
                        //плюс, возможно, нужно будет добпвлять не только привязавшиеся слоты, но и вообще все.

                    }
                }
            }
            return result;
        }
Exemple #6
0
        /// <summary>
        /// Привязываем субфрейм.
        /// </summary>
        /// <param name="frameTested"></param>
        /// <param name="marker"></param>
        /// <returns></returns>
        private FrameExample boundSubframe(Slot slot, FrameExample situationExample)
        {
            FrameExample result = null;
            Frame framePrototype = KnowLedgeBase.getFrameByID(slot.frameId);
            List<FrameExample> framesCandidates = new List<FrameExample>();
            foreach (FrameExample frame in KnowLedgeBase.FramesExamples)
                if (frame.CheckIsA(framePrototype))
                    framesCandidates.Add(frame);

            string[] separatorsOr = { "#or" };
            string[] separatorsAnd = { "#and" };
            foreach (FrameExample candidate in framesCandidates)
            {
                string[] conjuncts = slot.SlotMarker.Split(separatorsOr, StringSplitOptions.None);
                foreach (string conjunct in conjuncts)
                {
                    string[] conditions = conjunct.Split(separatorsAnd, StringSplitOptions.None);
                    bool fitsAllConditions = true;
                    foreach (string condition in conditions)
                    {
                        if (!doesFrameFitCondition(framePrototype, candidate, situationExample, slot, condition))
                        {
                            fitsAllConditions = false;
                            break;
                        }
                    }
                    if (fitsAllConditions)
                    {
                        result = candidate;
                        break;
                    }
                }
            }
            return result;
        }
Exemple #7
0
 /// <summary>
 /// Привязываем слот (проверяем на соответствие маркеру)
 /// </summary>
 /// <param name="slot">Сам слот</param>
 /// <param name="situationProrotype">Ситуация, которую мы сейчас привязываем (см. checkSituation).</param>
 /// <param name="situationExample">Текущий фрейм-экземпляр.</param>
 /// <returns>Если слот привязался, возвращаем его значение (в соответствие с типом слота). Иначе - null.</returns>
 private object boundSlot(Slot slot, Frame situationProrotype, FrameExample situationExample)
 {
     object result = null;
     switch (slot.SlotType)
     {
         case SlotType.Frame:
             FrameExample boundedSubframe = boundSubframe(slot, situationExample);
             result = boundedSubframe;
             break;
         //сколько тут получилось разнообразных вариантов вывода, аж обалдеть можно.
     }
     return result;
 }
Exemple #8
0
        /// <summary>
        /// Задает размер поля (в ячейках) по вертикали и горизонтали. !ВАЖНО: игровое поле создается заново! Все данные о ячейках стираются!
        /// </summary>
        /// <param name="rowCount">Число ячеек по вертикали (число рядов)</param>
        /// <param name="columnCount">Число ячеек по горизонтали (число столбцов)</param>
        public void setSize(int rowCount, int columnCount)
        {
            if (rowCount < 1 || columnCount < 1)
                throw new ArgumentException("Row and column counts have to be more than zero! size: (rc:"+rowCount.ToString()+" cc:"+columnCount.ToString()+")");
            cells = new List<List<GameCell>>(rowCount);
            //int xCoord = 0, yCoord = 0;
            this.SuspendLayout();
            this.Controls.Clear();
            for (int i = 0; i < RowCount; ++i)
            {
                //xCoord = 0;
                List<GameCell> row = new List<GameCell>(columnCount);
                for (int j = 0; j < ColumnCount; ++j)
                {
                    Frame framePrototype = new Frame();
                    framePrototype.FrameId = -1;
                    Slot slotImage = new Slot();
                    slotImage.SlotName = "image";
                    slotImage.SlotInheritance = SlotInherit.Override;
                    slotImage.SlotType = SlotType.String;
                    slotImage.SlotDefault = "Images\\unknown.png";
                    slotImage.SlotId = 0;
                    slotImage.ParentId = framePrototype.FrameId;

                    Slot slotRow = new Slot();
                    slotRow.SlotName = "Row";
                    slotRow.SlotType = SlotType.Integer;
                    slotRow.SlotInheritance = SlotInherit.Override;
                    slotRow.SlotId = 1;
                    slotRow.ParentId = framePrototype.FrameId;

                    Slot slotColumn = new Slot();
                    slotColumn.SlotName = "Column";
                    slotColumn.SlotType = SlotType.Integer;
                    slotColumn.SlotInheritance = SlotInherit.Override;
                    slotColumn.SlotId = 1;
                    slotColumn.ParentId = framePrototype.FrameId;

                    framePrototype.FrameSlots.Add(slotImage);
                    framePrototype.FrameSlots.Add(slotRow);
                    framePrototype.FrameSlots.Add(slotColumn);
                    FrameExample frameExample = new FrameExample(framePrototype);
                    frameExample.SetValue("image", "Images\\grass.jpg");

                    frameExample.SetValue("Row", i);
                    frameExample.SetValue("Column", j);

                    GameCell cell = new GameCell(this, i, j, cellSize, frameExample);
                    cell.GameCellClicked += new GameCellClickedEventHandler(cell_GameCellClicked);
                    row.Add(cell);
                    //cell.Location = new Point(xCoord, yCoord);
                    this.Controls.Add(cell);
                    //xCoord += cellSize+cellOffset;
                }
                cells.Add(row);
                //yCoord += cellSize+cellOffset;
            }

            this.rowCount = rowCount;
            this.columnCount = columnCount;
            this.ResumeLayout();
            this.layoutCells();
        }