traci.domain

  1# -*- coding: utf-8 -*-
  2# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
  3# Copyright (C) 2008-2026 German Aerospace Center (DLR) and others.
  4# This program and the accompanying materials are made available under the
  5# terms of the Eclipse Public License 2.0 which is available at
  6# https://www.eclipse.org/legal/epl-2.0/
  7# This Source Code may also be made available under the following Secondary
  8# Licenses when the conditions for such availability set forth in the Eclipse
  9# Public License 2.0 are satisfied: GNU General Public License, version 2
 10# or later which is available at
 11# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
 12# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
 13
 14# @file    domain.py
 15# @author  Michael Behrisch
 16# @author  Lena Kalleske
 17# @author  Mario Krumnow
 18# @author  Daniel Krajzewicz
 19# @author  Jakob Erdmann
 20# @date    2008-10-09
 21
 22from __future__ import print_function
 23from __future__ import absolute_import
 24import copy
 25import warnings
 26from functools import wraps
 27
 28from . import constants as tc
 29from .exceptions import FatalTraCIError, alias_param
 30
 31DOMAINS = []
 32DOMAIN_BY_ID = {}
 33
 34
 35def _readParameterWithKey(result):
 36    assert result.read("!i")[0] == 2  # compound size
 37    key = result.readTypedString()
 38    val = result.readTypedString()
 39    return key, val
 40
 41
 42def _parse(valueFunc, varID, data):
 43    varType = data.read("!B")[0]
 44    if varID in valueFunc:
 45        return valueFunc[varID](data)
 46    if varType in (tc.POSITION_2D, tc.POSITION_LON_LAT):
 47        return data.read("!dd")
 48    if varType in (tc.POSITION_3D, tc.POSITION_LON_LAT_ALT):
 49        return data.read("!ddd")
 50    if varType == tc.TYPE_POLYGON:
 51        return data.readShape()
 52    if varType == tc.TYPE_UBYTE:
 53        return data.read("!B")[0]
 54    if varType == tc.TYPE_BYTE:
 55        return data.read("!b")[0]
 56    if varType == tc.TYPE_INTEGER:
 57        return data.readInt()
 58    if varType == tc.TYPE_DOUBLE:
 59        return data.readDouble()
 60    if varType == tc.TYPE_STRING:
 61        return data.readString()
 62    if varType == tc.TYPE_STRINGLIST:
 63        return data.readStringList()
 64    if varType == tc.TYPE_DOUBLELIST:
 65        n = data.read("!i")[0]
 66        return tuple([data.readDouble() for i in range(n)])
 67    if varType == tc.TYPE_COLOR:
 68        return data.read("!BBBB")
 69    raise FatalTraCIError("Unknown variable %02x or invalid type %02x." % (varID, varType))
 70
 71
 72class SubscriptionResults:
 73
 74    def __init__(self, valueFunc):
 75        self._results = {}
 76        self._contextResults = {}
 77        self._valueFunc = valueFunc
 78
 79    def reset(self):
 80        self._results.clear()
 81        self._contextResults.clear()
 82
 83    def add(self, refID, varID, data):
 84        if refID not in self._results:
 85            self._results[refID] = {}
 86        self._results[refID][varID] = self.parse(varID, data)
 87
 88    def get(self, refID=None):
 89        if refID is None:
 90            return self._results
 91        return self._results.get(refID, {})
 92
 93    def addContext(self, refID, objectID=None, varID=None, value=None):
 94        if refID not in self._contextResults:
 95            self._contextResults[refID] = {}
 96        if objectID is not None and objectID not in self._contextResults[refID]:
 97            self._contextResults[refID][objectID] = {}
 98        if varID is not None and value is not None:
 99            self._contextResults[refID][objectID][varID] = value
