@Test public void testUpdateSeveralResources() throws LiveContextException { List<Statement> toAdd = new ArrayList<Statement>(); List<Statement> toRemove = new ArrayList<Statement>(); URI environment = new URIImpl("urn:uuid:"+UUID.randomUUID()); URI temp = new URIImpl("urn:uuid:"+UUID.randomUUID()); URI weather = new URIImpl("urn:uuid:"+UUID.randomUUID()); toAdd.add(new StatementImpl(null, environment, RDF.type, DCON.Environment)); toAdd.add(new StatementImpl(null, environment, DCON.currentTemperature, temp)); toAdd.add(new StatementImpl(null, environment, DCON.currentWeather, weather)); toAdd.add(new StatementImpl(null, temp, RDF.type, DPO.Temperature)); toAdd.add(new StatementImpl(null, temp, DCON.temperature, new DatatypeLiteralImpl("34.0", XSD._double))); toAdd.add(new StatementImpl(null, weather, RDF.type, DPO.WeatherConditions)); toAdd.add(new StatementImpl(null, weather, DCON.cloudcover, new DatatypeLiteralImpl("5", XSD._int))); strategy.update(toAdd, toRemove); Resource aspectURI = null; URI weatherConditionsURI = null; URI temperatureURI = null; assertEquals(0, this.previousContext.size()); assertEquals(7, this.liveContext.size()); assertTrue(this.liveContext.findStatements(environment, RDF.type, DCON.Environment).hasNext()); aspectURI = this.liveContext.findStatements(environment, RDF.type, DCON.Environment).next().getSubject(); assertTrue(this.liveContext.findStatements(aspectURI, DCON.currentWeather, Variable.ANY).hasNext()); weatherConditionsURI = this.liveContext.findStatements(aspectURI, DCON.currentWeather, Variable.ANY).next().getObject().asURI(); assertTrue(this.liveContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).hasNext()); temperatureURI = this.liveContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).next().getObject().asURI(); assertTrue(this.liveContext.findStatements(temperatureURI, RDF.type, DPO.Temperature).hasNext()); assertTrue(this.liveContext.findStatements(temperatureURI, DCON.temperature, new DatatypeLiteralImpl("34.0", XSD._double)).hasNext()); assertTrue(this.liveContext.findStatements(weatherConditionsURI, RDF.type, DPO.WeatherConditions).hasNext()); assertTrue(this.liveContext.findStatements(weatherConditionsURI, DCON.cloudcover, new DatatypeLiteralImpl("5", XSD._int)).hasNext()); toAdd.clear(); toRemove.clear(); toAdd.add(new StatementImpl(null, temp, DCON.temperature, new DatatypeLiteralImpl("36.0", XSD._double))); toRemove.add(new StatementImpl(null, temp, DCON.temperature, new DatatypeLiteralImpl("34.0", XSD._double))); strategy.update(toAdd, toRemove); assertEquals(5, this.previousContext.size()); assertTrue(this.previousContext.findStatements(environment, RDF.type, DCON.Environment).hasNext()); assertTrue(this.previousContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).hasNext()); temperatureURI = this.previousContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).next().getObject().asURI(); assertTrue(this.previousContext.findStatements(temperatureURI, RDF.type, DPO.Temperature).hasNext()); assertTrue(this.previousContext.findStatements(temperatureURI, DCON.temperature, new DatatypeLiteralImpl("34.0", XSD._double)).hasNext()); assertEquals(7, this.liveContext.size()); assertTrue(this.liveContext.findStatements(environment, RDF.type, DCON.Environment).hasNext()); aspectURI = this.liveContext.findStatements(environment, RDF.type, DCON.Environment).next().getSubject(); assertTrue(this.liveContext.findStatements(aspectURI, DCON.currentWeather, Variable.ANY).hasNext()); weatherConditionsURI = this.liveContext.findStatements(aspectURI, DCON.currentWeather, Variable.ANY).next().getObject().asURI(); assertTrue(this.liveContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).hasNext()); temperatureURI = this.liveContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).next().getObject().asURI(); assertTrue(this.liveContext.findStatements(temperatureURI, RDF.type, DPO.Temperature).hasNext()); assertTrue(this.liveContext.findStatements(temperatureURI, DCON.temperature, new DatatypeLiteralImpl("36.0", XSD._double)).hasNext()); assertTrue(this.liveContext.findStatements(weatherConditionsURI, RDF.type, DPO.WeatherConditions).hasNext()); assertTrue(this.liveContext.findStatements(weatherConditionsURI, DCON.cloudcover, new DatatypeLiteralImpl("5", XSD._int)).hasNext()); toAdd.clear(); toRemove.clear(); toAdd.add(new StatementImpl(null, weather, DCON.humidity, new DatatypeLiteralImpl("70", XSD._int))); strategy.update(toAdd, toRemove); assertEquals(7, this.previousContext.size()); assertTrue(this.previousContext.findStatements(environment, RDF.type, DCON.Environment).hasNext()); aspectURI = this.previousContext.findStatements(environment, RDF.type, DCON.Environment).next().getSubject(); assertTrue(this.previousContext.findStatements(aspectURI, DCON.currentWeather, Variable.ANY).hasNext()); weatherConditionsURI = this.previousContext.findStatements(aspectURI, DCON.currentWeather, Variable.ANY).next().getObject().asURI(); assertTrue(this.previousContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).hasNext()); temperatureURI = this.previousContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).next().getObject().asURI(); assertTrue(this.previousContext.findStatements(temperatureURI, RDF.type, DPO.Temperature).hasNext()); assertTrue(this.previousContext.findStatements(temperatureURI, DCON.temperature, new DatatypeLiteralImpl("34.0", XSD._double)).hasNext()); assertTrue(this.previousContext.findStatements(weatherConditionsURI, RDF.type, DPO.WeatherConditions).hasNext()); assertTrue(this.previousContext.findStatements(weatherConditionsURI, DCON.cloudcover, new DatatypeLiteralImpl("5", XSD._int)).hasNext()); assertEquals(8, this.liveContext.size()); assertTrue(this.liveContext.findStatements(environment, RDF.type, DCON.Environment).hasNext()); aspectURI = this.liveContext.findStatements(environment, RDF.type, DCON.Environment).next().getSubject(); assertTrue(this.liveContext.findStatements(aspectURI, DCON.currentWeather, Variable.ANY).hasNext()); weatherConditionsURI = this.liveContext.findStatements(aspectURI, DCON.currentWeather, Variable.ANY).next().getObject().asURI(); assertTrue(this.liveContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).hasNext()); temperatureURI = this.liveContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).next().getObject().asURI(); assertTrue(this.liveContext.findStatements(temperatureURI, RDF.type, DPO.Temperature).hasNext()); assertTrue(this.liveContext.findStatements(temperatureURI, DCON.temperature, new DatatypeLiteralImpl("36.0", XSD._double)).hasNext()); assertTrue(this.liveContext.findStatements(weatherConditionsURI, RDF.type, DPO.WeatherConditions).hasNext()); assertTrue(this.liveContext.findStatements(weatherConditionsURI, DCON.cloudcover, new DatatypeLiteralImpl("5", XSD._int)).hasNext()); assertTrue(this.liveContext.findStatements(weatherConditionsURI, DCON.humidity, new DatatypeLiteralImpl("70", XSD._int)).hasNext()); toAdd.clear(); toRemove.clear(); toAdd.add(new StatementImpl(null, temp, DCON.temperature, new DatatypeLiteralImpl("20.0", XSD._double))); toRemove.add(new StatementImpl(null, temp, DCON.temperature, new DatatypeLiteralImpl("36.0", XSD._double))); strategy.update(toAdd, toRemove); toAdd.clear(); toRemove.clear(); toAdd.add(new StatementImpl(null, weather, DCON.humidity, new DatatypeLiteralImpl("80", XSD._int))); toRemove.add(new StatementImpl(null, weather, DCON.humidity, new DatatypeLiteralImpl("70", XSD._int))); strategy.update(toAdd, toRemove); assertEquals(8, this.previousContext.size()); assertTrue(this.previousContext.findStatements(environment, RDF.type, DCON.Environment).hasNext()); aspectURI = this.previousContext.findStatements(environment, RDF.type, DCON.Environment).next().getSubject(); assertTrue(this.previousContext.findStatements(aspectURI, DCON.currentWeather, Variable.ANY).hasNext()); weatherConditionsURI = this.previousContext.findStatements(aspectURI, DCON.currentWeather, Variable.ANY).next().getObject().asURI(); assertTrue(this.previousContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).hasNext()); temperatureURI = this.previousContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).next().getObject().asURI(); assertTrue(this.previousContext.findStatements(temperatureURI, RDF.type, DPO.Temperature).hasNext()); assertTrue(this.previousContext.findStatements(temperatureURI, DCON.temperature, new DatatypeLiteralImpl("36.0", XSD._double)).hasNext()); assertTrue(this.previousContext.findStatements(weatherConditionsURI, RDF.type, DPO.WeatherConditions).hasNext()); assertTrue(this.previousContext.findStatements(weatherConditionsURI, DCON.cloudcover, new DatatypeLiteralImpl("5", XSD._int)).hasNext()); assertTrue(this.previousContext.findStatements(weatherConditionsURI, DCON.humidity, new DatatypeLiteralImpl("70", XSD._int)).hasNext()); assertEquals(8, this.liveContext.size()); assertTrue(this.liveContext.findStatements(environment, RDF.type, DCON.Environment).hasNext()); aspectURI = this.liveContext.findStatements(environment, RDF.type, DCON.Environment).next().getSubject(); assertTrue(this.liveContext.findStatements(aspectURI, DCON.currentWeather, Variable.ANY).hasNext()); weatherConditionsURI = this.liveContext.findStatements(aspectURI, DCON.currentWeather, Variable.ANY).next().getObject().asURI(); assertTrue(this.liveContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).hasNext()); temperatureURI = this.liveContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).next().getObject().asURI(); assertTrue(this.liveContext.findStatements(temperatureURI, RDF.type, DPO.Temperature).hasNext()); assertTrue(this.liveContext.findStatements(temperatureURI, DCON.temperature, new DatatypeLiteralImpl("20.0", XSD._double)).hasNext()); assertTrue(this.liveContext.findStatements(weatherConditionsURI, RDF.type, DPO.WeatherConditions).hasNext()); assertTrue(this.liveContext.findStatements(weatherConditionsURI, DCON.cloudcover, new DatatypeLiteralImpl("5", XSD._int)).hasNext()); assertTrue(this.liveContext.findStatements(weatherConditionsURI, DCON.humidity, new DatatypeLiteralImpl("80", XSD._int)).hasNext()); toAdd.clear(); toRemove.clear(); toAdd.add(new StatementImpl(null, weather, DCON.cloudcover, new DatatypeLiteralImpl("10", XSD._int))); toAdd.add(new StatementImpl(null, weather, DCON.humidity, new DatatypeLiteralImpl("70", XSD._int))); toRemove.add(new StatementImpl(null, weather, DCON.cloudcover, new DatatypeLiteralImpl("5", XSD._int))); toRemove.add(new StatementImpl(null, weather, DCON.humidity, new DatatypeLiteralImpl("80", XSD._int))); strategy.update(toAdd, toRemove); assertEquals(8, this.previousContext.size()); assertTrue(this.previousContext.findStatements(environment, RDF.type, DCON.Environment).hasNext()); aspectURI = this.previousContext.findStatements(environment, RDF.type, DCON.Environment).next().getSubject(); assertTrue(this.previousContext.findStatements(aspectURI, DCON.currentWeather, Variable.ANY).hasNext()); weatherConditionsURI = this.previousContext.findStatements(aspectURI, DCON.currentWeather, Variable.ANY).next().getObject().asURI(); assertTrue(this.previousContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).hasNext()); temperatureURI = this.previousContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).next().getObject().asURI(); assertTrue(this.previousContext.findStatements(temperatureURI, RDF.type, DPO.Temperature).hasNext()); assertTrue(this.previousContext.findStatements(temperatureURI, DCON.temperature, new DatatypeLiteralImpl("36.0", XSD._double)).hasNext()); assertTrue(this.previousContext.findStatements(weatherConditionsURI, RDF.type, DPO.WeatherConditions).hasNext()); assertTrue(this.previousContext.findStatements(weatherConditionsURI, DCON.cloudcover, new DatatypeLiteralImpl("5", XSD._int)).hasNext()); assertTrue(this.previousContext.findStatements(weatherConditionsURI, DCON.humidity, new DatatypeLiteralImpl("80", XSD._int)).hasNext()); assertEquals(8, this.liveContext.size()); assertTrue(this.liveContext.findStatements(environment, RDF.type, DCON.Environment).hasNext()); aspectURI = this.liveContext.findStatements(environment, RDF.type, DCON.Environment).next().getSubject(); assertTrue(this.liveContext.findStatements(aspectURI, DCON.currentWeather, Variable.ANY).hasNext()); weatherConditionsURI = this.liveContext.findStatements(aspectURI, DCON.currentWeather, Variable.ANY).next().getObject().asURI(); assertTrue(this.liveContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).hasNext()); temperatureURI = this.liveContext.findStatements(aspectURI, DCON.currentTemperature, Variable.ANY).next().getObject().asURI(); assertTrue(this.liveContext.findStatements(temperatureURI, RDF.type, DPO.Temperature).hasNext()); assertTrue(this.liveContext.findStatements(temperatureURI, DCON.temperature, new DatatypeLiteralImpl("20.0", XSD._double)).hasNext()); assertTrue(this.liveContext.findStatements(weatherConditionsURI, RDF.type, DPO.WeatherConditions).hasNext()); assertTrue(this.liveContext.findStatements(weatherConditionsURI, DCON.cloudcover, new DatatypeLiteralImpl("10", XSD._int)).hasNext()); assertTrue(this.liveContext.findStatements(weatherConditionsURI, DCON.humidity, new DatatypeLiteralImpl("70", XSD._int)).hasNext()); }
@Override public void update(List<Statement> toAdd, List<Statement> toRemove) { ClosableIterator<Statement> lcStmts = liveContext.iterator(); List<URI> aspects = new ArrayList<URI>(); List<Statement> toKeep = new ArrayList<Statement>(); while (lcStmts.hasNext()) { Statement statement = lcStmts.next(); Node object = statement.getObject(); if (object instanceof URI && ArrayUtils.contains(ASPECTS, object.asURI())) { aspects.add(statement.getSubject().asURI()); toKeep.add(statement); } } lcStmts.close(); lcStmts = liveContext.iterator(); while (lcStmts.hasNext()) { Statement statement = lcStmts.next(); Resource subject = statement.getSubject(); if (subject instanceof URI && aspects.contains(subject.asURI())) { toKeep.add(statement); } } lcStmts.close(); for (Statement stmt : toRemove) { URI resource = stmt.getSubject().asURI(); if (!aspects.contains(resource)) { previousContext.removeStatements(resource.asResource(), Variable.ANY, Variable.ANY); } } List<Statement> all = new ArrayList<Statement>(); all.addAll(toAdd); all.addAll(toRemove); for (Statement stmt : all) { URI resource = stmt.getSubject().asURI(); if (!aspects.contains(resource)) { previousContext.addAll(liveContext.findStatements(resource, Variable.ANY, Variable.ANY)); } } previousContext.addAll(toKeep.iterator()); liveContext.removeAll(toRemove.iterator()); liveContext.addAll(toAdd.iterator()); }
AspectBasedStrategy implements UpdateStrategy { @Override public void update(List<Statement> toAdd, List<Statement> toRemove) { ClosableIterator<Statement> lcStmts = liveContext.iterator(); List<URI> aspects = new ArrayList<URI>(); List<Statement> toKeep = new ArrayList<Statement>(); while (lcStmts.hasNext()) { Statement statement = lcStmts.next(); Node object = statement.getObject(); if (object instanceof URI && ArrayUtils.contains(ASPECTS, object.asURI())) { aspects.add(statement.getSubject().asURI()); toKeep.add(statement); } } lcStmts.close(); lcStmts = liveContext.iterator(); while (lcStmts.hasNext()) { Statement statement = lcStmts.next(); Resource subject = statement.getSubject(); if (subject instanceof URI && aspects.contains(subject.asURI())) { toKeep.add(statement); } } lcStmts.close(); for (Statement stmt : toRemove) { URI resource = stmt.getSubject().asURI(); if (!aspects.contains(resource)) { previousContext.removeStatements(resource.asResource(), Variable.ANY, Variable.ANY); } } List<Statement> all = new ArrayList<Statement>(); all.addAll(toAdd); all.addAll(toRemove); for (Statement stmt : all) { URI resource = stmt.getSubject().asURI(); if (!aspects.contains(resource)) { previousContext.addAll(liveContext.findStatements(resource, Variable.ANY, Variable.ANY)); } } previousContext.addAll(toKeep.iterator()); liveContext.removeAll(toRemove.iterator()); liveContext.addAll(toAdd.iterator()); } }
AspectBasedStrategy implements UpdateStrategy { @Override public void update(List<Statement> toAdd, List<Statement> toRemove) { ClosableIterator<Statement> lcStmts = liveContext.iterator(); List<URI> aspects = new ArrayList<URI>(); List<Statement> toKeep = new ArrayList<Statement>(); while (lcStmts.hasNext()) { Statement statement = lcStmts.next(); Node object = statement.getObject(); if (object instanceof URI && ArrayUtils.contains(ASPECTS, object.asURI())) { aspects.add(statement.getSubject().asURI()); toKeep.add(statement); } } lcStmts.close(); lcStmts = liveContext.iterator(); while (lcStmts.hasNext()) { Statement statement = lcStmts.next(); Resource subject = statement.getSubject(); if (subject instanceof URI && aspects.contains(subject.asURI())) { toKeep.add(statement); } } lcStmts.close(); for (Statement stmt : toRemove) { URI resource = stmt.getSubject().asURI(); if (!aspects.contains(resource)) { previousContext.removeStatements(resource.asResource(), Variable.ANY, Variable.ANY); } } List<Statement> all = new ArrayList<Statement>(); all.addAll(toAdd); all.addAll(toRemove); for (Statement stmt : all) { URI resource = stmt.getSubject().asURI(); if (!aspects.contains(resource)) { previousContext.addAll(liveContext.findStatements(resource, Variable.ANY, Variable.ANY)); } } previousContext.addAll(toKeep.iterator()); liveContext.removeAll(toRemove.iterator()); liveContext.addAll(toAdd.iterator()); } AspectBasedStrategy(Model previousContext, Model liveContext); }
AspectBasedStrategy implements UpdateStrategy { @Override public void update(List<Statement> toAdd, List<Statement> toRemove) { ClosableIterator<Statement> lcStmts = liveContext.iterator(); List<URI> aspects = new ArrayList<URI>(); List<Statement> toKeep = new ArrayList<Statement>(); while (lcStmts.hasNext()) { Statement statement = lcStmts.next(); Node object = statement.getObject(); if (object instanceof URI && ArrayUtils.contains(ASPECTS, object.asURI())) { aspects.add(statement.getSubject().asURI()); toKeep.add(statement); } } lcStmts.close(); lcStmts = liveContext.iterator(); while (lcStmts.hasNext()) { Statement statement = lcStmts.next(); Resource subject = statement.getSubject(); if (subject instanceof URI && aspects.contains(subject.asURI())) { toKeep.add(statement); } } lcStmts.close(); for (Statement stmt : toRemove) { URI resource = stmt.getSubject().asURI(); if (!aspects.contains(resource)) { previousContext.removeStatements(resource.asResource(), Variable.ANY, Variable.ANY); } } List<Statement> all = new ArrayList<Statement>(); all.addAll(toAdd); all.addAll(toRemove); for (Statement stmt : all) { URI resource = stmt.getSubject().asURI(); if (!aspects.contains(resource)) { previousContext.addAll(liveContext.findStatements(resource, Variable.ANY, Variable.ANY)); } } previousContext.addAll(toKeep.iterator()); liveContext.removeAll(toRemove.iterator()); liveContext.addAll(toAdd.iterator()); } AspectBasedStrategy(Model previousContext, Model liveContext); @Override void update(List<Statement> toAdd, List<Statement> toRemove); }
AspectBasedStrategy implements UpdateStrategy { @Override public void update(List<Statement> toAdd, List<Statement> toRemove) { ClosableIterator<Statement> lcStmts = liveContext.iterator(); List<URI> aspects = new ArrayList<URI>(); List<Statement> toKeep = new ArrayList<Statement>(); while (lcStmts.hasNext()) { Statement statement = lcStmts.next(); Node object = statement.getObject(); if (object instanceof URI && ArrayUtils.contains(ASPECTS, object.asURI())) { aspects.add(statement.getSubject().asURI()); toKeep.add(statement); } } lcStmts.close(); lcStmts = liveContext.iterator(); while (lcStmts.hasNext()) { Statement statement = lcStmts.next(); Resource subject = statement.getSubject(); if (subject instanceof URI && aspects.contains(subject.asURI())) { toKeep.add(statement); } } lcStmts.close(); for (Statement stmt : toRemove) { URI resource = stmt.getSubject().asURI(); if (!aspects.contains(resource)) { previousContext.removeStatements(resource.asResource(), Variable.ANY, Variable.ANY); } } List<Statement> all = new ArrayList<Statement>(); all.addAll(toAdd); all.addAll(toRemove); for (Statement stmt : all) { URI resource = stmt.getSubject().asURI(); if (!aspects.contains(resource)) { previousContext.addAll(liveContext.findStatements(resource, Variable.ANY, Variable.ANY)); } } previousContext.addAll(toKeep.iterator()); liveContext.removeAll(toRemove.iterator()); liveContext.addAll(toAdd.iterator()); } AspectBasedStrategy(Model previousContext, Model liveContext); @Override void update(List<Statement> toAdd, List<Statement> toRemove);  }