use of net.sourceforge.ondex.ONDEXPluginArguments in project knetbuilder by Rothamsted.
the class OndexPluginUtils method runPlugin.
/**
* Helper to invoke a plug-in as stand-alone, outside of the workflow.
*
* @param args these are translated into {@link ONDEXPluginArguments}. In case of multi-value
* arguments, use list values.
*
* @return a result that depends on the plug-in type: if it's instance of {@link ProducerONDEXPlugin}, returns
* {@link ProducerONDEXPlugin#collectResults() collectResults()}, if it's {@link RequiresGraph}, returns
* {@link RequiresGraph#getGraph() the plugin's graph}, else returns null.
*/
public static <T> T runPlugin(ONDEXPlugin plugin, Map<String, Object> args) throws UncheckedPluginException {
try {
// Prepare the arguments from the map
//
var pargDefs = plugin.getArgumentDefinitions();
var pargs = new ONDEXPluginArguments(pargDefs);
args.forEach(Exceptions.sneak().fromBiConsumer((argName, valObj) -> {
@SuppressWarnings("unchecked") Collection<Object> vlist = valObj instanceof Collection ? (Collection<Object>) valObj : List.of(valObj);
// if the arg is accepting a type other than string, addOption() will try to parse it
// using ArgugmentDefition.parseString()
vlist.forEach(Exceptions.sneak().consumer(val -> pargs.addOption(argName, val)));
}));
// OK, now set them
plugin.setArguments(pargs);
plugin.start();
if (plugin instanceof ProducerONDEXPlugin)
((ProducerONDEXPlugin) plugin).collectResults();
if (plugin instanceof RequiresGraph)
((RequiresGraph) plugin).getGraph();
return null;
} catch (Exception ex) {
throw new UncheckedPluginException("Error during plug-in execution: " + ex.getMessage(), ex);
}
}
use of net.sourceforge.ondex.ONDEXPluginArguments in project knetbuilder by Rothamsted.
the class Engine method runTransformer.
/**
* Runs a Transformer producer on the specified graph
*
* @param transformer the Transformer to run
* @param args arguments for the transformer
* @param graphInput the graph to use as input (and by implication output)
* @throws Exception
*/
public ONDEXGraph runTransformer(ONDEXTransformer transformer, ONDEXPluginArguments args, ONDEXGraph graphInput) throws Exception {
if (graphInput == null)
throw new NullPointerException("Can not run plugin with a null graphInput");
if (args == null)
args = new ONDEXPluginArguments(transformer.getArgumentDefinitions());
LuceneEnv lenv = null;
if (transformer.requiresIndexedGraph()) {
lenv = getIndex(graphInput, transformer.getName());
}
try {
String name = transformer.getName();
transformer.addONDEXListener(pluginLogger);
transformer.setArguments(args);
long start = System.currentTimeMillis();
transformer.setONDEXGraph(graphInput);
transformer.start();
fireEventOccurred(new GeneralOutputEvent(name + " took " + ((System.currentTimeMillis() - start) / 1000) + " seconds", getCurrentMethodName()));
removeIndex(graphInput, lenv);
return graphInput;
} finally {
if (lenv != null)
lenv.closeAll();
}
}
use of net.sourceforge.ondex.ONDEXPluginArguments in project knetbuilder by Rothamsted.
the class Engine method runMapping.
/**
* Runs a Mapping producer
*
* @param mapping the mapping to run
* @param args arguments for the mapping
* @param graphInput the graph to use as input (and by implication output)
*/
public ONDEXGraph runMapping(ONDEXMapping mapping, ONDEXPluginArguments args, ONDEXGraph graphInput) throws Exception {
if (graphInput == null)
throw new NullPointerException("Can not run plugin with a null graphInput");
Set<ONDEXRelation> rit = graphInput.getRelations();
if (rit == null)
return graphInput;
long relationsPre = rit.size();
if (args == null) {
args = new ONDEXPluginArguments(mapping.getArgumentDefinitions());
}
LuceneEnv lenv = null;
if (mapping.requiresIndexedGraph()) {
lenv = getIndex(graphInput, mapping.getName());
}
try {
String name = mapping.getName();
mapping.addONDEXListener(pluginLogger);
mapping.setArguments(args);
long start = System.currentTimeMillis();
mapping.setONDEXGraph(graphInput);
mapping.start();
fireEventOccurred(new GeneralOutputEvent(name + " took " + +((System.currentTimeMillis() - start) / 1000) + " seconds", getCurrentMethodName()));
rit = graphInput.getRelations();
long relationsPost = rit.size() - relationsPre;
fireEventOccurred(new GeneralOutputEvent("New Relations: " + relationsPost, getCurrentMethodName()));
rit = null;
removeIndex(graphInput, lenv);
return graphInput;
} finally {
if (lenv != null)
lenv.closeAll();
}
}
use of net.sourceforge.ondex.ONDEXPluginArguments in project knetbuilder by Rothamsted.
the class Engine method runExport.
/**
* Runs an export plug-in on the specified graph
*
* @param exporter the export to run
* @param exportArgs the args to run with
* @param inputGraph the graph to export
* @throws Exception if the export fails
*/
public void runExport(ONDEXExport exporter, ONDEXPluginArguments exportArgs, ONDEXGraph inputGraph) throws Exception {
if (inputGraph == null)
throw new NullPointerException("Can not run plugin with a null inputGraph");
if (exportArgs == null)
exportArgs = new ONDEXPluginArguments(exporter.getArgumentDefinitions());
String name = exporter.getName();
exporter.setArguments(exportArgs);
exporter.addONDEXListener(pluginLogger);
if (exporter.requiresValidators() != null && exporter.requiresValidators().length > 0) {
initializeValidators(exporter.requiresValidators(), inputGraph);
}
long start = System.currentTimeMillis();
exporter.setONDEXGraph(inputGraph);
exporter.start();
fireEventOccurred(new GeneralOutputEvent("Exporting with " + name + " took " + ((System.currentTimeMillis() - start) / 1000) + " seconds", getCurrentMethodName()));
}
use of net.sourceforge.ondex.ONDEXPluginArguments in project knetbuilder by Rothamsted.
the class Engine method runParser.
/**
* Runs a parser producer
*
* @param parser the parser to run
* @param args arguments for the parser
* @param graphInput the graph to use as input (and by implication output)
* @throws Exception
*/
public ONDEXGraph runParser(ONDEXParser parser, ONDEXPluginArguments args, ONDEXGraph graphInput) throws Exception {
if (graphInput == null)
throw new NullPointerException("Can not run plugin with a null graphInput");
if (args == null)
args = new ONDEXPluginArguments(parser.getArgumentDefinitions());
LuceneEnv lenv = null;
if (parser.requiresIndexedGraph()) {
lenv = getIndex(graphInput, parser.getName());
}
try {
parser.addONDEXListener(pluginLogger);
parser.setArguments(args);
if (parser.requiresValidators() != null && parser.requiresValidators().length > 0) {
initializeValidators(parser.requiresValidators(), graphInput);
}
long start = System.currentTimeMillis();
parser.setONDEXGraph(graphInput);
parser.start();
fireEventOccurred(new GeneralOutputEvent(parser.getName() + " took " + +((System.currentTimeMillis() - start) / 1000) + " seconds", getCurrentMethodName()));
removeIndex(graphInput, lenv);
return graphInput;
} finally {
if (lenv != null)
lenv.closeAll();
}
}
Aggregations