source: coopr.opt/trunk/coopr/opt/base/convert.py @ 2201

Last change on this file since 2201 was 2201, checked in by wehart, 10 years ago

Update to Coopr to account for changes in PyUtilib? package names.

File size: 3.5 KB
Line 
1#  _________________________________________________________________________
2#
3#  Coopr: A COmmon Optimization Python Repository
4#  Copyright (c) 2008 Sandia Corporation.
5#  This software is distributed under the BSD License.
6#  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7#  the U.S. Government retains certain rights in this software.
8#  For more information, see the Coopr README.txt file.
9#  _________________________________________________________________________
10
11__all__ = ['IProblemConverter', 'convert_problem']
12
13import copy
14import os
15from formats import ProblemFormat
16from error import *
17from pyutilib.component.core import *
18
19
20class IProblemConverter(Interface):
21
22    def can_convert(self, from_type, to_type):
23        """Returns true if this object supports the specified conversion"""
24
25    def convert(self, from_type, to_type):
26        """Convert an instance of one type into another"""
27
28
29def convert_problem(args, target_problem_type, valid_problem_types):
30    """
31    Convert a problem, defined by the 'args' tuple, into another problem.
32    """
33    #print "HERE",args,target_problem_type,valid_problem_types
34    if len(valid_problem_types) == 0:
35       raise ConverterError, "No valid problem types"
36    if not (target_problem_type is None or \
37             target_problem_type in valid_problem_types):
38       raise ConverterError, "Problem type "+str(target_problem_type)+" is not valid"
39    if len(args) == 0:
40       raise ConverterError, "Empty argument list"
41
42    #
43    # Setup list of source problem types
44    #
45    tmp = args[0]
46    if isinstance(tmp,basestring):
47       fname = tmp.split(os.sep)[-1]
48       if os.sep in fname:   #pragma:nocover
49          fname = tmp.split(os.sep)[-1]
50       suffix = fname.split('.')[-1]
51       try:
52          if suffix == "lp":
53             source_ptype = [ProblemFormat.cpxlp]
54          else:
55             source_ptype = [getattr(ProblemFormat,suffix)]
56       except AttributeError:
57          raise ConverterError, "Unknown suffix type: "+suffix
58    else:
59       source_ptype = args[0].valid_problem_types()
60
61    #
62    # Setup list of valid problem types
63    #
64    valid_ptypes = copy.copy(valid_problem_types)
65    if target_problem_type is not None:
66       valid_ptypes.remove(target_problem_type)
67       valid_ptypes = [target_problem_type]  + valid_ptypes
68    if source_ptype[0] in valid_ptypes:
69       ##print "HERE",source_ptype, valid_ptypes
70       valid_ptypes.remove(source_ptype[0])
71       valid_ptypes = [source_ptype[0]]  + valid_ptypes
72
73    #
74    # Iterate over the valid problem types, starting with the target type
75    #
76    # Apply conversion and return for first match
77    #
78    for ptype in valid_ptypes:
79      for s_ptype in source_ptype:
80        #print "HERE",str(s_ptype),str(ptype),args[0]
81        #
82        # If the source and target types are equal, then simply the return
83        # the args (return just the first element of the tuple if it has length
84        # one.
85        #
86        if s_ptype == ptype:
87           return (args,ptype,None)
88        #
89        # Otherwise, try to convert
90        #
91        for converter in ExtensionPoint(IProblemConverter):
92            #print "HERE",converter,s_ptype,ptype
93            if converter.can_convert(s_ptype,ptype):
94                tmp = [s_ptype,ptype] + list(args)
95                tmp = tuple(tmp)
96                problem_files, symbol_map = converter.apply(*tmp)
97                return problem_files, ptype, symbol_map
98    raise ConverterError, "No conversion possible.  Source problem type: %s.  Valid target types: %s" % (str(source_ptype[0]),map(str,valid_ptypes))
99
Note: See TracBrowser for help on using the repository browser.