public void IndexRule_Successful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType<Type>();
            objectTheorem.RegisterSuperType<Field>();

            var valueField1 = objectTheorem.CreateInstance<ValueField>("ValueField1");

            var stringType = objectTheorem.CreateInstance<ValueType>("StringType");

            // Act
            EFConstraints.AddIndexRuleConstraints(objectTheorem.ConstraintBuilder, stringType);

            objectTheorem.ConstraintBuilder
                .Assert(() => valueField1.Type == stringType);

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => valueField1.HasIndexAttribute == true);
            var assume2 = objectTheorem.ConstraintBuilder.Assume(() => valueField1.HasMaxLengthAttribute == true);

            // Assert
            var solved = objectTheorem.Solve();
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);
        }
        public void ComplexTypeConvention_Successful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType<Z3.ObjectTheorem.EF.Metamodel.Field>();
            objectTheorem.RegisterSuperType<Z3.ObjectTheorem.EF.Metamodel.Type>();

            var addressType = objectTheorem.CreateInstance<ReferenceType>("AddressType");
            var nameField = objectTheorem.CreateInstance<ValueField>("NameField");

            // Act
            EFConstraints.AddComplexTypeRuleConstraints(objectTheorem.ConstraintBuilder);

            objectTheorem.ConstraintBuilder
                .Assert(() => !addressType.HasCollectionFields && !addressType.HasSingleFields)
                .Assert(() => addressType.ValueFields == new[] { nameField });

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => addressType.HasComplexTypeAttribute == true);

            // Assert
            var solved = objectTheorem.Solve();
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);

            bool HasComplexTypeAttribute = solved.GetValue(addressType, i => i.HasComplexTypeAttribute);
            Assert.IsTrue(HasComplexTypeAttribute);
        }
