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)
Related
Example:
Students want to enroll in multiple courses of different course groups (Math, English, Spanish, History) and have issued preferences for each course group (ENG-1 > ENG-2 means course ENG-1 is preferred to course ENG-2).
Student A:
MATH-2 > MATH-4 > MATH-1 > ... > MATH-9
ENG-3 > ENG-4 > ENG-1 > ... > ENG-2
Student B:
ENG-1 > ENG-2 > ENG-4 > ... > ENG-3
SPA-4 > SPA-6 > SPA-3 > ... > SPA-2
HIST-1 > HIST-3 > HIST-2 ... > HIST-5
Student C:
...
Is it possible for a planning variable of each student (planning entity) to be a combination of each of their preferences? I.e. student A would be assigned MATH-2 and ENG-3, student B would be assigned ENG-1, SPA-4, and HIST-1, if the constraints allow for this.
Yes (and no). Technically no, because #PlanningVariable can only hold a single value.
But YES, OptaPlanner can handle your use case. You just need to choose the right way to map your domain to Java classes. You need to model a N:M relation between Student and Course:
Student A needs to enroll in 2 courses (one from the MATH group and one from the ENG group).
Student B needs to enroll in 3 courses (ENG, SPA, HIST).
etc.
You can model this type of relationship with the CourseAssignment class, which is your #PlanningEntity. It could look like this:
#PlanningEntity
class CourseAssignment {
final Student student; // e.g. Ann
final CourseGroup courseGroup; // e.g. MATH
#PlanningVariable(valueRangeProviderRefs = { "courseRange" })
Course course; // changed by Solver - could be MATH-1, MATH-2, ENG-1, HIST-...
}
Since the number of course assignments is known for each student and it's fixed, you'd simply create 2 CourseAssignment instances for Student A, 3 instances for Student B, etc.
Next, design your constraints to penalize each courseAssignment with a hard score penalty, if courseAssignment.course.group != courseAssignment.courseGroup and with a soft score penalty based on courseAssignment.student.getPreference(courseAssignment.course).
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))
))
}
Sorry for the vague question topic!
I've got a particular relational-algebra problem that has me and a couple of friends stumped.
Now, here's the question:
For each department, find the maximum salary of instructors in that
department. You may assume that every department has at least one
instructor.
I'll upload the schema as well, as a visual aide.
I've worked this out to a point;
I need a relation that includes all the instructors in any department, we've got that. It's the instructor relation.
Out of that relation i need to 'split' it up into a per-department basis. and once I have that relation I just take the max(salary) and return that.
Problem is, the only way I can think of to do that is something like this:
π(max(salary)(σ(dept_name = x(instructor)))
Where x = whatever dept_name i'm looking for, but If I did it this way, then I'd have to do a new relation for every department!
How would you do it?
(Note: I just copy and paste'd the symbols from wikipedia if you want to use them in your answer)
My relational algebra might be a bit rusty but I think that
dept_name_G_{max(salary)}(
σ_{ddept_name = idept_name}(
ρ_{dept_name/ddept_name}(department) ⨯
ρ_{dept_name/idept_name}(instructor)
)
)
is what you seek for.
Remember that all projections are just operations on sets. The first thing you would do to
connect the information of department and instructor is to bring the information together.
So you want to join department and instructor, basically a cross product (⨯):
department = {(depA, 100$), (depB, 200$)}
instructor = {(will, depA, 10$), (bob, depB, 20$), (will, depB, 9$)}
department ⨯ instructor = {
(depA, 100$, will, depA, 10$),
(depA, 100$, bob, depB, 20$),
...,
(depB, 200$, will, depA, 10$),
...
}
So what you would want now is to filter the tuples where the dept_name of the instructor equals the
dept_name of the department. But you also notice that you now have a naming collision,
namely the column dept_name comes up twice.
As you can't simply do σ_{dept_name = dept_name}(department ⨯ instructor) you need to rename at
least one of the dept_name fields. I renamed both for clarity which one belongs to what.
So what you now have is
σ_{ddept_name = idept_name}(
ρ_{dept_name/ddept_name}(department) ⨯
ρ_{dept_name/idept_name}(instructor)
)
giving you:
{
(depA, 100$, will, depA, 10$),
(depB, 200$, bob, depB, 20$),
(depB, 200$, will, depB, 9$)
}
The whole process is a natural join and can be expressed shortly with:
department ⋈ instructor
Now the final step is to project the maximum salary per department. A simple projection can't do that
but the aggregation operator can:
{dept_name}_G_{max(salary)}(department ⋈ instructor)
results in
{
(depA, 10$),
(depB, 20$)
}
Hi could someone please verify my work. Im not sure if im doing any of this correctly and would greatly appreciate any help. I am not allow to use the Bow tie operator. Thank you.
Question:
Books (ISBN, Title, Authors, Publisher, Ed, Year, Genre)
Patron (MemberNumber, FirstName, LastName, AddressLn1, AddressLn2, City, State, Zipcode)
Loan (MemberNumber,ISBN,DateLoaned,DateDue, DateReturned)
Business Logic
• You may assume that the library only has one copy of each book.
• Each book may have many authors. If a particular book has multiple authors, they are listed as a comma separated string. You may assume that the same author always uses the same exact name and no two authors will have the same name.
• Year is stored as an integer.
• DateLoaned, DateDue, and DateReturned are stored as a date.
• When a book is initially lent out, DateReturned is set to be NULL, upon its return, the value is updated.
1.1. Find all books that were loaned out after 12/22/2012. Show the ISBN, Title, and DateDue.
1.2. Find all library patrons who have borrowed a book titled "Database Systems". Show their FirstName, LastName, and DateLoaned.
1.3. Find all books that were ever loaned out. Display the ISBN.
1.4. Find all books returned before 12/22/2012. Display the ISBN.
1.5. Find all books returned on or after 12/22/2012. Display the ISBN.
1.6. Find all books returned either (before 12/22/2012) or (on or after 12/22/2012) Display the ISBN.
1.7. In 1 sentence explain the difference between 1.3 and 1.6.
1.8. Find all patrons who have never borrowed a book.
1.9. Find all books with Genre "Mystery" that have NEVER been loaned out.
1.10. Create a new attribute ImportantDates. A date is important if it is in the Loan relation either as a DateLoaned or a DateDue. Display ImportantDates.
1.11. Find all library patrons who have borrowed a book with an author "James Stewart". You may use the expression LIKE "%James Stewart%" in your Relational Algebra.
1.12. Find all library patrons who have never borrowed a book with an author "James Stewart". You may use the expression LIKE "%James Stewart%" in your Relational Algebra.
1.13. Find all library patrons who have only borrowed a book with an author "James Stewart". If they have ever borrowed a book without the author "James Stewart" they should be excluded. You may use the expression LIKE "%James Stewart%" in your Relational Algebra.
Answer:
1.1) πISBN,TITLE,DATEDUE(σDATELOANED > 12222012 AND BOOKS.ISBN = LOAN.ISBN(LOAN X BOOKS)
1.2) πFIRSTNAME,LASTNAME,DATELOANED(σTITLE = "DATABASE SYSTEMS" AND BOOKS.ISBN = LOAN.ISBN AND PATRON.MEMBERNUMBER = LOAN.MEMBERNUMBER(BOOKS X PATRON X LOAN))
1.3) πISBN(σDATELOANED <> "NULL" AND BOOK.ISBN = LOAN.ISBN(LOAN X BOOKS))
1.4) πISBN(σDATELOANED < 12222012 AND BOOK.ISBN = LOAN.ISBN(LOAN X BOOKS))
1.5) πISBN(σDATELOANED >= 12222012 AND BOOK.ISBN = LOAN.ISBN(LOAN X BOOKS))
1.6) πISBN(σDATELOANED >= 12222012 OR DATELOANED <12222012 AND BOOK.ISBN = LOAN.ISBN(LOAN X BOOKS))
1.7) 1.3 AND 1.6 are the same as they both find books that have been loaned.
1.8) σDATELOANED = "NULL" AND PATRON.MEMBERNUMBER = LOAN.MEMBERNUMBER(LOAN X PATRON)
1.9) σGENRE = "MYSTERY" AND BOOKS.ISBN = LOAN.ISBN AND DATELOANED = "NULL"
1.10) LOAN(DATELOANED,DATEDUE) -> IMPORTANTDATE
Could you please give me an example of either 1.11, 1.12, or 1.13 as I have no clue in how to use the LIKE expression.
The professor told you how to do it:
You may use the expression LIKE "%James Stewart%" in your Relational Algebra.
It has been about a year since I have had to use relational algebra, but it will be whatever your pre-conditions are (this is a task for you) followed by the line:
LIKE %James Stewart%
The SQL statement would look something like this:
Select * from patrons p where Books.author LIKE %James Stewart%
You will find in your studies relational algebra does not deal with functions of SQL it just looks at the purely mathematical side of things.
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