示例#1
0
 public List <string> Calculate(Dominance allele1, Dominance allele2)
 {
     if (allele1 != allele2)
     {
         return new List <string>()
                {
                    "CC", "Cc", "CC", "cc", "Cc", "cc"
                }
     }
     ;
     else if (allele1 == Dominance.Dominant && allele2 == Dominance.Dominant)
     {
         return new List <string>()
                {
                    "CC", "CC", "CC", "CC", "CC", "CC"
                }
     }
     ;
     else if (allele1 == Dominance.Recessive && allele2 == Dominance.Recessive)
     {
         return new List <string>()
                {
                    "cc", "cc", "cc", "cc", "cc", "cc"
                }
     }
     ;
     else
     {
         return(new List <string>());
     }
 }
示例#2
0
 public SpeedAllele(Dominance pDominance, float pWalking, float pRunning, float pAngular, float pAcceleration) : base(pDominance)
 {
     _walking      = pWalking;
     _running      = pRunning;
     _angular      = pAngular;
     _acceleration = pAcceleration;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="BaseMethodCompiler" /> class.
        /// </summary>
        /// <param name="compiler">The assembly compiler.</param>
        /// <param name="method">The method to compile by this instance.</param>
        /// <param name="basicBlocks">The basic blocks.</param>
        /// <param name="threadID">The thread identifier.</param>
        protected BaseMethodCompiler(BaseCompiler compiler, MosaMethod method, BasicBlocks basicBlocks, int threadID)
        {
            Compiler          = compiler;
            Method            = method;
            Type              = method.DeclaringType;
            Scheduler         = compiler.CompilationScheduler;
            Architecture      = compiler.Architecture;
            TypeSystem        = compiler.TypeSystem;
            TypeLayout        = compiler.TypeLayout;
            Trace             = compiler.CompilerTrace;
            Linker            = compiler.Linker;
            BasicBlocks       = basicBlocks ?? new BasicBlocks();
            Pipeline          = new CompilerPipeline();
            StackLayout       = new StackLayout(Architecture, method.Signature.Parameters.Count + (method.HasThis || method.HasExplicitThis ? 1 : 0));
            VirtualRegisters  = new VirtualRegisters(Architecture);
            LocalVariables    = emptyOperandList;
            ThreadID          = threadID;
            DominanceAnalysis = new Dominance(Compiler.CompilerOptions.DominanceAnalysisFactory, BasicBlocks);
            PluggedMethod     = compiler.PlugSystem.GetPlugMethod(Method);
            stop              = false;

            MethodData = compiler.CompilerData.GetCompilerMethodData(Method);
            MethodData.Counters.Clear();

            EvaluateParameterOperands();
        }
        public static Trait CreateNewTrait(string TraitName, string AlleleName, Dominance InheritanceType, GeneticCounsellorDbContext context)
        {
            var t = new Trait();

            t.TraitName       = TraitName;
            t.AlleleName      = AlleleName;
            t.InheritanceType = InheritanceType;
            context.Traits.Add(t);
            return(t);
        }
        public static Genotype CreateNewGenotype(string AlleleName, Dominance Allele1, Dominance Allele2, GeneticCounsellorDbContext context)
        {
            var g = new Genotype(AlleleName, Allele1, Allele2);

            g.AlleleName = AlleleName;
            g.Allele1    = Allele1;
            g.Allele2    = Allele2;

            context.Genotypes.Add(g);
            return(g);
        }
示例#6
0
 private string AlleleAsString(Dominance allele)
 {
     if (allele == Dominance.Dominant)
     {
         return(AlleleName.ToString());
     }
     else
     {
         return(AlleleName.ToString().ToLower());
     }
 }
示例#7
0
        public Trait AddTrait(string traitName, string alleleName, Dominance inheritanceType)
        {
            var t = new Trait();

            t.TraitName       = traitName;
            t.AlleleName      = alleleName.ToUpper();
            t.InheritanceType = inheritanceType;
            Db.Traits.Add(t);
            Db.SaveChanges();
            return(t);
        }
示例#8
0
 /// <summary>
 /// Finds a suitable single exit point for the specified loop.
 /// </summary>
 /// <remarks>This method must not write to the Visited flags on the CFG.</remarks>
 ControlFlowNode FindExitPoint(ControlFlowNode loopHead, IReadOnlyList <ControlFlowNode> naturalLoop)
 {
     if (!context.ControlFlowGraph.HasReachableExit(loopHead))
     {
         // Case 1:
         // There are no nodes n so that loopHead dominates a predecessor of n but not n itself
         // -> we could build a loop with zero exit points.
         ControlFlowNode exitPoint         = null;
         int             exitPointILOffset = -1;
         foreach (var node in loopHead.DominatorTreeChildren)
         {
             PickExitPoint(node, ref exitPoint, ref exitPointILOffset);
         }
         return(exitPoint);
     }
     else
     {
         // Case 2:
         // We need to pick our exit point so that all paths from the loop head
         // to the reachable exits run through that exit point.
         var cfg    = context.ControlFlowGraph.cfg;
         var revCfg = PrepareReverseCFG(loopHead);
         //ControlFlowNode.ExportGraph(cfg).Show("cfg");
         //ControlFlowNode.ExportGraph(revCfg).Show("rev");
         ControlFlowNode commonAncestor = revCfg[loopHead.UserIndex];
         Debug.Assert(commonAncestor.IsReachable);
         foreach (ControlFlowNode cfgNode in naturalLoop)
         {
             ControlFlowNode revNode = revCfg[cfgNode.UserIndex];
             if (revNode.IsReachable)
             {
                 commonAncestor = Dominance.FindCommonDominator(commonAncestor, revNode);
             }
         }
         ControlFlowNode exitPoint;
         while (commonAncestor.UserIndex >= 0)
         {
             exitPoint = cfg[commonAncestor.UserIndex];
             Debug.Assert(exitPoint.Visited == naturalLoop.Contains(exitPoint));
             if (exitPoint.Visited)
             {
                 commonAncestor = commonAncestor.ImmediateDominator;
                 continue;
             }
             else
             {
                 return(exitPoint);
             }
         }
         // least common dominator is the artificial exit node
         return(null);
     }
 }
示例#9
0
        protected BinaryGene Breed(BinaryGene parentA, BinaryGene parentB)
        {
            Alele aDominance = parentA.Alele;
            Alele bDominance = parentB.Alele;

            int aRand = new Random().Next(2);
            int bRand = new Random().Next(2);

            Dominance aDom = (aRand == 0) ? aDominance.Pair.Key : aDominance.Pair.Value;
            Dominance bDom = (bRand == 0) ? bDominance.Pair.Key : bDominance.Pair.Value;

            Alele      childAlele = new Alele(aDom, bDom);
            BinaryGene childGene  = new BinaryGene();

            childGene.Alele = childAlele;
            return(childGene);
        }
示例#10
0
        /// <summary>
        /// Constructs a control flow graph for the blocks in the given block container.
        ///
        /// Return statements, exceptions, or branches leaving the block container are not
        /// modeled by the control flow graph.
        /// </summary>
        public ControlFlowGraph(BlockContainer container, CancellationToken cancellationToken = default(CancellationToken))
        {
            this.container = container;
            this.cfg       = new ControlFlowNode[container.Blocks.Count];
            this.nodeHasDirectExitOutOfContainer = new BitSet(cfg.Length);
            for (int i = 0; i < cfg.Length; i++)
            {
                Block block = container.Blocks[i];
                cfg[i] = new ControlFlowNode {
                    UserIndex = i, UserData = block
                };
                dict.Add(block, cfg[i]);
            }

            CreateEdges(cancellationToken);
            Dominance.ComputeDominance(cfg[0], cancellationToken);
            this.nodeHasReachableExit = Dominance.MarkNodesWithReachableExits(cfg);
            this.nodeHasReachableExit.UnionWith(FindNodesWithExitsOutOfContainer());
        }
        public Genotype AddGenotype(string alleleName, Dominance allele1, Dominance allele2)
        {
            var g = new Genotype();

            g.AlleleName = alleleName;
            if (allele1 == Dominance.Recessive && allele2 == Dominance.Dominant)
            {
                g.Allele1 = allele2;
                g.Allele2 = allele1;
            }
            else
            {
                g.Allele1 = allele1;
                g.Allele2 = allele2;
            }

            Db.Genotypes.Add(g);
            Db.SaveChanges();
            return(g);
        }
示例#12
0
        ControlFlowNode[] PrepareReverseCFG(ControlFlowNode loopHead, bool treatBackEdgesAsExits)
        {
            ControlFlowNode[] cfg = context.ControlFlowGraph.cfg;
            ControlFlowNode[] rev = new ControlFlowNode[cfg.Length + 1];
            for (int i = 0; i < cfg.Length; i++)
            {
                rev[i] = new ControlFlowNode {
                    UserIndex = i, UserData = cfg[i].UserData
                };
            }
            ControlFlowNode exitNode = new ControlFlowNode {
                UserIndex = -1
            };

            rev[cfg.Length] = exitNode;
            for (int i = 0; i < cfg.Length; i++)
            {
                if (!loopHead.Dominates(cfg[i]))
                {
                    continue;
                }
                // Add reverse edges for all edges in cfg
                foreach (var succ in cfg[i].Successors)
                {
                    if (loopHead.Dominates(succ) && (!treatBackEdgesAsExits || loopHead != succ))
                    {
                        rev[succ.UserIndex].AddEdgeTo(rev[i]);
                    }
                    else
                    {
                        exitNode.AddEdgeTo(rev[i]);
                    }
                }
                if (context.ControlFlowGraph.HasDirectExitOutOfContainer(cfg[i]))
                {
                    exitNode.AddEdgeTo(rev[i]);
                }
            }
            Dominance.ComputeDominance(exitNode, context.CancellationToken);
            return(rev);
        }
示例#13
0
        /// <summary>
        /// Once dominance information is computed, compute the immediate (closest) dominator using BFS
        /// </summary>
        public void ComputeImmediateDominator()
        {
            // Use BFS to encounter the closest dominating node guaranteed
            Queue <BasicBlock> queue = new Queue <BasicBlock>(Predecessors);

            while (queue.Count != 0)
            {
                var b = queue.Dequeue();
                if (Dominance.Contains(b))
                {
                    ImmediateDominator = b;
                    if (b != this)
                    {
                        ImmediateDominator.DominanceTreeSuccessors.Add(this);
                    }
                    break;
                }
                foreach (var p in b.Predecessors)
                {
                    queue.Enqueue(p);
                }
            }
        }
示例#14
0
 private static void CreateTraitScreen(TraitRepository traitRepository)
 {
     try
     {
         Console.Clear();
         Console.WriteLine("Name of trait:");                                             // Colourblindness
         string inputName = Console.ReadLine();
         Console.WriteLine("Type of inheritance (currently Dominant or Recessive only)"); //Reccessive
         Dominance inputInheritanceType = (Dominance)Enum.Parse(typeof(Dominance), Console.ReadLine(), true);
         Console.WriteLine("What letter should represent it?");                           // C
         string inputAlelleName = Console.ReadLine();
         traitRepository.AddTrait(inputName, inputAlelleName, inputInheritanceType);
         Console.Clear();
         StringBuilder sb2 = new StringBuilder();
         ListAllTraitsScreen(sb2, traitRepository);
         string s2 = sb2.ToString();
         Console.Write(s2);
     }
     catch (Exception)
     {
         Console.WriteLine("Input not recognised- Please try again");
         CreateTraitScreen(traitRepository);
     }
 }
示例#15
0
        /// <summary>
        /// Finds a suitable single exit point for the specified loop.
        /// </summary>
        /// <returns>
        /// 1) If a suitable exit point was found: the control flow block that should be reached when breaking from the loop
        /// 2) If the loop should not have any exit point (extend by all dominated blocks): NoExitPoint
        /// 3) otherwise (exit point unknown, heuristically extend loop): null
        /// </returns>
        /// <remarks>This method must not write to the Visited flags on the CFG.</remarks>
        ControlFlowNode FindExitPoint(ControlFlowNode loopHead, IReadOnlyList <ControlFlowNode> naturalLoop, bool treatBackEdgesAsExits)
        {
            bool hasReachableExit = context.ControlFlowGraph.HasReachableExit(loopHead);

            if (!hasReachableExit && treatBackEdgesAsExits)
            {
                // If we're analyzing the switch, there's no reachable exit, but the loopHead (=switchHead) block
                // is also a loop head, we consider the back-edge a reachable exit for the switch.
                hasReachableExit = loopHead.Predecessors.Any(p => loopHead.Dominates(p));
            }
            if (!hasReachableExit)
            {
                // Case 1:
                // There are no nodes n so that loopHead dominates a predecessor of n but not n itself
                // -> we could build a loop with zero exit points.
                if (IsPossibleForeachLoop((Block)loopHead.UserData, out var exitBranch))
                {
                    if (exitBranch != null)
                    {
                        // let's see if the target of the exit branch is a suitable exit point
                        var cfgNode = loopHead.Successors.FirstOrDefault(n => n.UserData == exitBranch.TargetBlock);
                        if (cfgNode != null && loopHead.Dominates(cfgNode) && !context.ControlFlowGraph.HasReachableExit(cfgNode))
                        {
                            return(cfgNode);
                        }
                    }
                    return(NoExitPoint);
                }
                ControlFlowNode exitPoint         = null;
                int             exitPointILOffset = -1;
                foreach (var node in loopHead.DominatorTreeChildren)
                {
                    PickExitPoint(node, ref exitPoint, ref exitPointILOffset);
                }
                return(exitPoint);
            }
            else
            {
                // Case 2:
                // We need to pick our exit point so that all paths from the loop head
                // to the reachable exits run through that exit point.
                var cfg    = context.ControlFlowGraph.cfg;
                var revCfg = PrepareReverseCFG(loopHead, treatBackEdgesAsExits);
                //ControlFlowNode.ExportGraph(cfg).Show("cfg");
                //ControlFlowNode.ExportGraph(revCfg).Show("rev");
                ControlFlowNode commonAncestor = revCfg[loopHead.UserIndex];
                Debug.Assert(commonAncestor.IsReachable);
                foreach (ControlFlowNode cfgNode in naturalLoop)
                {
                    ControlFlowNode revNode = revCfg[cfgNode.UserIndex];
                    if (revNode.IsReachable)
                    {
                        commonAncestor = Dominance.FindCommonDominator(commonAncestor, revNode);
                    }
                }
                // All paths from within the loop to a reachable exit run through 'commonAncestor'.
                // However, this doesn't mean that 'commonAncestor' is valid as an exit point.
                // We walk up the post-dominator tree until we've got a valid exit point:
                ControlFlowNode exitPoint;
                while (commonAncestor.UserIndex >= 0)
                {
                    exitPoint = cfg[commonAncestor.UserIndex];
                    Debug.Assert(exitPoint.Visited == naturalLoop.Contains(exitPoint));
                    // It's possible that 'commonAncestor' is itself part of the natural loop.
                    // If so, it's not a valid exit point.
                    if (!exitPoint.Visited && ValidateExitPoint(loopHead, exitPoint))
                    {
                        // we found an exit point
                        return(exitPoint);
                    }
                    commonAncestor = commonAncestor.ImmediateDominator;
                }
                // least common post-dominator is the artificial exit node
                return(null);
            }
        }
示例#16
0
        /// <summary>
        /// Finds a suitable single exit point for the specified loop.
        /// </summary>
        /// <returns>
        /// 1) If a suitable exit point was found: the control flow block that should be reached when breaking from the loop
        /// 2) If the loop should not have any exit point (extend by all dominated blocks): NoExitPoint
        /// 3) otherwise (exit point unknown, heuristically extend loop): null
        /// </returns>
        /// <remarks>This method must not write to the Visited flags on the CFG.</remarks>
        internal ControlFlowNode FindExitPoint(ControlFlowNode loopHead, IReadOnlyList <ControlFlowNode> naturalLoop)
        {
            bool hasReachableExit = HasReachableExit(loopHead);

            if (!hasReachableExit)
            {
                // Case 1:
                // There are no nodes n so that loopHead dominates a predecessor of n but not n itself
                // -> we could build a loop with zero exit points.
                if (IsPossibleForeachLoop((Block)loopHead.UserData, out var exitBranch))
                {
                    if (exitBranch != null)
                    {
                        // let's see if the target of the exit branch is a suitable exit point
                        var cfgNode = loopHead.Successors.FirstOrDefault(n => n.UserData == exitBranch.TargetBlock);
                        if (cfgNode != null && loopHead.Dominates(cfgNode) && !context.ControlFlowGraph.HasReachableExit(cfgNode))
                        {
                            return(cfgNode);
                        }
                    }
                    return(NoExitPoint);
                }
                ControlFlowNode exitPoint         = null;
                int             exitPointILOffset = -1;
                ConsiderReturnAsExitPoint((Block)loopHead.UserData, ref exitPoint, ref exitPointILOffset);
                foreach (var node in loopHead.DominatorTreeChildren)
                {
                    PickExitPoint(node, ref exitPoint, ref exitPointILOffset);
                }
                return(exitPoint);
            }
            else
            {
                // Case 2:
                // We need to pick our exit point so that all paths from the loop head
                // to the reachable exits run through that exit point.
                var cfg    = context.ControlFlowGraph.cfg;
                var revCfg = PrepareReverseCFG(loopHead, out int exitNodeArity);
                //ControlFlowNode.ExportGraph(cfg).Show("cfg");
                //ControlFlowNode.ExportGraph(revCfg).Show("rev");
                ControlFlowNode commonAncestor = revCfg[loopHead.UserIndex];
                Debug.Assert(commonAncestor.IsReachable);
                foreach (ControlFlowNode cfgNode in naturalLoop)
                {
                    ControlFlowNode revNode = revCfg[cfgNode.UserIndex];
                    if (revNode.IsReachable)
                    {
                        commonAncestor = Dominance.FindCommonDominator(commonAncestor, revNode);
                    }
                }
                // All paths from within the loop to a reachable exit run through 'commonAncestor'.
                // However, this doesn't mean that 'commonAncestor' is valid as an exit point.
                // We walk up the post-dominator tree until we've got a valid exit point:
                ControlFlowNode exitPoint;
                while (commonAncestor.UserIndex >= 0)
                {
                    exitPoint = cfg[commonAncestor.UserIndex];
                    Debug.Assert(exitPoint.Visited == naturalLoop.Contains(exitPoint));
                    // It's possible that 'commonAncestor' is itself part of the natural loop.
                    // If so, it's not a valid exit point.
                    if (!exitPoint.Visited && ValidateExitPoint(loopHead, exitPoint))
                    {
                        // we found an exit point
                        return(exitPoint);
                    }
                    commonAncestor = commonAncestor.ImmediateDominator;
                }
                // least common post-dominator is the artificial exit node
                // This means we're in one of two cases:
                // * The loop might have multiple exit points.
                //     -> we should return null
                // * The loop has a single exit point that wasn't considered during post-dominance analysis.
                //        (which means the single exit isn't dominated by the loop head)
                //     -> we should return NoExitPoint so that all code dominated by the loop head is included into the loop
                if (exitNodeArity > 1)
                {
                    return(null);
                }

                // Exit node is on the very edge of the tree, and isn't important for determining inclusion
                // Still necessary for switch detection to insert correct leave statements
                if (exitNodeArity == 1 && isSwitch)
                {
                    return(loopContext.GetBreakTargets(loopHead).Distinct().Single());
                }

                // If exitNodeArity == 0, we should maybe look test if our exits out of the block container are all compatible?
                // but I don't think it hurts to have a bit too much code inside the loop in this rare case.
                return(NoExitPoint);
            }
        }
示例#17
0
        /// <summary>
        /// Constructs a new control flow graph.
        /// Each node cfg[i] has a corresponding node rev[i].
        /// Edges are only created for nodes dominated by loopHead, and are in reverse from their direction
        /// in the primary CFG.
        /// An artificial exit node is used for edges that leave the set of nodes dominated by loopHead,
        /// or that leave the block Container.
        /// </summary>
        /// <param name="loopHead">Entry point of the loop.</param>
        /// <param name="exitNodeArity">out: The number of different CFG nodes.
        /// Possible values:
        ///  0 = no CFG nodes used as exit nodes (although edges leaving the block container might still be exits);
        ///  1 = a single CFG node (not dominated by loopHead) was used as an exit node;
        ///  2 = more than one CFG node (not dominated by loopHead) was used as an exit node.
        /// </param>
        /// <returns></returns>
        ControlFlowNode[] PrepareReverseCFG(ControlFlowNode loopHead, out int exitNodeArity)
        {
            ControlFlowNode[] cfg = context.ControlFlowGraph.cfg;
            ControlFlowNode[] rev = new ControlFlowNode[cfg.Length + 1];
            for (int i = 0; i < cfg.Length; i++)
            {
                rev[i] = new ControlFlowNode {
                    UserIndex = i, UserData = cfg[i].UserData
                };
            }
            ControlFlowNode nodeTreatedAsExitNode           = null;
            bool            multipleNodesTreatedAsExitNodes = false;
            ControlFlowNode exitNode = new ControlFlowNode {
                UserIndex = -1
            };

            rev[cfg.Length] = exitNode;
            for (int i = 0; i < cfg.Length; i++)
            {
                if (!loopHead.Dominates(cfg[i]) || isSwitch && cfg[i] != loopHead && loopContext.MatchContinue(cfg[i]))
                {
                    continue;
                }

                // Add reverse edges for all edges in cfg
                foreach (var succ in cfg[i].Successors)
                {
                    // edges to outer loops still count as exits (labelled continue not implemented)
                    if (isSwitch && loopContext.MatchContinue(succ, 1))
                    {
                        continue;
                    }

                    if (loopHead.Dominates(succ))
                    {
                        rev[succ.UserIndex].AddEdgeTo(rev[i]);
                    }
                    else
                    {
                        if (nodeTreatedAsExitNode == null)
                        {
                            nodeTreatedAsExitNode = succ;
                        }
                        if (nodeTreatedAsExitNode != succ)
                        {
                            multipleNodesTreatedAsExitNodes = true;
                        }
                        exitNode.AddEdgeTo(rev[i]);
                    }
                }
                if (context.ControlFlowGraph.HasDirectExitOutOfContainer(cfg[i]))
                {
                    exitNode.AddEdgeTo(rev[i]);
                }
            }
            if (multipleNodesTreatedAsExitNodes)
            {
                exitNodeArity = 2;                 // more than 1
            }
            else if (nodeTreatedAsExitNode != null)
            {
                exitNodeArity = 1;
            }
            else
            {
                exitNodeArity = 0;
            }
            Dominance.ComputeDominance(exitNode, context.CancellationToken);
            return(rev);
        }
示例#18
0
 public Trait(string TraitName, string AlleleName, Dominance InheritanceType)
 {
     this.TraitName       = TraitName;
     this.AlleleName      = AlleleName.ToUpper();
     this.InheritanceType = InheritanceType;
 }
示例#19
0
 public HeightAllele(Dominance pDominance, float pHeight) : base(pDominance)
 {
     _height = pHeight;
 }
示例#20
0
 public ColorAllele(Dominance pDominance, Color pColor) : base(pDominance)
 {
     _color = pColor;
 }
 public BehaviorAllele(Dominance pDominance, float pPeerUtility, float pOpponentUtility, float pSocializingChance) : base(pDominance)
 {
     _peerUtility       = pPeerUtility;
     _opponentUtility   = pOpponentUtility;
     _socializingChance = pSocializingChance;
 }
示例#22
0
 public Genotype(string AlleleName, Dominance Allele1, Dominance Allele2)
 {
     this.AlleleName = AlleleName;
     this.Allele1    = Allele1;
     this.Allele2    = Allele2;
 }
示例#23
0
 public Alele(Dominance a, Dominance b)
 {
     Pair = new KeyValuePair <Dominance, Dominance>(a, b);
 }
示例#24
0
 /// <summary>
 /// Constructor with dominance parameter
 /// </summary>
 /// <param name="pDominance"></param>
 public Allele(Dominance pDominance)
 {
     dominance = pDominance;
 }