Search in sources :

Example 6 with ParseException

use of org.apache.clerezza.rdf.core.sparql.ParseException in project stanbol by apache.

the class ClerezzaYard method find.

@Override
public final QueryResultList<Representation> find(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
    if (parsedQuery == null) {
        throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
    }
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
    Query sparqlQuery;
    //NOTE:
    // - use the endpoint type standard, because we do not know what type of
    //   SPARQL implementation is configured for Clerezza via OSGI
    String sparqlQueryString = SparqlQueryUtils.createSparqlConstructQuery(query, limit, EndpointTypeEnum.Standard);
    try {
        sparqlQuery = QueryParser.getInstance().parse(sparqlQueryString);
    } catch (ParseException e) {
        log.error("ParseException for SPARQL Query in findRepresentation");
        log.error("FieldQuery: " + query);
        log.error("SPARQL Query: " + sparqlQueryString);
        throw new YardException("Unable to parse SPARQL query generated for the parse FieldQuery", e);
    }
    Object resultObject = tcManager.executeSparqlQuery(sparqlQuery, graph);
    final Graph resultGraph;
    if (resultObject instanceof Graph) {
        resultGraph = (Graph) resultObject;
    } else if (resultObject instanceof ImmutableGraph) {
        resultGraph = new IndexedGraph();
        resultGraph.addAll((ImmutableGraph) resultObject);
    } else {
        log.error("Unable to create " + Graph.class + " instance for query reults of type " + resultObject.getClass() + " (this indicates that the used SPARQL Query was not of type CONSTRUCT)");
        log.error("FieldQuery: " + query);
        log.error("SPARQL Query: " + sparqlQueryString);
        throw new YardException("Unable to process results of Query");
    }
    return new RdfQueryResultList(query, resultGraph);
}
Also used : YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) Query(org.apache.clerezza.rdf.core.sparql.query.Query) SparqlFieldQuery(org.apache.stanbol.entityhub.query.clerezza.SparqlFieldQuery) FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) SelectQuery(org.apache.clerezza.rdf.core.sparql.query.SelectQuery) RdfQueryResultList(org.apache.stanbol.entityhub.query.clerezza.RdfQueryResultList) SparqlFieldQuery(org.apache.stanbol.entityhub.query.clerezza.SparqlFieldQuery) ParseException(org.apache.clerezza.rdf.core.sparql.ParseException) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph)

Example 7 with ParseException

use of org.apache.clerezza.rdf.core.sparql.ParseException in project stanbol by apache.

the class ClerezzaRuleStore method findRulesByDescription.

@Override
public RuleList findRulesByDescription(String term) {
    String sparql = "SELECT ?recipe ?rule ?description " + "WHERE { " + "?recipe " + Symbols.hasRule + " ?rule . " + "?rule " + Symbols.description + " ?description . " + "FILTER (regex(?description, \"" + term + "\", \"i\"))" + "}";
    List<IRI> recipeIDs = listRecipeIDs();
    Graph[] tripleCollections = new Graph[recipeIDs.size()];
    for (int i = 0; i < tripleCollections.length; i++) {
        tripleCollections[i] = tcManager.getGraph(recipeIDs.get(i));
    }
    UnionGraph unionGraph = new UnionGraph(tripleCollections);
    RuleList matchingRules = new RuleList();
    try {
        SelectQuery query = (SelectQuery) QueryParser.getInstance().parse(sparql);
        ResultSet resultSet = tcManager.executeSparqlQuery(query, unionGraph);
        while (resultSet.hasNext()) {
            SolutionMapping solutionMapping = resultSet.next();
            IRI recipeID = (IRI) solutionMapping.get("recipe");
            IRI ruleID = (IRI) solutionMapping.get("rule");
            Literal description = (Literal) solutionMapping.get("description");
            try {
                Recipe recipe = getRecipe(recipeID);
                Rule rule = new RecipeRule(recipe, getRule(recipe, ruleID));
                if (description != null) {
                    rule.setDescription(description.getLexicalForm());
                }
                matchingRules.add(rule);
            } catch (NoSuchRecipeException e) {
            // in this case go on in the iteration by fetching other matching recipes
            } catch (RecipeConstructionException e) {
            // in this case go on in the iteration by fetching other matching recipes
            } catch (NoSuchRuleInRecipeException e) {
            // in this case go on in the iteration by fetching other matching recipes
            }
        }
    } catch (ParseException e) {
        log.error("The sparql query contains errors: ", e);
    }
    return matchingRules;
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) RuleList(org.apache.stanbol.rules.base.api.util.RuleList) SolutionMapping(org.apache.clerezza.rdf.core.sparql.SolutionMapping) Recipe(org.apache.stanbol.rules.base.api.Recipe) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) SelectQuery(org.apache.clerezza.rdf.core.sparql.query.SelectQuery) UnionGraph(org.apache.clerezza.rdf.utils.UnionGraph) Graph(org.apache.clerezza.commons.rdf.Graph) UnionGraph(org.apache.clerezza.rdf.utils.UnionGraph) NoSuchRuleInRecipeException(org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException) Literal(org.apache.clerezza.commons.rdf.Literal) ResultSet(org.apache.clerezza.rdf.core.sparql.ResultSet) Rule(org.apache.stanbol.rules.base.api.Rule) ParseException(org.apache.clerezza.rdf.core.sparql.ParseException)

