blob: 2e993682e2e5607518ca63caa8801ae8b762d038 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.reef.tang.examples;
import org.apache.reef.tang.Configuration;
import org.apache.reef.tang.ConfigurationBuilder;
import org.apache.reef.tang.Injector;
import org.apache.reef.tang.Tang;
import org.apache.reef.tang.annotations.Name;
import org.apache.reef.tang.annotations.NamedParameter;
import org.apache.reef.tang.annotations.Parameter;
import org.apache.reef.tang.exceptions.BindException;
import org.apache.reef.tang.exceptions.InjectionException;
import org.apache.reef.tang.formats.CommandLine;
import org.apache.reef.tang.implementation.InjectionPlan;
import org.apache.reef.tang.util.walk.graphviz.GraphvizConfigVisitor;
import org.apache.reef.tang.util.walk.graphviz.GraphvizInjectionPlanVisitor;
import javax.inject.Inject;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* Build a Graphviz representation of TANG configuration and injection plan.
*/
public final class PrintTypeHierarchy {
/**
* Parameter to test the injection.
*/
private final transient int id;
/**
* Constructor to test the parameter injection.
*
* @param id test parameter
*/
@Inject
public PrintTypeHierarchy(@Parameter(PrintTypeHierarchy.Id.class) final int id) {
this.id = id;
}
/**
* @param args command line arguments.
* @throws BindException configuration error.
* @throws InjectionException configuration error.
* @throws IOException cannot process command line parameters.
*/
public static void main(final String[] args)
throws BindException, InjectionException, IOException {
final Tang tang = Tang.Factory.getTang();
final ConfigurationBuilder confBuilder = tang.newConfigurationBuilder();
new CommandLine(confBuilder).processCommandLine(args);
final Configuration config = confBuilder.build();
final Injector injector = tang.newInjector(config);
final PrintTypeHierarchy myself = injector.getInstance(PrintTypeHierarchy.class);
try (Writer out = new OutputStreamWriter(
new FileOutputStream("type-hierarchy.dot"), StandardCharsets.UTF_8)) {
out.write(GraphvizConfigVisitor.getGraphvizString(config, true, true));
}
final InjectionPlan<PrintTypeHierarchy> plan =
injector.getInjectionPlan(PrintTypeHierarchy.class);
try (Writer out = new OutputStreamWriter(
new FileOutputStream("injection-plan.dot"), StandardCharsets.UTF_8)) {
out.write(GraphvizInjectionPlanVisitor.getGraphvizString(plan, true));
}
System.out.println(myself);
}
/**
* @return string representation of the object.
*/
@Override
public String toString() {
return this.getClass().getName() + " :: " + this.id;
}
/**
* Parameter to test the injection.
*/
@NamedParameter(default_value = "999", doc = "Test parameter", short_name = "id")
class Id implements Name<Integer> {
}
}