mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-03-20 00:16:36 +08:00
使用设计模式优化一下逻辑图
This commit is contained in:
@@ -169,277 +169,12 @@ namespace AIStudio.Wpf.Logical
|
||||
{
|
||||
link.Value = rd.NextDouble() * 10;
|
||||
}
|
||||
foreach (var item in DiagramViewModel.Items.OfType<ConstantDesignerItemViewModel>())
|
||||
{
|
||||
foreach (var output in item.Output)
|
||||
{
|
||||
output.Value.ConnectorValue = item.Value;
|
||||
output.Value.ColorViewModel.FillColor.Color = Colors.Green;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var item in DiagramViewModel.Items.OfType<InputItemViewModel>())
|
||||
{
|
||||
if (item.LinkPoint != null)
|
||||
{
|
||||
foreach (var output in item.Output)
|
||||
{
|
||||
output.Value.ConnectorValue = item.LinkPoint.Value;
|
||||
output.Value.ColorViewModel.FillColor.Color = Colors.Green;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var item in DiagramViewModel.Items.OfType<LogicalGateItemViewModelBase>().OrderBy(p => p.OrderNumber))
|
||||
{
|
||||
if (item.LogicalType != LogicalType.None)
|
||||
{
|
||||
foreach (var input in item.Input)
|
||||
{
|
||||
var connector = GetSourceItem(input.Value);
|
||||
if (connector == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (connector.SourceConnectorInfo.DataItem is LogicalGateItemViewModelBase)
|
||||
{
|
||||
input.Value.ConnectorValue = (connector.SourceConnectorInfo as LogicalConnectorInfo).ConnectorValue;
|
||||
|
||||
input.Value.ColorViewModel.FillColor.Color = connector.SourceConnectorInfo.ColorViewModel.FillColor.Color;
|
||||
connector.ColorViewModel.LineColor.Color = connector.SourceConnectorInfo.ColorViewModel.FillColor.Color;
|
||||
connector.ColorViewModel.FillColor.Color = connector.SourceConnectorInfo.ColorViewModel.FillColor.Color;
|
||||
|
||||
if (item.LogicalType == LogicalType.Output)
|
||||
{
|
||||
input.Value.ValueTypePoint = (connector.SourceConnectorInfo as LogicalConnectorInfo).ValueTypePoint;
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.NOT)
|
||||
{
|
||||
input.Value.ValueTypePoint = ((connector.SourceConnectorInfo as LogicalConnectorInfo).ValueTypePoint == ValueTypePoint.Bool) ? ValueTypePoint.Bool : ValueTypePoint.Int;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var output in item.Output)
|
||||
{
|
||||
if (item.LogicalType == LogicalType.Output)
|
||||
{
|
||||
var first = item.Input.Values.FirstOrDefault();
|
||||
output.Value.ConnectorValue = first.ConnectorValue;
|
||||
output.Value.ValueTypePoint = first.ValueTypePoint;
|
||||
(item as OutputItemViewModel).Value = first.ConnectorValue;
|
||||
(item as OutputItemViewModel).LinkPoint.Value = first.ConnectorValue;
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.ADD)
|
||||
{
|
||||
output.Value.ConnectorValue = item.Input.Values.Select(p => p.ConnectorValue).Sum();
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.SUB)
|
||||
{
|
||||
var first = item.Input.Values.Select(p => p.ConnectorValue).FirstOrDefault();
|
||||
var second = item.Input.Values.Where((value, index) => index != 0).Select(p => p.ConnectorValue).Sum();
|
||||
output.Value.ConnectorValue = first - second;
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.MUL)
|
||||
{
|
||||
double result = 0;
|
||||
foreach (var input in item.Input.Values)
|
||||
{
|
||||
if (result == 0)
|
||||
{
|
||||
result = 1;
|
||||
}
|
||||
result *= input.ConnectorValue;
|
||||
}
|
||||
output.Value.ConnectorValue = result;
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.DIV)
|
||||
{
|
||||
double result = item.Input.Values.Select(p => p.ConnectorValue).FirstOrDefault();
|
||||
foreach (var input in item.Input.Values.Where((value, index) => index != 0))
|
||||
{
|
||||
result /= input.ConnectorValue;
|
||||
}
|
||||
output.Value.ConnectorValue = result;
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.AVE)
|
||||
{
|
||||
output.Value.ConnectorValue = item.Input.Values.Select(p => p.ConnectorValue).Average();
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.MOD)
|
||||
{
|
||||
output.Value.ConnectorValue = item.Input[0].ConnectorValue % item.Input[1].ConnectorValue;
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.AND)
|
||||
{
|
||||
output.Value.ConnectorValue = Convert.ToInt32(item.Input[0].ConnectorValue) & Convert.ToInt32(item.Input[1].ConnectorValue);
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.OR)
|
||||
{
|
||||
output.Value.ConnectorValue = Convert.ToInt32(item.Input[0].ConnectorValue) | Convert.ToInt32(item.Input[1].ConnectorValue);
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.XOR)
|
||||
{
|
||||
output.Value.ConnectorValue = Convert.ToInt32(Convert.ToInt32(item.Input[0].ConnectorValue) ^ Convert.ToInt32(item.Input[1].ConnectorValue));
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.NOT)
|
||||
{
|
||||
if (item.Input[0].ValueTypePoint == ValueTypePoint.Bool)
|
||||
{
|
||||
output.Value.ConnectorValue = Convert.ToInt32(!Convert.ToBoolean(item.Input[0].ConnectorValue));
|
||||
output.Value.ValueTypePoint = ValueTypePoint.Bool;
|
||||
}
|
||||
else
|
||||
{
|
||||
output.Value.ConnectorValue = ~Convert.ToInt32(item.Input[0].ConnectorValue);
|
||||
output.Value.ValueTypePoint = ValueTypePoint.Int;
|
||||
}
|
||||
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.SHL)
|
||||
{
|
||||
output.Value.ConnectorValue = Convert.ToInt32(item.Input[0].ConnectorValue) << Convert.ToInt32(item.Input[1].ConnectorValue);
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.SHR)
|
||||
{
|
||||
output.Value.ConnectorValue = Convert.ToInt32(item.Input[0].ConnectorValue) >> Convert.ToInt32(item.Input[1].ConnectorValue);
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.ROL)
|
||||
{
|
||||
output.Value.ConnectorValue = (Convert.ToInt32(item.Input[0].ConnectorValue) << Convert.ToInt32(item.Input[1].ConnectorValue)) | (Convert.ToInt32(item.Input[0].ConnectorValue) >> 32 - Convert.ToInt32(item.Input[1].ConnectorValue));
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.ROR)
|
||||
{
|
||||
output.Value.ConnectorValue = (Convert.ToInt32(item.Input[0].ConnectorValue) >> Convert.ToInt32(item.Input[1].ConnectorValue)) | (Convert.ToInt32(item.Input[0].ConnectorValue) << 32 - Convert.ToInt32(item.Input[1].ConnectorValue));
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.SEL)
|
||||
{
|
||||
output.Value.ConnectorValue = (item.Input[0].ConnectorValue == output.Key) ? item.Input[1].ConnectorValue : item.Input[2].ConnectorValue;
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.MAX)
|
||||
{
|
||||
output.Value.ConnectorValue = item.Input.Values.Select(p => p.ConnectorValue).Max();
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.MIN)
|
||||
{
|
||||
output.Value.ConnectorValue = item.Input.Values.Select(p => p.ConnectorValue).Min();
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.LIMIT)
|
||||
{
|
||||
output.Value.ConnectorValue = (item.Input[0].ConnectorValue > item.Input[1].ConnectorValue) ? item.Input[1].ConnectorValue : item.Input[0].ConnectorValue;
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.GT)
|
||||
{
|
||||
output.Value.ConnectorValue = (item.Input[0].ConnectorValue > item.Input[1].ConnectorValue) ? 1 : 0;
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.LT)
|
||||
{
|
||||
output.Value.ConnectorValue = (item.Input[0].ConnectorValue < item.Input[1].ConnectorValue) ? 1 : 0;
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.GE)
|
||||
{
|
||||
output.Value.ConnectorValue = (item.Input[0].ConnectorValue >= item.Input[1].ConnectorValue) ? 1 : 0;
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.LE)
|
||||
{
|
||||
output.Value.ConnectorValue = (item.Input[0].ConnectorValue <= item.Input[1].ConnectorValue) ? 1 : 0;
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.EQ)
|
||||
{
|
||||
output.Value.ConnectorValue = (item.Input[0].ConnectorValue == item.Input[1].ConnectorValue) ? 1 : 0;
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.NE)
|
||||
{
|
||||
output.Value.ConnectorValue = (item.Input[0].ConnectorValue != item.Input[1].ConnectorValue) ? 1 : 0;
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.ABS)
|
||||
{
|
||||
output.Value.ConnectorValue = Math.Abs(item.Input[0].ConnectorValue);
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.SQRT)
|
||||
{
|
||||
output.Value.ConnectorValue = Math.Sqrt(item.Input[0].ConnectorValue);
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.LN)
|
||||
{
|
||||
output.Value.ConnectorValue = Math.Log10(item.Input[0].ConnectorValue);
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.LOG)
|
||||
{
|
||||
output.Value.ConnectorValue = Math.Log(item.Input[0].ConnectorValue);
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.EXP)
|
||||
{
|
||||
output.Value.ConnectorValue = Math.Exp(item.Input[0].ConnectorValue);
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.SIN)
|
||||
{
|
||||
output.Value.ConnectorValue = Math.Sin(item.Input[0].ConnectorValue);
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.COS)
|
||||
{
|
||||
output.Value.ConnectorValue = Math.Cos(item.Input[0].ConnectorValue);
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.TAN)
|
||||
{
|
||||
output.Value.ConnectorValue = Math.Tan(item.Input[0].ConnectorValue);
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.ASIN)
|
||||
{
|
||||
output.Value.ConnectorValue = Math.Asin(item.Input[0].ConnectorValue);
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.ACOS)
|
||||
{
|
||||
output.Value.ConnectorValue = Math.Acos(item.Input[0].ConnectorValue);
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.ATAN)
|
||||
{
|
||||
output.Value.ConnectorValue = Math.Atan(item.Input[0].ConnectorValue);
|
||||
}
|
||||
else if (item.LogicalType == LogicalType.EXPT)
|
||||
{
|
||||
output.Value.ConnectorValue = Math.Exp(item.Input[0].ConnectorValue);
|
||||
}
|
||||
|
||||
if (output.Value.ValueTypePoint == ValueTypePoint.Bool)
|
||||
{
|
||||
if (output.Value.ConnectorValue == 0)
|
||||
{
|
||||
output.Value.ColorViewModel.FillColor.Color = Colors.Red;
|
||||
if (item.LogicalType == LogicalType.Output)
|
||||
{
|
||||
item.ColorViewModel.FillColor.Color = Colors.Red;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
output.Value.ColorViewModel.FillColor.Color = Colors.Green;
|
||||
if (item.LogicalType == LogicalType.Output)
|
||||
{
|
||||
item.ColorViewModel.FillColor.Color = Colors.Green;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
output.Value.ColorViewModel.FillColor.Color = Colors.Green;
|
||||
}
|
||||
}
|
||||
}
|
||||
item.GetInput();
|
||||
item.CalculateOutput();
|
||||
}
|
||||
}
|
||||
|
||||
private ConnectionViewModel GetSourceItem(FullyCreatedConnectorInfo sinkConnector)
|
||||
{
|
||||
foreach (var connector in DiagramViewModel.Items.OfType<ConnectionViewModel>())
|
||||
{
|
||||
if (connector.SinkConnectorInfo == sinkConnector)
|
||||
{
|
||||
return connector;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user