CQEngine with Scala classes

143 views
Skip to first unread message

bugfoot

unread,
Apr 30, 2017, 2:56:33 PM4/30/17
to cqengine-discuss
Trying to use QEngine with Scala classes, and I run into a bit of problem when adding indices to a collection.

Let's say my Scala class with the necessary attributes defined in a companion object is:

class TestClass {
private var _x1: Int = 0
private var _x2: Int = 1

def x1 = _x1
def x2 = _x2

def this(y1: Int, y2: Int) {
this()
this._x1 = y1
this._x2 = y2
}
}

object TestClass {
def doSomething(x: Int, y: Int): Int = x * y

final val X_1: Attribute[TestClass, Int] = new SimpleAttribute[TestClass,Int]("X_1") {
def getValue(testClass: TestClass, queryOptions: QueryOptions): Int = testClass.x1
}

final val X_2: Attribute[TestClass, Int] = new SimpleAttribute[TestClass,Int]("X_2") {
def getValue(testClass: TestClass, queryOptions: QueryOptions): Int = testClass.x2
}
}


However, when I create an IndexedCollection and try to add an index:

val testClasses: IndexedCollection[TestClass] = new ConcurrentIndexedCollection[TestClass]()
testClasses.addIndex(NavigableIndex.onAttribute(TestClass.X_1))

then Intellij IDEA says "Cannot resolve reference onAttribute with such signature", even though TestClass.X_1 is of type Attribute[TestClass, Int] (or Attribute<O, A> in Java).

How can I use CQEngine for Scala classes? Would be fantastic...

Niall

unread,
May 3, 2017, 11:08:17 AM5/3/17
to cqengine-discuss
I'm not sure what's going on there. Others have managed to use CQEngine with scala. I definitely want to support scala but I don't know enough about it to help here I'm afraid!

bugfoot

unread,
May 4, 2017, 2:00:40 AM5/4/17
to cqengine-discuss
Thanks for the feedback (and for this amazing library!). I've progressed a bit, so I'll post my findings here, maybe it'll help someone:

Instead of trying to do everything in Scala, I created the indexed collection and added indices in Java from Scala class (and companion object) TestClass:

IndexedCollection<TestClass> testClasses = new ConcurrentIndexedCollection<TestClass>();

testClasses.addIndex(NavigableIndex.onAttribute(TestClass.X_1));

here my IDE cannot resolve symbol X_1, but then changing it to

testClasses.addIndex(NavigableIndex.onAttribute(TestClass.X_1()));

gives no instance(s) of type variable(s) A exist so that Object confirms to Comparable<A>, which indicates that Scala's Int is not the same as a Java Integer (and definitely not a Java int, which might pose performance problems due to boxing / unboxing, I guess) , and CQEngine (or Java generics) treats it as an object. Fortunately, if I change the definition of X_1 attribute in TestClass companion object to:

final val X_1: Attribute[TestClass, Integer] = new SimpleAttribute[TestClass,Integer]("X_1") {
def getValue(testClass: TestClass, queryOptions: QueryOptions): Integer = testClass.x1
}

then it compiles and runs, as Scala's Integer is the same as java.lang.Integer. The same is true for Scala String, it works, but not for Scala Byte, as it is recognised as an Object and not a Java byte, and I didn't even touch enumerations and the like.
So for now, I will stick using Java classes only for CQEngine.

Niall

unread,
May 29, 2017, 4:04:10 PM5/29/17
to cqengine-discuss
Hi bugfoot,

Sorry for my delayed reply.

Do you think there could be a way to improve compatibility with Scala in CQEngine? Or can you suggest anything?

I'd like to make things easier for scala users if there are pain points. But I'm not familiar enough with scala to know where to start!

Niall
Message has been deleted
Message has been deleted

Niall

unread,
Nov 16, 2017, 7:46:44 AM11/16/17
to cqengine-discuss
Hi bugfoot,

FYI I added your tips on using CQEngine with Scala to a new wiki page: https://github.com/npgall/cqengine/blob/master/documentation/OtherJVMLanguages.md

If you have any more tips, please let me know. You can also submit a PR to improve that wiki page if you can think of anything else!

Best regards,
Niall

julien.j...@gmail.com

unread,
Nov 21, 2017, 5:09:44 PM11/21/17
to cqengine-discuss
Hi Niall,

I posted two messages in this thread on October the 6th but they got deleted ; do you know why ?

Best regards,
Julien.

Niall

unread,
Nov 21, 2017, 7:30:43 PM11/21/17
to cqengine-discuss
Hi Julien,

Sorry, it looks like they were not deleted by a human, but apparently they were (erroneously) auto-deleted by the system as spam.
I just found a snippet of them in a spam report dated 7th October (copied below), which I hadn't read.

Anyway it looks like the content of the first message is intact in the below report but the second message looks truncated.
Could you re-send your second message?

Sorry about that. I'm not sure why it happened, as the messages look fairly normal and relevant!
In the second message it looks like you were about to share a tip for using CQEngine with scala, so it would be great if you could resend that.

Thanks,
Niall


---
This message is being sent to you because you are a moderator of the group cqengine-discuss.

The following suspicious messages were sent to your group, but are being held in your moderation queue because they are classified as likely spam messages.

If you take no action, all the messages below will be discarded automatically as spam.

However, if you see any messages that are not spam below, you may approve them individually by going to:

http://groups.google.com/group/cqengine-discuss/pendmsg

Please do not mark this notification as spam; this is a service for group moderators. If you do not wish to receive these notifications in the future, you may change your preferences by going to:

http://groups.google.com/group/cqengine-discuss/manage_post


------- 1 of 2  -------
Subject: Re: CQEngine with Scala classes
From: julien.j...@gmail.com
Date: Oct 06 07:36AM -0700

Hi bugfoot and Niall

I get exactly the same problem. Have you find a way to fix it?

Julien.

Le lundi 29 mai 2017 21:04:10 UTC+1, Niall a écrit :


Approve: http://groups.google.com/group/cqengine-discuss/pendmsg?view=full&pending_id=3381649573489731390

------- 2 of 2  -------
Subject: Re: CQEngine with Scala classes
From: julien.j...@gmail.com
Date: Oct 06 08:10AM -0700

Hi bugfoot and Niall

I get exactly the same problem. Have you find a way to fix it?

I've tweaked it using Java types rather than the one from Scala ; in my 
Scala main class I've set :

type jLong = java.lang.Long
type jInt =

Approve: http://groups.google.com/group/cqengine-discuss/pendmsg?view=full&pending_id=3161603099180221428


For more information about this message, please visit:
https://support.google.com/groups/answer/2466386
---

julien.j...@gmail.com

unread,
Dec 3, 2017, 4:38:32 AM12/3/17
to cqengine-discuss
Hi,

Here is the content of the first message I posted :

___________________________________________________


Hi bugfoot and Niall

I get exactly the same problem. Have you find a way to fix it?

I've tweaked it using Java types rather than the one from Scala ; in my Scala main class I've set :

type jLong = java.lang.Long
type jInt = java.lang.Integer

and I've changed some method and attributes accordingly.

I think this problem relies on Scala types auto-boxing (see this StackOverFlow thread).

Julien.

Niall

unread,
Dec 19, 2017, 11:01:30 AM12/19/17
to cqengine-discuss
Hi Julien,

Thanks for that. I've added that tip to the wiki page:

Let me know if you have any other suggestions.

Many thanks!
Niall
Reply all
Reply to author
Forward
0 new messages