Commit ec62e4a1 authored by Yasen Marinov's avatar Yasen Marinov

Pretty print the TTL and treat .ttl input as shacl

parent 9a4affc6
No preview for this file type
......@@ -2,22 +2,31 @@ package com.ontotext.shaclconver;
import static org.apache.jena.riot.Lang.TURTLE;
import org.apache.commons.io.output.WriterOutputStream;
import org.apache.jena.base.Sys;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.RDFReader;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.RDFFormat;
import org.apache.jena.riot.RDFParserRegistry;
import org.apache.jena.riot.system.PrefixMapStd;
import org.apache.jena.riot.system.StreamRDFWriter;
import org.topbraid.shacl.compact.SHACLCReader;
import org.topbraid.shacl.compact.SHACLCWriter;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
/**
* @author Yasen Marinov
......@@ -26,6 +35,9 @@ import java.nio.file.Paths;
public class ShaclConvert {
public static final String BASE = "http://example.org/";
public static final String SHACL = ".shacl";
public static final String TTL = ".ttl";
public static final String SHACLC = ".shaclc";
String inputFile;
String outputFile;
......@@ -35,23 +47,34 @@ public class ShaclConvert {
}
public void convert() throws IOException {
if (inputFile.toLowerCase().endsWith(".shacl")) {
String inputFileName = inputFile.toLowerCase();
if (inputFileName.endsWith(SHACL) || inputFileName.endsWith(TTL)) {
System.out.println("Converting Shacl to ShaclC");
normalToCompact();
} else {
} else if (inputFileName.endsWith(SHACLC)) {
System.out.println("Converting ShaclC to Shacl");
compactToNormal();
} else {
System.err.println(
"Input file should be one of the following: " + Arrays.asList(TTL, SHACL, SHACLC));
System.exit(1);
}
}
private void compactToNormal() throws IOException {
SHACLCReader shaclcReader = new SHACLCReader();
StringWriter writer = new StringWriter();
OutputStream os = new WriterOutputStream(writer, Charset.defaultCharset());
shaclcReader.read(
readFile(inputFile),
BASE,
TURTLE,
StreamRDFWriter.getWriterStream(getOutputStream(), TURTLE),
null);
readFile(inputFile), BASE, TURTLE, StreamRDFWriter.getWriterStream(os, TURTLE), null);
Model model = ModelFactory.createDefaultModel();
model.read(new StringReader(writer.toString()), BASE, "TTL");
RDFDataMgr.write(getOutputStream(), model, RDFFormat.TURTLE_PRETTY);
}
private void normalToCompact() throws IOException {
......
......@@ -34,6 +34,14 @@ public class ShaclConvertTest {
test(inputFile, outputFile);
}
@Test
void normalToCompact2() throws IOException {
String inputFile = "./src/test/resources/input.ttl";
String outputFile = "./src/test/resources/output.shaclc";
test(inputFile, outputFile);
}
private void test(String inputFile, String outputFile) throws IOException {
Path tmp = Files.createTempFile("tmp", "file");
try {
......
BASE <http://example.com/ns>
IMPORTS <http://example.com/person-ontology>
PREFIX ex: <http://example.com/ns#>
shape ex:PersonShape -> ex:Person {
closed=true ignoredProperties=[rdf:type] .
ex:ssn xsd:string [0..1] pattern="^\\d{3}-\\d{2}-\\d{4}$" .
ex:worksFor IRI ex:Company [0..*] .
ex:address BlankNode [0..1] {
ex:city xsd:string [1..1] .
ex:postalCode xsd:integer|xsd:string [1..1] maxLength=5 .
} .
closed=true ignoredProperties=[rdf:type] .
ex:ssn xsd:string [0..1] pattern="^\\d{3}-\\d{2}-\\d{4}$" .
ex:worksFor IRI ex:Company [0..*] .
ex:address BlankNode [0..1] {
ex:city xsd:string [1..1] .
ex:postalCode xsd:integer|xsd:string [1..1] maxLength=5 .
} .
}
\ No newline at end of file
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ex: <http://example.com/ns#> .
ex:PersonShape rdf:type sh:NodeShape ;
sh:targetClass ex:Person ;
sh:closed true .
_:b0 rdf:first rdf:type ;
rdf:rest rdf:nil .
ex:PersonShape sh:ignoredProperties _:b0 ;
sh:property _:b1 .
_:b1 sh:path ex:ssn ;
sh:maxCount 1 ;
sh:datatype xsd:string ;
sh:pattern "^\\d{3}-\\d{2}-\\d{4}$" .
ex:PersonShape sh:property _:b2 .
_:b2 sh:path ex:worksFor ;
sh:nodeKind sh:IRI ;
sh:class ex:Company .
ex:PersonShape sh:property _:b3 .
_:b3 sh:path ex:address ;
sh:maxCount 1 ;
sh:nodeKind sh:BlankNode ;
sh:node _:b4 .
_:b4 sh:property _:b5 .
_:b5 sh:path ex:city ;
sh:minCount 1 ;
sh:maxCount 1 ;
sh:datatype xsd:string .
_:b4 sh:property _:b6 .
_:b6 sh:path ex:postalCode ;
sh:minCount 1 ;
sh:maxCount 1 .
_:b7 sh:datatype xsd:integer .
_:b8 sh:datatype xsd:string .
_:b9 rdf:first _:b7 .
_:b10 rdf:first _:b8 ;
rdf:rest rdf:nil .
_:b9 rdf:rest _:b10 .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix ex: <http://example.com/ns#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ex: <http://example.com/ns#> .
ex:PersonShape rdf:type sh:NodeShape ;
sh:targetClass ex:Person ;
sh:closed true .
_:b0 rdf:first rdf:type ;
rdf:rest rdf:nil .
ex:PersonShape sh:ignoredProperties _:b0 ;
sh:property _:b1 .
_:b1 sh:path ex:ssn ;
sh:maxCount 1 ;
sh:datatype xsd:string ;
sh:pattern "^\\d{3}-\\d{2}-\\d{4}$" .
ex:PersonShape sh:property _:b2 .
_:b2 sh:path ex:worksFor ;
sh:nodeKind sh:IRI ;
sh:class ex:Company .
ex:PersonShape sh:property _:b3 .
_:b3 sh:path ex:address ;
sh:maxCount 1 ;
sh:nodeKind sh:BlankNode ;
sh:node _:b4 .
_:b4 sh:property _:b5 .
_:b5 sh:path ex:city ;
sh:minCount 1 ;
sh:maxCount 1 ;
sh:datatype xsd:string .
_:b4 sh:property _:b6 .
_:b6 sh:path ex:postalCode ;
sh:minCount 1 ;
sh:maxCount 1 .
_:b7 sh:datatype xsd:integer .
_:b8 sh:datatype xsd:string .
_:b9 rdf:first _:b7 .
_:b10 rdf:first _:b8 ;
rdf:rest rdf:nil .
_:b9 rdf:rest _:b10 .
ex:PersonShape a sh:NodeShape ;
sh:closed true ;
sh:ignoredProperties ( rdf:type ) ;
sh:property [ sh:maxCount 1 ;
sh:node [ sh:property [ sh:maxCount 1 ;
sh:minCount 1 ;
sh:path ex:postalCode
] ;
sh:property [ sh:datatype xsd:string ;
sh:maxCount 1 ;
sh:minCount 1 ;
sh:path ex:city
] ] ;
sh:nodeKind sh:BlankNode ;
sh:path ex:address
] ;
sh:property [ sh:class ex:Company ;
sh:nodeKind sh:IRI ;
sh:path ex:worksFor
] ;
sh:property [ sh:datatype xsd:string ;
sh:maxCount 1 ;
sh:path ex:ssn ;
sh:pattern "^\\d{3}-\\d{2}-\\d{4}$"
] ;
sh:targetClass ex:Person .
[ rdf:first _:b0 ; rdf:rest ( [ sh:datatype xsd:string ]
) ] .
_:b1 rdf:first _:b0 ;
rdf:rest _:b2 .
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment