def synthesize(spec, symbolic=False): """Return strategy satisfying the specification C{spec}. @type spec: L{GRSpec} or C{str} in structured slugs syntax. @return: If realizable return synthesized strategy, otherwise C{None}. @rtype: C{networkx.DiGraph} """ if isinstance(spec, GRSpec): struct = translate(spec, 'slugs') else: struct = spec import slugs s = slugs.convert_to_slugsin(struct, True) with tempfile.NamedTemporaryFile(delete=False) as fin: fin.write(s) logger.info('\n\n structured slugs:\n\n {struct}'.format( struct=struct) + '\n\n slugs in:\n\n {s}\n'.format(s=s)) realizable, out = _call_slugs(fin.name, synth=True, symbolic=symbolic) if not realizable: return None os.unlink(fin.name) # collect int vars vrs = dict(spec.sys_vars) vrs.update(spec.env_vars) dout = json.loads(out) g = nx.DiGraph() dvars = dout['variables'] for stru, d in dout['nodes'].iteritems(): u = int(stru) state = dict(zip(dvars, d['state'])) g.add_node(u, state=state) for v in d['trans']: g.add_edge(u, v) h = nx.DiGraph() for u, d in g.nodes_iter(data=True): bit_state = d['state'] int_state = _bitfields_to_ints(bit_state, vrs) h.add_node(u, state=int_state) for u, v in g.edges_iter(): h.add_edge(u, v) logger.debug( ('loaded strategy with vertices:\n {v}\n' 'and edges:\n {e}\n').format( v='\n '.join(str(x) for x in h.nodes(data=True)), e=h.edges())) return h
def synthesize(spec, symbolic=False): """Return strategy satisfying the specification C{spec}. @type spec: L{GRSpec} or C{str} in structured slugs syntax. @return: If realizable return synthesized strategy, otherwise C{None}. @rtype: C{networkx.DiGraph} """ if isinstance(spec, GRSpec): struct = translate(spec, 'slugs') else: struct = spec import slugs s = slugs.convert_to_slugsin(struct, True) with tempfile.NamedTemporaryFile(delete=False) as fin: fin.write(s) logger.info('\n\n structured slugs:\n\n {struct}'.format(struct=struct) + '\n\n slugs in:\n\n {s}\n'.format(s=s)) realizable, out = _call_slugs(fin.name, synth=True, symbolic=symbolic) if not realizable: return None os.unlink(fin.name) # collect int vars vrs = dict(spec.sys_vars) vrs.update(spec.env_vars) dout = json.loads(out) g = nx.DiGraph() dvars = dout['variables'] for stru, d in dout['nodes'].iteritems(): u = int(stru) state = dict(zip(dvars, d['state'])) g.add_node(u, state=state) for v in d['trans']: g.add_edge(u, v) h = nx.DiGraph() for u, d in g.nodes_iter(data=True): bit_state = d['state'] int_state = _bitfields_to_ints(bit_state, vrs) h.add_node(u, state=int_state) for u, v in g.edges_iter(): h.add_edge(u, v) logger.debug(('loaded strategy with vertices:\n {v}\n' 'and edges:\n {e}\n').format(v='\n '.join( str(x) for x in h.nodes(data=True)), e=h.edges())) return h
def check_realizable(spec): """Decide realizability of specification. Consult the documentation of L{synthesize} about parameters. @return: True if realizable, False if not, or an error occurs. """ if isinstance(spec, GRSpec): struct = translate(spec, 'slugs') else: struct = spec s = slugs.convert_to_slugsin(struct, True) with tempfile.NamedTemporaryFile(delete=False) as fin: fin.write(s) logger.info('\n\n structured slugs:\n\n {struct}'.format( struct=struct) + '\n\n slugs in:\n\n {s}\n'.format(s=s)) realizable, out = _call_slugs(fin.name, synth=False) return realizable
def check_realizable(spec): """Decide realizability of specification. Consult the documentation of L{synthesize} about parameters. @return: True if realizable, False if not, or an error occurs. """ if isinstance(spec, GRSpec): struct = translate(spec, 'slugs') else: struct = spec s = slugs.convert_to_slugsin(struct, True) with tempfile.NamedTemporaryFile(delete=False) as fin: fin.write(s) logger.info('\n\n structured slugs:\n\n {struct}'.format(struct=struct) + '\n\n slugs in:\n\n {s}\n'.format(s=s)) realizable, out = _call_slugs(fin.name, synth=False) return realizable