/** * Tries to return the items of a list or empty if th elist is empty or not an rdf:List */ public static Collection<RDFNode> getListItemsOrEmpty(RDFNode node) { ImmutableList.Builder<RDFNode> items = ImmutableList.builder(); if (isList(node)) { RDFList rdfList = node.as(RDFList.class); rdfList.iterator().forEachRemaining(items::add); } return items.build(); }
private static List<RDFNode> asJavaList( Resource resource ) { return (resource.as( RDFList.class )).asJavaList(); }
/** * <p>Answer a set of all of the RDF statements whose subject is one of the cells * of this list.</p> * @return A list of the statements that form the encoding of this list. */ public Set<Statement> collectStatements() { Set<Statement> stmts = new HashSet<>(); RDFList l = this; do { // collect all statements of this list cell for (Iterator<Statement> i = l.listProperties(); i.hasNext(); ) { stmts.add( i.next() ); } // move on to next cell l = l.getTail(); } while (!l.isEmpty()); return stmts; }
/** * Removes val from underlying list. * * @param val * @return the modified RDFList. */ private RDFList baseRemove(final RDFList val) { RDFList prev = null; RDFList cell = holder.getBaseItem(); final boolean searching = true; while (searching && !cell.isEmpty()) { if (cell.equals(val)) { // found the value to be removed final RDFList tail = cell.getTail(); if (prev != null) { prev.setTail(tail); } cell.removeProperties(); // return this unless we have removed the head element return (prev == null) ? tail : this; } else { // not found yet prev = cell; cell = cell.getTail(); } } // not found return this; }
list0 = list0.cons(r0); list1 = list1.cons(r1); while (!list0.isEmpty()) list0 = list0.removeHead(); checkValid("removeTest0", list0, true); list1.removeList(); RDFList list2 = (nil.as(RDFList.class)).cons(r2).cons(r1).cons(r0); Assert.assertTrue("list should contain x ", list2.contains(r0)); Assert.assertTrue("list should contain y ", list2.contains(r1)); Assert.assertTrue("list should contain z ", list2.contains(r2)); list2 = list2.remove(r1); Assert.assertTrue("list should contain x ", list2.contains(r0)); Assert.assertTrue("list should contain y ", !list2.contains(r1)); Assert.assertTrue("list should contain z ", list2.contains(r2)); list2 = list2.remove(r0); Assert.assertTrue("list should contain x ", !list2.contains(r0)); Assert.assertTrue("list should contain y ", !list2.contains(r1)); Assert.assertTrue("list should contain z ", list2.contains(r2)); list2 = list2.remove(r2); Assert.assertTrue("list should contain x ", !list2.contains(r0)); Assert.assertTrue("list should contain y ", !list2.contains(r1));
@Override public void concatenate(final RDFList list) throws UpdateDeniedException, AddDeniedException, AuthenticationRequiredException { checkUpdate(); if (holder.getBaseItem().isEmpty()) { // concatenating list onto the empty list is an error throw new EmptyListUpdateException( "Tried to concatenate onto the empty list"); } else { Triple t = new Triple(SecurityEvaluator.FUTURE, listFirst() .asNode(), Node.ANY); if (!canCreate(t)) { final ExtendedIterator<RDFNode> iter = list.iterator(); try { while (iter.hasNext()) { t = new Triple(SecurityEvaluator.FUTURE, listFirst() .asNode(), iter.next().asNode()); checkCreate(t); } } finally { iter.close(); } } holder.getBaseItem().concatenate(list); } }
public void testConcatenate() { model.read(getFileName("ontology/list5.rdf")); final Resource nil = model.getResource(RDF.nil.getURI()); RDFList list = nil.as(RDFList.class); final Resource r = model.createResource(TestList.NS + "foo"); // create a list of foos for (int i = 0; i < 5; i++) { list = list.cons(r); } final int listLen = list.size(); // now append foos to the root list final RDFList root = getListRoot(model); final int rootLen = root.size(); root.concatenate(list); // original list should be unchanged checkValid("concatTest0", list, true); Assert.assertEquals("Original list should be unchanged", listLen, list.size()); // but lhs list has changed checkValid("concatTest1", root, true); Assert.assertEquals("Root list should be new length", rootLen + listLen, root.size()); }
@Override public RDFList append(final RDFList list) throws ReadDeniedException, AuthenticationRequiredException { if (holder.getBaseItem().isEmpty()) { return list.size() == 0 ? ModelFactory.createDefaultModel() .createList() : list.copy(); } else { final RDFList copy = copy(); if (list.size() > 0) { copy.concatenate(list.copy()); } return copy; } }
public void testCreateListFromEmptyIterator() { RDFList list = model.createList(new ArrayList<RDFNode>().iterator()); Assert.assertEquals(0, list.size()); }
@Override public SecuredRDFList with(final RDFNode value) throws UpdateDeniedException, AddDeniedException, AuthenticationRequiredException { checkUpdate(); checkCreate(new Triple(SecurityEvaluator.FUTURE, listFirst().asNode(), value.asNode())); return SecuredRDFListImpl.getInstance(getModel(), holder.getBaseItem() .with(value)); } }
public void testConcatenate2() { model.read(getFileName("ontology/list5.rdf")); final Resource a = model.createResource(TestList.NS + "a"); // create a list of foos final Resource[] rs = new Resource[] { model.createResource(TestList.NS + "b"), model.createResource(TestList.NS + "c"), model.createResource(TestList.NS + "d"), model.createResource(TestList.NS + "e") }; final RDFList aList = model.createList().cons(a); final RDFList rsList = model.createList(rs); // concatenate the above resources onto the empty list aList.concatenate(rsList); checkValid("concatTest3", aList, true); final RDFList root = getListRoot(model); Assert.assertTrue("Constructed and loaded lists should be the same", aList.sameListAs(root)); }
public void testAppend() { model.read(getFileName("ontology/list5.rdf")); final Resource nil = model.getResource(RDF.nil.getURI()); RDFList list = nil.as(RDFList.class); final Resource r = model.createResource(TestList.NS + "foo"); // create a list of foos for (int i = 0; i < 5; i++) { list = list.cons(r); } final int listLen = list.size(); // now append foos to the root list final RDFList root = getListRoot(model); final int rootLen = root.size(); final RDFList appended = root.append(list); // original list should be unchanged checkValid("appendTest0", root, true); Assert.assertEquals("Original list should be unchanged", rootLen, root.size()); checkValid("appendTest1", list, true); Assert.assertEquals("Original list should be unchanged", listLen, list.size()); // new list should be length of combined checkValid("appendTest2", appended, true); Assert.assertEquals("Appended list not correct length", rootLen + listLen, appended.size()); }
/** Add the given value to a list which is the value of the given property */ protected void addListPropertyValue( Property p, String name, RDFNode value ) { checkProfile( p, name ); // get the list value if (hasProperty( p )) { RDFNode cur = getRequiredProperty( p ).getObject(); if (!cur.canAs( RDFList.class )) { throw new OntologyException( "Tried to add a value to a list-valued property " + p + " but the current value is not a list: " + cur ); } RDFList values = cur.as( RDFList.class ); // now add our value to the list if (!values.contains( value )){ RDFList newValues = values.with( value ); // if the previous values was nil, the return value will be a new list if (newValues != values) { removeAll( p ); addProperty( p, newValues ); } } } else { // create a new list to hold the only value we know so far addProperty( p, ((OntModel) getModel()).createList( new RDFNode[] {value} ) ); } }
/** * <p>Remove the statement that this enumeration includes <code>res</code> among its members. If this statement * is not true of the current model, nothing happens.</p> * @param res A resource that may be declared to be part of this enumeration, and which is * no longer one of the enumeration values. */ @Override public void removeOneOf( Resource res ) { setOneOf( getOneOf().remove( res ) ); }
public void testTail() { for (int i = 0; i <= 5; i++) { model.read( getFileName("ontology/list" + i + ".rdf")); RDFList l0 = getListRoot(model); // get the tail n times, should be nil at the end for (int j = 0; j < i; j++) { l0 = l0.getTail(); } Assert.assertTrue("Should have reached the end of the list after " + i + " getTail()'s", l0.isEmpty()); } }
public void testSetTail() { final Model m = ModelFactory.createDefaultModel(); final Resource root = m.createResource(TestList.NS + "root"); final Property p = m.createProperty(TestList.NS, "p"); final Resource nil = RDF.nil; m.add(nil, RDF.type, RDF.List); final Resource list0 = m.createResource(); m.add(list0, RDF.type, RDF.List); m.add(list0, RDF.first, "fred"); m.add(list0, RDF.rest, nil); m.add(root, p, list0); final RDFList l1 = getListRoot(m); checkValid("settail1", l1, true); final Resource list1 = m.createResource(); m.add(list1, RDF.type, RDF.List); m.add(list1, RDF.first, "george"); m.add(list1, RDF.rest, nil); final RDFList l2 = list1.as(RDFList.class); Assert.assertNotNull("as(RDFList) should not return null for root", l2); checkValid("settail2", l2, true); Assert.assertEquals("l1 should have length 1", 1, l1.size()); Assert.assertEquals("l2 should have length 1", 1, l2.size()); // use set tail to join the lists together l1.setTail(l2); checkValid("settail3", l1, true); checkValid("settail4", l2, true); Assert.assertEquals("l1 should have length 2", 2, l1.size()); Assert.assertEquals("l2 should have length 1", 1, l2.size()); }
if (goodList.getModel() == null) { goodList = securedModel.createList(rdfList.asJavaList().iterator());
@Override public void concatenate(final Iterator<? extends RDFNode> nodes) throws UpdateDeniedException, AddDeniedException, AuthenticationRequiredException { checkUpdate(); if (holder.getBaseItem().isEmpty()) { // concatenating list onto the empty list is an error throw new EmptyListUpdateException( "Tried to concatenate onto the empty list"); } else { Triple t = new Triple(SecurityEvaluator.FUTURE, listFirst() .asNode(), Node.ANY); if (!canCreate(t)) { final List<RDFNode> list = new ArrayList<>(); while (nodes.hasNext()) { final RDFNode n = nodes.next(); t = new Triple(SecurityEvaluator.FUTURE, listFirst() .asNode(), n.asNode()); checkCreate(t); list.add(n); } holder.getBaseItem().concatenate(list.iterator()); } else { holder.getBaseItem().concatenate(nodes); } } }