Permalink
Browse files

Graph will be generated if "CONSTRUCT" statement is in the SPARQL query.

 Still need to refine UI for building SPARQL.
  • Loading branch information...
1 parent 320d45d commit dd9b3fafc0c466ea9da0ff75b7569fb8dad62e01 @keiono committed Aug 25, 2011
View
6 pom.xml
@@ -105,6 +105,12 @@
<dependency>
<groupId>org.cytoscape</groupId>
+ <artifactId>property-api</artifactId>
+ <version>3.0.0-alpha6-SNAPSHOT</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.cytoscape</groupId>
<artifactId>work-api</artifactId>
<version>3.0.0-alpha6-SNAPSHOT</version>
<scope>provided</scope>
View
12 src/main/java/org/cytoscape/blueprints/graphdb/EndpointManager.java
@@ -0,0 +1,12 @@
+package org.cytoscape.blueprints.graphdb;
+
+import java.util.Map;
+
+public interface EndpointManager {
+
+ public static final String ENDPOINT_PREFIX = "sparql.endpoint";
+
+ // Human readable source name to URI
+ Map<String, String> getEndpointMap();
+
+}
View
12 src/main/java/org/cytoscape/blueprints/graphdb/SPARQLTask.java
@@ -0,0 +1,12 @@
+package org.cytoscape.blueprints.graphdb;
+
+import java.util.List;
+import java.util.Map;
+
+import com.tinkerpop.blueprints.pgm.Vertex;
+
+public interface SPARQLTask {
+
+ List<Map<String, Vertex>> getResult();
+
+}
View
30 src/main/java/org/cytoscape/blueprints/graphdb/internal/sail/ExecuteSPARQLTask.java
@@ -3,25 +3,43 @@
import java.util.List;
import java.util.Map;
+import org.cytoscape.blueprints.graphdb.SPARQLTask;
import org.cytoscape.work.AbstractTask;
import org.cytoscape.work.TaskMonitor;
+import org.cytoscape.work.Tunable;
import com.tinkerpop.blueprints.pgm.Vertex;
import com.tinkerpop.blueprints.pgm.impls.sail.SailGraph;
-public class ExecuteSPARQLTask extends AbstractTask {
+
+/**
+ * Execute SPARQL
+ *
+ */
+public class ExecuteSPARQLTask extends AbstractTask implements SPARQLTask {
private final SailGraph sail;
- private final String query;
- public ExecuteSPARQLTask(final SailGraph sail, final String query) {
+ @Tunable(description="Enter SPARQL Query")
+ public String query;
+
+ private List<Map<String, Vertex>> result;
+
+ public ExecuteSPARQLTask(final SailGraph sail) {
this.sail = sail;
- this.query = query;
}
@Override
public void run(TaskMonitor taskMonitor) throws Exception {
- final List<Map<String, Vertex>> result = sail.executeSparql(query);
+ if(query == null || sail == null)
+ throw new NullPointerException("Graph and Query should not be null.");
+
+ result = sail.executeSparql(query);
+ }
+
+
+ @Override
+ public List<Map<String, Vertex>> getResult() {
+ return result;
}
-
}
View
10 src/main/java/org/cytoscape/blueprints/graphdb/internal/sail/ExecuteSPARQLTaskFactory.java
@@ -7,19 +7,11 @@
public class ExecuteSPARQLTaskFactory implements TaskFactory {
- private String query;
private SailGraph sail;
@Override
public TaskIterator getTaskIterator() {
- if(sail == null || query == null)
- throw new NullPointerException("Graph or Query is null.");
-
- return new TaskIterator(new ExecuteSPARQLTask(sail, query));
- }
-
- public void setQuery(final String query) {
- this.query = query;
+ return new TaskIterator(new ExecuteSPARQLTask(sail));
}
public void setDB(final SailGraph sail) {
View
36 src/main/java/org/cytoscape/blueprints/graphdb/internal/sesame/EndpointManagerImpl.java
@@ -0,0 +1,36 @@
+package org.cytoscape.blueprints.graphdb.internal.sesame;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.cytoscape.blueprints.graphdb.EndpointManager;
+import org.cytoscape.property.CyProperty;
+
+public class EndpointManagerImpl implements EndpointManager {
+
+ private final Map<String, String> endpoints;
+
+ private final CyProperty<Properties> prop;
+
+ EndpointManagerImpl(final CyProperty<Properties> prop) {
+ this.prop = prop;
+ endpoints = new HashMap<String, String>();
+ }
+
+ @Override
+ public Map<String, String> getEndpointMap() {
+ final Properties spProps = prop.getProperties();
+ for(final Object key: spProps.keySet()) {
+ final String keyString = key.toString();
+ if(keyString.startsWith(ENDPOINT_PREFIX))
+ endpoints.put(keyString, spProps.getProperty(keyString));
+ }
+ System.out.println("===========> Endpoints = " + endpoints.size());
+ return endpoints;
+ }
+
+
+
+
+}
View
140 ...main/java/org/cytoscape/blueprints/graphdb/internal/sesame/SendSPARQLToEndpointsTask.java
@@ -0,0 +1,140 @@
+package org.cytoscape.blueprints.graphdb.internal.sesame;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.cytoscape.blueprints.graphdb.EndpointManager;
+import org.cytoscape.model.CyEdge;
+import org.cytoscape.model.CyNetwork;
+import org.cytoscape.model.CyNetworkFactory;
+import org.cytoscape.model.CyNetworkManager;
+import org.cytoscape.model.CyNode;
+import org.cytoscape.model.CyTableEntry;
+import org.cytoscape.work.AbstractTask;
+import org.cytoscape.work.TaskMonitor;
+import org.cytoscape.work.Tunable;
+import org.cytoscape.work.util.ListSingleSelection;
+import org.openrdf.model.Resource;
+import org.openrdf.model.Statement;
+import org.openrdf.model.URI;
+import org.openrdf.model.Value;
+import org.openrdf.query.BindingSet;
+import org.openrdf.query.GraphQuery;
+import org.openrdf.query.GraphQueryResult;
+import org.openrdf.query.QueryLanguage;
+import org.openrdf.query.TupleQuery;
+import org.openrdf.query.TupleQueryResult;
+import org.openrdf.repository.RepositoryConnection;
+import org.openrdf.repository.http.HTTPRepository;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This is a utility class to get result from remote/local endpoints.
+ *
+ */
+public class SendSPARQLToEndpointsTask extends AbstractTask {
+
+ private static final Logger logger = LoggerFactory.getLogger(SendSPARQLToEndpointsTask.class);
+
+ private final Map<String, String> eMap;
+
+ @Tunable(description = "SPARQL Endpoint")
+ public ListSingleSelection<String> endpoints;
+
+ @Tunable(description = "Create RDF Graph?")
+ public Boolean isGraph;
+
+ @Tunable(description = "SPARQL Query")
+ public String query;
+
+ private final CyNetworkFactory networkFactory;
+ private final CyNetworkManager networkManager;
+
+ SendSPARQLToEndpointsTask(final EndpointManager manager, final CyNetworkFactory networkFactory,
+ final CyNetworkManager networkManager) {
+ this.networkFactory = networkFactory;
+ this.networkManager = networkManager;
+
+ this.eMap = manager.getEndpointMap();
+ endpoints = new ListSingleSelection<String>(new ArrayList<String>(eMap.keySet()));
+ }
+
+ @Override
+ public void run(TaskMonitor taskMonitor) throws Exception {
+ String key = endpoints.getSelectedValue();
+ String urlString = eMap.get(key);
+ System.out.println("Sending query to the endpoint: " + urlString);
+ sendQuery(urlString);
+ }
+
+ private void sendQuery(final String endpointURL) throws Exception {
+ final HTTPRepository endpoint = new HTTPRepository(endpointURL, "");
+ endpoint.initialize();
+
+ final RepositoryConnection conn = endpoint.getConnection();
+
+ try {
+ if (this.isGraph)
+ createGraph(conn, endpointURL);
+ else
+ createTable(conn);
+ } finally {
+ conn.close();
+ }
+ }
+
+ private void createTable(RepositoryConnection conn) throws Exception {
+ final TupleQuery tQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, query);
+ final TupleQueryResult result = tQuery.evaluate();
+
+ while (result.hasNext()) {
+ final BindingSet entry = result.next();
+ final Set<String> names = entry.getBindingNames();
+ for (String name : names)
+ System.out.println(name + " = " + entry.getBinding(name));
+ }
+ }
+
+ private void createGraph(RepositoryConnection conn, String endpointURL) throws Exception {
+ final GraphQuery gQuery = conn.prepareGraphQuery(QueryLanguage.SPARQL, query);
+ final GraphQueryResult result = gQuery.evaluate();
+
+ // RDF Graph returned from the query
+ final CyNetwork network = networkFactory.getInstance();
+ network.getCyRow().set(CyTableEntry.NAME, "SPARQL Query Result: " + endpointURL);
+ final Map<String, CyNode> nodeMap = new HashMap<String, CyNode>();
+
+ while (result.hasNext()) {
+ final Statement entry = result.next();
+ final Resource sub = entry.getSubject();
+ final URI pr = entry.getPredicate();
+ final Value obj = entry.getObject();
+
+ final String subjectValue = sub.stringValue();
+ final String objectValue = obj.stringValue();
+ CyNode source = nodeMap.get(subjectValue);
+ CyNode target = nodeMap.get(objectValue);
+
+ if (source == null) {
+ source = network.addNode();
+ source.getCyRow().set(CyTableEntry.NAME, subjectValue);
+ nodeMap.put(subjectValue, source);
+ }
+
+ if (target == null) {
+ target = network.addNode();
+ target.getCyRow().set(CyTableEntry.NAME, objectValue);
+ nodeMap.put(objectValue, target);
+ }
+
+ final CyEdge edge = network.addEdge(source, target, true);
+ edge.getCyRow().set(CyTableEntry.NAME, pr.stringValue());
+
+ }
+
+ networkManager.addNetwork(network);
+ }
+}
View
26 ...va/org/cytoscape/blueprints/graphdb/internal/sesame/SendSPARQLToEndpointsTaskFactory.java
@@ -0,0 +1,26 @@
+package org.cytoscape.blueprints.graphdb.internal.sesame;
+
+import org.cytoscape.blueprints.graphdb.EndpointManager;
+import org.cytoscape.model.CyNetworkFactory;
+import org.cytoscape.model.CyNetworkManager;
+import org.cytoscape.work.TaskFactory;
+import org.cytoscape.work.TaskIterator;
+
+public class SendSPARQLToEndpointsTaskFactory implements TaskFactory {
+
+ private final EndpointManager manager;
+ private final CyNetworkFactory networkFactory;
+ private final CyNetworkManager networkManager;
+
+ SendSPARQLToEndpointsTaskFactory(final EndpointManager manager, final CyNetworkFactory networkFactory,
+ final CyNetworkManager networkManager) {
+ this.manager = manager;
+ this.networkFactory = networkFactory;
+ this.networkManager = networkManager;
+ }
+
+ @Override
+ public TaskIterator getTaskIterator() {
+ return new TaskIterator(new SendSPARQLToEndpointsTask(manager, networkFactory, networkManager));
+ }
+}
View
23 src/main/resources/META-INF/spring/bundle-context-osgi.xml
@@ -21,20 +21,35 @@
<entry key="menuGravity" value="2.0" />
</osgi:service-properties>
</osgi:service>
-
-
- <osgi:service id="convertGraphToCyNetworkTaskFactoryService" ref="convertGraphToCyNetworkTaskFactory"
- interface="org.cytoscape.work.TaskFactory">
+
+
+ <osgi:service id="convertGraphToCyNetworkTaskFactoryService"
+ ref="convertGraphToCyNetworkTaskFactory" interface="org.cytoscape.work.TaskFactory">
<osgi:service-properties>
<entry key="title" value="From Graph Database..." />
<entry key="preferredMenu" value="File.New.Network" />
<entry key="menuGravity" value="2.0" />
</osgi:service-properties>
</osgi:service>
+
+ <osgi:service id="sendSPARQLToEndpointsTaskFactoryService"
+ ref="sendSPARQLToEndpointsTaskFactory" interface="org.cytoscape.work.TaskFactory">
+ <osgi:service-properties>
+ <entry key="title" value="Send SPARQL Query to..." />
+ <entry key="preferredMenu" value="File.Import" />
+ <entry key="menuGravity" value="2.0" />
+ </osgi:service-properties>
+ </osgi:service>
<osgi:service id="databaseManagerService" ref="databaseManager"
auto-export="interfaces" />
<osgi:service id="graphConverterService" ref="graphConverter"
auto-export="interfaces" />
+
+ <osgi:service id="endpointManagerService" ref="endpointManager"
+ auto-export="interfaces" />
+
+ <osgi:reference id="cyPropertyServiceRef" interface="org.cytoscape.property.CyProperty"
+ filter="(cyPropertyName=cytoscape.props)" />
</beans>
View
23 src/main/resources/META-INF/spring/bundle-context.xml
@@ -11,10 +11,11 @@
http://www.springframework.org/schema/util/spring-util.xsd">
- <bean id="databaseManager" class="org.cytoscape.blueprints.graphdb.internal.sail.GraphDatabaseManagerImpl">
+ <bean id="databaseManager"
+ class="org.cytoscape.blueprints.graphdb.internal.sail.GraphDatabaseManagerImpl">
<constructor-arg ref="cyApplicationConfigurationServiceRef" />
</bean>
-
+
<bean id="graphConverter" class="org.cytoscape.blueprints.graphdb.GraphConverterImpl">
<constructor-arg ref="networkFactoryServiceRef" />
</bean>
@@ -23,12 +24,24 @@
class="org.cytoscape.blueprints.graphdb.internal.sail.LoadRdfToSailTaskFactory">
<constructor-arg ref="databaseManager" />
</bean>
-
- <bean id="convertGraphToCyNetworkTaskFactory" class="org.cytoscape.blueprints.graphdb.internal.sail.ConvertGraphToCyNetworkTaskFactory">
+
+ <bean id="convertGraphToCyNetworkTaskFactory"
+ class="org.cytoscape.blueprints.graphdb.internal.sail.ConvertGraphToCyNetworkTaskFactory">
<constructor-arg ref="databaseManager" />
<constructor-arg ref="graphConverter" />
<constructor-arg ref="cyNetworkManagerServiceRef" />
-
+ </bean>
+
+ <bean id="endpointManager"
+ class="org.cytoscape.blueprints.graphdb.internal.sesame.EndpointManagerImpl">
+ <constructor-arg ref="cyPropertyServiceRef" />
+ </bean>
+
+ <bean id="sendSPARQLToEndpointsTaskFactory"
+ class="org.cytoscape.blueprints.graphdb.internal.sesame.SendSPARQLToEndpointsTaskFactory">
+ <constructor-arg ref="endpointManager" />
+ <constructor-arg ref="networkFactoryServiceRef" />
+ <constructor-arg ref="cyNetworkManagerServiceRef" />
</bean>
</beans>

0 comments on commit dd9b3fa

Please sign in to comment.