100
101    def parse(self, varID=None, data=None):
102        return _parse(self._valueFunc, varID, data)
103
104    def getContext(self, refID=None):
105        if refID is None:
106            return self._contextResults
107        return self._contextResults.get(refID, {})
108
109    def __repr__(self):
110        return "<%s, %s>" % (self._results, self._contextResults)
111
112
113class Domain:
114
115    def __init__(self, name, cmdGetID, cmdSetID,
116                 subscribeID, subscribeResponseID,
117                 contextID, contextResponseID,
118                 retValFunc=None, deprecatedFor=None,
119                 subscriptionDefault=(tc.TRACI_ID_LIST,)):
120        self._name = name
121        self._cmdGetID = cmdGetID
122        self._cmdSetID = cmdSetID
123        self._subscribeID = subscribeID
124        self._subscribeResponseID = subscribeResponseID
125        self._contextID = contextID
126        self._contextResponseID = contextResponseID
127        self._retValFunc = {tc.VAR_PARAMETER_WITH_KEY: _readParameterWithKey}
128        if retValFunc is not None:
129            self._retValFunc.update(retValFunc)
130        self._deprecatedFor = deprecatedFor
131        self._subscriptionDefault = subscriptionDefault
132        self._connection = None
133        self._isGlobal = True
134        DOMAINS.append(self)
135        DOMAIN_BY_ID[cmdGetID] = self
136        # alias
137        self.DOMAIN_ID = cmdGetID
138
139    def __getattribute__(self, name):
140        attribute = object.__getattribute__(self, name)
141        if not name.startswith("_") and object.__getattribute__(self, "_isGlobal") and callable(attribute):
142            connection = object.__getattribute__(self, "_connection")
143            if connection is not None and connection._traceFile is not None:
144                # Resolve at call time so a saved global method still follows switch().
145                @wraps(attribute)
146                def tracedCall(*args, **kwargs):
147                    selected = object.__getattribute__(self, "_connection")
148                    if selected is None:
149                        return attribute(*args, **kwargs)
150                    return getattr(getattr(selected, object.__getattribute__(self, "_name")), name)(*args, **kwargs)
151                return tracedCall
152        return attribute
153
154    def _register(self, connection, mapping):
155        dom = copy.copy(self)
156        dom._connection = connection
157        dom._isGlobal = False
158        subscriptionResults = SubscriptionResults(self._retValFunc)
159        mapping[self._subscribeResponseID] = subscriptionResults
160        mapping[self._contextResponseID] = subscriptionResults
161        mapping[self._cmdGetID] = subscriptionResults
162        setattr(connection, self._name, dom)
163
164    def _setConnection(self, connection):
165        self._connection = connection
166
167    def _getUniversal(self, varID, objectID="", format="", *values):
168        if self._deprecatedFor:
169            warnings.warn("The domain %s is deprecated, use %s instead." % (self._name, self._deprecatedFor))
170        return _parse(self._retValFunc, varID, self._getCmd(varID, objectID, format, *values))
171
172    def _getCmd(self, varID, objID, format="", *values):
173        if self._connection is None:
174            raise FatalTraCIError("Not connected.")
175        r = self._connection._sendCmd(self._cmdGetID, varID, objID, format, *values)
176        r.readLength()
177        response, retVarID = r.read("!BB")
178        objectID = r.readString()
179        if response - self._cmdGetID != 16 or retVarID != varID or objectID != objID:
180            raise FatalTraCIError("Received answer %s,%s,%s for command %s,%s,%s."
181                                  % (response, retVarID, objectID, self._cmdGetID, varID, objID))
182        return r
183
184    def _setCmd(self, varID, objectID, format="", *values):
185        if self._connection is None:
186            raise FatalTraCIError("Not connected.")
187        self._connection._sendCmd(self._cmdSetID, varID, objectID, format, *values)
188
189    def getIDList(self):
190        """getIDList() -> tuple(string)
191
192        Returns a tuple of all objects in the network.
193        """
194        return self._getUniversal(tc.TRACI_ID_LIST)
195
196    def getIDCount(self):
197        """getIDCount() -> integer
198
199        Returns the number of currently loaded objects.
200        """
201        return self._getUniversal(tc.ID_COUNT)
202
203    def domainID(self):
204        return self._cmdGetID
205
206    def subscribe(self, objectID, varIDs=None, begin=tc.INVALID_DOUBLE_VALUE, end=tc.INVALID_DOUBLE_VALUE,
207                  parameters=None):
208        """subscribe(string, list(integer), double, double, map) -> None
209        The parameters map argument is needed when subscribing to functions that require additional parameters:
210        The map keys must be a subset of the varIDs
211        The map values may take any of the following forms:
212        - a single integer, float or string value (for methods that require only one additional argument of such type)
213        - a tuple where
214          - the first element is a string consisting of format specifiers (see method _pack in module connection)
215          - the remaining elements are the arguments (one per character in the format specifier string)
216        Subscribe to one or more object values for the given interval.
217        """
218        if varIDs is None:
219            varIDs = self._subscriptionDefault
220        self._connection._subscribe(self._subscribeID, begin, end, objectID, varIDs, parameters)
221
222    def unsubscribe(self, objectID):
223        """unsubscribe(string) -> None
224
225        Unsubscribe from receiving object values.
226        """
227        self._connection._subscribe(self._subscribeID, tc.INVALID_DOUBLE_VALUE, tc.INVALID_DOUBLE_VALUE,
228                                    objectID, [], None)
229
230    def getSubscriptionResults(self, objectID):
231        """getSubscriptionResults(string) -> dict(integer: <value_type>)
232
233        Returns the subscription results for the last time step and the given object.
234        If the object id is unknown or the subscription did for any reason return no data,
235        'None' is returned.
236        It is not possible to retrieve older subscription results than the ones
237        from the last time step.
238        """
239        return self._connection._getSubscriptionResults(self._subscribeResponseID).get(objectID)
240
241    def getAllSubscriptionResults(self):
242        """getAllSubscriptionResults() -> dict(string: dict(integer: <value_type>))
243
244        Returns the subscription results for the last time step and all objects of the domain.
245        It is not possible to retrieve older subscription results than the ones
246        from the last time step.
247        """
248        return self._connection._getSubscriptionResults(self._subscribeResponseID).get(None)
249
250    def subscribeContext(self, objectID, domain, dist, varIDs=None,
251                         begin=tc.INVALID_DOUBLE_VALUE, end=tc.INVALID_DOUBLE_VALUE, parameters=None):
252        """subscribeContext(string, int, double, list(integer), double, double, map) -> None
253
254        Subscribe to objects of the given domain (specified as domain=traci.constants.CMD_GET_<DOMAIN>_VARIABLE),
255        which are closer than dist to the object specified by objectID.
256        The parameters map has the same format as for subscribe().
257        """
258        if varIDs is None:
259            varIDs = DOMAIN_BY_ID.get(domain, self)._subscriptionDefault
260        self._connection._subscribeContext(self._contextID, begin, end, objectID, domain, dist, varIDs, parameters)
261
262    def unsubscribeContext(self, objectID, domain, dist):
263        self._connection._subscribeContext(self._contextID, tc.INVALID_DOUBLE_VALUE, tc.INVALID_DOUBLE_VALUE,
264                                           objectID, domain, dist, [], None)
265
266    def getContextSubscriptionResults(self, objectID):
267        return self._connection._getSubscriptionResults(self._contextResponseID).getContext(objectID)
268
269    def getAllContextSubscriptionResults(self):
270        return self._connection._getSubscriptionResults(self._contextResponseID).getContext(None)
271
272    @alias_param(("objectID", "key"), ("objID", "param"))
273    def getParameter(self, objectID, key):
274        """getParameter(string, string) -> string
275
276        Returns the value of the given parameter for the given objectID
277        """
278        return self._getUniversal(tc.VAR_PARAMETER, objectID, "s", key)
279
280    @alias_param(("objectID", "key"), ("objID", "param"))
281    def getParameterWithKey(self, objectID, key):
282        """getParameterWithKey(string, string) -> (string, string)
283
284        Returns the (key, value) tuple of the given parameter for the given objectID
285        """
286        return self._getUniversal(tc.VAR_PARAMETER_WITH_KEY, objectID, "s", key)
287
288    @alias_param("objectID", "objID")
289    def subscribeParameterWithKey(self, objectID, key, begin=tc.INVALID_DOUBLE_VALUE, end=tc.INVALID_DOUBLE_VALUE):
290        """subscribeParameterWithKey(string, string) -> None
291
292        Subscribe for a generic parameter with the given key.
293        """
294        self.subscribe(objectID, (tc.VAR_PARAMETER_WITH_KEY,), begin, end, {tc.VAR_PARAMETER_WITH_KEY: ("s", key)})
295
296    @alias_param(("objectID", "key"), ("objID", "param"))
297    def setParameter(self, objectID, key, value):
298        """setParameter(string, string, string) -> None
299
300        Sets the value of the given parameter to value for the given objectID
301        """
302        self._setCmd(tc.VAR_PARAMETER, objectID, "tss", 2, key, value)
class SubscriptionResults:
 73class SubscriptionResults:
 74
 75    def __init__(self, valueFunc):
 76        self._results = {}
 77        self._contextResults = {}
 78        self._valueFunc = valueFunc
 79
 80    def reset(self):
 81        self._results.clear()
 82        self._contextResults.clear()
 83
 84    def add(self, refID, varID, data):
 85        if refID not in self._results:
 86            self._results[refID] = {}
 87        self._results[refID][varID] = self.parse(varID, data)
 88
 89    def get(self, refID=None):
 90        if refID is None:
 91            return self._results
 92        return self._results.get(refID, {})
 93
 94    def addContext(self, refID, objectID=None, varID=None, value=None):
 95        if refID not in self._contextResults:
 96            self._contextResults[refID] = {}
 97        if objectID is not None and objectID not in self._contextResults[refID]:
 98            self._contextResults[refID][objectID] = {}
 99        if varID is not None and value is not None:
