about query in domain relational calculus - relational-division

There are 2 relations:
Prediction(cname, etype)
Measures(etype, provider)
cname - city name of predicted future disaster.
etype - event type. earthquake, tsunami...
provider - police, ambulance...
I need to write a query using domain relational calculus and it should find a provider that provides service to all predicted events in Milano.
I have this so far:
{<P> | ∃et <et,P> ∈ Measures ^ ∀ ev (<'Milano', ev> ∈ Prediction
⟹ ∃pr(ev,pr) ^ pr=P)}
I am not sure about it. Is it ok? or something is wrong?

You don't give a reference to your version of RA (relational algebra) or DRC (domain relational calculus). I'll guess some syntax from your attempt.
-- <cname, etype> rows where city cname suffers event type etype
-- { <cname, etype> | city cname can suffer event type etype }
Provider
-- <etype, provider> rows where event type etype service is provided by provider
-- { <etype, provider> | event type etype is service is provided by provider}
Measures
provider that provides service to all predicted events in Milano.
That is a classic ambiguous use of "all"/"every". If no event types happen in Milano, do you want all providers or no providers? (This is a common issue in queries calculated via variants of relational division.)
Maybe you want providers p where for all types e, if Milano suffers e then p services e:
-- <p> rows where
(for all e (
if city 'Milano' suffers event type e then event e service is provided by p))
{ <p> | (forall e (if <'Milano', e> ∈ Prediction then <e, p> ∈ Measures)) }
But from your query it seems like maybe you want providers p where there is a type that Milano suffers & for all types e, if Milano suffers e then p services e:
-- <p> rows where
(for some e ('Milano' suffers event type e))
& (for all e (
if city 'Milano' suffers event type e then event e service is provided by p))
{ <p> |
(exists e (<'Milano', e> ∈ Prediction))
& (forall e (if <'Milano', e> ∈ Prediction then <e, p> ∈ Measures))
}
Your query seems to be trying to be like the following complication of that:
{ <p> |
(exists e (<'Milano', e> ∈ Prediction))
& (forall e (
if <'Milano', e> ∈ Prediction
then (exists pr (<e, pr> ∈ Measures & pr = p))
))
}

Related

Translate "there exists one" from English sentence to ALCQO description logic

I have the following English question
There exists one school student.
And I would like to translate it to ALCQO or First order logic.
I have written the following:
∃SchoolStudent
or in First Order Logic:
∃x(SchoolStudent(x))
However, I know from mathematics and the theory that ∃ is translated as "at least one" or "some". Thus, I am wondering if the following two options are correct:
approach 1
¬∃x(SchoolStudent(x))
approach 2
(≥1student.SchoolStudent)⊓(≤1student.SchoolStudent)
∃SchoolStudent is meaningless in ALCQO. The only constructor available in ALCQO using existential restrictions are of the form
∃r.C
which is interpreted as
{d ∈ ΔI | there is an e ∈ ΔI with (d, e) ∈ rI and e ∈ CI}
In first order logic ∃r.C translates to πx(∃r.C) = ∃y.r(x, y) ∧ πy(C).
To define a school with only 1 student you will have to have the concepts School and Student and a role which can be used to associate a student with a school, say hasStudent.
School ≡ ∃hasStudent.Student ⊓ ≤1hasStudent.Student
This means School here is the set of schools that have only a single student.
But we can also define School as anything that has more than 1 student and then define a specific individual of the school as a school with only 1 student.
School ≡ ∃hasStudent.Student
School(x)
≤1hasStudent.Student(x)

How do I design a search operation in Z notation whereby the search function requires at least one details?

This is my Z schema for Appointment DB.
|--AppointmentDB----------------
|attendees : P Person /** those involved in the appointment **/
|
|/** a new TYPE object to store attendees, schedule and purpose **/
|appointments : P APPOINTMENT
|hasAppointment : Person <-> APPOINTMENT
|schedule : APPOINTMENT -> DateTime
|purpose : APPOINTMENT -> Report
|
|/** a forward relation compositions to relate attendees with purpose and schedule **/
|attendeePurpose : hasAppointment;purpose
|attendeeSchedule : hasAppointment;schedule
|-----------------------------
|attendees ⊆ dom(hasAppointment)
|attendees ⊆ dom(attendeePurpose)
|appointments ⊆ ran(hasAppointment)
|-----------------------------
I would like to create a search function that finds an appointment based on the name of the attendees.
I want the search function to return all the details of the appointment object.
How do I design it?
Here is my take :
|--FindAppointment---------------------------------------------------
|ΞAppointmentDB
|attendees? : Person
|appointmentAttendees! : P Person
|appointmentPurpose! : Report
|appointmentSchedule! : DateTime
|-----------------------------
|/** if name of any attendees is given, then it must exist in appointments' domain
|respectively before this function can run**/
|attendees? ∈ dom(attendees)
|
|/** return the set of attendees of the same APPOINTMENT using attendees? as input **/
|appointmentAttendees! = hasAppointment~(|{attendees?}|)
|
|/** Get the image of both forward relational compositions according to set of
|attendees?**/
|appointmentPurpose! = attendeePurpose(|{attendees?}|)
|appointmentSchedule! = attendeeSchedule(|{attendees?}|)
|----------------------------------------------------------------------
Have you type checked your specification?
Your declaration subject? : P Person states that subject? is a set of persons, but subject? : dom(attendees) implies that subject? is a single person.
If you want to have either none or one person given you could introduce a datatype analogous to the Maybe monad in functional programming languages (or null values in other programming languages):
MaybePerson ::= NoPerson | JustPerson <<Person>>
Then you can declare an input like
subject? : MaybePerson
Then I would to suggest to restrain the possible solutions for one input
subject? : ran(JustPerson) => schedule! : schedule(|{ JustPerson~ subject? }|)
If subject? is a set of persons you can achieve the same with:
subject? /= {} => schedule! : schedule(|subject?|)
And then just do the same for the other possible input. You can add also a condition that not both entries should be NoPerson resp. not both input sets should be empty.

