private Set<IRI> getSeeAlsoObjectUris(GraphNode node) { Set<IRI> result = new HashSet<IRI>(); Lock l = node.readLock(); l.lock(); try { Iterator<RDFTerm> objects = node.getObjects(RDFS.seeAlso); while (objects.hasNext()) { RDFTerm next = objects.next(); if (next instanceof IRI) { result.add((IRI)next); } } } finally { l.unlock(); } return result; }
@Override public Set<IRI> run() { Iterator<RDFTerm> baseUrisIter = getPlatformInstance(). getObjects(PLATFORM.baseUri); Set<IRI> baseUris = new HashSet<IRI>(); while (baseUrisIter.hasNext()) { IRI baseUri = (IRI) baseUrisIter.next(); baseUris.add(baseUri); } baseUris.add(getDefaultBaseUri()); return baseUris; } });
private Set<IRI> getSeeAlsoObjectUris(GraphNode node) { Set<IRI> result = new HashSet<IRI>(); Lock l = node.readLock(); l.lock(); try { Iterator<RDFTerm> objects = node.getObjects(RDFS.seeAlso); while (objects.hasNext()) { RDFTerm next = objects.next(); if (next instanceof IRI) { result.add((IRI)next); } } } finally { l.unlock(); } return result; }
public Iterator<Literal> getLiterals(IRI property) { final Iterator<RDFTerm> objects = getObjects(property); return new Iterator<Literal>() { Literal next = prepareNext(); @Override public boolean hasNext() { return next != null; } @Override public Literal next() { Literal result = next; next = prepareNext(); return result; } @Override public void remove() { throw new UnsupportedOperationException("Not supported yet."); } private Literal prepareNext() { while (objects.hasNext()) { RDFTerm nextObject = objects.next(); if (nextObject instanceof Literal) { return (Literal) nextObject; } } return null; } }; }
/** * Returns a iterator containing all objects of the triples where this * graph node is the subject and has the specified property. The objects * are returned as <code>GraphNode</code>s. * * @param property * @return */ public Iterator<GraphNode> getObjectNodes(IRI property) { final Iterator<RDFTerm> objects = this.getObjects(property); return new Iterator<GraphNode>() { @Override public boolean hasNext() { return objects.hasNext(); } @Override public GraphNode next() { RDFTerm object = objects.next(); return new GraphNode(object, graph); } @Override public void remove() { objects.remove(); } }; }
private Set<IRI> getSubjectExpansionProperties(GraphNode recipe) { final MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(true); final List<String> paramValues = queryParams.get(SUBJ_EXP_PARAM); final Set<IRI> result = new HashSet<IRI>(); if (paramValues != null) { for (String uriString : paramValues) { result.add(new IRI(uriString)); } } if (recipe != null) { Iterator<GraphNode> ingredients = recipe.getObjectNodes(RECIPES.ingredient); while (ingredients.hasNext()) { Iterator<RDFTerm> properties = ingredients.next().getObjects(RECIPES.ingredientInverseProperty); while (properties.hasNext()) { result.add((IRI)properties.next()); } } } return result; }
private <T> Iterator<T> getTypeSelectedObjects(IRI property, final Class<T> type) { final Iterator<RDFTerm> objects = getObjects(property); return new Iterator<T>() { T next = prepareNext(); @Override public boolean hasNext() { return next != null; } @Override public T next() { T result = next; next = prepareNext(); return result; } @Override public void remove() { throw new UnsupportedOperationException("Not supported yet."); } private T prepareNext() { while (objects.hasNext()) { RDFTerm nextObject = objects.next(); if (type.isAssignableFrom(nextObject.getClass())) { return (T) nextObject; } } return null; } }; }
/** * Checks wether this node has the given property with the given value. * If the given value is null, then it is checked if this node has the * specified property regardless of its value. * * @param property * @param object * @return true if the node represented by this object is the subject of a * statement with the given prediate and object, false otherwise */ public boolean hasProperty(IRI property, RDFTerm object) { Lock l = readLock(); l.lock(); try { Iterator<RDFTerm> objects = getObjects(property); if (object == null) { return objects.hasNext(); } while (objects.hasNext()) { if (objects.next().equals(object)) { return true; } } return false; } finally { l.unlock(); } }
private Set<IRI> getObjectExpansionProperties(GraphNode recipe) { final MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(true); final List<String> paramValues = queryParams.get(OBJ_EXP_PARAM); final Set<IRI> result = new HashSet<IRI>(); if (paramValues != null) { for (String uriString : paramValues) { result.add(new IRI(uriString)); } } if (recipe != null) { Iterator<GraphNode> ingredients = recipe.getObjectNodes(RECIPES.ingredient); while (ingredients.hasNext()) { Iterator<RDFTerm> properties = ingredients.next().getObjects(RECIPES.ingredientProperty); while (properties.hasNext()) { result.add((IRI)properties.next()); } } } return result; }
/** * Returns a iterator containing all objects of the triples where this * graph node is the subject and has the specified property. The objects * are returned as <code>GraphNode</code>s. * * @param property * @return */ public Iterator<GraphNode> getObjectNodes(IRI property) { final Iterator<RDFTerm> objects = this.getObjects(property); return new Iterator<GraphNode>() { @Override public boolean hasNext() { return objects.hasNext(); } @Override public GraphNode next() { RDFTerm object = objects.next(); return new GraphNode(object, graph); } @Override public void remove() { objects.remove(); } }; }
public Iterator<Literal> getLiterals(IRI property) { final Iterator<RDFTerm> objects = getObjects(property); return new Iterator<Literal>() { Literal next = prepareNext(); @Override public boolean hasNext() { return next != null; } @Override public Literal next() { Literal result = next; next = prepareNext(); return result; } @Override public void remove() { throw new UnsupportedOperationException("Not supported yet."); } private Literal prepareNext() { while (objects.hasNext()) { RDFTerm nextObject = objects.next(); if (nextObject instanceof Literal) { return (Literal) nextObject; } } return null; } }; }
@Override protected List<String> value(GraphNode node) { List<String> list = new ArrayList<String>(); Lock lock = node.readLock(); lock.lock(); try { Iterator<Resource> iter = node.getObjects(this.property); while(iter.hasNext()) { Resource resource = iter.next(); if(resource instanceof Literal) { list.add(((Literal)resource).getLexicalForm()); } else if(resource instanceof UriRef) { list.add(((UriRef) resource).getUnicodeString()); } } } finally { lock.unlock(); } return list; }
LanguageDescription(GraphNode resource) { this.resource = resource; Literal iso1Literal = null; TripleCollection configGraph = resource.getGraph(); if (configGraph instanceof LockableMGraph) { LockableMGraph lockableConfigGraph = (LockableMGraph)configGraph; Lock readLock = lockableConfigGraph.getLock().readLock(); readLock.lock(); try { iso1Literal = (Literal) resource.getObjects(LINGVOJ.iso1).next(); } finally { readLock.unlock(); } } else { iso1Literal = (Literal) resource.getObjects(LINGVOJ.iso1).next(); } if (iso1Literal == null) { throw new RuntimeException("No iso1 code for " +resource.getNode()); } String iso1 = iso1Literal.getLexicalForm(); this.language = new Language(iso1); }
@Override public Iterator<Object> get(String field) { if(field == null){ throw new IllegalArgumentException("The parsed field MUST NOT be NULL"); } else if(field.isEmpty()){ throw new IllegalArgumentException("The parsed field MUST NOT be Empty"); } return new AdaptingIterator<RDFTerm, Object>(graphNode.getObjects(new IRI(field)), new Resource2ValueAdapter<RDFTerm>(),Object.class); }
@Override public Iterator<Object> get(String field) { if(field == null){ throw new IllegalArgumentException("The parsed field MUST NOT be NULL"); } else if(field.isEmpty()){ throw new IllegalArgumentException("The parsed field MUST NOT be Empty"); } return new AdaptingIterator<RDFTerm, Object>(graphNode.getObjects(new IRI(field)), new Resource2ValueAdapter<RDFTerm>(),Object.class); }
@Override public IRI run() { GraphNode platformInstance = getPlatformInstance(); Lock l = platformInstance.readLock(); l.lock(); try { Iterator<RDFTerm> triples = platformInstance.getObjects(PLATFORM.defaultBaseUri); if (triples.hasNext()) { return (IRI) triples.next(); } else { String port = context.getProperty("org.osgi.service.http.port"); if (port == null) { port = DEFAULT_PORT; } if (port.equals("80")) { return new IRI("http://localhost/"); } return new IRI("http://localhost:" + port + "/"); } } finally { l.unlock(); } } });
private void indexNamedResource(UriRef uriRef, IndexWriter writer) throws IOException { Term term = new Term(URI_FIELD_NAME, uriRef.getUnicodeString()); writer.deleteDocuments(term); //the reindexing might be caused by the removal of a type statement GraphNode node = new GraphNode(uriRef, this.baseGraph); List<UriRef> types = new ArrayList<UriRef>(); Lock lock = node.readLock(); lock.lock(); try { Iterator<Resource> resources = node.getObjects(RDF.type); while (resources.hasNext()) { Resource res = resources.next(); if (res instanceof UriRef) { types.add((UriRef) res); } } } finally { lock.unlock(); } for (UriRef type : types) { if (type2IndexedProperties.containsKey(type)) { Document doc = resourceToDocument(uriRef, type); doc.add(new Field(URI_FIELD_NAME, uriRef.getUnicodeString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); writer.addDocument(doc); } } }
/** * Returns the label of the language described by this object in the * specified language, or null if no lable is available in that language * @param lang the language in which the label should be. * @return */ public String getLabel(Language lang) { Lock readLock = null; TripleCollection configGraph = resource.getGraph(); if (configGraph instanceof LockableMGraph) { LockableMGraph lockableConfigGraph = (LockableMGraph)configGraph; readLock = lockableConfigGraph.getLock().readLock(); readLock.lock(); } try { Iterator<Resource> labels = resource.getObjects(RDFS.label); while (labels.hasNext()) { PlainLiteral label = (PlainLiteral) labels.next(); if (label.getLanguage().equals(lang)) { return label.getLexicalForm(); } } return null; } finally { if (readLock != null) { readLock.unlock(); } } } }
/** * Returns a language uri of a language which has a label containing the * specified languageName. The label itself is in the language specified through * inLanguage. If inLanguage is null, then all labels of a language of searched. * If no language was found in the <http://www.lingvoj.org/lingvoj> * graph, then null is returned. The returned uri can be used to * add the language to Clerezza over the addLanguage()-method in this class. * @return a language uris */ public UriRef getLanguage(String languageName, Language inLanguage) { Graph lingvojGraph = getLingvojGraph(); Iterator<Triple> languages = lingvojGraph.filter(null, RDFS.isDefinedBy, null); while (languages.hasNext()) { GraphNode languageNode = new GraphNode((UriRef) languages.next().getSubject(), lingvojGraph); Iterator<Resource> labels = languageNode.getObjects(RDFS.label); while (labels.hasNext()) { PlainLiteral label = (PlainLiteral) labels.next(); if (label.getLanguage().equals(inLanguage) || inLanguage == null) { if (label.getLexicalForm().contains(languageName)) { return (UriRef) languageNode.getNode(); } } } } return null; }
private void removeCachedConcepts(GraphNode conceptCacheEntryGraphNode) { Iterator<Resource> searchResults = conceptCacheEntryGraphNode.getObjects(CONCEPTS.searchResult); while (searchResults.hasNext()) { Resource concept = searchResults.next(); GraphNode conceptGraphNode = new GraphNode(concept, conceptCacheEntryGraphNode.getGraph()); conceptGraphNode.deleteProperties(OWL.sameAs); conceptGraphNode.deleteProperties(RDFS.comment); conceptGraphNode.deleteProperties(SKOS.prefLabel); conceptGraphNode.deleteProperties(RDF.type); } conceptCacheEntryGraphNode.deleteProperties(CONCEPTS.searchResult); conceptCacheEntryGraphNode.deleteProperties(DC.date); conceptCacheEntryGraphNode.deleteProperties(CONCEPTS.searchTerm); conceptCacheEntryGraphNode.deleteProperties(RDF.type); }