@Test public void testAddRemoveElementFromLiveContext() throws Exception { List<Statement> toAdd = new ArrayList<Statement>(); List<Statement> toRemove = new ArrayList<Statement>(); URI ismael = new URIImpl("urn:ismael"); toAdd.add(new StatementImpl(null, PEERS, DCON.nearbyPerson, ismael)); strategy.update(toAdd, toRemove); toAdd.clear(); toRemove.clear(); toRemove.add(new StatementImpl(null, PEERS, DCON.nearbyPerson, ismael)); strategy.update(toAdd, toRemove); toAdd.clear(); toRemove.clear(); assertTrue(previousContext.contains(PEERS, DCON.nearbyPerson, ismael)); assertFalse(liveContext.contains(PEERS, DCON.nearbyPerson, Variable.ANY)); }
@Override public void update(List<Statement> toAdd, List<Statement> toRemove) { Model newData = RDF2Go.getModelFactory().createModel().open(); for (Statement s : toAdd) { newData.addStatement(s); } Set<URI> elements = new HashSet<URI>(); elements.addAll(findElements(toAdd)); elements.addAll(findElements(toRemove)); logger.debug("Updating live context... elements to be updated: "+StringUtils.join(elements, ", ")); for (URI element : elements) { if (liveContext.contains(Variable.ANY, Variable.ANY, element)) { previousContext.removeStatements(Variable.ANY, Variable.ANY, element); previousContext.removeStatements(element, Variable.ANY, Variable.ANY); previousContext.addAll(liveContext.findStatements(Variable.ANY, Variable.ANY, element)); previousContext.addAll(liveContext.findStatements(element, Variable.ANY, Variable.ANY)); } else { URI aspect = newData.findStatements(Variable.ANY, Variable.ANY, element).next().getSubject().asURI(); String query = "SELECT DISTINCT ?element WHERE { "+aspect.toSPARQL()+" ?p ?element . }"; Set<URI> existing = new HashSet<URI>(); ClosableIterator<QueryRow> results = liveContext.sparqlSelect(query).iterator(); while (results.hasNext()) { existing.add(results.next().getValue("element").asURI()); } results.close(); for (URI e : existing) { if (!previousContext.contains(aspect, Variable.ANY, e)) { previousContext.addAll(liveContext.findStatements(aspect, Variable.ANY, e)); previousContext.addAll(liveContext.findStatements(e, Variable.ANY, Variable.ANY)); } } } } newData.close(); liveContext.removeAll(toRemove.iterator()); liveContext.addAll(toAdd.iterator()); }
ElementBasedStrategy implements UpdateStrategy { @Override public void update(List<Statement> toAdd, List<Statement> toRemove) { Model newData = RDF2Go.getModelFactory().createModel().open(); for (Statement s : toAdd) { newData.addStatement(s); } Set<URI> elements = new HashSet<URI>(); elements.addAll(findElements(toAdd)); elements.addAll(findElements(toRemove)); logger.debug("Updating live context... elements to be updated: "+StringUtils.join(elements, ", ")); for (URI element : elements) { if (liveContext.contains(Variable.ANY, Variable.ANY, element)) { previousContext.removeStatements(Variable.ANY, Variable.ANY, element); previousContext.removeStatements(element, Variable.ANY, Variable.ANY); previousContext.addAll(liveContext.findStatements(Variable.ANY, Variable.ANY, element)); previousContext.addAll(liveContext.findStatements(element, Variable.ANY, Variable.ANY)); } else { URI aspect = newData.findStatements(Variable.ANY, Variable.ANY, element).next().getSubject().asURI(); String query = "SELECT DISTINCT ?element WHERE { "+aspect.toSPARQL()+" ?p ?element . }"; Set<URI> existing = new HashSet<URI>(); ClosableIterator<QueryRow> results = liveContext.sparqlSelect(query).iterator(); while (results.hasNext()) { existing.add(results.next().getValue("element").asURI()); } results.close(); for (URI e : existing) { if (!previousContext.contains(aspect, Variable.ANY, e)) { previousContext.addAll(liveContext.findStatements(aspect, Variable.ANY, e)); previousContext.addAll(liveContext.findStatements(e, Variable.ANY, Variable.ANY)); } } } } newData.close(); liveContext.removeAll(toRemove.iterator()); liveContext.addAll(toAdd.iterator()); } }
ElementBasedStrategy implements UpdateStrategy { @Override public void update(List<Statement> toAdd, List<Statement> toRemove) { Model newData = RDF2Go.getModelFactory().createModel().open(); for (Statement s : toAdd) { newData.addStatement(s); } Set<URI> elements = new HashSet<URI>(); elements.addAll(findElements(toAdd)); elements.addAll(findElements(toRemove)); logger.debug("Updating live context... elements to be updated: "+StringUtils.join(elements, ", ")); for (URI element : elements) { if (liveContext.contains(Variable.ANY, Variable.ANY, element)) { previousContext.removeStatements(Variable.ANY, Variable.ANY, element); previousContext.removeStatements(element, Variable.ANY, Variable.ANY); previousContext.addAll(liveContext.findStatements(Variable.ANY, Variable.ANY, element)); previousContext.addAll(liveContext.findStatements(element, Variable.ANY, Variable.ANY)); } else { URI aspect = newData.findStatements(Variable.ANY, Variable.ANY, element).next().getSubject().asURI(); String query = "SELECT DISTINCT ?element WHERE { "+aspect.toSPARQL()+" ?p ?element . }"; Set<URI> existing = new HashSet<URI>(); ClosableIterator<QueryRow> results = liveContext.sparqlSelect(query).iterator(); while (results.hasNext()) { existing.add(results.next().getValue("element").asURI()); } results.close(); for (URI e : existing) { if (!previousContext.contains(aspect, Variable.ANY, e)) { previousContext.addAll(liveContext.findStatements(aspect, Variable.ANY, e)); previousContext.addAll(liveContext.findStatements(e, Variable.ANY, Variable.ANY)); } } } } newData.close(); liveContext.removeAll(toRemove.iterator()); liveContext.addAll(toAdd.iterator()); } ElementBasedStrategy(Model previousContext, Model liveContext); }
ElementBasedStrategy implements UpdateStrategy { @Override public void update(List<Statement> toAdd, List<Statement> toRemove) { Model newData = RDF2Go.getModelFactory().createModel().open(); for (Statement s : toAdd) { newData.addStatement(s); } Set<URI> elements = new HashSet<URI>(); elements.addAll(findElements(toAdd)); elements.addAll(findElements(toRemove)); logger.debug("Updating live context... elements to be updated: "+StringUtils.join(elements, ", ")); for (URI element : elements) { if (liveContext.contains(Variable.ANY, Variable.ANY, element)) { previousContext.removeStatements(Variable.ANY, Variable.ANY, element); previousContext.removeStatements(element, Variable.ANY, Variable.ANY); previousContext.addAll(liveContext.findStatements(Variable.ANY, Variable.ANY, element)); previousContext.addAll(liveContext.findStatements(element, Variable.ANY, Variable.ANY)); } else { URI aspect = newData.findStatements(Variable.ANY, Variable.ANY, element).next().getSubject().asURI(); String query = "SELECT DISTINCT ?element WHERE { "+aspect.toSPARQL()+" ?p ?element . }"; Set<URI> existing = new HashSet<URI>(); ClosableIterator<QueryRow> results = liveContext.sparqlSelect(query).iterator(); while (results.hasNext()) { existing.add(results.next().getValue("element").asURI()); } results.close(); for (URI e : existing) { if (!previousContext.contains(aspect, Variable.ANY, e)) { previousContext.addAll(liveContext.findStatements(aspect, Variable.ANY, e)); previousContext.addAll(liveContext.findStatements(e, Variable.ANY, Variable.ANY)); } } } } newData.close(); liveContext.removeAll(toRemove.iterator()); liveContext.addAll(toAdd.iterator()); } ElementBasedStrategy(Model previousContext, Model liveContext); @Override void update(List<Statement> toAdd, List<Statement> toRemove); }
ElementBasedStrategy implements UpdateStrategy { @Override public void update(List<Statement> toAdd, List<Statement> toRemove) { Model newData = RDF2Go.getModelFactory().createModel().open(); for (Statement s : toAdd) { newData.addStatement(s); } Set<URI> elements = new HashSet<URI>(); elements.addAll(findElements(toAdd)); elements.addAll(findElements(toRemove)); logger.debug("Updating live context... elements to be updated: "+StringUtils.join(elements, ", ")); for (URI element : elements) { if (liveContext.contains(Variable.ANY, Variable.ANY, element)) { previousContext.removeStatements(Variable.ANY, Variable.ANY, element); previousContext.removeStatements(element, Variable.ANY, Variable.ANY); previousContext.addAll(liveContext.findStatements(Variable.ANY, Variable.ANY, element)); previousContext.addAll(liveContext.findStatements(element, Variable.ANY, Variable.ANY)); } else { URI aspect = newData.findStatements(Variable.ANY, Variable.ANY, element).next().getSubject().asURI(); String query = "SELECT DISTINCT ?element WHERE { "+aspect.toSPARQL()+" ?p ?element . }"; Set<URI> existing = new HashSet<URI>(); ClosableIterator<QueryRow> results = liveContext.sparqlSelect(query).iterator(); while (results.hasNext()) { existing.add(results.next().getValue("element").asURI()); } results.close(); for (URI e : existing) { if (!previousContext.contains(aspect, Variable.ANY, e)) { previousContext.addAll(liveContext.findStatements(aspect, Variable.ANY, e)); previousContext.addAll(liveContext.findStatements(e, Variable.ANY, Variable.ANY)); } } } } newData.close(); liveContext.removeAll(toRemove.iterator()); liveContext.addAll(toAdd.iterator()); } ElementBasedStrategy(Model previousContext, Model liveContext); @Override void update(List<Statement> toAdd, List<Statement> toRemove);  }