1 / 25

Complementi di basi di dati Progetti ModelGen - MIDST

Complementi di basi di dati Progetti ModelGen - MIDST . P. Atzeni, P. Cappellari, G. Gianforme 15 marzo 2007. Datalog. Un linguaggio di programmazione logica per basi di dati derivato dal Prolog Utilizza predicati di due tipi: estensionali : relazioni della base di dati

tambre
Télécharger la présentation

Complementi di basi di dati Progetti ModelGen - MIDST

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Complementi di basi di datiProgetti ModelGen - MIDST P. Atzeni, P. Cappellari, G. Gianforme 15 marzo 2007

  2. Datalog • Un linguaggio di programmazione logica per basi di dati derivato dal Prolog • Utilizza predicati di due tipi: • estensionali: relazioni della base di dati • intensionali: corrispondono alle viste • Il linguaggio è basato su regole utilizzate per "definire" i predicati estensionali 15 marzo 2007

  3. Datalog in ModelGen-MIDST • In ModelGen-MIDST usiamo Datalog per materializzare i risultati delle traduzioni: • le regole generano i costrutti tradotti • La distinzione fra predicati intensionali e estensionali è leggermente diversa dal normale • Utilizziamo un’estensione importante: generazione di OID con funzioni di Skolem 15 marzo 2007

  4. Datalog, sintassi • Regole: testa  corpo • testaè un predicato atomico (intensionale) • corpoè una lista (congiunzione) di predicati atomici • Le interrogazioni sono specificate per mezzo di predicati atomici (convenzionalmente preceduti da "?") • Vediamo ora anche le interrogazioni, anche se per i progetti ci interessano poco (ci interessa definire dati derivati) 15 marzo 2007

  5. Impiegati Matricola Nome Età Stipendio 7309 Rossi 34 45 5998 Bianchi 37 38 9553 Neri 42 35 5698 Bruni 43 42 4076 Mori 45 50 8123 Lupi 46 60 Supervisione Impiegato Capo 7309 5698 5998 5698 9553 4076 5698 4076 4076 8123 Esempi 15 marzo 2007

  6. Esempio -1 • Trovare matricola, nome, età e stipendio degli impiegati che hanno 30 anni SELEtà=30(Impiegati) { Matricola: m, Nome: n, Età: e, Stipendio: s | Impiegati(Matricola: m, Nome: n, Età: e, Stipendio: s)  s = 30 } ? Impiegati(Matricola: m, Nome: n, Età: 30, Stipendio: s) 15 marzo 2007

  7. Esempio 0a • Trovare matricola, nome, età e stipendio degli impiegati che guadagnano più di 40 SELStipendio>40(Impiegati) { Matricola: m, Nome: n, Età: e, Stipendio: s | Impiegati(Matricola: m, Nome: n, Età: e, Stipendio: s)  s > 40 } • Serve un predicato intensionale ImpRicchi(Matricola: m, Nome: n, Età: e, Stipendio: s)  Impiegati(Matricola: m, Nome: n, Età: e, Stipendio: s) , s >40 ? ImpRicchi(Matricola: m, Nome: n, Età: e, Stipendio: s) 15 marzo 2007

  8. Esempio 0b • Trovare matricola, nome ed età di tutti gli impiegati PROJMatricola, Nome, Età(Impiegati) { Matricola: m, Nome: n, Età: e | Impiegati(Matricola: m, Nome: n, Età: e, Stipendio: s)} InfoPubbliche(Matricola: m, Nome: n, Età: e) Impiegati(Matricola: m, Nome: n, Età: e, Stipendio: s) ? InfoPubbliche(Matricola: m, Nome: n, Età: e) 15 marzo 2007

  9. Esempio 2 • Trovare le matricole dei capi degli impiegati che guadagnano più di 40 PROJCapo (Supervisione JOINImpiegato=Matricola (SELStipendio>40(Impiegati))) { Capo: c | Supervisione(Capo:c,Impiegato:m) Impiegati(Matricola: m, Nome: n, Età: e, Stipendio: s)  s > 40 } CapiDeiRicchi (Capo:c) • ImpRicchi(Matricola: m, Nome: n, Età: e, Stipendio: s), Supervisione (Capo:c,Impiegato:m) ? CapiDeiRicchi (Capo:c) 15 marzo 2007

  10. Esempio 5 • Trovare matricola e nome dei capi i cui impiegati guadagnano tutti più di 40 PROJCapo (Supervisione) - PROJCapo (Supervisione JOINImpiegato=Matricola (SELStipendio  40(Impiegati))) • serve la negazione CapiDiNonRicchi (Capo:c) Supervisione (Capo:c,Impiegato:m), Impiegati (Matricola: m, Nome: n, Età: e, Stipendio: s) , s  40 CapiSoloDiRicchi (Matricola: c, Nome: n) • Impiegati (Matricola: c, Nome: n, Età: e, Stipendio: s) , Supervisione (Capo:c,Impiegato:m), not CapiDiNonRicchi (Capo:c) ? CapiSoloDiRicchi (Matricola: c, Nome: n) 15 marzo 2007

  11. Esempio 6 • Per ogni impiegato, trovare tutti i superiori. • Serve la ricorsione Superiore (Impiegato: i, SuperCapo: c)  Supervisione (Impiegato: i, Capo: c) Superiore (Impiegato: i, SuperCapo: c)  Supervisione (Impiegato: i, Capo: c'), Superiore (Impiegato: c', SuperCapo: c) 15 marzo 2007

  12. Datalog, semantica • La definizione della semantica delle regole ricorsive è delicata (in particolare con la negazione) • Potere espressivo: • Datalog non ricorsivo senza negazione è equivalente al calcolo senza negazione e senza quantificatore universale • Datalog non ricorsivo con negazione è equivalente al calcolo e all'algebra • Datalog ricorsivo senza negazione e calcolo sono incomparabili • Datalog ricorsivo con negazione è più espressivo di calcolo e algebra 15 marzo 2007

  13. Datalog with OID invention • Datalog (informally): • a logic programming language with no function symbols and predicates that correspond to relations in a database • we use a non-positional notation • Datalog with OID invention: • an extension of Datalog that uses Skolem functions to generate new identifiers when needed • Skolem functions: • injective functions that generate "new" values (value that do not appear anywhere else); so different Skolem functions have disjoint ranges 15 marzo 2007

  14. Esercizio • Traduzione da ER a relazionale, completando quanto accennato nei lucidi seguenti • Per questa volta ancora sulla carta, per ragionare sui concetti 15 marzo 2007

  15. A basic translation • From (a simple) binary ER model to the relational model • a table for each entity • a column (in the table for E) for each attribute of an entity E • for each M:N relationship • a table for the relationship • columns … • for each 1:N and 1:1 relationship: • a column for each attribute of the identifier … 15 marzo 2007

  16. EmpNo Employees Name 1,1 Affiliation 0,N Name Departments Address A basic translation application 15 marzo 2007

  17. A basic translation (in supermodel terms) • From (a simple) binary ER model to the relational model • an aggregation of lexicals for each abstract • a component of the aggregation for each attribute of abstract • for each M:N aggregation of abstracts … • … • From (a simple) binary ER model to the relational model • a table for each entity • a column (in the table for E) for each attribute of an entity E • for each M:N relationship • a table for the relationship • columns … • for each 1:N and 1:1 relationship: • a column for each attribute of the identifier … 15 marzo 2007

  18. Schemas in our dictionary EmpNo Employees Name Name Departments Address 15 marzo 2007

  19. SM_AggregationOfLexicals( OID: #aggregationOID_1(OID), Name: n)  SM_Abstract ( OID: OID, Name: n ) ; the value for the attribute Name is copied (by using variable n) the value for OID is "invented": a new value for the function #aggregationOID_1(OID) for each different value of OID, so a different value for each value of SM_Abstract.OID the materialization of the Skolem function describes the mapping "An aggregation of lexicals for each abstract" 15 marzo 2007

  20. SM_ComponentOfAggregation… ( OID: #componentOID_1(attOID), Name: name, AggrOID: #aggregationOID_1(absOID), IsNullable: isNullable, IsKey: isIdent, Type : type ) ← SM_AttributeOfAbstract( OID: attOID, Name: name, AbstractOID: absOID, IsIdent: isIdent, IsNullable: isNullable , Type : type ) ; Skolem functions are functions are injective have disjoint ranges the first function "generates" a new value the second "reuses" the value generated by the first rule "A component of the aggregation for each attribute of abstract" 15 marzo 2007

  21. SM_AggregationOfLexicals( OID: #aggregationOID_1(OID), Name: name)  SM_Abstract ( OID: OID, Name: name ) ; "An aggregation of lexicals for each abstract" 15 marzo 2007

  22. SM_AggregationOfLexicals( OID: #aggregationOID_1(OID), Name: n)  SM_Abstract ( OID: OID, Name: n ) ; the value for the attribute Name is copied (by using variable n) the value for OID is "invented": a new value for the function #aggregationOID_1(OID) for each different value of OID, so a different value for each value of SM_Abstract.OID "An aggregation of lexicals for each abstract" 15 marzo 2007

  23. SM-AggregationOfLexicals OID Schema Name SM-aggregationOID_1_SK OID absOID 301 "An aggregation of lexicals for each abstract" EmpNo Employees SM_AggregationOfLexicals( OID: #aggregationOID_1(OID), Name: n)  SM_Abstract ( OID: OID, Name: n ) ; Name 1001 11 Employees 1002 11 Departments 1001 1002 302 15 marzo 2007 … … …

  24. SM_ComponentOfAggregation… ( OID: #componentOID_1(attOID), Name: name, AggrOID: #aggregationOID_1(absOID), IsNullable: isNullable, IsKey: isIdent, Type : type ) ← SM_AttributeOfAbstract( OID: attOID, Name: name, AbstractOID: absOID, IsIdent: isIdent, IsNullable: isNullable , Type : type ) ; Skolem functions are functions are injective have disjoint ranges the first function "generates" a new value the second "reuses" the value generated by the first rule "A component of the aggregation for each attribute of abstract" 15 marzo 2007

  25. SM-ComponentOfAggregationOfLexicals SM-AggregationOfLexicals OID Schema Name isIdent isNullable Type AggrOID OID Schema Name 1003 11 EmpNo T F Int 1004 11 Name F F Text 1001 SM-aggregationOID_1_SK SM-componentOID_1_SK OID OID absOID absOID 401 A component of the aggregation for each attribute of abstract" SM_ComponentOfAggregation… ( OID: #componentOID_1(attOID), Name: name, AggrOID: #aggregationOID_1(absOID), IsNullable: isNullable, IsKey: isIdent, Type : type ) ← SM_AttributeOfAbstract( OID: attOID, Name: name, AbstractOID: absOID, IsIdent: isIdent, IsNullable: isNullable , Type : type ) ; EmpNo Employees Name 1001 1001 11 Employees 1002 11 Departments 1003 1001 301 1004 402 1002 302 15 marzo 2007 … … …

More Related