Skip to content

Commit

Permalink
ROBOT: Add support for ID range files.
Browse files Browse the repository at this point in the history
This commit adds a new mode to the kgcl:apply command for automatic
generation of IDs: a mode that is similar to the "manual" mode, except
that the details about the ID range are to be found in a OBO-style ID
range file, instead of being manually specified on the command line.

This mode is selected with the --auto-id-range-file option, which
expects the name of the ID range file. The range name can be specified
with --auto-id-range-name; if no such option is used, the command will
look for a range allocated to "kgcl", "KGCL", "ontobot", or "Ontobot".

If none of the options selecting an ID generation mode
(--auto-id-prefix, --auto-id-temp-prefix, --auto-id-range-file) is used,
the command will by default look for a file whose name ends with
"-idranges.owl". If such a file (only one) is found, the ID range file
mode is automatically enabled, provided a suitable range is found within
the file.
  • Loading branch information
gouttegd committed Mar 24, 2024
1 parent 31009b9 commit 9b3ac76
Showing 1 changed file with 75 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.incenp.obofoundry.kgcl.robot;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.time.LocalDate;
import java.time.ZoneId;
Expand All @@ -30,6 +31,10 @@

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.incenp.obofoundry.idrange.IDRange;
import org.incenp.obofoundry.idrange.IDRangePolicyException;
import org.incenp.obofoundry.idrange.IDRangePolicyParser;
import org.incenp.obofoundry.idrange.IIDRangePolicy;
import org.incenp.obofoundry.kgcl.AutoIDAllocator;
import org.incenp.obofoundry.kgcl.IAutoIDGenerator;
import org.incenp.obofoundry.kgcl.KGCLHelper;
Expand Down Expand Up @@ -80,6 +85,8 @@ public ApplyCommand() {
options.addOption(null, "auto-id-width", true, "Width of automatically assigned IDs");
options.addOption(null, "auto-id-prefix", true, "Prefix for automatically assigned IDs");
options.addOption(null, "auto-id-temp-prefix", true, "Generate random temporary IDs in the specified prefix");
options.addOption(null, "auto-id-range-file", true, "Assign IDs from the specified ID range file");
options.addOption(null, "auto-id-range-name", true, "Use the specified ID range name");
}

@Override
Expand Down Expand Up @@ -181,12 +188,9 @@ public CommandState execute(CommandState state, String[] args) throws Exception
}

if ( changeset.size() > 0 ) {
IAutoIDGenerator idGenerator = getAutoIDGenerator(line, ontology);
if ( idGenerator != null ) {
AutoIDAllocator idAllocator = new AutoIDAllocator(idGenerator);
if ( !idAllocator.reallocate(changeset) ) {
throw new Exception("Cannot generate automatic IDs");
}
AutoIDAllocator idAllocator = new AutoIDAllocator(getAutoIDGenerator(line, ontology));
if ( !idAllocator.reallocate(changeset) ) {
throw new Exception("Cannot generate automatic IDs");
}

List<RejectedChange> rejects = new ArrayList<RejectedChange>();
Expand Down Expand Up @@ -242,7 +246,72 @@ private IAutoIDGenerator getAutoIDGenerator(CommandLine line, OWLOntology ontolo
} else if ( line.hasOption("auto-id-temp-prefix") ) {
String prefix = line.getOptionValue("auto-id-temp-prefix");
return () -> prefix + UUID.randomUUID().toString();
} else if ( line.hasOption("auto-id-range-file") ) {
return getAutoIDGenerator(line, ontology, line.getOptionValue("auto-id-range-file"), false);
} else {
String rangeFile = findIDRangeFile();
if ( rangeFile != null ) {
return getAutoIDGenerator(line, ontology, rangeFile, true);
}
}
return null;
}

private IAutoIDGenerator getAutoIDGenerator(CommandLine line, OWLOntology ontology, String rangeFile,
boolean silent) throws Exception {
IDRangePolicyParser parser = new IDRangePolicyParser(rangeFile);
try {
IIDRangePolicy policy = parser.parse();
IDRange range = null;
if ( line.hasOption("auto-id-range-name") ) {
range = policy.getRange(line.getOptionValue("auto-id-range-name"));
if ( range == null ) {
throw new Exception("Requested range not found in ID range file");
}
}

if ( range == null ) {
range = policy.getRange("kgcl");
}
if ( range == null ) {
range = policy.getRange("KGCL");
}
if ( range == null ) {
range = policy.getRange("ontobot");
}
if ( range == null ) {
range = policy.getRange("Ontobot");
}

if ( range != null ) {
String format = String.format("%s%%0%dd", policy.getPrefix(), policy.getWidth());
return new SimpleIDGenerator(ontology, format, range.getLowerBound(), range.getUpperBound());
} else if ( !silent ) {
throw new Exception("No range specified and no default range found in ID range file");
}
} catch ( IDRangePolicyException e ) {
if ( !silent ) {
throw new Exception("Cannot parse ID range policy file");
}
}

return null;
}

private String findIDRangeFile() {
FilenameFilter idRangeFilter = new FilenameFilter() {
@Override
public boolean accept(File file, String name) {
return name.endsWith("-idranges.owl");
}
};

File currentDir = new File(".");
String[] rangeFiles = currentDir.list(idRangeFilter);
if ( rangeFiles.length == 1 ) {
return rangeFiles[0];
}

return null;
}
}

0 comments on commit 9b3ac76

Please sign in to comment.