← All examples
example Demo
# Universal Warehousing Demo
# user defined functions, in Java
typeside Type = literal {
imports
sql
external_functions
toUpper : String -> String = "x => (x.isPresent() ? java.util.Optional.of(x.get().toUpperCase()) : java.util.Optional.empty())"
}
###########################################################################
# input schemas
schema AmphibianSchema = literal : Type {
entities
Amphibian Animal
foreign_keys
toAnimal : Amphibian -> Animal
attributes
species : Animal -> String
}
schema LandSchema = literal : Type {
imports
AmphibianSchema
entities
LandAnimal
foreign_keys
isA : Amphibian -> LandAnimal
isA : LandAnimal -> Animal
path_equations
Amphibian.isA.isA = Amphibian.toAnimal
}
schema WaterSchema = literal : Type {
imports
AmphibianSchema
entities
WaterAnimal
foreign_keys
isA : Amphibian -> WaterAnimal
isA : WaterAnimal -> Animal
path_equations
Amphibian.isA.isA = Amphibian.toAnimal
}
###########################################################################
#input instances
instance LandInstance = literal : LandSchema {
generators x7 x8 : Amphibian x3 x4 x5 x6 : Animal x0 x1 x2 : LandAnimal
equations
x4.species = Gecko x5.species = equine x6.species=FROG
x7.isA = x0 x7.toAnimal = x4
x8.isA = x2 x8.toAnimal = x6
x0.isA = x4 x1.isA = x5 x2.isA = x6
}
instance WaterInstance = literal : WaterSchema {
generators x4 : Amphibian x2 x3 : Animal x0 x1 : WaterAnimal
equations
x4.isA = x1 x4.toAnimal = x3
x2.species = FISH x3.species = frog
x0.isA = x2 x1.isA = x3
}
###########################################################################
#compute the canonical schema colimit, then enhances its semantics
schema_colimit UniversalWarehouseSchema = quotient LandSchema + WaterSchema : Type {
entity_equations
LandSchema.Animal = WaterSchema.Animal
LandSchema.Amphibian = WaterSchema.Amphibian
path_equations
LandSchema_Amphibian.LandSchema_Amphibian_toAnimal = LandSchema_Amphibian.WaterSchema_Amphibian_toAnimal
options
simplify_names=false
}
schema_colimit ModifiedWarehouseSchema = modify UniversalWarehouseSchema {
rename entities
LandSchema_LandAnimal -> Land
WaterSchema_WaterAnimal -> Water
LandSchema_Amphibian -> Amphibian
LandSchema_Animal -> Animal
rename foreign_keys
Amphibian.LandSchema_Amphibian_isA -> land_is
Amphibian.WaterSchema_Amphibian_isA -> water_is
Amphibian.WaterSchema_Amphibian_toAnimal -> redundantW
Amphibian.LandSchema_Amphibian_toAnimal -> redundantL
Water.WaterSchema_WaterAnimal_isA -> isA
Land.LandSchema_LandAnimal_isA -> isA
rename attributes
Animal.LandSchema_Animal_species -> land_species
Animal.WaterSchema_Animal_species -> water_species
remove foreign_keys
Amphibian.redundantL -> land_is . isA
Amphibian.redundantW -> water_is. isA
}
################################################################################################
# migrate the data onto the warehouse schema
schema WarehouseSchema = getSchema ModifiedWarehouseSchema
mapping LandToWarehouse = getMapping ModifiedWarehouseSchema LandSchema
mapping WaterToWarehouse = getMapping ModifiedWarehouseSchema WaterSchema
instance LandInstanceForward = sigma LandToWarehouse LandInstance
instance WaterInstanceForward = sigma WaterToWarehouse WaterInstance
instance UnmergedWarehouse = coproduct LandInstanceForward + WaterInstanceForward : WarehouseSchema
################################################################################################
# merge duplicates
instance Warehouse = quotient_query UnmergedWarehouse {
entity Amphibian -> {from a:Amphibian b:Amphibian where toUpper(a.land_is.isA.land_species) = toUpper(b.water_is.isA.water_species)}
options
quotient_use_chase = false
}
################################################################################################
# Application 0 : View warehouse as graph
schema WarehouseAsGraph = pivot Warehouse
################################################################################################
# Application 1 : Project (round-trip) the warehouse back onto the land schema
instance RoundTripLand = delta LandToWarehouse Warehouse
transform RoundTripLandFn = unit LandToWarehouse LandInstance
################################################################################################
# Application 2 : Project further onto the Amphibians schema with a query
query LandToAmphibians = literal : LandSchema -> AmphibianSchema {
entity Amphibian -> {from amp:Amphibian
#where amp.toAnimal.species = "frog"
foreign_keys toAnimal -> {anim -> amp.toAnimal}}
entity Animal -> {from anim:Animal
#where anim.species = "frog"
attributes species -> anim.species}
}
instance RoundTripAmphibians = eval LandToAmphibians RoundTripLand
################################################################################################
# Application 3 : Check and/or repair additional rules
constraints AllFksInjective = literal : WarehouseSchema {
forall a1 a2 : Amphibian where a1.land_is = a2.land_is -> where a1 = a2
forall a1 a2 : Amphibian where a1.water_is = a2.water_is -> where a1 = a2
forall l1 l2 : Land where l1.isA = l2.isA -> where l1 = l2
forall w1 w2 : Water where w1.isA = w2.isA -> where w1 = w2
}
command cmd = check AllFksInjective Warehouse
instance ANonInjectiveWarehouse = literal : WarehouseSchema {
generators
frog1 frog2 : Amphibian
frog : Land
equations
frog1.land_is = frog
frog2.land_is = frog
}
instance RepairedInjectiveWarehouse = chase AllFksInjective ANonInjectiveWarehouse
instance ANonInjectiveWarehouse
Amphibian| ID | land_is | water_is |
|---|
| 0 | 3 | 4 |
| 1 | 3 | 5 |
Animal| ID | water_species | land_species |
|---|
| 2 | ?0 | ?1 |
instance LandInstance
Amphibian| ID | isA | toAnimal |
|---|
| 0 | 6 | 2 |
| 1 | 7 | 3 |
Animal| ID | species |
|---|
| 2 | Gecko |
| 3 | FROG |
| 4 | ?0 |
| 5 | equine |
instance LandInstanceForward
Amphibian| ID | land_is | water_is |
|---|
| 0 | 6 | 9 |
| 1 | 7 | 10 |
Animal| ID | water_species | land_species |
|---|
| 2 | ?0 | Gecko |
| 3 | ?1 | FROG |
| 4 | ?2 | ?3 |
| 5 | ?4 | equine |
instance RepairedInjectiveWarehouse
Amphibian| ID | land_is | water_is |
|---|
| 0 | 2 | 3 |
Animal| ID | water_species | land_species |
|---|
| 1 | ?0 | ?1 |
instance RoundTripAmphibians
Animal| ID | species |
|---|
| 2 | FROG |
| 3 | ?0 |
| 4 | Gecko |
| 5 | ?1 |
| 6 | equine |
instance RoundTripLand
Amphibian| ID | isA | toAnimal |
|---|
| 0 | 7 | 2 |
| 1 | 8 | 4 |
Animal| ID | species |
|---|
| 2 | FROG |
| 3 | ?0 |
| 4 | Gecko |
| 5 | ?1 |
| 6 | equine |
instance UnmergedWarehouse
Amphibian| ID | land_is | water_is |
|---|
| 0 | 9 | 13 |
| 1 | 10 | 14 |
| 2 | 12 | 15 |
Animal| ID | water_species | land_species |
|---|
| 3 | ?0 | Gecko |
| 4 | ?1 | FROG |
| 5 | ?2 | ?3 |
| 6 | ?4 | equine |
| 7 | frog | ?5 |
| 8 | FISH | ?6 |
instance Warehouse
Amphibian| ID | land_is | water_is |
|---|
| 0 | 7 | 10 |
| 1 | 8 | 12 |
Animal| ID | water_species | land_species |
|---|
| 2 | frog | FROG |
| 3 | FISH | ?0 |
| 4 | ?1 | Gecko |
| 5 | ?2 | ?3 |
| 6 | ?4 | equine |
instance WaterInstance
Amphibian| ID | isA | toAnimal |
|---|
| 0 | 3 | 1 |
Animal| ID | species |
|---|
| 1 | frog |
| 2 | FISH |
instance WaterInstanceForward
Amphibian| ID | land_is | water_is |
|---|
| 0 | 3 | 4 |
Animal| ID | water_species | land_species |
|---|
| 1 | frog | ?0 |
| 2 | FISH | ?1 |
command cmd
Satisfied