org.apache.jena.query.Syntax Java Examples

The following examples show how to use org.apache.jena.query.Syntax. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TripleIndexCreatorContext.java    From AGDISTIS with GNU Affero General Public License v3.0 6 votes vote down vote up
public String sparql(String subject) {

		// First query takes the most specific class from a given resource.
		String ontology_service = endpoint;

		String endpointsSparql = "select ?label where {<" + subject
				+ "> <http://www.w3.org/2000/01/rdf-schema#label> ?label FILTER (lang(?label) = 'en')} LIMIT 100";

		Query sparqlQuery = QueryFactory.create(endpointsSparql, Syntax.syntaxARQ);
		QueryEngineHTTP qexec = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(ontology_service, sparqlQuery);
		qexec.setModelContentType(WebContent.contentTypeRDFXML);
		ResultSet results = qexec.execSelect();
		String property = null;
		while (results.hasNext()) {
			QuerySolution qs = results.next();
			property = qs.getLiteral("?label").getLexicalForm();
		}
		return property;

	}
 
Example #2
Source File: OCUtils.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public static List<Query> loadQueries(File queryFile) throws IOException {
	List<Query> ret = new ArrayList<Query>();
	Reader in = new FileReader(queryFile);
	for (String queryText: SPARQLFileParser.readQueries(in)) {
		queryText= queryText.trim();
		if (queryText.startsWith("#")) {
			continue;
		}
		Query query= QueryFactory.create(queryText, Syntax.syntaxSPARQL_11);
		if (!query.isSelectType()) {
			logger.debug("Ignoring non select query: {}",query.toString());
			continue;
		}
		logger.debug("Query: {}"+query);
		ret.add(query);
	}
	in.close();
	return ret;
}
 
Example #3
Source File: OCUtils.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public static List<ConjunctiveQuery> loadConjunctiveQueries(File queryFile) throws IOException {
	List<ConjunctiveQuery> ret = new ArrayList<ConjunctiveQuery>();
	Reader in = new FileReader(queryFile);
	for (String queryText: SPARQLFileParser.readQueries(in)) {
		queryText= queryText.trim();
		if (queryText.startsWith("#")) {
			continue;
		}
		Query query= QueryFactory.create(queryText, Syntax.syntaxSPARQL_11);
		if (!query.isSelectType()) {
			logger.debug("Ignoring non select query: {}",query.toString());
			continue;
		}
		logger.debug("Query: {}"+query);
		ret.add(new ConjunctiveQuery(query));
	}
	in.close();
	return ret;
}
 
Example #4
Source File: SPARQLExt.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * Guess the syntax (query and update) based on filename.
 *
 * @param url the url of the syntax
 * @param defaultSyntax a default syntax
 * @return an available syntax for the filename
 */
public static Syntax guessFileSyntax(
        final String url,
        final Syntax defaultSyntax) {
    if (url.endsWith(".rqg")) {
        return SYNTAX;
    }
    return Syntax.guessFileSyntax(url, defaultSyntax);
}
 
