private String getNSPrefixString(Model model) { Map<String, String> nsPrefix = model.getNsPrefixMap(); Set<String> prefixSet = nsPrefix.keySet(); Iterator<String> it = prefixSet.iterator(); String sparqlPrefix = ""; while (it.hasNext()) { String prefix = it.next(); try { String uri = nsPrefix.get(prefix); uri = uri.replace("\\", "/"); sparqlPrefix += "PREFIX " + prefix + ": <" + (new URI(uri).toString()) + ">" + System.getProperty("line.separator"); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return sparqlPrefix; }
/** * @param model Input Model * @param lang See {@link com.hp.hpl.jena.rdf.model.Model#read(java.io.InputStream, String, * String)} * @param out Serialized model * @throws JenaException */ public static void serializeModel(Model model, String lang, OutputStream out) throws JenaException { if (model == null) throw new IllegalArgumentException( Messages.getServerString("model.util.model.null")); // $NON-NLS-1$ if (out == null) throw new IllegalArgumentException( Messages.getServerString("model.util.outputstream.null")); // $NON-NLS-1$ // Avoid 'WARN com.hp.hpl.jena.xmloutput.impl.BaseXMLWriter - Namespace prefix 'j.1' is // reserved by Jena.' for (String prefix : new HashSet<String>(model.getNsPrefixMap().keySet())) { model.removeNsPrefix(prefix); } model.write(out, lang); }
/* (non-Javadoc) * @see org.fcrepo.kernel.api.models.FedoraResource#replaceProperties * (org.fcrepo.kernel.api.identifiers.IdentifierConverter, com.hp.hpl.jena.rdf.model.Model) */ @Override public void replaceProperties( final IdentifierConverter<Resource, FedoraResource> idTranslator, final Model inputModel, final RdfStream originalTriples) throws MalformedRdfException { final RdfStream replacementStream = new RdfStream() .namespaces(inputModel.getNsPrefixMap()) .topic(idTranslator.reverse().convert(this).asNode()); final GraphDifferencingIterator differencer = new GraphDifferencingIterator(inputModel, originalTriples); final StringBuilder exceptions = new StringBuilder(); try { new RdfRemover(idTranslator, getSession(), replacementStream.withThisContext(differencer)) .consume(); } catch (final ConstraintViolationException e) { throw e; } catch (final MalformedRdfException e) { exceptions.append(e.getMessage()); exceptions.append("\n"); } try { new RdfAdder( idTranslator, getSession(), replacementStream.withThisContext(differencer.notCommon())) .consume(); } catch (final ConstraintViolationException e) { throw e; } catch (final MalformedRdfException e) { exceptions.append(e.getMessage()); } if (exceptions.length() > 0) { throw new MalformedRdfException(exceptions.toString()); } }
public void createJenaModel(RegisterContextRequest rcr) { OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); Model entityOnt = FileManager.get().loadModel(ONT_FILE); ontModel.addSubModel(entityOnt); ontModel.setNsPrefixes(entityOnt.getNsPrefixMap()); // ontModel.loadImports(); ExtendedIterator<OntProperty> iter = ontModel.listAllOntProperties(); while (iter.hasNext()) { OntProperty ontProp = iter.next(); System.out.println(ontProp.getLocalName()); // if (formParams.containsKey(ontProp.getLocalName())) { // regIndividual.addProperty(ontProp, // ontModel.getcreateTypedLiteral(formParams.get(ontProp.getLocalName())[0])); // } } // OntClass regClass = ontModel.getOntClass(ONT_URL + "iotReg"); // OntClass entClass = ontModel.createOntClass(ONT_URL + "entity"); // OntClass regClass = (OntClass) ontModel.createOntResource(OntClass.class, // null,ONT_URL+"Registration" ); // OntClass regClass = (OntClass) ontModel.createClass(ONT_URL + "Registration"); // OntClass entityClass = (OntClass) ontModel.createClass(ONT_URL + "Entity"); OntClass entityGroup = (OntClass) ontModel.getOntClass(ONT_URL + "EntityGroup"); OntClass entity = (OntClass) ontModel.getOntClass(ONT_URL + "Entity"); OntClass attribute = (OntClass) ontModel.getOntClass(ONT_URL + "Attribute"); OntClass metadata = (OntClass) ontModel.getOntClass(ONT_URL + "Metadata"); OntClass location = (OntClass) ontModel.getOntClass(entityOnt.getNsPrefixURI("geo") + "Point"); // System.out.println("Class type is: " + regClass.getLocalName()); String ngsiValue = ""; ngsiValue = rcr.getRegistrationId(); Individual entityGroupIndiv = ontModel.createIndividual(ONT_URL + ngsiValue, entityGroup); entityGroupIndiv.setPropertyValue( ontModel.getProperty(ONT_URL + "registrationId"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getTimestamp().toString(); entityGroupIndiv.setPropertyValue( ontModel.getProperty(ONT_URL + "regTimeStamp"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getDuration(); entityGroupIndiv.setPropertyValue( ontModel.getProperty(ONT_URL + "duration"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getContextRegistration().get(0).getEntityId().get(0).getId(); Individual entity1 = ontModel.createIndividual(ONT_URL + ngsiValue, entity); entityGroupIndiv.setPropertyValue(ontModel.getProperty(ONT_URL + "hasEntity"), entity1); ngsiValue = rcr.getContextRegistration().get(0).getEntityId().get(0).getId(); entity1.setPropertyValue( ontModel.getProperty(ONT_URL + "id"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getContextRegistration().get(0).getEntityId().get(0).getType(); entity1.setPropertyValue( ontModel.getProperty(ONT_URL + "type"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getContextRegistration().get(0).getContextRegistrationAttribute().get(0).getName(); Individual attribute1 = ontModel.createIndividual(ONT_URL + ngsiValue, attribute); entity1.setPropertyValue(ontModel.getProperty(ONT_URL + "hasAttribute"), attribute1); ngsiValue = rcr.getContextRegistration().get(0).getContextRegistrationAttribute().get(0).getType(); attribute1.setPropertyValue( ontModel.getProperty(ONT_URL + "type"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getContextRegistration() .get(0) .getContextRegistrationAttribute() .get(0) .getContextMetadata() .get(0) .getName(); Individual metadata1 = ontModel.createIndividual(ONT_URL + ngsiValue, metadata); attribute1.setPropertyValue(ontModel.getProperty(ONT_URL + "hasMetadata"), metadata1); ngsiValue = rcr.getContextRegistration() .get(0) .getContextRegistrationAttribute() .get(0) .getContextMetadata() .get(0) .getType(); metadata1.setPropertyValue( ontModel.getProperty(ONT_URL + "type"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getContextRegistration() .get(0) .getContextRegistrationAttribute() .get(0) .getContextMetadata() .get(0) .getValue() .toString(); metadata1.setPropertyValue( ontModel.getProperty(ONT_URL + "value"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getContextRegistration() .get(0) .getContextRegistrationAttribute() .get(0) .getContextMetadata() .get(1) .getName(); Individual metadata2 = ontModel.createIndividual(ONT_URL + ngsiValue, metadata); attribute1.addProperty(ontModel.getProperty(ONT_URL + "hasMetadata"), metadata2); ngsiValue = rcr.getContextRegistration() .get(0) .getContextRegistrationAttribute() .get(0) .getContextMetadata() .get(1) .getType(); metadata2.setPropertyValue( ontModel.getProperty(ONT_URL + "type"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getContextRegistration() .get(0) .getContextRegistrationAttribute() .get(0) .getContextMetadata() .get(1) .getValue() .toString(); metadata2.setPropertyValue( ontModel.getProperty(ONT_URL + "value"), ontModel.createLiteral(ngsiValue)); // ngsiValue = // rcr.getContextRegistration().get(0).getContextRegistrationAttribute().get(0).getContextMetadata().get(2).getName(); // Individual metadata3 = ontModel.createIndividual(ONT_URL + ngsiValue, metadata); // attribute1.addProperty(ontModel.getProperty(ONT_URL + "hasMetadata"), metadata3); // // ngsiValue = // rcr.getContextRegistration().get(0).getContextRegistrationAttribute().get(0).getContextMetadata().get(2).getType(); // metadata3.setPropertyValue(ontModel.getProperty(ONT_URL + "type"), // ontModel.createLiteral(ngsiValue)); // ngsiValue = // rcr.getContextRegistration().get(0).getContextRegistrationAttribute().get(0).getContextMetadata().get(2).getValue().toString(); // metadata3.setPropertyValue(ontModel.getProperty(ONT_URL + "value"), // ontModel.createLiteral(ngsiValue)); // System.out.println("has propertry // \"expiry\":"+entityIndiv.hasProperty(ontModel.getProperty(ONT_URL, "expiry"))); ontModel.write(System.out, "TURTLE"); // ontModel.write(System.out, "RDF/XML"); // ontModel.write(System.out, "JSON-LD"); }