/** * Retrieves all subject {@link BNode}s from the statements in the given model. * * @param m * the model from which to retrieve a subject IRI value. * @return a {@link Set} of subject {@link BNode} values from the given model. The returned Set may be empty. */ public static Set<BNode> subjectBNodes(Model m) { return m.subjects().stream().filter(s -> s instanceof BNode).map(s -> (BNode)s).collect( Collectors.toSet()); }
/** * Retrieves all subject {@link IRI}s from the statements in the given model. * * @param m * the model from which to retrieve a subject IRI value. * @return a {@link Set} of subject IRI values from the given model. The returned Set may be empty. */ public static Set<IRI> subjectIRIs(Model m) { return m.subjects().stream().filter(s -> s instanceof IRI).map(s -> (IRI)s).collect( Collectors.toSet()); }
/** * Retrieves all subject {@link BNode}s from the statements in the given model. * * @param m * the model from which to retrieve a subject IRI value. * @return a {@link Set} of subject {@link BNode} values from the given model. The returned Set may be empty. */ public static Set<BNode> subjectBNodes(Model m) { return m.subjects().stream().filter(s -> s instanceof BNode).map(s -> (BNode)s).collect( Collectors.toSet()); }
/** * Retrieves all subject {@link IRI}s from the statements in the given model. * * @param m * the model from which to retrieve a subject IRI value. * @return a {@link Set} of subject IRI values from the given model. The returned Set may be empty. */ public static Set<IRI> subjectIRIs(Model m) { return m.subjects().stream().filter(s -> s instanceof IRI).map(s -> (IRI)s).collect( Collectors.toSet()); }
@Override public Set makeEmptySet() { return makeEmptyModel().filter(null, RDF.VALUE, createURI("test")).subjects(); }
private int extractClasses(Model model) { // loads Simple Named Classes (https://www.w3.org/TR/2004/REC-owl-guide-20040210/#SimpleClasses) int classesLoaded = 0; Set<Resource> allClasses = model.filter(null, RDF.TYPE, OWL.CLASS).subjects(); allClasses.addAll(model.filter(null, RDF.TYPE, RDFS.CLASS).subjects()); Model scoStatements = model.filter(null, RDFS.SUBCLASSOF, null); allClasses.addAll(scoStatements.subjects()); scoStatements.objects().stream().filter(x -> x instanceof IRI).forEach( x -> allClasses.add((IRI)x)); for ( Resource classResource : allClasses) { if (!(classResource instanceof BNode)) { String cypher = String.format("MERGE (p:Class { uri:'%s'}) SET p+={props}", classResource.stringValue()); Map<String, Object> props = new HashMap<>(); for (Value classLabel : model.filter(classResource, RDFS.LABEL, null).objects()) { props.put("name", classLabel.stringValue().replace("'", "\'")); break; } for (Value classComment : model.filter(classResource, RDFS.COMMENT, null).objects()) { props.put("comment", classComment.stringValue().replace("'", "\'")); break; } Map<String, Object> params = new HashMap<>(); params.put("props", props); db.execute(cypher, params); extractClassHierarchy(model,classResource); classesLoaded++; } } return classesLoaded; }
@Override protected int applyRules(Model iteration) throws SailException { try { int nofInferred = 0; nofInferred += applyRulesInternal(iteration.subjects()); nofInferred += applyRulesInternal(Iterables.filter(iteration.objects(), Resource.class)); return nofInferred; } catch (SailException e) { throw e; } catch (RDF4JException e) { throw new SailException(e); } }
@Override protected int applyRules(Model iteration) throws SailException { try { int nofInferred = 0; nofInferred += applyRulesInternal(iteration.subjects()); nofInferred += applyRulesInternal(Iterables.filter(iteration.objects(), Resource.class)); return nofInferred; } catch (SailException e) { throw e; } catch (RDF4JException e) { throw new SailException(e); } }
private int extractProps(Model model, IRI propType) { // loads properties int propsLoaded = 0; Set<Resource> allDatatypeProps = model.filter(null, RDF.TYPE, propType).subjects(); for ( Resource propResource : allDatatypeProps) { if (!(propResource instanceof BNode)) { String cypher = String.format("MERGE (p:%s { uri:'%s'}) SET p+={props}", getNeoEquivalentForProp(propType), propResource.stringValue()); Map<String, Object> props = new HashMap<>(); Set<Value> classeNames = model.filter(propResource, RDFS.LABEL, null).objects(); props.put("name",classeNames.isEmpty()?((IRI)propResource).getLocalName(): classeNames.iterator().next().stringValue().replace("'", "\'")); Set<Value> comments = model.filter(propResource, RDFS.COMMENT, null).objects(); if (!comments.isEmpty()){ props.put("comment", comments.iterator().next().stringValue().replace("'", "\'")); } Map<String, Object> params = new HashMap<>(); params.put("props", props); db.execute(cypher, params); propsLoaded++; extractDomainAndRange(model, propResource, propType); extractPropertyHierarchy(model, propResource, propType); } } return propsLoaded; }
/** * Read the object from the RDF. * <p> * If there is more than one resource in the graph, you should use {@link #readValue(Model, Class, Resource)} and * specify the identifier of the object you wish to read. Otherwise, an {@link RDFMappingException} will be thrown * to indicate that it's not clear what resource should be read. * * @param theGraph the RDF * @param theClass the type of the object to read * @return the object * @throws RDFMappingException if the object could not be created */ @SuppressWarnings("unchecked") public <T> T readValue(final Model theGraph, final Class<T> theClass) { RDFCodec<T> aCodec = (RDFCodec<T>) mCodecs.get(theClass); final Collection<Resource> aSubjects = theGraph.subjects(); if (aSubjects.size() > 1) { throw new RDFMappingException("Multiple subjects found, need to specify the identifier of the object to create."); } else if (aSubjects.isEmpty()) { return aCodec == null ? newInstance(theClass) : aCodec.readValue(theGraph, SimpleValueFactory.getInstance().createBNode()); } final Resource aSubj = aSubjects.iterator().next(); if (aCodec != null) { return aCodec.readValue(theGraph, aSubj); } else { return readValue(theGraph, theClass, aSubj); } }
for (final Resource nextSubject : graph.subjects()) { jg.writeObjectFieldStart(RDFJSONWriter.resourceToString(nextSubject)); for (final IRI nextPredicate : graph.filter(nextSubject, null, null).predicates()) {
for (final Resource nextSubject : graph.subjects()) { jg.writeObjectFieldStart(RDFJSONWriter.resourceToString(nextSubject)); for (final IRI nextPredicate : graph.filter(nextSubject, null, null).predicates()) {