/// <summary>
        /// Creates a node of the specified type.
        /// </summary>
        /// <remarks>
        /// The method will specify the ports that the node should have based on its type.
        /// </remarks>
        private void CreateNode(IGraph graph, PointD location, LogicGateType type, string label, SizeD?size = null)
        {
            RectD newBounds = RectD.FromCenter(location, graph.NodeDefaults.Size);
            INode node;

            if (type >= LogicGateType.Timer)
            {
                node = graph.CreateNode(RectD.FromCenter(location, (SizeD)size), new ShapeNodeStyle {
                    Pen = new Pen(Brushes.Black, 2)
                });
            }
            else
            {
                node = graph.CreateNode(newBounds, new LogicGateNodeStyle {
                    GateType = type
                });
            }

            graph.AddLabel(node, label, InteriorLabelModel.Center);

            var portDescriptors = PortDescriptor.CreatePortDescriptors(type);

            // use relative port locations
            var model = new FreeNodePortLocationModel();

            // add ports for all descriptors using the descriptor as the tag of the port
            foreach (var descriptor in portDescriptors)
            {
                // use the descriptor's location as offset
                var portLocationModelParameter = model.CreateParameter(PointD.Origin, new PointD(descriptor.X, descriptor.Y));
                graph.AddPort(node, portLocationModelParameter, tag: descriptor);
            }
        }
 public LogicGate()
 {
     this.label                = "Logic gate:";
     this.innerFilter          = new PawnFilter();
     this.logicGateType        = LogicGateType.AND;
     this.filterPartListHeight = 0;
 }
        private string GateTypeToString(LogicGateType type)
        {
            if (type == LogicGateType.And)
            {
                return("&&");
            }

            if (type == LogicGateType.Or)
            {
                return("||");
            }

            throw new ArgumentException();
        }
        internal static LogicalNetworEntity Create(
            DNA<LogicalNetworkGene> dna,
            IEnumerable<LogicalNetworkGene> dominantGeneSequence,
            GateTypeEvolutionMethod evolutionMethod,
            TruthTable truthTableToSolve,
            LogicGateTypes gateTypeRestrictions)
        {
            Contract.Requires(dna != null);
            Contract.Requires(dominantGeneSequence != null);
            Contract.Requires(truthTableToSolve != null);
            Contract.Requires(gateTypeRestrictions != null);
            
            LogicalNetworkFactory factory;
            if (evolutionMethod == GateTypeEvolutionMethod.Restrict)
            {
                factory = new LogicalNetworkFactory(truthTableToSolve.InputInterfaceLength, truthTableToSolve.OutputInterfaceLength, gateTypeRestrictions);
            }
            else // Evolve
            {
                factory = new LogicalNetworkFactory(truthTableToSolve.InputInterfaceLength, truthTableToSolve.OutputInterfaceLength);
            }

            var network = LogicalNetworkDNADecoder.CreateNetwork(factory, dominantGeneSequence);

            int numOfNAGates = 0;
            if (evolutionMethod == GateTypeEvolutionMethod.Evolve && gateTypeRestrictions != null)
            {
                foreach (var entry in network.EntryArray)
                {
                    var gate = entry.NodeEntry.Node as LogicGate;
                    if (gate != null)
                    {
                        var type = new LogicGateType(gate.Operation, entry.UpperConnectionEntryArray.Length);
                        if (!gateTypeRestrictions.Contains(type)) numOfNAGates++;
                    }
                }
            }

            int errors = new TruthTableComputation(network).ComputeError(truthTableToSolve);

            return new LogicalNetworEntity(dna, network, errors, numOfNAGates);
        }
 public IntRange? this[LogicGateType gateType]
 {
     get
     {
         IntRange r;
         if (restrictions.TryGetValue(gateType, out r)) return r;
         return null;
     }
     set
     {
         if (isSealed) throw new InvalidOperationException("Cannot modify sealed restrictions.");
         if (value.HasValue)
         {
             restrictions[gateType] = value.Value;
         }
         else
         {
             restrictions.Remove(gateType);
         }
     }
 }