100            self._contextResults[refID][objectID][varID] = value
101
102    def parse(self, varID=None, data=None):
103        return _parse(self._valueFunc, varID, data)
104
105    def getContext(self, refID=None):
106        if refID is None:
107            return self._contextResults
108        return self._contextResults.get(refID, {})
109
110    def __repr__(self):
111        return "<%s, %s>" % (self._results, self._contextResults)
SubscriptionResults(valueFunc)
75    def __init__(self, valueFunc):
76        self._results = {}
77        self._contextResults = {}
78        self._valueFunc = valueFunc
def reset(self):
80    def reset(self):
81        self._results.clear()
82        self._contextResults.clear()
def add(self, refID, varID, data):
84    def add(self, refID, varID, data):
85        if refID not in self._results:
86            self._results[refID] = {}
87        self._results[refID][varID] = self.parse(varID, data)
def get(self, refID=None):
89    def get(self, refID=None):
90        if refID is None:
91            return self._results
92        return self._results.get(refID, {})
def addContext(self, refID, objectID=None, varID=None, value=None):
 94    def addContext(self, refID, objectID=None, varID=None, value=None):
 95        if refID not in self._contextResults:
 96            self._contextResults[refID] = {}
 97        if objectID is not None and objectID not in self._contextResults[refID]:
 98            self._contextResults[refID][objectID] = {}
 99        if varID is not None and value is not None:
100            self._contextResults[refID][objectID][varID] = value
def parse(self, varID=None, data=None):
102    def parse(self, varID=None, data=None):
103        return _parse(self._valueFunc, varID, data)
def getContext(self, refID=None):
105    def getContext(self, refID=None):
106        if refID is None:
107            return self._contextResults
108        return self._contextResults.get(refID, {})
class Domain:
114class Domain:
115
116    def __init__(self, name, cmdGetID, cmdSetID,
117                 subscribeID, subscribeResponseID,
118                 contextID, contextResponseID,
119                 retValFunc=None, deprecatedFor=None,
120                 subscriptionDefault=(tc.TRACI_ID_LIST,)):
121        self._name = name
122        self._cmdGetID = cmdGetID
123        self._cmdSetID = cmdSetID
124        self._subscribeID = subscribeID
125        self._subscribeResponseID = subscribeResponseID
126        self._contextID = contextID
127        self._contextResponseID = contextResponseID
128        self._retValFunc = {tc.VAR_PARAMETER_WITH_KEY: _readParameterWithKey}
129        if retValFunc is not None:
130            self._retValFunc.update(retValFunc)
131        self._deprecatedFor = deprecatedFor
132        self._subscriptionDefault = subscriptionDefault
133        self._connection = None
134        self._isGlobal = True
135        DOMAINS.append(self)
136        DOMAIN_BY_ID[cmdGetID] = self
137        # alias
138        self.DOMAIN_ID = cmdGetID
139
140    def __getattribute__(self, name):
141        attribute = object.__getattribute__(self, name)
142        if not name.startswith("_") and object.__getattribute__(self, "_isGlobal") and callable(attribute):
143            connection = object.__getattribute__(self, "_connection")
144            if connection is not None and connection._traceFile is not None:
145                # Resolve at call time so a saved global method still follows switch().
146                @wraps(attribute)
147                def tracedCall(*args, **kwargs):
148                    selected = object.__getattribute__(self, "_connection")
149                    if selected is None:
150                        return attribute(*args, **kwargs)
151                    return getattr(getattr(selected, object.__getattribute__(self, "_name")), name)(*args, **kwargs)
152                return tracedCall
153        return attribute
154
155    def _register(self, connection, mapping):
156        dom = copy.copy(self)
157        dom._connection = connection
158        dom._isGlobal = False
159        subscriptionResults = SubscriptionResults(self._retValFunc)
160        mapping[self._subscribeResponseID] = subscriptionResults
161        mapping[self._contextResponseID] = subscriptionResults
162        mapping[self._cmdGetID] = subscriptionResults
163        setattr(connection, self._name, dom)
164
165    def _setConnection(self, connection):
166        self._connection = connection
167
168    def _getUniversal(self, varID, objectID="", format="", *values):
169        if self._deprecatedFor:
170            warnings.warn("The domain %s is deprecated, use %s instead." % (self._name, self._deprecatedFor))
171        return _parse(self._retValFunc, varID, self._getCmd(varID, objectID, format, *values))
172
173    def _getCmd(self, varID, objID, format="", *values):
174        if self._connection is None:
175            raise FatalTraCIError("Not connected.")
176        r = self._connection._sendCmd(self._cmdGetID, varID, objID, format, *values)
177        r.readLength()
178        response, retVarID = r.read("!BB")
179        objectID = r.readString()
180        if response - self._cmdGetID != 16 or retVarID != varID or objectID != objID:
181            raise FatalTraCIError("Received answer %s,%s,%s for command %s,%s,%s."
182                                  % (response, retVarID, objectID, self._cmdGetID, varID, objID))
183        return r
184
185    def _setCmd(self, varID, objectID, format="", *values):
186        if self._connection is None:
187            raise FatalTraCIError("Not connected.")
188        self._connection._sendCmd(self._cmdSetID, varID, objectID, format, *values)
189
190    def getIDList(self):
191        """getIDList() -> tuple(string)
192
193        Returns a tuple of all objects in the network.
194        """
195        return self._getUniversal(tc.TRACI_ID_LIST)
196
197    def getIDCount(self):
198        """getIDCount() -> integer
199
200        Returns the number of currently loaded objects.
201        """
202        return self._getUniversal(tc.ID_COUNT)
203
204    def domainID(self):
205        return self._cmdGetID
206
207    def subscribe(self, objectID, varIDs=None, begin=tc.INVALID_DOUBLE_VALUE, end=tc.INVALID_DOUBLE_VALUE,
208                  parameters=None):
209        """subscribe(string, list(integer), double, double, map) -> None
210        The parameters map argument is needed when subscribing to functions that require additional parameters:
211        The map keys must be a subset of the varIDs
212        The map values may take any of the following forms:
213        - a single integer, float or string value (for methods that require only one additional argument of such type)
214        - a tuple where
215          - the first element is a string consisting of format specifiers (see method _pack in module connection)
216          - the remaining elements are the arguments (one per character in the format specifier string)
217        Subscribe to one or more object values for the given interval.
218        """
219        if varIDs is None:
220            varIDs = self._subscriptionDefault
221        self._connection._subscribe(self._subscribeID, begin, end, objectID, varIDs, parameters)
222
223    def unsubscribe(self, objectID):
224        """unsubscribe(string) -> None
225
226        Unsubscribe from receiving object values.
227        """
228        self._connection._subscribe(self._subscribeID, tc.INVALID_DOUBLE_VALUE, tc.INVALID_DOUBLE_VALUE,
229                                    objectID, [], None)
230
231    def getSubscriptionResults(self, objectID):
232        """getSubscriptionResults(string) -> dict(integer: <value_type>)
233
234        Returns the subscription results for the last time step and the given object.
235        If the object id is unknown or the subscription did for any reason return no data,
236        'None' is returned.
237        It is not possible to retrieve older subscription results than the ones
238        from the last time step.
239        """
240        return self._connection._getSubscriptionResults(self._subscribeResponseID).get(objectID)
241
242    def getAllSubscriptionResults(self):
243        """getAllSubscriptionResults() -> dict(string: dict(integer: <value_type>))
244
245        Returns the subscription results for the last time step and all objects of the domain.
246        It is not possible to retrieve older subscription results than the ones
247        from the last time step.
248        """
249        return self._connection._getSubscriptionResults(self._subscribeResponseID).get(None)
250
251    def subscribeContext(self, objectID, domain, dist, varIDs=None,
252                         begin=tc.INVALID_DOUBLE_VALUE, end=tc.INVALID_DOUBLE_VALUE, parameters=None):
253        """subscribeContext(string, int, double, list(integer), double, double, map) -> None
254
255        Subscribe to objects of the given domain (specified as domain=traci.constants.CMD_GET_<DOMAIN>_VARIABLE),
256        which are closer than dist to the object specified by objectID.
257        The parameters map has the same format as for subscribe().
258        """
259        if varIDs is None:
260            varIDs = DOMAIN_BY_ID.get(domain, self)._subscriptionDefault
261        self._connection._subscribeContext(self._contextID, begin, end, objectID, domain, dist, varIDs, parameters)
262
263    def unsubscribeContext(self, objectID, domain, dist):
264        self._connection._subscribeContext(self._contextID, tc.INVALID_DOUBLE_VALUE, tc.INVALID_DOUBLE_VALUE,
265                                           objectID, domain, dist, [], None)
266
267    def getContextSubscriptionResults(self, objectID):
268        return self._connection._getSubscriptionResults(self._contextResponseID).getContext(objectID)
269
270    def getAllContextSubscriptionResults(self):
271        return self._connection._getSubscriptionResults(self._contextResponseID).getContext(None)
272
273    @alias_param(("objectID", "key"), ("objID", "param"))
274    def getParameter(self, objectID, key):
275        """getParameter(string, string) -> string
276
277        Returns the value of the given parameter for the given objectID
278        """
279        return self._getUniversal(tc.VAR_PARAMETER, objectID, "s", key)
280
281    @alias_param(("objectID", "key"), ("objID", "param"))
282    def getParameterWithKey(self, objectID, key):
283        """getParameterWithKey(string, string) -> (string, string)
284
285        Returns the (key, value) tuple of the given parameter for the given objectID
286        """
287        return self._getUniversal(tc.VAR_PARAMETER_WITH_KEY, objectID, "s", key)
288
289    @alias_param("objectID", "objID")
290    def subscribeParameterWithKey(self, objectID, key, begin=tc.INVALID_DOUBLE_VALUE, end=tc.INVALID_DOUBLE_VALUE):
291        """subscribeParameterWithKey(string, string) -> None
292
293        Subscribe for a generic parameter with the given key.
294        """
295        self.subscribe(objectID, (tc.VAR_PARAMETER_WITH_KEY,), begin, end, {tc.VAR_PARAMETER_WITH_KEY: ("s", key)})
296
297    @alias_param(("objectID", "key"), ("objID", "param"))
298    def setParameter(self, objectID, key, value):
299        """setParameter(string, string, string) -> None
300
301        Sets the value of the given parameter to value for the given objectID
302        """
303        self._setCmd(tc.VAR_PARAMETER, objectID, "tss", 2, key, value)
Domain( name, cmdGetID, cmdSetID, subscribeID, subscribeResponseID, contextID, contextResponseID, retValFunc=None, deprecatedFor=None, subscriptionDefault=(0,))
116    def __init__(self, name, cmdGetID, cmdSetID,
117                 subscribeID, subscribeResponseID,
118                 contextID, contextResponseID,
119                 retValFunc=None, deprecatedFor=None,
120                 subscriptionDefault=(tc.TRACI_ID_LIST,)):
121        self._name = name
122        self._cmdGetID = cmdGetID
123        self._cmdSetID = cmdSetID
124        self._subscribeID = subscribeID
125        self._subscribeResponseID = subscribeResponseID
126        self._contextID = contextID
127        self._contextResponseID = contextResponseID
128        self._retValFunc = {tc.VAR_PARAMETER_WITH_KEY: _readParameterWithKey}
129        if retValFunc is not None:
130            self._retValFunc.update(retValFunc)
131        self._deprecatedFor = deprecatedFor
132        self._subscriptionDefault = subscriptionDefault
133        self._connection = None
134        self._isGlobal = True
135        DOMAINS.append(self)
136        DOMAIN_BY_ID[cmdGetID] = self
137        # alias
138        self.DOMAIN_ID = cmdGetID
DOMAIN_ID
def getIDList(self):
190    def getIDList(self):
191        """getIDList() -> tuple(string)
192
193        Returns a tuple of all objects in the network.
194        """
195        return self._getUniversal(tc.TRACI_ID_LIST)

getIDList() -> tuple(string)

Returns a tuple of all objects in the network.

def getIDCount(self):
197    def getIDCount(self):
198        """getIDCount() -> integer
199
200        Returns the number of currently loaded objects.
201        """
202        return self._getUniversal(tc.ID_COUNT)

getIDCount() -> integer

Returns the number of currently loaded objects.

def domainID(self):
204    def domainID(self):
205        return self._cmdGetID
def subscribe( self, objectID, varIDs=None, begin=-1073741824.0, end=-1073741824.0, parameters=None):
207    def subscribe(self, objectID, varIDs=None, begin=tc.INVALID_DOUBLE_VALUE, end=tc.INVALID_DOUBLE_VALUE,
208                  parameters=None):
209        """subscribe(string, list(integer), double, double, map) -> None
210        The parameters map argument is needed when subscribing to functions that require additional parameters:
211        The map keys must be a subset of the varIDs
212        The map values may take any of the following forms:
213        - a single integer, float or string value (for methods that require only one additional argument of such type)
214        - a tuple where
215          - the first element is a string consisting of format specifiers (see method _pack in module connection)
216          - the remaining elements are the arguments (one per character in the format specifier string)
217        Subscribe to one or more object values for the given interval.
218        """
219        if varIDs is None:
220            varIDs = self._subscriptionDefault
221        self._connection._subscribe(self._subscribeID, begin, end, objectID, varIDs, parameters)

