-
Couldn't load subscription status.
- Fork 5
Changed format how to save scripts #427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ec4a0b3
Add custom ttl serialiser
872e205
Improve sorting readability
156cc71
Remove usage of model
17cb5aa
Fix incorrect ident
bf4420c
Improve the test
49e1968
Expand the test data to ensure correct writing of the blank nodes.
4ed24dd
Improve the formatting
270f16b
Improve the test data
e080a45
Changed delegate to NodeFormatterTTL_Multiline
81602c6
Remove formatLiteral as obsolete
e7e7db8
Rename NodeFormatter
9180570
Introduce Javadoc
60f5df1
Improve documentation
blcham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
s-pipes-core/src/main/java/cz/cvut/spipes/util/CustomLangs.java
This file contains hidden or 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,12 @@ | ||
| package cz.cvut.spipes.util; | ||
|
|
||
| import org.apache.jena.riot.*; | ||
|
|
||
| public class CustomLangs { | ||
| public static final Lang SPIPES_TURTLE = LangBuilder.create("SPIPES-TURTLE", "text/spipes+turtle").build(); | ||
| public static final RDFFormat SPIPES_FORMAT = new RDFFormat(SPIPES_TURTLE); | ||
|
|
||
| static { | ||
| RDFWriterRegistry.register(SPIPES_FORMAT, (WriterGraphRIOTFactory) (lang) -> new SPipesTurtleWriter()); | ||
| } | ||
| } |
This file contains hidden or 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
223 changes: 223 additions & 0 deletions
223
s-pipes-core/src/main/java/cz/cvut/spipes/util/SPipesFormatter.java
This file contains hidden or 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,223 @@ | ||
| package cz.cvut.spipes.util; | ||
|
|
||
| import org.apache.jena.atlas.io.IndentedWriter; | ||
| import org.apache.jena.graph.Graph; | ||
| import org.apache.jena.graph.Node; | ||
| import org.apache.jena.graph.Triple; | ||
| import org.apache.jena.riot.system.PrefixMap; | ||
| import org.apache.jena.riot.system.PrefixMapFactory; | ||
| import org.apache.jena.vocabulary.OWL; | ||
| import org.apache.jena.vocabulary.RDF; | ||
| import org.apache.jena.atlas.io.AWriter; | ||
|
|
||
| import java.io.OutputStream; | ||
| import java.util.*; | ||
|
|
||
| import static org.apache.jena.riot.system.RiotLib.writePrefixes; | ||
|
|
||
| /** | ||
| * Formats an RDF graph into Turtle syntax with custom structure and blank node handling. | ||
| * Controls subject ordering, predicate sorting, and punctuation. | ||
| * Uses {@link SPipesNodeFormatterTTL} for node-level formatting. | ||
| * | ||
| * <h3>Formatting Rules:</h3> | ||
| * <ul> | ||
| * <li>Subject blocks end with {@code .} on a separate line (see {@link #writeTriples})</li> | ||
| * <li>Subject order: ontology, URIs, blank nodes (see {@link #sortSubjects})</li> | ||
| * <li>Type declaration using {@code a} comes first in each subject block | ||
| * (see {@link SPipesNodeFormatterTTL#PRED_ORDER})</li> | ||
| * <li>Multiline literals are formatted by default using {@code """}. | ||
| * If the string contains {@code "} but not {@code '}, then {@code '''} is used instead | ||
| * (see {@link org.apache.jena.riot.out.NodeFormatterTTL_MultiLine})</li> | ||
| * </ul> | ||
| * | ||
| * <h3>Example TTL Output:</h3> | ||
| * <pre> | ||
| * :construct-greeting | ||
| * a sml:ApplyConstruct ; | ||
| * sm:next :express-greeting_Return ; | ||
| * sml:constructQuery [ a sp:Construct ; sp:text ''' | ||
| * CONSTRUCT { | ||
| * <http://example.com/person1> :is-greeted-by-message ?greetingMessage . | ||
| * } WHERE { | ||
| * BIND(concat("Hello world") as ?greetingMessage) | ||
| * } | ||
| * ''' ; ] ; | ||
| * sml:replace true ; | ||
| * . | ||
| * </pre> | ||
| */ | ||
| public class SPipesFormatter { | ||
|
|
||
| private final Graph graph; | ||
| private final Map<String, String> ns; | ||
| private final Map<Node, Map<Node, List<Node>>> subjectMap = new LinkedHashMap<>(); | ||
| private final Map<String, Integer> inDegree = new HashMap<>(); | ||
| private final Map<String, String> bnodeLabels = new LinkedHashMap<>(); | ||
| private int bCounter = 0; | ||
|
|
||
| private final SPipesNodeFormatterTTL nodeFormatter; | ||
|
|
||
| public SPipesFormatter(Graph graph, PrefixMap prefixMap) { | ||
| this.graph = graph; | ||
| this.ns = new LinkedHashMap<>(prefixMap.getMapping()); | ||
| this.nodeFormatter = new SPipesNodeFormatterTTL(graph, ns, inDegree, bnodeLabels); | ||
| buildSubjectMap(); | ||
| assignBNodeLabels(); | ||
| } | ||
|
|
||
| /** | ||
| * Assigns stable labels to blank nodes that are referenced more than once. | ||
| * Labels are stored in {@code bnodeLabels} as {@code _:b0}, {@code _:b1}, etc. | ||
| */ | ||
| private void assignBNodeLabels() { | ||
| for (Node subj : subjectMap.keySet()) { | ||
| if (subj.isBlank() && inDegreeOf(subj) > 1) { | ||
| bnodeLabels.put(subj.getBlankNodeLabel(), "_:b" + (bCounter++)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Builds a nested map of triples: {@code subject → predicate → list of objects}. | ||
| * Also tracks in-degree of blank nodes (how often they appear as objects). | ||
| */ | ||
| private void buildSubjectMap() { | ||
| Iterator<Triple> it = graph.find(); | ||
| while (it.hasNext()) { | ||
| Triple t = it.next(); | ||
| Node s = t.getSubject(), p = t.getPredicate(), o = t.getObject(); | ||
| subjectMap.computeIfAbsent(s, k -> new LinkedHashMap<>()) | ||
| .computeIfAbsent(p, k -> new ArrayList<>()).add(o); | ||
| if (o.isBlank()) inDegree.merge(o.getBlankNodeLabel(), 1, Integer::sum); | ||
| } | ||
| } | ||
|
|
||
| private int inDegreeOf(Node n) { return n.isBlank() ? inDegree.getOrDefault(n.getBlankNodeLabel(), 0) : 0; } | ||
| private boolean hasLabel(Node n) { return n.isBlank() && bnodeLabels.containsKey(n.getBlankNodeLabel()); } | ||
|
|
||
| /** | ||
| * Entry point for serialising the graph to Turtle. | ||
| * Writes prefix declarations and serialises all triples. | ||
| * | ||
| * @param out the output stream to write to | ||
| */ | ||
| public void writeTo(OutputStream out) { | ||
| try(IndentedWriter aw = new IndentedWriter(out)){ | ||
| PrefixMap prefixMap = PrefixMapFactory.createForOutput(); | ||
| ns.forEach(prefixMap::add); | ||
|
|
||
| writePrefixes(aw, prefixMap); | ||
| aw.println(); | ||
|
|
||
| writeTriples(aw); | ||
| aw.flush(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Serializes all triples in the graph. | ||
| * Subjects are sorted using {@code SUBJECT_COMPARATOR}. | ||
| * Skips blank nodes that are referenced elsewhere and not labelled. | ||
| * Each subject block ends with {@code .} and a newline. | ||
| * | ||
| * @param w the writer to output to | ||
| */ | ||
| private void writeTriples(AWriter w) { | ||
| List<Node> subjects = sortSubjects(new ArrayList<>(subjectMap.keySet())); | ||
|
|
||
| for (Node subject : subjects) { | ||
| if (subject.isBlank() && !hasLabel(subject) && inDegreeOf(subject) >= 1) continue; | ||
|
|
||
| nodeFormatter.formatNode(w, subject, null); | ||
| w.println(); | ||
|
|
||
| Map<Node, List<Node>> predMap = new TreeMap<>(SPipesNodeFormatterTTL.PRED_ORDER); | ||
| predMap.putAll(subjectMap.getOrDefault(subject, Collections.emptyMap())); | ||
|
|
||
| if (!predMap.isEmpty()) { | ||
| writePredicates(w, predMap); | ||
| } | ||
| w.println(".\n"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Defines categories for sorting subjects: | ||
| * - {@code ONTOLOGY}: subjects typed as {@code owl:Ontology} | ||
| * - {@code URI}: subjects with URIs | ||
| * - {@code LABELED_BNODE}: blank nodes with assigned labels | ||
| * - {@code OTHER}: all other blank nodes | ||
| */ | ||
| private enum NodeCategory { | ||
| ONTOLOGY, URI, LABELED_BNODE, OTHER | ||
| } | ||
|
|
||
| /** | ||
| * Determines the {@link NodeCategory} of a node for sorting purposes. | ||
| * | ||
| * @param n the node to categorise | ||
| * @return the category of the node | ||
| */ | ||
| private NodeCategory category(Node n) { | ||
| Map<Node, List<Node>> preds = subjectMap.get(n); | ||
| if (preds != null && preds.getOrDefault(RDF.type.asNode(), List.of()) | ||
| .contains(OWL.Ontology.asNode())) { | ||
| return NodeCategory.ONTOLOGY; | ||
| } | ||
| if (n.isURI()) return NodeCategory.URI; | ||
| if (hasLabel(n)) return NodeCategory.LABELED_BNODE; | ||
| return NodeCategory.OTHER; | ||
| } | ||
|
|
||
| // URIs first, then labelled bnodes, then other bnodes | ||
| private final Comparator<Node> SUBJECT_COMPARATOR = | ||
| Comparator.comparing(this::category) | ||
| .thenComparing(n -> n.isURI() ? n.getURI() : "") | ||
| .thenComparing(n -> hasLabel(n) ? bnodeLabels.get(n.getBlankNodeLabel()) : ""); | ||
|
|
||
| /** | ||
| * Sorts subjects using {@code SUBJECT_COMPARATOR}, which prioritizes: | ||
| * - Ontologies | ||
| * - URIs | ||
| * - Labelled blank nodes | ||
| * - Other blank nodes | ||
| * | ||
| * @param subjects the list of subjects to sort | ||
| * @return the sorted list | ||
| */ | ||
| private List<Node> sortSubjects(List<Node> subjects) { | ||
| subjects.sort(SUBJECT_COMPARATOR); | ||
| return subjects; | ||
| } | ||
|
|
||
| /** | ||
| * Serializes all predicate–object pairs for a given subject. | ||
| * Predicates are sorted using {@code PRED_ORDER}. | ||
| * Uses {@link SPipesNodeFormatterTTL#formatPredicate} to format predicates. | ||
| * Objects are separated by commas ({@code ,}), predicates by semicolons ({@code ;}). | ||
| * | ||
| * @param w the writer to output to | ||
| * @param predMap the predicate–object map for the subject | ||
| */ | ||
| private void writePredicates(AWriter w, Map<Node, List<Node>> predMap) { | ||
| for (Map.Entry<Node, List<Node>> e : predMap.entrySet()) { | ||
| Node pred = e.getKey(); | ||
|
|
||
| w.print(" "); | ||
| nodeFormatter.formatPredicate(w, pred); | ||
| w.print(" "); | ||
|
|
||
| Iterator<Node> it = e.getValue().iterator(); | ||
| while (it.hasNext()) { | ||
| nodeFormatter.formatNode(w, it.next(), new HashSet<>()); | ||
| if (it.hasNext()) { | ||
| w.print(" ,\n "); | ||
| } else { | ||
| w.print(" ;"); | ||
| } | ||
| } | ||
| w.println(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.