Skip to content

Commit

Permalink
Merge pull request #106 from mgns/feature/newCli
Browse files Browse the repository at this point in the history
Code cleanup, dependencies bumped, and a new CLI
  • Loading branch information
jimkont authored Jan 30, 2021
2 parents c5536bb + 083e68e commit 4716b0c
Show file tree
Hide file tree
Showing 371 changed files with 21,072 additions and 19,254 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ target
log
pom.xml.releaseBackup
dependency-reduced-pom.xml

rdfunit-core/src/main/resources/
rdfunit-validate/src/main/resources/org/
182 changes: 93 additions & 89 deletions archive/ISWC_2013_evaluation_track/generate_tests/DisjointExtract.java
Original file line number Diff line number Diff line change
@@ -1,126 +1,130 @@
package transform;

import org.apache.commons.io.FileUtils;
import org.apache.jena.ontology.OntClass;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.util.FileManager;
import org.apache.jena.util.iterator.ExtendedIterator;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.jena.ontology.OntClass;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.util.FileManager;
import org.apache.jena.util.iterator.ExtendedIterator;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class DisjointExtract {

public static String dbpediaOwl = "http://mappings.dbpedia.org/server/ontology/dbpedia.owl" ;
public static File file = new File("disjoint_classes.nt");

public static void main (String args[]) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {

// get classes from dbpedia ontology
OntModel onto = ModelFactory.createOntologyModel();
InputStream in = FileManager.get().open( dbpediaOwl );
public static String dbpediaOwl = "http://mappings.dbpedia.org/server/ontology/dbpedia.owl";
public static File file = new File("disjoint_classes.nt");

public static void main(String args[])
throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {

// get classes from dbpedia ontology
OntModel onto = ModelFactory.createOntologyModel();
InputStream in = FileManager.get().open(dbpediaOwl);
if (in == null) {
throw new IllegalArgumentException(
"File: " + dbpediaOwl + " not found");
throw new IllegalArgumentException(
"File: " + dbpediaOwl + " not found");
}
onto.read(in, null);

StringBuilder triples = new StringBuilder();
Set<String> subjects = new HashSet();
Set<String> objects = new HashSet();
Set<String> tripleSet = new HashSet();
Set<String> subjects = new HashSet();
Set<String> objects = new HashSet();
Set<String> tripleSet = new HashSet();

ExtendedIterator classes = onto.listClasses();
while (classes.hasNext()) {

OntClass aClass = (OntClass) classes.next();
String label = aClass.getLocalName().toString();

// get value from xml and export from mappings api
String xmlFile = "http://mappings.dbpedia.org/index.php/Special:Export/OntologyClass:"+label;
String text = xpath(xmlFile,"mediawiki/page/revision/text");

// get value from xml and export from mappings api
String xmlFile =
"http://mappings.dbpedia.org/index.php/Special:Export/OntologyClass:" + label;
String text = xpath(xmlFile, "mediawiki/page/revision/text");
// only some are worth processing
if (text.contains("disjoint")) {

String [] lines = text.split("\n");
for (String line : lines) {
if (line.contains("owl:disjointWith")){

String disjoint = line.substring(line.lastIndexOf("=")+1,line.length()).trim();
String object = "http://dbpedia.org/ontology/" + disjoint;
subjects.add(aClass.getURI().toString());
objects.add(object.toString());
// get all sub-classes for found disjoint subjects
if (aClass.hasSubClass()){

ExtendedIterator subclasses = aClass.listSubClasses();
while (subclasses.hasNext()) {
OntClass sClass = (OntClass) subclasses.next();
subjects.add(sClass.getURI());
}
}
// get all sub-classes for found disjoint objects
OntClass subjectClass = onto.getOntClass(object);
if (subjectClass != null && subjectClass.hasSubClass()){
ExtendedIterator sub2classes = subjectClass.listSubClasses();
while (sub2classes.hasNext()) {
OntClass sClass = (OntClass) sub2classes.next();
objects.add(sClass.getURI());
}
}
}
// finally write all triples
if (!subjects.isEmpty()&& !objects.isEmpty()) {
Iterator itSubj = subjects.iterator();
while (itSubj.hasNext()) {
String strSubj = (String) itSubj.next();
Iterator itObj = objects.iterator();
while (itObj.hasNext()) {
String strObj = (String) itObj.next();
tripleSet.add("<"+strSubj +">"+" <http://www.w3.org/2002/07/owl#disjointWith> "+"<"+strObj+ "> .");
}
}
// cleanup sets for next class
objects.clear();
subjects.clear();
String[] lines = text.split("\n");
for (String line : lines) {
if (line.contains("owl:disjointWith")) {

String disjoint = line.substring(line.lastIndexOf("=") + 1, line.length()).trim();
String object = "http://dbpedia.org/ontology/" + disjoint;
subjects.add(aClass.getURI().toString());
objects.add(object.toString());
// get all sub-classes for found disjoint subjects
if (aClass.hasSubClass()) {

ExtendedIterator subclasses = aClass.listSubClasses();
while (subclasses.hasNext()) {
OntClass sClass = (OntClass) subclasses.next();
subjects.add(sClass.getURI());
}
}
// get all sub-classes for found disjoint objects
OntClass subjectClass = onto.getOntClass(object);
if (subjectClass != null && subjectClass.hasSubClass()) {
ExtendedIterator sub2classes = subjectClass.listSubClasses();
while (sub2classes.hasNext()) {
OntClass sClass = (OntClass) sub2classes.next();
objects.add(sClass.getURI());
}
}
}
}
// finally write all triples
if (!subjects.isEmpty() && !objects.isEmpty()) {
Iterator itSubj = subjects.iterator();
while (itSubj.hasNext()) {
String strSubj = (String) itSubj.next();
Iterator itObj = objects.iterator();
while (itObj.hasNext()) {
String strObj = (String) itObj.next();
tripleSet.add(
"<" + strSubj + ">" + " <http://www.w3.org/2002/07/owl#disjointWith> " + "<"
+ strObj + "> .");
}
}
// cleanup sets for next class
objects.clear();
subjects.clear();
}
}
}
}
// finally write triples to file
Iterator itTriple = tripleSet.iterator();
while (itTriple.hasNext()) {
String strTriple = (String) itTriple.next();
triples.append(strTriple +"\n");
}
FileUtils.writeStringToFile(file, triples.toString(), true);
Iterator itTriple = tripleSet.iterator();
while (itTriple.hasNext()) {
String strTriple = (String) itTriple.next();
triples.append(strTriple + "\n");
}
FileUtils.writeStringToFile(file, triples.toString(), true);
}

// get value from xml file
public static String xpath (String xmlFile, String path) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {

public static String xpath(String xmlFile, String path)
throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {

InputStream is = FileManager.get().open(xmlFile);
DocumentBuilderFactory xmlFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = xmlFactory.newDocumentBuilder();
Document xmlDoc = docBuilder.parse(is);
XPathFactory xpathFact = XPathFactory.newInstance();
XPath xpath = xpathFact.newXPath();
String text = (String) xpath.evaluate(path , xmlDoc, XPathConstants.STRING);
return text;
}

String text = (String) xpath.evaluate(path, xmlDoc, XPathConstants.STRING);

return text;
}
}
4 changes: 2 additions & 2 deletions bin/rdfunit
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# bin/rdfunit -d http://dbpedia.org -e http://dbpedia.org/sparql -g http://dbpedia.org -p dbo -s "owl,dbo,foaf,dcterms,dc,skos,geo,prov"
#

MAIN_CLS="org.aksw.rdfunit.validate.cli.ValidateCLI"
MAIN_CLS="org.aksw.rdfunit.validate.cli.CLI"

# TODO use the jar for faster execution
#if [ -s bin/rdfunit.jar ]
Expand All @@ -27,4 +27,4 @@ if [ ! -d "rdfunit-validate/target" ]; then
echo "Compiling finished..."
fi

mvn -pl rdfunit-validate exec:java -q -Dexec.mainClass="$MAIN_CLS" -Dexec.args="$*"
mvn -pl rdfunit-validate exec:java -q -Dexec.mainClass="$MAIN_CLS" -Dexec.args="validate $*"
30 changes: 30 additions & 0 deletions bin/rdfunit-generateTests
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

#
# example call for dump-based testing (You can use either a URI or a local file name):
# bin/rdfunit -d https://raw.github.com/AKSW/n3-collection/master/News-100.ttl -s nif
#
# example call for endpoint-based testing (dbpedia):
# bin/rdfunit -d http://dbpedia.org -e http://dbpedia.org/sparql -g http://dbpedia.org -p dbo -s "owl,dbo,foaf,dcterms,dc,skos,geo,prov"
#

MAIN_CLS="org.aksw.rdfunit.validate.cli.CLI"

# TODO use the jar for faster execution
#if [ -s bin/rdfunit.jar ]
#then
# mvn assembly:single -DdescriptorId=jar-with-dependencies
# cp /tmp/docu.html "$doc"".html"
#fi

if [ ! -d "rdfunit-validate/target" ]; then
echo "First run, compiling code..."
mvn -pl rdfunit-validate -am clean install \
-Dmaven.test.skip=true \
-Dmaven.javadoc.skip=true \
-Dgpg.skip=true \
-Dsource.skip=true
echo "Compiling finished..."
fi

mvn -pl rdfunit-validate exec:java -q -Dexec.mainClass="$MAIN_CLS" -Dexec.args="generate $*"
Loading

0 comments on commit 4716b0c

Please sign in to comment.