Presentation is loading. Please wait.

Presentation is loading. Please wait.

Declarative Languages With XML Michael D. Thomas

Similar presentations


Presentation on theme: "Declarative Languages With XML Michael D. Thomas"— Presentation transcript:

1 Declarative Languages With XML Michael D. Thomas mdthomas@ibiblio.org

2 What Are We Talking About? XML Is Very Popular For Configuration Files Configuration Files Manifest Declarative Languages Also, Configuration Files Often Manifest App. Domain Specific “Mini-Languages” So….

3 What Are We Talking About? XML configuration files can be a powerful keystone of loosely-coupled, highly configurable software architectures. A solid understanding of declarative & domain-specific “mini-languages” can help you develop better systems. When creating languages with XML, we need to grasp the pros vs. cons of using XML.

4 Agenda Opening Comments Declarative Language Overview Declarative XML Architectural Context Declarative XML Techniques

5 XML Is… XML is…. A Meta-Language A Tree-Based Data Structure An Interoperability-Promoting Standard Web Services Thingy Good Resume Fodder

6 XML As Meta-Language Syntax Defined Structure Defined Parsers & APIs Already Implemented Typing Already Defined (XSD, Others) Focus On Semantics

7 Different Types Of Languages Imperative Languages Object-Oriented Languages Procedural Languages Declarative Languages

8 Imperative Languages What we think of as “programming” Algorithmic approach Compute values, assign to memory locations Java/C++: Carry on the tradition Procedural & OO languages encapsulate low-level operations Provides execution efficiency

9 Declarative Languages “What” vs. “How” Programs provide “What” is required by the specification Imperative languages focus on “How” the solution is implemented Declarative languages focus on “What”

10 Declarative vs. Imperative Declarative: code the specification directly – the computer is an implementation detail Imperative: solve problems on computer’s terms – use sub-routines, OO & garbage collection to make life easier for humans Both Of Value Today’s goal: how to leverage both approaches in the same app. using XML

11 Contrasting Approaches Swing GUI vs. XHTML GUI SQL join vs. programmatic join

12 XHTML GUI Hello, World!

13 Swing GUI TextField field = new TextField(“Hello, World”); int face = Font.BOLD | Font.ITALIC; t.setFont(new Font("TimesRoman", face, 16)); panel.add(field);

14 Software Development: Small Projects On Small Projects, Life Is Simple….

15 Software Development: Large Projects

16 Declarative Language Interface

17 SQL Very successful approach SQL statements are expressions of relational algebra and calculus (Dr E.F. Codd, 1970) Allows developers to think spatially about problems

18 SQL SELECT ename, dname FROM emp, dept WHERE emp.deptno = dept.deptno

19 SQL: Algorithmic Approach Take the Cartesian product of the two lists Apply the condition to each row If the condition is met, select the interesting data items from that row Add data items to list to return

20 Imperative Approach To SQL public SortedMap intersectMaps(SortedMap seq_1, SortedMap seq_2, Condition condition) { TreeMap intersectedMap = new TreeMap(); Iterator it_1 = seq_1.values().iterator(); while (it_1.hasNext()) { Joinable joinable_1 = (Joinable)it_1.next(); Iterator it_2 = seq_2.values().iterator(); while (it_2.hasNext()) { Joinable joinable_2 = (Joinable)it_2.next(); if (condition.evaluate(joinable_1, joinable_2)) { Joinable joinedObject = condition.mergeJoinables(joinable_1, joinable_2); intersectedMap.put(joinedObject.getKey(),joinedObject); } } } return intersectedMap; }

