This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
多态选择操作的实现 define selectNoParam(selPred) is var result: \t1; candidate: \t2; begin result.create; !! 生成空结果集 foreach (candidate in self) if candidate.selPred then result.insert(candidate); return result; end define selectNoParam
应用举例• 在立方体实例库中,查找某一个顶点是原点的立
方体• 分析:其选择谓词需要顺次检查一个立方体的 8
个点,是否有一个点的 x 、 y 、 z 坐标值为 0 ,0±ε —— inOrigin 操作
• 其结果应当是下面二个集合的交集:– 包含在立方体实例集合中的实例—— myCuboid 所引
用– 满足谓词 inOrigin 的实例
• 多态的 Select 操作可以应用于任何一个集合类型
• inOrigin 定义declare inOrigin: Cuboid || bool;
define inOrigin is !!判断任何一个顶点是否处于坐标原点 return (( self.v1.x=0.0 ±ε and self.v1.y=0.0 ±ε and self.v1.z=0.0 ±ε) OR … ( self.v8.x=0.0 ±ε and self.v8.y=0.0 ±ε and self.v8.z=0.0 ±ε));
form b2 in(select b1from b1 in Brepwhere b1.weight>100)
where b2.weight<1000• 优化的结果• select b.name
form b in Brepwhere b.weight<1000 and b.weight>100
范围变量可以限定到实例集合的属性域上
• 示例:查询立方体 cubo#5 的 8 个顶点坐标。select v.x, v.y, v.z
from v in( select e.vertices from e in
( select f.edges from f in
( select b.faces from b in BRep where b.name="cubo#5" ) ) )
• 从最内层的 SFW开始( 7-9句): b被限定在 BRep 的类型扩展上
• 谓词 b.name=Cubo#5 将 b 限定到满足谓词的BRep 的一个子集上,返回每个几何体的 6 个面 f1-f6。
• 5- 6 行的 SFW 接受由内层返回的 6 个面,并计算它们的边的并集:
产生 12 个不同的 edge 对象
• 语句 3- 4产生 24 个点的并集--为 8 个不同的点
• 语句 1- 2产生 8 个点 x.y.z 值
},,{ 61
.f ff
edgesf
嵌套查询和路径表达式• 如下抽象的类型模式• type T0 is … type Tn-1 is type Tn is body body body
[… ; [… ; […]
A1: {T1} ; An: {Tn} ; … …] ; … ] ; end type Tn
… …
end type T0 end type Tn-1
• 即元组类型 Ti 通过它的属性 Ai+ 1产生对类型 Ti 的引用
• 一个嵌套查询的 SFW 表达式• 假如从引用链的始端 T0开始,到 Tn终止,查询
一个原子类型 SomeAttr 的值,那么它的查询如下:– select an.someAttr
from an in
( select an-1.An
from an-1 in
( select a1.A2
from a1 in
( select s.A1
from s in someSet ) )…)
• 利用路径表达式,其查询可以缩短为:– select an.someAttr
from an in someSet.A1.A2.….An
嵌套查询的数学表示• 数学上的表示:结果集合可以表示为并
集 Un ,其中 Ui被递归定义成:
11
.1
:
ii Ua
i n)ifor(1 Aia
0)if(i someSet
iu
不同的 Join 类型• 不同类型的联系
– 基于值的 Join :关系查询传统的 Join 操作– 标识 Join– 函数 Join
基于值的 Join
• 传统的连接操作,通过给定的 Join属性的值的比较来联系对象
• 示例:查询两个 BRep 对象,它们之间,第二个变量为第一个的两倍
• select b1.name,b2.name from b1 in BRep, b2 in BRep where b1.weight * 2.0 = b2.weight+є
• 对象 b1 , b2之间通过变量值来关联
标识 Join-- 同一 Jion
• 标识 Join-- 基于对象的相等,而不是值相等,因此,是基于 OID 值相等的连接
• 示例 .– 谓词 O1= O2-- 表示 O1 与 O2OID 相等而不是 O
1 与 O2 的状态相等– 谓词 OЄ<Object>-- 当且仅当 O 是 Object 集
合中的一个对象实例时为真,而不是说 Object中有一个对象状态与 O 的状态相等
标识 Join 示例• 父,母,子三个对象类型及其实例库• type Man with extention is
[name: string;...;hasKids:{Child};]; type WOMan with extention is [name: string;...;hasKids:{Child};]; type Child with extention is [name: string;...;father:Man;mother:Woman;];
• 查询同一个父亲的两个孩子 C1 , C2
• select c1.name,c2.name form c1 in child;c2 in child where c1.father=c2.father and c1!=c2
• 这里 c1.father= c2.father 是标识相等,而不是值相等
• 如果是这样的谓词Where c1.father.name=c2.father.name and
c1!=c2 -- 值相等查询
• 查询一个孩子的父母亲• select m.name,w.name,c.name
from m in Man,w in Woman,c in Child where c in m.hasKids and c in w.hasKids