コード例 #1
0
ファイル: ModelView.cs プロジェクト: woooodbond/infer
        public static void Add(GraphWriter g, ModelBuilder modelBuilder, bool useContainers)
        {
            GraphBuilder graphBuilder = new GraphBuilder();

            graphBuilder.UseContainers = useContainers;
            graphBuilder.Add(g, modelBuilder);
        }
コード例 #2
0
        protected Edge AddEdge(GraphWriter g, IModelExpression from, IModelExpression to, string name)
        {
            Node sourceNode = GetNode(g, from);
            Node targetNode = GetNode(g, to);

            childNodes.Add(targetNode);
            return(AddEdge(g, sourceNode, targetNode, name));
        }
コード例 #3
0
        protected void AddFactorEdges(GraphWriter g, MethodInvoke mi)
        {
            var parameters = mi.method.GetParameters();

            for (int i = 0; i < mi.args.Count; i++)
            {
                var parameter = parameters[i];
                if (parameter.IsOut)
                {
                    AddEdge(g, mi, mi.args[i], parameter.Name);
                }
                else
                {
                    AddEdge(g, mi.args[i], mi, parameter.Name);
                }
            }
            if (mi.returnValue != null)
            {
                AddEdge(g, mi, mi.returnValue, "");
            }
            if (!UseContainers)
            {
                // add edges from condition variables to target (if there are no such edges already)
                IModelExpression      target   = (mi.returnValue != null) ? mi.returnValue : mi;
                Set <IStatementBlock> excluded = new Set <IStatementBlock>();
                if (target is Variable)
                {
                    // if target is in the ConditionBlock, then don't connect with the condition variable
                    Variable targetVar = (Variable)target;
                    excluded.AddRange(targetVar.Containers);
                }
                foreach (IStatementBlock block in mi.Containers)
                {
                    if (excluded.Contains(block))
                    {
                        continue;
                    }
                    if (block is ConditionBlock)
                    {
                        ConditionBlock  cb = (ConditionBlock)block;
                        Variable        c  = cb.ConditionVariableUntyped;
                        List <Variable> condVars;
                        if (!conditionVariables.TryGetValue(target, out condVars))
                        {
                            condVars = new List <Variable>();
                            conditionVariables[target] = condVars;
                        }
                        if (!condVars.Contains(c))
                        {
                            AddEdge(g, c, target, "condition");
                            condVars.Add(c);
                        }
                    }
                }
            }
        }
コード例 #4
0
        protected Edge AddEdge(GraphWriter g, Node sourceNode, Node targetNode, string name)
        {
            string source = sourceNode.ID;
            string target = targetNode.ID;
            Edge   edge   = g.AddEdge(source, target);

            edge.Label     = name;
            edge.FontSize  = 8;
            edge.FontColor = Color.LightGray;
            return(edge);
        }
コード例 #5
0
        /// <summary>
        /// Write the graph to a file in DOT format
        /// </summary>
        /// <param name="path"></param>
        public void WriteDot(string path)
        {
            // References:
            // https://en.wikipedia.org/wiki/DOT_(graph_description_language)

            var builder = new GraphBuilder();

            builder.UseContainers = true;
            GraphWriter g = builder.ToGraph(modelBuilder);

            g.Write(path);
        }
