2012/01/13

Accessible Accessors

Now, say the title out loud quickly five times in a row and I'm sure you'll qualify for House Slytherin ...

In a previous entry I sort of hinted at the fact that in order to write core tools for NetKernel, you need Java. Since NetKernel is developed in Java I stand by that statement.

However, it is not impossible to use one of the dynamic languages and if you are addicted to one of those you can (and should) develop at least some of your tools in that language.

Which brings us to the workhorses of NetKernel, accessors. Check your local documentation for the explanation, In this entry I want to focus on the Service Accessors. Those accessors are resource transformation functions or in simpler words : they compute.

The fact you'll notice quickly about accessors is this : they require a class. Is that then the end of this entry ? No, for there is an alternative :

    <accessor>
      <id>python.simpleaccessor</id>
      <verbs>SOURCE,EXISTS</verbs>
      <prototype>PythonPrototype</prototype>
      <grammar>
        <active>
          <identifier>active:simpleaccessor</identifier>
          <argument name="foo" min="1" max="1" desc="foo"/>
        </active>
      </grammar>
      <script>res:/resources/python/simpleaccessor.py</script>
      <name>Python Simple Accessor</name>
      <description>python simple accessor</description>
    </accessor>

Instead of a class you can also pass a prototype (which is a class that provides you with an instance of the required runtime).

Indeed, I use Python. So shoot me. Anyway, here is the program.

from org.netkernel.layer1.representation import IDeterminateStringRepresentation;
from org.netkernel.layer0.nkf import INKFRequestReadOnly;
from org.netkernel.layer0.nkf.impl import NKFResponseImpl;


def onSource(lFoo):
  return context.createResponseFrom(lFoo + " was SOURCED");


def onExists(lFoo):
  return context.createResponseFrom(lFoo + " does EXIST");


def onError():
  return context.createResponseFrom("Error");


aFoo         = context.source("arg:foo", IDeterminateStringRepresentation).getString();
lThisRequest = context.getThisRequest();


requesthandler={
  INKFRequestReadOnly.VERB_SOURCE: onSource,
  INKFRequestReadOnly.VERB_EXISTS: onExists}
response = requesthandler.get(lThisRequest.getVerb(), onError)(aFoo);

If you've looked at accessors implemented in Java, you'll notice that those always implement a class that extends the StandardAccessorImpl class. Now, Python does have classes and at first I tried that (and it didn't work), until I realised that I was too late. The prototype is the class, once in the script you are already inside the accessor.

To show what my simple accessor can do (not a lot), here are two screenshots from executions in the request resolution trace tool (click to enlarge).




This entry is maybe (slightly) above the level of my intended audience. My main point is that your investment in a scripting language is not wasted. You'll automatically drift to Java as your tools become more and more core, but scripting languages definitely do the job.