eclipseantlrxtextsysmlsysml2

"org.eclipse.emf.ecore.xmi.PackageNotFoundException" when using sysml2 xtext plugins in stand-alone mode


For a company POC software I'm trying to use the sysml2 xtext plugin & ANTLR grammar in stand-alone mode, so as to parse & tokenize sysml2 files and eventually navigate tokens and do some simple code generation. I have managed so far to build the different JARs and get some basic code started, but I get hit with the following error when instantiating the Parser:

org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri 'http://www.omg.org/spec/UML/20161101' not found. (classpath:/org/omg/sysml.xmi, 3, 100)

I suppose that the UML XMI is not available at runtime, but I have no idea how to load it... Sadly, I cannot find any UmlStandAloneSetupGenerated or similar.
Any help would be appreciated, I am not familiar with the EMF framework and I cannot find any code snippets explaining how to load the XMI and map it to the URI. (as reference, the source for the xtext plugin is at https://github.com/Systems-Modeling/SysML-v2-Pilot-Implementation/tree/master/org.omg.sysml.xtext)

package company.sysml2_tools;

import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.service.GrammarProvider;
import org.omg.sysml.xtext.SysMLStandaloneSetupGenerated;
import org.omg.sysml.xtext.parser.antlr.internal.InternalSysMLLexer;
import org.omg.sysml.xtext.parser.antlr.internal.InternalSysMLParser;
import org.omg.sysml.xtext.services.SysMLGrammarAccess;
import com.google.inject.Injector;

public class App {
    public static void main(String[] args) {

        try {
            String test = "package banana{}";
            InternalSysMLLexer lexer = new InternalSysMLLexer(new ANTLRStringStream(test));
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            SysMLStandaloneSetupGenerated standalone = new SysMLStandaloneSetupGenerated();
            Injector injector = standalone.createInjectorAndDoEMFRegistration();

            InternalSysMLParser parser = new InternalSysMLParser(tokens, new SysMLGrammarAccess(
                    new GrammarProvider("org.omg.sysml", injector.getProvider(XtextResourceSet.class)), null));

        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
    }
}    

UPDATE: For the record, the packages can indeed be registered with e.g.

        UMLPackage umlPackageClass = UMLPackage.eINSTANCE;
        TypesPackage typesPackage = TypesPackage.eINSTANCE;
        StandardPackage standardPackage = StandardPackage.eINSTANCE;
        SysMLPackage sysmlPackageClass = SysMLPackage.eINSTANCE;

Solution

  • From the EMF FAQ it sounds like you're just lacking the UML ePackage's initialization.