Ejemplo n.º 1
0
            public SDSComputation BeginAdvanceTLG(bool intermediateShouldBeConsistent)
            {
                int i = tlgGen = stack.NewestRegisteredSDSGeneration;

                tlgEntry = stack.AllocateGeneration(i + 1);
                Assert.AreEqual(tlgEntry.Generation, i + 1);
                Assert.IsNotNull(stack.FindGeneration(i + 1));
                ctx.SetGeneration(i + 1);
                tlgComp = new SDSComputation(new DateTime(), consensus != null ? messages.GetMessages(i) : ExtMessagePack.CompleteBlank, TimeSpan.FromMilliseconds(10), ctx);
                if (intermediateShouldBeConsistent)
                {
                    AssertNoErrors(tlgComp, i.ToString());
                }
                if (intermediateShouldBeConsistent)
                {
                    Assert.AreEqual(tlgComp.Intermediate.ic.OneCount, 0);
                    Assert.IsTrue(tlgComp.Intermediate.inputConsistent);
                }
                Assert.AreEqual(tlgComp.Generation, i + 1);
                if (consensus != null)
                {
                    consensus[0].TrimOut(stack.NewestConsistentSDSGeneration);
                }
                messages.TrimGenerations(stack.NewestConsistentSDSGeneration);

                return(tlgComp);
            }
Ejemplo n.º 2
0
        public void EntityMotionTest()
        {
            int        numEntities = 100;
            var        ctx         = new SimulationContext(true);
            EntityPool pool        = new EntityPool(EntityPoolTests.CreateEntities(ctx.LocalSpace, numEntities, i => new RandomMotion()), ctx);

            InconsistencyCoverage ic = InconsistencyCoverage.NewCommon();

            for (int i = 0; i < 100; i++)
            {
                var             old = pool.ToArray();
                EntityChangeSet set = new EntityChangeSet();
                ctx.SetGeneration(i);
                var errors = pool.TestEvolve(set, InconsistencyCoverage.NewCommon(), i, TimeSpan.FromSeconds(1));
                Assert.IsNull(errors, errors != null ? errors[0].ToString() : "");
                Assert.AreEqual(numEntities, set.FindNamedSet("motions").Size);
                foreach (var e in old)
                {
                    var m = set.FindMotionOf(e.ID.Guid);
                    Assert.IsNotNull(m);
                    Assert.AreNotEqual(m.TargetLocation, m.Origin.Position, i.ToString());
                    Assert.IsTrue(Simulation.FullSimulationSpace.Contains(m.TargetLocation));
                    Assert.IsTrue(Simulation.MySpace.Contains(m.TargetLocation));
                }

                Assert.AreEqual(0, set.Execute(pool, ic, ctx));
                Assert.AreEqual(numEntities, pool.Count);
                Entity e1;
                foreach (var e in old)
                {
                    Assert.IsTrue(pool.Find(e.ID.Guid, out e1));
                    Assert.AreNotEqual(e.ID.Position, e1.ID.Position);
                }
            }
        }
Ejemplo n.º 3
0
        public void SimpleEntityFaultTest()
        {
            var        ctx  = new SimulationContext(true);
            EntityPool pool = new EntityPool(EntityPoolTests.CreateEntities(ctx.LocalSpace, 100,
                                                                            new RandomLogic(new Type[] { typeof(ConsistentLogic), typeof(FaultLogic) }).Instantiate), ctx);

            InconsistencyCoverage ic = InconsistencyCoverage.NewCommon();
            int any = -1;

            for (int i = 0; i < 8; i++)
            {
                EntityChangeSet set = new EntityChangeSet();
                ctx.SetGeneration(i);
                var errors = pool.TestEvolve(set, ic, i, TimeSpan.FromSeconds(1));
                if (errors != null)
                {
                    Assert.IsTrue(ic.OneCount > 0);
                    if (any == -1)
                    {
                        any = i;
                    }
                }
                ic = ic.Grow(true);
                Assert.IsTrue(ic.Size == InconsistencyCoverage.CommonResolution);
            }
            if (any == 0)
            {
                Assert.AreEqual(ic.OneCount, ic.Size.Product);
            }
        }
