Prepare: Added Jena and Mondial, performing a test query

This commit is contained in:
Francesco 2022-04-05 00:34:22 +02:00
parent 6c222bf049
commit e3e7d197d5
3 changed files with 169844 additions and 1 deletions

View File

@ -7,6 +7,9 @@ repositories {
}
dependencies {
implementation 'org.apache.jena:apache-jena-libs:4.4.0'
implementation 'org.apache.logging.log4j:log4j-core:2.17.2'
implementation 'org.slf4j:slf4j-simple:1.7.36'
}
version '0.1'

View File

@ -1,7 +1,60 @@
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.InfModel;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.reasoner.ReasonerRegistry;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public final class Main
{
public static void main(String[] args)
{
System.out.println("Hello world");
System.out.println("Loading knowledge graph...");
InfModel kg = ModelFactory.createInfModel(ReasonerRegistry.getTransitiveReasoner(), ModelFactory.createDefaultModel());
final InputStream mondialDataset = ClassLoader.getSystemResourceAsStream("mondial_2022_04_04.n3");
if (mondialDataset == null)
{
System.err.println("Impossible to open mondial dataset");
System.exit(1);
}
kg.read(mondialDataset, null, "TTL");
System.out.printf("Loaded %s triples\n", kg.listStatements().toList().size());
{
Map<String, Resource> countries = mondialGetEntitiesWithName(kg, "Country");
System.out.printf("There are %d countries in the dataset\n", countries.size());
}
}
private static Map<String, Resource> mondialGetEntitiesWithName(Model kg, String prefixedType)
{
Map<String, Resource> result = new HashMap<>();
String queryString = String.format("PREFIX m: <http://www.semwebtech.org/mondial/10/meta#> SELECT ?entity ?name WHERE { ?entity a m:%s ; m:name ?name. }", prefixedType);
try (QueryExecution execution = QueryExecutionFactory.create(queryString, kg))
{
final ResultSet resultSet = execution.execSelect();
while (resultSet.hasNext())
{
QuerySolution solution = resultSet.next();
Resource entity = solution.getResource("?entity");
String name = solution.getLiteral("?name").getString();
if (result.containsKey(name))
{
System.err.printf("Error: multiple entities with the name %s\n", name);
System.exit(1);
}
result.put(name, entity);
}
}
return result;
}
}

File diff suppressed because it is too large Load Diff