001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.tang.examples; 020 021import org.apache.reef.tang.Configuration; 022import org.apache.reef.tang.ConfigurationBuilder; 023import org.apache.reef.tang.Injector; 024import org.apache.reef.tang.Tang; 025import org.apache.reef.tang.annotations.Name; 026import org.apache.reef.tang.annotations.NamedParameter; 027import org.apache.reef.tang.annotations.Parameter; 028import org.apache.reef.tang.exceptions.BindException; 029import org.apache.reef.tang.exceptions.InjectionException; 030import org.apache.reef.tang.formats.CommandLine; 031import org.apache.reef.tang.implementation.InjectionPlan; 032import org.apache.reef.tang.util.walk.graphviz.GraphvizConfigVisitor; 033import org.apache.reef.tang.util.walk.graphviz.GraphvizInjectionPlanVisitor; 034 035import javax.inject.Inject; 036import java.io.FileOutputStream; 037import java.io.OutputStreamWriter; 038import java.io.Writer; 039import java.io.IOException; 040import java.nio.charset.StandardCharsets; 041 042/** 043 * Build a Graphviz representation of TANG configuration and injection plan. 044 */ 045public final class PrintTypeHierarchy { 046 047 /** 048 * Parameter to test the injection. 049 */ 050 private final transient int id; 051 052 /** 053 * Constructor to test the parameter injection. 054 * 055 * @param id test parameter 056 */ 057 @Inject 058 public PrintTypeHierarchy(@Parameter(PrintTypeHierarchy.Id.class) final int id) { 059 this.id = id; 060 } 061 062 /** 063 * @param args command line arguments. 064 * @throws BindException configuration error. 065 * @throws InjectionException configuration error. 066 * @throws IOException cannot process command line parameters. 067 */ 068 public static void main(final String[] args) 069 throws BindException, InjectionException, IOException { 070 071 final Tang tang = Tang.Factory.getTang(); 072 final ConfigurationBuilder confBuilder = tang.newConfigurationBuilder(); 073 074 new CommandLine(confBuilder).processCommandLine(args); 075 final Configuration config = confBuilder.build(); 076 077 final Injector injector = tang.newInjector(config); 078 final PrintTypeHierarchy myself = injector.getInstance(PrintTypeHierarchy.class); 079 080 try (final Writer out = new OutputStreamWriter( 081 new FileOutputStream("type-hierarchy.dot"), StandardCharsets.UTF_8)) { 082 out.write(GraphvizConfigVisitor.getGraphvizString(config, true, true)); 083 } 084 085 final InjectionPlan<PrintTypeHierarchy> plan = 086 injector.getInjectionPlan(PrintTypeHierarchy.class); 087 088 try (final Writer out = new OutputStreamWriter( 089 new FileOutputStream("injection-plan.dot"), StandardCharsets.UTF_8)) { 090 out.write(GraphvizInjectionPlanVisitor.getGraphvizString(plan, true)); 091 } 092 093 System.out.println(myself); 094 } 095 096 /** 097 * @return string representation of the object. 098 */ 099 @Override 100 public String toString() { 101 return this.getClass().getName() + " :: " + this.id; 102 } 103 104 /** 105 * Parameter to test the injection. 106 */ 107 @NamedParameter(default_value = "999", doc = "Test parameter", short_name = "id") 108 class Id implements Name<Integer> { 109 } 110}