Пример #1
0
        public static IFuzzySet CompositionOfBinaryRelations(IFuzzySet relation1, IFuzzySet relation2, IBinaryFunction tNorm, IBinaryFunction sNorm)    //zadatak ne specificira koristi li se max-min kompozicija
        {                                                                                                                                               //stoga je konfigurabilno
            MutableFuzzySet compositeRelation = new MutableFuzzySet(Domain.Combine(relation1.GetDomain().GetComponent(0), relation2.GetDomain().GetComponent(1)));

            if (relation1 == relation2)             //jer ugnjezdjeni foreach nad istom 'relation' instancom stvara probleme
            {
                relation2 = DeepCopy(relation1);
            }

            foreach (DomainElement element1 in compositeRelation.GetDomain())
            {
                int    x        = element1.GetComponentValue(0);
                int    z        = element1.GetComponentValue(1);
                double funccomp = compositeRelation.GetValueAt(DomainElement.Of(x, z));
                double Max      = 0;

                foreach (DomainElement element_a in relation1.GetDomain())          //funca
                {
                    foreach (DomainElement element_b in relation2.GetDomain())      //funcb
                    {
                        if (element_a.GetComponentValue(1) == element_b.GetComponentValue(0))
                        {
                            int y = element_a.GetComponentValue(1);                 //y
                            Max = sNorm.ValueAt(tNorm.ValueAt(relation1.GetValueAt(DomainElement.Of(x, y)), relation2.GetValueAt(DomainElement.Of(y, z))), Max);
                        }
                    }
                }

                compositeRelation.Set(DomainElement.Of(x, z), Max);
            }

            return(compositeRelation);
        }
 public BadDirection(string name)
 {
     Name     = name;
     FuzzySet = new MutableFuzzySet(IDomain.IntRange(0, 2))
                .Set(DomainElement.Of(0), 1.0)
                .Set(DomainElement.Of(1), 0.0);
 }
Пример #3
0
        static void Primjer12()
        {
            IDomain d = Domain.IntRange(0, 11); // {0,1,...,10}
            //Debug1.Print(d, "Elementi domene d1:");

            IFuzzySet set1 = new MutableFuzzySet(d)
                             .Set(DomainElement.Of(0), 1.0)
                             .Set(DomainElement.Of(1), 0.8)
                             .Set(DomainElement.Of(2), 0.6)
                             .Set(DomainElement.Of(3), 0.4)
                             .Set(DomainElement.Of(4), 0.2);

            Debug1.Print(set1, "Set1:");

            IDomain   d2   = Domain.IntRange(-5, 6); // {-5,-4,...,4,5}
            IFuzzySet set2 = new CalculatedFuzzySet(
                d2,
                StandardFuzzySets.LambdaFunction(
                    d2.IndexOfElement(DomainElement.Of(-4)),
                    d2.IndexOfElement(DomainElement.Of(0)),
                    d2.IndexOfElement(DomainElement.Of(4))
                    )
                );

            Debug1.Print(set2, "Set2:");
        }
Пример #4
0
        static void Primjer13()
        {
            IDomain   d    = Domain.IntRange(0, 11);
            IFuzzySet set1 = new MutableFuzzySet(d)
                             .Set(DomainElement.Of(0), 1.0)
                             .Set(DomainElement.Of(1), 0.8)
                             .Set(DomainElement.Of(2), 0.6)
                             .Set(DomainElement.Of(3), 0.4)
                             .Set(DomainElement.Of(4), 0.2);

            Debug1.Print(set1, "Set1:");

            IFuzzySet notSet1 = Operations.Operations.UnaryOperation(
                set1, Operations.Operations.ZadehNot());

            Debug1.Print(notSet1, "notSet1:");



            IFuzzySet union = Operations.Operations.BinaryOperation(
                set1, notSet1, Operations.Operations.ZadehOr());

            Debug1.Print(union, "Set1 union notSet1:");

            IFuzzySet hinters = Operations.Operations.BinaryOperation(
                set1, notSet1, Operations.Operations.HamacherTNorm(1.0));

            Debug1.Print(hinters, "Set1 intersection with notSet1 using parameterised Hamacher T norm with parameter 1.0:");

            IFuzzySet presjek = Operations.Operations.BinaryOperation(set1, notSet1, Operations.Operations.ZadehAnd());

            Debug1.Print(presjek, "Set1 presjek notSet1:");
        }
