Bläddra i källkod

Moving random EM generation methods in a dedicated file

Creating a class RandomEm in EditorialModel/randomem.py
Yann Weber 9 år sedan
förälder
incheckning
ebad5fae54
3 ändrade filer med 179 tillägg och 165 borttagningar
  1. 0
    163
      EditorialModel/model.py
  2. 177
    0
      EditorialModel/randomem.py
  3. 2
    2
      Lodel/management/commands/emgen.py

+ 0
- 163
EditorialModel/model.py Visa fil

@@ -3,9 +3,6 @@
3 3
 ## @file editorialmodel.py
4 4
 # Manage instance of an editorial model
5 5
 
6
-import random
7
-import time
8
-
9 6
 import EditorialModel
10 7
 from EditorialModel.migrationhandler.dummy import DummyMigrationHandler
11 8
 from EditorialModel.backend.dummy_backend import EmBackendDummy
@@ -13,8 +10,6 @@ from EditorialModel.classes import EmClass
13 10
 from EditorialModel.fieldgroups import EmFieldGroup
14 11
 from EditorialModel.fields import EmField
15 12
 from EditorialModel.types import EmType
16
-from EditorialModel.classtypes import EmClassType
17
-from Lodel.utils.mlstring import MlString
18 13
 from EditorialModel.exceptions import EmComponentCheckError, EmComponentNotExistError, MigrationHandlerChangeError
19 14
 import hashlib
20 15
 
@@ -278,161 +273,3 @@ class Model(object):
278 273
         del new_me
279 274
 
280 275
         self.migration_handler = new_mh
