Friday, 26 September 2014

Generate dot notation OIDs - Part 1

Now basic MIB file parsing is working, its always important to get full dotted name and oid.

In order to do this, there are 2 different approaches:
  • Add a feature within Identifier EObject that calculates the dotted name/oid and saves the value as a feature (attribute).
  • Do Model to Model Transformation (MMT).
I will opt for the first as this is rather a natural addition to the existing MIB model. Came across XCore that does provide exactly this and the functionality is called "Derived Feature".

To enable this, need to:
  • Take over the Ecore/Genmodel generation from Xtext 
  • Export this Ecore/Genmodel to Xcore
  • Finally make Xtext to refer to the Xcore instead.
Here are the actual steps:
  • Make sure Xcore is available in Eclipse



  • Before exporting Mib.genmodel to Xcore, make necessary changes for generation directories/id. Because once exported to Xcore, all the files are auto-generated - its messy work to clean up later.
  • Export Mib.genmodel to Xcore. Specify the location to "platform:/resource/com.ravi.mib.xtext/model/xcore/". Noticed it always fail on the first attempt and successful on the second.



  • Import Mib model instead of generating it:

Mib.xtext
//generate mib "http://www.ravi.com/mib/xtext/Mib"
import "http://www.ravi.com/mib/xtext/Mib"

  • Modify MWE to stop generating ECore and re-point the Xcore:

GenerateMib.mwe2
    component = Generator {
        pathRtProject = runtimeProject
        pathUiProject = "${runtimeProject}.ui"
        pathTestProject = "${runtimeProject}.tests"
        projectNameRt = projectName
        projectNameUi = "${projectName}.ui"
        encoding = encoding
        language = auto-inject {
            // switch to xcore
            loadedResource = "platform:/resource/org.eclipse.emf.ecore.xcore.lib/model/XcoreLang.xcore"
            loadedResource = "classpath:/model/Xbase.ecore"
            loadedResource = "classpath:/model/Xbase.genmodel"
            loadedResource = "classpath:/model/Ecore.ecore"
            loadedResource = "classpath:/model/Ecore.genmodel"
            loadedResource = "platform:/resource/${projectName}/model/xcore/Mib.xcore"
            uri = grammarURI
    
            // Java API to access grammar elements (required by several other fragments)
            fragment = grammarAccess.GrammarAccessFragment auto-inject {}
    
            // generates Java API for the generated EPackages
            // switch to xcore
            // fragment = ecore.EMFGeneratorFragment auto-inject {}
    

  • Add Xcore plugin to MANIFEST.MF:

MANIFEST.MF
Require-Bundle: org.eclipse.xtext;visibility:=reexport,
 ...
 org.eclipse.xtext.xbase.lib,
 org.eclipse.emf.ecore.xcore
Import-Package: org.apache.log4j,

  • Now, running MWE should not cause any error (besides maybe need to manual trigger Xcore code generation - by making the Xcore file dirty e.g enter space and save)
TODO: How to add Xcore generation in MWE?

Now add following derive feature to Xcore to generate dotted name (under Identifier) and dotted oid (under OidValue).

Mib.xcore
class Identifier {
 String name
 contains OidValue value
 derived String dottedName get {
  var p = value.parent
  var dname = name
  while (p != null) {
   dname = p.name + "." + dname
   p = p.value.parent
  }
  dname
 }
}

...
class OidValue {
 refers Identifier parent
 int oidnum
 derived String dottedOid get {
  var p = parent
  var doid = oidnum.toString
  while (p != null) {
   doid = p.value.oidnum + "." + doid
   p = p.value.parent
  }
  doid
 }
}
Now, dotted name and oid should be available in the MIB model (open this via Mib Model Editor instead of Xtext Mib editor).





No comments:

Post a Comment