Example #3
0
        public void IndexRule_Successful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType <Type>();
            objectTheorem.RegisterSuperType <Field>();

            var valueField1 = objectTheorem.CreateInstance <ValueField>("ValueField1");

            var stringType = objectTheorem.CreateInstance <ValueType>("StringType");

            // Act
            EFConstraints.AddIndexRuleConstraints(objectTheorem.ConstraintBuilder, stringType);

            objectTheorem.ConstraintBuilder
            .Assert(() => valueField1.Type == stringType);

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => valueField1.HasIndexAttribute == true);
            var assume2 = objectTheorem.ConstraintBuilder.Assume(() => valueField1.HasMaxLengthAttribute == true);

            // Assert
            var solved = objectTheorem.Solve();

            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);
        }
        public void ComplexTypeConvention_Successful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType <Z3.ObjectTheorem.EF.Metamodel.Field>();
            objectTheorem.RegisterSuperType <Z3.ObjectTheorem.EF.Metamodel.Type>();

            var addressType = objectTheorem.CreateInstance <ReferenceType>("AddressType");
            var nameField   = objectTheorem.CreateInstance <ValueField>("NameField");

            // Act
            EFConstraints.AddComplexTypeRuleConstraints(objectTheorem.ConstraintBuilder);

            objectTheorem.ConstraintBuilder
            .Assert(() => !addressType.HasCollectionFields && !addressType.HasSingleFields)
            .Assert(() => addressType.ValueFields == new[] { nameField });

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => addressType.HasComplexTypeAttribute == true);

            // Assert
            var solved = objectTheorem.Solve();

            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);

            bool HasComplexTypeAttribute = solved.GetValue(addressType, i => i.HasComplexTypeAttribute);

            Assert.IsTrue(HasComplexTypeAttribute);
        }
        public void DatabaseGeneratedRule_Successful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType<Type>();
            var employeeType = objectTheorem.CreateInstance<ReferenceType>("EmployeeType");

            objectTheorem.RegisterSuperType<Field>();
            objectTheorem.SetPossibleStringValues("Id", "ID", "EmployeeTypeId", "EmployeeTypeID");
            var idField = objectTheorem.CreateInstance<ValueField>("IdField");

            var stringType = objectTheorem.CreateInstance<ValueType>("StringType");
            var numberType = objectTheorem.CreateInstance<ValueType>("NumberType");
            var guidType = objectTheorem.CreateInstance<ValueType>("GuidType");

            // Act
            EFConstraints.AddPrimayKeyRuleConstraints(objectTheorem.ConstraintBuilder);
            EFConstraints.AddDatabaseGeneratedRuleConstraints(objectTheorem.ConstraintBuilder, stringType, numberType, guidType);

            objectTheorem.ConstraintBuilder
                .Assert(() => employeeType.IsEntity == true)
                .Assert(() => employeeType.ValueFields == new[] { idField })
                .Assert(() => idField.Name == "Id")
                .Assert(() => idField.Owner == employeeType)
                .Assert(() => idField.Type == numberType)
                .Assert(() => idField.DatabaseGeneratedAttribute == DatabaseGeneratedAttribute.None);

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => idField.DatabaseGeneratedAttribute == DatabaseGeneratedAttribute.None);

            // Assert
            var solved = objectTheorem.Solve();
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);
        }
        public void ForeignKeyDiscoveryConvention_Successful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType<Type>();
            objectTheorem.RegisterSuperType<Field>();
            objectTheorem.RegisterSuperType<ReferenceField>();

            var nullType = objectTheorem.CreateInstance<ReferenceType>("NullType");

            var categoryType = objectTheorem.CreateInstance<ReferenceType>("CategoryType");
            var categoryIdField = objectTheorem.CreateInstance<ValueField>("CategoryIdField");

            var productType = objectTheorem.CreateInstance<ReferenceType>("ProductType");
            var productIdField = objectTheorem.CreateInstance<ValueField>("ProductIdField");

            var productCategoryIdField = objectTheorem.CreateInstance<ValueField>("productCategoryIdField");
            var productCategoryField = objectTheorem.CreateInstance<SingleField>("productCategoryField");

            var stringType = objectTheorem.CreateInstance<ValueType>("StringType");
            var numberType = objectTheorem.CreateInstance<ValueType>("NumberType");
            var guidType = objectTheorem.CreateInstance<ValueType>("GuidType");

            objectTheorem.SetPossibleStringValues("CatId", "ProdId", "CategoryId", "Category", "-");

            // Act
            objectTheorem.ConstraintBuilder
                .AssertAll<SingleField>(sf => sf.Owner.ValueFields.Any(vf => vf.Name == sf.ForeignKeyAttribute && vf.ForeignType == sf.Type))
                .AssertAll<ValueField>(vf => vf.ForeignType == nullType ^ vf.ForeignType.ValueFields.Any(vf2 => vf2.IsPrimaryKey && vf2.Type == vf.Type))

                .Assert(() => categoryIdField.Owner == categoryType)
                .Assert(() => categoryIdField.Name == "CatId")
                .Assert(() => categoryIdField.Type == stringType)
                .Assert(() => categoryIdField.IsPrimaryKey == true)
                .Assert(() => categoryType.ValueFields == new[] { categoryIdField })

                .Assert(() => productIdField.Owner == productType)
                .Assert(() => productIdField.Name == "ProdId")
                .Assert(() => productIdField.Type == guidType)
                .Assert(() => productIdField.IsPrimaryKey == true)
                .Assert(() => productCategoryIdField.Owner == productType)
                .Assert(() => productCategoryIdField.Name == "CategoryId")
                .Assert(() => productCategoryIdField.Type == stringType)
                .Assert(() => productCategoryField.Owner == productType)
                .Assert(() => productCategoryField.Name == "Category")
                .Assert(() => productCategoryField.Type == categoryType)
                .Assert(() => productType.ValueFields == new[] { productIdField, productCategoryIdField })
                .Assert(() => productType.SingleFields == new[] { productCategoryField });

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => productCategoryField.ForeignKeyAttribute == "CategoryId");

            // Assert
            var solved = objectTheorem.Solve();
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);

            var test = solved.GetValue(productCategoryIdField, p => p.Type);
            Assert.AreEqual(stringType, test);
        }
        public void NotMappedRule_Unsuccessful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType<Type>();
            objectTheorem.RegisterSuperType<Field>();
            objectTheorem.RegisterSuperType<ReferenceField>();

            var customerType = objectTheorem.CreateInstance<ReferenceType>("Customer1");
            var addressType = objectTheorem.CreateInstance<ReferenceType>("Address1");

            var arrayOfAddressField = objectTheorem.CreateInstance<CollectionField>("AddressField");
            var namesField = objectTheorem.CreateInstance<ValueField>("NamesField");

            var arrayOfStringType = objectTheorem.CreateInstance<ValueType>("CollectionOfStringType");

            // Act
            EFConstraints.AddNotMappedRuleConstraints(objectTheorem.ConstraintBuilder, new[] { arrayOfStringType });

            objectTheorem.ConstraintBuilder
                .Assert(() => customerType.ValueFields == new[] { namesField })
                .Assert(() => namesField.Owner == customerType)
                .Assert(() => namesField.Type == arrayOfStringType)
                .Assert(() => customerType.CollectionFields == new[] { arrayOfAddressField })
                .Assert(() => arrayOfAddressField.Owner == customerType)
                .Assert(() => arrayOfAddressField.Type == addressType)
                .Assert(() => addressType.HasComplexTypeAttribute == true);

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => arrayOfAddressField.HasNotMappedAttribute == false);
            var assume2 = objectTheorem.ConstraintBuilder.Assume(() => namesField.HasNotMappedAttribute == false);

            // Assert
            var solved = objectTheorem.Solve();
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Unsatisfiable, solved.Status);

            var assume1Result = solved.IsAssumptionWrong(assume1);
            Assert.IsTrue(assume1Result);
            objectTheorem.ConstraintBuilder.RemoveAssumption(assume1);

            solved = objectTheorem.Solve();
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Unsatisfiable, solved.Status);

            var assume2Result = solved.IsAssumptionWrong(assume2);
            Assert.IsTrue(assume2Result);
            objectTheorem.ConstraintBuilder.RemoveAssumption(assume2);

            solved = objectTheorem.Solve();
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);

            bool HasNotMappedAttribute1 = solved.GetValue(arrayOfAddressField, f => f.HasNotMappedAttribute);
            bool HasNotMappedAttribute2 = solved.GetValue(namesField, f => f.HasNotMappedAttribute);
            Assert.IsTrue(HasNotMappedAttribute1);
            Assert.IsTrue(HasNotMappedAttribute2);
        }
        public void IdKeyDiscoveryConvention_And_KeyAttributeConvention_Unsuccessful_Unsatisfiable_()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType<Z3.ObjectTheorem.EF.Metamodel.Type>();
            var employeeType = objectTheorem.CreateInstance<ReferenceType>("EmployeeType");

            objectTheorem.RegisterSuperType<Field>();
            objectTheorem.SetPossibleStringValues("Id", "Title", "Name", "Income", "ID", "EmployeeTypeId", "EmployeeTypeID");
            var titleField = objectTheorem.CreateInstance<ValueField>("TitleField");
            var nameField = objectTheorem.CreateInstance<ValueField>("NameField");
            var incomeField = objectTheorem.CreateInstance<ValueField>("IncomeField");

            // Act
            EFConstraints.AddPrimayKeyRuleConstraints(objectTheorem.ConstraintBuilder);

            objectTheorem.ConstraintBuilder
                .Assert(() => employeeType.IsEntity == true)
                .Assert(() => employeeType.ValueFields == new[] { titleField, nameField, incomeField })
                .Assert(() => titleField.Name == "Title")
                .Assert(() => titleField.Owner == employeeType)
                .Assert(() => nameField.Name == "Name")
                .Assert(() => nameField.Owner == employeeType)
                .Assert(() => incomeField.Name == "Income")
                .Assert(() => incomeField.Owner == employeeType);

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => titleField.HasKeyAttribute == false);
            var assume2 = objectTheorem.ConstraintBuilder.Assume(() => nameField.HasKeyAttribute == false);
            var assume3 = objectTheorem.ConstraintBuilder.Assume(() => incomeField.HasKeyAttribute == false);

            // Assert
            var solved = objectTheorem.Solve();
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Unsatisfiable, solved.Status);

            var assume1Result = solved.IsAssumptionWrong(assume1);
            Assert.IsTrue(assume1Result);
            objectTheorem.ConstraintBuilder.RemoveAssumption(assume1);

            solved = objectTheorem.Solve();
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);

            bool HasKeyAttribute1 = solved.GetValue(titleField, i => i.HasKeyAttribute);
            bool HasKeyAttribute2 = solved.GetValue(nameField, i => i.HasKeyAttribute);
            bool HasKeyAttribute3 = solved.GetValue(incomeField, i => i.HasKeyAttribute);
            Assert.IsTrue(HasKeyAttribute1);
            Assert.IsFalse(HasKeyAttribute2);
            Assert.IsFalse(HasKeyAttribute3);

            bool IsPrimaryKey1 = solved.GetValue(titleField, i => i.IsPrimaryKey);
            bool IsPrimaryKey2 = solved.GetValue(nameField, i => i.IsPrimaryKey);
            bool IsPrimaryKey3 = solved.GetValue(incomeField, i => i.IsPrimaryKey);
            Assert.IsTrue(IsPrimaryKey1);
            Assert.IsFalse(IsPrimaryKey2);
            Assert.IsFalse(IsPrimaryKey3);
        }