281
-
282
-    @classmethod
283
-    ## @brief Generate a random editorial model
284
-    # 
285
-    # The random generator can be tuned with integer parameters
286
-    # that represent probability or maximum numbers of items.
287
-    # The probability (chances) works like 1/x chances to append
288
-    # with x the tunable parameter
289
-    # Tunable generator parameters :
290
-    # - classtype : Chances for a classtype to be empty (default 0)
291
-    # - nclass : Maximum number of classes per classtypes (default 5)
292
-    # - nofg : Chances for a classe to have no fieldgroup associated to it (default 10)
293
-    # - notype : Chances for a classe to have no type associated to it (default 5)
294
-    # - seltype : Chances for a type to select an optionnal field (default 2)
295
-    # - ntypesuperiors : Chances for a type to link with a superiors (default 3)
296
-    # - nofields : Chances for a fieldgroup to be empty (default 10)
297
-    # - nfields : Maximum number of field per fieldgroups (default 8)
298
-    # - rfields : Maximum number of relation_to_type attributes fields (default 5)
299
-    # - optfield : Chances for a field to be optionnal (default 2)
300
-    # @param backend : A backend to use with the new EM
301
-    # @param **kwargs dict : Provide tunable generation parameter
302
-    # @return A randomly generate EM
303
-    def random(cls, backend, **kwargs):
304
-        em = Model(backend)
305
-
306
-        chances = {
307
-            'classtype' : 0, # a class in classtype
308
-            'nclass': 5, #max number of classes per classtype
309
-            'nofg': 10, #no fieldgroup in a class
310
-            'nfg': 5, #max number of fieldgroups per classes
311
-            'notype': 10, # no types in a class
312
-            'ntype': 8,  # max number of types in a class
313
-            'seltype': 2, #chances to select an optional field
314
-            'ntypesuperiors': 2, #chances to link with a superior
315
-            'nofields': 10, # no fields in a fieldgroup
316
-            'nfields' : 8, #max number of fields per fieldgroups
317
-            'rfields': 5,#max number of attributes relation fields
318
-            'optfield': 2, #chances to be optionnal
319
-        }
320
-
321
-        for name,value in kwargs.items():
322
-            if name not in chances:
323
-                #warning
324
-                pass
325
-            else:
326
-                chances[name] = value
327
-
328
-        #classes creation
329
-        for classtype in EmClassType.getall():
330
-            if random.randint(0,chances['classtype']) == 0:
331
-                for _ in range(random.randint(1,chances['nclass'])):
332
-                    cdats = cls._rnd_component_datas()
333
-                    cdats['classtype'] = classtype['name']
334
-                    em.create_component('EmClass', cdats)
335
-
336
-        for emclass in em.classes():
337
-            #fieldgroups creation
338
-            if random.randint(0, chances['nofg']) != 0:
339
-                for _ in range(random.randint(1, chances['nfg'])):
340
-                    fgdats = cls._rnd_component_datas()
341
-                    fgdats['class_id'] = emclass.uid
342
-                    em.create_component('EmFieldGroup', fgdats)
343
-
344
-            #types creation
345
-            if random.randint(0, chances['notype']) != 0:
346
-                for _ in range(random.randint(1, chances['ntype'])):
347
-                    tdats = cls._rnd_component_datas()
348
-                    tdats['class_id'] = emclass.uid
349
-                    em.create_component('EmType', tdats)
350
-
351
-        #random type hierarchy
352
-        for emtype in em.components(EmType):
353
-            possible = emtype.possible_superiors()
354
-            for nat in possible:
355
-                if len(possible[nat]) > 0 and random.randint(0, chances['ntypesuperiors']) == 0:
356
-                    random.shuffle(possible[nat])
357
-                    for i in range(random.randint(1, len(possible[nat]))):
358
-                        emtype.add_superior(possible[nat][i], nat)
359
-
360
-
361
-        #fields creation
362
-        ft_l = EmField.fieldtypes_list()
363
-        for emfg in em.components(EmFieldGroup):
364
-            if random.randint(0, chances['nofields']) != 0:
365
-                for _ in range(random.randint(1, chances['nfields'])):
366
-                    ft = ft_l[random.randint(0,len(ft_l)-1)]
367
-                    fdats = cls._rnd_component_datas()
368
-                    fdats['fieldtype']=ft
369
-                    fdats['fieldgroup_id'] = emfg.uid
370
-                    if ft == 'rel2type':
371
-                        emtypes = em.components(EmType)
372
-                        fdats['rel_to_type_id'] = emtypes[random.randint(0,len(emtypes)-1)].uid
373
-                    if random.randint(0,chances['optfield']) == 0:
374
-                        fdats['optional'] = True
375
-                    em.create_component('EmField', fdats)
376
-
377
-        #relationnal fiels creation
378
-        ft_l = [ ft for ft in EmField.fieldtypes_list() if ft != 'rel2type' ]
379
-        for emrelf in [ f for f in em.components(EmField) if f.ftype == 'rel2type' ]:
380
-            for _ in range(0,chances['rfields']):
381
-                ft = ft_l[random.randint(0, len(ft_l)-1)]
382
-                fdats = cls._rnd_component_datas()
383
-                fdats['fieldtype'] = ft
384
-                fdats['fieldgroup_id'] = emrelf.fieldgroup_id
385
-                if random.randint(0, chances['optfield']) == 0:
386
-                    fdats['optional'] = True
387
-                em.create_component('EmField', fdats)
388
-                
389
-
390
-        #selection optionnal fields
391
-        for emtype in em.components(EmType):
392
-            selectable = [field for fieldgroup in emtype.fieldgroups() for field in fieldgroup.fields() if field.optional ]
393
-            for field in selectable:
394
-                if random.randint(0,chances['seltype']) == 0:
395
-                    emtype.select_field(field)
396
-                    
397
-
398
-        return em
399
-
400
-                
401
-    
402
-    @staticmethod
403
-    ## @brief Generate a random string
404
-    # @warning dirty cache trick with globals()
405
-    # @return a randomly selected string
406
-    def _rnd_str(words_src='/usr/share/dict/words'):
407
-        if '_words' not in globals() or globals()['_words_fname'] != words_src:
408
-            globals()['_words_fname'] = words_src
409
-            with open(words_src, 'r') as fpw:
410
-                globals()['_words'] = [ l.strip() for l in fpw ]
411
-        words = globals()['_words']
412
-        return words[random.randint(0,len(words)-1)]
413
-        
414
-    @classmethod
415
-    ## @brief Generate a random MlString
416
-    # @param nlng : Number of langs in the MlString
417
-    # @return a random MlString with nlng translations
418
-    # @todo use a dict to generated langages
419
-    def _rnd_mlstr(cls, nlng):
420
-        ret = MlString()
421
-        for _ in range(nlng):
422
-            ret.set(cls._rnd_str(), cls._rnd_str())
423
-        return ret
424
-
425
-    @classmethod
426
-    ## @brief returns randomly generated datas for an EmComponent
427
-    # @return a dict with name, string and help_text
428
-    def _rnd_component_datas(cls):
429
-        mlstr_nlang = 2;
430
-        ret = dict()
431
-        ret['name'] = cls._rnd_str()
432
-        ret['string'] = cls._rnd_mlstr(mlstr_nlang)
433
-        ret['help_text'] = cls._rnd_mlstr(mlstr_nlang)
434
-
435
-        return ret
436
-        
437
-        
438
-        

+ 177
- 0
EditorialModel/randomem.py Visa fil

