it.uniroma2.art.sheet2rdf.pearl.GraphPearlElementTripleBased Maven / Gradle / Ivy
package it.uniroma2.art.sheet2rdf.pearl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.rio.ntriples.NTriplesUtil;
import it.uniroma2.art.coda.structures.ValueOrString;
import it.uniroma2.art.sheet2rdf.utils.S2RDFUtils;
public class GraphPearlElementTripleBased extends GraphPearlElement {
private List triplesList;
public GraphPearlElementTripleBased(boolean optional) {
super(optional);
triplesList = new ArrayList();
}
/**
* Adds a graph triple to the GraphPearlElement
* @param subject
* @param predicate if null, the serialization will replace the predicate with a placeholder like
* <please_provide_a_predicate>
* @param object
*/
public void add(String subject, IRI predicate, ValueOrString object){
subject = "$"+subject;
triplesList.add(new GraphTriple(subject, predicate, object));
}
@Override
public String serialize(String tabs, Map prefixMapping){
String output = "";
for (int i=0; i it has not been collapsed to qname
predicateSerialization = NTriplesUtil.toNTriplesString(triple.getPredicate());
} else {
predicateSerialization = qname;
}
}
String objectSerialization = "";
ValueOrString object = triple.getObject();
if (object == null) {
objectSerialization = "%pls_provide_a_value%";
} else if (object.isValue()) {
Value objValue = object.getValue();
if (objValue instanceof IRI) {
//try to get the IRI as qname
String qname = S2RDFUtils.asQName((IRI)objValue, prefixMapping);
if (qname != null) {
objectSerialization = qname;
} else {
objectSerialization = NTriplesUtil.toNTriplesString(objValue);
}
}
} else {
objectSerialization = object.getString();
}
if (i==0){
if (optional) {
output = subject + " " + predicateSerialization + " " + objectSerialization + " .";
} else {
output = tabs +subject + " " + predicateSerialization + " " + objectSerialization + " .";
}
} else {
output = output + "\n";
if (optional) {
output = output + "\t";//if is the 2nd row of an optional, indent further
}
output = output + tabs +subject + " " + predicateSerialization + " " + objectSerialization + " .";
}
}
if (optional){
output = tabs + "OPTIONAL { " + output + " }";
}
return output;
}
private class GraphTriple{
private String subject;
private IRI predicate;
private ValueOrString object;
public GraphTriple(String subject, IRI predicate, ValueOrString object){
this.subject = subject;
this.predicate = predicate;
this.object = object;
}
public String getSubject() {
return subject;
}
public IRI getPredicate() {
return predicate;
}
public ValueOrString getObject() {
return object;
}
}
}