Beispiel #6
0
            public void Paint(IRenderContext context, Graphics graphics)
            {
                // paint path
                float         x    = (float)node.Layout.X;
                float         y    = (float)node.Layout.Y;
                float         w    = (float)node.Layout.Width;
                float         h    = (float)node.Layout.Height;
                LogicGateType type = gateType;

                var outlinePen = new Pen(OutlineColor, 3f);

                if (type == LogicGateType.Not)
                {
                    graphics.DrawLine(outlinePen, x, y + 0.5f * h, x + 0.1f * w, y + 0.5f * h);
                }
                else
                {
                    // in port lines
                    graphics.DrawLine(outlinePen, x, y + 5, x + 0.3f * w, y + 5);
                    graphics.DrawLine(outlinePen, x, y + 25, x + 0.3f * w, y + 25);
                }

                var outline = outlinePath;

                outline.Fill(graphics, new Matrix2D(w, 0, 0, h, x, y), new SolidBrush(FillColor));
                outline.Draw(graphics, new Matrix2D(w, 0, 0, h, x, y), new Pen(new SolidBrush(OutlineColor), 2));

                if (type == LogicGateType.And || type == LogicGateType.Or)
                {
                    graphics.DrawLine(outlinePen, x + 0.8f * w, y + 0.5f * h, x + w, y + 0.5f * h);
                }
                else
                {
                    graphics.DrawLine(outlinePen, x + 0.9f * w, y + 0.5f * h, x + w, y + 0.5f * h);
                }
            }
Beispiel #7
0
 public override string ToString()
 {
     return($"Logic Gate:{LogicGateType.ToString()}, InputWire1:{InputWire1.ToString()}, InputWire2:{InputWire2.ToString()}, OutputWire:{OutputWire.ToString()}");
 }
        public override float DoEditInterface(PawnFilterListing list)
        {
            float rectHeight = RowHeight * 2 + this.filterPartListHeight;
            Rect  rect       = list.GetPawnFilterPartRect(this, rectHeight);

            // Logic gate type list.
            Rect gateTypeRect = new Rect(rect.x, rect.y, rect.width, RowHeight);

            if (Widgets.ButtonText(gateTypeRect, this.logicGateType.ToString()))
            {
                FloatMenuUtility.MakeMenu((LogicGateType[])Enum.GetValues(typeof(LogicGateType)), type => type.ToString(), type => () => this.logicGateType = type);
            }

            // Add part button.
            Rect addPartButtonRect = new Rect(rect.x, rect.y + gateTypeRect.height, rect.width, RowHeight);

            if (Widgets.ButtonText(addPartButtonRect, "Add part"))
            {
                FloatMenuUtility.MakeMenu(PawnFilter.allFilterParts, def => def.label, def => () => {
                    PawnFilterPart part = (PawnFilterPart)Activator.CreateInstance(def.partClass);
                    part.def            = def;
                    this.innerFilter.parts.Add(part);
                });
            }

            // Build filter field.
            Rect filterFieldRect = new Rect(rect.x, rect.y + gateTypeRect.height + addPartButtonRect.height, rect.width, this.filterPartListHeight).Rounded();

            Widgets.DrawMenuSection(filterFieldRect);
            filterFieldRect = filterFieldRect.GetInnerRect();

            // Draw filter parts.
            this.filterPartListHeight = filterPartListHeightBuffer;
            PawnFilterListing filterPartList = new PawnFilterListing()
            {
                ColumnWidth = filterFieldRect.width
            };

            filterPartList.Begin(filterFieldRect);
            List <PawnFilterPart> partsToRemove = new List <PawnFilterPart>();           // Remove parts that should be removed here in order to avoid modifying enumerable during foreach.

            foreach (PawnFilterPart part in this.innerFilter.parts)
            {
                if (part.toRemove)
                {
                    partsToRemove.Add(part);
                }
                else
                {
                    this.filterPartListHeight += part.DoEditInterface(filterPartList) + RowHeight + PawnFilterListing.gapSize;
                }
            }
            foreach (PawnFilterPart part in partsToRemove)
            {
                _ = this.innerFilter.parts.Remove(part);
            }
            filterPartList.End();

            return(rectHeight);
        }
Beispiel #9
0
 public LogicGateVisual(INode node, LogicGateType gateType, GeneralPath outlinePath)
 {
     this.node        = node;
     this.gateType    = gateType;
     this.outlinePath = outlinePath;
 }
