public static DnfExpression <T> Build <T>( INcfExpression <T> ncfExpression) { var allPossibleVarCombinationOf = NonCanonicalBoolExpressionHelpers.AllPossibleVarCombinationFor( ncfExpression: ncfExpression); var trueCombinationList = allPossibleVarCombinationOf.Where( line => NonCanonicalBoolExpressionHelpers.Execute( ncfExpression, line)); var dnfExpression = new HashSet <DnfAnd <T> >(); foreach (var trueCombination in trueCombinationList) { var elementSet = new HashSet <IDnfVariable <T> >(); foreach (var keyValue in trueCombination) { var variable = keyValue.Value ? (IDnfVariable <T>) new DnfVariable <T>(value: keyValue.Key) : (IDnfVariable <T>) new DnfNotVariable <T>(value: keyValue.Key); elementSet.Add( item: variable); } dnfExpression.Add(new DnfAnd <T>( elementSet: elementSet)); } return(new DnfExpression <T>( andBlockSet: dnfExpression)); }
public static List <Dictionary <T, bool> > AllPossibleVarCombinationFor <T>( INcfExpression <T> ncfExpression) { var listOfVar = GetVariables(ncfExpression: ncfExpression) .ToList(); var numberOfAllPossibleCombination = (ulong)Math.Pow(2, listOfVar.Count); var tableOfAllPossibleCombination = new List <Dictionary <T, bool> >(); var numberOfVars = listOfVar.Count; uint binaryMaskForLine = 0; for (var i = 0u; i < numberOfAllPossibleCombination; ++i) { var lineOfCombination = new Dictionary <T, bool>( capacity: numberOfVars); uint digit = 1; for (var j = 0; j < numberOfVars; j++) { lineOfCombination.Add( key: listOfVar[j], value: (binaryMaskForLine & digit) > 0); digit <<= 1; } binaryMaskForLine += 1; tableOfAllPossibleCombination.Add(lineOfCombination); } return(tableOfAllPossibleCombination); }
public static HashSet <T> GetVariables <T>( INcfExpression <T> ncfExpression) { switch (ncfExpression) { case NcfVariable <T> v: return(new HashSet <T> { v.Value }); case INcfBlock <T> blockOf: return(new HashSet <T>( collection: blockOf.TermList.SelectMany(GetVariables))); case NcfNot <T> not: return(GetVariables(ncfExpression: not.NcfExpression)); default: throw new ArgumentException( message: "pattern matching in C# is sucks", paramName: nameof(ncfExpression)); } }
public static bool Execute <T>( INcfExpression <T> ncfExpression, Dictionary <T, bool> values) { switch (ncfExpression) { case NcfVariable <T> v: return(values[v.Value]); case NcfAndBlock <T> and: return(and.TermList.All(t => Execute(t, values))); case NcfOrBlock <T> and: return(and.TermList.Any(t => Execute(t, values))); case NcfNot <T> not: return(!Execute(not.NcfExpression, values)); default: throw new ArgumentException( message: "pattern matching in C# is sucks", paramName: nameof(ncfExpression)); } }
public NcfNot( INcfExpression <TCondition> ncfExpression) { this.NcfExpression = ncfExpression; }
public NcfNot <T> Not( INcfExpression <T> ncfExpression) { return(new NcfNot <T>( ncfExpression: ncfExpression)); }