Vous êtes sur la page 1sur 8

EXERCICES (correction)

Modélisation, Types génériques et polymorphisme


Table of contents
1 Exercice 1...........................................................................................................................2
2 Exercice 2...........................................................................................................................3
3 Exercice 3...........................................................................................................................5

Copyright © 2004 Jacques Lemordant and all. All rights reserved.


EXERCICES (correction)

1. Exercice 1
On demande de modéliser par un schéma XML un jeu de n dés à nf faces, le modèle gardant
un historique de tous les lancers de dés.

Jeu de dés
1. Donner le diagramme UML du modèle.

UML
2. Ecrire le schéma XML.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="des" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="des">
<xs:element name="jeu" type="Jeu" />
<xs:complexType name="Jeu">
<xs:sequence>
<xs:element name="de" maxOccurs="unbounded" type="De" />
</xs:sequence>
<xs:attribute name="nf" type="xs:int" />
</xs:complexType>
<xs:complexType name="De">
<xs:sequence>
<xs:element name="h" maxOccurs="unbounded" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:schema>
3. Ecrire un document XML valide.
<?xml version="1.0" encoding="UTF-8"?>
<jeu xmlns="des" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

Page 2
Copyright © 2004 Jacques Lemordant and all. All rights reserved.
EXERCICES (correction)

xsi:schemaLocation="des ../xsd/des.xsd " nf="6">


<de>
<h>1</h>
<h>4</h>
<h>2</h>
</de>
<de>
<h>1</h>
<h>3</h>
<h>2</h>
</de>
</jeu>
4. Montrez comme on peut parcourir l'arbre de contenu Java depuis la racine pour obtenir la
valeur de l'historique correspondant au deuxième lancer du deuxième dé.
Note:
Utiliser les types génériques (consulter le paragraphe Pattern Observer)

DocumentRoot doc = readDocSimple("de.xml");


Jeu j = doc.getJeu();
EList l = j.getDe();
List<De> ld = (List<De>)l;
De[]tab = ld.toArray(new De[1]);
List<Integer> li = (List<Integer>)tab[1].getH();
System.out.println(li.get(1));

2. Exercice 2
On demande de modéliser un système de menus en cascade:
1. Donner le diagramme UML du modèle.

UML

Page 3
Copyright © 2004 Jacques Lemordant and all. All rights reserved.
EXERCICES (correction)

2. Ecrire le schéma XML.


<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="cascade"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="cascade" elementFormDefault="qualified">
<xsd:element name="mainMenu" type="Action" />
<xsd:complexType name="Action" abstract="true">
<xsd:attribute name="nom" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="ActionTerminale">
<xsd:complexContent>
<xsd:extension base="Action">
<xsd:attribute name="handler" type="xsd:string" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="Menu">
<xsd:complexContent>
<xsd:extension base="Action">
<xsd:sequence>
<xsd:element name="m" maxOccurs="unbounded" type="Action"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>
3. Ecrire un document XML valide correspondant à la figure ci-dessous

Menus en cascade

Page 4
Copyright © 2004 Jacques Lemordant and all. All rights reserved.
EXERCICES (correction)

<?xml version="1.0" encoding="UTF-8"?>


<mainMenu xmlns="cascade"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="cascade ../xsd/menu.xsd " nom="MainMenu"
xsi:type="Menu">
<m xsi:type="Menu" nom="Shops">
<m xsi:type="ActionTerminale" nom="Automotives"
handler="ActionAutomotives" />
<m xsi:type="ActionTerminale" nom="Books" handler="ActionBooks" />
<m xsi:type="ActionTerminale" nom="Fashions"
handler="ActionFashions" />
</m>
<m xsi:type="Menu" nom="Restaurants">
<m xsi:type="ActionTerminale" nom="Bakeries"
handler="ActionAutomotives" />
<m xsi:type="ActionTerminale" nom="Coffee Shop"
handler="ActionCoffeeShop" />
<m xsi:type="ActionTerminale" nom="Diner" handler="ActionDiner" />
</m>
<m xsi:type="Menu" nom="Attractions">
<m xsi:type="ActionTerminale" nom="Museums"
handler="ActionMuseums" />
<m xsi:type="ActionTerminale" nom="Art Galleries"
handler="ActionArtGalleries" />
<m xsi:type="ActionTerminale" nom="Exhibitions"
handler="ActionExhibitions" />
</m>
</mainMenu>
4. Montrez comme on peut parcourir l'arbre de contenu Java depuis la racine pour atteindre
l'action Museums.
Note:
Utiliser les types génériques (consulter le paragraphe Pattern Observer)