Example #9
0
        public void ForeignKeyTypeMatchRule_Unsuccessful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType <Type>();
            objectTheorem.RegisterSuperType <Field>();
            objectTheorem.RegisterSuperType <ReferenceField>();

            var productType  = objectTheorem.CreateInstance <ReferenceType>("ProductType");
            var categoryType = objectTheorem.CreateInstance <ReferenceType>("CategoryType");

            var stringType = objectTheorem.CreateInstance <ValueType>("StringType");
            var guidType   = objectTheorem.CreateInstance <ValueType>("GuidType");

            var categoryField   = objectTheorem.CreateInstance <SingleField>("CategoryField");
            var categoryIdField = objectTheorem.CreateInstance <ValueField>("CategoryIdField");
            var idField         = objectTheorem.CreateInstance <ValueField>("IdField");

            objectTheorem.SetPossibleStringValues("ID", "Id", "CategoryId");

            // Act
            objectTheorem.ConstraintBuilder
            .Assert(() => categoryIdField.Type == idField.Type)

            .Assert(() => productType.SingleFields == new[] { categoryField })
            .Assert(() => categoryField.Owner == productType)
            .Assert(() => categoryField.Type == categoryType)
            .Assert(() => productType.ValueFields == new[] { categoryIdField })
            .Assert(() => categoryIdField.Owner == productType)
            .Assert(() => categoryIdField.Name == "CategoryId")
            .Assert(() => categoryType.ValueFields == new[] { idField })
            .Assert(() => idField.Owner == categoryType)
            .Assert(() => idField.Name == "Id")
            .Assert(() => idField.Type == guidType);

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => categoryIdField.Type == stringType);

            // Assert
            var solved = objectTheorem.Solve();

            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Unsatisfiable, solved.Status);

            var assume1Result = solved.IsAssumptionWrong(assume1);

            Assert.IsTrue(assume1Result);
            objectTheorem.ConstraintBuilder.RemoveAssumption(assume1);

            solved = objectTheorem.Solve();
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);

            ValueType Type1 = solved.GetValue(categoryIdField, f => f.Type);

            Assert.AreEqual(guidType, Type1);
        }
        public void ForeignKeyTypeMatchRule_Unsuccessful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType<Type>();
            objectTheorem.RegisterSuperType<Field>();
            objectTheorem.RegisterSuperType<ReferenceField>();

            var productType = objectTheorem.CreateInstance<ReferenceType>("ProductType");
            var categoryType = objectTheorem.CreateInstance<ReferenceType>("CategoryType");

            var stringType = objectTheorem.CreateInstance<ValueType>("StringType");
            var guidType = objectTheorem.CreateInstance<ValueType>("GuidType");

            var categoryField = objectTheorem.CreateInstance<SingleField>("CategoryField");
            var categoryIdField = objectTheorem.CreateInstance<ValueField>("CategoryIdField");
            var idField = objectTheorem.CreateInstance<ValueField>("IdField");

            objectTheorem.SetPossibleStringValues("ID", "Id", "CategoryId");

            // Act
            objectTheorem.ConstraintBuilder
                .Assert(() => categoryIdField.Type == idField.Type)

                .Assert(() => productType.SingleFields == new[] { categoryField })
                .Assert(() => categoryField.Owner == productType)
                .Assert(() => categoryField.Type == categoryType)
                .Assert(() => productType.ValueFields == new[] { categoryIdField })
                .Assert(() => categoryIdField.Owner == productType)
                .Assert(() => categoryIdField.Name == "CategoryId")
                .Assert(() => categoryType.ValueFields == new[] { idField })
                .Assert(() => idField.Owner == categoryType)
                .Assert(() => idField.Name == "Id")
                .Assert(() => idField.Type == guidType);

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => categoryIdField.Type == stringType);

            // Assert
            var solved = objectTheorem.Solve();
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Unsatisfiable, solved.Status);

            var assume1Result = solved.IsAssumptionWrong(assume1);
            Assert.IsTrue(assume1Result);
            objectTheorem.ConstraintBuilder.RemoveAssumption(assume1);

            solved = objectTheorem.Solve();
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);

            ValueType Type1 = solved.GetValue(categoryIdField, f => f.Type);
            Assert.AreEqual(guidType, Type1);
        }
