Permalink
Browse files

Second prototype. Now it includes DM and R2RML

  • Loading branch information...
1 parent 07f8d93 commit 2fc703b710acb8b687146bdcdbe61a12e3ce5f6b @boricles committed Apr 17, 2012
Showing with 136 additions and 26 deletions.
  1. +1 −1 build.xml
  2. +73 −4 src/org/rdb2rdf/testcase/th/Comparator.java
  3. +60 −21 src/org/rdb2rdf/testcase/th/model/RDB2RDFTC.java
  4. +2 −0 util/rdb2rdf-th
View
@@ -12,7 +12,7 @@
<property name="lib.dir" value="${dist.dir}/lib"/>
<property name="jar.name" value="${ant.project.name}.jar"/>
<property name="main-class" value="org.rdb2rdf.testcase.th.TCScanner"/>
-<property name="version" value="0.0.1"/>
+<property name="version" value="0.0.2"/>
<description> rdb2rdf-th build file</description>
@@ -1,29 +1,71 @@
package org.rdb2rdf.testcase.th;
import java.io.InputStream;
+import java.util.Iterator;
+import com.hp.hpl.jena.graph.Factory;
+import com.hp.hpl.jena.graph.Graph;
+import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
+import com.hp.hpl.jena.sparql.core.DatasetGraph;
+import com.hp.hpl.jena.sparql.core.Quad;
+import com.hp.hpl.jena.sparql.lib.DatasetLib;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
+import org.openjena.atlas.lib.Sink;
+import org.openjena.atlas.lib.SinkNull;
+import org.openjena.atlas.lib.SinkWrapper;
+import org.openjena.riot.Lang;
+import org.openjena.riot.RiotLoader;
+import org.openjena.riot.RiotReader;
+import org.openjena.riot.RiotWriter;
+import org.openjena.riot.lang.LangRIOT;
+import org.openjena.riot.lang.SinkToGraph;
+import org.openjena.riot.lang.SinkTriplesToGraph;
+
public class Comparator {
protected Model sourceModel;
protected Model targetModel;
+
+ protected DatasetGraph sourceDataSet;
+ protected DatasetGraph targetDataSet;
+
+ protected String sourceFileName;
+ protected String targetFileName;
public Comparator() {
-
}
public Comparator(String sourceFileName, String targetFileName) {
- sourceModel = loadModel(sourceFileName,sourceModel);
- targetModel = loadModel(targetFileName,targetModel);
+ this(sourceFileName,targetFileName,Lang.NTRIPLES);
+ }
+
+
+ public Comparator(String sourceFileName, String targetFileName, Lang lang) {
+ this.sourceFileName = sourceFileName;
+ this.targetFileName = targetFileName;
+ if ( lang == Lang.NTRIPLES) {
+ sourceModel = loadModel(sourceFileName,sourceModel);
+ targetModel = loadModel(targetFileName,targetModel);
+ }
+ else {
+ sourceDataSet = loadDataSet(sourceFileName,sourceDataSet);
+ targetDataSet = loadDataSet(targetFileName,targetDataSet);
+ }
+ }
+
+
+
+ protected DatasetGraph loadDataSet(String fileName, DatasetGraph datasetGraph) {
+ return RiotLoader.load(fileName, Lang.NQUADS);
}
protected Model loadModel(String fileName, Model model) {
@@ -41,13 +83,40 @@ protected Model loadModel(String fileName, Model model) {
model.read(in, "", "TTL");
}
catch (Exception ex) {
- ex.printStackTrace();
+ System.out.println("Error loading file " + fileName);
+ //ex.printStackTrace();
+ System.exit(0);
}
return model;
}
public boolean modelsAreEquivalent() {
return sourceModel.isIsomorphicWith(targetModel);
}
+
+ public boolean datasetsAreEquivalent() {
+ Iterator<Node> iterSource = sourceDataSet.listGraphNodes();
+ Iterator<Node> iterTarget = targetDataSet.listGraphNodes();
+
+ if (sourceDataSet.isEmpty() != targetDataSet.isEmpty() ) //if one is empty and the other does not -> they are different
+ return false;
+
+ if (sourceDataSet.isEmpty() && targetDataSet.isEmpty() ) { //load as models, because they don't have graphs
+ sourceModel = loadModel(sourceFileName,sourceModel);
+ targetModel = loadModel(targetFileName,targetModel);
+ return modelsAreEquivalent();
+ }
+
+ //if they have graphs
+ for (;iterSource.hasNext();) {
+ Node node = iterSource.next();
+ Graph sourceGraph = sourceDataSet.getGraph(node);
+ Graph targetGraph = targetDataSet.getGraph(node);
+ if (!sourceGraph.isIsomorphicWith(targetGraph))
+ return false;
+
+ }
+ return true;
+ }
}
@@ -10,6 +10,7 @@
import java.util.Date;
import java.util.List;
+import org.openjena.riot.Lang;
import org.rdb2rdf.testcase.th.Comparator;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
@@ -33,7 +34,11 @@
protected Model model;
protected OntModel oModel;
- protected static Model earlModel = null;
+ protected static Model earlModelDM = null;
+ protected static Model earlModelR2RML = null;
+
+ protected static String toolName = null;
+
protected static String nsBase = "http://mappingpedia.org/rdb2rdf/";
protected static String doapURI = "http://usefulinc.com/ns/doap#";
protected static String earlURI = "http://www.w3.org/ns/earl#";
@@ -44,6 +49,9 @@
protected static Resource myTH;
protected static Resource myTool;
+ protected static Resource softwareResource;
+ protected static Resource projectResouce;
+
protected static Resource earlPass;
protected static Resource earlFail;
@@ -68,8 +76,6 @@ public RDB2RDFTC(String vocabulary) {
loadVocabulary();
}
-
-
public void loadVocabulary() {
oModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF, null);
@@ -101,7 +107,7 @@ protected String getToolOutputFileName(String defaultOutputFileName,String toolN
return toolOutputFileName;
}
- protected void createAssertion(boolean passed, String identifier) {
+ protected void createAssertion(Model earlModel, boolean passed, String identifier) {
Resource assertion = earlModel.createResource();
Resource assertionClass = earlModel.createResource(earlURI + "Assertion");
assertion.addProperty(RDF.type, assertionClass);
@@ -168,9 +174,9 @@ protected void processDirectGraph(String toolName) throws Exception {
String id = rdfNode.toString();
if (comparator.modelsAreEquivalent())
- createAssertion(true,id);
+ createAssertion(earlModelDM,true,id);
else
- createAssertion(false,id);
+ createAssertion(earlModelDM,false,id);
}
}
@@ -195,21 +201,40 @@ protected void processR2RML(String toolName) throws Exception {
rdfNode = ind.getPropertyValue(iProperty);
defaultOutputFileName = rdfNode.toString();
toolOutputFileName = getToolOutputFileName(defaultOutputFileName,toolName);
- //System.out.println(defaultOutputFileName + "\t" + toolOutputFileName);
+
//Compare the content of the two files
+ Comparator comparator = new Comparator(currentDir + defaultOutputFileName, currentDir + toolOutputFileName,Lang.NQUADS);
+
+ property = dcURI + "identifier";
+ iProperty = oModel.getOntProperty(property);
+ rdfNode = ind.getPropertyValue(iProperty);
+ String id = rdfNode.toString();
+
+ if (comparator.datasetsAreEquivalent())
+ createAssertion(earlModelR2RML,true,id);
+ else
+ createAssertion(earlModelR2RML,false,id);
}
else { //if there is no expected output
+ property = dcURI + "identifier";
+ iProperty = oModel.getOntProperty(property);
+ rdfNode = ind.getPropertyValue(iProperty);
+ String id = rdfNode.toString();
+ String fileName = currentDir+ "mapped" + id.charAt(id.length()-1) +"-" + toolName + ".nq";
+ File f = new File(fileName);
+ if (f.exists()) //if there is a file generated from the tool
+ createAssertion(earlModelR2RML,false,id);
+ else
+ createAssertion(earlModelR2RML,true,id);
}
-
-
-
}
}
protected void processTCs(String toolName,boolean implementsDM, boolean implementsR2RML) throws Exception {
+ this.toolName = toolName;
if (implementsDM)
processDirectGraph(toolName);
if (implementsR2RML)
@@ -245,36 +270,42 @@ public String getPropertyValue(String file, String clazz, String predicate) {
}
}
catch (Exception ex) {
- ex.printStackTrace();
+ System.out.println("Error reading the file "+file +", class " + clazz + ", predicate "+ predicate);
+ System.exit(0);
}
return value;
}
- protected void checkEARLModel(String toolName,boolean implementsDM, boolean implementsR2RML) {
+ protected Model checkEARLModel(Model earlModel, String toolName,boolean implementsDM, boolean implementsR2RML) {
if (earlModel==null) {
earlModel = ModelFactory.createDefaultModel();
earlPass = earlModel.createResource(earlURI + "pass");
earlFail = earlModel.createResource(earlURI + "fail");
+ softwareResource = earlModel.createResource(earlURI+"Software");
+ projectResouce = earlModel.createResource(doapURI+ "Project");
+
myTool = earlModel.createResource(nsBase+"myProject"+"/"+toolName);
- myTool.addProperty(RDF.type, doapURI + "Project");
+ myTool.addProperty(RDF.type, projectResouce);
Property doapName = earlModel.createProperty(doapURI + "name" );
myTool.addProperty(doapName, toolName);
Property implDM = earlModel.createProperty(NS + "implementsDirectMapping" );
- myTool.addProperty(implDM, new Boolean(implementsDM).toString());
-
+ //myTool.addProperty(implDM, new Boolean(implementsDM).toString());
+ myTool.addLiteral(implDM,earlModel.createTypedLiteral(new Boolean(implementsDM).toString(),XSDDatatype.XSDboolean));
Property implR2RML = earlModel.createProperty(NS + "implementsR2RML" );
- myTool.addProperty(implR2RML, new Boolean(implementsR2RML).toString());
-
+ //myTool.addProperty(implR2RML, new Boolean(implementsR2RML).toString());
+ myTool.addLiteral(implR2RML,earlModel.createTypedLiteral(new Boolean(implementsR2RML).toString(),XSDDatatype.XSDboolean));
+
myTH = earlModel.createResource(nsBase+"myTestHarness");
- myTH.addProperty(RDF.type, earlURI + "Software");
+ myTH.addProperty(RDF.type, softwareResource);
}
+ return earlModel;
}
@@ -295,27 +326,35 @@ public void processDescription(String dbPath, String toolName, boolean implement
oModel.add(model);
- checkEARLModel(toolName, implementsDM, implementsR2RML);
+ earlModelDM = checkEARLModel(earlModelDM,toolName, implementsDM, implementsR2RML);
+ earlModelR2RML = checkEARLModel(earlModelR2RML,toolName, implementsDM, implementsR2RML);
processTCs(toolName,implementsDM,implementsR2RML);
oModel.remove(model);
}
catch (Exception ex) {
+ ex.printStackTrace();
System.out.println("Error while processing the TS");
System.exit(0);
}
}
- public static void saveEarlModel() {
+ public static void saveEarlModel () {
+ saveEarlModel(earlModelDM, toolName, "dm");
+ saveEarlModel(earlModelR2RML, toolName, "r2rml");
+ }
+
+ protected static void saveEarlModel(Model earlModel, String tool, String implement) {
try {
- FileOutputStream fout=new FileOutputStream("earl.ttl");
+ FileOutputStream fout=new FileOutputStream("earl-"+ tool +"-" + implement+ ".ttl");
earlModel.write(fout,"TTL");
}
catch(IOException e) {
System.out.println("Exception caught"+e.getMessage());
+ System.exit(0);
}
}
View
@@ -0,0 +1,2 @@
+#!/bin/bash
+java -cp lib/*:rdb2rdf-th.jar org.rdb2rdf.testcase.th.TCScanner "$@"

0 comments on commit 2fc703b

Please sign in to comment.