Beispiel #10
0
        /// <summary>
        /// Creates an enumerable of <see cref="PortDescriptor"/>s belonging to the type of the node as specified by its
        /// <see cref="LogicGateType"/> set as its tag or an empty list if there is no
        /// <see cref="LogicGateType"/>.
        /// </summary>
        /// <param name="logicGateType"></param>
        public static IEnumerable <PortDescriptor> CreatePortDescriptors(LogicGateType logicGateType)
        {
            var inside         = new InsideOutsidePortLabelModel().CreateInsideParameter();
            var aboveEdgeLeft  = FreePortLabelModel.Instance.CreateParameter(new PointD(-5, -3), new PointD(1, 1), PointD.Origin, 0);
            var aboveEdgeRight = FreePortLabelModel.Instance.CreateParameter(new PointD(5, -3), new PointD(0, 1), PointD.Origin, 0);
            var belowEdgeLeft  = FreePortLabelModel.Instance.CreateParameter(new PointD(-5, 3), new PointD(1, 0), PointD.Origin, 0);

            switch (logicGateType)
            {
            default:
            case LogicGateType.And:
                return(new List <PortDescriptor> {
                    new PortDescriptor {
                        X = 0, Y = 5, EdgeDirection = EdgeDirection.In, LabelText = "in1",
                        LabelPlacementWithEdge = aboveEdgeLeft
                    },
                    new PortDescriptor {
                        X = 0, Y = 25, EdgeDirection = EdgeDirection.In, LabelText = "in2",
                        LabelPlacementWithEdge = belowEdgeLeft
                    },
                    new PortDescriptor {
                        X = 50, Y = 15, EdgeDirection = EdgeDirection.Out, LabelText = "out",
                        LabelPlacementWithEdge = aboveEdgeRight
                    }
                });

            case LogicGateType.Nand:
                return(new List <PortDescriptor> {
                    new PortDescriptor {
                        X = 0, Y = 5, EdgeDirection = EdgeDirection.In, LabelText = "in1",
                        LabelPlacementWithEdge = aboveEdgeLeft
                    },
                    new PortDescriptor {
                        X = 0, Y = 25, EdgeDirection = EdgeDirection.In, LabelText = "in2",
                        LabelPlacementWithEdge = belowEdgeLeft
                    },
                    new PortDescriptor {
                        X = 50, Y = 15, EdgeDirection = EdgeDirection.Out, LabelText = "out",
                        LabelPlacementWithEdge = aboveEdgeRight
                    }
                });

            case LogicGateType.Or:
                return(new List <PortDescriptor> {
                    new PortDescriptor {
                        X = 0, Y = 5, EdgeDirection = EdgeDirection.In, LabelText = "in1",
                        LabelPlacementWithEdge = aboveEdgeLeft
                    },
                    new PortDescriptor {
                        X = 0, Y = 25, EdgeDirection = EdgeDirection.In, LabelText = "in2",
                        LabelPlacementWithEdge = belowEdgeLeft
                    },
                    new PortDescriptor {
                        X = 50, Y = 15, EdgeDirection = EdgeDirection.Out, LabelText = "out",
                        LabelPlacementWithEdge = aboveEdgeRight
                    }
                });

            case LogicGateType.Nor:
                return(new List <PortDescriptor> {
                    new PortDescriptor {
                        X = 0, Y = 5, EdgeDirection = EdgeDirection.In, LabelText = "in1",
                        LabelPlacementWithEdge = aboveEdgeLeft
                    },
                    new PortDescriptor {
                        X = 0, Y = 25, EdgeDirection = EdgeDirection.In, LabelText = "in2",
                        LabelPlacementWithEdge = belowEdgeLeft
                    },
                    new PortDescriptor {
                        X = 50, Y = 15, EdgeDirection = EdgeDirection.Out, LabelText = "out",
                        LabelPlacementWithEdge = aboveEdgeRight
                    }
                });

            case LogicGateType.Not:
                return(new List <PortDescriptor> {
                    new PortDescriptor {
                        X = 0, Y = 15, EdgeDirection = EdgeDirection.In, LabelText = "in",
                        LabelPlacementWithEdge = aboveEdgeLeft
                    },
                    new PortDescriptor {
                        X = 50, Y = 15, EdgeDirection = EdgeDirection.Out, LabelText = "out",
                        LabelPlacementWithEdge = aboveEdgeRight
                    }
                });

            case LogicGateType.Timer:
                return(new[] {
                    new PortDescriptor {
                        X = 0, Y = 20, EdgeDirection = EdgeDirection.In, LabelText = "gnd",
                        LabelPlacementWithEdge = inside
                    },
                    new PortDescriptor {
                        X = 0, Y = 40, EdgeDirection = EdgeDirection.In, LabelText = "trig",
                        LabelPlacementWithEdge = inside
                    },
                    new PortDescriptor {
                        X = 0, Y = 80, EdgeDirection = EdgeDirection.Out, LabelText = "out",
                        LabelPlacementWithEdge = inside
                    },
                    new PortDescriptor {
                        X = 0, Y = 100, EdgeDirection = EdgeDirection.In, LabelText = "rst",
                        LabelPlacementWithEdge = inside
                    },
                    new PortDescriptor {
                        X = 70, Y = 20, EdgeDirection = EdgeDirection.In, LabelText = "Vcc",
                        LabelPlacementWithEdge = inside
                    },
                    new PortDescriptor {
                        X = 70, Y = 40, EdgeDirection = EdgeDirection.Out, LabelText = "dis",
                        LabelPlacementWithEdge = inside
                    },
                    new PortDescriptor {
                        X = 70, Y = 80, EdgeDirection = EdgeDirection.In, LabelText = "thr",
                        LabelPlacementWithEdge = inside
                    },
                    new PortDescriptor {
                        X = 70, Y = 100, EdgeDirection = EdgeDirection.In, LabelText = "ctrl",
                        LabelPlacementWithEdge = inside
                    },
                });

            case LogicGateType.ADConverter:
                return(new[] {
                    new PortDescriptor {
                        X = 0, Y = 20, EdgeDirection = EdgeDirection.In, LabelText = "Vin",
                        LabelPlacementWithEdge = inside
                    },
                    new PortDescriptor {
                        X = 0, Y = 40, EdgeDirection = EdgeDirection.In, LabelText = "gnd",
                        LabelPlacementWithEdge = inside
                    },
                    new PortDescriptor {
                        X = 0, Y = 80, EdgeDirection = EdgeDirection.In, LabelText = "Vref",
                        LabelPlacementWithEdge = inside
                    },
                    new PortDescriptor {
                        X = 0, Y = 100, EdgeDirection = EdgeDirection.In, LabelText = "clk",
                        LabelPlacementWithEdge = inside
                    },
                    new PortDescriptor {
                        X = 70, Y = 20, EdgeDirection = EdgeDirection.Out, LabelText = "d1",
                        LabelPlacementWithEdge = inside
                    },
                    new PortDescriptor {
                        X = 70, Y = 40, EdgeDirection = EdgeDirection.Out, LabelText = "d2",
                        LabelPlacementWithEdge = inside
                    },
                    new PortDescriptor {
                        X = 70, Y = 100, EdgeDirection = EdgeDirection.Out, LabelText = "sign",
                        LabelPlacementWithEdge = inside
                    },
                });
            }
        }
