Skip to content

2.2 Create Processor (Java)

Nepomuk Seiler edited this page Feb 2, 2012 · 1 revision

This tutorial explains how to use the Knowing Java API to implement a generic _Processor. A Processor can be anything which processes data, e.g. classifier, clusterer, filter, and so on.

Create Project

Create an new Eclipse Plug-in Project with following options

  • No contributions to the UI
  • Activator yes

Configure Project

Right-click on the project->configure->Add Scala nature. This is neccessary because the factory and java-wrapper can only be written in scala.

Configure Dependencies

Open META-INF/MANIFEST.MF and open tab dependecies

  • Add de.lmu.ifi.dbs.knowing.core as required Plugin-in.
  • Remove _org.scala-ide.library.2.9.0
  • Add imported packages -> NONE

Optional

Eclipse automatically adds org.eclipse.core.runtime as dependency. If you want to use another OSGi runtime, remove org.eclipse.core.runtime from required bundles and add org.osgi.framework to imported packages as shown here:

Create Processor

To create your own processor your class must extend de.lmu.ifi.dbs.knowing.core.japi.AbstractProcessor. Implement the abstract methods.

If you want to answer queries you should use this snippet:

Instances query(Instance query, ActorRef ref) {
 //If you want to answer directly, what is the main intention, you have to do it like this:
 if(ref.getSender().isDefined()) { 
    ActorRef sender = ref.getSender().get(); 
    sender.sendOneWay("Answer"); //Or a message as you like	
}

To send results to other nodes:

//To every connected node
sendEvent(new Results(instances), null);
//To every connected node listening on port
sendEvent(new Results(instances), "output1");

Create Wrapper and Factory

The wrapping part is done currently in Scala. However, just a few lines of code though.

import java.util.Properties
import akka.actor.ActorRef
import akka.actor.Actor.actorOf
import de.lmu.ifi.dbs.knowing.core.factory.TFactory
import de.lmu.ifi.dbs.knowing.core.japi.JProcessor
import scala.collection.immutable.Map

class TestJavaProcessorFactory extends TFactory {

  val id = TestJavaProcessorFactory.id // id from static field
  val name = TestJavaProcessorFactory.name // name from static field

  // actorOf our Wrapper class defined below
  def getInstance(): ActorRef = actorOf[TestJavaProcessorWrapper]

  def createDefaultProperties(): Properties = new Properties
  def createPropertyValues(): Map[String, Array[_ <: Any]] = Map()
  def createPropertyDescription(): Map[String, String] = Map()
}

/**
 * static fields id and name
 */
object TestJavaProcessorFactory {
  val id = classOf[TestJavaProcessor].getName
  val name = "Test Java Processor"
}

/**
 * Wrapper class
 */
class TestJavaProcessorWrapper extends JProcessor {
  //processor field is abstract and must be implemented
  val processor = new TestJavaProcessor(this)
}

This factory must be registered as an OSGi-Service and you're done!