use of org.jgrapht.traverse.TopologicalOrderIterator in project storm by nathanmarz.
the class SubtopologyBolt method prepare.
@Override
public void prepare(Map conf, TopologyContext context, BatchOutputCollector batchCollector) {
int thisComponentNumTasks = context.getComponentTasks(context.getThisComponentId()).size();
for (Node n : _nodes) {
if (n.stateInfo != null) {
State s = n.stateInfo.spec.stateFactory.makeState(conf, context, context.getThisTaskIndex(), thisComponentNumTasks);
context.setTaskData(n.stateInfo.id, s);
}
}
DirectedSubgraph<Node, Object> subgraph = new DirectedSubgraph(_graph, _nodes, null);
TopologicalOrderIterator it = new TopologicalOrderIterator<Node, Object>(subgraph);
int stateIndex = 0;
while (it.hasNext()) {
Node n = (Node) it.next();
if (n instanceof ProcessorNode) {
ProcessorNode pn = (ProcessorNode) n;
String batchGroup = _batchGroups.get(n);
if (!_myTopologicallyOrdered.containsKey(batchGroup)) {
_myTopologicallyOrdered.put(batchGroup, new ArrayList());
}
_myTopologicallyOrdered.get(batchGroup).add(pn.processor);
List<String> parentStreams = new ArrayList();
List<Factory> parentFactories = new ArrayList();
for (Node p : TridentUtils.getParents(_graph, n)) {
parentStreams.add(p.streamId);
if (_nodes.contains(p)) {
parentFactories.add(_outputFactories.get(p));
} else {
if (!_roots.containsKey(p.streamId)) {
_roots.put(p.streamId, new InitialReceiver(p.streamId, getSourceOutputFields(context, p.streamId)));
}
_roots.get(p.streamId).addReceiver(pn.processor);
parentFactories.add(_roots.get(p.streamId).getOutputFactory());
}
}
List<TupleReceiver> targets = new ArrayList();
boolean outgoingNode = false;
for (Node cn : TridentUtils.getChildren(_graph, n)) {
if (_nodes.contains(cn)) {
targets.add(((ProcessorNode) cn).processor);
} else {
outgoingNode = true;
}
}
if (outgoingNode) {
targets.add(new BridgeReceiver(batchCollector));
}
TridentContext triContext = new TridentContext(pn.selfOutFields, parentFactories, parentStreams, targets, pn.streamId, stateIndex, batchCollector);
pn.processor.prepare(conf, context, triContext);
_outputFactories.put(n, pn.processor.getOutputFactory());
}
stateIndex++;
}
// TODO: get prepared one time into executor data... need to avoid the ser/deser
// for each task (probably need storm to support boltfactory)
}
use of org.jgrapht.traverse.TopologicalOrderIterator in project st-js by st-js.
the class AbstractSTJSMojo method packFiles.
/**
* packs all the files in a single file
*
* @param generator
* a {@link org.stjs.generator.Generator} object.
* @param gendir
* a {@link org.stjs.generator.GenerationDirectory} object.
* @throws org.apache.maven.plugin.MojoFailureException
* @throws org.apache.maven.plugin.MojoExecutionException
*/
protected void packFiles(Generator generator, GenerationDirectory gendir) throws MojoFailureException, MojoExecutionException {
if (!pack) {
return;
}
OutputStream allSourcesFile = null;
Writer packMapStream = null;
ClassLoader builtProjectClassLoader = getBuiltProjectClassLoader();
Map<String, File> currentProjectsFiles = new HashMap<String, File>();
// pack the files
try {
DirectedGraph<String, DefaultEdge> dependencyGraph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
File outputFile = new File(gendir.getGeneratedSourcesAbsolutePath(), project.getArtifactId() + ".js");
allSourcesFile = new BufferedOutputStream(new FileOutputStream(outputFile));
for (String sourceRoot : getCompileSourceRoots()) {
File sourceDir = new File(sourceRoot);
List<File> sources = new ArrayList<File>();
SourceMapping mapping = new SuffixMapping(".java", ".js");
SourceMapping stjsMapping = new SuffixMapping(".java", ".stjs");
// take all the files
sources = accumulateSources(gendir, sourceDir, mapping, stjsMapping, Integer.MIN_VALUE);
for (File source : sources) {
File absoluteTarget = (File) mapping.getTargetFiles(gendir.getGeneratedSourcesAbsolutePath(), source.getPath()).iterator().next();
String className = getClassNameForSource(source.getPath());
if (!absoluteTarget.exists()) {
getLog().debug(className + " is a bridge. Don't add it to the pack file");
continue;
}
// add this file to the hashmap to know that this class is part of the project
currentProjectsFiles.put(className, absoluteTarget);
if (getLog().isDebugEnabled()) {
getLog().debug("Packing " + absoluteTarget);
}
ClassWithJavascript cjs = generator.getExistingStjsClass(builtProjectClassLoader, builtProjectClassLoader.loadClass(className));
dependencyGraph.addVertex(className);
for (Map.Entry<ClassWithJavascript, DependencyType> dep : cjs.getDirectDependencyMap().entrySet()) {
if (dep.getKey() instanceof STJSClass) {
dependencyGraph.addVertex(dep.getKey().getJavaClassName());
if (dep.getValue() != DependencyType.OTHER) {
dependencyGraph.addEdge(dep.getKey().getJavaClassName(), className);
}
}
}
}
}
// check for cycles
detectCycles(dependencyGraph);
// dump all the files in the dependency order in the pack file
SourceMapGeneratorV3 packSourceMap = (SourceMapGeneratorV3) SourceMapGeneratorFactory.getInstance(SourceMapFormat.V3);
int currentLine = 0;
Iterator<String> it = new TopologicalOrderIterator<String, DefaultEdge>(dependencyGraph);
while (it.hasNext()) {
File targetFile = currentProjectsFiles.get(it.next());
// target file is absolute
if (targetFile != null) {
// for this project's files
if (generateSourceMap) {
currentLine = SourceMapUtils.appendFileSkipSourceMap(gendir.getGeneratedSourcesAbsolutePath(), allSourcesFile, targetFile, currentLine, packSourceMap, sourceEncoding);
} else {
Files.copy(targetFile, allSourcesFile);
}
allSourcesFile.flush();
}
}
if (generateSourceMap) {
File packMapFile = new File(gendir.getGeneratedSourcesAbsolutePath(), project.getArtifactId() + ".map");
packMapStream = new BufferedWriter(new FileWriter(packMapFile));
packSourceMap.appendTo(packMapStream, project.getArtifactId() + ".js");
allSourcesFile.write(("//# sourceMappingURL=" + project.getArtifactId() + ".map\n").getBytes());
allSourcesFile.flush();
}
} catch (Exception ex) {
throw new MojoFailureException("Error when packing files:" + ex.getMessage(), ex);
} finally {
try {
Closeables.close(allSourcesFile, true);
} catch (IOException e) {
LOG.log(Level.SEVERE, "IOException should not have been thrown.", e);
}
try {
Closeables.close(packMapStream, true);
} catch (IOException e) {
LOG.log(Level.SEVERE, "IOException should not have been thrown.", e);
}
}
}
use of org.jgrapht.traverse.TopologicalOrderIterator in project evosuite by EvoSuite.
the class RegexDistanceUtils method cacheRegex.
private static void cacheRegex(String regex) {
String r = expandRegex(regex);
Automaton automaton = new RegExp(r, RegExp.NONE).toAutomaton();
automaton.expandSingleton();
// We convert this to a graph without self-loops in order to determine the topological order
DirectedGraph<State, DefaultEdge> regexGraph = new DefaultDirectedGraph<State, DefaultEdge>(DefaultEdge.class);
Set<State> visitedStates = new HashSet<State>();
Queue<State> states = new LinkedList<State>();
State initialState = automaton.getInitialState();
states.add(initialState);
while (!states.isEmpty()) {
State currentState = states.poll();
if (visitedStates.contains(currentState))
continue;
if (!regexGraph.containsVertex(currentState))
regexGraph.addVertex(currentState);
for (Transition t : currentState.getTransitions()) {
// Need to get rid of back edges, otherwise there is no topological order!
if (!t.getDest().equals(currentState)) {
regexGraph.addVertex(t.getDest());
regexGraph.addEdge(currentState, t.getDest());
states.add(t.getDest());
CycleDetector<State, DefaultEdge> det = new CycleDetector<State, DefaultEdge>(regexGraph);
if (det.detectCycles()) {
regexGraph.removeEdge(currentState, t.getDest());
}
}
}
visitedStates.add(currentState);
}
TopologicalOrderIterator<State, DefaultEdge> iterator = new TopologicalOrderIterator<State, DefaultEdge>(regexGraph);
List<State> topologicalOrder = new ArrayList<State>();
while (iterator.hasNext()) {
topologicalOrder.add(iterator.next());
}
regexStateCache.put(regex, topologicalOrder);
regexAutomatonCache.put(regex, automaton);
}
use of org.jgrapht.traverse.TopologicalOrderIterator in project opentsdb by OpenTSDB.
the class QueryExecutor method execute.
/**
* Execute the RPC and serialize the response
* @param query The HTTP query to parse and and return results to
*/
public void execute(final HttpQuery query) {
http_query = query;
final QueryStats query_stats = new QueryStats(query.getRemoteAddress(), ts_query, query.getHeaders());
ts_query.setQueryStats(query_stats);
/**
* Sends the serialized results to the caller. This should be the very
* last callback executed.
*/
class CompleteCB implements Callback<Object, ChannelBuffer> {
@Override
public Object call(final ChannelBuffer cb) throws Exception {
query.sendReply(cb);
return null;
}
}
/**
* After all of the queries have run and we have data (or not) then we
* need to compile the iterators.
* This class could probably be improved:
* First we iterate over the results AND for each result, iterate over
* the expressions, giving a time synced iterator to each expression that
* needs the result set.
* THEN we iterate over the expressions again and build a DAG to determine
* if any of the expressions require the output of an expression. If so
* then we add the expressions to the proper parent and compile them in
* order.
* After all of that we're ready to start serializing and iterating
* over the results.
*/
class QueriesCB implements Callback<Object, ArrayList<DataPoints[]>> {
public Object call(final ArrayList<DataPoints[]> query_results) throws Exception {
for (int i = 0; i < query_results.size(); i++) {
final TSSubQuery sub = ts_query.getQueries().get(i);
Iterator<Entry<String, TSSubQuery>> it = sub_queries.entrySet().iterator();
while (it.hasNext()) {
final Entry<String, TSSubQuery> entry = it.next();
if (entry.getValue().equals(sub)) {
sub_query_results.put(entry.getKey(), query_results.get(i));
if (expressions != null) {
for (final ExpressionIterator ei : expressions.values()) {
if (ei.getVariableNames().contains(entry.getKey())) {
final TimeSyncedIterator tsi = new TimeSyncedIterator(entry.getKey(), sub.getFilterTagKs(), query_results.get(i));
final NumericFillPolicy fill = fills.get(entry.getKey());
if (fill != null) {
tsi.setFillPolicy(fill);
}
ei.addResults(entry.getKey(), tsi);
if (LOG.isDebugEnabled()) {
LOG.debug("Added results for " + entry.getKey() + " to " + ei.getId());
}
}
}
}
}
}
}
// handle nested expressions
final DirectedAcyclicGraph<String, DefaultEdge> graph = new DirectedAcyclicGraph<String, DefaultEdge>(DefaultEdge.class);
if (expressions != null) {
for (final Entry<String, ExpressionIterator> eii : expressions.entrySet()) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Expression entry key is %s, value is %s", eii.getKey(), eii.getValue().toString()));
LOG.debug(String.format("Time to loop through the variable names " + "for %s", eii.getKey()));
}
if (!graph.containsVertex(eii.getKey())) {
if (LOG.isDebugEnabled()) {
LOG.debug("Adding vertex " + eii.getKey());
}
graph.addVertex(eii.getKey());
}
for (final String var : eii.getValue().getVariableNames()) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("var is %s", var));
}
final ExpressionIterator ei = expressions.get(var);
if (ei != null) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("The expression iterator for %s is %s", var, ei.toString()));
}
// TODO - really ought to calculate this earlier
if (eii.getKey().equals(var)) {
throw new IllegalArgumentException("Self referencing expression found: " + eii.getKey());
}
if (LOG.isDebugEnabled()) {
LOG.debug("Nested expression detected. " + eii.getKey() + " depends on " + var);
}
if (!graph.containsVertex(eii.getKey())) {
if (LOG.isDebugEnabled()) {
LOG.debug("Added vertex " + eii.getKey());
}
graph.addVertex(eii.getKey());
} else if (LOG.isDebugEnabled()) {
LOG.debug("Already contains vertex " + eii.getKey());
}
if (!graph.containsVertex(var)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Added vertex " + var);
}
graph.addVertex(var);
} else if (LOG.isDebugEnabled()) {
LOG.debug("Already contains vertex " + var);
}
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Added Edge " + eii.getKey() + " - " + var);
}
graph.addDagEdge(eii.getKey(), var);
} catch (CycleFoundException cfe) {
throw new IllegalArgumentException("Circular reference found: " + eii.getKey(), cfe);
}
} else if (LOG.isDebugEnabled()) {
LOG.debug(String.format("The expression iterator for %s is null", var));
}
}
}
}
// compile all of the expressions
final long intersect_start = DateTime.currentTimeMillis();
final int expressionLength = expressions == null ? 0 : expressions.size();
final ExpressionIterator[] compile_stack = new ExpressionIterator[expressionLength];
final TopologicalOrderIterator<String, DefaultEdge> it = new TopologicalOrderIterator<String, DefaultEdge>(graph);
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Expressions Size is %d", expressionLength));
LOG.debug(String.format("Topology Iterator %s", it.toString()));
}
int i = 0;
while (it.hasNext()) {
String next = it.next();
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Expression: %s", next));
}
ExpressionIterator ei = expressions.get(next);
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Expression Iterator: %s", ei.toString()));
}
if (ei == null) {
LOG.error(String.format("The expression iterator for %s is null", next));
}
compile_stack[i] = ei;
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Added expression %s to compile_stack[%d]", next, i));
}
i++;
}
if (i != expressionLength) {
throw new IOException(String.format(" Internal Error: Fewer " + "expressions where added to the compile stack than " + "expressions.size (%d instead of %d)", i, expressionLength));
}
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("compile stack length: %d", compile_stack.length));
}
for (int x = compile_stack.length - 1; x >= 0; x--) {
if (compile_stack[x] == null) {
throw new NullPointerException(String.format("Item %d in " + "compile_stack[] is null", x));
}
// look for and add expressions
for (final String var : compile_stack[x].getVariableNames()) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Looking for variable %s for %s", var, compile_stack[x].getId()));
}
ExpressionIterator source = expressions.get(var);
if (source != null) {
compile_stack[x].addResults(var, source.getCopy());
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Adding expression %s to %s", source.getId(), compile_stack[x].getId()));
}
}
}
compile_stack[x].compile();
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Successfully compiled %s", compile_stack[x].getId()));
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("Finished compilations in " + (DateTime.currentTimeMillis() - intersect_start) + " ms");
}
return serialize().addCallback(new CompleteCB()).addErrback(new ErrorCB());
}
}
/**
* Callback executed after we have resolved the metric, tag names and tag
* values to their respective UIDs. This callback then runs the actual
* queries and fetches their results.
*/
class BuildCB implements Callback<Deferred<Object>, net.opentsdb.core.Query[]> {
@Override
public Deferred<Object> call(final net.opentsdb.core.Query[] queries) {
final ArrayList<Deferred<DataPoints[]>> deferreds = new ArrayList<Deferred<DataPoints[]>>(queries.length);
for (final net.opentsdb.core.Query query : queries) {
deferreds.add(query.runAsync());
}
return Deferred.groupInOrder(deferreds).addCallback(new QueriesCB()).addErrback(new ErrorCB());
}
}
// TODO - only run the ones that will be involved in an output. Folks WILL
// ask for stuff they don't need.... *sigh*
ts_query.buildQueriesAsync(tsdb).addCallback(new BuildCB()).addErrback(new ErrorCB());
}
use of org.jgrapht.traverse.TopologicalOrderIterator in project jop by jop-devel.
the class GreedyOptimizer method optimize.
public void optimize() {
List<MethodInfo> rootMethods = config.getTargetMethods();
// initialization
resetCounters();
boolean useWCAProvider = config.useWCA() && config.useWCAExecCount();
GreedyOrder order = config.getOrder();
if (order != GreedyOrder.WCAFirst) {
// updated as well.
if (!config.getTargetMethodSet().equals(config.getWCATargetSet())) {
logger.warn("Using the WCA for exec frequencies is currently only supported if order is WCAFirst " + "or if the target method is the WCA target method");
useWCAProvider = false;
}
}
AnalysisManager analyses = initializeAnalyses(config.useWCEP() || useWCAProvider);
for (CodeOptimizer opt : optimizers) {
opt.initialize(analyses, rootMethods);
}
CandidateSelector selector;
if (config.useWCA()) {
GainCalculator gc = new GainCalculator(analyses);
if (config.useWCEP()) {
logger.info("Using WCEP driven selector");
selector = new WCEPRebateSelector(analyses, gc, config.getMaxCodesize());
} else {
logger.info("Using WCA driven selector");
selector = new WCETRebateSelector(analyses, gc, config.getMaxCodesize());
}
} else {
logger.info("WCA is disabled, not using WCA for optimization.");
selector = new ACETRebateSelector(analyses, new GainCalculator(analyses), config.getMaxCodesize());
}
selector.initialize(config, true);
ExecFrequencyProvider ecp = useWCAProvider ? analyses.getWCAInvoker() : analyses.getExecFrequencyAnalysis();
if (config.useLocalExecCount()) {
ecp = new LocalExecFrequencyProvider(ecp);
}
// dump initial callgraph
logger.info("Initial number of methods in target callgraph: " + analyses.getTargetCallGraph().getMethodInfos().size());
analyses.getTargetCallGraph().dumpCallgraph(jcopter.getJConfig().getConfig(), "greedy-target", config.getTargetCallgraphDumpType(), true);
if (order == GreedyOrder.Global || (order == GreedyOrder.WCAFirst && !config.useWCA())) {
optimizeMethods(analyses, ecp, selector, analyses.getTargetCallGraph().getMethodInfos());
} else if (order == GreedyOrder.Targets) {
for (MethodInfo target : config.getTargetMethods()) {
optimizeMethods(analyses, ecp, selector, analyses.getTargetCallGraph().getReachableImplementationsSet(target));
}
} else if (order == GreedyOrder.WCAFirst) {
logger.info("Optimizing WCA target");
Set<MethodInfo> wcaMethods = analyses.getWCAMethods();
optimizeMethods(analyses, ecp, selector, wcaMethods);
selector.printStatistics();
// We do not want to include the wca methods in the second pass because inlining there could have negative
// effects on the WCET path due to the cache
Set<MethodInfo> others = new LinkedHashSet<MethodInfo>(analyses.getTargetCallGraph().getMethodInfos());
others.removeAll(wcaMethods);
logger.info("Optimizing non-WCA code");
// analyses.dumpTargetCallgraph("acet", true);
selector = new ACETRebateSelector(analyses, new GainCalculator(analyses), config.getMaxCodesize());
selector.initialize(config, false);
ecp = analyses.getExecFrequencyAnalysis();
if (config.useLocalExecCount()) {
ecp = new LocalExecFrequencyProvider(ecp);
}
optimizeMethods(analyses, ecp, selector, others);
} else if (order == GreedyOrder.TopDown || order == GreedyOrder.BottomUp) {
if (config.useWCA() && !analyses.hasWCATargetsOnly()) {
// TODO iterate over WCA and then non-wca graph or something in this case..
throw new AppInfoError("Order " + order + " currently only works with WCA if the target method is the WCA target");
}
TopologicalOrderIterator<MethodNode, InvokeEdge> topOrder = new TopologicalOrderIterator<MethodNode, InvokeEdge>(analyses.getTargetCallGraph().getAcyclicMergedGraph(order == GreedyOrder.BottomUp));
while (topOrder.hasNext()) {
MethodNode node = topOrder.next();
optimizeMethods(analyses, ecp, selector, Collections.singleton(node.getMethodInfo()));
}
} else {
throw new AppInfoError("Order " + order + " not yet implemented.");
}
// dump final callgraph
analyses.getTargetCallGraph().dumpCallgraph(jcopter.getJConfig().getConfig(), "greedy-target-opt", config.getTargetCallgraphDumpType(), true);
selector.printStatistics();
printStatistics();
}
Aggregations