Example #11
0
        public void IdKeyDiscoveryConvention_Successful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType <Z3.ObjectTheorem.EF.Metamodel.Type>();
            var employeeType = objectTheorem.CreateInstance <ReferenceType>("EmployeeType");

            objectTheorem.RegisterSuperType <Field>();
            objectTheorem.SetPossibleStringValues("Id", "Name", "Income", "ID", "EmployeeTypeId", "EmployeeTypeID");
            var idField     = objectTheorem.CreateInstance <ValueField>("IdField");
            var nameField   = objectTheorem.CreateInstance <ValueField>("NameField");
            var incomeField = objectTheorem.CreateInstance <ValueField>("IncomeField");

            // Act
            EFConstraints.AddPrimayKeyRuleConstraints(objectTheorem.ConstraintBuilder);

            objectTheorem.ConstraintBuilder
            .Assert(() => employeeType.IsEntity == true)
            .Assert(() => employeeType.ValueFields == new[] { idField, nameField, incomeField })
            .Assert(() => idField.Name == "Id")
            .Assert(() => idField.Owner == employeeType)
            .Assert(() => nameField.Name == "Name")
            .Assert(() => nameField.Owner == employeeType)
            .Assert(() => incomeField.Name == "Income")
            .Assert(() => incomeField.Owner == employeeType);

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => idField.HasKeyAttribute == false);
            var assume2 = objectTheorem.ConstraintBuilder.Assume(() => nameField.HasKeyAttribute == false);
            var assume3 = objectTheorem.ConstraintBuilder.Assume(() => incomeField.HasKeyAttribute == false);

            // Assert
            var solved = objectTheorem.Solve();

            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);

            bool HasKeyAttribute1 = solved.GetValue(idField, i => i.HasKeyAttribute);
            bool HasKeyAttribute2 = solved.GetValue(nameField, i => i.HasKeyAttribute);
            bool HasKeyAttribute3 = solved.GetValue(incomeField, i => i.HasKeyAttribute);

            Assert.IsFalse(HasKeyAttribute1);
            Assert.IsFalse(HasKeyAttribute2);
            Assert.IsFalse(HasKeyAttribute3);

            bool IsPrimaryKey1 = solved.GetValue(idField, i => i.IsPrimaryKey);
            bool IsPrimaryKey2 = solved.GetValue(nameField, i => i.IsPrimaryKey);
            bool IsPrimaryKey3 = solved.GetValue(incomeField, i => i.IsPrimaryKey);

            Assert.IsTrue(IsPrimaryKey1);
            Assert.IsFalse(IsPrimaryKey2);
            Assert.IsFalse(IsPrimaryKey3);
        }
        public void HM_WithRefPropsAndAssert_ShouldBe_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType <Vehicle>();
            var bicycleInstance1 = objectTheorem.CreateInstance <Bicycle>("BicycleInstance1");
            var bicycleInstance2 = objectTheorem.CreateInstance <Bicycle>("BicycleInstance2");
            var carInstance1     = objectTheorem.CreateInstance <Car>("CarInstance1");

            // Act
            objectTheorem.ConstraintBuilder
            .AssertAll <Vehicle>(v => v.Speed > 0)
            .AssertAll <Bicycle, Car>((b, c) => b.Speed < c.Speed)
            .Assert(() => bicycleInstance1.Speed == 10);

            // Assert
            var solved = objectTheorem.Solve();

            Assert.IsNotNull(solved);

            int bicycle1Speed = solved.GetValue(bicycleInstance1, i => i.Speed);
            int bicycle2Speed = solved.GetValue(bicycleInstance2, i => i.Speed);
            int carSpeed      = solved.GetValue(carInstance1, i => i.Speed);

            Assert.AreEqual(10, bicycle1Speed);
            Assert.IsTrue(bicycle2Speed > 0);

            Assert.IsTrue(carSpeed > 0);
            Assert.IsTrue(bicycle1Speed < carSpeed);
            Assert.IsTrue(bicycle2Speed < carSpeed);
        }
        public void HM_WithRefPropsAndAssert_ShouldBe_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType<Vehicle>();
            var bicycleInstance1 = objectTheorem.CreateInstance<Bicycle>("BicycleInstance1");
            var bicycleInstance2 = objectTheorem.CreateInstance<Bicycle>("BicycleInstance2");
            var carInstance1 = objectTheorem.CreateInstance<Car>("CarInstance1");

            // Act
            objectTheorem.ConstraintBuilder
                .AssertAll<Vehicle>(v => v.Speed > 0)
                .AssertAll<Bicycle, Car>((b, c) => b.Speed < c.Speed)
                .Assert(() => bicycleInstance1.Speed == 10);

            // Assert
            var solved = objectTheorem.Solve();
            Assert.IsNotNull(solved);

            int bicycle1Speed = solved.GetValue(bicycleInstance1, i => i.Speed);
            int bicycle2Speed = solved.GetValue(bicycleInstance2, i => i.Speed);
            int carSpeed = solved.GetValue(carInstance1, i => i.Speed);

            Assert.AreEqual(10, bicycle1Speed);
            Assert.IsTrue(bicycle2Speed > 0);
            
            Assert.IsTrue(carSpeed > 0);
            Assert.IsTrue(bicycle1Speed < carSpeed);
            Assert.IsTrue(bicycle2Speed < carSpeed);
        }
