You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Last month, I posted a question of how to access specific rdf:Bag sections for an RDF within the XMP section of JPEG images (XMP is RDF/XML). I did not get a reply but have eventually figured it out myself. Section 6 "Working with rdf](rdf:Alt, rdf:Bag, rdf:Seq)" does not really provide much help for this type of issue. It works, but I am still not 100% sure this is the best way to do it. Looking at other messages, not many people are using RDF4J in this way.
I have put a few examples together for adding a rdf:Bag structure to a model and then updating the model. If helpful, I would be happy to make them more generic and contribute to the documentation in this area.
Here is the code to add the structure, (if it does not already exist) using ModelBuilder:
BNode locationNode = bnode();
BNode bagNode = bnode();
ModelBuilder builder = new ModelBuilder();
builder
.setNamespace("rdf",RDF.NAMESPACE)
.setNamespace(IPTC_E_PREFIX,IPTC_E_NAMESPACE)
.setNamespace(IPTC_C_PREFIX,IPTC_C_NAMESPACE)
.defaultGraph()
.subject(IRI_RESOURCE)
.add(IRI_LOCATIONCREATED, locationNode)
.subject(IRI_LOCATIONCREATED)
.subject(locationNode)
.add(RDF.TYPE,RDF.BAG)
.add(BAG_1,bagNode);
builder.namedGraph(BAG_1)
.subject(bagNode)
.add(IRI_E_COUNTRY,"")
.add(IRI_E_COUNTRYCODE,"")
.add(IRI_E_STATE,"")
.add(IRI_E_SUBLOCATION,"")
.add(IRI_E_CITY,"")
.add(IRI_E_WORLDREGION,"");
Model mm = builder.build();
//add to the model
m.addAll(mm);
//add namespaces
mm.getNamespaces().stream()
.filter(ns -> !m.getNamespace(ns.getPrefix()).isPresent())
.forEach(m::setNamespace);
Rio.write(m, System.out, RDFFormat.RDFXML);
To retrieve a value is relatively simple in my case, as there is only ever one LI item in the list (according to XMP specification.) , so we can just filter on the Predicate 👍
Model iriFilter=model.filter(null,IRI_E_COUNTRY,null);
To update the structure, however, we have to find the correct locationNode and Bag node:
Model locationFilter = m.filter(IRI_RESOURCE, IRI_LOCATIONCREATED, null);
Resource locationNode = objectResource(locationFilter).orElse(null);
ValueFactory vf = SimpleValueFactory.getInstance();
if(locationNode != null) {
Model bagFilter = m.filter(locationNode,null, null);
// there may be multiple values - only one is supported by IPTC/XMP
int count =0;
for(Statement bags : bagFilter)
{
if(!bags.getObject().equals(RDF.BAG))
{
Resource bagNode=(Resource) bags.getObject();
Model valueFilter = m.filter(bagNode,IRI_E_CITY, null);
// code to replace based on the filter :
}
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Last month, I posted a question of how to access specific rdf:Bag sections for an RDF within the XMP section of JPEG images (XMP is RDF/XML). I did not get a reply but have eventually figured it out myself. Section 6 "Working with rdf](rdf:Alt, rdf:Bag, rdf:Seq)" does not really provide much help for this type of issue. It works, but I am still not 100% sure this is the best way to do it. Looking at other messages, not many people are using RDF4J in this way.
I have put a few examples together for adding a rdf:Bag structure to a model and then updating the model. If helpful, I would be happy to make them more generic and contribute to the documentation in this area.
I wanted to update the following structure:
Once read in by RDF4J - it is represented like this in XML :
Here is the code to add the structure, (if it does not already exist) using ModelBuilder:
To retrieve a value is relatively simple in my case, as there is only ever one LI item in the list (according to XMP specification.) , so we can just filter on the Predicate 👍
Model iriFilter=model.filter(null,IRI_E_COUNTRY,null);
To update the structure, however, we have to find the correct locationNode and Bag node:
Hope this is useful !
Beta Was this translation helpful? Give feedback.
All reactions