21 Imperative Approach To SQL public SortedMap unionMaps(SortedMap seq1, SortedMap seq2, Condition condition) { //implementation } public SortedMap reduceMap(SortedMap seq, Condition condition) { //implementation } public SortedMap inverseMap(SortedMap seq, Condition condition) { //implementation } public SortedMap sortMap(SortedMap list1, Comparator comp) { //implementation }

22 Imperative Approach To SQL public interface Condition { public boolean evaluate(Joinable joinable_1, Joinable joinable_2); public Joinable mergeJoinables(Joinable joinable_1, Joinable joinable_2); } public interface Joinable { public Object getKey(); }

23 Declarative XML As User Interface Joiner joiner = new Joiner(); Condition condition1 = new SalGreaterThan(“500”); Condition condition2 = new DeptNoJoin(); SortedMap salFilter = joiner.reduceMap(empTable, condition1); SortedMap retMap = joiner.intersectMaps(salFilter, deptTable, condition2);

24 What Was That? SELECT ename, dname FROM emp, dept WHERE sal > 500 AND emp.deptno = dept.deptno

25 The O-R Mapping Problem The Object-Relational Mapping Problem Is Often Solved with…. An XML Declarative Language! E.g, a J2EE deployment descriptor (Uses language layering – more on that later)

26 Architectural Context XML is tree-based XML syntax is simple, but can be made overly complex Must map XML languages to underlying object model (e.g., Java reflection) Can layer other languages (e.g., SQL) in to your XML language

27 When To Use Declarative XML

28 XML As User Interface Development teams often forget to write use cases for administrators, downstream developers XML declarative languages must be considered as ways to provide user interfaces Documentation, examples important Usability, Learnability issues important

29 Declarative XML In App. Lifecycle Compile-Time – Best Performance, Least Flexibility Start-Time – XML Parsed Once, Good Performance, Good Flexibility. App must be restarted or manual refresh to get flexibility Run-Time – Can Impact Performance, But Great Flexibility. Web Services Approach

30 Start-Time Configuration

31 Runtime Configuration

32 Language Validation Use XML Schema, DTDs for validation For more complex validation: XSLT transformation Programmatic validation But… Far more important to provide good documentation & examples than to provide good validation

33 Parsing Can use your favorite XML API Make the SAX vs. DOM decision Use binding approach with JAXB

34 Declarative XML Pattern

35 Example Usages Standard app. configuration files HTTP Frameworks Workflow applications Rules engines

36 Techniques Dynamic Binding Imperative vs. Declarative Partitioning Language Layering Mapping Trees Branching Recursion Normalization & Encapsulation

37 Dynamic Binding Declarative XML States An Object Name That Should Be Used Struts Example: <form-bean name = “helloForm” type = “com.mike.MikeActionForm”> Java: Class aFClass = Class.forName(typeAttr); ActionForm aF =(ActionForm)aFClass.newInstance();

38 Dynamic Binding Translation from XML language to object model Pattern: Super-interface that defines an object’s methods to the system, named class in XML defines implementation Using reflection/introspection, can define method calls Careful – can lose the benefits of the strongly typed imperative language

39 Imperative/Declarative Partitioning Some “programming” is in the new declarative language, some in the imperative language Don’t re-invent the wheel! Tempting to put too much complexity in the new declarative language Cost/benefit: Imperative language can provide flexibility through pluggable modules

40 Language Layering Other languages can be layered in to your declarative XML Example: SELECT ename, sal FROM emp WHERE sal > 500 Also, XPath expressions in XSLT Better than re-inventing a language in XML

41 Mapping Trees XML is tree-based, and is very good and mapping trees Any flowchart, activity diagram or state diagram can be described in XML Can use such XML to drive workflow systems XML forces tree representations, but graphs can also be represented

42 Branching Can use the XSLT technique:...... Can also use best-match technique

43 Best match technique Given inputs such as: var1=val1&var2=val2&var3=val3& Can have XML such as: Foo3 is the best match

44 Using Document Order In previous example, could be possible that there could be a tie Document order can resolve ambiguity As with tree structure, document order can provide semantic meaning Careful – canonical form issues can arise

45 Input & Output Processing Business logic should be separated from presentation logic Business logic can also be de-coupled from inputs and outputs

46 Iteration Need for pure iteration is a sign your language is becoming more algorithmic Could be a better domain-specific abstraction to use Don’t forget about recursion

47 Normalization/Encapsulation Any programming language that encapsulates is a better language Don’t make the users of your language repeat themselves For anything more complicated than a couple of attributes, allow a way to re- use XML elements that are definitions

48 Conclusions Declarative Languages are a well established programming paradigm XML can help you create your own powerful languages Doesn’t solve all problems. Pros/cons This presentation exposed you to theoretical basis, historical practice and XML specific techniques


Download ppt "Declarative Languages With XML Michael D. Thomas"

Similar presentations


Ads by Google