Import Scala library "Apache NLPCraft"

Hi, everyone!

I found a great library written in Scala - Apache NLPCraft. I want to import it into a new binding. Is this even possible?

I’m created new binding skeleton, added dependency’s to pom.xml:

<dependency>
      <groupId>org.apache.nlpcraft</groupId>
      <artifactId>nlpcraft</artifactId>
      <version>1.0.0</version>
      <scope>compile</scope>
</dependency>
<dependency>
      <groupId>org.scala-lang</groupId>
      <artifactId>scala-library</artifactId>
      <version>2.13.12</version>
      <scope>compile</scope>
</dependency>

Library downloaded. And new java file:

package org.openhab.binding.nlpcraft.internal;

import org.apache.nlpcraft.*;
import org.apache.nlpcraft.annotations.*;

class NLPCraft extends NCModel {
  
}

And VSCode says:

The type NCModel cannot be the superclass of lightswitch; a superclass must be a class

Is it because I can’t extend a Scala class in Java? Or is it a problem with the IDE? If it’s not possible to mix Scala and Java, can I write whole the binding in Scala? If yes, how to do it?

Thanks in advance!

I don’t know scala to well, but from what I remember they have some bytecode level changes to support all weird compilation options they have. I am not quite sure about runtime binding these days, but in the past it was possible to call scala from java and vice versa: How to Call Scala From Java: Using Scala Classes — muhuk's blog. You have to look closer at classes created by nlpcraft to see how they are named etc. in order to properly interact with it outside of scala.

For the compile error - if you compile java you need to follow java constraints. It could also be that you miss a compile step within VS Code which should resolve the issue.

Thanks @splatch.

Your link goes to 404 page, but I found something similar INTERACTING WITH JAVA.

But if I import a compiled Scala JAR library, will it matter what the classes were named after compilation?

Honestly, I didn’t try to compile that, just following IDE error. I’ll try tonight. Thanks.

In order to call NLPCraft you need to load valid class, so knowing valid class name is relevant. If you can compile your addon code with NLPcraft and your custom model then it should be mostly fine. IDE tooling can fail due to multiple reasons (i.e mapping of build lifecycle such as code generation etc.), and scala might be one of them. :wink:

Anyhow, thumbs up for trying apache stuff!

Cheers,
Łukasz

Unfortunately, the compiler produces the same errors as VSCode. It’s probably impossible. The only way I see is to write the binding in Scala. Does anyone know if this is real?