subscribe(string, list(integer), double, double, map) -> None The parameters map argument is needed when subscribing to functions that require additional parameters: The map keys must be a subset of the varIDs The map values may take any of the following forms:

  • a single integer, float or string value (for methods that require only one additional argument of such type)
  • a tuple where
    • the first element is a string consisting of format specifiers (see method _pack in module connection)
    • the remaining elements are the arguments (one per character in the format specifier string) Subscribe to one or more object values for the given interval.
def unsubscribe(self, objectID):
223    def unsubscribe(self, objectID):
224        """unsubscribe(string) -> None
225
226        Unsubscribe from receiving object values.
227        """
228        self._connection._subscribe(self._subscribeID, tc.INVALID_DOUBLE_VALUE, tc.INVALID_DOUBLE_VALUE,
229                                    objectID, [], None)

unsubscribe(string) -> None

Unsubscribe from receiving object values.

def getSubscriptionResults(self, objectID):
231    def getSubscriptionResults(self, objectID):
232        """getSubscriptionResults(string) -> dict(integer: <value_type>)
233
234        Returns the subscription results for the last time step and the given object.
235        If the object id is unknown or the subscription did for any reason return no data,
236        'None' is returned.
237        It is not possible to retrieve older subscription results than the ones
238        from the last time step.
239        """
240        return self._connection._getSubscriptionResults(self._subscribeResponseID).get(objectID)

