コード例 #1
0
        public static bool TryConvertAndToOr(IBooleanFunction boolFunction, out IBooleanFunction result)
        {
            result = default(IBooleanFunction);

            bool isNotRoot = false;

            if (boolFunction is NotFunction)
            {
                isNotRoot    = true;
                boolFunction = ((NotFunction)boolFunction).Argument;
            }

            if (boolFunction is OrFunction)
            {
                var orFunction = (OrFunction)boolFunction;
                var conjunctionOfInversions = new AndFunction(
                    orFunction.Items.Select(i => new NotFunction(i)));
                result = isNotRoot ? orFunction : (IBooleanFunction) new NotFunction(orFunction);
                return(false);
            }
            else
            {
                return(false);
            }
        }
コード例 #2
0
 /// <summary>
 /// Private constructor.
 /// </summary>
 /// <param name="report">An instance of the report model.</param>
 /// <param name="events">An instance of an event publish/subscribe engine.</param>
 /// <param name="expressionFunction">The expression to evaluate</param>
 private ExpressionReportFrequency(Report report, IEvent events, IBooleanFunction expressionFunction)
 {
     this.report             = report;
     this.events             = events;
     this.expressionFunction = expressionFunction;
     events.Subscribe("[Clock].DoReport", OnDoReport);
 }
コード例 #3
0
        /// <summary>
        /// Gets if function is input function or inversed input.
        /// </summary>
        public static bool TestInputInversable(IBooleanFunction function)
        {
            switch (function)
            {
            case InputFunction input: return(true);

            case NotFunction not: return(TestInputInversable(not.Argument));

            default: return(false);
            }
        }
コード例 #4
0
 public static IBooleanFunction NotOptimized(IBooleanFunction function)
 {
     if (function is NotFunction)
     {
         return(((NotFunction)function).Argument);
     }
     else
     {
         return(new NotFunction(function));
     }
 }
コード例 #5
0
        static void Main(string[] args)
        {
            bool[] output = BitTools.GetOutputBool(KaterynaBodnarchukTask.KateBodnarchukCase);

            PrintWithTitle("Original Function", LogicConvert.ToOrFunction(PositiveMcCluskeyMethod.GetConstituents(output)));

            IList <Implicant> minimalDisjunctionalNormalForm =
                PositiveMcCluskeyMethod.GetImplicantDisjunctionNormalForm(output);

            PrintWithTitle("Optimized Function", LogicConvert.ToOrFunction(minimalDisjunctionalNormalForm));
            Console.WriteLine();

            bool[] outputInversed = output.Select(i => !i).ToArray();
            PrintWithTitle(
                "Original Inversed Function",
                LogicConvert.ToOrFunction(PositiveMcCluskeyMethod.GetConstituents(outputInversed)));

            IBooleanFunction minimalDisjunctionalNormalFormInversed =
                LogicConvert.ToOrFunction(PositiveMcCluskeyMethod.GetImplicantDisjunctionNormalForm(outputInversed));

            PrintWithTitle("Optimized Inversed Function", minimalDisjunctionalNormalFormInversed);
            Console.WriteLine();

            IBooleanFunction minimalDisjunctionalNormalFormDobleInversed = new NotFunction(minimalDisjunctionalNormalFormInversed);

            PrintWithTitle("Optimized Inversed Function Inversed", minimalDisjunctionalNormalFormDobleInversed);
            Console.WriteLine();

            IBooleanFunction andOfNotAndForm = RecursiveTransform.ExecuteRecursive(
                minimalDisjunctionalNormalFormDobleInversed, DeMorgansLaw.TryConvertNotOfOrToAndOfNot);

            PrintWithTitle("Not of not and", andOfNotAndForm);

            IBooleanFunction andOfOrForm = RecursiveTransform.ExecuteRecursive(
                andOfNotAndForm, DeMorgansLaw.TryConvertNotOfAndToOrOfNot);

            PrintWithTitle("And of or", andOfOrForm);

            IBooleanFunction doubleNotOfAndOfOrForm = NotFunction.DobleNot(andOfOrForm);
            IBooleanFunction notOfOrNotOfOrForm     = RecursiveTransform.ExecuteRecursive(
                doubleNotOfAndOfOrForm, DeMorgansLaw.TryConvertNotOfAndToOrOfNot);

            PrintWithTitle("NotOfOr/NotOfOr", notOfOrNotOfOrForm);

            uint input = uint.Parse(Console.ReadLine());

            Console.WriteLine("Output: " + notOfOrNotOfOrForm.Evaluate(new UInt32EvaluationContext(input)));
            Console.ReadKey();
        }
