use of org.eclipse.rdf4j.model.Statement in project opentheso by miledrousset.
the class ReadRdfFile_rdf4j method readFile.
// TODO add test methods here.
// The methods must be annotated with annotation @Test. For example:
//
@Test
public void readFile() {
File file = new File("/Users/Miled/Desktop/Bureau2/test_unesco.rdf");
RDFParser rdfParser = Rio.createParser(RDFFormat.RDFXML);
Model model = new LinkedHashModel();
rdfParser.setRDFHandler(new StatementCollector(model));
Rio.write(model, System.out, RDFFormat.TURTLE);
try {
rdfParser.parse(new FileReader(file), "http://example.org");
/* for (Statement statement : model) {
// writeLine(statement.getObject().stringValue(), statement.getSubject().stringValue());
System.out.println("LocalName = " + statement.getPredicate().getLocalName());
System.out.println("objet = " + statement.getObject().stringValue());
System.out.println("predicat = " + statement.getPredicate());
System.out.println("URI = " + statement.getSubject());
System.out.println("");
// model.getNamespace(statement.getClass().getgetObject().stringValue());
} */
for (Statement statement2 : model) {
Literal literal = (SimpleLiteral) statement2;
System.out.println(literal.getLabel());
// System.out.println(literal.getLanguage());
System.out.println(literal.getDatatype());
}
} catch (IOException e) {
// handle IO problems (e.g. the file could not be read)
} catch (RDFParseException e) {
// handle unrecoverable parse error
} catch (RDFHandlerException e) {
// handle a problem encountered by the RDFHandler
}
}
use of org.eclipse.rdf4j.model.Statement in project opentheso by miledrousset.
the class ReadRdfFileTest method test1.
public void test1() {
ModelBuilder builder = new ModelBuilder();
Model model = builder.setNamespace("ex", "http://example.org/").subject("ex:PotatoEaters").add("ex:creationDate", new GregorianCalendar(1885, Calendar.APRIL, 1).getTime()).add("ex:peopleDepicted", 5).build();
for (Statement st : model) {
IRI property = st.getPredicate();
Value value = st.getObject();
if (value instanceof Literal) {
Literal literal = (Literal) value;
// get the value of the literal directly as a Java primitive.
if (property.getLocalName().equals("peopleDepicted")) {
int peopleDepicted = literal.intValue();
System.out.println(peopleDepicted + " people are depicted in this painting");
} else if (property.getLocalName().equals("creationDate")) {
XMLGregorianCalendar calendar = literal.calendarValue();
Date date = calendar.toGregorianCalendar().getTime();
System.out.println("The painting was created on " + date);
}
// You can also just get the lexical value (a string) without
// worrying about the datatype
// System.out.println("Lexical value: '" + literal.getLabel() + "'");
}
}
}
use of org.eclipse.rdf4j.model.Statement in project opentheso by miledrousset.
the class ReadRdfFileTest method test2.
public static void test2() {
ValueFactory vf = SimpleValueFactory.getInstance();
ModelBuilder builder = new ModelBuilder();
Model model = builder.setNamespace("ex", "http://example.org/").subject("ex:PotatoEaters").add(DC.TITLE, vf.createLiteral("The Potato Eaters", "en")).add(DC.TITLE, vf.createLiteral("De Aardappeleters", "nl")).build();
// To see what's in our model, let's just print it to the screen
for (Statement st : model) {
// we want to see the object values of each statement
Value value = st.getObject();
if (value instanceof Literal) {
Literal title = (Literal) value;
System.out.println("language: " + title.getLanguage().orElse("unknown"));
System.out.println(" title: " + title.getLabel());
}
}
}
use of org.eclipse.rdf4j.model.Statement in project opentheso by miledrousset.
the class ReadRdfFileTest method test4.
@Test
public void test4() {
// read the file 'example-data-artists.ttl' as an InputStream.
InputStream is = null;
try {
is = new FileInputStream("test_unesco - Copie.rdf");
} catch (FileNotFoundException ex) {
Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
}
Model model = null;
try {
model = Rio.parse(is, "", RDFFormat.RDFXML);
} catch (IOException ex) {
Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (RDFParseException ex) {
Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedRDFormatException ex) {
Logger.getLogger(ReadRdfFileTest.class.getName()).log(Level.SEVERE, null, ex);
}
for (Statement statement : model) {
System.out.println(statement);
}
System.out.println("\n----------------");
for (Statement st : model) {
// we want to see the object values of each statement
Value value = st.getObject();
IRI property = st.getPredicate();
if (property.getLocalName().equals("type"))
System.out.println("\n......\n");
if (property.getLocalName().equals("notation")) {
Literal title = (Literal) value;
System.out.println("notation: " + title.getLabel());
} else if (property.getLocalName().equals("lat")) {
Literal title = (Literal) value;
System.out.println("latitude : " + title.getLabel());
} else if (property.getLocalName().equals("long")) {
Literal title = (Literal) value;
System.out.println("longitude : " + title.getLabel());
} else if (property.getLocalName().equals("created")) {
Literal title = (Literal) value;
System.out.println("created : " + title.getLabel());
} else if (property.getLocalName().equals("modified")) {
Literal title = (Literal) value;
System.out.println("modified : " + title.getLabel());
} else if (value instanceof Literal) {
String pref = "";
if (property.getLocalName().equals("prefLabel"))
pref = " // pref label";
Literal title = (Literal) value;
System.out.println("language: " + title.getLanguage().orElse("unknown"));
System.out.println(" title: " + title.getLabel() + pref);
} else {
System.out.println(" **** " + value + " //// " + property.getLocalName());
if (property.getLocalName().equals("type")) {
System.out.println(" URL: " + st.getSubject());
}
}
}
Rio.write(model, System.out, RDFFormat.RDFXML);
}
use of org.eclipse.rdf4j.model.Statement in project opentheso by miledrousset.
the class ReadRdf4j method readModel.
/**
* Permet de lire un fichier RDF précédament charger avec la fonction
* laodModel() les données sont stoqué dans la variable thesaurus
*/
private void readModel() {
ReadStruct readStruct = new ReadStruct();
readStruct.resource = null;
boolean validProperty = false;
// pour le debug :
ArrayList<String> nonReco = new ArrayList();
for (Statement st : model) {
readStruct.value = st.getObject();
readStruct.property = st.getPredicate();
if (readStruct.value instanceof Literal) {
readStruct.literal = (Literal) readStruct.value;
}
// Concept or ConceptScheme or Collection or ConceptGroup
if (readStruct.property.getLocalName().equals("type")) {
validProperty = false;
int prop = -1;
String type = readStruct.value.toString();
type = type.toUpperCase();
if (type.contains("ConceptScheme".toUpperCase())) {
prop = SKOSProperty.ConceptScheme;
sKOSXmlDocument.setTitle(st.getSubject().stringValue());
validProperty = true;
} else if (type.contains("ConceptGroup".toUpperCase())) {
prop = SKOSProperty.ConceptGroup;
validProperty = true;
} else if (type.contains("Theme".toUpperCase())) {
prop = SKOSProperty.Theme;
validProperty = true;
} else if (type.contains("MicroThesaurus".toUpperCase())) {
prop = SKOSProperty.MicroThesaurus;
validProperty = true;
} else if (type.contains("Collection".toUpperCase())) {
prop = SKOSProperty.Collection;
validProperty = true;
} else if (type.contains("Concept".toUpperCase())) {
prop = SKOSProperty.Concept;
validProperty = true;
}
if (validProperty) {
String uri = st.getSubject().stringValue();
// System.out.println("URI = " + uri);
readStruct.resource = new SKOSResource(uri, prop);
if (prop == SKOSProperty.ConceptScheme) {
sKOSXmlDocument.setConceptScheme(readStruct.resource);
} else if (prop == SKOSProperty.ConceptGroup || prop == SKOSProperty.Collection || prop == SKOSProperty.Theme || prop == SKOSProperty.MicroThesaurus) {
sKOSXmlDocument.addGroup(readStruct.resource);
} else if (prop == SKOSProperty.Concept) {
sKOSXmlDocument.addconcept(readStruct.resource);
} else {
logger.info("This is how you configure Java Logging with SLF4J");
// System.out.println("Erreur de type : " + prop);
}
}
} else // Labelling Properties
if (readLabellingProperties(readStruct)) {
if (readStruct.resource == null)
readStruct.resource = new SKOSResource();
// Dates
if (readDate(readStruct)) {
// Semantic Relationships
if (readRelationships(readStruct)) {
// Documentation Properties
if (readDocumentation(readStruct)) {
if (readCreator(readStruct)) {
if (readGPSCoordinates(readStruct)) {
if (readNotation(readStruct)) {
if (readIdentifier(readStruct)) {
if (readMatch(readStruct)) {
// debug
if (!nonReco.contains(readStruct.property.getLocalName())) {
// System.out.println("non reconue : " + readStruct.property.getLocalName());
nonReco.add(readStruct.property.getLocalName());
}
}
}
}
}
}
}
}
}
}
}
if (!nonReco.isEmpty()) {
String balises = "";
for (String b : nonReco) {
balises += " " + b + "\n";
}
// fileBean.setWarning("Not readed RDF tag\n" + balises);
message = message + "Not readed RDF tag\n" + balises;
}
}
Aggregations