我想将Quine–McCluskey Algorithm与Composite Specification Pattern合并。
首先,让我们考虑基础知识,即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;
}
}
我真的只需要MicroSpecification4和MicroSpecification2来实现简化的等效MacroSpecification IsSatisfiedBy方法。
所以我的问题是,有没有办法使用Quine-McCluskey将宏指定IsSatisfiedBy语句从四个规范简化为两个规范(或等效的东西)?
最好不要给出复合规格模式(并且不失一般性)模式设计一般都是某种“智能”。
答案 0 :(得分:1)
您无法使用Quine-McCluskey进行简化,因为您需要的简化要求知道MicroSpecification2 => MicroSpecification1
和MicroSpecification4 => MicroSpecification3
。 Q-M仅适用于简化独立布尔变量的方程。
您可以根据candidate
的最后20位重写规范,然后使用QM简化22个布尔变量中的方程混沌:candidate
的20个低位;单个布尔值,如果设置了candidate
中的任何剩余位,则为true;如果candidate
为int
,则为true的单个布尔值。由此产生的简化适用于芯片设计,但可能不适用于任何其他目的。遗憾。