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);
            }
        }
Exemplo n.º 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);
 }
Exemplo n.º 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);
            }
        }
Exemplo n.º 4
0
 public static IBooleanFunction NotOptimized(IBooleanFunction function)
 {
     if (function is NotFunction)
     {
         return(((NotFunction)function).Argument);
     }
     else
     {
         return(new NotFunction(function));
     }
 }
Exemplo n.º 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();
        }
Exemplo n.º 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);
                }
            }
        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();
            }
        }
        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);
        }
Exemplo n.º 9
0
 static void PrintWithTitle(string title, IBooleanFunction function)
 {
     Console.WriteLine(title + ": " + function.GetFormulaString(UniversityTeacherFormatting.Instance));
 }
Exemplo n.º 10
0
 public static NotFunction DobleNot(IBooleanFunction argument)
 => new NotFunction(new NotFunction(argument));
Exemplo n.º 11
0
 public NotFunction(IBooleanFunction argument)
 {
     this.Argument = argument;
 }