Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Applying Mathematical Models in Software Design

Alina Andreica, Daniel Stuparu, Călin Miu


IT Department
Babes-Bolyai University
Cluj-Napoca, Romania
{alina.andreica, daniel.stuparu, calin.miu}@ubbcluj.ro

Abstract— The paper focuses on techniques for systematically


applying mathematical models in software design. We implement II. IMPLEMENTING GENERIC SORTING AT DATABASE
a generic sorting algorithm at database level, revealing the LEVEL
advantages of the approach. Based on techniques for processing In [3], we present a general framework, based on category theory
hierarchical structures at database level, a dedicated analysis principles, for designing algorithms by based on generic definitions of
addresses means of implementing simplification and equivalence a Set category and Ordered set category. Algorithmic definitions
algorithms on data structures based on modular entities. We within this framework are also given in [2], while in [1] we address
reveal the advantages of systematically implementing the database level structure from an automated proving point of view.
mathematical algorithms in software design and implementation, Within this paper, we use this general framework by adapting it for
including the level of database processing. The novelty of the implementing generic sorting at database level.
implementation consists in applying generic algorithms at
Sorting & merging algorithms in various forms, including
database level and in modelling discipline and module
categorical approaches are extensively studied. In [7] they appear as a
equivalence with equivalence and simplification algorithms.
case study for algorithm synthesis and automatic proving.
Keywords- software design, generic sorting, hierarchical The Set and OrderedSet categories are defined in a modular style
structures, database processing, run-time efficiency, equivalence [3], being parameterized with the domain of elements. We also define
algorithms, simplification their specific operators, which can be redefined, if necessary, in
descendant domains . Inheritance is modelled by explicit call of the
inherited context / category in the current one. The OrderedSet
I. INTRODUCTION category [3] may be then used for defining sorting algorithms over
Aiming at constructing a consistent framework for various domains [5]
developing integrated and distributed software systems, Contextual_module SetCat(S:Symbol; D:Symbol)
software development techniques should apply object & //defines the semigroup S over domain D
component oriented design [11] and use flexible n-tier //(with elements in domain D)
SD:=SD U {S} //set of symbols for the
architectures [9] and efficient database management techniques defined set category
[14] for database processing.  expr sD, Canonic(s:SymbolList):=disjoint list
of symbols in lexicographical ordering;
Symbolic implementations of category theory mainly // canonical representation in S
pursue two directions: a constructive approach [8] and a a,bS, S("=",a,b):= Module{
systematic logical approach [7], which enables the definition of n1,n2,i : Integer; eq : Boolean; eq:=True;
categories, domains, operators and their properties mainly for //verifies set equality
automatic proving. n1=Length(a); n2=Length(b);
If (n1<>n2) Return False;
Applying generic techniques in software design in useful Else if (n1==1) Return Dom(" = ",a,b);
both for design reasons and for dealing with specific problems, Else For (i=1; i<=n; if (Dom("<>",a[i],b[i]])
which are more efficiently solved by applying mathematical eq:=False; Return eq; }
models. In this respect, the paper aims at presenting systematic  a,beS, S("<>",a,b):= not S("=",a,b);
implementations of mathematical models in software design  a,beS, S("»",a,b):= Union (a,b);
techniques for processing hierarchical structures and  a,beS, S("*",a,b):= Intersection(a,b);
implementing equivalence algorithms at database level.  a,beS, S("\",a,b):= Complement(a,b);
End Contextual module
Within section 2 we present techniques for implementing
generic algorithms for sorting at database level, while section 3 For a domain Dom with the equality operator "=", Dom("=", a, b)
is dedicated to discussing techniques for representing tests the equality of the two expressions a, b over Dom and if an order
hierarchic structures, data selection & processing techniques. operator "<" is also defined on Dom, the Dom("<", a, b) is used for
verifying over Dom the corresponding relation between two
Section 4 addresses means of implementing simplification and expressions . Operators "<=", ">", ">=", "<>" are used in a similar
equivalence algorithms on data structures based on modular manner over a domain Dom
entities. Conclusions reveal the most important topics
presented in the paper and future research directions A categorical form of a sorting algorithm provides a general
context for performing sorting, over various domains.

978-1-4673-2952-1/12/$31.00 ©2012 IEEE 87


The ordered set category inherits the set category by explicitly The SQL functions “GreaterThan” and “LessThan” on
calling the corresponding module. We also define the operators "<", tuples are similarly implemented, based on the definitions:
">=", ">", ">=" referring to the lexicographical ordering of set (n1,n2) > (m1, m2) œ n1>m1 and n2>m2
elements over the definition domain (see definitions below).
Contextual_module (n1,n2) < (m1, m2) œ n1<m1 and n2<m2 ,
OrderedSet(S:Symbol; Dom:Symbol, op="<") where n1,n2, m1, m2  Integers
// defines an ordered set S over domain D SetCat
(S,Dom); The SQL stored procedure for bubble-sort and counting-
 a,beS, S("Rel",a,b):=Module{ i=1; rel=0; sort algorithms on tuples represented at database level are
n1=Length[a]; n2=Length[b]; nr=Min[n1,n2]; presented in [5] . The temporary table @tbl_manevra used for
While (i<=nr && rel==0) {
If (Dom["<",a[i],b[i]]) rel=-1; retaining auxiliary indexes (IDX column) significantly
If (Dom[">",a[i],b[i]]) rel=1; increases run-times compared to bubble sort – see table 1.
i=i+1; }
If (rel==0 && n1>nr) rel=1; As known, generic layers reduce run-time efficiency [3] but
If (rel==0 && n2>nr) rel=-1; these algorithms model tuple sorting, which is not implicitly
Return rel ; } defined at database level, and therefore has to be generically
 a,beS, S("<",a,b):= S("Rel", a, b)==-1;
extended. We may conclude this section by revealing the
 a,beS, S("<=",a,b):=S("<",a,b) or S("=",a,b);
 a,beS, S(">",a,b):= S("Rel", a, b)==1;
design advantages of the generic approach for modeling
 a,beS, S(">=",a,b):=S(">", a, b) or S("=",a,b); specific contexts; in this respect, we extend the implementation
S("sort", L:list) :=Module{ i,j,n:Integer; of order relations and sorting algorithms at database level.
aux:Symbol; li:List, n=Length[L]; li=L;
for (i=0;i<n;i++)
for (j=i + 1;j<=n;j++) III. HIERARCHICAL STRUCTURES - REPRESENTATION AND
if (Dom (op, li[j], li[i]) PROCESSING PRINCIPLES
{aux=L[i]; li[i]=li[j]; li[j]=aux} Return li;}
S("sort-n", L:list) :=Module{i,j,n:Integer; Hierarchical data structures are often necessary to be
LS:SymbolList; Poz:IntegerList; n=Length[L]; processed in a database; such structures may be retained in
//sorting by counting relational databases by means of ascendant / successor pointers
for (i=0;i<n;i++) { Poz[i]=0;
for (j=0;j<n;j++) if (j<>i) in dedicated tables. Principles for processing hierarchical
if (Dom (op,L[j],L[i]]) Poz[i]++; structures and a comparison of their processing techniques are
LS[i]=L[Poz[i]]; } presented in [4]
return LS; }
End Contextual module These tehchiques are applied within AcademicInfo system
for educational management, that we have designed and
The OrderedSet categorical definition of the sorting algorithm can
be applied, in a parametric manner, for each domain .We applied this implemented [4]. We represented curricula as hierarchical
generic framework in implementing a sorting algorithms for tuples structures [5] based on a modular structure. A curriculum
(pairs), at a database level, on the table Tbl_Optiuni, which retains comprises a set of study modules, where a study module
admission options for our AcademicInfo system (see 3) consists of either a set of disciplines, or a set of other modules.
For each module we retain, in a dedicated table, the identifiers
Tbl_Optiuni [IdSpec, IdOption] of its ascendant module, first descendant module, preceding
The tuple type which models this structure is defined by the module (same tree level) and successor module (same level):
SQL sequence: Tbl_Modules [module_id, ascmodule, descmodule,
USE [EvidscolAdmitere] GO precmodule, succmodule, modulename, …, level,
IF EXISTS (SELECT * FROM sys.types st JOIN
moduletype_id, credit_tot, duration, isLeaf ]
sys.schemas ss ON st.schema_id = ss.schema_id WHERE
st.name = N'TupleTableType' AND ss.name = N'dbo') moduletype_id is mainly used in order to activate or
DROP TYPE TupleTableType GO deactivate the visibility of the module in various reports that
/* Creates the TupleType for retaining (idspec, are generated based on curriculum information.
idoption) */
CREATE TYPE TupleTableType AS TABLE The data selection techniques we have implemented
( [IDSpec] INT NOT NULL evolved from complex and multi-level sub-select views that
, [IDOption] INT NOT NULL) GO
SELECT * FROM sys.table_types contained all descendant modules for each module. Views are
instantly updated by the database engine, being therefore
The equality of tuples is verified by the SQL function: convenient when all selected data is needed. Yet, this technique
USE [EvidscolAdmitere] GO
IF EXISTS (SELECT * FROM sys.objects WHERE is dependant of a maximum number of levels in the
object_id = OBJECT_ID(N'[dbo].[EqualWith]') AND type hierarchical structure that corresponds to the number of JOIN
in (N'FN', N'IF', N'TF', N'FS', N'FT')) operations. We solved this problem by building stored
DROP FUNCTION [dbo].[EqualWith] GO procedures parameterized with the level value, data selection
CREATE FUNCTION [dbo].[EqualWith](
@IDSpec1 AS INT, @IDOption1 AS INT, operation being performed dynamically, in respect with this
@IDSpec2 AS INT, @IDOption2 AS INT ) value. Nevertheless, the database engine has to rebuild
RETURNS BIT AS BEGIN execution plans at each run time both when using views and
DECLARE @ResultVar AS BIT stored procedures, operation which has a certain complexity.
IF (@IDSpec1 = @IDSpec2) AND (@IDOption1 =
@IDOption2) Stored procedures enable efficient data selection based on
SET @ResultVar = 0; -- TRUE
ELSE SET @ResultVar = 1; -- FALSE the module data. Procedures are parameterized as strongly as
RETURN @ResultVar END GO possible for minimizing the volume of selected data.

88
Postorder type n-ary tree evaluation algorithms based on SELECT @rez = MAX (idactivitate) FROM Explorer
RETURN @rez END
the above described tree representation are used in order to
compute the total number of credits for curricula of weighted FCT_SuntDisciplineEchivalente MS SQL function [5]
average grade of students on various modules. Some efficiency verifies if two activities are equivalent by testing if they have
studies we have performed on processing the hierarchical the same canonical representative:
CREATE FUNCTION
structure are also given in [15]. [dbo].[FCT_SuntDisciplineEchivalente] ( @d1 as INT,
@d2 as INT) RETURNS BIT AS BEGIN
The view technique is convenient (fast at the database DECLARE @c1 AS INT DECLARE @c2 AS INT
engine level) for extracting complex data. Still, when DECLARE @r AS BIT
transmitted to client modules, large views may induce SELECT @c1 = [dbo].[FCT_GetCanon] (@d1)
SELECT @c2 = [dbo].[FCT_GetCanon] (@d2)
inefficiency by transferring a large amount of data via the IF (@c1 = @c2 ) OR (@d1 = @d2) BEGIN
network. By replacing such program sequences with strongly SET @r = 1 END
parameterized stored procedures, we succeeded to improve ELSE BEGIN SET @r = 0 END
run-time efficiency with up to 80%. A systematic comparison RETURN @r END
of hierarchic structures processing techniques is given in [4]. We further implement equivalence verification for modules
[5]. Let m1, m2 be two modules that contain distinct activity
representatives. We define the equivalence relations between
IV. IMPLEMENTING EQUIVALENCE ALGORITHMS ON modules as “mapping” (from one module to the other) only
MODULE HIERARCHIC STRUCTURES disciplines that are equivalent. Since in our hierarchic
Within this section we present means of implementing representation, all non-leaf modules consist only of modules,
equivalence algorithms [6] on the above described modular we introduce the following definition for module equivalence:
structures [5] ­(d1  m1 , ! d 2  m2 : d1 ~ d 2 ) š
Didactic entities, named activities (or in particular °(d  m , ! d  m : d ~ d ), IsLeafModule(m )
disciplines) are retained in a dedicated table – Tbl_activitati : m1 | m2 œ °° 2 2 1 1 1 2 1

®
IsActivity(d) := ­®true, d  Tbl _ Activitati [idactivitate] ° (md1  m1 , ! md2  m2 : md1 | md2 ) š
¯ false, otherwise °
°¯ (md2  m2 , ! md1  m1 : md1 | md2 ), otherwise
Disciplines are organized in hierarchies of modules, where
Two leaf modules are equivalent iif all contained
modules are retained in a dedicated table – see previous
disciplines are equivalent; two non-leaf modules are equivalent
section. In our hierarchic representation, all non-leaf modules
iif all contained modules are equivalent. By generically
consist only of modules.
denoting with ‘~’ an equivalence relations for activities or
IsModule(m) := ­®true, m  Tbl _ Modules[idmodul ] modules as above defined, we may state that:
¯ false, otherwise
m1 | m2 œ (d1  m1, ! d2  m2 : d1 ~ d2 ) š
IsLeafModule(m) := ­®true, d  m : IsActivity [d] (d2  m2, ! d1  m1 : d1 ~ d2 )
¯ false, otherwise For a leaf module m, we consider Canonic (m) =
Let d1, d2 be two didactic activities (in particular, {Canonic(d) | dm} – the set of canonical representative for
disciplines). We use the notation ‘~’ for describing the the contained activities / disciplines . It can be shown that two
equivalence of two activities / disciplines d1, d2 in the sense leaf equivalent modules have the same sets of canonical
that they are curricular-equivalent: d1 ~ d2 . module representatives. For a module m we can recursively
‘~’ is an equivalence relation [6] since it verifies compute its canonical representative set as [5]:
reflexivity, symmetry, transitivity properties. Canonic(m) = ­ {Canonic(d ) | d  m}, IsLeafModule[m]
®
The canonical representative of a disciplines’ equivalence ¯ {Canonic(md) | md  m}, otherwise
class will be the ‘newest’ defined discipline i. e. having the Intuitively, the canonical set of a module is obtained by
biggest identifier code. “flattening” its module sub-tree and computing the union set of
We implemented, by means of stored a procedure, the all canonical sets corresponding to its descendant leaf modules.
simplification algorithm for determining the canonical Generically, we may state: Canonic (m) = {Canonic(d) | dm}
representative [6] for a given discipline. Based on this We further implement these hierarchic processings at a
algorithm, we may also test the equivalence of two disciplines database level, by means of stored procedures. We give below
by verifying they have the same canonical representative [5]: the MS SQL function that computes the canonical set of a
module (canonical set of all disciplines belonging to its
CREATE FUNCTION [dbo].[FCT_GetCanon]
(@IDA AS INT ) RETURNS INT AS BEGIN descendant leaf modules).
DECLARE @rez AS INT; CREATE FUNCTION [dbo].[FCT_GetActivitatiEchivModul]
WITH Explorer AS ( (@idModul AS INT)
SELECT idactivitate, idactivitateechiv RETURNS @rez TABLE (idactivitate INT) AS BEGIN
FROM echivalari_discipline -- select leaf modules from the module parent ..
WHERE idactivitate = @IDA UNION ALL WITH Explorer AS(
SELECT M.idactivitate, M.idactivitateechiv SELECT idmodul, legaturaasc, denumiremodul,
FROM echivalari_discipline M INNER JOIN Explorer E nivel, semstudiu
ON M.idactivitate = E.idactivitateechiv )

89
FROM modul WHERE idmodul = @idModul UNION ALL properties in order to increase software proficiency. We also
SELECT M.idmodul, M.legaturaasc,
M.denumiremodul, M.nivel, M.semstudiu
intend to study additional means of processing hierarchical
FROM modul M INNER JOIN Explorer E ON structures and complexity analysis directions with the view to
M.legaturaasc = E.idmodul ) improving software design techniques.
-- select disciplines from leaf modules
INSERT INTO @rez
SELECT ad.idactivitate FROM Explorer mc Acknowledgment We thank the development team in our
INNER JOIN modul_discipline md on IT department for contributing to AcademicInfo’s
mc.idmodul=md.idmodul implementation: Florina Covaci, Gabriel Pop, Monica Bojan,
INNER JOIN activitati_discipline ad ON
md.idactivitate = ad.idactivitate
Carmen Pavel, Kerekes Hunor, Zölde Attila, Kerekes Tünde,
RETURN END Andrei Popescu (and authors).
USP_SuntModuleEchivalente-CuCanonic – see [5] verifies
module equivalence by testing the equality of the canonical REFERENCES
sets. An alternate approach – MS SQL procedure [1] Alina Andreica, D. Stuparu, I..Mantu, “Symbolic Modelling of Database
USP_SuntModule_Echiv, which implements the module Representations”, International Symposium on Symbolic and Numeric
Algorithms for Scientific Computing 2005, IEEE Press, 2005, p 59-62
equivalence definition is also desribed in [5] .
[2] Alina Andreica, Calcul simbolic şi aplicaţii, Dacia, Cluj-Napoca, 2005
A run time benchmark for 20 / 100 activities per module – [3] Alina Andreica,”Implementing Parameterized Type Algorithm
see table 2 – is computed in order to compare stored Definitions in Mathematica”, IEEE Proceedings, Eighth International
procedures for module equivalence by implementing the SYNASC Symposium, 2006, P.35 - 40
definition versus verifying canonical sets; we may obviously [4] Alina Andreica, D Stuparu, C Miu, “Design Techniques in Processing
Hierarchical Structures at Database Level”, Iadis Information Systems
notice that the canonical approach is more efficient 2010 Proc, Porto, Ed: M. Nunes, P. Isaias, P. Powell, 2010, P. 483-488
[5] Alina Andreica, D Stuparu, C Miu, Mathematical Models in Software
V. CONCLUSIONS AND FUTURE WORK Design - Generic Sorting and Equivalence Algorithms, Theory and
Practice in Modern Computing 2012, 17-19 July 2012, Lisbon, Portugal
The categorical and parameterized approach reveals a [6] Bruno Buchberger, R. Loos, Algebraic Simplification, Computing,
general method for defining computational domains: this Suppl. 4, Springer Verlag, p.11-43, 1982
approach is used for implementing generic sorting procedures [7] B. Buchberger, "Automatic Invention and Verification by Lazy
at database level. We reveal the advantages of the approach in Thinking", D. Petcu, V. Negru, D. Zaharie, T. Jebelean Eds, Proceedings
order to deal with specific problems. The novelty of the of SYNASC 2003, p. 2-26
implementation consists in applying generic algorithms at [8] L. Dragan, S. Watt, "Performance Analysis of Generics in Scientific
database level and in modelling discipline and module Computing", SYNASC 2005 International Symposium, JD Cantarella
editor, IEEE Computer Society Press, 2005, p. 93-100
equivalence with equivalence and simplification algorithms.
[9] Client/Server and the N-Tier Model of Distributed Computing,
We also deal with techniques for representing and Micromax Information Services Ltd. 1999
processing hierarchical data structures that are used to [10] Dhawan P., .NET Remoting Performance, Building Distributed
implement simplification algorithms at database level and Applications with MS .NET, MS Press, 2002
addresses efficiency issues in this respect. The method we [11] Gamma E, Helm R, Johnson R, Vlissides J, Design Patterns, Teora, 2002
propose addresses means of implementing simplification and [12] Larson B., Delivering Business Intelligence with Microsoft SQL Server
equivalence algorithms on data structures based on modular 2005, McGraw-Hill/Osborne, 2006
entities. We reveal the advantages of applying the algebraic [13] MCAD/MCSD Training Kit: Developing XML Web Services & Server
equivalence model at a database level and of applying Components with MS Visual Basic .NET and MS Visual C# .NET
Editor, MS Press, Redmond, 2003
canonical representatives’ properties. Case studies are
[14] Ramakrishnan, R., Database Management Systems, 3rd edition, New
performed on our AcademicInfo information system that we York: McGraw-Hill, 2002
have designed and implemented [15] D. Stuparu, Alina Andreica, I.Mantu, “Comparing Access Techniques
Future work is related to refining techniques of software on Databases in Distributed Application Frameworks”, Collaborative
Support Systems in Business and Education, Cluj-Napoca, 2005, p 1-10
design by implementing specific mathematical models and

TABLE I. RUN TIME BENCHMARK FOR GENERIC SORTING ALGORITHMS

Generic algorithm BubbleSort CountingSort


100 tuples 0.851 sec 1.042 sec
1000 tuples 65.305 sec 272.456 sec
10000 tuples Exponential – 1624.521 sec Exponential – 8743.947 sec

TABLE II. RUN TIME BENCHMARK FOR EQUIVALENCE PROCEDURES

Run time (ms) for 10.000 Run time (ms) for 10.000
Procedure modules, 20 disciplines / module, ~100 disciplines /
compared module compared module
USP_SuntModule_Echiv 28895 136941
USP_SuntModuleEchivalente-CuCanonic 1394 1717

90

You might also like