19 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
# |
# |
21 |
# |
# |
22 |
#Fichero: tema.py |
#Fichero: tema.py |
23 |
|
|
24 |
"""Clase Tema.""" |
"""Clase Tema.""" |
25 |
|
|
26 |
|
|
27 |
from xml.dom import minidom |
from xml.dom import minidom |
28 |
import random |
import random |
29 |
import sys |
import sys |
30 |
import string |
import string |
31 |
import os |
import os |
32 |
|
|
33 |
from utils import * |
from utils import * |
34 |
from bloquedialogo import * |
from bloquedialogo import * |
35 |
from item import * |
from item import * |
36 |
|
|
37 |
class Tema(BloqueDialogo): |
class Tema(BloqueDialogo): |
38 |
"""Objeto sobre el que puede indagar el jugador en la conversacion con un PNJ. |
"""Objeto sobre el que puede indagar el jugador en la conversacion con un PNJ. |
39 |
|
|
40 |
Contiene los siguientes atributos: |
Contiene los siguientes atributos: |
41 |
- _contexto |
- _contexto |
42 |
- _nombre(string) |
- _nombre(string) |
43 |
- _sinonimos([string]): nombres alternativos del mismo tema |
- _sinonimos([string]): nombres alternativos del mismo tema |
44 |
- _items: items de texto que componen el tema |
- _items: items de texto que componen el tema |
45 |
|
|
46 |
Invariantes: |
Invariantes: |
47 |
_contexto != None |
_contexto != None |
48 |
_nombre != None |
_nombre != None |
49 |
_sinonimos != None |
_sinonimos != None |
50 |
_items != None & len(_items) > 0 |
_items != None & len(_items) > 0 |
51 |
""" |
""" |
52 |
|
|
53 |
|
|
54 |
def __init__(self, tema_xml, contexto): |
def __init__(self, tema_xml, contexto): |
55 |
"""Crea un tema nuevo a partir de un arbol XML de descripción de tema. |
"""Crea un tema nuevo a partir de un arbol XML de descripción de tema. |
56 |
|
|
57 |
Requiere: |
Requiere: |
58 |
tema_xml != None |
tema_xml != None |
59 |
tema_xml corresponde con un nodo DOM de tema |
tema_xml corresponde con un nodo DOM de tema |
60 |
contexto != None |
contexto != None |
61 |
""" |
""" |
62 |
self._contexto = contexto |
self._contexto = contexto |
63 |
self._nombre = tema_xml.getAttribute("nombre").encode('ISO-8859-1') |
self._nombre = tema_xml.getAttribute("nombre").encode('ISO-8859-1') |
64 |
self._items = self._parsear_items(tema_xml) |
self._items = self._parsear_items(tema_xml) |
65 |
if not self._items: |
if not self._items: |
66 |
raise ValueError("Tema " + self._nombre + " sin items." ) |
raise ValueError("Tema " + self._nombre + " sin items." ) |
67 |
self._sinonimos = self._parsear_sinonimos(tema_xml) |
self._sinonimos = self._parsear_sinonimos(tema_xml) |
68 |
# print "Tema: " + self._nombre |
# print "Tema: " + self._nombre |
69 |
|
|
70 |
def nombre(self): |
def nombre(self): |
71 |
return self._nombre |
return self._nombre |
72 |
|
|
73 |
def agotado_en(self, visita): |
def agotado_en(self, visita): |
74 |
"""Devuelve cierto si todos los items están visitados o inactivos en esta visita""" |
"""Devuelve cierto si todos los items están visitados o inactivos en esta visita""" |
78 |
if item.satisfecho_en(visita) and (visita._item_repeticiones(item)!= 0 or not item in visita.items()): |
if item.satisfecho_en(visita) and (visita._item_repeticiones(item)!= 0 or not item in visita.items()): |
79 |
return 0 |
return 0 |
80 |
return 1 |
return 1 |
81 |
|
|
82 |
def items(self): |
def items(self): |
83 |
return self._items |
return self._items |
84 |
|
|
85 |
def contexto(self): |
def contexto(self): |
86 |
return self._contexto |
return self._contexto |
87 |
|
|
88 |
def es_sinonimo(self, nombre): |
def es_sinonimo(self, nombre): |
89 |
"""Devuelve cierto si el nombre del tema está en la lista de sinónimos.""" |
"""Devuelve cierto si el nombre del tema está en la lista de sinónimos.""" |
90 |
return nombre in self._sinonimos |
return nombre in self._sinonimos |
91 |
|
|
92 |
def __str__(self): |
def __str__(self): |
93 |
texto = '\t<Tema - "' + self.nombre() |
texto = '\t<Tema - "' + self.nombre() |
94 |
texto = texto + '"\n\t\tSinonimos:\n' |
texto = texto + '"\n\t\tSinonimos:\n' |
95 |
for sinonimo in self._sinonimos: |
for sinonimo in self._sinonimos: |
96 |
texto = texto + "\t\t\t" + str(sinonimo) + "\n" |
texto = texto + "\t\t\t" + str(sinonimo) + "\n" |
97 |
texto = texto + "\t\tItems: \n" |
texto = texto + "\t\tItems: \n" |
98 |
for item in self.items(): |
for item in self.items(): |
99 |
texto = texto + str(item) |
texto = texto + str(item) |
100 |
texto = texto + "\t/>\n" |
texto = texto + "\t/>\n" |
101 |
return texto |
return texto |
102 |
|
|
103 |
class TemaEspecial(Tema): |
class TemaEspecial(Tema): |
104 |
"""Tema de tipo agotado, offtopic o no encontrado. |
"""Tema de tipo agotado, offtopic o no encontrado. |
105 |
|
|
106 |
Invariantes: |
Invariantes: |
107 |
_nombre = el nombre del anterior tema del que se haya hablado |
_nombre = el nombre del anterior tema del que se haya hablado |
108 |
""" |
""" |
109 |
def __init__(self, tema_xml, tipo): |
def __init__(self, tema_xml, tipo): |
110 |
self._tipo = tipo |
self._tipo = tipo |
111 |
self._items = self._parsear_items(tema_xml) |
self._items = self._parsear_items(tema_xml) |
112 |
self._nombre = "el tiempo" # Tema por defecto |
self._nombre = "el tiempo" # Tema por defecto |
113 |
# print "Tema especial de tipo: " + self._tipo |
# print "Tema especial de tipo: " + self._tipo |
114 |
|
|
115 |
def nombre(self): |
def nombre(self): |
116 |
return self._nombre |
return self._nombre |
117 |
|
|
136 |
texto = texto + str(item) |
texto = texto + str(item) |
137 |
texto = texto + "\t/>\n" |
texto = texto + "\t/>\n" |
138 |
return texto |
return texto |
139 |
|
|
140 |
# Código para pruebas unitarias del módulo. |
# Código para pruebas unitarias del módulo. |
141 |
|
|
142 |
if (__name__ == '__main__'): |
if (__name__ == '__main__'): |
143 |
archivo = os.path.join(BloqueDialogo.DIR_DESC_DIALOGOS, "ejemplo", "ejemplo-hablar" + BloqueDialogo.EXT_DESC_DIALOGOS) |
archivo = os.path.join(BloqueDialogo.DIR_DESC_DIALOGOS, "ejemplo", "ejemplo-hablar" + BloqueDialogo.EXT_DESC_DIALOGOS) |
144 |
print archivo |
print archivo |
145 |
stream = open(archivo) |
stream = open(archivo) |
146 |
doc_xml = minidom.parse(stream).documentElement |
doc_xml = minidom.parse(stream).documentElement |
147 |
aux = doc_xml.getElementsByTagName("tema")[0] |
aux = doc_xml.getElementsByTagName("tema")[0] |
148 |
|
|
149 |
tema = Tema(aux, None) |
tema = Tema(aux, None) |
150 |
stream.close() |
stream.close() |
151 |
|
|
152 |
print "Tema cargado correctamente.\n" + str(tema) # + str(tema._sinonimos) |
print "Tema cargado correctamente.\n" + str(tema) # + str(tema._sinonimos) |
153 |
|
|