getSubscriptionResults(string) -> dict(integer: )

Returns the subscription results for the last time step and the given object. If the object id is unknown or the subscription did for any reason return no data, 'None' is returned. It is not possible to retrieve older subscription results than the ones from the last time step.

def getAllSubscriptionResults(self):
242    def getAllSubscriptionResults(self):
243        """getAllSubscriptionResults() -> dict(string: dict(integer: <value_type>))
244
245        Returns the subscription results for the last time step and all objects of the domain.
246        It is not possible to retrieve older subscription results than the ones
247        from the last time step.
248        """
249        return self._connection._getSubscriptionResults(self._subscribeResponseID).get(None)

getAllSubscriptionResults() -> dict(string: dict(integer: ))

Returns the subscription results for the last time step and all objects of the domain. It is not possible to retrieve older subscription results than the ones from the last time step.

def subscribeContext( self, objectID, domain, dist, varIDs=None, begin=-1073741824.0, end=-1073741824.0, parameters=None):
251    def subscribeContext(self, objectID, domain, dist, varIDs=None,
252                         begin=tc.INVALID_DOUBLE_VALUE, end=tc.INVALID_DOUBLE_VALUE, parameters=None):
253        """subscribeContext(string, int, double, list(integer), double, double, map) -> None
254
255        Subscribe to objects of the given domain (specified as domain=traci.constants.CMD_GET_<DOMAIN>_VARIABLE),
256        which are closer than dist to the object specified by objectID.
257        The parameters map has the same format as for subscribe().
258        """
259        if varIDs is None:
260            varIDs = DOMAIN_BY_ID.get(domain, self)._subscriptionDefault
261        self._connection._subscribeContext(self._contextID, begin, end, objectID, domain, dist, varIDs, parameters)

