Search in sources :

Example 1 with NoSuchEntityException

use of org.apache.clerezza.rdf.core.access.NoSuchEntityException in project stanbol by apache.

the class ReasoningServiceExecutor method save.

/**
     * To save data in the triple store.
     * 
     * @param data
     * @param targetGraphID
     * @throws IOException
     */
protected void save(Object data, String targetGraphID) throws IOException {
    log.debug("Attempt saving in target graph {}", targetGraphID);
    final long startSave = System.currentTimeMillis();
    Graph mGraph;
    IRI graphIRI = new IRI(targetGraphID);
    // tcManager must be synchronized
    synchronized (tcManager) {
        try {
            // Check whether the graph already exists
            mGraph = tcManager.getGraph(graphIRI);
        } catch (NoSuchEntityException e) {
            mGraph = tcManager.createGraph(graphIRI);
        }
    }
    // We lock the graph before proceed
    Lock writeLock = mGraph.getLock().writeLock();
    boolean saved = false;
    if (data instanceof Model) {
        Graph m = JenaToClerezzaConverter.jenaModelToClerezzaGraph((Model) data);
        writeLock.lock();
        saved = mGraph.addAll(m);
        writeLock.unlock();
    } else if (data instanceof OWLOntology) {
        Graph m = (Graph) OWLAPIToClerezzaConverter.owlOntologyToClerezzaGraph((OWLOntology) data);
        writeLock.lock();
        saved = mGraph.addAll(m);
        writeLock.unlock();
    }
    if (!saved)
        throw new IOException("Cannot save the result in clerezza!");
    final long endSave = System.currentTimeMillis();
    log.debug("Saved in time: {}ms", (endSave - startSave));
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Graph(org.apache.clerezza.commons.rdf.Graph) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Model(com.hp.hpl.jena.rdf.model.Model) IOException(java.io.IOException) NoSuchEntityException(org.apache.clerezza.rdf.core.access.NoSuchEntityException) Lock(java.util.concurrent.locks.Lock)

Example 2 with NoSuchEntityException

use of org.apache.clerezza.rdf.core.access.NoSuchEntityException in project stanbol by apache.

the class ClerezzaRuleStore method removeRecipe.

@Override
public boolean removeRecipe(IRI recipeID) throws RecipeEliminationException {
    // remove the recipe from the TcManager
    try {
        tcManager.deleteGraph(recipeID);
    } catch (NoSuchEntityException e) {
        throw new RecipeEliminationException(e);
    }
    Graph recipeIndexGraph = tcManager.getGraph(new IRI(recipeIndexLocation));
    Triple triple = new TripleImpl(recipeID, RDF.type, Symbols.Recipe);
    recipeIndexGraph.remove(triple);
    // System.out.println("Recipes: " +recipes.size());
    // remove the recipe ID from in-memory list
    recipes.remove(recipeID);
    return true;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) RecipeEliminationException(org.apache.stanbol.rules.base.api.RecipeEliminationException) UnionGraph(org.apache.clerezza.rdf.utils.UnionGraph) Graph(org.apache.clerezza.commons.rdf.Graph) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) NoSuchEntityException(org.apache.clerezza.rdf.core.access.NoSuchEntityException)

Example 3 with NoSuchEntityException

use of org.apache.clerezza.rdf.core.access.NoSuchEntityException in project stanbol by apache.

the class ClerezzaYard method activate.

/**
     * Internally used to activate the Yard. In case the Yard runs within a
     * OSGI container it is called by the {@link #activate(ComponentContext)}
     * Method. In case the Yard runs outside of an OSGI Container it is called
     * by the Constructor taking the {@link YardConfig} as parameter
     * @param config The configuration for the new Yard instance
     * @throws IllegalArgumentException In case <code>null</code> is parsed as 
     * configuration or the configuration is invalid
     */