Ejemplo n.º 4
0
        public void StateAdvertisementTest()
        {
            var        ctx  = new SimulationContext();
            EntityPool pool = RandomDefaultPool(100, ctx);

            for (int i = 0; i < 100; i++)
            {
                EntityChangeSet set = new EntityChangeSet();
                ctx.SetGeneration(i);
                var errors = pool.TestEvolve(set, InconsistencyCoverage.NewCommon(), i, TimeSpan.FromSeconds(5));
                Assert.IsNull(errors, errors != null ? errors[0].ToString() : "");
                Assert.AreEqual(0, set.Execute(pool, ctx));

                HashSet <Guid> env   = new HashSet <Guid>();
                var            state = pool.ToArray();
                int            it    = 0;
                foreach (var e in state)
                {
                    env.Clear();
                    foreach (var e1 in state)
                    {
                        float dist = ctx.GetDistance(e.ID.Position, e1.ID.Position);
                        if (e1.ID.Guid != e.ID.Guid && dist <= Simulation.SensorRange)
                        {
                            Console.WriteLine(dist + "/" + Simulation.SensorRange);
                            env.Add(e1.ID.Guid);
                        }
                    }
                    Assert.AreEqual(env.Count, e.Contacts.Length, i + "." + it + ": " + e);
                    if (env.Count > 0)
                    {
                        it++;
                    }

                    foreach (var c in e.Contacts)
                    {
                        float dist = ctx.GetDistance(c.ID.Position, e.ID.Position);
                        Assert.IsTrue(dist <= Simulation.SensorRange, dist + " <= " + Simulation.SensorRange);
                        Assert.AreNotEqual(c.ID.Guid, e.ID.Guid);
                        Assert.IsTrue(env.Contains(c.ID.Guid));
                    }

                    var app = e.Appearances.Get <ConsistencyAppearance>();
                    if (i > 0)
                    {
                        Assert.IsNotNull(e.Appearances);
                        Assert.IsNotNull(app);
                    }
                    if (app != null)
                    {
                        Assert.IsTrue(app.IsConsistent);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public void ExtensiveEntityFaultTest()
        {
            int mismatched = 0;

            var ctx = new SimulationContext(true);

            for (int k = 0; k < 20; k++)
            {
                InconsistencyCoverage ic     = InconsistencyCoverage.NewCommon(),
                                      nextIC = ic;

                EntityPool pool = new EntityPool(EntityPoolTests.CreateEntities(ctx.LocalSpace, 100, i => i > 0 ? new ConsistentLogic() : (EntityLogic)(new FaultLogic())), ctx);

                int    faultyCount = 0;
                Entity faulty      = new Entity();
                foreach (var e in pool.ToArray())
                {
                    if (Helper.Deserialize(e.SerialLogicState) is FaultLogic)
                    {
                        faultyCount++;
                        faulty = e;
                    }
                }
                Assert.AreEqual(faultyCount, 1);

                bool doGrow = k % 2 != 0;
                ConsistentLogic.GrowingIC = doGrow;

                for (int i = 0; i < InconsistencyCoverage.CommonResolution; i++)                        //no point going further than current resolution
                {
                    EntityChangeSet set = new EntityChangeSet();
                    ctx.SetGeneration(i);

                    var errors = pool.TestEvolve(set, ic, i, TimeSpan.FromSeconds(1));
                    Assert.AreEqual(1, errors.Count, Helper.Concat(",", errors));
                    Assert.AreEqual(0, set.Execute(pool, ic, ctx));
                    //Assert.AreEqual(ic.OneCount, 1);
                    if (doGrow)
                    {
                        ic = ic.Grow(true);
                    }

                    if (!doGrow)
                    {
                        Assert.AreEqual(ic.OneCount, 1);
                    }


                    Entity[] entities  = pool.ToArray();
                    bool     hasFaulty = false;
                    foreach (var e in entities)
                    {
                        bool isFaultOrigin = (Helper.Deserialize(e.SerialLogicState) is FaultLogic);
                        if (isFaultOrigin)
                        {
                            faulty = e;
                            Assert.IsFalse(hasFaulty);
                            hasFaulty = true;
                        }
                    }
                    Assert.IsTrue(hasFaulty);

                    foreach (var e in entities)
                    {
                        bool isFaultOrigin = (Helper.Deserialize(e.SerialLogicState) is FaultLogic);
#if STATE_ADV
                        if (!isFaultOrigin)
                        {
                            if (ctx.GetDistance(e.ID.Position, faulty.ID.Position) <= Simulation.SensorRange)
                            {
                                Assert.IsTrue(e.HasContact(faulty.ID.Guid));
                                Assert.IsTrue(e.HasContact(faulty.ID));
                            }

                            var adv = set.FindAdvertisementFor(e.ID);
                            Assert.IsNotNull(e.Appearances);
                        }
                        bool consistent = ConsistentLogic.IsConsistent(e.Appearances);
#else
                        ConsistentLogic c          = e.MyLogic as ConsistentLogic;
                        bool            consistent = c != null && c.isConsistent;
#endif
                        bool icIsInc = ic.IsInconsistentR(Simulation.MySpace.Relativate(e.ID.Position));

                        if (!consistent && !icIsInc)
                        {
                            if (doGrow)
                            {
                                Assert.Fail("Inconsistent entity located outside IC area: " + e + ", " + e.MyLogic);
                            }
                            else
                            {
                                mismatched++;
                                break;
                            }
                        }
                    }

                    //Assert.AreEqual(ic.OneCount, 1);
                }
            }
            Assert.AreNotEqual(mismatched, 0);
        }