Beispiel #11
0
        /// <summary>
        /// Actually creates the visual appearance of a node.
        /// </summary>
        /// <remarks>
        /// This renders the node and the edges to the labels and adds the visuals to the <paramref name="container"/>.
        /// All items are arranged as if the node was located at (0,0). <see cref="CreateVisual"/> and <see cref="UpdateVisual"/>
        /// finally arrange the container so that the drawing is translated into the final position.
        /// </remarks>
        private void Render(IRenderContext context, INode node, VisualGroup container)
        {
            // paint path
            double        w    = node.Layout.Width;
            double        h    = node.Layout.Height;
            LogicGateType type = GateType;

            if (type == LogicGateType.Not)
            {
                var inPortLine = new Line
                {
                    X1              = 0,
                    X2              = 0.1 * w,
                    Y1              = 0.5 * h,
                    Y2              = 0.5 * h,
                    Stroke          = Brushes.Black,
                    StrokeThickness = 3
                };
                container.Children.Add(inPortLine);
            }
            else
            {
                // in port lines
                container.Children.Add(new Line
                {
                    X1              = 0,
                    X2              = 0.3 * w,
                    Y1              = 5,
                    Y2              = 5,
                    Stroke          = Brushes.Black,
                    StrokeThickness = 3
                });
                container.Children.Add(new Line
                {
                    X1              = 0,
                    X2              = 0.3 * w,
                    Y1              = 25,
                    Y2              = 25,
                    Stroke          = Brushes.Black,
                    StrokeThickness = 3
                });
            }

            var outline = GetNodeOutlinePath();
            var path    = outline.CreatePath(
                new SolidColorBrush(FillColor),
                new Pen(new SolidColorBrush(OutlineColor), 2),
                new Matrix2D(w, 0, 0, h, 0, 0), // resize the path to fit our box
                FillMode.FillClosedFigures);

            container.Children.Add(path);


            if (type == LogicGateType.And || type == LogicGateType.Or)
            {
                var outPortLine = new Line
                {
                    X1              = 0.8 * w,
                    X2              = w,
                    Y1              = 0.5 * h,
                    Y2              = 0.5 * h,
                    Stroke          = Brushes.Black,
                    StrokeThickness = 3
                };
                container.Children.Add(outPortLine);
            }
            else
            {
                var outPortLine = new Line
                {
                    X1              = 0.9 * w,
                    X2              = w,
                    Y1              = 0.5 * h,
                    Y2              = 0.5 * h,
                    Stroke          = Brushes.Black,
                    StrokeThickness = 3
                };
                container.Children.Add(outPortLine);
            }
        }