org.apache.jena.rdf.model.StmtIterator Java Examples
The following examples show how to use
org.apache.jena.rdf.model.StmtIterator.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JenaTransformationInjectPosLength.java From trainbenchmark with Eclipse Public License 1.0 | 6 votes |
@Override public void activate(final Collection<JenaPosLengthInjectMatch> matches) throws IOException { final Model model = driver.getModel(); final Property lengthProperty = model.getProperty(BASE_PREFIX + LENGTH); for (final JenaPosLengthInjectMatch match : matches) { final Resource segment = match.getSegment(); final Selector selector = new SimpleSelector(segment, lengthProperty, (RDFNode) null); final StmtIterator oldStatements = model.listStatements(selector); if (!oldStatements.hasNext()) { continue; } final Statement oldStatement = oldStatements.next(); model.remove(oldStatement); final Statement newStatement = model.createLiteralStatement(segment, lengthProperty, 0); model.add(newStatement); } }
Example #2
Source File: ReasoningOntology.java From Heracles with GNU General Public License v3.0 | 6 votes |
public HashMap<String, String> lexToURI(){ StmtIterator iter = ontology.listStatements(new SimpleSelector(null, ontology.getProperty(NS+"#lex"),(Literal)null)); HashMap<String, String> ontoConcepts=new HashMap<String,String>(); while (iter.hasNext()) { Statement stmt = iter.nextStatement(); Resource subject = stmt.getSubject(); // get the subject RDFNode lex = stmt.getObject(); StmtIterator iter2 = ontology.listStatements(new SimpleSelector( subject, ontology.getProperty("http://www.w3.org/2000/01/rdf-schema#subClassOf"), ontology.getResource(this.URI_Mention))); if (iter2.hasNext()){ ontoConcepts.put(lex.toString(), subject.getURI()); } else { System.out.println("No subclass of Mention: "+subject.toString()); } // } return ontoConcepts; }
Example #3
Source File: ReasoningOntology.java From Heracles with GNU General Public License v3.0 | 6 votes |
public HashSet<String> test(String literal){ StmtIterator iter = ontology.listStatements(new SimpleSelector(null, ontology.getProperty("lex"),ontology.createLiteral(literal))); HashSet<String> ontoConcepts=new HashSet<String>(); // System.out.println(literal); while (iter.hasNext()) { Statement stmt = iter.nextStatement(); Resource subject = stmt.getSubject(); // get the subject // StmtIterator iter2 = ontology.listStatements(new SimpleSelector( // subject, // ontology.getProperty("http://www.w3.org/2000/01/rdf-schema#subClassOf"), // ontology.getResource(NS+"#Mention"))); // if (iter2.hasNext()) ontoConcepts.add(subject.getURI()); // System.out.println(subject.toString()); } return ontoConcepts; }
Example #4
Source File: SimpleSubClassInferencer.java From gerbil with GNU Affero General Public License v3.0 | 6 votes |
@Override public void inferSubClasses(String classURI, ClassSet hierarchy, ClassNodeFactory<? extends ClassNode> factory) { Resource classResource = new ResourceImpl(classURI); Set<String> alreadySeenUris = new HashSet<String>(); addOrUpdateUri(classResource, hierarchy, factory, alreadySeenUris); if (!classModel.containsResource(classResource)) { return; } StmtIterator iterator = classModel.listStatements(null, RDFS.subClassOf, classResource); Statement stmt; Resource resource; while (iterator.hasNext()) { stmt = iterator.next(); resource = stmt.getSubject(); if (!alreadySeenUris.contains(resource.getURI())) { addOrUpdateUri(resource, hierarchy, factory, alreadySeenUris); } } }
Example #5
Source File: GtRDFReader.java From JedAIToolkit with Apache License 2.0 | 6 votes |
protected void performReading() { final Model model = RDFDataMgr.loadModel(inputFilePath); final StmtIterator iter = model.listStatements(); while (iter.hasNext()) { Statement stmt = iter.nextStatement(); final String pred = stmt.getPredicate().toString(); if (!(pred.contains("sameAs"))) { continue; } final String sub = stmt.getSubject().toString(); final String obj = stmt.getObject().toString(); // add a new edge for every pair of duplicate entities int entityId1 = urlToEntityId1.get(sub); int entityId2 = urlToEntityId1.get(obj) + datasetLimit; duplicatesGraph.addEdge(entityId1, entityId2); } }
Example #6
Source File: AbstractDataGenerator.java From IGUANA with GNU Affero General Public License v3.0 | 6 votes |
/** * Will send a {@link org.apache.jena.rdf.model.Model} */ public void sendData(Model m) throws Exception { int sendedSize=0; //split Model into small parts StmtIterator sti = m.listStatements(); while(sti.hasNext()){ Model send = ModelFactory.createDefaultModel(); send.setNsPrefixes(m.getNsPrefixMap()); while(sendedSize < maxSize){ Statement stmt = sti.next(); send.add(stmt); sendedSize+=1; } sendDataSnippet(RabbitMQUtils.getData(send)); } }
Example #7
Source File: JenaDriver.java From trainbenchmark with Eclipse Public License 1.0 | 6 votes |
public void deleteOutgoingEdges(final Collection<Resource> vertices, final String edgeType) throws IOException { final Property property = model.getProperty(RdfConstants.BASE_PREFIX + edgeType); for (final Resource vertex : vertices) { final Selector selector = new SimpleSelector(vertex, property, (RDFNode) null); final StmtIterator edges = model.listStatements(selector); final List<Statement> statementsToRemove = new ArrayList<>(); while (edges.hasNext()) { final Statement edge = edges.next(); statementsToRemove.add(edge); } for (final Statement statement : statementsToRemove) { model.remove(statement); } } }
Example #8
Source File: SHParameterizableImpl.java From shacl with Apache License 2.0 | 6 votes |
@Override public List<SHParameter> getParameters() { List<SHParameter> results = new LinkedList<SHParameter>(); StmtIterator it = null; JenaUtil.setGraphReadOptimization(true); try { Set<Resource> classes = JenaUtil.getAllSuperClasses(this); classes.add(this); for(Resource cls : classes) { it = cls.listProperties(SH.parameter); while(it.hasNext()) { Resource param = it.next().getResource(); results.add(param.as(SHParameter.class)); } } } finally { if (it != null) { it.close(); } JenaUtil.setGraphReadOptimization(false); } return results; }
Example #9
Source File: ExecutionPlatform.java From shacl with Apache License 2.0 | 6 votes |
public static boolean canExecute(Resource executable) { StmtIterator it = executable.listProperties(DASH.requiredExecutionPlatform); if(!it.hasNext()) { return true; } else { while(it.hasNext()) { Statement s = it.next(); if(s.getObject().isResource() && isCompatibleWith(s.getResource())) { it.close(); return true; } } return false; } }
Example #10
Source File: Skolemizer.java From Processor with Apache License 2.0 | 6 votes |
protected Resource getResource(Resource resource, String name) { if (resource == null) throw new IllegalArgumentException("Resource cannot be null"); StmtIterator it = resource.listProperties(); try { while (it.hasNext()) { Statement stmt = it.next(); if (stmt.getObject().isAnon() && stmt.getPredicate().getLocalName().equals(name)) { if (log.isTraceEnabled()) log.trace("Found Resource {} for property name: {} ", stmt.getResource(), name); return stmt.getResource(); } } } finally { it.close(); } return null; }
Example #11
Source File: Mapping.java From FCA-Map with GNU General Public License v3.0 | 6 votes |
public String listMappingCellSPO(MappingCell mc) { if (mc == null || m_source == null || m_target == null) return "null"; StringBuilder sb = new StringBuilder(); Resource r1 = m_source.getResource(mc.getEntity1()); sb.append(String.format("%n<<<<<<<")); sb.append(String.format("%n* %s%n", r1.getURI())); for (StmtIterator stmt = r1.listProperties(); stmt.hasNext(); ) { sb.append(String.format("** %s%n", stmt.nextStatement())); } sb.append(String.format("=======")); Resource r2 = m_target.getResource(mc.getEntity2()); sb.append(String.format("%n* %s%n", r2.getURI())); for (StmtIterator stmt = r2.listProperties(); stmt.hasNext(); ) { sb.append(String.format("** %s%n", stmt.nextStatement())); } sb.append(String.format(">>>>>>>%n")); return sb.toString(); }
Example #12
Source File: DB2Closure.java From quetzal with Eclipse Public License 2.0 | 6 votes |
private static void closureNoTest(Resource r, Model closureBlob, Collection<Resource> visited, ClosureTest test, Collection<String> resources) { visited.add(r); String gid = ((DB2Graph) r.getModel().getGraph()).getGraphID(); String key = null; if (r.isAnon()) { key = gid + ":" + r.getId(); } else { key = gid + ":" + r.getURI(); } if (resources.contains(key)) { return; } resources.add(key); StmtIterator sIter = r.listProperties(); for (; sIter.hasNext();) { Statement stmt = sIter.nextStatement(); closure(stmt, closureBlob, visited, test, resources); } }
Example #13
Source File: AdditionalPropertyMatcherImpl.java From FCA-Map with GNU General Public License v3.0 | 6 votes |
private <T extends Resource> void addContext(Set<T> properties, int from_id, Map<Resource, Set<MappingCell>> m, Map<ResourceWrapper<T>, Set<SubjectObject>> context) { for (final T p : properties) { StmtIterator it = p.getModel().listStatements( new SimpleSelector(null, null, (RDFNode) null) { @Override public boolean selects(Statement s) { return s.getPredicate().getURI().equals(p.getURI()) && s.getObject().isResource(); } } ); while (it.hasNext()) { Statement stmt = it.nextStatement(); Resource subject = stmt.getSubject(); Resource object = stmt.getObject().asResource(); ResourceWrapper<T> rw = new ResourceWrapper<T>(p, from_id); addContextFromSO(context, rw, m, subject, object); } } }
Example #14
Source File: Exporter.java From fcrepo-import-export with Apache License 2.0 | 6 votes |
private Set<URI> filterInboundReferences(final URI uri, final Model model) { final String withSlash = withSlash(uri).toString(); final String withoutSlash = withoutSlash(uri).toString(); final Set<URI> inboundMembers = new HashSet<>(); final List<Statement> removeList = new ArrayList<>(); for (final StmtIterator inbound = model.listStatements(); inbound.hasNext(); ) { final Statement s = inbound.next(); final String subject = s.getSubject().toString(); if (!subject.equals(withSlash) && !subject.equals(withoutSlash)) { removeList.add(s); logger.trace("Filtering inbound reference: {}", s); inboundMembers.add(URI.create(subject)); } } model.remove(removeList); return inboundMembers; }
Example #15
Source File: ShapesGraph.java From shacl with Apache License 2.0 | 6 votes |
public SHConstraintComponent getComponentWithParameter(Property parameter) { return parametersMap.computeIfAbsent(parameter, p -> { StmtIterator it = shapesModel.listStatements(null, SH.path, parameter); while(it.hasNext()) { Resource param = it.next().getSubject(); if(!param.hasProperty(SH.optional, JenaDatatypes.TRUE)) { StmtIterator i2 = shapesModel.listStatements(null, SH.parameter, param); while(i2.hasNext()) { Resource r = i2.next().getSubject(); if(JenaUtil.hasIndirectType(r, SH.ConstraintComponent)) { i2.close(); it.close(); SHConstraintComponent cc = SHFactory.asConstraintComponent(r); return cc; } } } } return null; }); }
Example #16
Source File: Importer.java From fcrepo-import-export with Apache License 2.0 | 6 votes |
/** * Removes statements from the provided model that affect triples that need not be (and indeed * cannot be) modified directly through PUT, POST or PATCH requests to fedora. * * Certain triples included in a resource from fedora cannot be explicitly stored, but because * they're derived from other content that *can* be stored will still appear identical when the * other RDF and content is ingested. Examples include those properties that reflect innate * characteristics of binary resources like file size and message digest, Or triples that * represent characteristics of rdf resources like the number of children, whether it has * versions and some of the types. * * @param model the RDF statements about an exported resource * @return the provided model updated to omit statements that may not be updated directly through * the fedora API * @throws IOException * @throws FcrepoOperationFailedException */ private Model sanitize(final Model model) throws IOException, FcrepoOperationFailedException { final List<Statement> remove = new ArrayList<>(); for (final StmtIterator it = model.listStatements(); it.hasNext(); ) { final Statement s = it.nextStatement(); if ((s.getPredicate().getNameSpace().equals(REPOSITORY_NAMESPACE) && !relaxedPredicate(s.getPredicate())) || s.getSubject().getURI().endsWith("fcr:export?format=jcr/xml") || s.getSubject().getURI().equals(REPOSITORY_NAMESPACE + "jcr/xml") || s.getPredicate().equals(DESCRIBEDBY) || s.getPredicate().equals(CONTAINS) || s.getPredicate().equals(HAS_MESSAGE_DIGEST) || (s.getPredicate().equals(RDF_TYPE) && forbiddenType(s.getResource()))) { remove.add(s); } else if (s.getObject().isResource()) { // make sure that referenced repository objects exist final String obj = s.getResource().toString(); if (obj.startsWith(repositoryRoot.toString())) { ensureExists(URI.create(obj)); } } } return model.remove(remove); }
Example #17
Source File: RDFIntroSpector.java From ontopia with Apache License 2.0 | 6 votes |
private static void parseN3(GrabMappingsHandler handler, String infileurl) { Model model = ModelFactory.createDefaultModel(); model.read(infileurl, "N3"); AResourceImpl sub = new AResourceImpl(); AResourceImpl pred = new AResourceImpl(); AResourceImpl objres = new AResourceImpl(); ALiteralImpl objlit = new ALiteralImpl(); StmtIterator it = model.listStatements(); while (it.hasNext()) { Statement stmt = it.nextStatement(); RDFNode object = stmt.getObject(); sub.setResource(stmt.getSubject()); pred.setResource(stmt.getPredicate()); if (object instanceof Literal) { objlit.setLiteral((Literal) object); handler.statement(sub, pred, objlit); } else { objres.setResource((Resource) object); handler.statement(sub, pred, objres); } } }
Example #18
Source File: JenaUtil.java From shacl with Apache License 2.0 | 6 votes |
private static void addTransitiveObjects(Set<Resource> resources, Set<Resource> reached, Resource subject, Property predicate) { resources.add(subject); reached.add(subject); StmtIterator it = subject.listProperties(predicate); try { while (it.hasNext()) { RDFNode object = it.next().getObject(); if (object instanceof Resource) { if (!reached.contains(object)) { addTransitiveObjects(resources, reached, (Resource)object, predicate); } } } } finally { it.close(); } }
Example #19
Source File: JenaUtil.java From shacl with Apache License 2.0 | 6 votes |
private static void addTransitiveSubjects(Set<Resource> reached, Resource object, Property predicate, ProgressMonitor monitor) { if (object != null) { reached.add(object); StmtIterator it = object.getModel().listStatements(null, predicate, object); try { while (it.hasNext()) { if (monitor != null && monitor.isCanceled()) { it.close(); return; } Resource subject = it.next().getSubject(); if (!reached.contains(subject)) { addTransitiveSubjects(reached, subject, predicate, monitor); } } } finally { it.close(); } } }
Example #20
Source File: JenaUtil.java From shacl with Apache License 2.0 | 6 votes |
/** * Gets all instances of a given class and its subclasses. * @param cls the class to get the instances of * @return the instances */ public static Set<Resource> getAllInstances(Resource cls) { JenaUtil.setGraphReadOptimization(true); try { Model model = cls.getModel(); Set<Resource> classes = getAllSubClasses(cls); classes.add(cls); Set<Resource> results = new HashSet<>(); for(Resource subClass : classes) { StmtIterator it = model.listStatements(null, RDF.type, subClass); while (it.hasNext()) { results.add(it.next().getSubject()); } } return results; } finally { JenaUtil.setGraphReadOptimization(false); } }
Example #21
Source File: JenaUtil.java From shacl with Apache License 2.0 | 6 votes |
private static Resource getFirstRange(Resource property, Set<Resource> reached) { Resource directRange = getFirstDirectRange(property); if(directRange != null) { return directRange; } StmtIterator it = property.listProperties(RDFS.subPropertyOf); while (it.hasNext()) { Statement ss = it.next(); if (ss.getObject().isURIResource()) { Resource superProperty = ss.getResource(); if (!reached.contains(superProperty)) { reached.add(superProperty); Resource r = getFirstRange(superProperty, reached); if (r != null) { it.close(); return r; } } } } return null; }
Example #22
Source File: ValidationEngine.java From shacl with Apache License 2.0 | 6 votes |
public void updateConforms() { boolean conforms = true; StmtIterator it = report.listProperties(SH.result); while(it.hasNext()) { Statement s = it.next(); if(s.getResource().hasProperty(RDF.type, SH.ValidationResult)) { conforms = false; it.close(); break; } } if(report.hasProperty(SH.conforms)) { report.removeAll(SH.conforms); } report.addProperty(SH.conforms, conforms ? JenaDatatypes.TRUE : JenaDatatypes.FALSE); }
Example #23
Source File: JenaUtil.java From shacl with Apache License 2.0 | 6 votes |
private static <T> T getNearest(Resource cls, java.util.function.Function<Resource,T> function, Set<Resource> reached) { reached.add(cls); StmtIterator it = cls.listProperties(RDFS.subClassOf); while(it.hasNext()) { Statement s = it.next(); if(s.getObject().isResource() && !reached.contains(s.getResource())) { T result = function.apply(s.getResource()); if(result == null) { result = getNearest(s.getResource(), function, reached); } if(result != null) { it.close(); return result; } } } return null; }
Example #24
Source File: SPARQLConstraintExecutor.java From shacl with Apache License 2.0 | 6 votes |
@Override protected String getSPARQL(Constraint constraint) { SHSPARQLConstraint sc = SHFactory.asSPARQLConstraint(constraint.getParameterValue()); String select = JenaUtil.getStringProperty(sc, SH.select); if(select == null) { String message = "Missing " + SH.PREFIX + ":" + SH.select.getLocalName() + " of " + RDFLabels.get().getLabel(sc); if(sc.isAnon()) { StmtIterator it = sc.getModel().listStatements(null, null, sc); if(it.hasNext()) { Statement s = it.next(); it.close(); message += " at " + RDFLabels.get().getLabel(s.getSubject()); message += " via " + RDFLabels.get().getLabel(s.getPredicate()); } } throw new SHACLException(message); } return SPARQLSubstitutions.withPrefixes(select, sc); }
Example #25
Source File: JenaTransformationRepairPosLength.java From trainbenchmark with Eclipse Public License 1.0 | 6 votes |
@Override public void activate(final Collection<JenaPosLengthMatch> matches) throws IOException { final Model model = driver.getModel(); final Property lengthProperty = model.getProperty(BASE_PREFIX + LENGTH); for (final JenaPosLengthMatch match : matches) { final Resource segment = match.getSegment(); final int length = match.getLength().getInt(); final int newLength = -length + 1; final Selector selector = new SimpleSelector(segment, lengthProperty, (RDFNode) null); final StmtIterator statementsToRemove = model.listStatements(selector); if (statementsToRemove.hasNext()) { final Statement oldStatement = statementsToRemove.next(); model.remove(oldStatement); } final Statement newStatement = model.createLiteralStatement(segment, lengthProperty, newLength); model.add(newStatement); } }
Example #26
Source File: OrConstraintExecutor.java From shacl with Apache License 2.0 | 6 votes |
private boolean hasOnlyDatatypeConstraints() { if(shapes.size() == 0) { return false; } for(Resource shape : shapes) { StmtIterator mit = shape.listProperties(); if(mit.hasNext()) { Statement s = mit.next(); if(!SH.datatype.equals(s.getPredicate()) || mit.hasNext() || !s.getObject().isURIResource()) { mit.close(); return false; } } else { return false; } } return true; }
Example #27
Source File: JenaUtil.java From shacl with Apache License 2.0 | 6 votes |
/** * Checks whether a given Resource is an instance of a given type, or * a subclass thereof. Make sure that the expectedType parameter is associated * with the right Model, because the system will try to walk up the superclasses * of expectedType. The expectedType may have no Model, in which case * the method will use the instance's Model. * @param instance the Resource to test * @param expectedType the type that instance is expected to have * @return true if resource has rdf:type expectedType */ public static boolean hasIndirectType(Resource instance, Resource expectedType) { if(expectedType.getModel() == null) { expectedType = expectedType.inModel(instance.getModel()); } StmtIterator it = instance.listProperties(RDF.type); while(it.hasNext()) { Statement s = it.next(); if(s.getObject().isResource()) { Resource actualType = s.getResource(); if(actualType.equals(expectedType) || JenaUtil.hasSuperClass(actualType, expectedType)) { it.close(); return true; } } } return false; }
Example #28
Source File: JenaUtil.java From shacl with Apache License 2.0 | 6 votes |
private static boolean hasSuperClass(Resource subClass, Resource superClass, Set<Resource> reached) { StmtIterator it = subClass.listProperties(RDFS.subClassOf); while(it.hasNext()) { Statement s = it.next(); if(superClass.equals(s.getObject())) { it.close(); return true; } else if(!reached.contains(s.getResource())) { reached.add(s.getResource()); if(hasSuperClass(s.getResource(), superClass, reached)) { it.close(); return true; } } } return false; }
Example #29
Source File: Exporter.java From fcrepo-import-export with Apache License 2.0 | 6 votes |
/** * Filter out the binary resource references from the model * @param uri the URI for the resource * @param model the RDF Model of the resource * @return the RDF model with no binary references * @throws FcrepoOperationFailedException * @throws IOException */ private void filterBinaryReferences(final URI uri, final Model model) throws IOException, FcrepoOperationFailedException { final List<Statement> removeList = new ArrayList<>(); for (final StmtIterator it = model.listStatements(); it.hasNext();) { final Statement s = it.nextStatement(); final RDFNode obj = s.getObject(); if (obj.isResource() && obj.toString().startsWith(repositoryRoot.toString()) && !s.getPredicate().toString().equals(REPOSITORY_NAMESPACE + "hasTransactionProvider")) { try (final FcrepoResponse resp = client().head(URI.create(obj.toString())).disableRedirects() .perform()) { checkValidResponse(resp, URI.create(obj.toString()), config.getUsername()); final List<URI> linkHeaders = resp.getLinkHeaders("type"); if (linkHeaders.contains(binaryURI)) { removeList.add(s); } } } } model.remove(removeList); }
Example #30
Source File: Skolemizer.java From Processor with Apache License 2.0 | 5 votes |
protected Literal getLiteral(Resource resource, String namePath) { if (resource == null) throw new IllegalArgumentException("Resource cannot be null"); if (namePath.contains(".")) { String name = namePath.substring(0, namePath.indexOf(".")); String nameSubPath = namePath.substring(namePath.indexOf(".") + 1); Resource subResource = getResource(resource, name); if (subResource != null) return getLiteral(subResource, nameSubPath); } StmtIterator it = resource.listProperties(); try { while (it.hasNext()) { Statement stmt = it.next(); if (stmt.getObject().isLiteral() && stmt.getPredicate().getLocalName().equals(namePath)) { if (log.isTraceEnabled()) log.trace("Found Literal {} for property name: {} ", stmt.getLiteral(), namePath); return stmt.getLiteral(); } } } finally { it.close(); } return null; }