DocumentRoot doc = readDocSimple("menuCascade.xml");


Menu mainMenu =(Menu) doc.getMainMenu();
List<Menu> lmainMenu =(List<Menu>) mainMenu.getM();
Menu attraction = lmainMenu.get(2);
List<ActionTerminale> lattraction =(List<ActionTerminale>)
attraction.getM();;
System.out.println(lattraction.get(0).getNom());

3. Exercice 3

Page 5
Copyright © 2004 Jacques Lemordant and all. All rights reserved.
EXERCICES (correction)

Maillage hierarchique
On utilise la pattern composant/container pour construire le modèle:

UML

Pattern Composite

le schéma XML est le suivant:


xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="geom" elementFormDefault="qualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="geom">

Page 6
Copyright © 2004 Jacques Lemordant and all. All rights reserved.
EXERCICES (correction)

<xsd:element name="representation" type="XMLRepresentation" />


<xsd:complexType name="XMLRepresentation">
<xsd:sequence>
<xsd:element name="m" type="Composant" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Composant" abstract="true">
<xsd:attribute name="id" type="xsd:unsignedInt" />
</xsd:complexType>
<xsd:complexType name="Container">
<xsd:complexContent>
<xsd:extension base="Composant">
<xsd:sequence>
<xsd:element name="m" type="Composant" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="Polygone">
<xsd:complexContent>
<xsd:extension base="Composant">
<xsd:attribute name="sommets" type="IntList" use="optional" />
<xsd:attribute name="couleur" type="FloatList" use="optional" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:simpleType name="IntList">
<xsd:list itemType="xsd:int" />
</xsd:simpleType>
<xsd:simpleType name="FloatList">
<xsd:list itemType="xsd:float" />
</xsd:simpleType>
</xsd:schema>

le document XML correspondant à la figure est:


xml version="1.0" encoding="UTF-8"?>
<representation xmlns="geom"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="geom ../xsd/maillage.xsd ">
<m id="0" xsi:type="Container">
<m xsi:type="Polygone" id="1" sommets="1 6 7" couleur="0.3 0.4 0.1" />
<m xsi:type="Polygone" id="2" sommets="1 7 2" couleur="0.2 0.7 0.1" />
<m xsi:type="Polygone" id="3" sommets="2 7 3" couleur="0.3 0.3 0.8" />
<m xsi:type="Polygone" id="4" sommets="3 7 9" couleur="0.3 0.4 0.1" />
<m xsi:type="Polygone" id="5" sommets="3 9 4" couleur="0.2 0.7 0.1" />

Page 7
Copyright © 2004 Jacques Lemordant and all. All rights reserved.
EXERCICES (correction)

<m xsi:type="Polygone" id="6" sommets="4 9 10" couleur="0.6 0.3 0.8" />


<m xsi:type="Polygone" id="7" sommets="4 10 5" couleur="0.3 0.5 0.3" />
<m xsi:type="Polygone" id="8" sommets="5 10 11 6" couleur="0.8 0.2 0.1"
/>
<m xsi:type="Polygone" id="9" sommets="6 11 7" couleur="0.9 0.9 0.1" />
<m id="1" xsi:type="Container">
<m xsi:type="Polygone" id="1" sommets="7 12 8" couleur="0.1 0.0 0.1" />
<m xsi:type="Polygone" id="2" sommets="8 12 9" couleur="0.0 0.7 0.1" />
<m xsi:type="Polygone" id="3" sommets="9 12 10" couleur="0.3 0.1 0.8" />
<m xsi:type="Polygone" id="4" sommets="10 12 11" couleur="0.0 0.0 0.1"
/>
<m xsi:type="Polygone" id="5" sommets="11 12 7" couleur="0.0 0.0 0.8" />
</m>
</m>
</representation>

Page 8
Copyright © 2004 Jacques Lemordant and all. All rights reserved.