Skip to content

Commit

Permalink
Feat: Add PycramRoboKudoPrinter for planning
Browse files Browse the repository at this point in the history
  • Loading branch information
McModknower committed Jun 28, 2024
1 parent 1268aad commit c0c6024
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ out/

# owl2anything
owl2anything/output/*
*.owl
*.owl

### Emacs ###
# emacs backup files
*~
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ public static void main(String[] args) {
CramRoboKudoPrinter.print(owlRecords, new File(outputDir, "cram_robokudo.txt"));
CramKnowRobPrinter.print(owlRecords, new File(outputDir, "cram_knowrob.txt"));
PyCramKnowRobPrinter.print(owlRecords, new File(outputDir, "pycram_knowrob.py"));
PyCramRoboKudoPrinter.print(owlRecords, new File(outputDir, "pycram_robokudo.py"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,25 @@ public static String toPycramFormat(OwlRecord owlRecord) {
// aka remove the underscores not removed in the step before,
// since the left capital letter was already part of a replacement in that step
name = CAPITAL_LETTER_GROUP.matcher(name).replaceAll("$1$2");
// Lastly convert "_Cereal_Box" and "_Toyota_HSR" to "cereal_box" and "toyota_hsr"

// Lastly convert "_Cereal_Box" and "_Toyota_HSR" to "cereal_box" and "toyota_hsr"
// aka remove the underscore at the beginning (since all names start with a capital letter)
// and convert it to lower case
return name.substring(1).toLowerCase();

// since the current suturo_perception group made some object names without a capital letter at the start, only remove in that case.
if (name.startsWith("_")) {
name = name.substring(1);
}
return name.toLowerCase();
}

/**
* @param owlRecord the record to convert
* @return the record in the format needed for the CRAM - RoboKudo interface in Perception
*/
public static String toRoboKudoFormat(OwlRecord owlRecord) {
String lowerCaseName = owlRecord.getIriName().toLowerCase();
return lowerCaseName.substring(0, 1).toUpperCase() + lowerCaseName.substring(1);
String iriName = owlRecord.getIriName();
String restLowerCaseName = iriName.substring(1).toLowerCase(Locale.ROOT);
return iriName.charAt(0) + restLowerCaseName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.malte3d.suturo.knowledge.owl2anything.output;

import com.google.gson.Gson;
import com.malte3d.suturo.knowledge.owl2anything.converter.OwlRecord;
import lombok.NonNull;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.Comparator;
import java.util.List;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
* Utility class to create a .py file for the RoboKudo - PyCRAM - KnowRob interface
*/
@Slf4j
@UtilityClass
public class PyCramRoboKudoPrinter {

private static final Gson GSON = new Gson().newBuilder()
.enableComplexMapKeySerialization()
.disableHtmlEscaping()
.setPrettyPrinting()
.create();

public static void print(@NonNull List<OwlRecord> classes, @NonNull File outputFile) {

List<OwlRecord> output = PerceptionClassesFilter.filter(classes);

TreeMap<String, String> perceptionNames = output.stream()
.collect(Collectors.toMap(OwlRecordConverter::toRoboKudoFormat, OwlRecordConverter::toKnowRobFormat,
(c1, c2) -> {
throw new IllegalStateException(String.format("Duplicate robokudo name for classes %s and %s", c1, c2));
},
TreeMap::new));

try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputFile), StandardCharsets.UTF_8)) {

writer.append("mapping = ").append(GSON.toJson(perceptionNames));

log.info("Successfully created {}", outputFile.getName());

} catch (IOException e) {
log.error("Error while writing the Cram Object Names file", e);
}
}
}

0 comments on commit c0c6024

Please sign in to comment.