Пример #5
0
        public static void Test33()
        {
            double[] antecMembershipFunctions = new double[] {
                0.8, 0.8, 0.7, 1, 0.7, 0.9
            };

            double minMembershipFunc = antecMembershipFunctions.Min();

            IDomain u = Domain.IntRange(-5, 6);

            IFuzzySet _conseq = new CalculatedFuzzySet(
                u,
                StandardFuzzySets.LambdaFunction(
                    u.IndexOfElement(DomainElement.Of(-5)),
                    u.IndexOfElement(DomainElement.Of(-3)),
                    u.IndexOfElement(DomainElement.Of(0))
                    )
                );

            Debug1.Print(_conseq, "Konsekv1:");


            IFuzzySet antecedents = new CalculatedFuzzySet(                                     //mjere pripadnosti su na minimumu mj.pripadnosti antecedenata
                _conseq.GetDomain(),
                StandardFuzzySets.UniversalFunction(minMembershipFunc)
                );

            IFuzzySet conclusion = Operations.Operations.BinaryOperation(antecedents, _conseq, Operations.Operations.ZadehAnd());

            Debug1.Print(conclusion, "Zakljucak1:");
        }
Пример #6
0
        public static void Primjer23()
        {
            IDomain   u = Domain.IntRange(1, 5); // {1,2,3,4}
            IFuzzySet r = new MutableFuzzySet(Domain.Combine(u, u))
                          .Set(DomainElement.Of(1, 1), 1)
                          .Set(DomainElement.Of(2, 2), 1)
                          .Set(DomainElement.Of(3, 3), 1)
                          .Set(DomainElement.Of(4, 4), 1)
                          .Set(DomainElement.Of(1, 2), 0.3)
                          .Set(DomainElement.Of(2, 1), 0.3)
                          .Set(DomainElement.Of(2, 3), 0.5)
                          .Set(DomainElement.Of(3, 2), 0.5)
                          .Set(DomainElement.Of(3, 4), 0.2)
                          .Set(DomainElement.Of(4, 3), 0.2);

            IFuzzySet r2 = r;

            Console.WriteLine("Početna relacija je neizrazita relacija ekvivalencije? " + Relations.Relations.IsFuzzyEquivalence(r2));
            Console.WriteLine();

            for (int i = 1; i <= 3; i++)
            {
                r2 = Relations.Relations.CompositionOfBinaryRelations(r2, r, Operations.Operations.ZadehAnd(), Operations.Operations.ZadehOr());
                Console.WriteLine("Broj odrađenih kompozicija: " + i + ". Relacija je:");
                foreach (DomainElement e in r2.GetDomain())
                {
                    Console.WriteLine("mu(" + e + ")=" + r2.GetValueAt(e));
                }
                Console.WriteLine("Ova relacija je neizrazita relacija ekvivalencije? " + Relations.Relations.IsFuzzyEquivalence(r2));
                Console.WriteLine();
            }
        }
Пример #7
0
        private void AddToDomain(T value)
        {
            bool found = false;

            for (int i = domainIndex; i < domain.Count && !found; i++)
            {
                if (EqualityComparer <T> .Default.Equals(domain[i].Value, value))
                {
                    domain[i].Count++;
                    domainIndex = (i + 1) % domain.Count;
                    found       = true;
                }
            }
            for (int i = 0; i < domainIndex && !found; i++)
            {
                if (EqualityComparer <T> .Default.Equals(domain[i].Value, value))
                {
                    domain[i].Count++;
                    domainIndex = (i + 1) % domain.Count;
                    found       = true;
                }
            }

            if (!found)
            {
                DomainElement newElement = new DomainElement(value);
                domain.Add(newElement);
                domainIndex = 0;
            }
        }
Пример #8
0
        private static void Main()
        {
            var d = Domain.IntRange(0, 11); // {0,1,...,10}

            IFuzzySet set1 = new MutableFuzzySet(d)
                             .Set(DomainElement.Of(0), 0.4)
                             .Set(DomainElement.Of(1), 0.8)
                             .Set(DomainElement.Of(2), 0.6)
                             .Set(DomainElement.Of(3), 0.4)
                             .Set(DomainElement.Of(4), 0.2);

            Console.WriteLine("Set 1:");
            Console.WriteLine(set1);
            Console.WriteLine();

            var notSet1 = Operations.UnaryOperation(set1, Operations.ZadehNot());

            Console.WriteLine("notSet 1:");
            Console.WriteLine(notSet1);
            Console.WriteLine();

            var union = Operations.BinaryOperation(set1, notSet1, Operations.ZadehOr());

            Console.WriteLine("Set1 union notSet1:");
            Console.WriteLine(union);
            Console.WriteLine();

            var hinters = Operations.BinaryOperation(set1, notSet1, Operations.HamacherTNorm(1.0));

            Console.WriteLine("Set1 intersection with notSet1 using parameterised Hamacher T norm with parameter 1.0:");
            Console.WriteLine(hinters);
            Console.WriteLine();

            Console.ReadKey();
        }
