From: <jen...@us...> - 2011-12-09 09:23:00
|
Revision: 3493 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=3493&view=rev Author: jenslehmann Date: 2011-12-09 09:22:49 +0000 (Fri, 09 Dec 2011) Log Message: ----------- basic LOD enrichment script done Modified Paths: -------------- trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java trunk/interfaces/src/main/java/org/dllearner/cli/GlobalEnrichment.java Modified: trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java 2011-12-08 16:59:38 UTC (rev 3492) +++ trunk/interfaces/src/main/java/org/dllearner/cli/Enrichment.java 2011-12-09 09:22:49 UTC (rev 3493) @@ -155,7 +155,7 @@ public class Enrichment { // data structure for holding the result of an algorithm run - private class AlgorithmRun { + protected class AlgorithmRun { // we only store the algorithm class and not the learning algorithm object, // since otherwise we run into memory problems for full enrichment @@ -521,7 +521,7 @@ /* * Generates list of OWL axioms. */ - private List<OWLAxiom> toRDF(List<EvaluatedAxiom> evalAxioms, Class<? extends LearningAlgorithm> algorithm, Map<ConfigOption,Object> parameters, SparqlEndpointKS ks){ + List<OWLAxiom> toRDF(List<EvaluatedAxiom> evalAxioms, Class<? extends LearningAlgorithm> algorithm, Map<ConfigOption,Object> parameters, SparqlEndpointKS ks){ return toRDF(evalAxioms, algorithm, parameters, ks, null); } @@ -653,7 +653,7 @@ // return model; // } - private Model getModel(List<OWLAxiom> axioms) { + Model getModel(List<OWLAxiom> axioms) { Model model = ModelFactory.createDefaultModel(); try { OWLOntology ontology = OWLManager.createOWLOntologyManager().createOntology(new HashSet<OWLAxiom>(axioms)); Modified: trunk/interfaces/src/main/java/org/dllearner/cli/GlobalEnrichment.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/cli/GlobalEnrichment.java 2011-12-08 16:59:38 UTC (rev 3492) +++ trunk/interfaces/src/main/java/org/dllearner/cli/GlobalEnrichment.java 2011-12-09 09:22:49 UTC (rev 3493) @@ -19,16 +19,33 @@ */ package org.dllearner.cli; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; import java.net.URL; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import org.apache.log4j.ConsoleAppender; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.SimpleLayout; +import org.dllearner.cli.Enrichment.AlgorithmRun; +import org.dllearner.core.ComponentInitException; +import org.dllearner.core.LearningProblemUnsupportedException; +import org.dllearner.kb.SparqlEndpointKS; import org.dllearner.kb.sparql.SparqlEndpoint; import org.dllearner.kb.sparql.SparqlQuery; +import org.semanticweb.owlapi.model.OWLAxiom; import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.Model; /** * Enriches all of the LOD cloud. @@ -38,36 +55,89 @@ */ public class GlobalEnrichment { + // parameters + private static double threshold = 0.8; + private static int nrOfAxiomsToLearn = 10; + private static boolean useInference = true; + + // directory for generated schemata + private static String baseDir = "log/lod-enriched/"; + /** * @param args * @throws MalformedURLException + * @throws LearningProblemUnsupportedException + * @throws NoSuchMethodException + * @throws InvocationTargetException + * @throws IllegalAccessException + * @throws InstantiationException + * @throws ComponentInitException + * @throws SecurityException + * @throws IllegalArgumentException + * @throws FileNotFoundException */ - public static void main(String[] args) throws MalformedURLException { - // get all SPARQL endpoints and their graphs - List<SparqlEndpoint> endpoints = new LinkedList<SparqlEndpoint>(); + public static void main(String[] args) throws MalformedURLException, IllegalArgumentException, SecurityException, ComponentInitException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, LearningProblemUnsupportedException, FileNotFoundException { + SimpleLayout layout = new SimpleLayout(); + ConsoleAppender consoleAppender = new ConsoleAppender(layout); + Logger.getRootLogger().setLevel(Level.WARN); + Logger.getLogger("org.dllearner").setLevel(Level.WARN); // seems to be needed for some reason (?) + Logger.getRootLogger().removeAllAppenders(); + Logger.getRootLogger().addAppender(consoleAppender); + + // get all SPARQL endpoints and their graphs - the key is a name-identifier + Map<String,SparqlEndpoint> endpoints = new HashMap<String,SparqlEndpoint>(); + String query = ""; - query += "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "; - query += "PREFIX void: <http://rdfs.org/ns/void#> "; - query += "PREFIX dcterms: <http://purl.org/dc/terms/> "; - query += "SELECT ?endpoint "; - query += "WHERE { "; - query += "?item rdf:type void:Dataset . "; - query += "?item dcterms:isPartOf <http://ckan.net/group/lodcloud> . "; - query += "?item void:sparqlEndpoint ?endpoint . "; - query += "}"; + query += "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n"; + query += "PREFIX void: <http://rdfs.org/ns/void#> \n"; + query += "PREFIX dcterms: <http://purl.org/dc/terms/> \n"; + query += "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n"; + query += "PREFIX ov: <http://open.vocab.org/terms/> \n"; + query += "SELECT * \n"; + query += "WHERE { \n"; + query += " ?item rdf:type void:Dataset . \n"; + query += " ?item dcterms:isPartOf <http://ckan.net/group/lodcloud> . \n"; + query += " ?item void:sparqlEndpoint ?endpoint . \n"; +// query += " ?item dcterms:subject ?subject . \n"; +// query += " ?item rdfs:label ?label . \n"; + query += " ?item ov:shortName ?shortName . \n"; + query += "}"; // query += "LIMIT 20"; + System.out.println("Getting list of SPARQL endpoints from LATC DSI:"); + System.out.println(query); - // LATC DSI/MDS + // contact LATC DSI/MDS SparqlEndpoint dsi = new SparqlEndpoint(new URL("http://api.talis.com/stores/latc-mds/services/sparql")); SparqlQuery sq = new SparqlQuery(query, dsi); ResultSet rs = sq.send(); while(rs.hasNext()) { QuerySolution qs = rs.next(); String endpoint = qs.get("endpoint").toString(); -// String graph = qs.getLiteral("graph").getString(); - System.out.println(endpoint); + String shortName = qs.get("shortName").toString(); + endpoints.put(shortName, new SparqlEndpoint(new URL(endpoint))); } + System.out.println(endpoints.size() + " endpoints detected."); + + // perform enrichment on endpoints + for(Entry<String,SparqlEndpoint> endpoint : endpoints.entrySet()) { + // run enrichment + SparqlEndpoint se = endpoint.getValue(); + String name = endpoint.getKey(); + System.out.println("Enriching " + name + " using " + se); + Enrichment e = new Enrichment(se, null, threshold, nrOfAxiomsToLearn, useInference, false); + e.start(); + // save results to a file + SparqlEndpointKS ks = new SparqlEndpointKS(se); + List<AlgorithmRun> runs = e.getAlgorithmRuns(); + List<OWLAxiom> axioms = new LinkedList<OWLAxiom>(); + for(AlgorithmRun run : runs) { + axioms.addAll(e.toRDF(run.getAxioms(), run.getAlgorithm(), run.getParameters(), ks)); + } + Model model = e.getModel(axioms); + File f = new File(baseDir + name + ".ttl"); + model.write(new FileOutputStream(f), "TURTLE"); + } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |