2525from gbd_init .initializer import Initializer , InitializerException
2626
2727try :
28- from gbdc import cnf2kis , sanitize
28+ from gbdc import cnf2kis , sanitise , normalise
2929except ImportError :
30- def cnf2kis (ipath , opath , maxedges , maxnodes , tlim , mlim , flim ):
30+ def cnf2kis (ipath , opath ):
3131 raise ModuleNotFoundError ("gbdc not found" , name = "gbdc" )
3232
33- def sanitize (ipath , tlim , mlim ):
33+ def sanitise (ipath , opath ):
34+ raise ModuleNotFoundError ("gbdc not found" , name = "gbdc" )
35+
36+ def normalise (ipath , opath ):
3437 raise ModuleNotFoundError ("gbdc not found" , name = "gbdc" )
3538
3639
37- # Transform SAT Problem to k-Independent Set Problem
3840def kis_filename (path ):
3941 kispath = reduce (lambda path , suffix : path [:- len (suffix )] if path .endswith (suffix ) else path , contexts .suffixes ('cnf' ), path )
4042 return kispath + '.kis'
4143
42- # Sanitize CNF
43- def sanitized_filename (path ):
44+ def sanitised_filename (path ):
4445 sanpath = reduce (lambda path , suffix : path [:- len (suffix )] if path .endswith (suffix ) else path , contexts .suffixes ('cnf' ), path )
4546 return sanpath + '.sanitized.cnf'
4647
48+ def normalised_filename (path ):
49+ normpath = reduce (lambda path , suffix : path [:- len (suffix )] if path .endswith (suffix ) else path , contexts .suffixes ('cnf' ), path )
50+ return normpath + '.normalised.cnf'
51+
4752
4853def wrap_cnf2kis (hash , path , limits ):
4954 kispath = kis_filename (path )
5055 util .eprint ('Transforming {} to k-ISP {}' .format (path , kispath ))
5156 try :
52- result = cnf2kis (path , kispath , 2 ** 32 , 2 ** 32 , limits [ 'tlim' ], limits [ 'mlim' ], limits [ 'flim' ] )
57+ result = cnf2kis (path , kispath )
5358 if "local" in result :
5459 kishash = result ['hash' ]
5560 return [ ('local' , kishash , result ['local' ]), ('to_cnf' , kishash , hash ),
@@ -63,20 +68,39 @@ def wrap_cnf2kis(hash, path, limits):
6368
6469 return [ ]
6570
66- def wrap_sanitize (hash , path , limits ):
67- sanname = sanitized_filename (path )
68- util .eprint ('Sanitizing {}' .format (path ))
71+ def wrap_sanitise (hash , path , limits ):
72+ sanpath = sanitised_filename (path )
73+ util .eprint ('Sanitising {}' .format (path ))
6974 try :
70- with open (sanname , 'w' ) as f , util .stdout_redirected (f ):
71- if sanitize (path ):
72- sanhash = identify (sanname )
73- return [ ('local' , sanhash , sanname ), ('to_cnf' , sanhash , hash ) ]
75+ with open (sanpath , 'w' ) as f , util .stdout_redirected (f ):
76+ result = sanitise (path , sanpath )
77+ if "local" in result :
78+ sanhash = result ['hash' ]
79+ return [ ('local' , sanhash , result ['local' ]), ('to_cnf' , sanhash , hash ) ]
7480 else :
7581 raise GBDException ("Sanitization failed for {}" .format (path ))
7682 except Exception as e :
7783 util .eprint (str (e ))
78- if os .path .exists (sanname ):
79- os .remove (sanname )
84+ if os .path .exists (sanpath ):
85+ os .remove (sanpath )
86+
87+ return [ ]
88+
89+ def wrap_normalise (hash , path , limits ):
90+ normpath = normalised_filename (path )
91+ util .eprint ('Normalising {}' .format (path ))
92+ try :
93+ with open (normpath , 'w' ) as f , util .stdout_redirected (f ):
94+ result = normalise (path , normpath )
95+ normhash = result ['hash' ]
96+ if "local" in result and hash == normhash :
97+ return [ ('local' , normhash , result ['local' ]) ]
98+ else :
99+ raise GBDException ("Normalisation failed for {}" .format (path ))
100+ except Exception as e :
101+ util .eprint (str (e ))
102+ if os .path .exists (normpath ):
103+ os .remove (normpath )
80104
81105 return [ ]
82106
@@ -98,13 +122,21 @@ def transform_instances_generic(key: str, api: GBD, rlimits, query, hashes, targ
98122
99123
100124generic_transformers = {
101- "sanitize " : {
102- "description" : "Sanitize CNF files. " ,
125+ "sanitise " : {
126+ "description" : "Sanitise CNF files. " ,
103127 "source" : [ "cnf" ],
104128 "target" : [ "sancnf" ],
105129 "features" : [ ('local' , None ) , ('to_cnf' , None ) ],
106- "compute" : wrap_sanitize ,
107- "filename" : sanitized_filename ,
130+ "compute" : wrap_sanitise ,
131+ "filename" : sanitised_filename ,
132+ },
133+ "normalise" : {
134+ "description" : "Normalise CNF files. " ,
135+ "source" : [ "cnf" ],
136+ "target" : [ "cnf" ],
137+ "features" : [ ('local' , None ) ],
138+ "compute" : wrap_normalise ,
139+ "filename" : normalised_filename ,
108140 },
109141 "cnf2kis" : {
110142 "description" : "Transform CNF files to k-ISP instances. " ,
0 commit comments