Пример #9
0
        private void lbxDomains_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbxDomains.SelectedItems.Count == 0)
            {
                txtDomainName.Text               = "";
                txtDomainSelector.Text           = "";
                txtDomainPrivateKeyFilename.Text = "";
                txtDomainDNS.Text        = "";
                gbxDomainDetails.Enabled = false;
                cbKeyLength.Text         = UserPreferences.Default.KeyLength.ToString();
            }
            else
            {
                DomainElement oSelected = (DomainElement)lbxDomains.SelectedItem;
                txtDomainName.Text               = oSelected.Domain;
                txtDomainSelector.Text           = oSelected.Selector;
                txtDomainPrivateKeyFilename.Text = oSelected.PrivateKeyFile;

                /*if (oSelected.CryptoProvider == null)
                 * {
                 *  oSelected.InitElement(Constants.DKIM_SIGNER_PATH);
                 * }
                 * else
                 * {
                 *  cbKeyLength.Text = oSelected.CryptoProvider.KeySize.ToString();
                 * }*/

                UpdateSuggestedDns();
                txtDomainDNS.Text        = "";
                gbxDomainDetails.Enabled = true;
                btDomainDelete.Enabled   = true;
                btDomainSave.Enabled     = false;
                bDataUpdated             = false;
            }
        }
