2

我想将Quine-McCluskey 算法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 和一些 MicroSpecification:

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 方法,我真的只需要 MicroSpecification4 和 MicroSpecification2。

所以我的问题是,有没有办法使用 Quine-McCluskey 将宏规范 IsSatisfiedBy 语句从四个规范简化为两个规范(或等效的东西)?

一般来说,必须给复合规范模式(并且不失一般性)模式设计提供某种“智能”会很好。

4

1 回答 1

1

您不能使用 Quine-McCluskey 进行简化,因为您正在寻找的简化需要知道 thatMicroSpecification2 => MicroSpecification1和 that MicroSpecification4 => MicroSpecification3。QM 仅适用于简化独立布尔变量的方程。

您可以根据 的最后 20 位重写您的规范candidate,然后使用 QM 来简化 22 个布尔变量中方程的混沌: 的 20 个低位candidate;如果设置了任何剩余位,candidate则为 true 的单个布尔值,并且当当且仅当是 时为 true 的单个布尔candidateint。由此产生的简化将适用于芯片设计,但可能不适用于任何其他目的。对不起。

于 2013-04-15T00:26:59.770 回答