-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add PyCramKnowRobPrinter for Planning
- Loading branch information
1 parent
601242a
commit 1268aad
Showing
5 changed files
with
124 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/com/malte3d/suturo/knowledge/owl2anything/output/PyCramKnowRobPrinter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.malte3d.suturo.knowledge.owl2anything.output; | ||
|
||
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.regex.Pattern; | ||
|
||
/** | ||
* Utility class to create a .py file for the PyCRAM - KnowRob interface | ||
*/ | ||
@Slf4j | ||
@UtilityClass | ||
public class PyCramKnowRobPrinter { | ||
|
||
private static final Pattern SINGLE_QUOTE = Pattern.compile("'"); | ||
|
||
public static void print(@NonNull List<OwlRecord> classes, @NonNull File outputFile) { | ||
|
||
List<OwlRecord> output = PerceptionClassesFilter.filter(classes); | ||
|
||
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputFile), StandardCharsets.UTF_8)) { | ||
|
||
writer.append(generateCramObjectListString(output)); | ||
|
||
log.info("Successfully created {}", outputFile.getName()); | ||
|
||
} catch (IOException e) { | ||
log.error("Error while writing the Cram Object Names file", e); | ||
} | ||
} | ||
|
||
public static String generateCramObjectListString(@NonNull List<OwlRecord> classes) { | ||
|
||
|
||
List<OwlRecord> perceptionClasses = classes.stream() | ||
.filter(csvRecord -> csvRecord.getPerceptionId() != null) | ||
.sorted(Comparator.comparing(OwlRecord::getPerceptionId)) | ||
.toList(); | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
|
||
for (OwlRecord owlRecord : perceptionClasses) { | ||
|
||
sb.append(OwlRecordConverter.toPycramFormat(owlRecord)) | ||
.append(" = '").append(OwlRecordConverter.toKnowRobFormat(owlRecord)).append("'").append("\n"); | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
|
||
private static String escapeSingleQuotes(String literal) { | ||
return SINGLE_QUOTE.matcher(literal).replaceAll("\\\\'"); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/test/java/com/malte3d/suturo/knowledge/owl2anything/output/OwlRecordConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.malte3d.suturo.knowledge.owl2anything.output; | ||
|
||
import com.malte3d.suturo.knowledge.owl2anything.converter.OwlRecord; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class OwlRecordConverterTest { | ||
|
||
@ParameterizedTest | ||
@CsvSource({ | ||
"CerealBox, cereal_box", | ||
"ToyotaHSR, toyota_hsr", | ||
"JellOChocolatePuddingBox, jell_ochocolate_pudding_box", | ||
"Drawer, drawer" | ||
}) | ||
void toPycramFormat(String iriName, String pycramName) { | ||
OwlRecord record = OwlRecord.builder() | ||
// The actual test data | ||
.iriName(iriName) | ||
// and the other values with empty strings since lombok does not allow them to be null | ||
.iriNamespace("") | ||
.iriNamespaceShort("") | ||
.naturalName("") | ||
.description("") | ||
.build(); | ||
assertEquals(pycramName, OwlRecordConverter.toPycramFormat(record)); | ||
} | ||
} |