コード例 #6
0
        private void OnSimulationCommencing(object sender, EventArgs e)
        {
            if (forages == null)
            {
                throw new Exception("No forages component found in simulation.");
            }
            var parentZone = Parent as Zone;

            if (parentZone == null)
            {
                throw new Exception("SimpleGrazing is not in a zone");
            }
            allForages          = forages.ModelsWithDigestibleBiomass.Where(forage => forage.Zone == parentZone).ToList();
            ProportionOfTotalDM = new double[allForages.Count()];

            if (GrazingRotationType == GrazingRotationTypeEnum.TargetMass)
            {
                if (PreGrazeDMArray == null || PreGrazeDMArray.Length != 12)
                {
                    throw new Exception("There must be 12 values input for the pre-grazing DM");
                }
                if (PostGrazeDMArray == null || PostGrazeDMArray.Length != 12)
                {
                    throw new Exception("There must be 12 values input for the post-grazing DM");
                }
            }
            else if (GrazingRotationType == GrazingRotationTypeEnum.Flexible)
            {
                if (string.IsNullOrEmpty(FlexibleExpressionForTimingOfGrazing))
                {
                    throw new Exception("You must specify an expression for timing of grazing.");
                }
                if (CSharpExpressionFunction.Compile(FlexibleExpressionForTimingOfGrazing, this, compiler, out IBooleanFunction f, out string errors))
                {
                    expressionFunction = f;
                }
                else
                {
                    throw new Exception(errors);
                }
            }
コード例 #7
0
        public static IBooleanFunction ExecuteRecursive(IBooleanFunction booleanFunction, TryTransformHandler tryTransform)
        {
            switch (booleanFunction)
            {
            case InputFunction inputFunction: return(inputFunction);

            case NotFunction notFunction:
                var argumentTransformed = ExecuteRecursive(notFunction.Argument, tryTransform);
                var internals           = NotFunction.NotOptimized(argumentTransformed);
                if (tryTransform(internals, out IBooleanFunction internalsFullyTransformed))
                {
                    return(internalsFullyTransformed);
                }
                else
                {
                    return(internals);
                }

            case AndFunction andFunction:
                var andItemsTransformed = new List <IBooleanFunction>();
                foreach (var item in andFunction.Items)
                {
                    var itemTransformed = ExecuteRecursive(item, tryTransform);
                    andItemsTransformed.Add(itemTransformed);
                }
                return(new AndFunction(andItemsTransformed));

            case OrFunction orFunction:
                var orItemsTransformed = new List <IBooleanFunction>();
                foreach (var item in orFunction.Items)
                {
                    var itemTransformed = ExecuteRecursive(item, tryTransform);
                    orItemsTransformed.Add(itemTransformed);
                }
                return(new OrFunction(orItemsTransformed));

            default: throw new NotSupportedException();
            }
        }
コード例 #8
0
        public static bool TryConvertNotOfAndToOrOfNot(
            IBooleanFunction boolFunction, out IBooleanFunction result)
        {
            result = null;

            if (!(boolFunction is NotFunction))
            {
                return(false);
            }

            NotFunction root = (NotFunction)boolFunction;

            if (!(root.Argument is AndFunction))
            {
                return(false);
            }

            AndFunction andFunction = (AndFunction)root.Argument;

            result = new OrFunction(
                andFunction.Items.Select(NotFunction.NotOptimized)
                );
            return(true);
        }
コード例 #9
0
 static void PrintWithTitle(string title, IBooleanFunction function)
 {
     Console.WriteLine(title + ": " + function.GetFormulaString(UniversityTeacherFormatting.Instance));
 }
コード例 #10
0
 public static NotFunction DobleNot(IBooleanFunction argument)
 => new NotFunction(new NotFunction(argument));
コード例 #11
0
 public NotFunction(IBooleanFunction argument)
 {
     this.Argument = argument;
 }