@@ -0,0 +1,177 @@
1
+#-*- coding: utf-8 -*-
2
+
3
+## @package EditorialModel.randomem
4
+#
5
+# Provide methods for random EM generation
6
+
7
+import random
8
+from EditorialModel.backend.dummy_backend import EmBackendDummy
9
+from EditorialModel.model import Model
10
+from EditorialModel.fieldgroups import EmFieldGroup
11
+from EditorialModel.fields import EmField
12
+from EditorialModel.types import EmType
13
+from EditorialModel.classtypes import EmClassType
14
+from Lodel.utils.mlstring import MlString
15
+
16
+
17
+class RandomEm(object):
18
+    
19
+    ## @brief Instanciate a class allowing to generate random EM
20
+    # @see RandomEm::random_em()
21
+    def __init__(self, backend=None, **kwargs):
22
+        self.backend = backend
23
+        self.kwargs = kwargs
24
+
25
+    ## @brief Return a random EM
26
+    # @return A random EM
27
+    def gen(self):
28
+        return self.random_em(self.backend, **self.kwargs)
29
+
30
+    @classmethod
31
+    ## @brief Generate a random editorial model
32
+    #
33
+    # The random generator can be tuned with integer parameters
34
+    # that represent probability or maximum numbers of items.
35
+    # The probability (chances) works like 1/x chances to append
36
+    # with x the tunable parameter
37
+    # Tunable generator parameters :
38
+    # - classtype : Chances for a classtype to be empty (default 0)
39
+    # - nclass : Maximum number of classes per classtypes (default 5)
40
+    # - nofg : Chances for a classe to have no fieldgroup associated to it (default 10)
41
+    # - notype : Chances for a classe to have no type associated to it (default 5)
42
+    # - seltype : Chances for a type to select an optionnal field (default 2)
43
+    # - ntypesuperiors : Chances for a type to link with a superiors (default 3)
44
+    # - nofields : Chances for a fieldgroup to be empty (default 10)
45
+    # - nfields : Maximum number of field per fieldgroups (default 8)
46
+    # - rfields : Maximum number of relation_to_type attributes fields (default 5)
47
+    # - optfield : Chances for a field to be optionnal (default 2)
48
+    # @param backend : A backend to use with the new EM
49
+    # @param **kwargs dict : Provide tunable generation parameter
50
+    # @return A randomly generate EM
51
+    def random_em(cls, backend=None, **kwargs):
52
+        ed_mod = Model(EmBackendDummy if backend is None else backend)
53
+
54
+        chances = {
55
+            'classtype': 0,         # a class in classtype
56
+            'nclass': 5,            # max number of classes per classtype
57
+            'nofg': 10,             # no fieldgroup in a class
58
+            'nfg': 5,               # max number of fieldgroups per classes
59
+            'notype': 10,           # no types in a class
60
+            'ntype': 8,             # max number of types in a class
61
+            'seltype': 2,           # chances to select an optional field
62
+            'ntypesuperiors': 2,    # chances to link with a superior
63
+            'nofields': 10,         # no fields in a fieldgroup
64
+            'nfields': 8,           # max number of fields per fieldgroups
65
+            'rfields': 5,           # max number of attributes relation fields
66
+            'optfield': 2,          # chances to be optionnal
67
+        }
68
+
69
+        for name, value in kwargs.items():
70
+            if name not in chances:
71
+                #warning
72
+                pass
73
+            else:
74
+                chances[name] = value
75
+
76
+        #classes creation
77
+        for classtype in EmClassType.getall():
78
+            if random.randint(0, chances['classtype']) == 0:
79
+                for _ in range(random.randint(1, chances['nclass'])):
80
+                    cdats = cls._rnd_component_datas()
81
+                    cdats['classtype'] = classtype['name']
82
+                    ed_mod.create_component('EmClass', cdats)
83
+
84
+        for emclass in ed_mod.classes():
85
+            #fieldgroups creation
86
+            if random.randint(0, chances['nofg']) != 0:
87
+                for _ in range(random.randint(1, chances['nfg'])):
88
+                    fgdats = cls._rnd_component_datas()
89
+                    fgdats['class_id'] = emclass.uid
90
+                    ed_mod.create_component('EmFieldGroup', fgdats)
91
+
92
+            #types creation
93
+            if random.randint(0, chances['notype']) != 0:
94
+                for _ in range(random.randint(1, chances['ntype'])):
95
+                    tdats = cls._rnd_component_datas()
96
+                    tdats['class_id'] = emclass.uid
97
+                    ed_mod.create_component('EmType', tdats)
98
+
99
+        #random type hierarchy
100
+        for emtype in ed_mod.components(EmType):
101
+            possible = emtype.possible_superiors()
102
+            for nat in possible:
103
+                if len(possible[nat]) > 0 and random.randint(0, chances['ntypesuperiors']) == 0:
104
+                    random.shuffle(possible[nat])
105
+                    for i in range(random.randint(1, len(possible[nat]))):
106
+                        emtype.add_superior(possible[nat][i], nat)
107
+
108
+        #fields creation
109
+        ft_l = EmField.fieldtypes_list()
110
+        for emfg in ed_mod.components(EmFieldGroup):
111
+            if random.randint(0, chances['nofields']) != 0:
112
+                for _ in range(random.randint(1, chances['nfields'])):
113
+                    field_type = ft_l[random.randint(0, len(ft_l) - 1)]
114
+                    fdats = cls._rnd_component_datas()
115
+                    fdats['fieldtype'] = field_type
116
+                    fdats['fieldgroup_id'] = emfg.uid
117
+                    if field_type == 'rel2type':
118
+                        emtypes = ed_mod.components(EmType)
119
+                        fdats['rel_to_type_id'] = emtypes[random.randint(0, len(emtypes) - 1)].uid
120
+                    if random.randint(0, chances['optfield']) == 0:
121
+                        fdats['optional'] = True
122
+                    ed_mod.create_component('EmField', fdats)
123
+
124
+        #relationnal fiels creation
125
+        ft_l = [field_type for field_type in EmField.fieldtypes_list() if field_type != 'rel2type']
126
+        for emrelf in [f for f in ed_mod.components(EmField) if f.ftype == 'rel2type']:
127
+            for _ in range(0, chances['rfields']):
128
+                field_type = ft_l[random.randint(0, len(ft_l) - 1)]
129
+                fdats = cls._rnd_component_datas()
130
+                fdats['fieldtype'] = field_type
131
+                fdats['fieldgroup_id'] = emrelf.fieldgroup_id
132
+                if random.randint(0, chances['optfield']) == 0:
133
+                    fdats['optional'] = True
134
+                ed_mod.create_component('EmField', fdats)
135
+
136
+        #selection optionnal fields
137
+        for emtype in ed_mod.components(EmType):
138
+            selectable = [field for fieldgroup in emtype.fieldgroups() for field in fieldgroup.fields() if field.optional]
139
+            for field in selectable:
140
+                if random.randint(0, chances['seltype']) == 0:
141
+                    emtype.select_field(field)
142
+        return ed_mod
143
+
144
+    @staticmethod
145
+    ## @brief Generate a random string
146
+    # @warning dirty cache trick with globals()
147
+    # @return a randomly selected string
148
+    def _rnd_str(words_src='/usr/share/dict/words'):
149
+        if '_words' not in globals() or globals()['_words_fname'] != words_src:
150
+            globals()['_words_fname'] = words_src
151
+            with open(words_src, 'r') as fpw:
152
+                globals()['_words'] = [l.strip() for l in fpw]
153
+        words = globals()['_words']
154
+        return words[random.randint(0, len(words) - 1)]
155
+
156
+    @classmethod
157
+    ## @brief Generate a random MlString
158
+    # @param nlng : Number of langs in the MlString
159
+    # @return a random MlString with nlng translations
160
+    # @todo use a dict to generated langages
161
+    def _rnd_mlstr(cls, nlng):
162
+        ret = MlString()
163
+        for _ in range(nlng):
164
+            ret.set(cls._rnd_str(), cls._rnd_str())
165
+        return ret
166
+
167
+    @classmethod
168
+    ## @brief returns randomly generated datas for an EmComponent
169
+    # @return a dict with name, string and help_text
170
+    def _rnd_component_datas(cls):
171
+        mlstr_nlang = 2
172
+        ret = dict()
173
+        ret['name'] = cls._rnd_str()
174
+        ret['string'] = cls._rnd_mlstr(mlstr_nlang)
175
+        ret['help_text'] = cls._rnd_mlstr(mlstr_nlang)
176
+
177
+        return ret

+ 2
- 2
Lodel/management/commands/emgen.py Visa fil

@@ -1,6 +1,6 @@
1 1
 from django.core.management.base import BaseCommand, CommandError
2 2
 from optparse import make_option
3
-from EditorialModel.model import Model
3
+from EditorialModel.randomem import RandomEm
4 4
 from EditorialModel.backend.json_backend import EmBackendJson
5 5
 from EditorialModel.backend.dummy_backend import EmBackendDummy
6 6
 from EditorialModel.backend.graphviz import EmBackendGraphviz
@@ -127,7 +127,7 @@ class Command(BaseCommand):
127 127
         
128 128
         bj = EmBackendJson(options['output'])
129 129
 
130
-        em = Model.random(EmBackendDummy())
130
+        em = RandomEm.random_em(EmBackendDummy())
131 131
         bj.save(em)
132 132
 
133 133
         if options['dotout']:

Loading…
Avbryt
Spara