Esempio n. 1
0
        public void TestEqualsForRulesWithDifferLiteralsQuantity()
        {
            var first  = new RuleGeneric(new[] { 1, 2 }, Reason.JobInstall, null);
            var second = new RuleGeneric(new[] { 1 }, Reason.JobInstall, null);

            Assert.IsFalse(first.Equals(second));
        }
Esempio n. 2
0
        public void TestEqualsForRulesWithSameLiterals()
        {
            var first  = new RuleGeneric(new[] { 1, 2 }, Reason.JobInstall, null);
            var second = new RuleGeneric(new[] { 1, 2 }, Reason.JobInstall, null);

            Assert.IsTrue(first.Equals(second));
        }
Esempio n. 3
0
        public void TestIsAssertions()
        {
            var first  = new RuleGeneric(new[] { 1, 2 }, Reason.JobInstall, null);
            var second = new RuleGeneric(new[] { 1 }, Reason.JobInstall, null);

            Assert.IsFalse(first.IsAssertion);
            Assert.IsTrue(second.IsAssertion);
        }
Esempio n. 4
0
        public void TestToString()
        {
            var rule = new RuleGeneric(new[] { 1, -2 }, Reason.JobInstall, null);

            Assert.AreEqual("(-2|1)", rule.ToString());
            rule.Enable = false;
            Assert.AreEqual("disabled(-2|1)", rule.ToString());
        }
Esempio n. 5
0
        public void TestSetAndGetType()
        {
            var rule = new RuleGeneric(Array.Empty <int>(), Reason.JobInstall, null);

            rule.SetRuleType(RuleType.Job);

            Assert.AreEqual(RuleType.Job, rule.GetRuleType());
        }
Esempio n. 6
0
        private static void ProcessCA()
        {
            string[] values = _caRule.Split('-');
            if (values.Length != 5)
            {
                Console.WriteLine("[ERROR] Missing arguments for --ca option");
            }

            try
            {
                int width    = Convert.ToInt32(values[0]);
                int length   = Convert.ToInt32(values[1]);
                int height   = Convert.ToInt32(values[2]);
                int lifetime = Convert.ToInt32(values[3]);


                string[] conditions = values[4].Split('/');
                int      a          = Convert.ToInt32(conditions[0]);
                int      b          = Convert.ToInt32(conditions[1]);
                int[,,] field = new int[width, length, height];


                // Random initial positions
                Random r = new Random((int)DateTime.Now.Ticks);
                for (int y = 0; y < height; y++)
                {
                    for (int z = 0; z < length; z++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            field[x, y, z] = r.Next(0, 1 + 1);
                        }
                    }
                }

                //for (int y = 0; y < height; y++)
                //{
                //    for (int z = 0; z < length; z++)
                //    {
                //        for (int x = 0; x < width; x++)
                //        {
                //            field[x, y, z] = 1;
                //        }
                //    }
                //}

                RuleSet            ruleSet   = new RuleGeneric(field, width, length, height, a, b);
                RuleSetToSchematic converter = new RuleSetToSchematic("", ruleSet, lifetime);
                Schematic          schematic = converter.WriteSchematic();
                VoxWriter          writer    = new VoxWriter();
                writer.WriteModel(_outputFile + ".vox", schematic, _direction);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.ReadLine();
            }
        }
Esempio n. 7
0
        public void TestGetLastLiteral()
        {
            var reason1 = new RuleGeneric(Array.Empty <int>(), Reason.Undefined, null);
            var reason2 = new RuleGeneric(Array.Empty <int>(), Reason.Undefined, null);

            decisions.Decide(1, 1, reason1);
            decisions.Decide(-2, 2, reason2);

            Assert.AreEqual(-2, decisions.GetLastLiteral());
        }
Esempio n. 8
0
        public void TestCount()
        {
            var first = new RuleGeneric(new[] { 1 }, Reason.JobInstall, null);

            var ruleSet = new RuleSet();

            ruleSet.Add(first, RuleType.Job);

            Assert.AreEqual(1, ruleSet.Count);
        }
Esempio n. 9
0
        public void TestEnable()
        {
            var rule = new RuleGeneric(Array.Empty <int>(), Reason.JobInstall, null)
            {
                Enable = false,
            };

            Assert.AreEqual(false, rule.Enable);

            rule.Enable = true;
            Assert.AreEqual(true, rule.Enable);
        }
