source: trunk/coopr/pyomo/base/plugin.py @ 1261

Last change on this file since 1261 was 1221, checked in by wehart, 12 years ago

Adding hooks to support presolves. I'm not sure if this works right now,
but I can't easily test this under Cygwin.

File size: 2.4 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__ = ['IPyomoExpression', 'ExpressionRegistration', 'ExpressionFactory',
12        'IPyomoSet', 'IModelComponent', 'ComponentRegistration',
13        'ComponentFactory',
14        'IPyomoPresolver']
15
16from pyutilib.plugin import *
17
18
19class IPyomoPresolver(Interface):
20
21    def presolve(self, instance):
22        """Apply the presolver to this instance, and return the
23        revised instance"""
24
25
26class IPyomoExpression(Interface):
27
28    def type(self):
29        """Return the type of expression"""
30
31    def create(self, args):
32        """Create an instance of this expression type"""
33
34
35class ExpressionRegistration(Plugin):
36
37    implements(IPyomoExpression)
38
39    def __init__(self, type, cls):
40        Plugin.__init__(self, name=type)
41        self._type = type
42        self._cls = cls
43
44    def type(self):
45        return self._type
46
47    def create(self, args):
48        return self._cls(args)
49
50
51def ExpressionFactory(name=None, args=[]):
52    ep = ExtensionPoint(IPyomoExpression)
53    if name is None:
54        return map(lambda x:x.name, ep())
55    return ep.service(name).create(args)
56
57
58
59
60class IPyomoSet(Interface):
61    pass
62
63
64
65class IModelComponent(Interface):
66
67    def type(self):
68        """Return the type of component"""
69
70    def cls(self):
71        """Return the class type of this component"""
72
73    def create(self):
74        """Create an instance of this component type"""
75
76
77class ComponentRegistration(Plugin):
78
79    implements(IModelComponent)
80
81    def __init__(self, type, cls, doc=""):
82        Plugin.__init__(self, name=type)
83        self._type = type
84        self._cls = cls
85        self.doc=doc
86
87    def type(self):
88        return self._type
89
90    def cls(self):
91        return self._cls
92
93    def create(self):
94        return self._cls()
95
96
97def ComponentFactory(name=None, args=[]):
98    ep = ExtensionPoint(IModelComponent)
99    if name is None:
100        return map(lambda x:x.name, ep())
101    return ep.service(name).create(*args)
102
103
Note: See TracBrowser for help on using the repository browser.