private List<Statement> expandSubClasses(Model model){ List<Statement> stmts = new ArrayList<Statement>(); String sparql = "PREFIX rdfs: <" + RDFS.getURI() + ">" + "SELECT DISTINCT ?class ?synonym " + "WHERE { " + "?class rdfs:subClassOf+ ?subClass . " + "?subClass <" + synonym + "> ?synonym" + "}"; Query query = QueryFactory.create(sparql, Syntax.syntaxARQ); QueryExecution queryExecution = QueryExecutionFactory.create(query, model); ResultSet resultSet = queryExecution.execSelect(); resultSet.forEachRemaining(querySolution -> { stmts.add(new StatementImpl(querySolution.getResource("class"), synonym, querySolution.getLiteral("synonym"))); }); return stmts; }
private List<Statement> expandSubProperties(Model model){ List<Statement> stmts = new ArrayList<Statement>(); String sparql = "PREFIX rdfs: <" + RDFS.getURI() + ">" + "SELECT DISTINCT ?property ?synonym " + "WHERE { " + "?property rdfs:subPropertyOf+ ?subProperty . " + "?subProperty <" + synonym + "> ?synonym" + "}"; Query query = QueryFactory.create(sparql, Syntax.syntaxARQ); QueryExecution queryExecution = QueryExecutionFactory.create(query, model); ResultSet resultSet = queryExecution.execSelect(); resultSet.forEachRemaining(querySolution -> { stmts.add(new StatementImpl(querySolution.getResource("property"), synonym, querySolution.getLiteral("synonym"))); }); return stmts; }
/** * Create instance triples corresponding towards a Quality Report * * @param computedOn - The resource URI of the dataset computed on * @param problemReport - A list of quality problem as RDF Jena models * * @return A Jena Model which can be queried or stored */ public Model createQualityReport(Resource computedOn, List<String> problemReportModels){ Model m = dataset.getDefaultModel(); Resource reportURI = Commons.generateURI(); m.add(new StatementImpl(reportURI, RDF.type, QPRO.QualityReport)); m.add(new StatementImpl(reportURI, QPRO.computedOn, computedOn)); for(String prModelURI : problemReportModels){ Model prModel = getProblemReportFromTBD(prModelURI); for(Resource r : getProblemURI(prModel)){ m.add(new StatementImpl(reportURI, QPRO.hasProblem, r)); } m.add(prModel); dataset.removeNamedModel(prModelURI); } return m; }
private void createViolatingTriple(Statement stmt, String resource){ Model m = ModelFactory.createDefaultModel(); Resource subject = m.createResource(resource); m.add(new StatementImpl(subject, QPRO.exceptionDescription, DQMPROB.ViolatingTriple)); RDFNode violatedTriple = Commons.generateRDFBlankNode(); m.add(new StatementImpl(violatedTriple.asResource(), RDF.type, RDF.Statement)); m.add(new StatementImpl(violatedTriple.asResource(), RDF.subject, stmt.getSubject())); m.add(new StatementImpl(violatedTriple.asResource(), RDF.predicate, stmt.getPredicate())); m.add(new StatementImpl(violatedTriple.asResource(), RDF.object, stmt.getObject())); m.add(new StatementImpl(subject, DQMPROB.hasViolatingTriple, violatedTriple)); this._problemList.add(m); }
private List<Statement> getUsage(Property property, Model model){ List<Statement> stmts = new ArrayList<Statement>(); String sparql = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " + "PREFIX owl: <http://www.w3.org/2002/07/owl#> " + "SELECT DISTINCT ?concept " + "WHERE{" + " {<" + property.getURI() + "> rdfs:domain ?concept} " + " UNION " + " { " + " ?concept rdfs:subClassOf|owl:equivalentClass ?restriction . " + " ?restriction a owl:Restriction; " + " owl:onProperty <" + property.getURI() + "> " + " } " + "}"; Query query = QueryFactory.create(sparql, Syntax.syntaxARQ); QueryExecution queryExecution = QueryExecutionFactory.create(query, model); ResultSet resultSet = queryExecution.execSelect(); while(resultSet.hasNext()){ QuerySolution querySolution = resultSet.next(); Resource concept = querySolution.getResource("concept"); stmts.add(new StatementImpl(property, usage, concept)); } return stmts; }
private List<Statement> getUsage(OntClass ontClass, Model model){ List<Statement> stmts = new ArrayList<Statement>(); try{ String sparql = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " + "PREFIX owl: <http://www.w3.org/2002/07/owl#> " + "SELECT DISTINCT ?concept " + "WHERE{" + " {?prop rdfs:range <" + ontClass.getURI() + ">; " + " rdfs:domain ?concept" + " }" + " UNION " + " { " + " ?concept rdfs:subClassOf|owl:equivalentClass ?restriction . " + " ?restriction a owl:Restriction; " + " ?p <" + ontClass.getURI() + "> " + " } " + "}"; Query query = QueryFactory.create(sparql, Syntax.syntaxARQ); QueryExecution queryExecution = QueryExecutionFactory.create(query, model); ResultSet resultSet = queryExecution.execSelect(); while(resultSet.hasNext()){ QuerySolution querySolution = resultSet.next(); Resource concept = querySolution.getResource("concept"); stmts.add(new StatementImpl(ontClass, usage, concept)); } } catch(Exception e){ log.error(e.getMessage(), e); } return stmts; }
/** * The Wikipedia Id or -1 if the Id couldn't be retrieved. * * FIXME The method throws an exception for "http://DBpedia.org/resource/Origin_of_the_name_"Empire_State"". this * might be happen because of the quotes inside the URI. * * @param dbpediaUri * @return */ @Deprecated public static int getIdFromDBpedia(String dbpediaUri) { int id = -1; ParameterizedSparqlString query = new ParameterizedSparqlString( "SELECT ?id WHERE { ?dbpedia dbo:wikiPageID ?id .}", prefixes); query.setIri("dbpedia", dbpediaUri); QueryExecution qexec = null; try { qexec = QueryExecutionFactory.create(query.asQuery(), model); } catch (QueryParseException e) { LOGGER.error("Got a bad dbpediaUri \"" + dbpediaUri + "\" which couldn't be parse inside of a SPARQL query. Returning -1.", e); return id; } ResultSet result = qexec.execSelect(); if (result.hasNext()) { id = result.next().get("id").asLiteral().getInt(); return id; } qexec = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", query.asQuery()); result = qexec.execSelect(); if (result.hasNext()) { id = result.next().get("id").asLiteral().getInt(); model.add(new StatementImpl(model.createResource(dbpediaUri), model .createProperty("http://dbpedia.org/ontology/wikiPageID"), model.createTypedLiteral(id))); return id; } model.add(new StatementImpl(model.createResource(dbpediaUri), model .createProperty("http://dbpedia.org/ontology/wikiPageID"), model.createTypedLiteral(id))); return id; }
public void startTopology(Model returnModel){ ResIterator resIterator = this.createModel.listResourcesWithProperty(Omn.hasResource); while(resIterator.hasNext()){ Resource resource = resIterator.nextResource(); Property property = returnModel.createProperty(Omn_lifecycle.hasState.getNameSpace(),Omn_lifecycle.hasState.getLocalName()); property.addProperty(RDF.type, OWL.FunctionalProperty); Statement statement = new StatementImpl(resource, property, Omn_lifecycle.Started); returnModel.add(statement); } }
public void extendResourcesProperties(Model returnModel, Model resultModel){ StmtIterator statementIterator_createModel = this.createModel.listStatements(new SimpleSelector((Resource)null, Omn.hasResource, (Object)null)); while(statementIterator_createModel.hasNext()){ Statement requestedResourceStatement = statementIterator_createModel.nextStatement(); Resource requestedResource = requestedResourceStatement.getObject().asResource(); String resource_id = this.createModel.getRequiredProperty(requestedResource, Omn_lifecycle.hasID).getString(); Resource resourceProperties = this.createModel.getResource(requestedResource.getURI()); StmtIterator stmtIterator = resultModel.listStatements(new SimpleSelector((Resource)null, Omn_lifecycle.hasID, (Object) resource_id)); while(stmtIterator.hasNext()){ Statement statement = stmtIterator.nextStatement(); Resource createdResource = statement.getSubject(); StmtIterator stmtIter = resultModel.listStatements(new SimpleSelector(createdResource, (Property)null, (RDFNode)null)); while(stmtIter.hasNext()){ Statement createdStatement = stmtIter.nextStatement(); if(!resourceProperties.hasProperty(createdStatement.getPredicate(), createdStatement.getObject())){ Statement stmt = new StatementImpl(requestedResource, createdStatement.getPredicate(), createdStatement.getObject()); returnModel.add(stmt); } } } } }
@Before public void initialize(){ Model dummyModel = ModelFactory.createDefaultModel(); Resource adapterType = dummyModel.createResource("adapter type"); Statement dummyStatement = new StatementImpl(adapterType, RDFS.subClassOf, MessageBusOntologyModel.classAdapter); dummyModel.add(dummyStatement); Resource resource = dummyModel.createResource("dummy resource"); adapter = new ToscaAdapter(dummyModel, resource, null); callOpenSDNcore = new CallOpenSDNcore(dummyModel, adapter); }
/** Creates a partial statement from a partial triplet (subject, predicate, object). * * At least one of the parameter is assumed to be null. * * @param subject The subject of the statement * @param predicate The predicate of the statement * @param object The object of the statement * @param model The ontology this partial statement refers to. */ public PartialStatement(Resource subject, Property predicate, RDFNode object, ModelCom model) { stmtTokens = new ArrayList<String>(); if (subject == null) { stmtTokens.add("?subject"); subject = model.createResource("nullSubject"); } else stmtTokens.add(Namespaces.toLightString(subject)); if (predicate == null) { stmtTokens.add("?predicate"); predicate = model.createProperty("nullPredicate"); } else stmtTokens.add(Namespaces.toLightString(predicate)); if (object == null) { stmtTokens.add("?object"); object = model.createResource("nullObject"); } else stmtTokens.add(Namespaces.toLightString(object)); baseStmt = new StatementImpl(subject, predicate, object, model); }
/** * Create a new partial statement from its string representation.<br/> * Works as {@link OpenRobotsOntology#createStatement(String)} except at least one variable, prepended with a "?", is expected.</br> * * This class implements {@link Statement}, but the {@link #getSubject()}, {@link #getPredicate()} and {@link #getObject()} method will return {@code null} if the corresponding part of the statement is unbounded. * * @param partialStatement The string representing the partial statement. For example, {@code "?mysterious oro:objProperty2 ?object"} or {@code "?subject oro:dataProperty1 true"} are valid. * @param model The ontology this partial statement refers to. * @throws IllegalStatementException Currently thrown only if the statement doesn't contain three tokens. * @see OpenRobotsOntology#createStatement(String) Syntax details */ public PartialStatement(String partialStatement, ModelCom model) throws IllegalStatementException { Resource subject = null; Property predicate = null; RDFNode object = null; stmtTokens = Helpers.tokenize(partialStatement.trim(), ' '); if (stmtTokens.size() != 3) throw new IllegalStatementException("Three tokens are expected in a partial statement, " + stmtTokens.size() + " found in " + partialStatement + "."); //checks that at least one token starts with a "?". if (!((stmtTokens.get(0).length() > 0 && stmtTokens.get(0).charAt(0) == '?') || (stmtTokens.get(1).length() > 0 && stmtTokens.get(1).charAt(0) == '?') || (stmtTokens.get(2).length() > 0 && stmtTokens.get(2).charAt(0) == '?')) ) throw new IllegalStatementException("At least one token should be marked as unbounded (starting with a \"?\")."); if (stmtTokens.get(0).length() == 0 || stmtTokens.get(0).charAt(0) != '?') subject = model.getResource(Namespaces.format(stmtTokens.get(0))); else subject = model.createResource("nullSubject"); //if the subject is unbounded (a variable), creates an "nullSubject" resource to replace the subject. if (stmtTokens.get(1).length() == 0 || stmtTokens.get(1).charAt(0) != '?') predicate = model.getProperty(Namespaces.format(stmtTokens.get(1))); else predicate = model.createProperty("nullPredicate"); //if the predicate is unbounded (a variable), creates an "nullPredicate" property to replace the predicate. if (stmtTokens.get(2).length() == 0 || stmtTokens.get(2).charAt(0) != '?') { object = Helpers.parseLiteral(stmtTokens.get(2), model); assert(object!=null); } else object = model.createResource("nullObject"); //if the object is unbounded (a variable), creates an "nullObject" object to replace the object. baseStmt = new StatementImpl(subject, predicate, object, model); }
private void createProblemModel(String resource, String expectedContentType, String actualContentType){ Model m = ModelFactory.createDefaultModel(); Resource subject = m.createResource(resource); m.add(new StatementImpl(subject, QPRO.exceptionDescription, DQMPROB.MisreportedTypeException)); m.add(new StatementImpl(subject, DQMPROB.expectedContentType, m.createLiteral(expectedContentType))); m.add(new StatementImpl(subject, DQMPROB.actualContentType, m.createLiteral(actualContentType))); this._problemList.add(m); }
private void createNotValidForwardLink(String resource){ Model m = ModelFactory.createDefaultModel(); Resource subject = m.createResource(resource); m.add(new StatementImpl(subject, QPRO.exceptionDescription, DQMPROB.NotValidForwardLink)); this._problemList.add(m); }
private void createProblemModel(String resource, String expectedContentType, String actualContentType){ Model m = ModelFactory.createDefaultModel(); Resource subject = m.createResource(resource); m.add(new StatementImpl(subject, QPRO.exceptionDescription, DQMPROB.MisreportedTypeException)); if ((expectedContentType == null) || (expectedContentType.equals(""))) m.add(new StatementImpl(subject, DQMPROB.expectedContentType, m.createLiteral("Unknown Expected Content Type"))); else m.add(new StatementImpl(subject, DQMPROB.expectedContentType, m.createLiteral(expectedContentType))); if ((actualContentType == null) || (actualContentType.equals(""))) m.add(new StatementImpl(subject, DQMPROB.actualContentType, m.createLiteral("Unknown Content Type"))); else m.add(new StatementImpl(subject, DQMPROB.actualContentType, m.createLiteral(actualContentType))); this._problemList.add(m); }
private void createNotValidBacklink(String resource){ Model m = ModelFactory.createDefaultModel(); Resource subject = m.createResource(resource); m.add(new StatementImpl(subject, QPRO.exceptionDescription, DQMPROB.NotValidDereferenceableBackLink)); this._problemList.add(m); }
private void createNotValidBackLink(String resource){ Model m = ModelFactory.createDefaultModel(); Resource subject = m.createResource(resource); m.add(new StatementImpl(subject, QPRO.exceptionDescription, DQMPROB.NotValidDereferenceableBackLink)); this._problemList.add(m); }
List<Statement> createProblemModel(){ List<Statement> lst = new ArrayList<Statement>(); Resource anon = ModelFactory.createDefaultModel().createResource(AnonId.create()); lst.add(new StatementImpl(anon, RDF.subject, Commons.asRDFNode(q.getSubject()))); lst.add(new StatementImpl(anon, RDF.predicate, Commons.asRDFNode(q.getPredicate()))); lst.add(new StatementImpl(anon, RDF.object, Commons.asRDFNode(q.getObject()))); return lst; }
Model createProblemModel(){ Model m = ModelFactory.createDefaultModel(); Resource subject = m.createResource(resource.toString()); m.add(new StatementImpl(subject, QPRO.exceptionDescription, DQMPROB.MultiTypedResourceWithDisjointedClasses)); for(String s : setView){ m.add(new StatementImpl(subject, DQMPROB.violatingDisjoinedClass, m.createResource(s))); } return m; }
Model createProblemModel(){ Model m = ModelFactory.createDefaultModel(); Resource subject = m.createResource(resource.toString()); m.add(new StatementImpl(subject, QPRO.exceptionDescription, type)); if (type.equals(DQMPROB.MisplacedClass)) m.add(new StatementImpl(subject, DQMPROB.hasMisplacedClass, m.asRDFNode(classOrProperty))); else m.add(new StatementImpl(subject, DQMPROB.hasMisplacedProperty, m.asRDFNode(classOrProperty))); return m; }
Model createProblemModel(){ Model m = ModelFactory.createDefaultModel(); Resource subject = m.createResource(resource.toString()); m.add(new StatementImpl(subject, QPRO.exceptionDescription, type)); if (type.equals(DQMPROB.MisusedDatatypeProperty)) m.add(new StatementImpl(subject, DQMPROB.hasMisusedDatatypeProperty, m.asRDFNode(property))); else m.add(new StatementImpl(subject, DQMPROB.hasMisusedObjectProperty, m.asRDFNode(property))); return m; }
Statement getProblemQuad(){ Statement s = new StatementImpl(Commons.asRDFNode(q.getSubject()).asResource(), ModelFactory.createDefaultModel().createProperty(q.getPredicate().getURI()), Commons.asRDFNode(q.getObject()) ); return s; }
List<Statement> createProblemModel(){ List<Statement> lst = new ArrayList<Statement>(); Resource anon = ModelFactory.createDefaultModel().createResource(Commons.generateURI()); lst.add(new StatementImpl(anon, RDF.subject, Commons.asRDFNode(q.getSubject()))); lst.add(new StatementImpl(anon, RDF.predicate, Commons.asRDFNode(q.getPredicate()))); lst.add(new StatementImpl(anon, RDF.object, Commons.asRDFNode(q.getObject()))); return lst; }
private List<Statement> getSynonyms(Property property, Model model){ List<Statement> stmts = new ArrayList<Statement>(); try{ Site wnSite = siteManager.getSite(Constants.wordNetSiteID); FieldQuery fieldQuery = DefaultQueryFactory.getInstance().createFieldQuery(); fieldQuery.addSelectedField(property.getURI()); fieldQuery.setOffset(0); fieldQuery.setLimit(3); StmtIterator labelIt = model.listStatements(null, property, (RDFNode)null); labelIt.forEachRemaining(stmt -> { Resource subject = stmt.getSubject(); RDFNode object = stmt.getObject(); if(object.isLiteral()){ String label = ((Literal)object).getLexicalForm(); String lang = ((Literal)object).getLanguage(); if(lang == null || lang.equals("it")){ label = lemmatize(label); Constraint similarityConstraint = new SimilarityConstraint(label, null); fieldQuery.setConstraint(property.getURI(), similarityConstraint); /* Add label as synonym. * But first we lemmatise the term in order to increase the recall. */ Literal objLiteral = null; if(lang != null) objLiteral = ResourceFactory.createLangLiteral(label, lang); else objLiteral = ResourceFactory.createPlainLiteral(label); stmts.add(new StatementImpl(subject, synonym, objLiteral)); final String lab = label; QueryResultList<Representation> result = wnSite.find(fieldQuery); result.forEach(representation -> { float[] score = new float[]{0}; Iterator<Object> objIt = representation.get(Constants.entityHubScore); if(objIt != null){ objIt.forEachRemaining(obj -> { score[0] = (float) obj; }); } if(score[0] > Constants.wordnetSynonymityConfidence){ objIt = representation.get(property.getURI()); if(objIt != null){ objIt.forEachRemaining(obj -> { Text text = (Text) obj; String value = text.getText(); String language = text.getLanguage(); // Add labels of synomyms log.debug("Syn {} ({}) - {} : {}", subject, lab, value, score); Literal synLabel = ResourceFactory.createLangLiteral(lemmatize(value), language); stmts.add(new StatementImpl(subject, synonym, synLabel)); }); } } }); } } }); } catch(Exception e){ log.error(e.getMessage(), e); } return stmts; }
public static Statement replace(Statement pr, Statement original ){ Resource subject = patternReplace(Statement::getSubject, pr, original); Property predicate = patternReplace(Statement::getPredicate, pr, original); RDFNode object = patternReplace(Statement::getObject, pr, original); return new StatementImpl(subject, predicate, object); }
public Statement createStatement(String statement) throws IllegalStatementException { Resource subject; Property predicate; RDFNode object; List<String> tokens_statement = Helpers.tokenize(statement.trim(), ' '); if (tokens_statement.size() != 3) throw new IllegalStatementException( "Three tokens are expected in a statement, " + tokens_statement.size() + " found in " + statement + "."); //expand the namespaces for subject and predicate. for (int i = 0; i<2; i++){ tokens_statement.set(i, Namespaces.format(tokens_statement.get(i))); } subject = onto.getResource(tokens_statement.get(0)); predicate = onto.getProperty(tokens_statement.get(1)); //Handle objects object = Helpers.parseLiteral(tokens_statement.get(2), (ModelCom)onto); assert(object!=null); Statement s =new StatementImpl(subject, predicate, object); return s; }
Model createProblemModel(){ Model m = ModelFactory.createDefaultModel(); Resource subject = m.createResource(resource.toString()); m.add(new StatementImpl(subject, QPRO.exceptionDescription, DQMPROB.MultiTypedResourceWithDisjointedClasses)); m.add(new StatementImpl(subject, DQMPROB.violatingDisjoinedClass, m.asRDFNode(_class))); for(RDFNode s : _otherClasses){ m.add(new StatementImpl(subject, DQMPROB.violatingDisjoinedClass, s)); } return m; }
Model createProblemModel(){ Model m = ModelFactory.createDefaultModel(); Resource subject = m.createResource(resource.toString()); m.add(new StatementImpl(subject, QPRO.exceptionDescription, DQMPROB.MultiTypedResourceWithDisjointedClasses)); m.add(new StatementImpl(subject, DQMPROB.violatingDisjoinedClass, m.asRDFNode(_class))); m.add(new StatementImpl(subject, DQMPROB.violatingDisjoinedClass, m.asRDFNode(_otherClass))); return m; }