Skip to content

Instantly share code, notes, and snippets.

@virresh
Created March 14, 2020 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virresh/5a28dc3adb1f40bf9070e2cb4ecfa90d to your computer and use it in GitHub Desktop.
Save virresh/5a28dc3adb1f40bf9070e2cb4ecfa90d to your computer and use it in GitHub Desktop.
@base <http://example.org/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rel: <http://www.perceive.net/schemas/relationship/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<#spiderman> a foaf:Person ;
rel:enemyOf <#green-goblin> ;
foaf:name "Человек-паук"@ru , "Spiderman" .
<#green-goblin> a foaf:Person ;
rel:enemyOf <#spiderman> ;
foaf:name "Green Goblin" .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rel: <http://www.perceive.net/schemas/relationship/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<#spiderman> foaf:name "Человек-паук"@ru ;
foaf:name "Spiderman" ;
rdf:type foaf:Person ;
rel:enemyOf <#green-goblin> .
<#green-goblin> foaf:name "Green Goblin" ;
rdf:type foaf:Person ;
rel:enemyOf <#spiderman> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rel: <http://www.perceive.net/schemas/relationship/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix base: <http://example.org/> .
<http://example.org/#spiderman>
a foaf:Person ;
rel:enemyOf <http://example.org/#green-goblin> ;
foaf:name "Человек-паук"@ru , "Spiderman" .
<http://example.org/#green-goblin>
a foaf:Person ;
rel:enemyOf <http://example.org/#spiderman> ;
foaf:name "Green Goblin" .
@base <http://example.org/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rel: <http://www.perceive.net/schemas/relationship/> .
<#green-goblin>
rel:enemyOf <#spiderman> ;
a foaf:Person ; # in the context of the Marvel universe
foaf:name "Green Goblin" .
<#spiderman>
rel:enemyOf <#green-goblin> ;
a foaf:Person ;
foaf:name "Spiderman", "Человек-паук"@ru .
// Jena version related information mentioned in pom.xml
package mwa_jenabug;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.RDFFormat;
import org.apache.jena.riot.RDFWriter;
public class JenaBug {
public static Model read_rdfxml(String filename, String base) {
Model m = ModelFactory.createDefaultModel();
InputStream in = null;
try {
in = (InputStream) new FileInputStream(filename);
m.read(in, base, "RDF/XML");
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return m;
}
public static Model read_rdfttl(String filename, String base) {
Model m = ModelFactory.createDefaultModel();
InputStream in = null;
try {
in = (InputStream) new FileInputStream(filename);
m.read(in, base, "TTL");
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return m;
}
public static int write_as_ttlA(Model m, String filename, String base) {
OutputStream tout = null;
try {
tout = (OutputStream) new FileOutputStream(filename);
m.write(tout, "TURTLE", base);
if(tout != null) {
tout.close();
}
} catch (IOException e) {
e.printStackTrace();
return 1;
}
return 0;
}
public static int write_as_ttlB(Model m, String filename, String base) {
OutputStream tout = null;
try {
tout = (OutputStream) new FileOutputStream(filename);
RDFWriter.create()
.source(m)
.format(RDFFormat.TURTLE_BLOCKS)
.base(base)
.output(tout);
if(tout != null) {
tout.close();
}
} catch (IOException e) {
e.printStackTrace();
return 1;
}
return 0;
}
public static int write_as_ttlC(Model m, String filename, String base) {
OutputStream tout = null;
try {
tout = (OutputStream) new FileOutputStream(filename);
// no option to specify base !
// moreover adding "base" in the prefix namespace doesn't help
m.setNsPrefix("base", base);
RDFDataMgr.write((OutputStream) tout, m, RDFFormat.TTL);
if(tout != null) {
tout.close();
}
} catch (IOException e) {
e.printStackTrace();
return 1;
}
return 0;
}
/**
* TTL
* TURTLE
* TURTLE_PRETTY work fine
*
*
* TURTLE_FLAT
* TURTLE_BLOCKS don't output base
*/
public static void main(String[] args) {
String base = "http://example.org/";
Model m = read_rdfttl("./ggoblin.ttl", base);
write_as_ttlA(m, "./example07_A.ttl", base);
write_as_ttlB(m, "./example07_B.ttl", base);
write_as_ttlC(m, "./example07_C.ttl", base);
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mwa_jenabug</groupId>
<artifactId>mwa_jenabug</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>apache-jena-libs</artifactId>
<version>3.14.0</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment