public static Optional<Value> getObject(Model theGraph, Resource theSubj, IRI thePred) { Iterator<Value> aCollection = theGraph.filter(theSubj, thePred, (Value)null, new Resource[0]).objects().iterator(); return aCollection.hasNext() ? Optional.of(aCollection.next()) : Optional.empty(); }
/** * Retrieve all property values for the supplied subject and property from the given model. * * @param m * the model from which to retrieve the property values. * @param subject * the subject resource for which to retrieve all property values. * @param property * the property for which to retrieve all values. * @param contexts * the contexts from which to retrieve the property values. Optional vararg argument. If not * specified the operations works on the entire Model. * @return a Set of all property values for the supplied input. The resulting set may be empty. */ public static Set<Value> getProperties(Model m, Resource subject, IRI property, Resource... contexts) { Objects.requireNonNull(m, "model may not be null"); Objects.requireNonNull(subject, "subject may not be null"); Objects.requireNonNull(property, "property may not be null"); return m.filter(subject, property, null, contexts).objects(); }
/** * Retrieve all property values for the supplied subject and property from the given model. * * @param m * the model from which to retrieve the property values. * @param subject * the subject resource for which to retrieve all property values. * @param property * the property for which to retrieve all values. * @param contexts * the contexts from which to retrieve the property values. Optional vararg argument. If not * specified the operations works on the entire Model. * @return a Set of all property values for the supplied input. The resulting set may be empty. */ public static Set<Value> getProperties(Model m, Resource subject, IRI property, Resource... contexts) { Objects.requireNonNull(m, "model may not be null"); Objects.requireNonNull(subject, "subject may not be null"); Objects.requireNonNull(property, "property may not be null"); return m.filter(subject, property, null, contexts).objects(); }
@Override public Set<Type> decide(Model model, Resource resource) { Set<IRI> rdfTypes = model.filter(resource, RDF.TYPE, null).objects().stream() .map(v -> (IRI) v) .collect(Collectors.toSet()); Set<IRI> usedPredicates = model.filter(resource, null, null).predicates().stream() .filter(p -> !p.equals(RDF.TYPE)) .collect(Collectors.toSet()); usedPredicates.forEach(p -> { if (inferenceMap.containsKey(p)) { rdfTypes.add(inferenceMap.get(p)); } }); return rdfTypes.stream() .map(mapper::getDecidableType) .collect(Collectors.toSet()); }
@Override public Set<Type> decide(Model model, Resource resource) { List<IRI> rdfTypes = model.filter(resource, RDF.TYPE, null).objects().stream() .map(v -> (IRI) v) .collect(Collectors.toList()); // TODO what if multiple rdf:types? probably choose the only 1 that's known/registered. what if multiple of those? if (rdfTypes.size() > 1) return rdfTypes.stream() .map(mapper::getDecidableType) .collect(Collectors.toSet()); // if no rdf:type, use property type (or its registered implementation) as target type if (rdfTypes.isEmpty()) { if(propertyTypeDecider.isPresent()) { return propertyTypeDecider.get().decide(model, resource); } else { throw new RuntimeException(String.format("No decidable type found for %s. Register decidable type on rdf mapper.", resource)); } } IRI rdfType = rdfTypes.get(0); return ImmutableSet.of(mapper.getDecidableType(rdfType)); }
@Override public Set<Type> decide(Model model, Resource resource) { Set<IRI> rdfTypes = model.filter(resource, RDF.TYPE, null).objects().stream() .map(v -> (IRI) v) .collect(Collectors.toSet()); Set<IRI> usedPredicates = model.filter(resource, null, null).predicates().stream() .filter(p -> !p.equals(RDF.TYPE)) .collect(Collectors.toSet()); usedPredicates.forEach(p -> { if (inferenceMap.containsKey(p)) { rdfTypes.add(inferenceMap.get(p)); } }); return rdfTypes.stream() .map(mapper::getDecidableType) .collect(Collectors.toSet()); }
@Override public Set<Type> decide(Model model, Resource resource) { List<IRI> rdfTypes = model.filter(resource, RDF.TYPE, null).objects().stream() .map(v -> (IRI) v) .collect(Collectors.toList()); // TODO what if multiple rdf:types? probably choose the only 1 that's known/registered. what if multiple of those? if (rdfTypes.size() > 1) return rdfTypes.stream() .map(mapper::getDecidableType) .collect(Collectors.toSet()); // if no rdf:type, use property type (or its registered implementation) as target type if (rdfTypes.isEmpty()) { if(propertyTypeDecider.isPresent()) { return propertyTypeDecider.get().decide(model, resource); } else { throw new RuntimeException(String.format("No decidable type found for %s. Register decidable type on rdf mapper.", resource)); } } IRI rdfType = rdfTypes.get(0); return ImmutableSet.of(mapper.getDecidableType(rdfType)); }
private Optional<Object> determineValue(Model model, Resource resource) { Set<Value> objects = model.filter(resource, predicate, null).objects(); if (objects.size() > 1) throw new RuntimeException("more than 1 object for the predicate [" + predicate + "] for a logical source"); if (objects.isEmpty()) return Optional.empty(); Value object = objects.iterator().next(); if (object instanceof Literal) return Optional.of(object.stringValue()); // map 'object' to some complex type // TODO quite nasty to create the transformer here ComplexValueTransformer transformer = new ComplexValueTransformer( new LogicalSourceSourceTypeDecider(mapper), mappingCache, mapper, o -> o ); Object value = transformer.transform(model, object); return Optional.of(value); }
private Optional<Object> determineValue(Model model, Resource resource) { Set<Value> objects = model.filter(resource, predicate, null).objects(); if (objects.size() > 1) throw new RuntimeException("more than 1 object for the predicate [" + predicate + "] for a logical source"); if (objects.isEmpty()) return Optional.empty(); Value object = objects.iterator().next(); if (object instanceof Literal) return Optional.of(object.stringValue()); // map 'object' to some complex type // TODO quite nasty to create the transformer here ComplexValueTransformer transformer = new ComplexValueTransformer( new LogicalSourceSourceTypeDecider(mapper), mappingCache, mapper, o -> o ); Object value = transformer.transform(model, object); return Optional.of(value); }
private void extractClassHierarchy(Model model, Resource classResource) { for (Value object: model.filter(classResource, RDFS.SUBCLASSOF, null).objects()){ if (object instanceof IRI){ db.execute(String.format( "MATCH (p:Class { uri:'%s'}), (c { uri:'%s'}) MERGE (p)-[:SCO]->(c)", classResource.stringValue(), object.stringValue())); } } }
@Override public Set makeEmptySet() { return new LinkedHashModel().filter(createURI("test"), RDF.VALUE, null).objects(); }
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; }
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; }
@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); } }
iri -> setBaseURI(iri.stringValue())); Set<Value> objects = model.filter(resource, READ_CONTEXT, null).objects(); setReadContexts(objects.toArray(new IRI[objects.size()])); objects = model.filter(resource, ADD_CONTEXT, null).objects(); setAddContexts(objects.toArray(new IRI[objects.size()])); objects = model.filter(resource, REMOVE_CONTEXT, null).objects(); setRemoveContexts(objects.toArray(new IRI[objects.size()])); objects = model.filter(resource, ARCHIVE_CONTEXT, null).objects(); setArchiveContexts(objects.toArray(new IRI[objects.size()]));
private void extractPropertyHierarchy(Model model, Resource propResource, IRI propType) { for (Value object: model.filter(propResource, RDFS.SUBPROPERTYOF, null).objects()){ if (object instanceof IRI && (model.contains((IRI)object,RDF.TYPE, OWL.OBJECTPROPERTY) || model.contains((IRI)object,RDF.TYPE, OWL.DATATYPEPROPERTY) || model.contains((IRI)object,RDF.TYPE, RDF.PROPERTY))){ db.execute(String.format( "MATCH (p:%s { uri:'%s'}), (c { uri:'%s'}) MERGE (p)-[:%s]->(c)", getNeoEquivalentForProp(propType), propResource.stringValue(), object.stringValue(), (propType.equals(OWL.DATATYPEPROPERTY)?"SPO":"SRO"))); } } }
private void extractDomainAndRange(Model model, Resource propResource, IRI propType) { for (Value object: model.filter(propResource, RDFS.DOMAIN, null).objects()){ if (object instanceof IRI && (model.contains((IRI)object,RDF.TYPE, OWL.CLASS) || model.contains((IRI)object,RDF.TYPE, RDFS.CLASS) || model.contains((IRI)object,RDF.TYPE, OWL.OBJECTPROPERTY))){ //This last bit picks up OWL definitions of attributes on properties. // Totally non standard, but in case it's used to accommodate LPG style properties on rels definitions db.execute(String.format( "MATCH (p:%s { uri:'%s'}), (c { uri:'%s'}) MERGE (p)-[:DOMAIN]->(c)", // c can be a class or an object property getNeoEquivalentForProp(propType), propResource.stringValue(), object.stringValue())); } } for (Value object: model.filter(propResource, RDFS.RANGE, null).objects()){ if (object instanceof IRI && (model.contains((IRI)object,RDF.TYPE, OWL.CLASS) || model.contains((IRI)object,RDF.TYPE, RDFS.CLASS))){ //only picks ranges that are classes, which means, only ObjectProperties // (no XSD ranges for DatatypeProps) db.execute(String.format( "MATCH (p:%s { uri:'%s'}), (c:Class { uri:'%s'}) MERGE (p)-[:RANGE]->(c)", getNeoEquivalentForProp(propType), propResource.stringValue(), object.stringValue())); } } }
for (final IRI nextPredicate : graph.filter(nextSubject, null, null).predicates()) { jg.writeArrayFieldStart(nextPredicate.stringValue()); for (final Value nextObject : graph.filter(nextSubject, nextPredicate, null).objects()) {
for (final IRI nextPredicate : graph.filter(nextSubject, null, null).predicates()) { jg.writeArrayFieldStart(nextPredicate.stringValue()); for (final Value nextObject : graph.filter(nextSubject, nextPredicate, null).objects()) {