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));
}
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;
}
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
}
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);
}
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);
}
}
Aggregations