How to use the graphlib.alg.topsort function in graphlib

To help you get started, we’ve selected a few graphlib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ConsenSys / ethql / packages / plugin / src / bootstrap.ts View on Github external
// Add the node.
    graph.setNode(name, plugin);

    // Add an implicit dependency on core, if core is not explicitly listed.
    if (coreLoaded && name !== 'core' && ![...after, ...before].includes('core')) {
      after.push('core');
    }

    // Add the edges.
    before.forEach(b => graph.setEdge(name, b));
    after.forEach(a => graph.setEdge(a, name));
  }

  // Sort plugins topologically.
  const orderedPlugins = alg.topsort(graph).map(node => graph.node(node) as EthqlPlugin);

  const sources = graph.sources();
  if (sources.length === 0 || !alg.isAcyclic(graph)) {
    throw new Error(ERR_MSG_NO_ROOT);
  } else if (sources.length > 1) {
    throw new Error(ERR_MSG_MANY_ROOTS(sources));
  }

  console.log(`⚒   Bootstrapping with plugins: ${orderedPlugins.map(p => p.name).join(', ')}.`);

  // Merge schemas, resolvers, serviceDefinitions from all plugins.
  let merged: MergeResult = { config: runtimeConfig, schema: [], resolvers: {}, serviceDefinitions: {} };
  for (let plugin of orderedPlugins) {
    if (typeof plugin.resolvers === 'function') {
      plugin.resolvers = plugin.resolvers(merged.resolvers);
    }
github GiraffeTools / GiraffeTools / frontend / porcupine / selectors / selectors.js View on Github external
(session) => {
      // #TODO: this graph is quite some antipattern to functional
      // programming...
      const nodes = session.Node.all().toModelArray();

      const graph = Graph.getInstance();
      let sorted;
      try {
        const order = GraphAlgorithms.topsort(graph);
        sorted = nodes.sort((a, b) => order.findIndex((id) => id === a.id)
          - order.findIndex((id) => id === b.id));
      } catch (error) {
        // #TODO not sure how to check this:
        // if (error instanceof CycleException) {
        sorted = nodes;
        console.warn('You probably have a circularity in your graph...');
      }
      return sorted.map((node) => {
        const parameters = node.parameters && node.parameters.toRefArray();
        return {...node.ref, parameters};
      });
    }
github gregjacobs / js-to-ts-converter / src / converter / add-class-property-declarations / correct-js-properties.ts View on Github external
This class's graph ID: ${jsClass.id}
					It's superclass's graph ID: ${jsClass.superclassId}
					
					Current IDs in the graph:
					    ${jsClassHierarchyGraph.nodes().join( '\n    ' )}
				`.replace( /^\t*/gm, '' ) );
			}

			jsClassHierarchyGraph.setEdge( jsClass.id, jsClass.superclassId );
		}
	} );

	// the topological sort is going to put superclasses later in the returned
	// array, so reverse it
	logger.debug( 'Topologically sorting the graph in superclass->subclass order' );
	const superclassToSubclassOrder = alg.topsort( jsClassHierarchyGraph ).reverse();

	// Starting from superclass JsClass instances and walking down to subclass
	// JsClass instances, fill in the property sets. When a subclass is
	// encountered, take all of the properties that were used in that subclass,
	// minus the properties in its superclass, in order to determine the
	// subclass-specific properties
	superclassToSubclassOrder.forEach( jsClassId => {
		const jsClass = jsClassHierarchyGraph.node( jsClassId ) as JsClass;
		const subclassOnlyProperties = new Set( jsClass.properties );

		const superclasses = getSuperclasses( jsClass );
		superclasses.forEach( ( superclass: JsClass ) => {
			// Filter out both properties and methods from each superclass
			superclass.members.forEach( ( superclassProp: string ) => {
				subclassOnlyProperties.delete( superclassProp );
			} );
github unboundedsystems / adapt / core / src / deploy / execution_plan.ts View on Github external
const deps = succIds.map((sId) => {
                const isHard = hardSet.delete(sId);
                return { id: sId, type: isHard ? "hard" : "soft" };
            });
            if (hardSet.size !== 0) {
                throw new InternalError(`Internal consistency check failed: ` +
                    `not all hardDeps are successors`);
            }
            const entry: EPDependencies[string] = { detail: detail(node), deps };
            if (node.element) entry.elementId = node.element.id;
            return entry;
        };

        const ret: EPDependencies = {};
        const ids = alg.isAcyclic(this.graph) ?
            alg.topsort(this.graph) : this.graph.nodes();

        // Insert starting with leaves for a more human-readable ordering
        for (let i = ids.length - 1; i >= 0; i--) {
            const id = ids[i];
            const node = this.getNode(id);
            ret[id] = getDeps(node, id);
        }
        return ret;
    }
github ezolenko / rollup-plugin-typescript2 / src / tscache.ts View on Github external
public walkTree(cb: (id: string) => void | false): void
	{
		const acyclic = alg.isAcyclic(this.dependencyTree);

		if (acyclic)
		{
			_.each(alg.topsort(this.dependencyTree), (id: string) => cb(id));
			return;
		}

		this.context.info(yellow("import tree has cycles"));

		_.each(this.dependencyTree.nodes(), (id: string) => cb(id));
	}
github ezolenko / rollup-plugin-typescript2 / dist / rollup-plugin-typescript2.es.js View on Github external
TsCache.prototype.walkTree = function (cb) {
        var acyclic = alg.isAcyclic(this.dependencyTree);
        if (acyclic) {
            each(alg.topsort(this.dependencyTree), function (id) { return cb(id); });
            return;
        }
        this.context.info(yellow("import tree has cycles"));
        each(this.dependencyTree.nodes(), function (id) { return cb(id); });
    };
    TsCache.prototype.done = function () {