Esempio n. 10
0
        public void TestPrettyString(string expected, Reason reason)
        {
            var pool       = new Pool();
            var package1   = Helper.MockPackage("foo", "2.1");
            var package2   = Helper.MockPackage("baz", "1.1");
            var repository = Helper.MockRepository(package1, package2);

            pool.AddRepository(repository);

            var rule = new RuleGeneric(new[] { package1.Id, -package2.Id }, reason, null);

            Assert.AreEqual(expected, rule.GetPrettyString(pool));
        }
Esempio n. 11
0
        public void TestGetDecisionReason()
        {
            var reason1 = new RuleGeneric(Array.Empty <int>(), Reason.Undefined, null);
            var reason2 = new RuleGeneric(Array.Empty <int>(), Reason.Undefined, null);

            decisions.Decide(1, 1, reason1);
            decisions.Decide(-2, 2, reason2);

            Assert.AreSame(reason1, decisions.GetDecisionReason(1));
            Assert.AreSame(reason1, decisions.GetDecisionReason(-1));
            Assert.AreSame(reason2, decisions.GetDecisionReason(2));
            Assert.AreSame(reason2, decisions.GetDecisionReason(-2));
        }
Esempio n. 12
0
        public void TestGetPrettyString()
        {
            var pool       = new Pool();
            var package    = Helper.MockPackage("foo", "2.1");
            var repository = Helper.MockRepository(package);

            pool.AddRepository(repository);
            var literal = package.Id;

            var ruleSet = new RuleSet();
            var rule    = new RuleGeneric(new[] { literal }, Reason.JobInstall, null);

            ruleSet.Add(rule, RuleType.Job);
            StringAssert.Contains(ruleSet.GetPrettyString(pool), "Job     : Install command rule (install foo 2.1)");
        }
Esempio n. 13
0
        public void TestAddIgnoresDuplicates()
        {
            var first  = new RuleGeneric(new[] { 1 }, Reason.JobInstall, null);
            var second = new RuleGeneric(new[] { 1 }, Reason.JobInstall, null);
            var third  = new RuleGeneric(new[] { 1 }, Reason.JobInstall, null);

            var ruleSet = new RuleSet();

            ruleSet.Add(first, RuleType.Job);
            Assert.IsFalse(ruleSet.Add(second, RuleType.Job));
            Assert.IsFalse(ruleSet.Add(third, RuleType.Job));

            Assert.AreEqual(1, ruleSet.Count);
            CollectionAssert.AreEqual(new[] { first }, ruleSet.GetEnumeratorFor(RuleType.Job).ToArray());
            CollectionAssert.AreEqual(Array.Empty <Rule>(), ruleSet.GetEnumeratorFor(RuleType.Learned).ToArray());
        }
Esempio n. 14
0
        public void TestRuleById()
        {
            var first  = new RuleGeneric(new[] { 1 }, Reason.JobInstall, null);
            var second = new RuleGeneric(new[] { 2 }, Reason.JobInstall, null);
            var third  = new RuleGeneric(Array.Empty <int>(), Reason.InternalAllowUpdate, null);

            var ruleSet = new RuleSet();

            ruleSet.Add(first, RuleType.Job);
            ruleSet.Add(third, RuleType.Learned);
            ruleSet.Add(second, RuleType.Job);

            Assert.AreEqual(first, ruleSet.GetRuleById(0));
            Assert.AreEqual(third, ruleSet.GetRuleById(1));
            Assert.AreEqual(second, ruleSet.GetRuleById(2));
        }
Esempio n. 15
0
        public void TestPrettyStringWithRequire()
        {
            var pool       = new Pool();
            var package1   = Helper.MockPackage("foo", "2.1");
            var package2   = Helper.MockPackage("baz", "1.1");
            var package3   = Helper.MockPackage("boo", "1.2");
            var package4   = Helper.MockPackage("boo", "6.8");
            var repository = Helper.MockRepository(package1, package2, package3, package4);

            pool.AddRepository(repository);
            var link = new Link("foo", "boo", new Constraint("=", "1.2"));

            var rule = new RuleGeneric(new[] { -package1.Id, package2.Id, package3.Id, package4.Id }, Reason.PackageRequire, link);

            Assert.AreEqual("foo 2.1 relates to boo == 1.2 -> satisfiable by baz[1.1], boo[1.2, 6.8].", rule.GetPrettyString(pool));
        }