コード例 #6
0
ファイル: ModelView.cs プロジェクト: woooodbond/infer
        private void Add(GraphWriter g, ModelBuilder mb)
        {
            Count = 0;
            if (mb == null)
            {
                return;
            }
            foreach (IModelExpression me in mb.ModelExpressions)
            {
                if (me is MethodInvoke)
                {
                    AddFactorEdges(g, (MethodInvoke)me);
                }
            }
            // connect nodes that represent the same variable with undirected edges
            Dictionary <Variable, List <Node> > nodesOfVariable = new Dictionary <Variable, List <Node> >();

            foreach (KeyValuePair <IModelExpression, Node> entry in nodeOfExpr)
            {
                if (!(entry.Key is Variable))
                {
                    continue;
                }
                Variable v = (Variable)entry.Key;
                v = GetBaseVariable(v);
                List <Node> nodes;
                if (!nodesOfVariable.TryGetValue(v, out nodes))
                {
                    nodes = new List <Node>();
                    nodesOfVariable[v] = nodes;
                }
                nodes.Add(entry.Value);
            }
            foreach (List <Node> nodes in nodesOfVariable.Values)
            {
                for (int i = 0; i < nodes.Count; i++)
                {
                    for (int j = 0; j < nodes.Count; j++)
                    {
                        if (i != j && childNodes.Contains(nodes[i]))
                        {
                            // Graph layout tools use the edge direction as a layout hint, so only use child nodes as sources
                            Edge edge = g.AddEdge(nodes[i].ID, nodes[j].ID);
                            edge.ArrowheadAtTarget = ArrowheadStyle.None;
                        }
                    }
                }
            }
        }
コード例 #7
0
        //CodeTransformer ct = new CodeTransformer();
        protected Node AddNode(GraphWriter g, IStatement ist, Stage stage)
        {
            if (ist is ICommentStatement)
            {
                return(null);
            }
            Node nd = GetNodeForStatement(ist, stage);

            if (nd != null)
            {
                nd.Label = Count + "," + nd.Label;
                Count++;
                return(nd);
            }
            DependencyInformation di = context.InputAttributes.Get <DependencyInformation>(ist);

            if ((di != null) && (di.IsOutput))
            {
                return(null);
            }
            string s = Count + ". " + StatementLabel(ist);

            nd          = g.AddNode("node" + Count);
            nd.UserData = Count++;
            SetNodeForStatement(ist, stage, nd);
            //if (di.IsOutput) nd.Fillcolor = Color.LightBlue;
            nd.Label = s;
            if (stage == Stage.Initialisation)
            {
                nd.FillColor = Color.LightGray;
            }
            if (ist is IExpressionStatement)
            {
                IExpressionStatement ies = (IExpressionStatement)ist;
                IAssignExpression    iae = ies.Expression as IAssignExpression;
                if ((iae != null) && (iae.Target is IVariableDeclarationExpression))
                {
                    nd.BorderWidth = 2;
                }
            }
            nd.Shape    = ShapeStyle.Box;
            nd.FontSize = 9;
            return(nd);
        }
コード例 #8
0
        protected void AddEdges(GraphWriter g, IStatement ist, Stage stage)
        {
            Node nd = GetNodeForStatement(ist, stage);

            if (nd == null)
            {
                return;
            }
            DependencyInformation di = context.InputAttributes.Get <DependencyInformation>(ist);

            if (di == null)
            {
                return;
            }
            AddDependencyEdges(g, nd, di, Stage.Initialisation, stage);
            if (stage != Stage.Initialisation)
            {
                AddDependencyEdges(g, nd, di, Stage.Update, stage);
            }
        }
コード例 #9
0
 protected void AddDependencyEdges(GraphWriter g, Node nd, DependencyInformation di, Stage stage, Stage parentStage)
 {
     foreach (IStatement source in di.GetDependenciesOfType(DependencyType.Dependency | DependencyType.Declaration))
     {
         Node nd2 = GetNodeForStatement(source, stage);
         if (nd2 == null)
         {
             continue;
         }
         bool backwards = ((int)nd.UserData) < ((int)nd2.UserData);
         //Console.WriteLine("stage=" + stage + " " + nd.UserData + " " + nd2.UserData);
         Edge e;
         if (backwards)
         {
             e         = g.AddEdge(nd.ID, nd2.ID);
             e.Color   = Color.Red;
             e.Reverse = true;
         }
         else
         {
             e = g.AddEdge(nd2.ID, nd.ID);
             if (parentStage != stage)
             {
                 e.Color = Color.LightGray;
             }
         }
         if (di.HasDependency(DependencyType.Trigger, source))
         {
             e.Width = 2;
         }
         if (di.HasDependency(DependencyType.Fresh, source))
         {
             e.Style = EdgeStyle.Dashed;
         }
         if (di.HasDependency(DependencyType.Cancels, source))
         {
             e.Color = Color.LightGray;
         }
     }
     //e.Label = StatementLabel(ist);            e.Label.FontSize = 8;
 }
