-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first complete version of injection plan traversal and graphviz output.
- Loading branch information
Showing
9 changed files
with
272 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
type-hierarchy.* | ||
injection-plan.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
tang/src/main/java/com/microsoft/tang/util/walk/AbstractInjectionPlanNodeVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2013 Microsoft. | ||
* | ||
* Licensed 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 com.microsoft.tang.util.walk; | ||
|
||
import com.microsoft.tang.implementation.InjectionPlan; | ||
import com.microsoft.tang.implementation.Constructor; | ||
import com.microsoft.tang.implementation.RequiredSingleton; | ||
import com.microsoft.tang.implementation.Subplan; | ||
import com.microsoft.tang.implementation.java.JavaInstance; | ||
|
||
/** | ||
* Generic interface to traverse nodes of the injection plan. | ||
* Dispatches between Constructor, Subplan, RequiredSingleton, and JavaInstance types. | ||
* It is used e.g. in Walk.preorder() | ||
*/ | ||
public abstract class AbstractInjectionPlanNodeVisitor implements NodeVisitor<InjectionPlan<?>> { | ||
|
||
/** | ||
* Manually dispatch between different types of injection plan objects and call proper | ||
* visit() method. Currently dispatches between Constructor, Subplan, RequiredSingleton, | ||
* and JavaInstance types. | ||
* @param aNode TANG injection plan node. | ||
* @return true to proceed with the next node, false to cancel. | ||
* @throws ClassCastException if argument is not one of Constructor, Subplan, | ||
* RequiredSingleton, or JavaInstance. | ||
*/ | ||
@Override | ||
public boolean visit(final InjectionPlan<?> aNode) { | ||
if (aNode instanceof Constructor<?>) { | ||
return visit((Constructor) aNode); | ||
} else if (aNode instanceof Subplan<?>) { | ||
return visit((Subplan) aNode); | ||
} else if (aNode instanceof RequiredSingleton<?,?>) { | ||
return visit((RequiredSingleton) aNode); | ||
} else if (aNode instanceof JavaInstance<?>) { | ||
return visit((JavaInstance) aNode); | ||
} | ||
throw new ClassCastException( | ||
"Node " + aNode.getClass() + " cannot be casted to one of the known subclasses." | ||
+ " Override this method to handle the case."); | ||
} | ||
|
||
/** | ||
* Process current injection plan node of Constructor type. | ||
* @param aNode Current injection plan node. | ||
* @return true to proceed with the next node, false to cancel. | ||
*/ | ||
public abstract boolean visit(Constructor<?> aNode); | ||
|
||
/** | ||
* Process current injection plan node of JavaInstance type. | ||
* @param aNode Current injection plan node. | ||
* @return true to proceed with the next node, false to cancel. | ||
*/ | ||
public abstract boolean visit(JavaInstance<?> aNode); | ||
|
||
/** | ||
* Process current injection plan node of RequiredSingleton type. | ||
* @param aNode Current injection plan node. | ||
* @return true to proceed with the next node, false to cancel. | ||
*/ | ||
public abstract boolean visit(RequiredSingleton<?,?> aNode); | ||
|
||
/** | ||
* Process current injection plan node of Subplan type. | ||
* @param aNode Current injection plan node. | ||
* @return true to proceed with the next node, false to cancel. | ||
*/ | ||
public abstract boolean visit(Subplan<?> aNode); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
tang/src/main/java/com/microsoft/tang/util/walk/graphviz/GraphvizInjectionPlanVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Copyright 2013 Microsoft. | ||
* | ||
* Licensed 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 com.microsoft.tang.util.walk.graphviz; | ||
|
||
import com.microsoft.tang.implementation.InjectionPlan; | ||
import com.microsoft.tang.implementation.Constructor; | ||
import com.microsoft.tang.implementation.RequiredSingleton; | ||
import com.microsoft.tang.implementation.Subplan; | ||
import com.microsoft.tang.implementation.java.JavaInstance; | ||
|
||
import com.microsoft.tang.util.walk.Walk; | ||
import com.microsoft.tang.util.walk.EdgeVisitor; | ||
import com.microsoft.tang.util.walk.AbstractInjectionPlanNodeVisitor; | ||
|
||
/** | ||
* Build a Graphviz representation of the injection plan graph. | ||
*/ | ||
public final class GraphvizInjectionPlanVisitor | ||
extends AbstractInjectionPlanNodeVisitor implements EdgeVisitor<InjectionPlan<?>> | ||
{ | ||
/** Accumulate string representation of the graph here. */ | ||
private final transient StringBuilder mGraphStr = | ||
new StringBuilder("digraph InjectionPlanMain {\n"); | ||
|
||
/** | ||
* Process current injection plan node of Constructor type. | ||
* @param aNode Current injection plan node. | ||
* @return true to proceed with the next node, false to cancel. | ||
*/ | ||
@Override | ||
public boolean visit(final Constructor<?> aNode) { | ||
mGraphStr.append(" ") | ||
.append(aNode.getNode().getName()) | ||
.append(" [label=\"") | ||
.append(aNode) | ||
.append("\", shape=box") | ||
.append("];\n"); | ||
return true; | ||
} | ||
|
||
/** | ||
* Process current injection plan node of JavaInstance type. | ||
* @param aNode Current injection plan node. | ||
* @return true to proceed with the next node, false to cancel. | ||
*/ | ||
@Override | ||
public boolean visit(final JavaInstance<?> aNode) { | ||
mGraphStr.append(" ") | ||
.append(aNode.getNode().getName()) | ||
.append(" [label=\"") | ||
.append(aNode) | ||
.append("\", shape=box") | ||
.append("];\n"); | ||
return true; | ||
} | ||
|
||
/** | ||
* Process current injection plan node of RequiredSingleton type. | ||
* @param aNode Current injection plan node. | ||
* @return true to proceed with the next node, false to cancel. | ||
*/ | ||
@Override | ||
public boolean visit(final RequiredSingleton<?,?> aNode) { | ||
mGraphStr.append(" ") | ||
.append(aNode.getNode().getName()) | ||
.append(" [label=\"") | ||
.append(aNode) | ||
.append("\", shape=box, style=filled") | ||
.append("];\n"); | ||
return true; | ||
} | ||
|
||
/** | ||
* Process current injection plan node of Subplan type. | ||
* @param aNode Current injection plan node. | ||
* @return true to proceed with the next node, false to cancel. | ||
*/ | ||
@Override | ||
public boolean visit(final Subplan<?> aNode) { | ||
mGraphStr.append(" ") | ||
.append(aNode.getNode().getName()) | ||
.append(" [label=\"") | ||
.append(aNode) | ||
.append("\", shape=oval") | ||
.append("];\n"); | ||
return true; | ||
} | ||
|
||
/** | ||
* Process current edge of the injection plan. | ||
* @param aNodeFrom Current injection plan node. | ||
* @param aNodeTo Destination injection plan node. | ||
* @return true to proceed with the next node, false to cancel. | ||
*/ | ||
@Override | ||
public boolean visit(final InjectionPlan<?> aNodeFrom, final InjectionPlan<?> aNodeTo) { | ||
mGraphStr.append(" ") | ||
.append(aNodeFrom.getNode().getName()) | ||
.append(" -> ") | ||
.append(aNodeTo.getNode().getName()) | ||
.append(" [style=solid];\n"); | ||
return true; | ||
} | ||
|
||
/** | ||
* @return TANG injection plan represented as a Graphviz DOT string. | ||
*/ | ||
@Override | ||
public String toString() { | ||
return mGraphStr.toString() + "}\n"; | ||
} | ||
|
||
/** | ||
* Produce a Graphviz DOT string for a given TANG injection plan. | ||
* @param aConfig TANG configuration object. | ||
* @param aShowImpl If true, plot IS-A edges for know implementations. | ||
* @param aShowLegend If true, add legend to the plot. | ||
* @return Injection plan represented as a string in Graphviz DOT format. | ||
*/ | ||
public static String getGraphvizStr(final InjectionPlan<?> plan) | ||
{ | ||
final GraphvizInjectionPlanVisitor visitor = new GraphvizInjectionPlanVisitor(); | ||
Walk.preorder(visitor, visitor, plan); | ||
return visitor.toString(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
tang/src/main/java/com/microsoft/tang/util/walk/graphviz/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2013 Microsoft. | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
/** | ||
* produce Graphviz representation of TANG configuration graph and injection plan. | ||
*/ | ||
package com.microsoft.tang.util.walk.graphviz; |
Oops, something went wrong.