You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by rw...@apache.org on 2014/01/27 14:18:18 UTC
svn commit: r1561664 - in
/stanbol/trunk/development/archetypes/enhancement-engine: ./
src/main/resources/META-INF/maven/ src/main/resources/archetype-resources/
src/main/resources/archetype-resources/src/main/java/
src/main/resources/archetype-resourc...
Author: rwesten
Date: Mon Jan 27 13:18:18 2014
New Revision: 1561664
URL: http://svn.apache.org/r1561664
Log:
STANBOL-1270: updates to the Enhancement Engine Archetype as descriped by the issue.
Added:
stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/src/main/resources/
stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/src/main/resources/OSGI-INF/
stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/src/main/resources/OSGI-INF/metatype/
stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/src/main/resources/OSGI-INF/metatype/metatype.properties
Modified:
stanbol/trunk/development/archetypes/enhancement-engine/ (props changed)
stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/META-INF/maven/archetype-metadata.xml
stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/pom.xml
stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/src/main/java/ExampleEnhancer.java
Propchange: stanbol/trunk/development/archetypes/enhancement-engine/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Mon Jan 27 13:18:18 2014
@@ -1,3 +1,5 @@
target
.project
+
+.settings
Modified: stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/META-INF/maven/archetype-metadata.xml
URL: http://svn.apache.org/viewvc/stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/META-INF/maven/archetype-metadata.xml?rev=1561664&r1=1561663&r2=1561664&view=diff
==============================================================================
--- stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/META-INF/maven/archetype-metadata.xml (original)
+++ stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/META-INF/maven/archetype-metadata.xml Mon Jan 27 13:18:18 2014
@@ -32,6 +32,12 @@
</includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
+ <directory>src/main/resources</directory>
+ <includes>
+ <include>**/*.properties</include>
+ </includes>
+ </fileSet>
+ <fileSet filtered="true" encoding="UTF-8">
<directory></directory>
<includes>
<include>nbactions.xml</include>
Modified: stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/pom.xml
URL: http://svn.apache.org/viewvc/stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/pom.xml?rev=1561664&r1=1561663&r2=1561664&view=diff
==============================================================================
--- stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/pom.xml (original)
+++ stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/pom.xml Mon Jan 27 13:18:18 2014
@@ -1,14 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>stanbol-parent</artifactId>
+ <version>5-SNAPSHOT</version>
+ </parent>
<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<packaging>bundle</packaging>
-
+ <!-- TODO: change the name and description for your engine -->
<name>Skeleton for ${artifactId}</name>
-
+ <description>The description for the ${artifactId}</description>
+
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
@@ -44,6 +51,26 @@
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <!-- Keep engine classes private as Enhancement engines typically
+ do not export functionality. Because of that we have an empty
+ list of exported packages and are defining all packages of the
+ engine as private -->
+ <Export-Package></Export-Package>
+ <Private-Package>
+ ${package}*;version=${project.version}
+ </Private-Package>
+ <!-- those import statements allow to use the engine within Stanbol
+ version 0.11 or higher. If you just want to use your engine to
+ be used with Stanbol 1.0+ you can remove this element -->
+ <Import-Package>
+ org.apache.stanbol.enhancer.servicesapi; provide:=true; version="[0.11,1.1)",
+ org.apache.stanbol.enhancer.servicesapi.*;version="[0.11,1.1)",
+ *
+ </Import-Package>
+ </instructions>
+ </configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
Modified: stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/src/main/java/ExampleEnhancer.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/src/main/java/ExampleEnhancer.java?rev=1561664&r1=1561663&r2=1561664&view=diff
==============================================================================
--- stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/src/main/java/ExampleEnhancer.java (original)
+++ stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/src/main/java/ExampleEnhancer.java Mon Jan 27 13:18:18 2014
@@ -5,14 +5,20 @@ package ${package};
import java.io.IOException;
import java.util.Collections;
+import java.util.Dictionary;
import java.util.Map;
+import java.util.Map.Entry;
+
import org.apache.clerezza.rdf.core.MGraph;
import org.apache.clerezza.rdf.core.UriRef;
import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
import org.apache.clerezza.rdf.core.impl.TripleImpl;
import org.apache.clerezza.rdf.ontologies.DCTERMS;
import org.apache.clerezza.rdf.ontologies.RDFS;
+import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.ConfigurationPolicy;
+import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
@@ -25,15 +31,22 @@ import org.apache.stanbol.enhancer.servi
import org.apache.stanbol.enhancer.servicesapi.helper.ContentItemHelper;
import org.apache.stanbol.enhancer.servicesapi.helper.EnhancementEngineHelper;
import org.apache.stanbol.enhancer.servicesapi.impl.AbstractEnhancementEngine;
+import org.osgi.framework.Constants;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-@Component(immediate = true, metatype = true, inherit = true)
-@Service
-@Properties(value = {
- @Property(name = EnhancementEngine.PROPERTY_NAME, value = "${artifactId}-example")
+@Component(configurationFactory = true, //allow multiple service instances (false for a singelton instance)
+ policy = ConfigurationPolicy.OPTIONAL, //use REQUIRE if a non default option is present
+ immediate = true, //activate service instances on startup
+ metatype = true, inherit = true, specVersion = "1.1")
+@Service //this will register the engine as an OSGI service
+@Properties(value = { //Configuration properties included in the config form
+ @Property(name = EnhancementEngine.PROPERTY_NAME, value = "${artifactId}-example"),
+ @Property(name = Constants.SERVICE_RANKING, intValue = 0)
})
-public class ExampleEnhancer extends AbstractEnhancementEngine
+public class ExampleEnhancer extends AbstractEnhancementEngine<RuntimeException,RuntimeException>
implements EnhancementEngine, ServiceProperties {
/**
@@ -42,54 +55,108 @@ public class ExampleEnhancer extends Abs
private static final Logger log = LoggerFactory.getLogger(ExampleEnhancer.class);
/**
+ * TODO: change to fit your engine. See constants defined in the
+ * ServiceProperties class
+ */
+ protected static final Integer ENGINE_ORDERING = ServiceProperties.ORDERING_PRE_PROCESSING;
+
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ * OSGI lifecycle methods
+ * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ */
+ /**
+ * Activate and read the properties. Configures and initialises a POSTagger for each language configured in
+ * CONFIG_LANGUAGES.
+ *
+ * @param ce the {@link org.osgi.service.component.ComponentContext}
+ */
+ @Activate
+ protected void activate(ComponentContext ce) throws ConfigurationException {
+ super.activate(ce);
+ log.info("activating {}: {}", getClass().getSimpleName(), getName());
+ @SuppressWarnings("unchecked")
+ Dictionary<String, Object> properties = ce.getProperties();
+ //TODO: parse custom properties
+ }
+
+ @Deactivate
+ protected void deactivate(ComponentContext context) {
+ log.info("deactivating {}: {}", getClass().getSimpleName(), getName());
+ //TODO: reset fields to default, close resources ...
+ super.deactivate(context);
+ }
+
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ * ServiceProperties interface method
+ * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ */
+
+ /**
* ServiceProperties are currently only used for automatic ordering of the
* execution of EnhancementEngines (e.g. by the WeightedChain implementation).
* Default ordering means that the engine is called after all engines that
* use a value < {@link ServiceProperties#ORDERING_CONTENT_EXTRACTION}
* and >= {@link ServiceProperties#ORDERING_EXTRACTION_ENHANCEMENT}.
*/
- public Map getServiceProperties() {
- return Collections.unmodifiableMap(Collections.singletonMap(
- ENHANCEMENT_ENGINE_ORDERING, ORDERING_DEFAULT));
+ public Map<String,Object> getServiceProperties() {
+ return Collections.unmodifiableMap(Collections.<String,Object>singletonMap(
+ ENHANCEMENT_ENGINE_ORDERING, ENGINE_ORDERING));
}
+
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ * EnhancementEngine interface methods
+ * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ */
/**
* @return if and how (asynchronously) we can enhance a ContentItem
*/
public int canEnhance(ContentItem ci) throws EngineException {
- // check if content is present
- try {
- if ((ci.getBlob() == null)
- || (ci.getBlob().getStream().read() == -1)) {
- return CANNOT_ENHANCE;
- }
- } catch (IOException e) {
- log.error("Failed to get the text for "
- + "enhancement of content: " + ci.getUri(), e);
- throw new InvalidContentException(this, ci, e);
+ // check if a Content in the supported type is available
+ //NOTE: you can parse multiple content types
+ Entry<UriRef,Blob> textBlob = ContentItemHelper.getBlob(
+ ci, Collections.singleton("text/plain"));
+ if(textBlob == null) {
+ return CANNOT_ENHANCE;
}
+ //TODO: test additional requirements for this EnhancementEngine
+
// no reason why we should require to be executed synchronously
return ENHANCE_ASYNC;
}
public void computeEnhancements(ContentItem ci) throws EngineException {
+ //(1) retrieve the required data from the ConentItem
+ String content;
try {
//get the (generated or submitted) text version of the ContentItem
- Blob textBlob =
- ContentItemHelper.getBlob(ci,
+ Blob textBlob = ContentItemHelper.getBlob(ci,
Collections.singleton("text/plain")).getValue();
- String content = ContentItemHelper.getText(textBlob);
- // get the metadata graph
- MGraph metadata = ci.getMetadata();
- // update some sample data
+ //Blob provides an InputStream. Use the Utility to load the String
+ content = ContentItemHelper.getText(textBlob);
+ } catch (IOException ex) {
+ log.error("Exception reading content item.", ex);
+ throw new InvalidContentException("Exception reading content item.", ex);
+ }
+
+ //(2) compute the enhancements
+ int contentLength = content.length();
+
+ //(3) write the enhancement results
+ // get the metadata graph
+ MGraph metadata = ci.getMetadata();
+ //NOTE: as we allow synchronous calls we need to use read/write
+ // locks on the ContentItem
+ ci.getLock().writeLock().lock();
+ try {
+ // TODO: replace this with real enhancements
UriRef textAnnotation = EnhancementEngineHelper.createTextEnhancement(ci, this);
metadata.add(new TripleImpl(textAnnotation, DCTERMS.type,
new UriRef("http://example.org/ontology/LengthEnhancement")));
metadata.add(new TripleImpl(textAnnotation, RDFS.comment,
- new PlainLiteralImpl("A text of " + content.length() + " charaters")));
- } catch (IOException ex) {
- log.error("Exception reading content item.", ex);
- throw new InvalidContentException("Exception reading content item.", ex);
+ new PlainLiteralImpl("A text of " + contentLength + " charaters")));
+ } finally {
+ ci.getLock().writeLock().unlock();
}
}
}
Added: stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: http://svn.apache.org/viewvc/stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1561664&view=auto
==============================================================================
--- stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/src/main/resources/OSGI-INF/metatype/metatype.properties (added)
+++ stanbol/trunk/development/archetypes/enhancement-engine/src/main/resources/archetype-resources/src/main/resources/OSGI-INF/metatype/metatype.properties Mon Jan 27 13:18:18 2014
@@ -0,0 +1,41 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+
+#===============================================================================
+#Properties and Options used by all Enhancement Engines
+#===============================================================================
+
+stanbol.enhancer.engine.name.name=Name
+stanbol.enhancer.engine.name.description=The name of the enhancement engine as \
+used in the RESTful interface '/engine/<name>'
+
+service.ranking.name=Ranking
+service.ranking.description=If two enhancement engines with the same name are active the \
+one with the higher ranking will be used to process parsed content items.
+
+#===============================================================================
+# Name and description for your Engine
+#===============================================================================
+
+# TODO: change name and description
+
+${package}.ExampleEnhancer.name=Apache \
+Stanbol Enhancer Engine: ${artifactId} Example
+${package}.ExampleEnhancer.description=Enhancement \
+Engine Description generated for ${artifactId} Example.
+
+#===============================================================================
+# Special Properties and options used by this Enhancement Engine
+#===============================================================================