// NEED -> SUPERCLASSE private ArrayList<java.net.URI> getSuperClassFromUri(String uri) { String queryBegin = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + "PREFIX mstr: <http://methodo-stat-tutor.com#>\n" + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + "PREFIX owl: <http://www.w3.org/2002/07/owl#> \n" + "PREFIX sparqldl: <http://pellet.owldl.com/ns/sdle#>\n " + "SELECT DISTINCT ?SUPERCLASS ?LAB ?COM \n" + "WHERE {"; String queryEnd = "}"; String queryStr2 = queryBegin + // "?titi owl:Class ?CLASS ." + // "?NOTION a ?CLASS ." + "?CLASS sparqldl:directSubClassOf ?SUPERCLASS ." + "OPTIONAL {?SUPERCLASS rdfs:label ?LAB} ." + "OPTIONAL {?SUPERCLASS rdfs:comment ?COM} ." + "FILTER (?CLASS = <" + uri + ">)" + queryEnd; log.error(queryStr2); Query query2 = QueryFactory.create(queryStr2); ResultSet results = SparqlDLExecutionFactory.create(query2, ontModel).execSelect(); ArrayList<java.net.URI> aru = new ArrayList<java.net.URI>(); while (results.hasNext()) { aru.add(java.net.URI.create(results.next().get("?SUPERCLASS").toString())); } return aru; }
public String getQcmNotion() { String queryBegin = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + "PREFIX mstr: <http://methodo-stat-tutor.com#>\n" + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + "PREFIX owl: <http://www.w3.org/2002/07/owl#> \n" + "PREFIX sparqldl: <http://pellet.owldl.com/ns/sdle#>\n " + "SELECT ?NOTION ?NOTIONLAB ?NOTIONCOMM ?TYP \n" + "WHERE {"; String queryEnd = "}"; // create a query that asks for the color of the wine that // would go with each meal course String queryStr2 = queryBegin + "{?EXO a mstr:Qcm .\n" + " ?EXO mstr:traiteNotion ?NOTION .\n" + " BIND(IRI(str(?NOTION)) as ?NOTION_IRI) .\n" + "OPTIONAL { ?NOTION_IRI rdfs:label ?NOTIONLAB } .\n" + "OPTIONAL { ?NOTION_IRI rdfs:comment ?NOTIONCOMM } \n" + "values ?TYP { 'traite' }\n" + "FILTER (?EXO = mstr:" + this.exercise + ")\n" + "}\n" + "UNION \n" + "{?EXO a mstr:Qcm .\n" + " ?EXO mstr:needNotion ?NOTION .\n" + " BIND(IRI(str(?NOTION)) as ?NOTION_IRI) .\n" + "OPTIONAL { ?NOTION_IRI rdfs:label ?NOTIONLAB } .\n" + "OPTIONAL { ?NOTION_IRI rdfs:comment ?NOTIONCOMM } \n" + "values ?TYP { 'need' }\n" + "FILTER (?EXO = mstr:" + this.exercise + ")" + "}" + "UNION \n" + "{?EXO a mstr:Qcm .\n" + " ?EXO mstr:giveNotion ?NOTION .\n" + " BIND(IRI(str(?NOTION)) as ?NOTION_IRI) .\n" + "OPTIONAL { ?NOTION_IRI rdfs:label ?NOTIONLAB } .\n" + "OPTIONAL { ?NOTION_IRI rdfs:comment ?NOTIONCOMM } \n" + "values ?TYP { 'give' }\n" + "FILTER (?EXO = mstr:" + this.exercise + ")" + " }" + queryEnd; log.error(queryStr2); Query query2 = QueryFactory.create(queryStr2); ResultSet results = SparqlDLExecutionFactory.create(query2, ontModel).execSelect(); String str = MstrUtils.getSparqlResultsetAsJson(results); return str; }
@Override public void processResults() { String queryBegin = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "PREFIX mstr: <http://methodo-stat-tutor.com#>" + "SELECT ?NOTION " + "WHERE {"; String queryEnd = "}"; // create a query that asks for the color of the wine that // would go with each meal course String queryStr2 = queryBegin + " ?EXO mstr:traiteNotion ?NOTION ." + "FILTER (?EXO = mstr:" + this.exercise + ")\n" + queryEnd; log.error(queryStr2); Query query2 = QueryFactory.create(queryStr2); ResultSet results = SparqlDLExecutionFactory.create(query2, ontModel).execSelect(); ArrayList<java.net.URI> needNotion = new ArrayList<java.net.URI>(); ArrayList<java.net.URI> giveNotion = new ArrayList<java.net.URI>(); while (results.hasNext()) { RDFNode a = results.next().get("?NOTION"); giveNotion.add(java.net.URI.create(a.asLiteral().getString())); needNotion.addAll(getSuperClassFromUri(a.asLiteral().getString())); } // remove duplicates from neednotion Set<java.net.URI> hs = new HashSet<>(); hs.addAll(needNotion); needNotion.clear(); needNotion.addAll(hs); // end remove duplicate this.needNotion = needNotion; this.giveNotion = giveNotion; // ajout des notions au modele int i = 0; while (i < needNotion.size()) { log.error(needNotion.get(i).toString()); addStatementToOntModel("needNotion", needNotion.get(i).toString()); i++; } i = 0; while (i < giveNotion.size()) { log.error(giveNotion.get(i).toString()); addStatementToOntModel("giveNotion", giveNotion.get(i).toString()); i++; } }
private IRI executeRule(final Rule r, final IRI inputIRI) { try { PelletOptions.USE_ANNOTATION_SUPPORT = true; PelletOptions.TREAT_ALL_VARS_DISTINGUISHED = controller.isTreatAllVariablesDistinguished(); QueryEngineType type = (QueryEngineType) controller.getQueryEngineType(); final QueryExecution qe; final ByteArrayOutputStream w = new ByteArrayOutputStream(); final Query qSelect = getSelectExampleQuery(r.getQuery()); if (type.toPellet() != null) { final OWLOntology queryOntology = getInputOntologyForRule(inputIRI); final PelletReasoner reasoner = PelletReasonerFactory.getInstance().createReasoner(queryOntology); log.info("Ontology size: " + reasoner.getKB().getInfo()); final Dataset ds = kb2ds(reasoner.getKB()); final QueryExecution qeSelect = SparqlDLExecutionFactory.create(qSelect, ds, null, type.toPellet()); final ResultSet rs = qeSelect.execSelect(); controller.setSelect(r, rs.getResultVars(), ResultSetFormatter.toList(rs)); qe = SparqlDLExecutionFactory.create( r.getQuery(), kb2ds(reasoner.getKB()), null, type.toPellet()); qe.execConstruct().write(w); } else { final ByteArrayOutputStream w2 = new ByteArrayOutputStream(); final Model model = ModelFactory.createDefaultModel(); try { controller .getOWLOntologyManager() .saveOntology(queryOntology, new TurtleOntologyFormat(), w2); model.read(new ByteArrayInputStream(w2.toByteArray()), "", "TURTLE"); final QueryExecution qeSelect = QueryExecutionFactory.create(qSelect, model); final ResultSet rs = qeSelect.execSelect(); controller.setSelect(r, rs.getResultVars(), ResultSetFormatter.toList(rs)); qe = QueryExecutionFactory.create(r.getQuery(), model); qe.execConstruct().write(w); } catch (OWLOntologyStorageException e) { // TODO Auto-generated catch block e.printStackTrace(); } } final IRI outputIRI = getOntologyIRIForRuleName(r.getName()); // loaded generated ontology final OWLOntology generatedOntology = controller .getOWLOntologyManager() .loadOntologyFromOntologyDocument(new ByteArrayInputStream(w.toByteArray())); controller.updateOntology( generatedOntology, outputIRI, inputIRI, controller.getRuleSpec().getResultFile(r).toURI()); controller.setStatus("Rule " + r.getName() + " successfully executed"); return outputIRI; } catch (OWLOntologyCreationException e1) { controller.setStatus(e1.getMessage()); return null; } }