Locked History Actions

Diff for "guice/Manual/Extensions/Grapher"

Differences between revisions 1 and 2
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
When you've written a sophisticated application, Guice's rich introspection API can describe the object graph in detail. The built-in grapher extension exposes this data as an easily understandable visualization. It can show the bindings and dependencies from several classes in a complex application in a unified diagram.
Generating a .dot file
洗練されたアプリケーションを書いたなら、GuiceのリッチなイントロスペクションAPIを使って、
詳細なオブジェクトグラフを記述することができる。
ビルトインのgrapher拡張がこのデータを簡単にビジュアル化してくれる。
複雑なアプリケーションの中の様々なバインディングや依存を統合されたダイアグラムで表示する。
Line 6: Line 8:
Guice's grapher leans heavily on GraphViz, an open source graph visualization package. It cleanly separates graph specification from visualization and layout. To produce a graph .dot file for an Injector, you can use the following code: == .dotファイルを作成する ==

Guiceのgrapherは、[[http://www.graphviz.org/|GraphViz]]の力を借りている。
オープンソースのグラフビジュアル化パッケージである。
これは、グラフの明細とビジュアル・レイアウトをクリアに分離する。
インジェクタの.dotファイルをグラフ化するには、次のコードを使えばいい。
Line 29: Line 36:
== The .dot file == == .dotファイル ==
Line 31: Line 38:
Executing the code above produces a .dot file that specifies a graph. Each entry in the file represents either a node or an edge in the graph. Here's a sample .dot file: 上のコードを実行すると、グラフ明細を表す.dotファイルが作成される。
ファイルのそれぞれのエントリはグラフ内のノードやエッジを表す。
次に.dotファイルのサンプルを示す。
Line 74: Line 83:
== Rendering the .dot file == == .dotファイルをレンダリングする ==
Line 76: Line 85:
Download a Graphviz viewer for your platform, and use it to render the .dot file. The rendered graph might take a few minutes to render. Exporting the rendered graph as a PDF or image makes it easier to share. 使用するプラットフォーム向けの[[http://www.graphviz.org/|Graphvizのビューア]]をダウンロードし、
.dotファイルをレンダリングしよう。
レンダリングには数分かかるかもしれない。
レンダリングされたグラフをPDFやイメージにエクスポートすることで、共有が簡単になる。
Line 78: Line 90:
On Linux, you can use the command-line dot tool to convert .dot files into images. Linux上では、コマンドラインのdotツールを.dotファイルからイメージに変換するのに使うことができる。
Line 82: Line 94:
The tool currently has issues with our font tags. We're still working that out.
Graph display
このツールは現在のところ、我々のフォントタグの扱いに問題があることがわかっている。
我々はこの問題を解消中である。


== グラフ表示 ==
Line 87: Line 102:
    * Solid edges represent dependencies from implementations to the types they depend on.
    * Dashed edges represent bindings from types to their implementations.
    * Double arrows indicate that the binding or dependency is to a Provider.
 * Solid edges represent dependencies from implementations to the types they depend on.
 * Dashed edges represent bindings from types to their implementations.
 * Double arrows indicate that the binding or dependency is to a Provider.
Line 93: Line 108:
    * Implementation types are given black backgrounds.
    * Implementation instances have gray backgrounds.
 * Implementation types are given black backgrounds.
 * Implementation instances have gray backgrounds.
Line 98: Line 113:
    * If an injection point requests a Provider<Foo>, the grapher will elide the Provider and just show a dependency to Foo.
    * The grapher will use an instance's toString() method to render its title if it has overridden Object's default implementation.
 * If an injection point requests a Provider<Foo>, the grapher will elide the Provider and just show a dependency to Foo.
 * The grapher will use an instance's toString() method to render its title if it has overridden Object's default implementation.

Grapher

洗練されたアプリケーションを書いたなら、GuiceのリッチなイントロスペクションAPIを使って、 詳細なオブジェクトグラフを記述することができる。 ビルトインのgrapher拡張がこのデータを簡単にビジュアル化してくれる。 複雑なアプリケーションの中の様々なバインディングや依存を統合されたダイアグラムで表示する。

.dotファイルを作成する

Guiceのgrapherは、GraphVizの力を借りている。 オープンソースのグラフビジュアル化パッケージである。 これは、グラフの明細とビジュアル・レイアウトをクリアに分離する。 インジェクタの.dotファイルをグラフ化するには、次のコードを使えばいい。

import com.google.inject.Injector;
import com.google.inject.grapher.GrapherModule;
import com.google.inject.grapher.InjectorGrapher;
import com.google.inject.grapher.graphviz.GraphvizModule;
import com.google.inject.grapher.graphviz.GraphvizRenderer;

public class Grapher {
  private void graph(String filename, Injector demoInjector) throws IOException {
    PrintWriter out = new PrintWriter(new File(filename), "UTF-8");

    Injector injector = Guice.createInjector(new GrapherModule(), new GraphvizModule());
    GraphvizRenderer renderer = injector.getInstance(GraphvizRenderer.class);
    renderer.setOut(out).setRankdir("TB");

    injector.getInstance(InjectorGrapher.class)
        .of(demoInjector)
        .graph();
  }
}

.dotファイル

上のコードを実行すると、グラフ明細を表す.dotファイルが作成される。 ファイルのそれぞれのエントリはグラフ内のノードやエッジを表す。 次に.dotファイルのサンプルを示す。

digraph injector {
  graph [rankdir=TB];
  k_997fdab [shape=box, label=<<table cellspacing="0" cellpadding="5" cellborder="0" border="0"><tr><td 
      align="left" port="header" bgcolor="#ffffff"><font align="left" color="#000000" point-size="10">@Nuclear<br
      align="left"/></font><font color="#000000">EnergySource<br align="left"/></font></td></tr></table>>,
      style=dashed, margin=0.02,0]
  k_119e4fd8 [shape=box, label=<<table cellspacing="0" cellpadding="5" cellborder="0" border="0"><tr><td
      align="left" port="header" bgcolor="#ffffff"><font align="left" color="#000000" point-size="10">@Driver<br 
      align="left"/></font><font color="#000000">Person<br align="left"/></font></td></tr></table>>, 
      style=dashed, margin=0.02,0]
  k_115c9d69 [shape=box, label=<<table cellspacing="0" cellpadding="5" cellborder="0" border="0"><tr><td 
      align="left" port="header" bgcolor="#ffffff"><font color="#000000">FluxCapacitor<br 
      align="left"/></font></td></tr></table>>, style=dashed, margin=0.02,0]
  i_115c9d69 [shape=box, label=<<table cellspacing="0" cellpadding="5" cellborder="1" border="0"><tr><td 
      align="left" port="header" bgcolor="#aaaaaa"><font align="left" color="#ffffff" point-
      size="10">BackToTheFutureModule.java:42<br align="left"/></font><font 
      color="#ffffff">#provideFluxCapacitor(EnergySource)<br align="left"/></font></td></tr></table>>, style=invis, 
      margin=0.02,0]
  k_1c5031a6 [shape=box, label=<<table cellspacing="0" cellpadding="5" cellborder="1" border="0"><tr><td 
      align="left" port="header" bgcolor="#000000"><font color="#ffffff">Plutonium<br 
      align="left"/></font></td></tr><tr><td align="left" port="m_9c5dfb84">&lt;init&gt;</td></tr></table>>, 
      style=invis, margin=0.02,0]
  k_17b87c3a [shape=box, label=<<table cellspacing="0" cellpadding="5" cellborder="1" border="0"><tr><td 
      align="left" port="header" bgcolor="#000000"><font color="#ffffff">MartyMcFly<br 
      align="left"/></font></td></tr><tr><td align="left" port="m_8b2cda3d">&lt;init&gt;</td></tr></table>>, 
      style=invis, margin=0.02,0]
  k_9acc501 [shape=box, label=<<table cellspacing="0" cellpadding="5" cellborder="0" border="0"><tr><td 
      align="left" port="header" bgcolor="#ffffff"><font color="#000000">EnergySource<br 
      align="left"/></font></td></tr></table>>, style=dashed, margin=0.02,0]
  k_dd456f9 [shape=box, label=<<table cellspacing="0" cellpadding="5" cellborder="1" border="0"><tr><td 
      align="left" port="header" bgcolor="#000000"><font color="#ffffff">EnergySourceProvider<br 
      align="left"/></font></td></tr><tr><td align="left" port="m_f4b5f9f7">&lt;init&gt;</td></tr></table>>, 
      style=invis, margin=0.02,0]
  k_997fdab -> k_1c5031a6 [arrowtail=none, style=dashed, arrowhead=onormal]
  k_119e4fd8 -> k_17b87c3a [arrowtail=none, style=dashed, arrowhead=onormal]
  k_115c9d69 -> i_115c9d69 [arrowtail=none, style=dashed, arrowhead=onormalonormal]
  i_115c9d69:header:e -> k_9acc501 [arrowtail=none, style=solid, arrowhead=normal]
  k_9acc501 -> k_dd456f9 [arrowtail=none, style=dashed, arrowhead=onormalonormal]
}

.dotファイルをレンダリングする

使用するプラットフォーム向けのGraphvizのビューアをダウンロードし、 .dotファイルをレンダリングしよう。 レンダリングには数分かかるかもしれない。 レンダリングされたグラフをPDFやイメージにエクスポートすることで、共有が簡単になる。

Linux上では、コマンドラインのdotツールを.dotファイルからイメージに変換するのに使うことができる。

  dot -T png my_injector.dot > my_injector.png

このツールは現在のところ、我々のフォントタグの扱いに問題があることがわかっている。 我々はこの問題を解消中である。

グラフ表示

Edges:

  • Solid edges represent dependencies from implementations to the types they depend on.
  • Dashed edges represent bindings from types to their implementations.
  • Double arrows indicate that the binding or dependency is to a Provider.

Nodes:

  • Implementation types are given black backgrounds.
  • Implementation instances have gray backgrounds.

Other behavior:

  • If an injection point requests a Provider<Foo>, the grapher will elide the Provider and just show a dependency to Foo.

  • The grapher will use an instance's toString() method to render its title if it has overridden Object's default implementation.