B _j:@sdgZddlmZddlmZddlmZmZddlm Z ddl m Z m Z m Z ddlmZddlmZdd lmZdd lZdd lZed Zd d ZGdddeZe dGdddeZGdddeeZGdddeZd S)Arc)Port)ActiveComponentData)ActiveIndexedComponentUnindexedComponent_set)apply_indexed_rule)ModelComponentFactoryIPyomoScriptModifyInstanceTransformationFactory)ConstructionTimer) iteritems)refNz pyomo.networkcCst|tk r~y t|}Wntk r0d}YnX|dksFt|dkrRtd||rd|\}}d}nd}}t||||d}nd|kr||d<|S)NzAValue for arc '%s' is not either a dict or a two-member iterable.)source destinationportsdirectedr)typedicttuple TypeErrorlen ValueError)valsrnamerrrr5/tmp/pip-unpacked-wheel-d4p3hk07/pyomo/network/arc.py_iterable_to_dicts$    rcseZdZdZdZdddZfddZdd Zed d Z e Z ed d Z e Z eddZ eddZeddZddZddZZS)_ArcDataam This class defines the data for a single Arc Attributes ---------- source: `Port` The source Port when directed, else None. Aliases to src. destination: `Port` The destination Port when directed, else None. Aliases to dest. ports: `tuple` A tuple containing both ports. If directed, this is in the order (source, destination). directed: `bool` True if directed, False if not expanded_block: `Block` A reference to the block on which expanded constraints for this arc were placed )_ports _directed_expanded_blockNcKsD|dk rt|nd|_d|_d|_d|_d|_t|r@||dS)NT) weakref_refZ _component_activerr r!r set_value)self componentkwdsrrr__init__Isz_ArcData.__init__cs0tt|}xtjD]}t||||<qW|S)N)superr __getstate__ __slots__getattr)r%statei) __class__rrr*Ys z_ArcData.__getstate__cCsF|j}|dk r.y t||Stk r,YnXtd|jj|fdS)z/Returns `self.expanded_block.name` if it existsNz!'%s' object has no attribute '%s')expanded_blockr,AttributeErrorr/__name__)r%rZebrrr __getattr__cs z_ArcData.__getattr__cCs|jr|jdk r|jdSdS)Nr)r r)r%rrrrqsz_ArcData.sourcecCs|jr|jdk r|jdSdS)N)r r)r%rrrr{sz_ArcData.destinationcCs|jS)N)r)r%rrrrsz_ArcData.portscCs|jS)N)r )r%rrrrsz_ArcData.directedcCs|jS)N)r!)r%rrrr0sz_ArcData.expanded_blockc Cs|jdk r|jn|j}t|||j}|dd}|dd}|dd}|dd}t|rtdddd t |D|dk r|dkr|dkr|r|dk ry|\}}d}Wqtd |jYqXn|std |j| ||||j dk rHt |}x|j D]}|j |qW|jrH|jj||jj||dk rZt|n||f|_|dk |_t |}x|jD]}|j |q~W|jr|j||j|dS) z#Set the port attributes on this arcNrrrrz0set_value passed unrecognized keywords in val: z css|]\}}d||fVqdS)z%s = %sNr).0kvrrr sz%_ArcData.set_value..zYFailed to unpack 'ports' argument of arc '%s'. Argument must be a 2-member tuple or list.zTPassed False value for 'directed' for arc '%s', but specified source or destination.)r Zparent_component_init_directedrrpoprrjoinr _validate_portsrr"Z_arcsremoverZ_destsrZ_sourcesrrappend) r%rdrrrrZ weakref_selfportrrrr$sT             z_ArcData.set_valuec Csd|j}|dk r|dk s"|dk r.t|dt|ttfksJt|dkrVt|dxr|D]j}y<|jtk rt|d|jn|rt|d|jWq\t k rt|dt |Yq\Xq\Wn|dks|dkrt|dx|df|d fgD]\}}yH|jtk r(t|d |j|fn |rHt|d |j|fWqt k rxt|d t ||fYqXqWdS) NzArc %s: zEcannot specify 'source' or 'destination' when using 'ports' argument.rzBargument 'ports' must be list or tuple containing exactly 2 Ports.z.found object '%s' in 'ports' not of type Port.zBfound indexed Port '%s' in 'ports', must use single Ports for Arc.z>must specify both 'source' and 'destination' for directed Arc.rrz %s object '%s' not of type Port.z=found indexed Port '%s' as %s, must use single Ports for Arc.) rrrlistrrctyper is_indexedr1str)r%rrrmsgpZsiderrrr<sJ     z_ArcData._validate_ports)N)r2 __module__ __qualname____doc__r+r(r*r3propertyrsrcrdestrrr0r$r< __classcell__rr)r/rr3s       :rz(Component used for connecting two Ports.csBeZdZdZeZfddZfddZd ddZd d Z Z S) ra Component used for connecting the members of two Port objects Parameters ---------- source: `Port` A single Port for a directed arc. Aliases to src. destination: `Port` A single`Port for a directed arc. Aliases to dest. ports A two-member list or tuple of single Ports for an undirected arc directed: `bool` Set True for directed. Use along with `rule` to be able to return an implied (source, destination) tuple. rule: `function` A function that returns either a dictionary of the arc arguments or a two-member iterable of ports csL|tkrtt||S|r4|dtkr>t|dkr>ttSttSdS)Nrr4)rr)__new__rr SimpleArc IndexedArc)clsargsr')r/rrrN s  z Arc.__new__cs|d|dd}|d|dd}|dd}|dd|_|dd|_|dttt|j|||dkr|dkr|dkrd|_nt|||d |_dS) NrrKrrLrrZrulerB)rrr) r:r9_rule setdefaultrr)r( _init_valsr)r%rRr'rrr)r/rrr(s  z Arc.__init__Nc Csttjrtd|j|jr&dSt|}d|_|jdkrL|j dkrLdS|jdk rn|j dk rnt d|j| }| s|jdkr|j }|j |d<n^y||}Wn>tk rtd}td|jt|j|fYnXt||j |j}|d|n|j dk r"td|jx|jD]~}yt||j||}WnFtk rtd}td |jt|t|j|fYnXt||j |j}|||q*W|dS) zInitialize the ArcTzConstructing Arc %sNzMCannot specify rule along with source/destination/ports keywords for arc '%s'rr4z5Rule failed when generating values for arc %s: %s: %szHArc '%s': Cannot initialize multiple indices of an arc with single portszCRule failed when generating values for arc %s with index %s: %s: %s)logger isEnabledForloggingDEBUGdebugr _constructedr rSrUr_parentrCr9 Exceptionsysexc_infoerrorrr2rZ_setitem_when_not_present IndexError_indexrrDreport)r%dataZtimerZ self_parenttmperridxrrr construct$sV          z Arc.constructcCs:dt|fd|r|jndfd|jfgt|dddfS)z4Return data that will be printed for this component.SizeZIndexNActive)ZPortsZDirectedrjcSs"|jdk rd|jnd|j|jgS)Nz(%s, %s))rractive)r6r7rrrdszArc._pprint..)rrCrbrkr )r%rrr_pprint\s   z Arc._pprint)N) r2rGrHrIrZ_ComponentDataClassrNr(rhrmrMrr)r/rrs    8cs$eZdZddZfddZZS)rOcOs"t||tj|f||dS)N)rr(r)r%rRr'rrrr(ks zSimpleArc.__init__cs\|jstd|jt|jdkr,||jd<ytt||Wn|jd=YnXdS)z Set the port attributes on this arc. If these values are being reassigned, note that the defaults are still None, so you may need to repass some attributes. zhSetting the value of arc '%s' before the Arc has been constructed (there is currently no object to set).rN)r[rrr_datar)rOr$)r%r)r/rrr$os  zSimpleArc.set_value)r2rGrHr(r$rMrr)r/rrOisrOcs(eZdZfddZeddZZS)rPcsd|_tt|j||dS)N)r!r)rPr()r%rRr')r/rrr(szIndexedArc.__init__cCs|jS)N)r!)r%rrrr0szIndexedArc.expanded_block)r2rGrHr(rJr0rMrr)r/rrPs rP)__all__Zpyomo.network.portrZpyomo.core.base.componentrZ!pyomo.core.base.indexed_componentrrZpyomo.core.base.miscrZpyomo.core.base.pluginrr r Zpyomo.common.timingr sixr weakrefr r"rXr^ getLoggerrVrrregisterrrOrPrrrr s"       Cs