Example #5
Source File: OCUtils.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public static ConjunctiveQuery parse(String sparqlQuery) {
	try {
		Query query= QueryFactory.create(sparqlQuery, Syntax.syntaxSPARQL_11);
		if (!query.isSelectType()) {
			throw new RuntimeException("Non select query: "+sparqlQuery.toString());
		}
		return new ConjunctiveQuery(query);
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example #6
Source File: ConjunctiveQuery.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public ConjunctiveQuery(Query q) {
	if (!q.isSelectType()) {
		throw new RuntimeException("A conjunctive query can only be created from a select query: "+q.serialize(Syntax.syntaxSPARQL_11));
	}
	query = q;
	setQueryPattern(q.getQueryPattern());
}
 
Example #7
Source File: SPARQLStar2SPARQL.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
public Query convert( Query query )
{
	final ElementTransformSPARQLStar etss = new ElementTransformSPARQLStar();
	final Query convertedQuery = QueryTransformOps.transform(query, etss);
	convertedQuery.setSyntax(Syntax.syntaxSPARQL);
	return convertedQuery;
}
 
Example #8
Source File: SPARQLExt.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * Creates and registers a new syntax with this symbol.
 *
 * @param uri the name of the syntax
 * @return the syntax
 */
public static Syntax make(final String uri) {
    if (uri == null) {
        return null;
    }
    Symbol sym = Symbol.create(uri);
    if (sym.equals(SYNTAX)) {
        return SYNTAX;
    }
    return Syntax.make(uri);
}
 
Example #9
Source File: SPARQLExt.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public boolean accept(Syntax syntax) {
    // Since ARQ syntax is a super set of SPARQL 1.1 both SPARQL 1.0
    // and SPARQL 1.1 can be serialized by the same serializer
    return Syntax.syntaxARQ.equals(syntax) || Syntax.syntaxSPARQL_10.equals(syntax)
            || Syntax.syntaxSPARQL_11.equals(syntax) || SPARQLExt.SYNTAX.equals(syntax);
}
 
Example #10
Source File: ElementTransformSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
protected void checkBgpAndBindOnlyQuery( String sparqlstarQueryString, String expectedResultQuery )
{
	final String baseIRI = null;
	final Query expectedQuery = QueryFactory.create(expectedResultQuery, baseIRI, Syntax.syntaxSPARQL);
	final ElementGroup expectedEG = (ElementGroup) expectedQuery.getQueryPattern();

	final Query convertedQuery = convert( sparqlstarQueryString );
	final ElementGroup convertedEG = (ElementGroup) convertedQuery.getQueryPattern();
	
	checkForEquivalence( expectedEG, mergeElementPathBlocks(convertedEG) );
}
 
Example #11
Source File: SPARQLStar.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Override
      public boolean accept(Syntax s) {
	if (s.equals(syntax))
		return true;
	else
		return wrappedFactory.accept(s);
}
 
Example #12
Source File: SPARQLExt.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public QueryVisitor create(Syntax syntax, Prologue prologue, IndentedWriter writer) {
    SerializationContext context = new SerializationContext(prologue, new NodeToLabelMapBNode("bn", false));
    return new SPARQLExtQuerySerializer(writer, context);
}
 
Example #13
Source File: ExecuteSPARQLStar.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Override
protected Syntax getDefaultSyntax() { return SPARQLStar.syntax; }
 
Example #14
Source File: Query.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Syntax getSyntax() {
	return Syntax.syntaxSPARQL_10;
}
 
Example #15
Source File: PropertyPathToRules.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception{
PropertyPathToRules toRules = new PropertyPathToRules();
String q = "select * where { " +
		"?x (^<http://example.org/p>|<http://example.org/s>?)/(<http://example.org/q>| <http://example.org/r>)+/!(^<http://example.org/t>| ^<http://example.org/u>|<http://example.org/v> )?y ." +
		"?x <http://example.org/s>/<http://example.org/r>/^<http://example.org/t> ?y ." +
		"?x <http://example.org/s>/<http://example.org/r>+/^<http://example.org/t> ?y ." +
		"?x (<http://example.org/s>/<http://example.org/r>+/^<http://example.org/t>)+ ?y ." +
		"?x (<http://example.org/s>/(^(<http://example.org/r>+|<http://example.org/p>))+/(^<http://example.org/t>)+)+ ?y ." +
	"?x <http://example.org/s>/<http://example.org/r>/<http://example.org/t> ?y ." +
	"?x !(a|<http://example.org/s>|<http://example.org/r>| <http://example.org/t> ) ?y ." +
	"?x !(<http://example.org/r>|^<http://example.org/t>| ^a|<http://example.org/s> ) ?y ." +
		"}"; 
// ( iRIref | 'a' )
com.ibm.research.rdf.store.sparql11.model.Query db2rdfQuery =  SparqlParserUtilities.parseSparqlString(q);
System.out.println("Query:\n"+db2rdfQuery.toString());

//System.exit(0);

Query query = QueryFactory.create(q, Syntax.syntaxSPARQL_11);
System.out.println("Parsed Query:\n\t"+query);
ElementGroup group = (ElementGroup)  query.getQueryPattern();
ElementPathBlock p = (ElementPathBlock) group.getElements().get(0);
for (TriplePath path : p.getPattern().getList()) {
	System.out.println("Path: "+path);
	RuleSystem rs = toRules.toRules(path, true, "Triple", "NegatedPropertySetTriple");
	System.out.println("Equivalent Rule System (Main formula: "+rs.getMainHeadFormula()+"):\n"+rs);
	rs = rs.simplify(Collections.singleton(rs.getMainHeadFormula().getPredicate()));
	System.out.println("Equivalent Rule System after simplification (Main formula: "+rs.getMainHeadFormula()+"):\n"
			+rs);
	/*Graph<Predicate> graph = DatalogEngine.buildDependencyGraph(rs);
	LinkedList<Set<Predicate>> sccs =DatalogEngine.topologicalSortOfSCC(graph);
	System.out.println("Predicate dependency graph:\n"+graph);
	System.out.println("Topological sort of predicates:");
	for (Set<Predicate> scc: sccs) {
		System.out.println("\t"+scc);
	}*/
	AtomicFormula goal = new AtomicFormula(rs.getMainHeadFormula().getPredicate(), new ConstantExpr(0), new VariableExpr("Y"));
	RuleSystem mgrs = rs.magicSetTransformation(goal, true, true, false);
	Set<Predicate> predicatesToKeep = HashSetFactory.make();
	predicatesToKeep.add(goal.getPredicate());
	for (Predicate pred: mgrs.getHeadPredicates()) {
		if (pred instanceof MagicSetPredicate) {
			//predicatesToKeep.add(pred);
			/*Set<Predicate> preds = HashSetFactory.make();
			preds.add(goal.getPredicate());
			preds.add(pred);
			RuleSystem rules = mgrs.simplify(preds);
			System.out.println("Rule System For Magic set predicate: "+pred+"\n"+rules);
			*/
		}
	}
	mgrs = mgrs.simplify(predicatesToKeep);
	System.out.println("Rule System after magic set transformation (goal: "+goal+"):\n"+mgrs);
	/*graph = DatalogEngine.buildDependencyGraph(mgrs);
	sccs =DatalogEngine.topologicalSortOfSCC(graph);
	System.out.println("Predicate dependency graph:\n"+graph);
	System.out.println("Topological sort of predicates:");
	for (Set<Predicate> scc: sccs) {
		System.out.println("\t"+scc);
	}*/
}
	

}
 
Example #16
Source File: SPARQLStar.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Override
      public QueryVisitor create(Syntax syntax, Prologue prologue, IndentedWriter writer) {
	return wrappedFactory.create(syntax, prologue, writer);
}
 
Example #17
Source File: SPARQLStar.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Override
      public QueryVisitor create(Syntax syntax, SerializationContext context, IndentedWriter writer) {
	return wrappedFactory.create(syntax, context, writer);
}
 
Example #18
Source File: ConjunctiveQuery.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
public String toString() {
	return query.serialize(Syntax.syntaxSPARQL_11);
}
 
Example #19
Source File: UpdateFactoryFilter.java    From shacl with Apache License 2.0 4 votes vote down vote up
public UpdateRequest create(String str) {
	analyzeRequest(str);
	return UpdateFactory.create(str, Syntax.syntaxARQ);
}
 
Example #20
Source File: SparqlToGremlinCompiler.java    From sparql-gremlin with Apache License 2.0 4 votes vote down vote up
public static GraphTraversal<Vertex, ?> convertToGremlinTraversal(final GraphTraversalSource g, final String query) {
    return convertToGremlinTraversal(g, QueryFactory.create(Prefixes.prepend(query), Syntax.syntaxSPARQL));
}
 
Example #21
Source File: SparqlToGremlinCompiler.java    From sparql-gremlin with Apache License 2.0 4 votes vote down vote up
public static GraphTraversal<Vertex, ?> convertToGremlinTraversal(final Graph graph, final String query) {
    return convertToGremlinTraversal(graph.traversal(), QueryFactory.create(Prefixes.prepend(query), Syntax.syntaxSPARQL));
}
 
Example #22
Source File: SPARQLExt.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public boolean accept(final Syntax syntax) {
    return SYNTAX.equals(syntax);
}
 
Example #23
Source File: SPARQLExt.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public QueryVisitor create(Syntax syntax, SerializationContext context, IndentedWriter writer) {
    return new SPARQLExtQuerySerializer(writer, context);
}
 
Example #24
Source File: SPARQLExt.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public SPARQLParser create(final Syntax syntax) {
    return new ParserSPARQLExt();
}
 
Example #25
Source File: ElementTransformSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 3 votes vote down vote up
protected Query convert( String sparqlstarQueryString )
{
	final String baseIRI = null;

	final Query sparqlstarQuery = QueryFactory.create(sparqlstarQueryString, baseIRI, SPARQLStar.syntax);

	final Query sparqlQuery = new SPARQLStar2SPARQL().convert(sparqlstarQuery);

	assertEquals( Syntax.syntaxSPARQL, sparqlQuery.getSyntax() );

	return sparqlQuery;
}
 
Example #26
Source File: SparqlToGremlinCompiler.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * Converts SPARQL to a Gremlin traversal.
 *
 * @param g           the {@link GraphTraversalSource} instance to execute the traversal from
 * @param sparqlQuery the query to compile to Gremlin
 */
public static GraphTraversal<Vertex, ?> compile(final GraphTraversalSource g, final String sparqlQuery) {
    return compile(g, QueryFactory.create(Prefixes.prepend(sparqlQuery), Syntax.syntaxSPARQL));
}
 
Example #27
Source File: ARQFactory.java    From shacl with Apache License 2.0 2 votes vote down vote up
/**
 * The ARQ Syntax used by default: Syntax.syntaxARQ.
 * @return the default syntax
 */
public Syntax getSyntax() {
	return Syntax.syntaxARQ;
}