Example 8 with ParseException

use of org.apache.clerezza.rdf.core.sparql.ParseException in project stanbol by apache.

the class ClerezzaRuleStore method getRecipe.

@Override
public Recipe getRecipe(IRI recipeID) throws NoSuchRecipeException, RecipeConstructionException {
    log.info("Called get recipe for id: " + recipeID);
    Graph recipeGraph = null;
    /**
         * Throw a NoSuchRecipeException in case of the TcManager throws a NoSuchEntityException with respect
         * to IRI representing the recipe.
         */
    try {
        recipeGraph = tcManager.getGraph(recipeID);
    } catch (NoSuchEntityException e) {
        throw new NoSuchRecipeException(recipeID.toString());
    }
    Iterator<Triple> descriptions = recipeGraph.filter(null, Symbols.description, null);
    String recipeDescription = null;
    if (descriptions != null && descriptions.hasNext()) {
        recipeDescription = descriptions.next().getObject().toString();
    }
    String query = "SELECT ?rule ?ruleName ?ruleBody ?ruleHead " + "WHERE { " + "	" + recipeID.toString() + " " + Symbols.hasRule.toString() + " ?rule . " + "	?rule " + Symbols.ruleName.toString() + " ?ruleName . " + "	?rule " + Symbols.ruleBody.toString() + " ?ruleBody . " + "	?rule " + Symbols.ruleHead.toString() + " ?ruleHead . " + "}";
    Query sparql;
    try {
        sparql = QueryParser.getInstance().parse(query);
        ResultSet resultSet = tcManager.executeSparqlQuery((SelectQuery) sparql, recipeGraph);
        StringBuilder stanbolRulesBuilder = new StringBuilder();
        boolean firstIteration = true;
        while (resultSet.hasNext()) {
            SolutionMapping solutionMapping = resultSet.next();
            RDFTerm nameResource = solutionMapping.get("ruleName");
            RDFTerm bodyResource = solutionMapping.get("ruleBody");
            RDFTerm headResource = solutionMapping.get("ruleHead");
            StringBuilder stanbolRuleBuilder = new StringBuilder();
            stanbolRuleBuilder.append(((Literal) nameResource).getLexicalForm());
            stanbolRuleBuilder.append("[");
            stanbolRuleBuilder.append(((Literal) bodyResource).getLexicalForm());
            stanbolRuleBuilder.append(" -> ");
            stanbolRuleBuilder.append(((Literal) headResource).getLexicalForm());
            stanbolRuleBuilder.append("]");
            if (!firstIteration) {
                stanbolRulesBuilder.append(" . ");
            } else {
                firstIteration = false;
            }
            String stanbolSyntax = stanbolRuleBuilder.toString();
            log.info("Rule content {}", stanbolSyntax);
            stanbolRulesBuilder.append(stanbolSyntax);
        }
        String stanbolSyntax = stanbolRulesBuilder.toString();
        RuleList ruleList = null;
        if (!stanbolSyntax.isEmpty()) {
            String namespace = recipeID.toString().substring(1, recipeID.toString().length() - 1) + "/";
            ruleList = RuleParserImpl.parse(namespace, stanbolSyntax).getRuleList();
        }
        return new RecipeImpl(recipeID, recipeDescription, ruleList);
    } catch (ParseException e) {
        throw new RecipeConstructionException(e);
    }
}
Also used : SolutionMapping(org.apache.clerezza.rdf.core.sparql.SolutionMapping) RuleList(org.apache.stanbol.rules.base.api.util.RuleList) Query(org.apache.clerezza.rdf.core.sparql.query.Query) SelectQuery(org.apache.clerezza.rdf.core.sparql.query.SelectQuery) NoSuchRecipeException(org.apache.stanbol.rules.base.api.NoSuchRecipeException) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) NoSuchEntityException(org.apache.clerezza.rdf.core.access.NoSuchEntityException) RecipeConstructionException(org.apache.stanbol.rules.base.api.RecipeConstructionException) Triple(org.apache.clerezza.commons.rdf.Triple) UnionGraph(org.apache.clerezza.rdf.utils.UnionGraph) Graph(org.apache.clerezza.commons.rdf.Graph) ResultSet(org.apache.clerezza.rdf.core.sparql.ResultSet) ParseException(org.apache.clerezza.rdf.core.sparql.ParseException)

