public bool AddFrame(Frame frame) { if (frame != null) { Frames.Add(frame); return true; } return false; }
// закидываем создаем новый фрейм и отображаем private void добавитьФреймшаблонToolStripMenuItem_Click(object sender, EventArgs e) { AddFrameFrm frm = new AddFrameFrm(); if (frm.ShowDialog()== DialogResult.OK) { Random rand = new Random(); Frame frame = new Frame { FrameId = ClassFactory.kBase.GetMaxNodeId(1), FrameName = frm.TextBox }; //frame.FrameSlots.Add(new Slot { SlotName = "slot" + ClassFactory.kBase.GetMaxSlotId(1), SlotId = 0, SlotInheritance = SlotInherit.Override, SlotType = Classes.SlotType.Integer }); //frame.FrameSlots.Add(new Slot { SlotName = "slo1t" + ClassFactory.kBase.GetMaxSlotId(1), SlotId = 1, SlotInheritance = SlotInherit.Same, SlotType = Classes.SlotType.Frame }); ShapeNode node = new ShapeNode { Text = frame.FrameName, Id = frame.FrameId, Brush = new SolidBrush(Color.PowderBlue), Shape = Shapes.Ellipse }; node.SetBounds(new RectangleF(new PointF(rand.Next(100, 400), rand.Next(100, 400)), new SizeF(100, 30)), true, true); node.Font = new System.Drawing.Font("Microsoft Sans Serif", 9, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); node.TextFormat.Alignment = StringAlignment.Center; node.TextFormat.LineAlignment = StringAlignment.Center; ClassFactory.isSaved = false; frame.X = node.GetBounds().X; frame.Y = node.GetBounds().Y; FrameDiagram.Nodes.Add(node); ClassFactory.kBase.AddFrame(frame); TreeNode frameitem = new TreeNode { Text = frame.FrameName }; FrameTreeView.Nodes.Add(frameitem); } }
private void FrameDiagram_LinkCreated(object sender, LinkEventArgs e) { Frame frame = new Frame(); Frame frame1 = new Frame(); Frame frameUpper;// = ClassFactory.kBase.FrameList().Find(f => f.FrameId == (int)e.Link.Destination.Id); Frame frameLower; string str = ""; switch (cmbTypeLink.SelectedIndex) { case 0: e.Link.Text = "Is_a"; e.Link.TextColor = Color.DarkRed; e.Link.Pen.Color = Color.Red; frameUpper = ClassFactory.kBase.FrameList().Find(f => f.FrameId == (int)e.Link.Destination.Id); frameLower = ClassFactory.kBase.FrameList().Find(f => f.FrameId == (int)e.Link.Origin.Id); //foreach (DiagramNode node in FrameDiagram.Nodes) //{ // if (node.GetBounds().Contains(StartLinkPoint.X, StartLinkPoint.Y)) // { // if (cmbTypeLink.SelectedIndex == 0) // { // frame = ClassFactory.kBase.FrameList().Find(f => f.FrameId == (int)node.Id); // } // str = node.Id.ToString() + cmbTypeLink.SelectedItem.ToString(); // } // if (node.GetBounds().Contains(EndLinkPoint.X, EndLinkPoint.Y)) // { // frame1 = ClassFactory.kBase.FrameList().Find(f => f.FrameId == (int)node.Id); // } //} //frame.IsA.ParentId = frame1.FrameId; //frame.IsA.SlotDefault = frame1.FrameName; //frame.IsA.frameId = frame1.FrameId; frameLower.IsA.ParentId = frameUpper.FrameId; frameLower.IsA.SlotDefault = frameUpper.FrameName; frameLower.IsA.frameId = frameUpper.FrameId; ClassFactory.kBase.AddIsA(frame); //простор для оптимизации break; case 1: e.Link.Text = "Sub"; e.Link.TextColor = Color.DarkBlue; e.Link.Pen.Color = Color.Blue; frameUpper = ClassFactory.kBase.FrameList().Find(f => f.FrameId == (int)e.Link.Destination.Id); frameLower = ClassFactory.kBase.FrameList().Find(f => f.FrameId == (int)e.Link.Origin.Id); //foreach (DiagramNode node in FrameDiagram.Nodes) //{ // if (node.GetBounds().Contains(StartLinkPoint.X, StartLinkPoint.Y)) // { // if (cmbTypeLink.SelectedIndex == 0) // { // frame = ClassFactory.kBase.FrameList().Find(f => f.FrameId == (int)node.Id); // } // str = node.Id.ToString() + cmbTypeLink.SelectedItem.ToString(); // } // if (node.GetBounds().Contains(EndLinkPoint.X, EndLinkPoint.Y)) // { // frame1 = ClassFactory.kBase.FrameList().Find(f => f.FrameId == (int)node.Id); // } //} Slot slotSubframe = new Slot(){ IsSystem = false, frameId = frameLower.FrameId, ParentId = frameUpper.FrameId, SlotName="sub", SlotType = Classes.SlotType.Frame}; frameUpper.FrameSlots.Add(slotSubframe); break; } //MessageBox.Show(str + str1); }
private void FraimDiagram_NodeCreated(object sender, MindFusion.Diagramming.NodeEventArgs e) { ShapeNode node = e.Node as ShapeNode; if (node != null) { AddFrameFrm frm = new AddFrameFrm(); if (frm.ShowDialog()== DialogResult.OK) { Frame frame = new Frame { FrameId = ClassFactory.kBase.GetMaxNodeId(), FrameName = frm.TextBox }; node.Id = frame.FrameId; node.Text = frame.FrameName; ClassFactory.kBase.AddFrame(frame); TreeNode frameitem = new TreeNode { Text = frame.FrameName }; FrameTreeView.Nodes.Add(frameitem); } } }
/// <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; }
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; }
/// <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; }
/// <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; }
/// <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(); }
public bool AddIsA(Frame frame) { // выбираем фрейм, у слота IsA присваиваем Frames.Find(f => f.FrameId == frame.FrameId).IsA = frame.IsA; return true; }
public bool CheckIsA(Frame frameParentToCheck) { bool result = false; if (this.BaseFrame != null) result = BaseFrame.CheckIsA(frameParentToCheck); return result; }
/// <summary> /// /// </summary> /// <param name="BaseFrame">ссылка на фрейм-прототип. НЕ может быть null</param> public FrameExample(Frame inputBaseFrame=null) { this.BaseFrame = inputBaseFrame; }
public bool CheckIsA(Frame frameParentToCheck) { bool result = false; if (frameParentToCheck != null) { if (frameParentToCheck.FrameId == this.FrameId) result = true; else { Frame frameParent = this.GetParentFrame(); if (frameParent != null) result = frameParent.CheckIsA(frameParentToCheck); } } return result; }
public bool AddFrame(Frame frame) { if (frame != null) { Frames.Add(frame); if (this.FramesChangedEvent != null) this.FramesChangedEvent(this, new FramesChangedEventArgs(this.FrameList())); return true; } return false; }
public bool AddIsA(Frame frame) { // выбираем фрейм, у слота IsA присваиваем Frames.Find(f => f.FrameId == frame.FrameId).IsA = frame.IsA; if (this.FramesChangedEvent != null) this.FramesChangedEvent(this, new FramesChangedEventArgs(this.FrameList())); return true; }