Author: reto
Date: Wed Feb 20 11:09:48 2013
New Revision: 1448078
URL: http://svn.apache.org/r1448078
Log:
STANBOL-946: allowing to control expansion of graph using recipes
Added:
stanbol/trunk/commons/web/rdfviewable-writer/src/main/java/org/apache/stanbol/commons/web/rdfviewable/writer/RECIPES.java
stanbol/trunk/commons/web/rdfviewable-writer/src/main/java/org/apache/stanbol/commons/web/rdfviewable/writer/RecipesGraphProvider.java
stanbol/trunk/commons/web/rdfviewable-writer/src/main/resources/
Modified:
stanbol/trunk/commons/web/rdfviewable-writer/pom.xml
stanbol/trunk/commons/web/rdfviewable-writer/src/main/java/org/apache/stanbol/commons/web/rdfviewable/writer/impl/RdfSerializingWriter.java
Modified: stanbol/trunk/commons/web/rdfviewable-writer/pom.xml
URL:
http://svn.apache.org/viewvc/stanbol/trunk/commons/web/rdfviewable-writer/pom.xml?rev=1448078&r1=1448077&r2=1448078&view=diff
==============================================================================
--- stanbol/trunk/commons/web/rdfviewable-writer/pom.xml (original)
+++ stanbol/trunk/commons/web/rdfviewable-writer/pom.xml Wed Feb 20 11:09:48
2013
@@ -94,7 +94,12 @@
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.scr.annotations</artifactId>
</dependency>
-
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.commons.indexedgraph</artifactId>
+ <version>0.10.0-SNAPSHOT</version>
+ <type>jar</type>
+ </dependency>
</dependencies>
</project>
Added:
stanbol/trunk/commons/web/rdfviewable-writer/src/main/java/org/apache/stanbol/commons/web/rdfviewable/writer/RECIPES.java
URL:
http://svn.apache.org/viewvc/stanbol/trunk/commons/web/rdfviewable-writer/src/main/java/org/apache/stanbol/commons/web/rdfviewable/writer/RECIPES.java?rev=1448078&view=auto
==============================================================================
---
stanbol/trunk/commons/web/rdfviewable-writer/src/main/java/org/apache/stanbol/commons/web/rdfviewable/writer/RECIPES.java
(added)
+++
stanbol/trunk/commons/web/rdfviewable-writer/src/main/java/org/apache/stanbol/commons/web/rdfviewable/writer/RECIPES.java
Wed Feb 20 11:09:48 2013
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2013 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.stanbol.commons.web.rdfviewable.writer;
+
+import org.apache.clerezza.rdf.core.UriRef;
+
+/**
+ * Used ontologicaal terms from recipes ontology
+ *
+ * TODO: generate this with the maven plugin in separate projects
+ */
+public class RECIPES {
+ public static final UriRef Recipe = new
UriRef("http://vocab.netlabs.org/recipe#Recipe");
+
+ public static final UriRef recipeDomain = new
UriRef("http://vocab.netlabs.org/recipe#recipeDomain");
+
+ public static final UriRef ingredient = new
UriRef("http://vocab.netlabs.org/recipe#ingredient");
+
+ public static final UriRef ingredientProperty = new
UriRef("http://vocab.netlabs.org/recipe#ingredientProperty");
+
+ public static final UriRef ingredientInverseProperty = new
UriRef("http://vocab.netlabs.org/recipe#ingredientInverseProperty");
+}
Added:
stanbol/trunk/commons/web/rdfviewable-writer/src/main/java/org/apache/stanbol/commons/web/rdfviewable/writer/RecipesGraphProvider.java
URL:
http://svn.apache.org/viewvc/stanbol/trunk/commons/web/rdfviewable-writer/src/main/java/org/apache/stanbol/commons/web/rdfviewable/writer/RecipesGraphProvider.java?rev=1448078&view=auto
==============================================================================
---
stanbol/trunk/commons/web/rdfviewable-writer/src/main/java/org/apache/stanbol/commons/web/rdfviewable/writer/RecipesGraphProvider.java
(added)
+++
stanbol/trunk/commons/web/rdfviewable-writer/src/main/java/org/apache/stanbol/commons/web/rdfviewable/writer/RecipesGraphProvider.java
Wed Feb 20 11:09:48 2013
@@ -0,0 +1,107 @@
+/*
+ * Copyright 2013 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.stanbol.commons.web.rdfviewable.writer;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Enumeration;
+import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.TripleCollection;
+import org.apache.clerezza.rdf.core.serializedform.Parser;
+import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.stanbol.commons.indexedgraph.IndexedMGraph;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component
+@Service(RecipesGraphProvider.class)
+public class RecipesGraphProvider implements BundleListener {
+
+ private static final String RECIPES_PATH_IN_BUNDLE =
"META-INF/graphs/recipes/";
+
+ @Reference
+ private Parser parser;
+
+ private static Logger log =
+ LoggerFactory.getLogger(RecipesGraphProvider.class);
+
+ private MGraph recipesGraph = null;
+
+ public TripleCollection getRecipesGraph() {
+ return recipesGraph;
+ }
+
+ @Activate
+ protected void activate(BundleContext context) {
+ recipesGraph = new IndexedMGraph();
+ context.addBundleListener(this);
+ for (Bundle b : context.getBundles()) {
+ if (b.getState() == Bundle.ACTIVE) {
+ loadRecipesData(b);
+ }
+ }
+ }
+
+ @Deactivate
+ protected void deactivate(BundleContext context) {
+ context.removeBundleListener(this);
+ recipesGraph = null;
+ }
+
+ private void loadRecipesData(Bundle b) {
+ Enumeration e = b.findEntries(RECIPES_PATH_IN_BUNDLE, "*", false);
+ if (e != null) {
+ while (e.hasMoreElements()) {
+ URL rdfResource = (URL) e.nextElement();
+ try {
+ parser.parse(recipesGraph, rdfResource.openStream(),
guessFormat(rdfResource));
+ } catch (IOException ex) {
+ log.error("Couldn't parse recipe data "+e+" in bundle"+b,
ex);
+ }
+ }
+ }
+ }
+
+ private String guessFormat(URL url) {
+ final String path = url.getPath();
+ if (path.endsWith("ttl") || path.endsWith("turtle")) {
+ return SupportedFormat.TURTLE;
+ }
+ if (path.endsWith("rdf") || path.endsWith("xml")) {
+ return SupportedFormat.RDF_XML;
+ }
+ if (path.endsWith("nt") || path.endsWith("n3")) {
+ return SupportedFormat.N3;
+ }
+ throw new RuntimeException("Don't know the mediatype of "+path);
+ }
+
+ @Override
+ public void bundleChanged(BundleEvent event) {
+ if (event.getType() == BundleEvent.STARTED) {
+ loadRecipesData(event.getBundle());
+ }
+ }
+}
\ No newline at end of file
Modified:
stanbol/trunk/commons/web/rdfviewable-writer/src/main/java/org/apache/stanbol/commons/web/rdfviewable/writer/impl/RdfSerializingWriter.java
URL:
http://svn.apache.org/viewvc/stanbol/trunk/commons/web/rdfviewable-writer/src/main/java/org/apache/stanbol/commons/web/rdfviewable/writer/impl/RdfSerializingWriter.java?rev=1448078&r1=1448077&r2=1448078&view=diff
==============================================================================
---
stanbol/trunk/commons/web/rdfviewable-writer/src/main/java/org/apache/stanbol/commons/web/rdfviewable/writer/impl/RdfSerializingWriter.java
(original)
+++
stanbol/trunk/commons/web/rdfviewable-writer/src/main/java/org/apache/stanbol/commons/web/rdfviewable/writer/impl/RdfSerializingWriter.java
Wed Feb 20 11:09:48 2013
@@ -23,6 +23,7 @@ import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.ws.rs.Produces;
@@ -33,10 +34,12 @@ import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
+import org.apache.clerezza.rdf.core.LiteralFactory;
import org.apache.clerezza.rdf.core.Resource;
import org.apache.clerezza.rdf.core.Triple;
import org.apache.clerezza.rdf.core.TripleCollection;
import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
import org.apache.clerezza.rdf.core.serializedform.Serializer;
import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
@@ -45,11 +48,14 @@ import org.apache.felix.scr.annotations.
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
+import org.apache.stanbol.commons.web.rdfviewable.writer.RECIPES;
+import org.apache.stanbol.commons.web.rdfviewable.writer.RecipesGraphProvider;
import org.apache.stanbol.commons.web.viewable.RdfViewable;
/**
* By default this returns a serialization of the context of the GraphNode. The
- * template path of the RdfViewable is ignored.
+ * template path of the RdfViewable is used to look for a recipe to control the
+ * expansion of the seriaization.
*
* Same as with
* <code>org.apache.clerezza.jaxrs.rdf.providers</code> the expansion can be
@@ -68,11 +74,16 @@ import org.apache.stanbol.commons.web.vi
SupportedFormat.X_TURTLE, SupportedFormat.RDF_JSON})
public class RdfSerializingWriter implements MessageBodyWriter<RdfViewable> {
+
public static final String OBJ_EXP_PARAM = "xPropObj";
public static final String SUBJ_EXP_PARAM = "xPropSubj";
@Reference
private Serializer serializer;
+
+ @Reference
+ private RecipesGraphProvider recipesGraphProvider;
+
private UriInfo uriInfo;
@Override
@@ -93,7 +104,13 @@ public class RdfSerializingWriter implem
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException,
WebApplicationException {
GraphNode node = v.getGraphNode();
- serializer.serialize(entityStream, getExpandedContext(node),
mediaType.toString());
+ GraphNode recipe = getRecipe(v.getTemplatePath());
+ if (recipe != null) {
+ serializer.serialize(System.out, recipe.getNodeContext(),
"text/turtle");
+ } else {
+ System.out.println("no recipe found");
+ }
+ serializer.serialize(entityStream, getExpandedContext(node, recipe),
mediaType.toString());
}
@Context
@@ -101,12 +118,12 @@ public class RdfSerializingWriter implem
this.uriInfo = uriInfo;
}
- private TripleCollection getExpandedContext(GraphNode node) {
+ private TripleCollection getExpandedContext(GraphNode node, GraphNode
recipe) {
final TripleCollection result = new
SimpleMGraph(node.getNodeContext());
final Set<Resource> expandedResources = new HashSet<Resource>();
expandedResources.add(node.getNode());
while (true) {
- Set<Resource> additionalExpansionRes =
getAdditionalExpansionResources(result);
+ Set<Resource> additionalExpansionRes =
getAdditionalExpansionResources(result, recipe);
additionalExpansionRes.removeAll(expandedResources);
if (additionalExpansionRes.size() == 0) {
return result;
@@ -119,9 +136,9 @@ public class RdfSerializingWriter implem
}
}
- private Set<Resource> getAdditionalExpansionResources(TripleCollection tc)
{
- final Set<UriRef> subjectExpansionProperties =
getSubjectExpansionProperties();
- final Set<UriRef> objectExpansionProperties =
getObjectExpansionProperties();
+ private Set<Resource> getAdditionalExpansionResources(TripleCollection tc,
GraphNode recipe) {
+ final Set<UriRef> subjectExpansionProperties =
getSubjectExpansionProperties(recipe);
+ final Set<UriRef> objectExpansionProperties =
getObjectExpansionProperties(recipe);
final Set<Resource> result = new HashSet<Resource>();
if ((subjectExpansionProperties.size() > 0)
|| (objectExpansionProperties.size() > 0)) {
@@ -138,29 +155,59 @@ public class RdfSerializingWriter implem
return result;
}
- private Set<UriRef> getSubjectExpansionProperties() {
+ private Set<UriRef> getSubjectExpansionProperties(GraphNode recipe) {
final MultivaluedMap<String, String> queryParams =
uriInfo.getQueryParameters();
final List<String> paramValues = queryParams.get(SUBJ_EXP_PARAM);
- if (paramValues == null) {
- return new HashSet<UriRef>(0);
+ final Set<UriRef> result = new HashSet<UriRef>();
+ if (paramValues != null) {
+ for (String uriString : paramValues) {
+ result.add(new UriRef(uriString));
+ }
}
- final Set<UriRef> result = new HashSet<UriRef>(paramValues.size());
- for (String uriString : paramValues) {
- result.add(new UriRef(uriString));
+ if (recipe != null) {
+ Iterator<GraphNode> ingredients =
recipe.getObjectNodes(RECIPES.ingredient);
+ while (ingredients.hasNext()) {
+ Iterator<Resource> properties =
+
ingredients.next().getObjects(RECIPES.ingredientInverseProperty);
+ while (properties.hasNext()) {
+ result.add((UriRef)properties.next());
+ }
+ }
}
return result;
}
- private Set<UriRef> getObjectExpansionProperties() {
+ private Set<UriRef> getObjectExpansionProperties(GraphNode recipe) {
final MultivaluedMap<String, String> queryParams =
uriInfo.getQueryParameters();
final List<String> paramValues = queryParams.get(OBJ_EXP_PARAM);
- if (paramValues == null) {
- return new HashSet<UriRef>(0);
+ final Set<UriRef> result = new HashSet<UriRef>();
+ if (paramValues != null) {
+ for (String uriString : paramValues) {
+ result.add(new UriRef(uriString));
+ }
}
- final Set<UriRef> result = new HashSet<UriRef>(paramValues.size());
- for (String uriString : paramValues) {
- result.add(new UriRef(uriString));
+ if (recipe != null) {
+ Iterator<GraphNode> ingredients =
recipe.getObjectNodes(RECIPES.ingredient);
+ while (ingredients.hasNext()) {
+ Iterator<Resource> properties =
+
ingredients.next().getObjects(RECIPES.ingredientProperty);
+ while (properties.hasNext()) {
+ result.add((UriRef)properties.next());
+ }
+ }
}
+
return result;
}
+
+ private GraphNode getRecipe(String templatePath) {
+ TripleCollection rg = recipesGraphProvider.getRecipesGraph();
+ GraphNode literalNode = new GraphNode(new
PlainLiteralImpl(templatePath), rg);
+ Iterator<GraphNode> recipes =
literalNode.getSubjectNodes(RECIPES.recipeDomain);
+ if (recipes.hasNext()) {
+ return recipes.next();
+ } else {
+ return null;
+ }
+ }
}