Changeset 2274


Ignore:
Timestamp:
Feb 6, 2010 10:13:24 AM (10 years ago)
Author:
wehart
Message:

Added the 'include' command, which can include other *.dat files. This works
like the AMPL include command, with the exception that it must be terminated by
a semicolon.

Reworked the AMPL datafile plugin to simple execute an include command
on a *.dat file. This rework has the advantage that filename/lineno information
is now propigated throught he AMPL command processing.

Location:
coopr.pyomo/trunk/coopr/pyomo
Files:
3 added
6 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/data/TableData.py

    r2227 r2274  
    5252        Return the data that was extracted from this table
    5353        """
    54         return _process_data(self._info, model, data, default)
     54        return _process_data(self._info, model, data, default, self.filename)
    5555
    5656    def clear(self):
  • coopr.pyomo/trunk/coopr/pyomo/data/ampl.py

    r2227 r2274  
     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#  _________________________________________________________________________
    110
    211import os.path
    312import re
    4 from pyutilib.misc import Options, quote_split
     13from pyutilib.misc import Options
    514from coopr.pyomo.base.plugin import DataManagerRegistration
    6 from process_data import _process_data
     15from process_data import _process_include
    716
    817class AmplDataCommands(object):
     
    2029        if not os.path.exists(self.filename):
    2130            raise IOError, "Cannot find file '%s'" % self.filename
    22         self.INPUT = open(self.filename, 'r')
    2331
    2432    def close(self):
    25         self.INPUT.close()
     33        pass
    2634       
    2735    def read(self):
    2836        """
    29         Create a table of tuples to values, based on data from a file.
    30         We assume that this data is defined in a format that is
    31         compatible with AMPL.
     37        This function does nothing, since executing AMPL data commands
     38        both reads and processes the data all at once.
    3239        """
    33         global Filename
    34         Filename = self.filename
    35         global Lineno
    36         Lineno = 0
    37         cmd=""
    38         status=True
    39         for line in self.INPUT:
    40           Lineno += 1
    41           line = re.sub(":"," :",line)
    42           line = line.strip()
    43           if line == "" or line[0] == '#':
    44              continue
    45           cmd = cmd + " " + line
    46           if ';' in cmd:
    47              #
    48              # We assume that a ';' indicates an end-of-command declaration.
    49              # However, the user might have put multiple commands on a single
    50              # line, so we need to split the line based on these values.
    51              # BUT, at the end of the line we should see an 'empty' command,
    52              # which we ignore.
    53              #
    54              for item in cmd.split(';'):
    55                item = item.strip()
    56                if item != "":
    57                   self._info.append(quote_split("[\t ]+",item))
    58              cmd = ""
    59         if cmd != "":
    60             raise IOError, "ERROR: There was unprocessed text at the end of the data file!: \"" + cmd + "\""
    61             self.INPUT.close()
    62         self.INPUT.close()
    63         return status
     40        return True
    6441
    6542    def process(self, model, data, default):
    6643        """
    67         Return the data that was extracted from this table
     44        Read AMPL data commands and process the data.
    6845        """
    69         for item in self._info:
    70             _process_data(item, model, data, default)
     46        _process_include(['include', self.filename], model, data, default)
    7147
    7248    def clear(self):
  • coopr.pyomo/trunk/coopr/pyomo/data/process_data.py

    r2227 r2274  
    1212import copy
    1313import math
     14from pyutilib.misc import quote_split
    1415from coopr.pyomo.base.plugin import *
    1516from coopr.pyomo.base import pyomo
     17
     18global Lineno
     19global Filename
    1620
    1721
     
    372376
    373377
    374 def _process_data(cmd, _model, _data, _default):
     378def _process_include(cmd, _model, _data, _default):
     379    if len(cmd) == 1:
     380        raise IOError, "Cannot execute 'include' command without a filename"
     381    if len(cmd) > 2:
     382        raise IOError, "The 'include' command only accepts a single filename"
     383
     384    global Filename
     385    Filename = cmd[1]
     386    global Lineno
     387    Lineno = 0
     388    cmd=""
     389    status=True
     390    INPUT=open(Filename,'r')
     391    for line in INPUT:
     392        Lineno = Lineno + 1
     393        line = re.sub(":"," :",line)
     394        line = line.strip()
     395        if line == "" or line[0] == '#':
     396            continue
     397        cmd = cmd + " " + line
     398        if ';' in cmd:
     399            #
     400            # We assume that a ';' indicates an end-of-command declaration.
     401            # However, the user might have put multiple commands on a single
     402            # line, so we need to split the line based on these values.
     403            # BUT, at the end of the line we should see an 'empty' command,
     404            # which we ignore.
     405            #
     406            for item in cmd.split(';'):
     407                item = item.strip()
     408                if item != "":
     409                    _process_data(quote_split("[\t ]+",item), _model, _data, _default, Filename, Lineno)
     410                cmd = ""
     411    if cmd != "":
     412        raise IOError, "ERROR: There was unprocessed text at the end of the data file!: \"" + cmd + "\""
     413        INPUT.close()
     414    INPUT.close()
     415    return status
     416
     417
     418def _process_data(cmd, _model, _data, _default, Filename_, Lineno_=0):
    375419        """
    376420        Called by import_file() to (1) preprocess data and (2) call
    377421        subroutines to process different types of data
    378422        """
    379         #global Lineno
    380         #global Filename
     423        global Lineno
     424        global Filename
     425        Lineno=Lineno_
     426        Filename=Filename_
     427
    381428        if pyomo.debug("reader"):               #pragma:nocover
    382429           print "DEBUG: _process_data (start)",cmd
    383430        if len(cmd) == 0:                       #pragma:nocover
    384431           raise ValueError, "ERROR: Empty list passed to Model::_process_data"
     432
    385433        cmd = _preprocess_data(cmd)
     434
    386435        if cmd[0] == "data":
    387436           return True
    388437        if cmd[0] == "end":
    389438           return False
    390         if cmd[0][0:3] == "set":
     439        if cmd[0].startswith('set'):
    391440           _process_set(cmd, _model, _data)
    392         elif cmd[0][0:5] == "param":
     441        elif cmd[0].startswith('param'):
    393442           _process_param(cmd, _model, _data, _default)
     443        elif cmd[0] == 'include':
     444           _process_include(cmd, _model, _data, _default)
    394445        else:
    395446           raise IOError, "ERROR: Unknown data command: "+" ".join(cmd)
  • coopr.pyomo/trunk/coopr/pyomo/data/sheet.py

    r2227 r2274  
     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#  _________________________________________________________________________
    110
    211import os.path
  • coopr.pyomo/trunk/coopr/pyomo/data/text.py

    r2227 r2274  
     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#  _________________________________________________________________________
    110
    211import os.path
  • coopr.pyomo/trunk/coopr/pyomo/tests/unit/test_modeldata.py

    r2227 r2274  
    270270        md.read(model)
    271271
     272    def test_md11(self):
     273        cwd = os.getcwd()
     274        os.chdir(currdir)
     275        md = ModelData()
     276        md.add(currdir+"data11.dat")
     277        model=Model()
     278        model.A=Set()
     279        model.B=Set()
     280        model.C=Set()
     281        model.D=Set()
     282        md.read(model)
     283        os.chdir(cwd)
     284
    272285
    273286if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.