Example 9 with ParseException

use of org.apache.clerezza.rdf.core.sparql.ParseException in project stanbol by apache.

the class ClerezzaAdpterTest method test.

@SuppressWarnings("unchecked")
@Test
public void test() {
    try {
        List<ConstructQuery> constructQueries = (List<ConstructQuery>) ruleAdapter.adaptTo(recipeGood, ConstructQuery.class);
        for (ConstructQuery constructQuery : constructQueries) {
            ConstructQuery cq = (ConstructQuery) QueryParser.getInstance().parse(constructQuery.toString());
            System.out.println(cq.toString());
        }
        Assert.assertTrue(true);
    } catch (UnavailableRuleObjectException e) {
        Assert.fail(e.getMessage());
    } catch (UnsupportedTypeForExportException e) {
        Assert.fail(e.getMessage());
    } catch (RuleAtomCallExeption e) {
        Assert.fail(e.getMessage());
    } catch (ParseException e) {
        Assert.fail(e.getMessage());
    }
}
Also used : UnsupportedTypeForExportException(org.apache.stanbol.rules.base.api.UnsupportedTypeForExportException) UnavailableRuleObjectException(org.apache.stanbol.rules.base.api.UnavailableRuleObjectException) List(java.util.List) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) ParseException(org.apache.clerezza.rdf.core.sparql.ParseException) ConstructQuery(org.apache.clerezza.rdf.core.sparql.query.ConstructQuery) Test(org.junit.Test)

Aggregations

ParseException (org.apache.clerezza.rdf.core.sparql.ParseException)9 SelectQuery (org.apache.clerezza.rdf.core.sparql.query.SelectQuery)7 Graph (org.apache.clerezza.commons.rdf.Graph)6 ResultSet (org.apache.clerezza.rdf.core.sparql.ResultSet)5 SolutionMapping (org.apache.clerezza.rdf.core.sparql.SolutionMapping)5 IRI (org.apache.clerezza.commons.rdf.IRI)4 UnionGraph (org.apache.clerezza.rdf.utils.UnionGraph)4 NoSuchRecipeException (org.apache.stanbol.rules.base.api.NoSuchRecipeException)4 RecipeConstructionException (org.apache.stanbol.rules.base.api.RecipeConstructionException)4 Literal (org.apache.clerezza.commons.rdf.Literal)3 Recipe (org.apache.stanbol.rules.base.api.Recipe)3 RuleList (org.apache.stanbol.rules.base.api.util.RuleList)3 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)2 Query (org.apache.clerezza.rdf.core.sparql.query.Query)2 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)2 NoSuchRuleInRecipeException (org.apache.stanbol.rules.base.api.NoSuchRuleInRecipeException)2 Rule (org.apache.stanbol.rules.base.api.Rule)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Consumes (javax.ws.rs.Consumes)1