示例#1
0
        public Dictionary <ElementPort, bool> GetElementOutput(LogicSchemeManagerContext context, Dictionary <ElementPort, bool> inputValues)
        {
            var outputValues = new Dictionary <ElementPort, bool>();

            //ToDo: check if ports exists

            var combination = context.Combinations
                              .Include(c => c.CombinationPorts)
                              .ThenInclude(cp => cp.Port)
                              .Where(c => c.ElementTypeId == ElementTypeId &&
                                     !c.CombinationPorts
                                     .Where(cp => !cp.Port.IsOutput)
                                     .Any(cp => ElementPorts
                                          .Where(ep => !ep.Port.IsOutput)
                                          .Any(input => input.PortId == cp.PortId && inputValues[input] != cp.Value))
                                     ).FirstOrDefault();

            if (combination == default(Combination))
            {
                return(null);
            }

            ElementPorts.Where(ep => ep.ElementId == ElementId && ep.Port.IsOutput).ToList().ForEach(ep => {
                outputValues[ep] = combination.CombinationPorts.FirstOrDefault(cp => cp.Port.IsOutput && cp.PortId == ep.PortId).Value;
            });

            return(outputValues);
        }
示例#2
0
        public Dictionary <ElementPort, bool> GetSchemaOutput(LogicSchemeManagerContext context, Dictionary <ElementPort, bool> inputValues)
        {
            var outputValues      = new Dictionary <ElementPort, bool>();
            var processedElements = new HashSet <int>();
            var processingNeeded  = true;

            while (processingNeeded)
            {
                processingNeeded = false;

                Elements.Where(element => !element.ElementPorts.Where(ep => !ep.Port.IsOutput).Any(ep => !inputValues.ContainsKey(ep)) && !processedElements.Contains(element.ElementId))
                .ToList()
                .ForEach(element => {
                    var elementOutputValues = element.GetElementOutput(context, inputValues);
                    foreach (var elementOutputValue in elementOutputValues)
                    {
                        //ToDo: create logic for output that connected to another element and also are outputs of scheme
                        outputValues[elementOutputValue.Key] = elementOutputValue.Value;

                        Elements.SelectMany(e => e.ElementPorts)
                        .Where(ep => ep.ParentId == elementOutputValue.Key.ElementPortId).ToList().ForEach(ep => {
                            inputValues[ep] = elementOutputValue.Value;
                        });
                    }
                    processedElements.Add(element.ElementId);
                    processingNeeded = true;
                });
            }


            return(outputValues);
        }
示例#3
0
        public TruthTable GetErrorTruthTable(LogicSchemeManagerContext context, KeyValuePair <int, bool> error)
        {
            var truthTable = new TruthTable();            //new Dictionary<Dictionary<ElementPort, bool>, Dictionary<ElementPort, bool>>();

            GetAvailableInputCombinations().ForEach(availableInputCombination => {
                truthTable.Rows.Add(new TruthTableRow(availableInputCombination, GetSchemaOutputWithError(context, availableInputCombination, error)));
            });

            return(truthTable);
        }
示例#4
0
        public TruthTable GetTruthTable(LogicSchemeManagerContext context)
        {
            var truthTable = new TruthTable();            //new Dictionary<Dictionary<ElementPort, bool>, Dictionary<ElementPort, bool>>();

            GetAvailableInputCombinations().ForEach(availableInputCombination => {
                truthTable.Rows.Add(new TruthTableRow(availableInputCombination, GetSchemaOutput(context, availableInputCombination)));
            });

            return(truthTable);
        }
示例#5
0
        public Dictionary <ElementPort, bool> GetSchemaOutputWithError(LogicSchemeManagerContext context, Dictionary <ElementPort, bool> inputValues, KeyValuePair <int, bool> error)
        {
            var outputValues      = new Dictionary <ElementPort, bool>();
            var processedElements = new HashSet <int>();
            var processingNeeded  = true;

            var errorValues = new Dictionary <ElementPort, bool>();

            foreach (var pair in inputValues)
            {
                errorValues.Add(pair.Key, pair.Value);
            }
            var errorInput = errorValues.FirstOrDefault(kvp => kvp.Key.ElementPortId == error.Key);

            if (!errorInput.Equals(default(KeyValuePair <ElementPort, bool>)))
            {
                errorValues[errorInput.Key] = error.Value;
            }

            while (processingNeeded)
            {
                processingNeeded = false;

                Elements.Where(element => !element.ElementPorts.Where(ep => !ep.Port.IsOutput).Any(ep => !errorValues.ContainsKey(ep)) && !processedElements.Contains(element.ElementId))
                .ToList()
                .ForEach(element => {
                    var elementOutputValues = element.GetElementOutput(context, errorValues);
                    foreach (var elementOutputValue in elementOutputValues)
                    {
                        //ToDo: create logic for output that connected to another element and also are outputs of scheme
                        outputValues[elementOutputValue.Key] = elementOutputValue.Key.ElementPortId == error.Key ? error.Value : elementOutputValue.Value;

                        Elements.SelectMany(e => e.ElementPorts)
                        .Where(ep => ep.ParentId == elementOutputValue.Key.ElementPortId).ToList().ForEach(ep => {
                            errorValues[ep] = outputValues[elementOutputValue.Key];
                        });
                    }
                    processedElements.Add(element.ElementId);
                    processingNeeded = true;
                });
            }


            return(outputValues);
        }