OWL-API Intersection of more concepts - owl-api

I'm new at OWL-API.
I need to represent an intersection of N concepts
So, intersectionOf (C1, C2, ..... CN).
IntersectionOf has two arguments, but how can I do the general purpose solution?
Is it good enough to build an HashSet and then put it into the arguments?

OWLDataFactory has methods for obtaining an intersection that accept collections - usually sets- of class expressions. I believe that's what you're after.

Just like Ignazio said, but with code:
Java Code:
OWLDataFactory factory = manager.getOWLDataFactory();
Set<OWLClassExpression> mySet = new HashSet<OWLClassExpression>();
factory.getOWLObjectIntersectionOf(mySet);
HTH

Related

How to find which element failed comparison between arrays in Kotlin?

I'm writing automated tests for one site. There's a page with all items added to the cart. Maximum items is 58. Instead of verification of each element one by one I decided to create 2 arrays filled with strings: 1 with correct names : String and 1 with names : String I got from the site. Then I compare those 2 arrays with contentEquals.
If that comparison fails, how do I know which element exactly caused comparison fail?
Short simple of what I have now:
#Test
fun verifyNamesOfAddedItems () {
val getAllElementsNames = arrayOf(materials.text, element2.text,
element3.text...)
val correctElementsNames = arrayOf("name1", "name2", "name3"...)
val areArraysEqual = getAllElementsNames contentEquals correctElementsNames
if (!areArraysEqual) {
assert(false)
} else {
assert(true)
}
}
This test fails if 2 arrays are not the same but it doesn't show me the details, so is there a way to see more details of fail, e.g. element that failed comparison?
Thanks.
I recommend using a matcher library like Hamcrest or AssertJ in tests. They provide much better error messages for cases like this. In this case with Hamcrest it would be:
import org.hamcrest.Matchers.*
assertThat(getAllElementsNames, contains(*correctElementsNames))
// or just
assertThat(getAllElementsNames, contains("name1", "name2", "name3", ...))
There are also matcher libraries made specifically for Kotlin: https://github.com/kotlintest/kotlintest, https://yobriefca.se/expect.kt/, https://github.com/winterbe/expekt, https://github.com/MarkusAmshove/Kluent, probably more. Tests using them should be even more readable, but I haven't tried any of them. Look at their documentation and examples and pick the one you like.
You need to find the intersection between the two collections. Intersection will be the common elements. After than removing the intersection collection from the collection you want to perform the test will give you the complementary elements.
val intersection = getAllElementsNames.intersect(correctElementsNames)
getAllElementsNames.removeAll(intersection)

How to use Collections.binarySearch() in a CodenameOne project

I am used to being able to perform a binary search of a sorted list of, say, Strings or Integers, with code along the lines of:
Vector<String> vstr = new Vector<String>();
// etc...
int index = Collections.binarySearch (vstr, "abcd");
I'm not clear on how codenameone handles standard java methods and classes, but it looks like this could be fixed easily if classes like Integer and String (or the codenameone versions of these) implemented the Comparable interface.
Edit: I now see that code along the lines of the following will do the job.
int index = Collections.binarySearch(vstr, "abcd", new Comparator<String>() {
#Override
public int compare(String object1, String object2) {
return object1.compareTo(object2);
}
});
Adding the Comparable interface (to the various primitive "wrappers") would also would also make it easier to use Collections.sort (another very useful method :-))
You can also sort with a comparator but I agree, this is one of the important enhancements we need to provide in the native VM's on the various platforms personally this is my biggest peeve in our current VM.
Can you file an RFE on that and mention it as a comment in the Number issue?
If we are doing that change might as well do both.

EasyMock - expect anyObject except some

Let's say I have a method like this:
foo (A a, B b)
I want to set expectation so that anyObject is expected except some, like the imaginary code below:
expect(mockedObject).foo(anyObject(A.class), anyObject(B.class)).andReturn(something).anyTimes();
expect(mockedObject).foo(new A("1"), new B("1")).andReturn(something).times(0);
expect(mockedObject).foo(new A("2"), new B("2")).andReturn(something).times(0);
expect(mockedObject).foo(new A("3"), new B("3")).andReturn(something).times(0);
However there is no times(0) in EasyMock. I can create a chain of EasyMock.or() and EasyMock.not() but it's going to be dirty when there are a lot of unexpected objects.
On Mockito I can easily specify
verify(mockedObject, never()).foo(new A("1"), new B("1"));
What's the easiest way on EasyMock to do the same thing as above?
It is something missing indeed.
Right now, the easier is to capture() all your parameters and then check that no invalid permutation was used.

one to many relationship in Objectify4

I am working with GAE with java. i am just creating a sample application with Student and Course relationships. I am having many branches and many students. Each branch can have many students, i tried like
ObjectifyService.register(Course.class);
ObjectifyService.register(Student.class);
Course course = new Course();
course.setName("ss");
course.setBranch("cs");
ofy().save().entity(course).now();
Student stu = new Student();
stu.setName("student1");
Key<Course> courseKey = new Key<Course>(Course.class, course.getId()); // getting error here
stu.setCourse(courseKey);
System.out.println("saving");
ofy().save().entity(stu).now();
I am not sure how to define this relationship in objecitfy4. I followed the tutorial http://www.eteration.com/objectify-an-easy-way-to-use-google-datastore/
Thanks.
Look at the Javadocs for Key. Instead of a public constructor, there is a more convenient creator method which requires less typing of <> generics:
Key<Course> courseKey = Key.create(Course.class, course.getId());
There are two keys that you might use:
first the GAE low-level com.google.appengine.api.datastore.Key and
second, the objectify's com.googlecode.objectify.Key.
You can use both with Objectify (as under the hood they are ultimately converted to low-level API).
Neither has a public constructor so you can not use new with them.
With low-level keys you'd use KeyFactory.createKey("Course", course.getId()).
With objectify key you'd use com.googlecode.objectify.Key.create(Course.class, course.getId())
When we use Objectify , if we need any Key to be created for condition(KeyFactory.createKey("Course", course.getId()) , In course object, Id field should be specified with index. It will work fine.

How to check if element in groovy array/hash/collection/list?

How do I figure out if an array contains an element?
I thought there might be something like [1, 2, 3].includes(1) which would evaluate as true.
Some syntax sugar
1 in [1,2,3]
.contains() is the best method for lists, but for maps you will need to use .containsKey() or .containsValue()
[a:1,b:2,c:3].containsValue(3)
[a:1,b:2,c:3].containsKey('a')
For lists, use contains:
[1,2,3].contains(1) == true
If you really want your includes method on an ArrayList, just add it:
ArrayList.metaClass.includes = { i -> i in delegate }
You can use Membership operator:
def list = ['Grace','Rob','Emmy']
assert ('Emmy' in list)
Membership operator Groovy
IMPORTANT Gotcha for using .contains() on a Collection of Objects, such as Domains. If the Domain declaration contains a EqualsAndHashCode, or some other equals() implementation to determine if those Ojbects are equal, and you've set it like this...
import groovy.transform.EqualsAndHashCode
#EqualsAndHashCode(includes = "settingNameId, value")
then the .contains(myObjectToCompareTo) will evaluate the data in myObjectToCompareTo with the data for each Object instance in the Collection. So, if your equals method isn't up to snuff, as mine was not, you might see unexpected results.
def fruitBag = ["orange","banana","coconut"]
def fruit = fruitBag.collect{item -> item.contains('n')}
I did it like this so it works if someone is looking for it.
You can also use matches with regular expression like this:
boolean bool = List.matches("(?i).*SOME STRING HERE.*")

Resources