Get enumerated values with a reasoner - owl

Assumedly I have a data property named fooType with 2 possible values {"Low", "High"}:
<DataPropertyRange>
<DataProperty IRI="#fooType"/>
<DataOneOf>
<Literal datatypeIRI="http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral">Low</Literal>
<Literal datatypeIRI="http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral">High</Literal>
</DataOneOf>
</DataPropertyRange>
How can I use owlapi and a reasoner to:
Get all ranges of data property fooType (gets "Low" and "High")
Get all fooType values of a given individual?
So far I've tried and stucked:
// 1. How to get "Low" and "High" strings in the next step?
OWLDataProperty dataProperty = ...
Set<OWLDataPropertyRangeAxiom> dataPropertyRangeAxioms = ontology.getDataPropertyRangeAxioms(dataProperty);
// 2. How to get fooType's values in the next step?
OWLIndividual individual = ...
Set<OWLLiteral> literals = reasoner.getDataPropertyValues(individual, dataProperty);

A reasoner is not necessary to list all enumerated values - for example, it will not list values that are not used but allowed to be used in the enumeration.
To access all ranges and their components:
OWLOntology o = ...
OWLDataProperty p = ...
o.dataPropertyRangeAxioms(p)
.map(OWLDataPropertyRangeAxiom::getRange)
.forEach((OWLDataRange range) ->
// this is where you can visit all ranges
// using an OWLDataRangeVisitor
)
);

Related

Return object after performing intersection of two arrays based on attribute

I have two arrays, both filled with objects that have numerous attributes. Both arrays are holding the same object types. I want to find where objects match based on their attribute id
Object example:
#<Link:0x00007fac5eb6afc8 #id = 2002001, #length=40, #area='mars' ...>
Example arrays filled with objects:
array_links = [<Link:0x00007fac5eb6afc8>, <Link:0x00007fdf5eb7afc2>, <Link:0x000081dag6zb7agg8>... ]
selected_links = [<Link:0x00007fad8ob6gbh5>, <Link:0x00007fdg7hh4tif4>, <Link:0x000081dag7ij5bhh9>... ]
If these were strings of the object IDs and there was a match, I could use:
intersection = array_links & selected_links
However I want to do this based on their attribute and return a matching object itself.
Something like:
intersection = array_links.select(&:id) & selected_links.select(&:id)
But of course, not that, as that doesn't work, any ideas? :)
you can:
1 :
override the eql?(other) method then the array intersection will work
class Link < ApplicationRecord
def eql?(other)
self.class == other.class && self.id == other&.id # classes comparing class is a guard here
end
# you should always update the hash if you are overriding the eql?() https://stackoverflow.com/a/54961965/5872935
def hash
self.id.hash
end
end
2:
use array.select:
array_links.flat_map {|i| selected_links.select {|k| k.user_id == i.user_id }}
If they are the same object in memory, ie array_links = [<Link:0x123] and selected_links = [<Link:0x123>], then your solution of:
intersection = array_links & selected_links
Should work.
If they are not, you could loop over you array_links and select those which are in selected_links:
intersection = array_links.select do |link|
link.id.in? selected_links.map(&:id)
end
The result will be the same if you loop over selected_links and select those in array_links.
Depending on your resources and the size of these arrays, you could memoize selected_links.map(&:id) to prevent this from being re-built on each iteration.

Ordering when using scala.collection.Searching

I have an Array of [Long, Q] and would like to make a binary search on it. I tried below :
import scala.collection.Searching._
class Q (val b:Double, val a:Double)
val myArray = Array(5L -> new Q(1,2), 6L-> new Q(6,9), 7L-> new Q(7,6))
val i = myArray.search(6L).insertionPoint
but had this error
No implicit Ordering defined for Any
Unspecified value parameter ord.
I understand that I need to specify an odering rule for this collection Array[(Long,Q)] but can't figure this out myself.
Please help
Signature of search is search[B >: A](elem: B)(implicit ord: Ordering[B]). You've got an array of type [Long, Q]. So in order for the compiler to infer Ordering correctly, you'd have to invoke search like that:
myArray.search(6L-> q/*Q(6,9)*/)(Ordering.by(_._1)) //ordering by the first value in a tuple.
and what you're doing is: myArray.search(6L). If I understand correctly what you're trying to do, it's probably to find both value and position in the array.
You could solve it by using two separate data structures:
keys could be stored in the array, like this:
val myArray = Array(5L, 6L, 7L).toList
myArray.search(6L).insertionPoint
and if you'd need values, you could use map which would work as a dictionary:
val dictionary = Map(
5L -> new Q(1,2),
6L-> new Q(6,9),
7L-> new Q(7,6)
)
EDIT:
Actually, I noticed something like that would work:
val dummy = new Q(0,0) //any instance of Q
myArray.search(6L-> dummy)(Ordering.by(_._1)).insertionPoint //1
It works since for lookup of the insertion point Ordering is used and no equality test is performed.

change key's value across multiple objects at once in JS

I have multiple objects that all have the same keys lets say each object has: name and position. The first object will start with position=0. The second object would have position=1. The third object would have position=2 and so on until we get to the 10th object that would have position=9.
I need a way to subtract 1 from every objects position (with only possible values being 0-9 so that 0-1=9)
Looking for a solution that handles all of them mathematically at once, not just re-writing out new values to assign to each key individually.
Suppose you have an array of JavaScript objects, you could use map:
var newObjs = objects.map(function (object) {
object.position = (object.position === 9 ? 0 : object.position--);
return object;
});
A better approach would be:
objects.forEach( function (object) {
object.position--;
object.position = object.position < 0 ? 9 : object.position;
});

Dapper Results(Dapper Row) with Bracket Notation

According to the Dapper documentation, you can get a dynamic list back from dapper using below code :
var rows = connection.Query("select 1 A, 2 B union all select 3, 4");
((int)rows[0].A)
.IsEqualTo(1);
((int)rows[0].B)
.IsEqualTo(2);
((int)rows[1].A)
.IsEqualTo(3);
((int)rows[1].B)
.IsEqualTo(4);
What is however the use of dynamic if you have to know the field names and datatypes of the fields.
If I have :
var result = Db.Query("Select * from Data.Tables");
I want to be able to do the following :
Get a list of the field names and data types returned.
Iterate over it using the field names and get back data in the following ways :
result.Fields
["Id", "Description"]
result[0].values
[1, "This is the description"]
This would allow me to get
result[0].["Id"].Value
which will give results 1 and be of type e.g. Int 32
result[0].["Id"].Type --- what datattype is the value returned
result[0].["Description"]
which will give results "This is the description" and will be of type string.
I see there is a results[0].table which has a dapperrow object with an array of the fieldnames and there is also a result.values which is an object[2] with the values in it, but it can not be accessed. If I add a watch to the drilled down column name, I can get the id. The automatically created watch is :
(new System.Collections.Generic.Mscorlib_CollectionDebugView<Dapper.SqlMapper.DapperRow>(result as System.Collections.Generic.List<Dapper.SqlMapper.DapperRow>)).Items[0].table.FieldNames[0] "Id" string
So I should be able to get result[0].Items[0].table.FieldNames[0] and get "Id" back.
You can cast each row to an IDictionary<string, object>, which should provide access to the names and the values. We don't explicitly track the types currently - we simply don't have a need to. If that isn't enough, consider using the dapper method that returns an IDataReader - this will provide access to the raw data, while still allowing convenient call / parameterization syntax.
For example:
var rows = ...
foreach(IDictionary<string, object> row in rows) {
Console.WriteLine("row:");
foreach(var pair in row) {
Console.WriteLine(" {0} = {1}", pair.Key, pair.Value);
}
}

Apex: Sorting a Map

I have a Map of type Sobject as a key and Integer as a value. The Sobject has field of type Text, other field of type date and another field of type Number(16,2). When I put data into the map and then debug it, the map returns data in a sorted way. The map sorts it by the Number field present in the Key i.e the number data field of the object. Can I get the map sorted by the date field of the object which is its key? Below is the rough structure of my Map and the key object fields.
Map<Effort_Allocation__c, Double> cellContent;
Effort_Allocation__c.Allocated_Effort_Hours__c; //The Number field by which the map gets sorted
Effort_Allocation__c.Assignment_Date__c; // The date field by which I want the map to get sorted
It's a bad idea to use an object as a key for a map. You should, instead, use the object's ID as the key. The reasons for this are discussed in detail here. Short version - the object's values may change which changes the object's hash value and wrecks your map.
Although maps cannot be sorted directly, lists can be sorted, so they can be used to access map elements in sorted order. You'll need a wrapper class for your object that implements the "Comparable" interface. There's an example of that here. Note that the example sorts by date.
The class is declared with "comparable"
global class AccountHistoryWrapper implements Comparable{
and has the following CompareTo method
global Integer compareTo(Object compareTo) {
// Cast argument to AccountHistoryWrapper
AccountHistoryWrapper aHW = (AccountHistoryWrapper)compareTo;
// The return value of 0 indicates that both elements are equal.
Integer returnValue = 0;
if ( aHW.account.CreatedDate > aHW.account.CreatedDate) {
// Set return value to a positive value.
returnValue = 1;
} else if ( aHW.account.CreatedDate < aHW.account.CreatedDate) {
// Set return value to a negative value.
returnValue = -1;
}
return returnValue;
}

Resources