コード例 #10
0
        protected Node GetNode(GraphWriter g, ConditionContext context)
        {
            Node node;

            if (!nodeOfContext.TryGetValue(context, out node))
            {
                node = g.AddNode("node" + (Count++));
                nodeOfContext[context] = node;
                node.Label             = context.GetLabel();
                node.UserData          = this.GroupInstance;
                ConditionContext parent = context.GetParentContext();
                if (parent != null)
                {
                    var parentNode = GetNode(g, parent);
                    AddGroupEdge(g, parentNode, node);
                }
                var  variable      = context.GetConditionVariable();
                Node conditionNode = GetNode(g, variable);
                g.AddEdge(conditionNode.ID, node.ID);
            }
            return(node);
        }
コード例 #11
0
        protected void OnModelChange()
        {
            var builder = new GraphBuilder();

            graph = builder.ToGraph(modelBuilder);
        }
コード例 #12
0
        protected void AddGroupEdge(GraphWriter g, Node parent, Node child)
        {
            Edge edge = g.AddEdge(parent.ID, child.ID);

            edge.UserData = this.GroupInstance;
        }
コード例 #13
0
        protected Node GetNode(GraphWriter g, IModelExpression expr)
        {
            if (nodeOfExpr.ContainsKey(expr))
            {
                return(nodeOfExpr[expr]);
            }
            Node nd = g.AddNode("node" + (Count++));

            nodeOfExpr[expr] = nd;
            nd.Label         = expr.ToString();
            nd.FontSize      = 9;
            if (expr is Variable)
            {
                Variable ve = (Variable)expr;
                if (ve.IsObserved)
                {
                    nd.Shape = ShapeStyle.None;

                    if (ve.IsBase)
                    {
                        // if the observed value is a ValueType, display it directly rather than the variable name
                        object value = ((HasObservedValue)ve).ObservedValue;
                        if (ReferenceEquals(value, null))
                        {
                            nd.Label = "null";
                        }
                        else if (value.GetType().IsValueType)
                        {
                            nd.Label = value.ToString();
                        }
                    }
                }
                if (!ve.IsReadOnly)
                {
                    nd.FontSize  = 10;
                    nd.FontColor = Color.Blue;
                }
                if (UseContainers && ve.Containers.Count > 0)
                {
                    var context = ConditionContext.GetContext(ve.Containers);
                    if (context != null)
                    {
                        var contextNode = GetNode(g, context);
                        AddGroupEdge(g, contextNode, nd);
                    }
                }
            }
            else if (expr is MethodInvoke)
            {
                MethodInvoke mi = (MethodInvoke)expr;
                nd.FillColor = Color.Black;
                nd.FontColor = Color.White;
                nd.Shape     = ShapeStyle.Box;
                nd.FontSize  = 8;
                string methodName = mi.method.Name;
                if (mi.op != null)
                {
                    methodName = mi.op.ToString();
                }
                nd.Label = methodName;
                if (UseContainers && mi.Containers.Count > 0)
                {
                    var context = ConditionContext.GetContext(mi.Containers);
                    if (context != null)
                    {
                        var contextNode = GetNode(g, context);
                        AddGroupEdge(g, contextNode, nd);
                    }
                }
            }
            return(nd);
        }
コード例 #14
0
ファイル: ModelView.cs プロジェクト: woooodbond/infer
 protected void OnModelChange()
 {
     graph = new DotGraphWriter("Model");
     GraphBuilder.Add(graph, modelBuilder, false);
 }
コード例 #15
0
 protected void OnTasksChanged()
 {
     graph = ToGraph(pretasks, looptasks);
 }