Esempio n. 16
0
        public void TestAddAndGetEnumeratorFor()
        {
            var first  = new RuleGeneric(new[] { 1 }, Reason.JobInstall, null);
            var second = new RuleGeneric(new[] { 2 }, Reason.JobInstall, null);
            var third  = new RuleGeneric(Array.Empty <int>(), Reason.InternalAllowUpdate, null);

            var ruleSet = new RuleSet();

            ruleSet.Add(first, RuleType.Job);
            ruleSet.Add(third, RuleType.Learned);
            ruleSet.Add(second, RuleType.Job);

            Assert.AreEqual(3, ruleSet.Count);
            CollectionAssert.AreEqual(new[] { first, second }, ruleSet.GetEnumeratorFor(RuleType.Job).ToArray());
            CollectionAssert.AreEqual(new[] { third }, ruleSet.GetEnumeratorFor(RuleType.Learned).ToArray());
        }
Esempio n. 17
0
        private (int LearnLiteral, int NewLevel, Rule NewRule, int Why) Analyze(int level, Rule rule)
        {
            var analyzedRule    = rule;
            var ruleLevel       = 1;
            var num             = 0;
            var level1Num       = 0;
            var seen            = new HashSet <int>();
            var learnedLiterals = new List <int>()
            {
                0
            };

            var decisionId       = decisions.Count;
            var learnedPoolRules = new LinkedList <Rule>();

            learnedPool.Add(learnedPoolRules);

            while (true)
            {
                learnedPoolRules.AddLast(rule);

                foreach (var literal in rule.GetLiterals())
                {
                    // skip the one true literal
                    if (decisions.IsSatisfy(literal) || seen.Contains(Math.Abs(literal)))
                    {
                        continue;
                    }

                    seen.Add(Math.Abs(literal));

                    var decisionLevel = decisions.GetDecisionLevel(literal);
                    if (decisionLevel == 1)
                    {
                        level1Num++;
                    }
                    else if (level == decisionLevel)
                    {
                        num++;
                    }
                    else
                    {
                        // not level1 or conflict level, add to new rule
                        learnedLiterals.Add(literal);
                        if (decisionLevel > ruleLevel)
                        {
                            ruleLevel = decisionLevel;
                        }
                    }
                }

                var level1Retry = true;
                while (level1Retry)
                {
                    level1Retry = false;
                    if (num == 0 && (--level1Num) == 0)
                    {
                        // all level 1 literals done.
                        goto end_cycle;
                    }

                    int literal;
                    while (true)
                    {
                        if (decisionId <= 0)
                        {
                            throw new SolverBugException(
                                      $"Reached invalid decision id {decisionId} while looking through {rule} for a literal present in the analyzed rule {analyzedRule}.");
                        }

                        decisionId--;
                        var decision = decisions.At(decisionId);
                        literal = decision.Literal;
                        if (seen.Contains(Math.Abs(literal)))
                        {
                            break;
                        }
                    }

                    seen.Remove(Math.Abs(literal));
                    if (num > 0 && ((--num) == 0))
                    {
                        if (literal < 0)
                        {
                            TestFlagLearnedPositiveLiteral = true;
                        }

                        learnedLiterals[0] = -literal;
                        if (level1Num == 0)
                        {
                            goto end_cycle;
                        }

                        var first = true;
                        foreach (var learnedLiteral in learnedLiterals)
                        {
                            if (!first)
                            {
                                seen.Remove(Math.Abs(learnedLiteral));
                            }

                            first = false;
                        }

                        // only level 1 marks left
                        level1Num++;
                        level1Retry = true;
                    }
                }

                rule = decisions.At(decisionId).Reason;
            }

end_cycle:
            var why = learnedPool.Count - 1;

            if (learnedLiterals[0] == 0)
            {
                throw new SolverBugException(
                          $"Did not find a learnable literal in analyzed rule {analyzedRule}.");
            }

            var newRule = new RuleGeneric(learnedLiterals.ToArray(), Reason.Learned, why);

            return(learnedLiterals[0], ruleLevel, newRule, why);
        }
Esempio n. 18
0
        public void TestGetLiterals()
        {
            var rule = new RuleGeneric(new[] { 1, -2 }, Reason.JobInstall, null);

            CollectionAssert.AreEqual(new[] { -2, 1 }, rule.GetLiterals());
        }
Esempio n. 19
0
        public void TestGetHash()
        {
            var rule = new RuleGeneric(new[] { 123 }, Reason.JobInstall, null);

            Assert.AreEqual("123".GetHashCode(StringComparison.Ordinal), rule.GetHashCode());
        }