Skip to content

Commit

Permalink
Merge pull request #5 from egonw/feature/compareModels
Browse files Browse the repository at this point in the history
Feature/compare models
  • Loading branch information
goglepox committed Aug 20, 2014
2 parents 01eebd2 + 6aed209 commit 9656d81
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,44 @@ public List<String> allOwlSameAs(IRDFStore store, String resourceURI)
)
public List<String> getForPredicate(IRDFStore store, String resourceURI, String predicate)
throws IOException, BioclipseException, CoreException;

@Recorded
@PublishedMethod(
params = "IRDFStore first, IRDFStore second",
methodSummary = "Takes the union of the two stores and returns that as a IRDFStore."
)
public IRDFStore union(IRDFStore first, IRDFStore second)
throws IOException, BioclipseException, CoreException;

@Recorded
@PublishedMethod(
params = "IRDFStore first, IRDFStore second",
methodSummary = "Takes the intersection of the two stores and returns that as a IRDFStore."
)
public IRDFStore intersection(IRDFStore first, IRDFStore second)
throws IOException, BioclipseException, CoreException;

@Recorded
@PublishedMethod(
params = "IRDFStore first, IRDFStore second",
methodSummary = "Takes the difference of the two stores and returns that as a IRDFStore."
)
public IRDFStore difference(IRDFStore first, IRDFStore second)
throws IOException, BioclipseException, CoreException;

@Recorded
@PublishedMethod(
params = "IRDFStore store, String firstNode, String secondNode",
methodSummary = "The shortest path between two nodes."
)
public List<String> shortestPath(IRDFStore store, String firstNode, String secondNode)
throws IOException, BioclipseException, CoreException;

@Recorded
@PublishedMethod(
params = "IRDFStore store, String firstNode, String secondNode, String predicate",
methodSummary = "The shortest path between two nodes, following only one specific predicate."
)
public List<String> shortestPath(IRDFStore store, String firstNode, String secondNode, String predicate)
throws IOException, BioclipseException, CoreException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import org.eclipse.core.runtime.NullProgressMonitor;

import com.hp.hpl.jena.n3.turtle.TurtleParseException;
import com.hp.hpl.jena.ontology.OntTools;
import com.hp.hpl.jena.ontology.OntTools.Path;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
Expand All @@ -60,6 +62,7 @@
import com.hp.hpl.jena.shared.PrefixMapping;
import com.hp.hpl.jena.shared.SyntaxError;
import com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP;
import com.hp.hpl.jena.util.iterator.Filter;

public class RDFManager implements IBioclipseManager {

Expand Down Expand Up @@ -424,6 +427,42 @@ public void copy(IRDFStore targetStore, IRDFStore sourceStore)
);
}

public IRDFStore union(IRDFStore first, IRDFStore second)
throws IOException, BioclipseException, CoreException {
if (!(first instanceof IJenaStore &&
second instanceof IJenaStore)) {
throw new BioclipseException("Only supporting IJenaStore.");
}
Model unionModel = ((IJenaStore)first).getModel().union(
((IJenaStore)second).getModel()
);
return new JenaModel(unionModel);
}

public IRDFStore intersection(IRDFStore first, IRDFStore second)
throws IOException, BioclipseException, CoreException {
if (!(first instanceof IJenaStore &&
second instanceof IJenaStore)) {
throw new BioclipseException("Only supporting IJenaStore.");
}
Model unionModel = ((IJenaStore)first).getModel().intersection(
((IJenaStore)second).getModel()
);
return new JenaModel(unionModel);
}

public IRDFStore difference(IRDFStore first, IRDFStore second)
throws IOException, BioclipseException, CoreException {
if (!(first instanceof IJenaStore &&
second instanceof IJenaStore)) {
throw new BioclipseException("Only supporting IJenaStore.");
}
Model unionModel = ((IJenaStore)first).getModel().difference(
((IJenaStore)second).getModel()
);
return new JenaModel(unionModel);
}

public void addPrefix(IRDFStore store, String prefix, String namespace)
throws BioclipseException {
if (!(store instanceof IJenaStore)) {
Expand Down Expand Up @@ -664,4 +703,78 @@ public List<String> allOwlEquivalentClassOneDown(IRDFStore store, String resourc
resources.addAll(results.getColumn("resource"));
return resources;
}

public List<String> shortestPath(IRDFStore store, String firstNode, String secondNode)
throws IOException, BioclipseException, CoreException {
if (!(store instanceof IJenaStore))
throw new RuntimeException(
"Can only handle IJenaStore's for now."
);
Model model = ((IJenaStore)store).getModel();

Path path = OntTools.findShortestPath(
model,
model.createResource(firstNode),
model.createResource(secondNode),
new Filter<Statement>() {
@Override
public boolean accept(Statement arg0) {
return true; // any predicate in the path is fine
}
}
);
if (path == null) return Collections.emptyList();

List<String> uriList = new ArrayList<String>();
Resource lastNode = null;
for (Statement statement : path) {
System.out.println(
statement.getSubject() + " -> " +
statement.getPredicate() + " -> " +
statement.getObject()
);
uriList.add(statement.getSubject().getURI());
uriList.add(statement.getPredicate().getURI());
lastNode = statement.getObject().asResource();
}
uriList.add(lastNode.getURI());
return uriList;
}

public List<String> shortestPath(IRDFStore store, String firstNode, String secondNode, final String predicate)
throws IOException, BioclipseException, CoreException {
if (!(store instanceof IJenaStore))
throw new RuntimeException(
"Can only handle IJenaStore's for now."
);
Model model = ((IJenaStore)store).getModel();

Path path = OntTools.findShortestPath(
model,
model.createResource(firstNode),
model.createResource(secondNode),
new Filter<Statement>() {
@Override
public boolean accept(Statement arg0) {
return arg0.getPredicate().hasURI(predicate);
}
}
);
if (path == null) return Collections.emptyList();

List<String> uriList = new ArrayList<String>();
Resource lastNode = null;
for (Statement statement : path) {
System.out.println(
statement.getSubject() + " -> " +
statement.getPredicate() + " -> " +
statement.getObject()
);
uriList.add(statement.getSubject().getURI());
uriList.add(statement.getPredicate().getURI());
lastNode = statement.getObject().asResource();
}
uriList.add(lastNode.getURI());
return uriList;
}
}

0 comments on commit 9656d81

Please sign in to comment.