subscribeContext(string, int, double, list(integer), double, double, map) -> None

Subscribe to objects of the given domain (specified as domain=traci.constants.CMD_GET__VARIABLE), which are closer than dist to the object specified by objectID. The parameters map has the same format as for subscribe().

def unsubscribeContext(self, objectID, domain, dist):
263    def unsubscribeContext(self, objectID, domain, dist):
264        self._connection._subscribeContext(self._contextID, tc.INVALID_DOUBLE_VALUE, tc.INVALID_DOUBLE_VALUE,
265                                           objectID, domain, dist, [], None)
def getContextSubscriptionResults(self, objectID):
267    def getContextSubscriptionResults(self, objectID):
268        return self._connection._getSubscriptionResults(self._contextResponseID).getContext(objectID)
def getAllContextSubscriptionResults(self):
270    def getAllContextSubscriptionResults(self):
271        return self._connection._getSubscriptionResults(self._contextResponseID).getContext(None)
@alias_param(('objectID', 'key'), ('objID', 'param'))
def getParameter(self, objectID, key):
273    @alias_param(("objectID", "key"), ("objID", "param"))
274    def getParameter(self, objectID, key):
275        """getParameter(string, string) -> string
276
277        Returns the value of the given parameter for the given objectID
278        """
279        return self._getUniversal(tc.VAR_PARAMETER, objectID, "s", key)

getParameter(string, string) -> string

Returns the value of the given parameter for the given objectID

@alias_param(('objectID', 'key'), ('objID', 'param'))
def getParameterWithKey(self, objectID, key):
281    @alias_param(("objectID", "key"), ("objID", "param"))
282    def getParameterWithKey(self, objectID, key):
283        """getParameterWithKey(string, string) -> (string, string)
284
285        Returns the (key, value) tuple of the given parameter for the given objectID
286        """
287        return self._getUniversal(tc.VAR_PARAMETER_WITH_KEY, objectID, "s", key)

getParameterWithKey(string, string) -> (string, string)

Returns the (key, value) tuple of the given parameter for the given objectID

@alias_param('objectID', 'objID')
def subscribeParameterWithKey(self, objectID, key, begin=-1073741824.0, end=-1073741824.0):
289    @alias_param("objectID", "objID")
290    def subscribeParameterWithKey(self, objectID, key, begin=tc.INVALID_DOUBLE_VALUE, end=tc.INVALID_DOUBLE_VALUE):
291        """subscribeParameterWithKey(string, string) -> None
292
293        Subscribe for a generic parameter with the given key.
294        """
295        self.subscribe(objectID, (tc.VAR_PARAMETER_WITH_KEY,), begin, end, {tc.VAR_PARAMETER_WITH_KEY: ("s", key)})

subscribeParameterWithKey(string, string) -> None

Subscribe for a generic parameter with the given key.

@alias_param(('objectID', 'key'), ('objID', 'param'))
def setParameter(self, objectID, key, value):
297    @alias_param(("objectID", "key"), ("objID", "param"))
298    def setParameter(self, objectID, key, value):
299        """setParameter(string, string, string) -> None
300
301        Sets the value of the given parameter to value for the given objectID
302        """
303        self._setCmd(tc.VAR_PARAMETER, objectID, "tss", 2, key, value)

setParameter(string, string, string) -> None

Sets the value of the given parameter to value for the given objectID