private final void activate(ClerezzaYardConfig config) throws IllegalArgumentException {
    super.activate(RdfValueFactory.getInstance(), SparqlFieldQueryFactory.getInstance(), config);
    if (tcManager == null) {
        //this will be the case if we are not in an OSGI environment
        //use the getInstance() method!
        tcManager = TcManager.getInstance();
    }
    this.yardGraphUri = config.getGraphUri();
    if (this.yardGraphUri == null) {
        // use default
        String yardUri = getUriPrefix();
        //remove the "." at the last position of the prefix
        this.yardGraphUri = new IRI(yardUri.substring(0, yardUri.length() - 2));
    }
    try {
        try {
            this.graph = tcManager.getImmutableGraph(yardGraphUri);
            immutable = true;
        } catch (NoSuchEntityException e) {
            this.graph = tcManager.getGraph(yardGraphUri);
            immutable = false;
        }
        log.info("  ... (re)use existing Graph {} for Yard {}", yardGraphUri, config.getName());
    } catch (NoSuchEntityException e) {
        log.info("   ... create new Graph {} for Yard {}", yardGraphUri, config.getName());
        this.graph = tcManager.createGraph(yardGraphUri);
    }
    if (context != null) {
        //within an OSGI environment
        //Register the graph with the Stanbol SPARQL endpoint (STANBOL-677)
        Dictionary<String, Object> graphRegProp = new Hashtable<String, Object>();
        graphRegProp.put("graph.uri", yardGraphUri.getUnicodeString());
        graphRegProp.put(Constants.SERVICE_RANKING, new Integer(-100));
        graphRegProp.put("graph.name", getConfig().getName());
        if (getConfig().getDescription() != null) {
            graphRegProp.put("graph.description", getConfig().getDescription());
        }
        graphRegistration = context.getBundleContext().registerService(Graph.class.getName(), graph, graphRegProp);
    }
//else do not register when running outside OSGI
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) Hashtable(java.util.Hashtable) NoSuchEntityException(org.apache.clerezza.rdf.core.access.NoSuchEntityException)

Example 4 with NoSuchEntityException

use of org.apache.clerezza.rdf.core.access.NoSuchEntityException in project stanbol by apache.

the class ClerezzaRuleStore method activate.

/**
     * Should be called within both OSGi and non-OSGi environments.
     * 
     * @param configuration
     * @throws IOException
     */
protected void activate(Dictionary<String, Object> configuration) throws IOException {
    if (recipeIndexLocation == null || recipeIndexLocation.trim().isEmpty()) {
        String value = (String) configuration.get(RECIPE_INDEX_LOCATION);
        if (value != null && !value.trim().isEmpty())
            recipeIndexLocation = value;
        else
            recipeIndexLocation = _RECIPE_INDEX_LOCATION_DEFAULT;
    }
    recipes = new ArrayList<IRI>();
    Graph tripleCollection = null;
    try {
        tripleCollection = tcManager.getGraph(new IRI(recipeIndexLocation));
    } catch (NoSuchEntityException e) {
        tripleCollection = tcManager.createGraph(new IRI(recipeIndexLocation));
    }
    for (Triple triple : tripleCollection) {
        IRI recipeID = (IRI) triple.getSubject();
        recipes.add(recipeID);
    }
    log.info("Rule Store activated. It contains " + recipes.size() + " recipes.", this);
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) UnionGraph(org.apache.clerezza.rdf.utils.UnionGraph) Graph(org.apache.clerezza.commons.rdf.Graph) NoSuchEntityException(org.apache.clerezza.rdf.core.access.NoSuchEntityException)

Example 5 with NoSuchEntityException

use of org.apache.clerezza.rdf.core.access.NoSuchEntityException 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)

Aggregations

NoSuchEntityException (org.apache.clerezza.rdf.core.access.NoSuchEntityException)5 Graph (org.apache.clerezza.commons.rdf.Graph)4 IRI (org.apache.clerezza.commons.rdf.IRI)4 Triple (org.apache.clerezza.commons.rdf.Triple)3 UnionGraph (org.apache.clerezza.rdf.utils.UnionGraph)3 Model (com.hp.hpl.jena.rdf.model.Model)1 IOException (java.io.IOException)1 Hashtable (java.util.Hashtable)1 Lock (java.util.concurrent.locks.Lock)1 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)1 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)1 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)1 ParseException (org.apache.clerezza.rdf.core.sparql.ParseException)1 ResultSet (org.apache.clerezza.rdf.core.sparql.ResultSet)1 SolutionMapping (org.apache.clerezza.rdf.core.sparql.SolutionMapping)1 Query (org.apache.clerezza.rdf.core.sparql.query.Query)1 SelectQuery (org.apache.clerezza.rdf.core.sparql.query.SelectQuery)1 NoSuchRecipeException (org.apache.stanbol.rules.base.api.NoSuchRecipeException)1 RecipeConstructionException (org.apache.stanbol.rules.base.api.RecipeConstructionException)1 RecipeEliminationException (org.apache.stanbol.rules.base.api.RecipeEliminationException)1