Mentions légales du service

Skip to content
Snippets Groups Projects
Commit b21b223c authored by Arun Sharma's avatar Arun Sharma
Browse files

No commit message

No commit message
parent 53d1af9c
No related branches found
No related tags found
No related merge requests found
/*
* QueryProcessor.java
*
* Created on March 20, 2006, 10:29 AM
*
*/
package fr.inrialpes.exmo.QueryProcessor;
/**
*
* @author Arun Sharma
*/
public interface QueryProcessor {
/**
* @param query -- The query string
* @param type -- The query type, can be one of SELECT, ASK, CONSTRUCT, or DESCRIBE
* @returns Result, result form depends on type
*/
public Result query(String query, Type type);
/**
*@param query -- The query string
*/
public Result query(String query);
/**
*@param query -- The query string
*@returns query results as string
*/
public String queryWithStringResults(String query);
/**
*@param query -- the query string
*@returns the type of the query
*/
public int getType(String query);
public void loadOntology(String uri);
}
/*
* QueryTypeMismatchException.java
*
* Created on March 20, 2006, 11:03 AM
*
*/
package fr.inrialpes.exmo.QueryProcessor;
/**
*
* @author Arun Sharma
*/
public class QueryTypeMismatchException extends Exception {
/**
* Create a new <code>QueryTypeMismatchException</code> with no
* detail mesage.
*/
public QueryTypeMismatchException() {
super();
}
/**
* Create a new <code>QueryTypeMismatchException</code> with
* the <code>String</code> specified as an error message.
*
* @param message The error message for the exception.
*/
public QueryTypeMismatchException(String message) {
super(message);
}
/**
* Create a new <code>QueryTypeMismatchException</code> with
* the <code>String</code> specified as an error message and the
* <code>Throwable</code> that caused this exception to be raised.
* @param message The error message for the exception
* @param cause The cause of the exception
*/
public QueryTypeMismatchException(String message, Throwable cause) {
super(message, cause);
}
}
/*
* RDFGraph.java
*
* Created on March 20, 2006, 11:10 AM
*
*/
package fr.inrialpes.exmo.QueryProcessor;
/**
*
* @author Arun Sharma
*/
public interface RDFGraph {
/**
*@returns RDF/XML representation of the graph
*/
public String getXML();
/**@returns rdf triples
*/
public Triple[] getTriples();
//TODO: getN3();
//TODO: some JenaRepresentation
}
/*
* Result.java
*
* Created on March 20, 2006, 10:55 AM
*
*/
package fr.inrialpes.exmo.QueryProcessor;
import java.util.Collection;
/**
*
* @author Arun Sharma
*/
public interface Result {
/**@returns the type of the result set
*/
public int getType();
/**@returns the reslut for ASK type queries
*/
public boolean getAskResult() throws QueryTypeMismatchException;
/**
*@returns the RDF graph for construct queries
*/
public RDFGraph getConstructResult() throws QueryTypeMismatchException;
/**@returns a collection set for SELECT queries
*/
public Collection getSelectResult() throws QueryTypeMismatchException;
/**@returns an XML string for the SELECT queries
*/
public String getSelectResultasXML() throws QueryTypeMismatchException;
}
/*
* Triple.java
*
* Created on March 20, 2006, 11:12 AM
*
*/
package fr.inrialpes.exmo.QueryProcessor;
/**
*
* @author Arun Sharma
*/
public interface Triple {
public String subject = "";
public String object = "";
public String predicate = "";
public String getObject();
public void setObject(String obj);
public String getPredicate() ;
public void setPredicate(String pred);
public String getSubject();
public void setSubject(String sub);
}
/*
* Type.java
*
* Created on March 20, 2006, 11:20 AM
*
*
*/
package fr.inrialpes.exmo.QueryProcessor;
/**
*
* @author Arun Sharma
*/
public class Type {
public static int SELECT = 1;
public static int ASK = 2;
public static int CONSTRUCT = 3;
public static int DESCRIBE = 4;
/** Creates a new instance of Type */
public Type() {
}
}
package fr.inrialpes.exmo.QueryProcessor.impl;
/**
* @author Arun Sharma
*/
import java.util.Set;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
import java.net.URL;
import java.io.File;
import java.io.FileInputStream;
import java.io.DataInputStream;
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.StringWriter;
import org.mindswap.pellet.output.TableData;
import org.mindswap.pellet.output.OutputFormatter;
import fr.inrialpes.exmo.elster.picster.markup.Razor;
import fr.inrialpes.exmo.elster.picster.markup.MarkupModel;
import org.mindswap.pellet.output.ATermAbstractSyntaxRenderer;
import fr.inrialpes.exmo.elster.picster.*;
import org.mindswap.pellet.output.ATermRenderer;
import com.hp.hpl.jena.query.ARQ;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import org.mindswap.markup.media.MarkupImageModel;
import fr.inrialpes.exmo.QueryProcessor.Result;
import fr.inrialpes.exmo.QueryProcessor.Type;
import fr.inrialpes.exmo.QueryProcessor.QueryProcessor;
import fr.inrialpes.exmo.QueryProcessor.impl.ResultImpl;
/**
*
* @author Arun Sharma
*/
public class QueryProcessorImpl implements QueryProcessor {
String query;
Model aModel;
ResultSet aResultSet;
Set aResults = new HashSet();
Hashtable annotation = new Hashtable();
public QueryProcessorImpl() {
}
public QueryProcessorImpl(Model m) {
aModel = m;
}
public Result query(String aQuery) {
query = aQuery;
Query myquery = QueryFactory.create(query);
QueryExecution qe = QueryExecutionFactory.create(myquery, aModel);
System.err.println("Executing query: " + query);
aResultSet = qe.execSelect();
while (aResultSet.hasNext())
{
QuerySolution aSolution = aResultSet.nextSolution();
Resource aRes = (Resource)aSolution.get("uri");
aResults.add(aRes.getURI());
getAnnotations(aRes, aRes.getURI());
Property p = aModel.getProperty("http://www.mindswap.org/~glapizco/technical.owl#", "depiction");
StmtIterator stmtr = aRes.listProperties(p);
//String imguri = "";
String imaAnnos = "";
while(stmtr.hasNext()) {
Statement st = stmtr.nextStatement();
String imguri = st.getObject().toString();
System.out.println("Depiction URI = " + imguri);
if(!aResults.contains(imguri))
aResults.add(imguri);
String thisImgAnno = getAnnotations(aRes, imguri);
imaAnnos = imaAnnos + thisImgAnno + "<image>";
}
//getAnnotations(aRes, imguri);
annotation.put(aRes.getURI(), imaAnnos);
}
qe.close();
ResultImpl res = new ResultImpl(aResults);
return res;
}
public ResultSet performQuery() {
Query myquery = QueryFactory.create(query);
QueryExecution qe = QueryExecutionFactory.create(myquery, aModel);
System.err.println("Executing query: " + query);
aResultSet = qe.execSelect();
while (aResultSet.hasNext())
{
QuerySolution aSolution = aResultSet.nextSolution();
Resource aRes = (Resource)aSolution.get("uri");
aResults.add(aRes.getURI());
getAnnotations(aRes, aRes.getURI());
Property p = aModel.getProperty("http://www.mindswap.org/~glapizco/technical.owl#", "depiction");
StmtIterator stmtr = aRes.listProperties(p);
//String imguri = "";
String imaAnnos = "";
while(stmtr.hasNext()) {
Statement st = stmtr.nextStatement();
String imguri = st.getObject().toString();
System.out.println("Depiction URI = " + imguri);
if(!aResults.contains(imguri))
aResults.add(imguri);
String thisImgAnno = getAnnotations(aRes, imguri);
imaAnnos = imaAnnos + thisImgAnno + "<image>";
}
//getAnnotations(aRes, imguri);
annotation.put(aRes.getURI(), imaAnnos);
}
qe.close();
return aResultSet;
}
public String getAnnotations(Resource res, String mediaUri) {
String queryAnno = "";
String uri = res.getURI();
String instanceFileName = uri.substring(uri.lastIndexOf('/')+1, uri.lastIndexOf('.')) + ".rdf";
String annoFileName = "";
if(!uri.equals(mediaUri)) {
if(mediaUri.contains("region")) {
String temp = mediaUri.substring(0, mediaUri.indexOf("region")-1);
annoFileName = temp.substring(temp.lastIndexOf('/')+1, temp.length() ) + ".rdf";
}
else
annoFileName = mediaUri.substring(mediaUri.lastIndexOf('/')+1, mediaUri.length()) + ".rdf";
}
System.out.println("\n\n\nInstanceFileName = " + instanceFileName);
System.out.println("\n\n\nAnnotationFileName = " + annoFileName);
try {
FileInputStream in = new FileInputStream(new File("annotations/" + annoFileName));
DataInputStream ds = new DataInputStream(in);
String annotations = "";
while(ds.available() != 0) {
annotations = annotations + ds.readLine();
annotations = annotations + "\n";
}
OntModel m = ModelFactory.createOntologyModel();
StmtIterator stms = res.listProperties();
m.add(stms);
StringWriter sr = new StringWriter();
m.write(sr, "RDF/XML-ABBREV");
String instance = sr.toString();
/* queryAnno = "<" + annoFileName + ">" + annotations + "</" + annoFileName + ">"
+ "<" + instanceFileName + ">" + instance + "</" + instanceFileName + ">";
*/
queryAnno = annoFileName + "<divider>" +annotations + "<divider>"+ instanceFileName + "<divider>" + instance;
} catch(Exception e) {
System.err.println("Annotation found not found");
}
//annotation.put(mediaUri, queryAnno);
return queryAnno;
}
public String getAnnotationforUri(String uri) {
String aAnno = (String) annotation.get(uri);
return aAnno;
}
public Set getResourceUrisAsSet(ResultSet aResultSet) {
/* Set aResults = new HashSet();
while (aResultSet.hasNext())
{
}*/
return aResults;
}
public void showImages(Set mResults, Razor myApp) {
System.out.println("Reached show Images");
Vector imgUrls = new Vector();
Iterator itr = mResults.iterator();
while(itr.hasNext()) {
String uri = (String) itr.next();
String imageLocation = (String)MarkupImageModel.getUriMap().get(uri);
if(imageLocation == null)
imageLocation = (String)MarkupImageModel.getRegUriMap().get(uri);
imgUrls.add(imageLocation);
}
for(int j = 0; j < imgUrls.size(); j++) {
try {
String url = (String) imgUrls.get(j);
System.out.println("Image Location = " + url);
URL aUrl = new URL(url);
myApp.loadURL(aUrl);
} catch(Exception e) {
System.out.println("Error in image location");
}
}
}
public Result query(String query, Type type) {
return null;
}
public String queryWithStringResults(String query) {
return null;
}
public int getType(String query) {
return 0;
}
public void loadOntology(String uri) {
}
}
/*
* ResultImpl.java
*
* Created on May 21, 2006, 10:49 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package fr.inrialpes.exmo.QueryProcessor.impl;
import fr.inrialpes.exmo.QueryProcessor.*;
import java.util.*;
/**
*
* @author Arun Sharma
*/
public class ResultImpl implements Result{
Set aSet = new HashSet();
public ResultImpl(Set set) {
aSet = set;
}
/**@returns a collection set for SELECT queries
*/
public Collection getSelectResult() throws QueryTypeMismatchException {
return aSet;
}
public int getType() {
return 0;
}
public boolean getAskResult() throws QueryTypeMismatchException {
return false;
}
public RDFGraph getConstructResult() throws QueryTypeMismatchException {
return null;
}
public String getSelectResultasXML() throws QueryTypeMismatchException {
return null;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment