Listing instances next to each class in SPARQL query - format

I want to write a SPARQL query that will give me a list that has all the subclasses of a specific class and all the instances of each subclass. E.g., if I have the class Animal with subclasses Dog and Cat and instances of Dog: Fido, Bowser and Cat: Fluffy, Whiskers I want the output to look like:
Dog Fido Bowser
Cat Fluffy Whiskers
I have a query to get all the instances of each subclass:
SELECT ?class_label ?instance_label
WHERE {?anmlclass rdfs:subClassOf ex:Animal;
rdfs:label ?class_label.
?anml a ?anmlclass;
rdfs:label ?instance_label.}
But this gives output like:
Dog Fido
Dog Bowser
Is there a way to format the result so that all the instances are shown in the same row as the relevant class?

From a comment by UninformedUser:
SELECT ?class_label (group_concat(?instance_label; separator=", ") as ?instances)
WHERE {
?anmlclass
rdfs:subClassOf ex:Animal ;
rdfs:label ?class_label .
?anml
a ?anmlclass ;
rdfs:label ?instance_label
}
GROUP BY ?anmlclass

Related

How to Join Through a Join Table in Ruby Object Mapper SQL

Given a foo table, a bar table, and a foos_bars table, all three with id columns, the approach to getting bars with foos that the documentation would seem to imply is something like:
class Foo < ROM::Relation[:sql]
def with_foos_bars
qualified.inner_join(:foos_bars, foo_id: :id)
end
def with_bars
with_category_fixtures.qualified.inner_join(:categories, id: :bar_id)
end
end
However, #qualified only applies to the class, so this is actually just qualifying "Foo" twice, but we need to qualify at least two of the tables for a usable SQL query. The same seems to be the case for #prefix. Omitting #qualified and prefix simply leads to an ambiguous SQL query.
To clarify: the question is how does one join through a join table in Ruby Object Mapper?
You need to use symbol column names with Sequel naming conventions for now, so something like this:
class Foo < ROM::Relation[:sql]
def with_foos_bars
qualified.inner_join(
foos_bars, foos_bars__foo_id: foos__id
)
end
def with_bars
with_category_fixtures.qualified.inner_join(
:categories, categories__id: :foos_bars__bar_id
)
end
end
The plan is to provide new interfaces that would simplify that, although I gotta say this simple naming conventions has been working well for me. Having said that, there's definitely place for improvements here.
I hope this helps.

How create new instance of a Ruby Struct using named arguments (instead of assuming correct order of arguments)

Given: Customer = Struct.new(:name, :address, :zip)
Is there a way to name the arguments instead of presuming a certain order?
The docs say do it like this:
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", "12345")
which IMO makes it too easy to switch two parameters accidentally.
I'd like to do something like this:
joe = Customer.new(name: "Joe Smith", address: "123 Maple, Anytown NC", zip: "12345")
so that the order is not important:
joe = Customer.new(zip: "12345", name: "Joe Smith", address: "123 Maple, Anytown NC")
Named parameters are not (yet) possible in Ruby's Struct class. You can create a subclass of your own in line with this Gist: https://gist.github.com/mjohnsullivan/951668
As you know, full-fledged Classes can have named parameters. I'd love to learn why they aren't possible with Structs... I surmise that someone on the Core team has thought of this and rejected it.

How to get an array of values from a conditional ActiveRecord query with associations?

I'm looking to define a method on one of my objects that will return a just one column of data from all of its child objects so long as another column in the same record meets certain conditions.
For instance if I have two objects
ParentObject
has_many: child_objects
#fields
name (string)
ChildObject
belongs_to: parent_object
#fields
name (string)
whitelisted_at (datetime)
I've read up that I can get a list of all child_object records for a parent_object based on a conditional specified using .where(). For instance in my controller I have code like so:
ParentObject.child_objects.where("whitelisted_at IS NOT NULL")
This gives me an active record associate like so:
#<ActiveRecord::AssociationRelation [
<ChildObject id: 1, name:"Susan", whitelisted_at: "2015-02-18 12:07:37">,
<ChildObject id: 1, name:"Simon", whitelisted_at: "2015-02-18 12:07:37">,
<ChildObject id: 1, name:"Sally", whitelisted_at: "2015-02-18 12:07:37">
]
I was looking how I would then filter through these to return an array of just names. Ideally i'd be able to run this all as a Model method so:
class ChainObject < ActiveRecord::Base
...
def whitelisted_names
#... outputs [Susan, Simon, Sally]
end
end
What would be the most concise and rails-y way of doing this. I thought about doing a .where() then an .each() and having a block method but that seems really cumbersome and I'm sure I'm just missing some smart ActiveRecord or Association method that could pluck an array of values from multiple hashes. I'm pouring over the APIdock but I think the problem is I don't know how to describe what I'm trying to do!
In your parent model you could use where.not and use the pluck method ActiveRecord gives you (props to Stefan - see pluck)
class ParentObject < ActiveRecord::Base
...
def whitelisted_names
child_objects.where.not(whitelisted_at: nil).pluck(:name)
end
end

Can one uniquely initialize each object created by the create_list method in FactoryGirl?

In order to do this, I imagine passing an array of hashes to the create_list method. I am considering something that would look like this:
FactoryGirl.create_list(
:person, 3, [
{name: 'Rebekah', description: 'A woman with straight brown hair'},
{name: 'Day', description: 'A man with curly brown hair'},
{name: 'Ihsan', description: 'A boy with wavy blond hair'}
]
)
This would persist three objects initialized with the custom name and description values.
Is there any way to do this directly or ought I just loop through the array creating an individual instance with each set of unique values?
Do you really need them to be different? If you do, I see two options:
1- Set up the factory properties in a sequence. Like this:
FactoryGirl.define do
factory :person do
sequence(:name) { |n| "Person number #{n}" }
end
2- Create a list and set them up individually
list = FactoryGirl.create_list(:person, 3)
list.each do |person|
#setting up
end
There are other answers, but I would go with the first

How to compare two text files with value in ruby

I have two text file, and they have one same field(pet). I want to combine two file and output a new file contain three fields(owner pet buyer). My code is mainly depends on if both file have the same pet(name and number of pets) than I will add the name of buyer to the field of buyer in the new file. It should print out only if the two file have same filed of pet.
But my code did not work as I want, need some help, thanks.
input_file_1 (.txt)
owner pet
Michael dog, cat
John pig, rabbit
Marry dog, cat
input_file_2 (.txt)
buyer pet
Sean cat, dog
Mark cat, dog
Joy dog, mouse
Tina cat, dog
I want the result to look like this:
owner pet buyer
Michael cat, dog Sean, Mark, Tina
Mary cat, dog Sean, Mark, Tina
My code looks like this:
input_file_1 = ARGV[0]
input_file_2 = ARGV[1]
hash_1 = {}
File.readlines(input_file_1, "\n").each do |line|
owner, pet = line.chomp.split("\t")
hash_1[owner] = pet
end
hash_2 = {}
File.readlines(input_file_2, "\n").each do |line|
buyer, pet = line.chomp.split("\t")
hash_2[buyer] = pet
end
hash_1.each do |key, value|
if hash_2.has_value? value
puts "#{key}\t#{value}\t#{hash_2[key]}"
end
end
I would suggest you use the pet as key:
input_file_1 = ARGV[0]
input_file_2 = ARGV[1]
hash_1 = Hash.new([])
File.readlines(input_file_1, "\n").each do |line|
owner, pet = line.chomp.split("\t")
hash_1[pet] += [owner]
end
hash_2 = Hash.new([])
File.readlines(input_file_2, "\n").each do |line|
buyer, pet = line.chomp.split("\t")
hash_2[pet] += [buyer]
end
hash_1.each do |pet, owners|
if hash_2.include? pet
owners.each do |owner|
puts "#{owner}\t#{pet}\t#{hash_2[pet].join(", ")}"
end
end
end

Resources