pig is null query

I am using pig 0.10.
i have an outer bag(relation)
grunt>dump e;
(vinyas,(shetty,12),{(12,vinyas),(99,shetty)})
(vas,(shety,12),{(12,vinyas),(33,shetty)})
(fgkyas,(shety,12),{(12,vinyas),(12,shetty)})
(fky,(uhjyt,12),{(,),(,)})
grunt> describe e;
e: {name: chararray,t: (),b: {t: (x: int,y: chararray)}}
grunt> op = filter e by IsEmpty(b) or b is null;
Now op does not return anything.I was actually expecting the last tuple of relation e(i.e name with "fky") to be returned.Can some1 plz explain this behavior
IsEmpty checks at the size of the DataBag, which takes into account empty tuples as well. So you need to specify a more rigorous check.
(And b is definitely not NULL - so that would not get what you wanted).

Table based declarative reactive programming

Is there a programming language or package that supports table based reactive declarative programming in memory very similar to the SQL language and trigger facility?
For example, I could define PERSON and JOB tables as functions
name: PERSON -> STRING
female: PERSON -> BOOLEAN
mother: PERSON -> PEOPLE
father: PERSON -> PEOPLE
title: JOB -> STRING
company: JOB -> STRING
salary: JOB -> INTEGER
empoyee: JOB -> PERSON
Then I would like to calculate functions like:
childcount: PERSON -> INTEGER
childcount(P) = |{ Q in PERSON : father(Q) = P or mather(Q) = P }|
income: PERSON -> INTEGER
income(P) = SUM { salary(J) : J in JOB and empoyee(J) = P }
incomeperchild: PERSON -> INTEGER
incomeperchild(P) = income(P) / childcount(P)
parent: PERSON x PERSON -> BOOLEAN
person(P,Q) = (P = father(Q)) or (P = mother(Q))
error: PERSON -> BOOLEAN
error(P) = (female(P) and (exists Q in PERSON)(father(Q) = P))
or (not female(P) and (exists Q in PERSON)(mother(Q) = P))
or (exists Q in PERSON)(parent(P,Q) and error(Q))
So essentially I would like to have calculated columns in tables that are automatically updated whenever values in the tables change. Similar things could be expressed with SQL triggers, but I would like to have such functionality built into a language and executed in memory. The propagation of changes need to be optimized. Are there frameworks to do this?
The observer patter and reactive programming focuses on individual objects. But I do not want to maintain pointers and extra structure for each row in my tables as there could be million of rows. All the rules are generic (although they can refer to different rows via parent/children relations, etc), so some form of recursion is required.
One way to approach this is via attribute grammars: http://www.haskell.org/haskellwiki/Attribute_grammar

jpa join query on a subclass

I have the following relationships in JPA (hibernate).
Object X has two subclasses, Y and Z.
Object A has a manyToOne relationship to object X. (Note, this is a one-sided relationship so object X cannot see object A).
Now, I want to get the max value of a column in object A, but only where the relationship is of a specific subtype, ie...Y.
So, that equates to...get the max value of column1 in object A, across all instances of A where they have a relationship with Y. Is this possible? I'm a bit lost as how to query it.
I was thinking of something like:
String query = "SELECT MAX(a.columnName) FROM A a join a.x;
Query query = super.entityManager.createQuery(query);
query.execute();
However that doesn't take account of the subclass of X...so I'm a bit lost.
Any help would be much appreciated.
JPA 2.0 allows queries to restrict the classes returned from a polymorphic query with the TYPE operator. From the JPA 2.0 specification:
4.6.17.4 Entity Type Expressions
An entity type expression can be used
to restrict query polymorphism. The
TYPE operator returns the exact type
of the argument.
The syntax of an entity type
expression is as follows:
entity_type_expression ::=
type_discriminator |
entity_type_literal |
input_parameter
type_discriminator ::=
TYPE(identification_variable |
single_valued_object_path_expression |
input_parameter )
An entity_type_literal is designated
by the entity name.
The Java class of the entity is used
as an input parameter to specify the
entity type.
Examples:
SELECT e
FROM Employee e
WHERE TYPE(e) IN (Exempt, Contractor)
SELECT e
FROM Employee e
WHERE TYPE(e) IN (:empType1, :empType2)
SELECT e
FROM Employee e
WHERE TYPE(e) IN :empTypes
SELECT TYPE(e)
FROM Employee e
WHERE TYPE(e) <> Exempt
JPA 1.0 doesn't offer such a mechanism so if you're using JPA 1.0, this won't be possible using standard JPQL.
If you don't mind using proprietary HQL, then you can use the special property class:
Likewise, the special property class accesses the discriminator value of an instance in the case of polymorphic persistence. A Java class name embedded in the where clause will be translated to its discriminator value.
from Cat cat where cat.class = DomesticCat
So in your case, I'd try something like that:
SELECT MAX(a.columnName) FROM A a where a.x.class = Y
I have not tested it, but try something like this:
SELECT MAX(a.columnName) FROM A a join a.x WHERE a.x.class = y.class
Hope it helps,
Anton