Example #14
0
        public ObjectTheoremResult Solve(IEnumerable <INamedTypeSymbol> entityTypeSymbols)
        {
            _objectTheorem.RegisterSuperType <Metamodel.Type>();
            _objectTheorem.RegisterSuperType <Field>();
            _objectTheorem.RegisterSuperType <ReferenceField>();

            var stopwatch = Stopwatch.StartNew();

            var efMetamodelInstantiation = new EFMetamodelInstantiation(entityTypeSymbols, _objectTheorem, _classAssumptions, _propertyAssumptions);

            efMetamodelInstantiation.GenerateConstraints();

            stopwatch.Stop();
            Trace.WriteLine("Parsing & Metamodel Instantiation: " + stopwatch.Elapsed);

            EFConstraints.AddAll(_objectTheorem, efMetamodelInstantiation.CreatedTypes);

            return(_objectTheorem.Solve());
        }
Example #15
0
        public void IndexRule_Unsuccessful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType <Type>();
            objectTheorem.RegisterSuperType <Field>();

            var valueField1 = objectTheorem.CreateInstance <ValueField>("ValueField1");

            var stringType = objectTheorem.CreateInstance <ValueType>("StringType");

            // Act
            EFConstraints.AddIndexRuleConstraints(objectTheorem.ConstraintBuilder, stringType);

            objectTheorem.ConstraintBuilder
            .Assert(() => valueField1.Type == stringType);

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => valueField1.HasIndexAttribute == true);
            var assume2 = objectTheorem.ConstraintBuilder.Assume(() => valueField1.HasMaxLengthAttribute == false);

            // Assert
            var solved = objectTheorem.Solve();

            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Unsatisfiable, solved.Status);

            var assume1Result = solved.IsAssumptionWrong(assume1);

            Assert.IsTrue(assume1Result);
            objectTheorem.ConstraintBuilder.RemoveAssumption(assume1);

            solved = objectTheorem.Solve();
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);

            bool HasComplexTypeAttribute = solved.GetValue(valueField1, i => i.HasIndexAttribute);

            Assert.IsFalse(HasComplexTypeAttribute);
        }
        public void NotMappedRule_Successful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType <Type>();
            objectTheorem.RegisterSuperType <Field>();
            objectTheorem.RegisterSuperType <ReferenceField>();

            var customerType = objectTheorem.CreateInstance <ReferenceType>("Customer1");
            var addressType  = objectTheorem.CreateInstance <ReferenceType>("Address1");

            var arrayOfAddressField = objectTheorem.CreateInstance <CollectionField>("AddressField");
            var namesField          = objectTheorem.CreateInstance <ValueField>("NamesField");

            var arrayOfStringType = objectTheorem.CreateInstance <ValueType>("CollectionOfStringType");

            // Act
            EFConstraints.AddNotMappedRuleConstraints(objectTheorem.ConstraintBuilder, new[] { arrayOfStringType });

            objectTheorem.ConstraintBuilder
            .Assert(() => customerType.ValueFields == new[] { namesField })
            .Assert(() => namesField.Owner == customerType)
            .Assert(() => namesField.Type == arrayOfStringType)
            .Assert(() => customerType.CollectionFields == new[] { arrayOfAddressField })
            .Assert(() => arrayOfAddressField.Owner == customerType)
            .Assert(() => arrayOfAddressField.Type == addressType)
            .Assert(() => addressType.HasComplexTypeAttribute == true);

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => arrayOfAddressField.HasNotMappedAttribute == true);
            var assume2 = objectTheorem.ConstraintBuilder.Assume(() => namesField.HasNotMappedAttribute == true);

            // Assert
            var solved = objectTheorem.Solve();

            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);
        }
        public void IndexRule_Unsuccessful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType<Type>();
            objectTheorem.RegisterSuperType<Field>();

            var valueField1 = objectTheorem.CreateInstance<ValueField>("ValueField1");

            var stringType = objectTheorem.CreateInstance<ValueType>("StringType");

            // Act
            EFConstraints.AddIndexRuleConstraints(objectTheorem.ConstraintBuilder, stringType);

            objectTheorem.ConstraintBuilder
                .Assert(() => valueField1.Type == stringType);

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => valueField1.HasIndexAttribute == true);
            var assume2 = objectTheorem.ConstraintBuilder.Assume(() => valueField1.HasMaxLengthAttribute == false);

            // Assert
            var solved = objectTheorem.Solve();
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Unsatisfiable, solved.Status);

            var assume1Result = solved.IsAssumptionWrong(assume1);
            Assert.IsTrue(assume1Result);
            objectTheorem.ConstraintBuilder.RemoveAssumption(assume1);

            solved = objectTheorem.Solve();
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);

            bool HasComplexTypeAttribute = solved.GetValue(valueField1, i => i.HasIndexAttribute);
            Assert.IsFalse(HasComplexTypeAttribute);
        }
        public void DatabaseGeneratedRule_Successful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType <Type>();
            var employeeType = objectTheorem.CreateInstance <ReferenceType>("EmployeeType");

            objectTheorem.RegisterSuperType <Field>();
            objectTheorem.SetPossibleStringValues("Id", "ID", "EmployeeTypeId", "EmployeeTypeID");
            var idField = objectTheorem.CreateInstance <ValueField>("IdField");

            var stringType = objectTheorem.CreateInstance <ValueType>("StringType");
            var numberType = objectTheorem.CreateInstance <ValueType>("NumberType");
            var guidType   = objectTheorem.CreateInstance <ValueType>("GuidType");

            // Act
            EFConstraints.AddPrimayKeyRuleConstraints(objectTheorem.ConstraintBuilder);
            EFConstraints.AddDatabaseGeneratedRuleConstraints(objectTheorem.ConstraintBuilder, stringType, numberType, guidType);

            objectTheorem.ConstraintBuilder
            .Assert(() => employeeType.IsEntity == true)
            .Assert(() => employeeType.ValueFields == new[] { idField })
            .Assert(() => idField.Name == "Id")
            .Assert(() => idField.Owner == employeeType)
            .Assert(() => idField.Type == numberType)
            .Assert(() => idField.DatabaseGeneratedAttribute == DatabaseGeneratedAttribute.None);

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => idField.DatabaseGeneratedAttribute == DatabaseGeneratedAttribute.None);

            // Assert
            var solved = objectTheorem.Solve();

            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);
        }
        public void ForeignKeyDiscoveryConvention_Successful_Satisfiable()
        {
            // Arrange
            var objectTheorem = new ObjectTheoremContext();

            objectTheorem.RegisterSuperType <Type>();
            objectTheorem.RegisterSuperType <Field>();
            objectTheorem.RegisterSuperType <ReferenceField>();

            var nullType = objectTheorem.CreateInstance <ReferenceType>("NullType");

            var categoryType    = objectTheorem.CreateInstance <ReferenceType>("CategoryType");
            var categoryIdField = objectTheorem.CreateInstance <ValueField>("CategoryIdField");

            var productType    = objectTheorem.CreateInstance <ReferenceType>("ProductType");
            var productIdField = objectTheorem.CreateInstance <ValueField>("ProductIdField");

            var productCategoryIdField = objectTheorem.CreateInstance <ValueField>("productCategoryIdField");
            var productCategoryField   = objectTheorem.CreateInstance <SingleField>("productCategoryField");

            var stringType = objectTheorem.CreateInstance <ValueType>("StringType");
            var numberType = objectTheorem.CreateInstance <ValueType>("NumberType");
            var guidType   = objectTheorem.CreateInstance <ValueType>("GuidType");

            objectTheorem.SetPossibleStringValues("CatId", "ProdId", "CategoryId", "Category", "-");

            // Act
            objectTheorem.ConstraintBuilder
            .AssertAll <SingleField>(sf => sf.Owner.ValueFields.Any(vf => vf.Name == sf.ForeignKeyAttribute && vf.ForeignType == sf.Type))
            .AssertAll <ValueField>(vf => vf.ForeignType == nullType ^ vf.ForeignType.ValueFields.Any(vf2 => vf2.IsPrimaryKey && vf2.Type == vf.Type))

            .Assert(() => categoryIdField.Owner == categoryType)
            .Assert(() => categoryIdField.Name == "CatId")
            .Assert(() => categoryIdField.Type == stringType)
            .Assert(() => categoryIdField.IsPrimaryKey == true)
            .Assert(() => categoryType.ValueFields == new[] { categoryIdField })

            .Assert(() => productIdField.Owner == productType)
            .Assert(() => productIdField.Name == "ProdId")
            .Assert(() => productIdField.Type == guidType)
            .Assert(() => productIdField.IsPrimaryKey == true)
            .Assert(() => productCategoryIdField.Owner == productType)
            .Assert(() => productCategoryIdField.Name == "CategoryId")
            .Assert(() => productCategoryIdField.Type == stringType)
            .Assert(() => productCategoryField.Owner == productType)
            .Assert(() => productCategoryField.Name == "Category")
            .Assert(() => productCategoryField.Type == categoryType)
            .Assert(() => productType.ValueFields == new[] { productIdField, productCategoryIdField })
            .Assert(() => productType.SingleFields == new[] { productCategoryField });

            var assume1 = objectTheorem.ConstraintBuilder.Assume(() => productCategoryField.ForeignKeyAttribute == "CategoryId");

            // Assert
            var solved = objectTheorem.Solve();

            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);

            var test = solved.GetValue(productCategoryIdField, p => p.Type);

            Assert.AreEqual(stringType, test);
        }