如何将Quine-McCluskey算法与复合规范模式相结合
本文关键字:范模式 模式 相结合 复合 Quine-McCluskey 算法 | 更新日期: 2023-09-27 18:09:13
我想将Quine-McCluskey算法与复合规范模式结合起来。
首先,让我们考虑一些基础知识,c#中的规范模式:
public interface ISpecification
{
bool IsSatisfiedBy(object candidate);
ISpecification And(ISpecification other);
ISpecification Or(ISpecification other);
ISpecification Not();
}
public abstract class CompositeSpecification : ISpecification
{
public abstract bool IsSatisfiedBy(object candidate);
public ISpecification And(ISpecification other)
{
return new AndSpecification(this, other);
}
public ISpecification Or(ISpecification other)
{
return new OrSpecification(this, other);
}
public ISpecification Not()
{
return new NotSpecification(this);
}
}
public class AndSpecification : CompositeSpecification
{
private ISpecification One;
private ISpecification Other;
public AndSpecification(ISpecification x, ISpecification y)
{
One = x;
Other = y;
}
public override bool IsSatisfiedBy(object candidate)
{
return One.IsSatisfiedBy(candidate) && Other.IsSatisfiedBy(candidate);
}
}
public class OrSpecification : CompositeSpecification
{
private ISpecification One;
private ISpecification Other;
public OrSpecification(ISpecification x, ISpecification y)
{
One = x;
Other = y;
}
public override bool IsSatisfiedBy(object candidate)
{
return One.IsSatisfiedBy(candidate) || Other.IsSatisfiedBy(candidate);
}
}
public class NotSpecification : CompositeSpecification
{
private ISpecification Wrapped;
public NotSpecification(ISpecification x)
{
Wrapped = x;
}
public override bool IsSatisfiedBy(object candidate)
{
return !Wrapped.IsSatisfiedBy(candidate);
}
}
现在假设我有一个MacroSpecification和一些MicroSpecifications:
public class MacroSpecification : CompositeSpecification
{
MicroSpecification1 Spec1 = new MicroSpecification1();
MicroSpecification2 Spec2 = new MicroSpecification2();
MicroSpecification3 Spec3 = new MicroSpecification3();
MicroSpecification4 Spec4 = new MicroSpecification4();
public override bool IsSatisfiedBy (object candidate)
{
if (candidate is int)
{
return Spec1.And(Spec2).And(Spec3).And(Spec4).IsSatisfiedBy(candidate);
}
}
}
public class MicroSpecification1 : CompositeSpecification
{
public override bool IsSatisfiedBy (object candidate)
{
if (candidate is int)
{
return (candidate > 100);
}
return false;
}
}
public class MicroSpecification2 : CompositeSpecification
{
public override bool IsSatisfiedBy (object candidate)
{
if (candidate is int)
{
return (candidate > 300);
}
return false;
}
}
public class MicroSpecification3 : CompositeSpecification
{
public override bool IsSatisfiedBy (object candidate)
{
if (candidate is int)
{
return (candidate < 1000000);
}
return false;
}
}
public class MicroSpecification4 : CompositeSpecification
{
public override bool IsSatisfiedBy (object candidate)
{
if (candidate is int)
{
return (candidate < 500000);
}
return false;
}
}
对于简化的、等效的MacroSpecification IsSatisfiedBy方法,我真的只需要microspeciation4和microspeciation2。
所以我的问题是,是否有一种方法可以使用Quine-McCluskey将宏规范IsSatisfiedBy语句从四个规范简化为两个规范(或等效的东西)?
在不丧失通用性的情况下,给复合规范模式(Composite Specification Pattern)模式设计提供某种"智能"是很好的。
你不能用Quine-McCluskey来做这个化简,因为你正在寻找的化简需要知道MicroSpecification2 => MicroSpecification1
和MicroSpecification4 => MicroSpecification3
。Q-M只适用于布尔独立变量方程的化简。
您可以根据candidate
的最后20位重写您的规范,然后使用Q-M来简化22个布尔变量的方程混沌:candidate
的20个低阶位;一个布尔值,如果candidate
的剩余位被设置,则为true;如果candidate
是int
,则为true。由此产生的简化将适用于芯片设计,但可能不适用于任何其他目的。对不起。