Пример #10
0
        /// <summary>
        /// Button "delete domain" have been click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btDomainDelete_Click(object sender, EventArgs e)
        {
            if (lbxDomains.SelectedItem != null)
            {
                DomainElement oCurrentDomain = (DomainElement)lbxDomains.SelectedItem;
                oConfig.Domains.Remove(oCurrentDomain);
                lbxDomains.Items.Remove(oCurrentDomain);
                lbxDomains.SelectedItem = null;
            }

            string keyFile = Path.Combine(Constants.DkimSignerPath, "keys", txtDomainPrivateKeyFilename.Text);

            List <string> asFile = new List <string>();

            asFile.Add(keyFile);
            asFile.Add(keyFile + ".pub");
            asFile.Add(keyFile + ".pem");

            foreach (string sFile in asFile)
            {
                if (File.Exists(sFile) && ShowMessageBox("Delete key?", "Do you want me to delete the key file?\n" + sFile, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        File.Delete(sFile);
                    }
                    catch (IOException ex)
                    {
                        ShowMessageBox("Error deleting file", "Couldn't delete file:\n" + sFile + "\n" + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

            SaveDkimSignerConfig();
        }
Пример #11
0
        private static void PrintExample5()
        {
            Console.WriteLine("EXAMPLE 5");
            IDomain   u1 = IDomain.IntRange(1, 5); // {1,2,3,4}
            IDomain   u2 = IDomain.IntRange(1, 4); // {1,2,3}
            IDomain   u3 = IDomain.IntRange(1, 5); // {1,2,3,4}
            IFuzzySet r1 = new MutableFuzzySet(IDomain.Combine(u1, u2))
                           .Set(DomainElement.Of(1, 1), 0.3)
                           .Set(DomainElement.Of(1, 2), 1)
                           .Set(DomainElement.Of(3, 3), 0.5)
                           .Set(DomainElement.Of(4, 3), 0.5);
            IFuzzySet r2 = new MutableFuzzySet(IDomain.Combine(u2, u3))
                           .Set(DomainElement.Of(1, 1), 1)
                           .Set(DomainElement.Of(2, 1), 0.5)
                           .Set(DomainElement.Of(2, 2), 0.7)
                           .Set(DomainElement.Of(3, 3), 1)
                           .Set(DomainElement.Of(3, 4), 0.4);
            IFuzzySet r1r2 = Relations.CompositionOfBinaryRelations(r1, r2) !;

            foreach (var element in r1r2.GetDomain())
            {
                Console.WriteLine($"mu({element}) = {r1r2.GetValueAt(element)}");
            }
            Console.WriteLine("---------------------------------------");
            Console.WriteLine();
        }
Пример #12
0
        private static void Main()
        {
            var d1 = Domain.IntRange(0, 5);  // {0,1,2,3,4}

            Console.WriteLine("Elementi domene d1:");
            Console.WriteLine(d1);
            Console.WriteLine();

            var d2 = Domain.IntRange(0, 3);  // {0,1,2}

            Console.WriteLine("Elementi domene d2:");
            Console.WriteLine(d2);
            Console.WriteLine();

            var d3 = Domain.Combine(d1, d2);

            Console.WriteLine("Elementi domene d3:");
            Console.WriteLine(d3);
            Console.WriteLine();

            Console.WriteLine(d3.ElementForIndex(0));
            Console.WriteLine(d3.ElementForIndex(5));
            Console.WriteLine(d3.ElementForIndex(14));
            Console.WriteLine(d3.IndexOfElement(DomainElement.Of(4, 1)));

            Console.ReadKey();
        }
Пример #13
0
        static void Main(string[] args)
        {
            IDomain u1 = Domain.IntRange(1, 5);  // {1,2,3,4}
            IDomain u2 = Domain.IntRange(1, 4);  // {1,2,3}
            IDomain u3 = Domain.IntRange(1, 5);  // {1,2,3,4}

            IFuzzySet r1 = new MutableFuzzySet(Domain.Combine(u1, u2))
                           .Set(DomainElement.Of(1, 1), 0.3)
                           .Set(DomainElement.Of(1, 2), 1)
                           .Set(DomainElement.Of(3, 3), 0.5)
                           .Set(DomainElement.Of(4, 3), 0.5);

            IFuzzySet r2 = new MutableFuzzySet(Domain.Combine(u2, u3))
                           .Set(DomainElement.Of(1, 1), 1)
                           .Set(DomainElement.Of(2, 1), 0.5)
                           .Set(DomainElement.Of(2, 2), 0.7)
                           .Set(DomainElement.Of(3, 3), 1)
                           .Set(DomainElement.Of(3, 4), 0.4);

            IFuzzySet r1r2 = Relations.CompositionOfBinaryRelations(r1, r2);

            Console.WriteLine(r1r2);

            Console.ReadKey();
        }
Пример #14
0
        public static void Test32()
        {
            IDomain u  = Domain.IntRange(-5, 6); // {-5,-4,...,4,5}
            IDomain u2 = Domain.Combine(u, u);

            IFuzzySet set = new CalculatedFuzzySet(
                u2,
                StandardFuzzySets.LambdaFunction(
                    u2.IndexOfElement(DomainElement.Of(-4, 2)),
                    u2.IndexOfElement(DomainElement.Of(0, 0)),
                    u2.IndexOfElement(DomainElement.Of(4, 2))
                    )
                );

            Debug1.Print(set, "Set cf:");

            IFuzzySet notSet1 = Operations.Operations.UnaryOperation(
                set, Operations.Operations.ZadehNot());

            Debug1.Print(notSet1, "notSet1:");



            IFuzzySet union = Operations.Operations.BinaryOperation(
                set, notSet1, Operations.Operations.ZadehOr());

            Debug1.Print(union, "Set1 union notSet1:");
        }
        public IFuzzySet SingleRuleConclusionProduct(int L, int D, int LK, int DK, int V, int S)       //TODO napravi varijantu i za StrojMinimum i za StrojProdukt
        {
            double[] antecMembershipFunctions = new double[] {
                _antec1.GetValueAt(DomainElement.Of(L)),
                _antec2.GetValueAt(DomainElement.Of(D)),
                _antec3.GetValueAt(DomainElement.Of(LK)),
                _antec4.GetValueAt(DomainElement.Of(DK)),
                _antec5.GetValueAt(DomainElement.Of(V)),
                _antec6.GetValueAt(DomainElement.Of(S))
            };

            double prodMembershipFunc = 1;

            foreach (double var in antecMembershipFunctions)
            {
                prodMembershipFunc *= var;
            }

            IFuzzySet antecedents = new CalculatedFuzzySet(                                     //mjere pripadnosti su na minimumu mj.pripadnosti antecedenata
                _conseq.GetDomain(),
                StandardFuzzySets.UniversalFunction(prodMembershipFunc)
                );


            //KormiloFuzzySystemMin.Print(antecedents, "antecedents:");
            //KormiloFuzzySystemMin.Print(_conseq, "_conseq:");
            //KormiloFuzzySystemMin.Print(_antec5, "_antec:");


            IFuzzySet conclusion = Operations.Operations.BinaryOperation(antecedents, _conseq, Operations.Operations.AlgProduct());

            //KormiloFuzzySystemMin.Print(conclusion, "conclusion:");

            return(conclusion);
        }
Пример #16
0
        public static bool IsMaxMinTransitive(IFuzzySet relation)
        {
            IFuzzySet copiedRelation = DeepCopy(relation);              //jer ugnjezdjeni foreach nad istom 'relation' instancom stvara probleme

            foreach (DomainElement element1 in relation.GetDomain())
            {
                int    x      = element1.GetComponentValue(0);
                int    z      = element1.GetComponentValue(1);
                double funcxz = relation.GetValueAt(DomainElement.Of(x, z));
                double Max    = 0;
                foreach (DomainElement element2 in copiedRelation.GetDomain())
                {
                    if (element2.GetComponentValue(0) == element2.GetComponentValue(1))
                    {
                        int y = element2.GetComponentValue(0);
                        if (Math.Min(copiedRelation.GetValueAt(DomainElement.Of(x, y)), copiedRelation.GetValueAt(DomainElement.Of(y, z))) > Max)
                        {
                            Max = Math.Min(copiedRelation.GetValueAt(DomainElement.Of(x, y)), copiedRelation.GetValueAt(DomainElement.Of(y, z)));
                        }
                    }
                }
                if (funcxz < Max)
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #17
0
        public static bool IsMaxMinTransitive(IFuzzySet relation)
        {
            if (!IsUTimesURelation(relation))
            {
                return(false);
            }

            IDomain domain = relation.GetDomain()[0];

            foreach (var x in domain)
            {
                foreach (var z in domain)
                {
                    var value = 0.0;

                    foreach (var y in domain)
                    {
                        var xy = relation.GetValueAt(DomainElement.Of(x[0], y[0]));
                        var yz = relation.GetValueAt(DomainElement.Of(y[0], z[0]));

                        value = Math.Max(value, Math.Min(xy, yz));
                    }
                    if (value > relation.GetValueAt(DomainElement.Of(x[0], z[0])))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Пример #18
0
        public static IFuzzySet CompositionOfBinaryRelations(IFuzzySet r1, IFuzzySet r2)
        {
            IDomain xDomain = r1.GetDomain()[0];
            IDomain yDomain = r1.GetDomain()[1];
            IDomain zDomain = r2.GetDomain()[1];

            var result = new MutableFuzzySet(Domain.Combine(xDomain, zDomain));
            var or     = Operations.ZadehOr();
            var and    = Operations.ZadehAnd();

            foreach (var x in xDomain)
            {
                foreach (var z in zDomain)
                {
                    var value = 0.0;

                    foreach (var y in yDomain)
                    {
                        var value1 = r1.GetValueAt(DomainElement.Of(x[0], y[0]));
                        var value2 = r2.GetValueAt(DomainElement.Of(y[0], z[0]));

                        value = or(value, and(value1, value2));
                    }

                    result.Set(DomainElement.Of(x[0], z[0]), value);
                }
            }

            return(result);
        }
Пример #19
0
        private static void Main()
        {
            var d = Domain.IntRange(0, 11); // {0,1,...,10}

            IFuzzySet set1 = new MutableFuzzySet(d)
                             .Set(DomainElement.Of(0), 1.0)
                             .Set(DomainElement.Of(1), 0.8)
                             .Set(DomainElement.Of(2), 0.6)
                             .Set(DomainElement.Of(3), 0.4)
                             .Set(DomainElement.Of(4), 0.2);

            Console.WriteLine("Set 1:");
            Console.WriteLine(set1);
            Console.WriteLine();

            var       d2   = Domain.IntRange(-5, 6); // {-5,-4,...,4,5}
            IFuzzySet set2 = new CalculatedFuzzySet(
                d2,
                StandardFuzzySets.LambdaFunction(
                    d2.IndexOfElement(DomainElement.Of(-4)),
                    d2.IndexOfElement(DomainElement.Of(0)),
                    d2.IndexOfElement(DomainElement.Of(4))
                    )
                );

            Console.WriteLine("Set 2:");
            Console.WriteLine(set2);
            Console.WriteLine();

            Console.ReadKey();
        }
Пример #20
0
        private static void PrintExample3()
        {
            Console.WriteLine("EXAMPLE 3");
            IDomain d = IDomain.IntRange(0, 11);

            IFuzzySet set1 = new MutableFuzzySet(d).Set(DomainElement.Of(0), 1.0)
                             .Set(DomainElement.Of(1), 0.8)
                             .Set(DomainElement.Of(2), 0.6)
                             .Set(DomainElement.Of(3), 0.4)
                             .Set(DomainElement.Of(4), 0.2);

            PrintFuzzySet(set1, "Set1: ");

            IFuzzySet notSet1 = Operations.UnaryOperation(set1, Operations.ZadehNot());

            PrintFuzzySet(notSet1, "notSet1: ");

            IFuzzySet union = Operations.BinaryOperation(set1, notSet1, Operations.ZadehOr());

            PrintFuzzySet(union, "Set1 U notSet1: ");

            IFuzzySet hinters = Operations.BinaryOperation(set1, notSet1, Operations.HamacherTNorm(1.0));

            PrintFuzzySet(hinters, "Set1 Intersection with notSet1 using parameterised Hamacher T norm with parameter 1.0: ");
            Console.WriteLine("---------------------------------------");
            Console.WriteLine();
        }
        public static IFuzzySet?CompositionOfBinaryRelations(IFuzzySet A, IFuzzySet B)
        {
            if (!AreRelationsMultiplicative(A, B))
            {
                return(null);
            }

            var rowAComponent    = A.GetDomain().GetComponent(0);
            var columnAComponent = A.GetDomain().GetComponent(1);
            var rowBComponent    = B.GetDomain().GetComponent(0);
            var columnBComponent = B.GetDomain().GetComponent(1);

            var rowsA = rowAComponent.GetCardinality();
            var colsA = columnAComponent.GetCardinality();

            var colsB = columnBComponent.GetCardinality();

            var fuzzySet = new MutableFuzzySet(new CompositeDomain(new SimpleDomain[]
            {
                (SimpleDomain)rowAComponent,
                (SimpleDomain)columnBComponent
            }));

            for (var i = 0; i < rowsA; i++)
            {
                for (var j = 0; j < colsB; j++)
                {
                    var maxValue = 0.0;
                    for (var k = 0; k < colsA; k++)
                    {
                        var iElement = rowAComponent.ElementForIndex(i).GetComponentValue(0);
                        var jElement = columnBComponent.ElementForIndex(j).GetComponentValue(0);
                        var kElement = rowAComponent.ElementForIndex(k).GetComponentValue(0);

                        var ikElement = new DomainElement(iElement, kElement);
                        var kjElement = new DomainElement(kElement, jElement);

                        var ikElementValue  = A.GetValueAt(ikElement);
                        var kjElementValue  = B.GetValueAt(kjElement);
                        var newElementValue = Math.Min(ikElementValue, kjElementValue);

                        if (maxValue < newElementValue)
                        {
                            maxValue = newElementValue;
                        }
                    }
                    var iiElement = rowAComponent.ElementForIndex(i).GetComponentValue(0);
                    var jjElement = columnBComponent.ElementForIndex(j).GetComponentValue(0);

                    var ijElement = new DomainElement(iiElement, jjElement);
                    fuzzySet.Set(ijElement, maxValue);
                }
            }

            return(fuzzySet);
        }
Пример #22
0
 public static bool IsSymmetric(IFuzzySet relation)
 {
     foreach (DomainElement element1 in relation.GetDomain())
     {
         if (relation.GetValueAt(DomainElement.Of(element1.GetComponentValue(0), element1.GetComponentValue(1))) != relation.GetValueAt(DomainElement.Of(element1.GetComponentValue(1), element1.GetComponentValue(0))))
         {
             return(false);
         }
     }
     return(true);
 }
Пример #23
0
 /// <summary>
 /// Builds an unsigned DKIM-Signature header. Note that the returned
 /// header will NOT have a CRLF at the end.
 /// </summary>
 /// <param name="bodyHash">The hash of the body.</param>
 /// <returns>The unsigned DKIM-Signature header.</returns>
 private string GetUnsignedDkimHeader(string bodyHash, DomainElement domain)
 {
     return(string.Format(
                CultureInfo.InvariantCulture,
                "DKIM-Signature: v=1; a={0}; s={1}; d={2}; c=simple/simple; q=dns/txt; h={3}; bh={4}; b=;",
                this.hashAlgorithmDkimCode,
                domain.Selector,
                domain.Domain,
                string.Join(" : ", this.eligibleHeaders.OrderBy(x => x, StringComparer.Ordinal).ToArray()),
                bodyHash));
 }
Пример #24
0
        /// <summary>
        /// Signs the given mail item, if possible, according to the DKIM standard.
        /// </summary>
        /// <param name="mailItem">The mail item that is to be signed, if possible.</param>
        private void SignMailItem(MailItem mailItem)
        {
            // If the mail item is a "system message" then it will be read-only here,
            // and we can't sign it. Additionally, if the message has a "TnefPart",
            // then it is in a proprietary format used by Outlook and Exchange Server,
            // which means we shouldn't bother signing it.
            if (!mailItem.Message.IsSystemMessage && mailItem.Message.TnefPart == null)
            {
                /* Check if DKIM is defined for the current domain */
                DomainElement domain = null;
                foreach (DomainElement e in domainSettings)
                {
                    if (mailItem.FromAddress.DomainPart
                        .ToUpperInvariant()
                        .Contains(e.getDomain().ToUpperInvariant()))
                    {
                        domain = e;
                    }
                }

                /* If domain was found in define domain configuration, we just do nothing */
                if (domain != null)
                {
                    using (var inputStream = mailItem.GetMimeReadStream())
                    {
                        string dkim = this.dkimSigner.CanSign(domain, inputStream);

                        if (dkim.Length != 0)
                        {
                            Logger.LogInformation("Signing mail with header: " + dkim);

                            inputStream.Seek(0, SeekOrigin.Begin);
                            byte[] inputBuffer = ReadFully(inputStream);
                            inputStream.Close();

                            using (var outputStream = mailItem.GetMimeWriteStream())
                            {
                                try
                                {
                                    this.dkimSigner.Sign(inputBuffer, outputStream, dkim);
                                }
                                catch (Exception ex)
                                {
                                    Logger.LogError("Signing went terribly wrong: " + ex.ToString());
                                }

                                outputStream.Close();
                            }
                        }
                    }
                }
            }
        }
Пример #25
0
        public AkcelFuzzySystem(Defuzzifier defuzzifier)
        {
            this.defuzzifier = defuzzifier;

            IDomain acceleration = Domain.IntRange(-5, 6);
            IDomain direction    = Domain.IntRange(0, 2);
            IDomain distance     = Domain.IntRange(0, 1301);
            IDomain velocity     = Domain.IntRange(0, 100);

            MutableFuzzySet Id = new MutableFuzzySet(distance);
            MutableFuzzySet Iv = new MutableFuzzySet(velocity);

            foreach (var element in distance)
            {
                Id.Set(element, 1);
            }
            foreach (var element in velocity)
            {
                Iv.Set(element, 1);
            }

            IFuzzySet Slow   = new CalculatedFuzzySet(velocity, StandardFuzzySets.LFunction(0, 40));
            IFuzzySet Medium = new CalculatedFuzzySet(velocity, StandardFuzzySets.LambdaFunction(37, 45, 55));
            IFuzzySet Fast   = new CalculatedFuzzySet(velocity, StandardFuzzySets.GammaFunction(48, 60));

            IFuzzySet CriticalClose = new CalculatedFuzzySet(distance, StandardFuzzySets.LFunction(10, 30));
            IFuzzySet Close         = new CalculatedFuzzySet(distance, StandardFuzzySets.LFunction(25, 50));
            IFuzzySet Far           = new CalculatedFuzzySet(distance, StandardFuzzySets.GammaFunction(70, 100));

            IFuzzySet Backward = new MutableFuzzySet(direction).Set(DomainElement.Of(0), 1);
            IFuzzySet Forward  = new MutableFuzzySet(direction).Set(DomainElement.Of(1), 1);

            IFuzzySet SpeedUp = new MutableFuzzySet(acceleration)
                                .Set(DomainElement.Of(5), 1)
                                .Set(DomainElement.Of(4), 0.6)
                                .Set(DomainElement.Of(3), 0.3);
            IFuzzySet Neutral  = new MutableFuzzySet(acceleration).Set(DomainElement.Of(0), 1);
            IFuzzySet SlowDown = new MutableFuzzySet(acceleration)
                                 .Set(DomainElement.Of(-5), 1)
                                 .Set(DomainElement.Of(-4), 0.6)
                                 .Set(DomainElement.Of(-3), 0.3);

            // L, D, LK, RK, V, S
            rules = new List <Rule>();
            rules.Add(new Rule(new[] { Id, Id, CriticalClose, Id, Iv, Forward }, Neutral, tNorm, implication));
            rules.Add(new Rule(new[] { Id, Id, Id, CriticalClose, Iv, Forward }, Neutral, tNorm, implication));
            rules.Add(new Rule(new[] { Id, Id, CriticalClose, Id, Iv, Backward }, Neutral, tNorm, implication));
            rules.Add(new Rule(new[] { Id, Id, Id, CriticalClose, Iv, Backward }, Neutral, tNorm, implication));

            rules.Add(new Rule(new[] { Id, Id, Id, Id, Fast, Forward }, SlowDown, tNorm, implication));
            rules.Add(new Rule(new[] { Id, Id, Id, Id, Medium, Forward }, Neutral, tNorm, implication));
            rules.Add(new Rule(new[] { Id, Id, Id, Id, Slow, Forward }, SpeedUp, tNorm, implication));
        }
Пример #26
0
 /// <summary>
 /// Builds an unsigned DKIM-Signature header. Note that the returned
 /// header will NOT have a CRLF at the end.
 /// </summary>
 /// <param name="bodyHash">The hash of the body.</param>
 /// <returns>The unsigned DKIM-Signature header.</returns>
 private string GetUnsignedDkimHeader(DomainElement domain, string bodyHash)
 {
     return(string.Format(
                CultureInfo.InvariantCulture,
                "DKIM-Signature: v=1; a={0}; s={1}; d={2}; c={3}/{4}; q=dns/txt; h={5}; bh={6}; b=;",
                this.hashAlgorithmDkimCode,
                domain.getSelector(),
                domain.getDomain(),
                this.headerCanonicalization.ToString().ToLower(),
                this.bodyCanonicalization.ToString().ToLower(),
                string.Join(" : ", this.eligibleHeaders.OrderBy(x => x, StringComparer.Ordinal).ToArray()),
                bodyHash));
 }
Пример #27
0
        /// <summary>
        /// Gets the version of the DKIM-Signature header with the signature appended, along with
        /// the CRLF.
        /// </summary>
        /// <param name="unsignedDkimHeader">The unsigned DKIM header, to use as a template.</param>
        /// <param name="canonicalizedHeaders">The headers to be included as part of the signature.</param>
        /// <returns>The signed DKIM-Signature header.</returns>
        private string GetSignedDkimHeader(DomainElement domain, string unsignedDkimHeader, IEnumerable <string> canonicalizedHeaders)
        {
            byte[]        signatureBytes;
            string        signatureText;
            StringBuilder signedDkimHeader;

            if (domain.CryptoProvider == null)
            {
                throw new Exception("CryptoProvider for domain " + domain.getDomain() + " is null.");
            }

            using (var stream = new MemoryStream())
            {
                using (var writer = new StreamWriter(stream))
                {
                    foreach (var canonicalizedHeader in canonicalizedHeaders)
                    {
                        writer.Write(canonicalizedHeader);
                    }

                    if (this.headerCanonicalization == DkimCanonicalizationKind.Relaxed)
                    {
                        unsignedDkimHeader = Regex.Replace(unsignedDkimHeader, @" ?: ?", ":");
                        string[] temp = unsignedDkimHeader.Split(new char[] { ':' }, 2);
                        unsignedDkimHeader = temp[0].ToLower() + ":" + temp[1];
                    }

                    writer.Write(unsignedDkimHeader);
                    writer.Flush();

                    stream.Seek(0, SeekOrigin.Begin);

                    // Why not pass this.hashAlgorithm here, since we already have it? If we're supporting
                    // Exchange 2007, then we're stuck on CLR 2.0. The SHA-256 functionality was added in
                    // .NET 3.5 SP1, but it was done in such a way that the switch statement used internally
                    // by the Crypto .NET classes won't recognize the new SHA256CryptoServiceProvider type.
                    // So, we have to use the string method instead. More details available at
                    // http://blogs.msdn.com/b/shawnfa/archive/2008/08/25/using-rsacryptoserviceprovider-for-rsa-sha256-signatures.aspx
                    signatureBytes = domain.CryptoProvider.SignData(stream, this.hashAlgorithmCryptoCode);
                }
            }

            signatureText    = Convert.ToBase64String(signatureBytes);
            signedDkimHeader = new StringBuilder(unsignedDkimHeader.Substring(0, unsignedDkimHeader.Length - 1));
            signedDkimHeader.Append(signatureText);
            signedDkimHeader.Append(";\r\n");

            return(signedDkimHeader.ToString());
        }
Пример #28
0
        public static void Test31()
        {
            IDomain u  = Domain.IntRange(-5, 6); // {-5,-4,...,4,5}
            IDomain u2 = Domain.Combine(u, u);

            IFuzzySet set = new CalculatedFuzzySet(
                u2,
                StandardFuzzySets.LambdaFunction(
                    u2.IndexOfElement(DomainElement.Of(-4, 2)),
                    u2.IndexOfElement(DomainElement.Of(0, 0)),
                    u2.IndexOfElement(DomainElement.Of(4, 2))
                    )
                );

            Debug1.Print(set, "Set cf:");
        }
Пример #29
0
        private static void Main(string[] args)
        {
            SimpleDomain sd = new SimpleDomain(-4, 5);

            PrintDomain(sd, "sd: ");

            IDomain cd = IDomain.Combine(sd, sd);

            PrintDomain(cd, "cd: ");

            IDomain   d    = IDomain.IntRange(0, 11);
            IFuzzySet set1 = new MutableFuzzySet(d).Set(DomainElement.Of(0), 1.0);

            PrintFuzzySet(set1, "set1: ");

            IDomain   d2   = IDomain.IntRange(-5, 6);
            IFuzzySet set2 = new CalculatedFuzzySet(d2,
                                                    StandardFuzzySets.LambdaFunction(d2.IndexOfElement(DomainElement.Of(-4)),
                                                                                     d2.IndexOfElement(DomainElement.Of(0)),
                                                                                     d2.IndexOfElement(DomainElement.Of(4))));

            PrintFuzzySet(set2, "set2: ");


            IDomain d7 = IDomain.IntRange(0, 11);

            IFuzzySet set3 = new MutableFuzzySet(d7).Set(DomainElement.Of(0), 1.0)
                             .Set(DomainElement.Of(1), 0.8)
                             .Set(DomainElement.Of(2), 0.6)
                             .Set(DomainElement.Of(3), 0.4)
                             .Set(DomainElement.Of(4), 0.2);

            PrintFuzzySet(set3, "set3: ");

            IFuzzySet notSet3 = Operations.UnaryOperation(set3, Operations.ZadehNot());

            PrintFuzzySet(notSet3, "notSet3: ");

            IFuzzySet union = Operations.BinaryOperation(set3, notSet3, Operations.ZadehOr());

            PrintFuzzySet(union, "Set3 U notSet3: ");

            IFuzzySet hinters = Operations.BinaryOperation(set3, notSet3, Operations.HamacherTNorm(1.0));

            PrintFuzzySet(hinters, "Set3 Intersection with notSet3 using parameterised Hamacher T norm with parameter 1.0: ");
        }
Пример #30
0
        /// <summary>
        /// Returns a value indicating whether or not the unsigned MIME message in the
        /// given stream can be signed. In this case, we iterate until we see the From:
        /// header, and then we only sign it if our domain matches the domain of the From:
        /// address.
        /// </summary>
        /// <param name="inputStream">The input stream.</param>
        /// <returns>The output stream.</returns>
        public string CanSign(DomainElement domain, Stream inputStream)
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException("Exchange DkimSigner disposed.");
            }

            inputStream.Seek(0, SeekOrigin.Begin);

            // Generate the hash for the body
            var bodyHash           = this.GetBodyHash(inputStream);
            var unsignedDkimHeader = this.GetUnsignedDkimHeader(domain, bodyHash);

            // Generate the hash for the header
            var canonicalizedHeaders = this.GetCanonicalizedHeaders(inputStream);
            var signedDkimHeader     = this.GetSignedDkimHeader(domain, unsignedDkimHeader, canonicalizedHeaders);

            return(signedDkimHeader);
        }
Пример #31
0
 public DomainElementSigner(DomainElement domain, MimeKit.Cryptography.DkimSigner signerInstance)
 {
     DomainElement = domain;
     Signer = signerInstance;
 }