# MEBN-RM: A Mapping between Multi-Entity Bayesian Network and Relational Model
## 3 Mebn-Rm
### MEBN-RM Mapping Algorithm  
In the previous subsections, we discussed the mapping definitions for entities, resident nodes, MFrags, and MTheories. This subsection presents a _MEBN-RM mapping algorithm_ (Algorithm 1) which performs the _RDBS-MTheory Mapping_ in Definition 22 and specifies how to convert an MTheory from a relational database schema using the MEBN-RM definitions. For the _MEBN-RM mapping algorithm_, we assume that (1) the relational database schema are normalized by _Entity-Relationship Normalization_ (Definition 17), and (2) the list of relation schemas in the relational database schema are sorted by the entity relation schemas (ERS) first and the relationship relation schemas (RRS) second. For the algorithm, let M be an MTheory, M.E be a set of entity types of M, M.F be a set of MFrags, F.C be a set of context nodes in an MFrag F, F.R be a set of resident nodes of F, RDBS be a relational database schema, rs be a relation schema in RDBS, and rs.O be a set of attributes for NF and/or NK of rs. The algorithm takes the relational database schema RDBS as an input and produces the MTheory M as an output. ```
Input: RDBS a relational database schema Output: M a partial mapped MTheory
1 FunctionMEBN-RM Mapping ( RDBS )
2 M \(\leftarrow\) create a default MTheory ;
3 M.name \(\leftarrow\) have a schema name using RDBS;
4 for\(i\gets 1\) to \(n\)do
5\(rs_{i}\leftarrow\) have \(i\)-th relation schema from RDBS;
6 RM-MFrag Mapping(\(rs_{i}\), M);
7
8 return M;
9 FunctionRM-MFrag Mapping ( rs, M )
10 if\(rs\) = \(ERS\)then
11 M.E \(\leftarrow\) create an entity type from \(rs\) using ERS to Entity Mapping;
12 if\(rs\) = \(ERS\) and \(1\)rs.O \(|>0\)then
13 M.F \(\leftarrow\) create an MFrag for \(rs\);
14 F.C \(\leftarrow\) create IsA nodes from the entity types M.E associated with M.F;
15 F.R \(\leftarrow\) create resident nodes from \(rs\) using Function Resident Node Mapping;
16
17 elseif\(rs\) = \(RRS\)then
18 M.F \(\leftarrow\) F \(\leftarrow\) create an MFrag for \(rs\);
19 F.C \(\leftarrow\) create IsA nodes from the entity types M.E associated with M.F;
20 if\(rs\).O \(|=0\)then
21 F.R \(\leftarrow\) create resident nodes from \(rs\) using Predicate Resident Node Mapping;
22
23 elseifif\(1\)rs.O \(|>0\)then
24 F.R \(\leftarrow\) create resident nodes from \(rs\) using Function Resident Node Mapping;
25
26
27
28 end if
29
30 end for
```  
**Algorithm 1**MEBN-RM Mapping  
Inputs of this algorithm are a relational database schema RDBS. (2) The algorithm starts with creating a default MTheory M. (3) The name of RDBS is used to create the name of M. (4), (5) All relation schema are investigated from a first relation schema \(rs_{1}\) to a last relation schema \(rs_{n}\), where n denotes the number of the relation schemas in RDBS. (6) For an \(i\)-th relation schema, the algorithm performs the procedure _RM-MFrag Mapping_ defined in Definition 21. (9) If the \(i\)-th relation schema is ERS, (10) the _ERS to Entity Mapping_ (Definition 18) is performed. (11) If the \(i\)-th relation schema \(rs\) is ERS and there is an attribute O for NF or NK, then (12) an MFrag F for the \(rs\) is created and added to the set of MFrags of M, (13) IsA context nodes are created from the entity types M.E associated with F and added to the set of context nodes of F, and (14) resident nodes are created from \(rs\) using the _function resident node mapping_ (Definition 20) and added into the set of resident nodes of F. (15) If the \(i\)-th relation schema \(rs\) the is RRS, then performs (16) to (21). (16) An MFrag F for the \(rs\) is created and added to the set of MFrags of M. (17) IsA context nodes are created from the entity types M.E associated with F and added to the set of context nodes of F. (18) If there is no attribute O for NF or NK, (19) the _predicate resident node mapping_ (Definition 19) for \(rs\) is performed. (20) If there is an attribute O for NF or NK, (21) the _function resident node mapping_ (Definition 20) for \(rs\) is performed.