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

Last change on this file since 1768 was 1768, checked in by wehart, 11 years ago

Rework of Coopr to use the new PyUtilib? package decomposition.

NOTE: to use Coopr with this update, we need to work with a new version of coopr_install.

File size: 3.1 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', 'IPyomoPresolveAction']
15
16from pyutilib.plugin.core import *
17
18
19class IPyomoPresolver(Interface):
20
21    def get_actions(self):
22        """Return a list of presolve actions, in the order in which
23        they will be applied."""
24   
25    def activate_action(self, action):
26        """Activate an action, but leave its default rank"""
27
28    def deactivate_action(self, action):
29        """Deactivate an action"""
30
31    def set_actions(self, actions):
32        """Set presolve action list"""
33
34    def presolve(self, instance):
35        """Apply the presolve actions to this instance, and return the
36        revised instance"""
37
38
39class IPyomoPresolveAction(Interface):
40
41    def presolve(self, instance):
42        """Apply the presolve action to this instance, and return the
43        revised instance"""
44
45    def rank(self):
46        """Return an integer that is used to automatically order presolve actions,
47        from low to high rank."""
48
49
50class IPyomoExpression(Interface):
51
52    def type(self):
53        """Return the type of expression"""
54
55    def create(self, args):
56        """Create an instance of this expression type"""
57
58
59class ExpressionRegistration(Plugin):
60
61    implements(IPyomoExpression)
62
63    def __init__(self, type, cls):
64        Plugin.__init__(self, name=type)
65        self._type = type
66        self._cls = cls
67
68    def type(self):
69        return self._type
70
71    def create(self, args):
72        return self._cls(args)
73
74
75def ExpressionFactory(name=None, args=[]):
76    ep = ExpressionFactory.ep
77    if name is None:
78        return map(lambda x:x.name, ep())
79    return ep.service(name).create(args)
80ExpressionFactory.ep = ExtensionPoint(IPyomoExpression)
81
82
83
84
85class IPyomoSet(Interface):
86    pass
87
88
89
90class IModelComponent(Interface):
91
92    def type(self):
93        """Return the type of component"""
94
95    def cls(self):
96        """Return the class type of this component"""
97
98    def create(self):
99        """Create an instance of this component type"""
100
101
102class ComponentRegistration(Plugin):
103
104    implements(IModelComponent)
105
106    def __init__(self, type, cls, doc=""):
107        Plugin.__init__(self, name=type)
108        self._type = type
109        self._cls = cls
110        self.doc=doc
111
112    def type(self):
113        return self._type
114
115    def cls(self):
116        return self._cls
117
118    def create(self):
119        return self._cls()
120
121
122def ComponentFactory(name=None, args=[]):
123    ep = ExtensionPoint(IModelComponent)
124    if name is None:
125        return map(lambda x:x.name, ep())
126    return ep.service(name).create(*args)
127
128
Note: See TracBrowser for help on using the repository browser.