@Test public void testDelete() throws Exception { Resource rhmRes = model.getResource(FakeRDFModel.rhm); Model toRemove = ModelFactory.createDefaultModel(); toRemove.add(model.listStatements(rhmRes, null, (RDFNode) null)); toRemove.add(model.listStatements(null, null, rhmRes)); model.remove(toRemove); Model foo = changes.get(0).getChange(); assert changes.size() == 1; assert !model.containsResource(rhmRes); changes.undo(); assert changes.size() == 1; assert model.containsResource(rhmRes); assert model.containsAll(foo); changes.redo(); assert changes.size() == 1; assert !model.containsResource(rhmRes); }
private void updateAddRemoveDataGetterStatements( OntModel displayModel, Model removeStatements, Model addStatements, Resource oldType, Resource newType) { log.debug("Old type: " + oldType.getURI() + " - newType: " + newType.getURI()); removeStatements.add(displayModel.listStatements(null, RDF.type, oldType)); StmtIterator oldStatements = displayModel.listStatements(null, RDF.type, oldType); while (oldStatements.hasNext()) { Statement stmt = oldStatements.nextStatement(); addStatements.add(stmt.getSubject(), RDF.type, newType); } // Print out remove and add statements here StringWriter sw = new StringWriter(); try { log.debug( "Adding statements with old data getter types to remove statements, remove statements is now"); removeStatements.write(sw, "N3"); log.debug(sw.toString()); sw.close(); sw = new StringWriter(); log.debug( "Adding statements with new data getter types to add statements, add statements is now"); addStatements.write(sw, "N3"); log.debug(sw.toString()); sw.close(); } catch (Exception ex) { log.error("Error occurred in writing out remove and statements for data getter types", ex); } }
@Test public void shouldSortTriplesForDisplay() { final Model model = createDefaultModel(); model.setNsPrefix("prefix", "namespace"); final Property propertyA = model.createProperty("namespace", "a"); final Property propertyB = model.createProperty("namespace", "b"); final Property propertyC = model.createProperty("c"); final Literal literalA = model.createLiteral("a"); final Literal literalB = model.createLiteral("b"); final Resource resourceB = model.createResource("b"); model.add(resourceB, propertyA, literalA); final Resource a = model.createResource("a"); model.add(a, propertyC, literalA); model.add(a, propertyB, literalA); model.add(a, propertyA, literalA); model.add(a, propertyA, literalB); final Iterator<Quad> iterator = DatasetFactory.create(model).asDatasetGraph().find(); final List<Quad> sortedTriples = testObj.getSortedTriples(model, iterator); sortedTriples.get(0).matches(ANY, a.asNode(), propertyA.asNode(), literalA.asNode()); sortedTriples.get(1).matches(ANY, a.asNode(), propertyA.asNode(), literalB.asNode()); sortedTriples.get(2).matches(ANY, a.asNode(), propertyB.asNode(), literalA.asNode()); sortedTriples.get(3).matches(ANY, a.asNode(), propertyC.asNode(), literalA.asNode()); sortedTriples.get(4).matches(ANY, resourceB.asNode(), propertyC.asNode(), literalA.asNode()); }
private Model createModel() { Model cubeData = ModelFactory.createDefaultModel(); Resource structure = cubeData.createResource(STRUCTURE, QB.DataStructureDefinition); Resource c1 = cubeData.createResource(STRUCTURE + "/c1", QB.ComponentSpecification); c1.addProperty(RDFS.label, cubeData.createLiteral("Component Specification of Class", "en")); c1.addProperty(QB.dimension, GK.DIM.Class); Resource c2 = cubeData.createResource(STRUCTURE + "/c2", QB.ComponentSpecification); c2.addProperty(QB.measure, GK.MEASURE.PropertyCount); c2.addProperty( RDFS.label, cubeData.createLiteral("Component Specification of Number of Properties", "en")); // Resource c3 = cubeData.createResource(STRUCTURE+"/c3",QB.ComponentSpecification); // c3.addProperty(RDFS.label, cubeData.createLiteral("Component Specification of Instance", // "en")); // c3.addProperty(QB.dimension, GK.DIM.Instance); structure.addProperty(QB.component, c1); structure.addProperty(QB.component, c2); // structure.addProperty(QB.component, c3); cubeData.add(GK.DIM.ClassStatements); cubeData.add(GK.DIM.PropertyStatements); cubeData.add(GK.DIM.InstanceStatements); cubeData.add(GK.MEASURE.PropertyCountStatements); return cubeData; }
private void changeNsUri(String from, String to) { /* * o Change all usages of URI 'from' to 'to' * o Set the prefix of 'from' to be that of 'to' */ Model toAdd = ModelFactory.createDefaultModel(); Model toDel = ModelFactory.createDefaultModel(); Resource newUri = output.createResource(to); StmtIterator it = output.listStatements(output.createResource(from), null, (RDFNode) null); while (it.hasNext()) { Statement curr = it.nextStatement(); toDel.add(curr); toAdd.add(newUri, curr.getPredicate(), curr.getObject()); } it = output.listStatements(null, null, output.createResource(from)); while (it.hasNext()) { Statement curr = it.nextStatement(); toDel.add(curr); toAdd.add(curr.getSubject(), curr.getPredicate(), newUri); } output.remove(toDel); output.add(toAdd); String prefix = output.getNsURIPrefix(from); output.setNsPrefix(prefix, to); }
/** "dirty" reifications - those with conflicting quadlets - should fail. */ public void testDirtyReification() { Resource R = model.createResource(aURI); model.add(R, RDF.type, RDF.Statement); model.add(R, RDF.subject, S); model.add(R, RDF.subject, P); testDoesNotReify("boo", R); }
private void processNode( Resource node, Model queryModel, Model outputModel, Resource parent, Resource tree) { final Set<Resource> children = queryModel.listResourcesWithProperty(RDFS.subClassOf, node).toSet(); final Iterator<Resource> childIterator = children.iterator(); if (children.size() == 1) { this.processNode(childIterator.next(), queryModel, outputModel, parent, tree); } else { final Resource blankNode = outputModel.createResource(); if (parent != null) { outputModel.add(blankNode, Vocab.has_Parent, parent); } else { outputModel.add(tree, Vocab.has_Root, blankNode); } this.addLabel(blankNode, node, outputModel); outputModel.add(blankNode, RDF.type, Vocab.Node); outputModel.add(blankNode, DC.subject, node); if (children.size() > 1) { while (childIterator.hasNext()) { final Resource child = childIterator.next(); this.processNode(child, queryModel, outputModel, blankNode, tree); } } } }
/** * If the {@link ConfigurationProperties} has a name for the initial admin user, create the user * and add it to the model. */ protected void createInitialAdminUser(Model model) { String initialAdminUsername = ConfigurationProperties.getProperty("initialAdminUser"); if (initialAdminUsername == null) { return; } // A hard-coded MD5 encryption of "defaultAdmin" String initialAdminPassword = "******"; String vitroDefaultNs = DEFAULT_DEFAULT_NAMESPACE; Resource user = model.createResource(vitroDefaultNs + "defaultAdminUser"); model.add( model.createStatement( user, model.createProperty(VitroVocabulary.RDF_TYPE), model.getResource(VitroVocabulary.USER))); model.add( model.createStatement( user, model.createProperty(VitroVocabulary.USER_USERNAME), model.createTypedLiteral(initialAdminUsername))); model.add( model.createStatement( user, model.createProperty(VitroVocabulary.USER_MD5PASSWORD), model.createTypedLiteral(initialAdminPassword))); model.add( model.createStatement( user, model.createProperty(VitroVocabulary.USER_ROLE), model.createTypedLiteral("role:/50"))); }
/** * Creates a Start Element and adds it to this Process * * @return */ public Resource createStartElement() { Resource rv = null; Model model = process.getModel(); rv = model.createResource( String.format("%1$s%2$s%3$s", NAMESPACE, "START", UUID.randomUUID().toString())); model.add(rv, RDF.type, RNRM.JoinElement); model.add(process.getProcessResource(), RNRM.hasStartElement, rv); return rv; }
/** * Creates a Fork Element and adds it to this Process * * @return */ public Resource createForkElement() { Resource rv = null; Model model = process.getModel(); Resource processResource = process.getProcessResource(); rv = model.createResource( String.format("%1$s%2$s%3$s", NAMESPACE, "FORK", UUID.randomUUID().toString())); model.add(rv, RDF.type, RNRM.ForkElement); model.add(processResource, RNRM.hasElement, rv); return rv; }
/** * the simplest case: if we assert all the components of a reification quad, we can get a * ReifiedStatement that represents the reified statement. */ public void testBasicReification() { if (model.getReificationStyle() != ModelFactory.Minimal) { Resource R = model.createResource(aURI); model.add(R, RDF.type, RDF.Statement); model.add(R, RDF.subject, S); model.add(R, RDF.predicate, P); model.add(R, RDF.object, O); RDFNode rs = R.as(ReifiedStatement.class); assertEquals("can recover statement", SPO, ((ReifiedStatement) rs).getStatement()); } }
public void testListReifiedSpecificStatements() { assertEquals("no statements should match st", empty, getSetRS(model, SPO)); /* */ ReifiedStatement rs = model.createReifiedStatement(aURI, SPO); ReifiedStatement rs2 = model.createReifiedStatement(anotherURI, SPO2); model.add(rs, P, O); // assertEquals( "still no matching statement", empty, getSetRS( m, stOther ) ); /* */ Set justRS2 = arrayToSet(new Object[] {rs2}); model.add(rs2, P, O); assertEquals("now one matching statement", justRS2, getSetRS(model, SPO2)); }
public void testBulkByModel(Model m) { assertEquals("precondition: model must be empty", 0, m.size()); Model A = modelWithStatements("clouds offer rain; trees offer shelter"); Model B = modelWithStatements("x R y; y Q z; z P x"); m.add(A); assertIsoModels(A, m); m.add(B); m.remove(A); assertIsoModels(B, m); m.remove(B); assertEquals("", 0, m.size()); }
private void createProblemModel(Node resource, Node classOrProperty, Resource type) { Model m = ModelFactory.createDefaultModel(); Resource subject = m.createResource(resource.toString()); m.add(new StatementImpl(subject, QPRO.exceptionDescription, type)); if (type.equals(DQM.MisplacedClass)) m.add(new StatementImpl(subject, DQM.hasMisplacedClass, m.asRDFNode(classOrProperty))); else m.add(new StatementImpl(subject, DQM.hasMisplacedProperty, m.asRDFNode(classOrProperty))); this.problemList.add(m); }
public void doCleanLiterals(Model model) { Model retractionsModel = ModelFactory.createDefaultModel(); Model additionsModel = ModelFactory.createDefaultModel(); model.enterCriticalSection(Lock.WRITE); try { ClosableIterator<Statement> closeIt = model.listStatements(); try { for (Iterator<Statement> stmtIt = closeIt; stmtIt.hasNext(); ) { Statement stmt = stmtIt.next(); if (stmt.getObject().isLiteral()) { Literal lit = (Literal) stmt.getObject(); String lex = lit.getLexicalForm(); char[] chars = lex.toCharArray(); char[] cleanChars = new char[chars.length]; int cleanPos = 0; boolean badChar = false; for (int i = 0; i < chars.length; i++) { if (java.lang.Character.getNumericValue(chars[i]) > 31 && java.lang.Character.isDefined(chars[i])) { cleanChars[cleanPos] = chars[i]; cleanPos++; } else { log.error("Bad char in " + lex); log.error("Numeric value " + java.lang.Character.getNumericValue(chars[i])); badChar = true; } } String cleanLex = new String(cleanChars); if (badChar) { retractionsModel.add(stmt); Literal newLit = null; if (lit.getLanguage() != null && lit.getLanguage().length() > 0) { newLit = additionsModel.createLiteral(cleanLex, lit.getLanguage()); } else if (lit.getDatatype() != null) { newLit = additionsModel.createTypedLiteral(cleanLex, lit.getDatatype()); } else { newLit = additionsModel.createLiteral(cleanLex); } additionsModel.add(stmt.getSubject(), stmt.getPredicate(), newLit); } } } } finally { closeIt.close(); } model.remove(retractionsModel); model.add(additionsModel); log.debug("Cleaned " + additionsModel.size() + " literals"); } finally { model.leaveCriticalSection(); } }
private static void put(String graph, String mediaType, String lang) throws ClientProtocolException, URISyntaxException, IOException { put(model, graph, mediaType, lang); Model m = ModelFactory.createDefaultModel(); m.add(m.createResource("foo:bar2"), RDFS.label, m.createLiteral("Bar2")); put(m, graph, mediaType, lang); model.add(m); getExisting(graph, mediaType, lang); }
protected static void addRangeTypes(Model result, Model schema) { Model toAdd = ModelFactory.createDefaultModel(); for (StmtIterator it = schema.listStatements(ANY, RDFS.range, ANY); it.hasNext(); ) { Statement s = it.nextStatement(); RDFNode type = s.getObject(); Property property = s.getSubject().as(Property.class); for (StmtIterator x = result.listStatements(ANY, property, ANY); x.hasNext(); ) { RDFNode ob = x.nextStatement().getObject(); if (ob.isResource()) toAdd.add((Resource) ob, RDF.type, type); } } result.add(toAdd); }
private void createBackLinkViolation(String subjectURI, String resource) { Model m = ModelFactory.createDefaultModel(); Resource subject = m.createResource(resource); m.add(new StatementImpl(subject, QPRO.exceptionDescription, DQM.NoBackLink)); RDFNode violatedTriple = Commons.generateRDFBlankNode(); m.add( new StatementImpl(violatedTriple.asResource(), RDF.subject, m.createResource(subjectURI))); m.add(new StatementImpl(subject, DQM.hasViolatingTriple, violatedTriple)); this._problemList.add(m); }
/** * Inserts the given {@link Resource} into the rnrm:isFlowingTo path after the specified {@link * Resource} * * @param toInsert Process element to insert * @param after Process element to insert after */ public void insertAfter(Resource toInsert, Resource after) { List<Statement> outgoing = process.getOutgoingEdges(after); Model model = process.getModel(); if (outgoing.size() > 1) { throw new IllegalArgumentException("Cannot insert after nodes with multiple outgoing links."); } model.add(after, RNRM.isFlowingTo, toInsert); if (outgoing.size() > 0) { Statement stmt = outgoing.get(0); Resource next = stmt.getResource(); model.remove(stmt); model.add(toInsert, RNRM.isFlowingTo, next); } }
public Model getTriples() { Model m = ModelFactory.createDefaultModel(); m.add(unknowns.getTriples()); m.add(booleans.getTriples()); m.add(strings.getTriples()); m.add(integers.getTriples()); m.add(doubles.getTriples()); m.add(floats.getTriples()); m.add(dates.getTriples()); m.add(gYears.getTriples()); m.add(gYearMonths.getTriples()); m.add(gMonthDays.getTriples()); m.add(times.getTriples()); return m; }
protected static void addSupertypes(Model result) { Model temp = ModelFactory.createDefaultModel(); for (StmtIterator it = result.listStatements(ANY, RDF.type, ANY); it.hasNext(); ) { Statement s = it.nextStatement(); Resource c = AssemblerHelp.getResource(s); for (StmtIterator subclasses = result.listStatements(c, RDFS.subClassOf, ANY); subclasses.hasNext(); ) { RDFNode type = subclasses.nextStatement().getObject(); // System.err.println( ">> adding super type: subject " + s.getSubject() + ", type " + type // ); temp.add(s.getSubject(), RDF.type, type); } } result.add(temp); }
/** * Inserts the given {@link Resource} into the rnrm:isFlowingTo path before the specified {@link * Resource} * * @param toInsert Process element to insert * @param before Process element to insert before */ public void insertBefore(Resource toInsert, Resource before) { List<Statement> incoming = process.getIncomingEdges(before); Model model = process.getModel(); if (incoming.size() > 1) { throw new IllegalArgumentException( "Cannot insert before nodes with multiple incoming links."); } model.add(toInsert, RNRM.isFlowingTo, before); if (incoming.size() > 0) { Statement stmt = incoming.get(0); Resource prior = stmt.getSubject(); model.remove(stmt); model.add(prior, RNRM.isFlowingTo, toInsert); } }
private void doRenameBNodes( VitroRequest vreq, String namespaceEtc, boolean patternBoolean, String pattern, String[] sourceModel) { OntModel source = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); String property = vreq.getParameter("property"); Boolean csv2rdf = false; try { csv2rdf = Boolean.parseBoolean(vreq.getParameter("csv2rdf")); } catch (Exception e) { log.error(e, e); } if (csv2rdf) { source.addSubModel((Model) vreq.getSession().getAttribute("csv2rdfResult")); } else { for (int i = 0; i < sourceModel.length; i++) { Model m = getModel(sourceModel[i], vreq); source.addSubModel(m); } } Model destination = (csv2rdf) ? ModelFactory.createDefaultModel() : getModel(vreq.getParameter("destinationModelName"), vreq); JenaIngestUtils utils = new JenaIngestUtils(); destination.enterCriticalSection(Lock.WRITE); try { if (!patternBoolean) { destination.add(utils.renameBNodes(source, namespaceEtc, vreq.getJenaOntModel())); } else { destination.add( utils.renameBNodesByPattern( source, namespaceEtc, vreq.getJenaOntModel(), pattern, property)); } if (csv2rdf) { Model ultimateDestination = getModel(vreq.getParameter("destinationModelName"), vreq); ultimateDestination.add(destination); } } finally { destination.leaveCriticalSection(); } }
@Test public void testUpdate() throws Exception { Resource rhmRes = model.getResource(FakeRDFModel.rhm); Property name = model.getProperty(foaf + "name"); Statement nameSt = rhmRes.listProperties(name).nextStatement(); String originalName = nameSt.getLiteral().getString(); // assume update is always first remove and then add model.remove(nameSt); model.add(rhmRes, name, "TESTNAME"); assert changes.size() == 2; assert rhmRes.listProperties(name).toList().size() == 1; assert rhmRes.listProperties(name).nextStatement().getLiteral().getString().equals("TESTNAME"); changes.undo(); assert changes.size() == 2; assert rhmRes.listProperties(name).toList().size() == 1; assert rhmRes .listProperties(name) .nextStatement() .getLiteral() .getString() .equals(originalName); changes.redo(); assert changes.size() == 2; assert rhmRes.listProperties(name).toList().size() == 1; assert rhmRes.listProperties(name).nextStatement().getLiteral().getString().equals("TESTNAME"); }
public void testMBU(Model m) { Statement[] sArray = statements(m, "moon orbits earth; earth orbits sun"); List<Statement> sList = Arrays.asList(statements(m, "I drink tea; you drink coffee")); m.add(sArray); testContains(m, sArray); m.add(sList); testContains(m, sList); testContains(m, sArray); /* */ m.remove(sArray); testOmits(m, sArray); testContains(m, sList); m.remove(sList); testOmits(m, sArray); testOmits(m, sList); }
private void generateSample() { logger.info("Generating sample..."); sample = ModelFactory.createDefaultModel(); // we have to set up a new query execution factory working on our local model qef = new QueryExecutionFactoryModel(sample); reasoner = new SPARQLReasoner(qef); // get the page size // TODO put to base class long pageSize = 10000; // PaginationUtils.adjustPageSize(globalQef, 10000); ParameterizedSparqlString sampleQueryTemplate = getSampleQuery(); sampleQueryTemplate.setIri("p", entityToDescribe.toStringID()); Query query = sampleQueryTemplate.asQuery(); query.setLimit(pageSize); boolean isEmpty = false; int i = 0; while (!isTimeout() && !isEmpty) { // get next sample logger.debug("Extending sample..."); query.setOffset(i++ * pageSize); QueryExecution qe = ksQef.createQueryExecution(query); Model tmp = qe.execConstruct(); sample.add(tmp); // if last call returned empty model, we can leave loop isEmpty = tmp.isEmpty(); } logger.info("...done. Sample size: " + sample.size() + " triples"); }
/** * Creates an Observable and includes it in the model * * @param uri URI of the Observable to create * @return */ public Resource createObservable(String uri) { Resource rv = null; Model model = process.getModel(); rv = model.createResource(uri); model.add(rv, RDF.type, RNRM.Observable); return rv; }
/** * Cleans up the process by ensuring that any isolated ActivityElements are added as unordered * paths. */ public void finalizeProcess() { List<Resource> isolatedActivities = process.getIsolatedActivities(); if (isolatedActivities.size() > 0) { Model model = process.getModel(); // TODO: Fix this so that it doesn't keep stacking outer fork/joins // when they aren't necessary // create fork/join for all elements Resource fork = createForkElement(); Resource join = createJoinElement(); Resource start = process.getStartElement(); Resource end = process.getEndElement(); // insert fork after the start insertAfter(fork, start); // insert join before the finish insertBefore(join, end); // add any isolated activities for (Resource r : isolatedActivities) { model.add(fork, RNRM.isFlowingTo, r); model.add(r, RNRM.isFlowingTo, join); } } }
/** * Deletes the given process element from the rnrm:isFlowingTo linked list without breaking the * path. The element is also removed from the process. * * @param processElement */ public void deleteFromPath(Resource processElement) { Model model = process.getModel(); List<Statement> incoming = process.getIncomingEdges(processElement); List<Statement> outgoing = process.getOutgoingEdges(processElement); if (incoming.size() > 1 || outgoing.size() > 1) { throw new IllegalArgumentException( "Error deleting element. Elements with more than 1 outgoing or incoming links cannot be deleted."); } Resource before = null; Resource after = null; Statement stmt = null; if (incoming.size() > 0) { stmt = incoming.get(0); before = stmt.getSubject(); model.remove(stmt); } if (outgoing.size() > 0) { stmt = outgoing.get(0); after = stmt.getResource(); model.remove(stmt); } if (null != before && null != after) { model.add(before, RNRM.isFlowingTo, after); } // now disassociate this element from the process removeFromProcess(processElement); }
private void verbModel(Model model, String verb) { Model m = ModelFactory.createDefaultModel(); int testLimit = 1000; StmtIterator stmtIt = model.listStatements(); int count = 0; try { while (stmtIt.hasNext()) { count++; m.add(stmtIt.nextStatement()); if (count % testLimit == 0 || !stmtIt.hasNext()) { StringWriter sw = new StringWriter(); m.write(sw, "N-TRIPLE"); StringBuffer updateStringBuff = new StringBuffer(); String graphURI = graph.getGraphURI(); updateStringBuff.append( verb + " DATA { " + ((graphURI != null) ? "GRAPH <" + graphURI + "> { " : "")); updateStringBuff.append(sw); updateStringBuff.append(((graphURI != null) ? " } " : "") + " }"); String updateString = updateStringBuff.toString(); // log.info(updateString); graph.executeUpdate(updateString); m.removeAll(); } } } finally { stmtIt.close(); } }