def domainForValue(value): """Return a Domain for this type of Scenic value, when possible""" ty = underlyingType(value) if ty is float or ty is int: domain = scalarDomain elif ty is GTACarModel: domain = gtaModelDomain elif ty is WebotsCarModel: domain = webotsModelDomain elif ty is Color: domain = colorDomain elif canCoerceType(ty, Vector): domain = vectorDomain elif ty is str: # We can only handle strings when they come from a finite set of # possibilities; we heuristically detect that here. if isinstance(value, Options): domain = Categorical(*value.options) else: domain = None # we can't ensure the domain is finite else: domain = None # no corresponding Domain known if not needsSampling(value): # We can handle constants of unknown types, but when possible we # convert the value to a VerifAI type. value = convertToVerifaiType(value, strict=False) return Constant(value) return domain
def convertToVerifaiType(value, strict=True): """Attempt to convert a Scenic value to a type known to Verifai""" ty = underlyingType(value) if ty is float or ty is int: return float(value) elif canCoerceType(ty, Vector): return tuple(coerce(value, Vector)) elif ty is GTACarModel: return value elif ty is WebotsCarModel: return value elif ty is CarColor: return value elif strict: # Unknown type, so give up if we're being strict raise RuntimeError( f'attempted to convert Scenic value {value} of unknown type {ty}') else: return value
def convertToVerifaiType(value, strict=True): """Attempt to convert a Scenic value to a type known to VerifAI""" ty = underlyingType(value) if ty is float or ty is int: return float(value) elif ty is list or ty is tuple: return tuple(convertToVerifaiType(e, strict=strict) for e in value) elif issubclass(ty, dict) and not needsSampling(value): return frozendict(value) elif ty is GTACarModel: return value elif ty is WebotsCarModel: return value elif ty is Color: return value elif canCoerceType(ty, Vector): return tuple(coerce(value, Vector)) elif strict: # Unknown type, so give up if we're being strict raise RuntimeError( f'attempted to convert Scenic value {value} of unknown type {ty}') else: return value