The Benefits Of A General Template

Templates can facilitate the development of software construction. The general template shown in Figure 10-4 represents a template for building a new water-balance or irrigation-scheduling model. It represents the common classes used for these types of models and their relationships. Each of the classes of the template can be populated with attributes and methods needed by the particular approach used for model development, as shown in Figures 10-5 and 10-6.

In addition to the documentation provided by the class diagram, UML tools usually include methods for creating complementary documentation. The software used allowed users to create extra documentation for each component of the class diagram: The relationships, classes, class attributes, or class methods. This extra documentation allows the developer to explain the meaning and the role of each of these in the system. Attributes can be defined and equations used in the calculation of processes can be presented, for example, in pseudo-code. That is, equations can be presented in a format similar to that used in papers, books, etc. It need not be written in a specific programming language. An example of documentation for a method is shown in Figure 10-7.

The general template could easily be extended to accommodate other agricultural and environmental models. For example, models of crop nutrient uptake rely on the same basic system elements as soil water-balance models. They could be represented with little or no modification of the template, perhaps requiring a class to represent additional management practices such as fertilizer management. Ultimately, the template could be extended to include numerous classes representing all aspects of such systems.

From these templates, UMLs forward-engineering capabilities can be used to generate skeleton programming code, which can form the basis of the final code implementation. Usually, UML tools provide a means of translating diagrams into several implementation languages such as Java, Visual Basic, C++, etc. Rational Rose (http://rational.com) provides capabilities for code generation in a variety of languages such Java, Visual C++, Visual Basic, ADA83, ADA95, CORBA, and XMLJDTD. This capability greatly simplifies the final software development process. Figure 10-8 shows an example of automatic code generation in Java for class Cell Figure 10-9 shows an example of automatic code generation in Visual Basic for class SoilLayer, and Figure 10-10 shows an example of code generation for class SoilPlantAtmosphere. In Figure 10-10, the code generation includes the relationships class SoilPlantAtmosphere has with classes Plant and Weather.

K 0 SoilLayer : saturation fieldCapacity permanentwiltingPoint : depth rootWeightingFactor ; initialMoistureContent ^ SoilLayer ^ calculateDrainage ^ calculateM oistureContent - ^ calculateEvaporation ■ ^ calculatelnfiltration ^ calculates oilM oistureH oldinc ^ calculatellpwardsVaterFlow ^ calculateAvailableWater & theSoilProfile (SoilProfile) it; HI SoilPlanfcAtmosphere ; El SoilProfile :t; El Weather :+! Associations it; CD Component View ;

|dfj Deployment View f&q Model Properties v

This method calculates the current soil volumetric moisture content. The current value is dependent on the previous value (the moisture content on the previous day). It is used by method calculates oilM oistureH oldingCapacity(J.

It is calculated prior to and then again following drainage of water out of the layer.

If prior to drainage then calculateM oistureContent(currentD ay) -calculateM oistureContentjcurrentD ay) • calculateDrainageQ

_SoilLayer

^»saturation ^fieldCapacity <%>perm anentwiltingPoint depth

^>root Wei ghft ing Factor ini ti a! Mo ist ure Co ntent

^So il Layer (} ^calculateDrainageQ ^calculateMoistureContentO ^ c a le ulat e Evap orat io n () ^calculai elnfiltrationf) ^caiculateSoilMoistureHoldingcapacityO ^calc ulat eUpwardWatefFI ow() ^calculai e Ava i I a bl e W at e i()

Figure 10-7. Example of documentation provided for a method.

Class diagrams created with UML and Rational Rose (other software provide publishing capabilities as well) can be used to create well-organized and easily understandable documentation, which can be published on the Web. Publishing the diagrams on the Web allows developers from different groups, in different locations, to easily coordinate their efforts. Additionally, because UML diagrams use plain English and can be understood by programmers and non-programmers alike, they can facilitate collaboration between these groups. Model-specific documentation for existing and future models can be created using the methodology presented and can aid in model evaluation. The template developed can be used to represent various models regardless of the approach used in the model, using a common set of visual elements. Both single and multi-layer approaches to modeling changes in soil water content could be described using the same UML diagram and the template has the potential to be expanded to represent other soil-crop system models, such as nutrition models. In addition to documenting the underlying science of the models represented, the template outlines the structure of the code organization and can be used to build new models. Generating skeleton code from class diagrams, using UML tools, can greatly simplify code implementation. Models could be implemented in different programming languages, allowing programmers to choose the best environment according to their specific architectural needs or to update models in newer programming languages. The template could be used to facilitate model maintenance and modification, allowing the programmer to easily locate where changes must be made. Subsequent reuse of code could also be simplified. The following figures show examples of code generation in Visual Basic and Java using the same class diagram.

1 public class Cell {

2 private int runoffCurveNumberSCS;

3 private double soilSurfaceAlbedo;

4 private double UmitFirstStageSoilEvaporation;

5 private SoilProfile theSoilprofile;

6 public Weather the Weather;

7 public Plant thePlant;

8 public SoilPlantAtmosphere theSoilPlantAtmosphere;

9 public CellO {}

10 public void calculateRunoff() {}

11 public void calculateInfiltration() {}

12 public void calculateAlbedo() {}

Figure 10-8. Example of code generation in Java.

iSiE'te Edit 5dew Eroject Fgrroat Debug Sun Qy«? D.agfam lools &dcf-Ins fflndow Uetp

IbIBpHP

iSiE'te Edit 5dew Eroject Fgrroat Debug Sun Qy«? D.agfam lools &dcf-Ins fflndow Uetp

Option Explicit

' # tHecle 11 dM0BEMhOO:.m Private saturation As Variant ' # $K<> tie 11 dr ' 4 0 9E A BB7 012 7 Private fialdCapacity As Variant

* * fModeI id-40-EABBB0iFD

Private permanentWiltmgPomt A« Variant

Private depth As Variant

Private rootWeightingFactor As Variant ' # tMa<11d1-409KA.BE6002A

Private initilaMoistureContent As Variant

Public Mev/Property As SoilProfile

' # ? He deli d - 4 0 9 MA 9? r, 0391 Public Sub calculateDrainageO End Sub

Public Sub calculateMoistureContent() End Sub

* #fModel Id-409EA99KCC5B

Public Sub caleulateEvaporation(} End Sub

1 Id409EA9A A009A

Public Sub calculatelnfiltration{)

End Sub

' £fMcdelld™409EA93500'i5

Public Sub calculatesoilHoistureHoldingCapacity(} End Sub

' * i#Mode1Id™4 09EA9C90L5 3 Public Sub caXculateUpwardWaterFlow{) End Sub

'*IHcdet rd-40 9KA9KT0* 2K

Public Sub calculateAvailableWater()

End Sub

Figure 10-9. Example of code generation in Visual Basic for class SoilLayer.

(General)

jrJ jc^lculateAviiilabiaWater

In Visual Basic, operations are defined as subroutines starting with the keywords Public Sub and ending with End Sub. Between these words, the user will enter the logic for the operation.

& £ie g<Mt View Project Format OebuQ Run Query Diagram lools &dcMns V^ndow &elp b - Til® h! r.MHl O ^ I ► M almm «

pGeneral) ^ jrj j {declarations}

0 0

Post a comment