Exemple #1
0
def main():
    parser = argparse.ArgumentParser(
        description=('Script to execute simple query on graph using '
                     'tensor product automata intersection'),
    )
    parser.add_argument(
        "-g", "--graph-file",
        help=('File with graph as edge list. '
              'Format: (vertice, label, vertice)'),
        required=True,
    )
    parser.add_argument(
        "-r", "--regexp-file",
        help=('File with regexp as a query. '
              'e.g. (0 2 (13? 6)*)+  (15 | 14)*)'),
        required=True,
    )
    parser.add_argument(
        "--start-vertices-file",
        help=('File with starting vertices set. '
              'e.g. 0 4 5 8'),
        required=False,
    )
    parser.add_argument(
        "--end-vertices-file",
        help=('File with  vertices set. '
              'e.g. 0 4 5 8'),
        required=False,
    )

    args = parser.parse_args()

    starts = None
    starts_file = args.start_vertices_file
    if starts_file is not None:
        starts = list(map(int, open(starts_file).readline().split()))

    ends = None
    ends_file = args.end_vertices_file
    if ends_file is not None:
        ends = list(map(int, open(ends_file).readline().split()))

    graph = Graph.from_txt(args.graph_file, starts, ends)
    regexp = Regexp.from_txt(args.regexp_file)

    intersection, reachable = algo(graph, regexp)

    labeled_edges = {}
    for label, bool_m in intersection.bool_ms.items():
        labeled_edges[label] = bool_m.nvals

    print("Num of labeled edges:")
    print(labeled_edges)
    print("Pairs of reachable vertices")
    print(reachable)
    def run_test(
        self,
        num,
        ndfa=None,
        starts_spec=False,
        ends_spec=False,
        ans_spec=False,
    ):
        current_test_path = Path(
            Path(__file__).parent,
            self.tests_folder,
            f'test{num}',
        )

        starts = None
        ends = None
        if starts_spec:
            starts_file = Path(current_test_path, f'starts{num}.txt')
            starts = list(map(int, open(starts_file).readline().split()))
        if ends_spec:
            ends_file = Path(current_test_path, f'ends{num}.txt')
            ends = list(map(int, open(ends_file).readline().split()))

        graph = Graph.from_txt(
            Path(current_test_path, f'graph{num}.txt'),
            starts,
            ends,
        )
        regexp = Regexp.from_txt(Path(current_test_path, f'regexp{num}.txt'), )
        intersection, reachable = algo.algo(graph, regexp)

        if ans_spec:
            with open(Path(current_test_path, f'ans{num}.txt'), 'r') as f_ans:
                for line in f_ans.readlines():
                    vs, ve = map(int, line.split())
                    assert (vs, ve) in reachable

        if ndfa is not None:
            print(intersection.fa.to_dict(), intersection.fa.start_states)
            print(ndfa.to_dict(), ndfa.start_states)
            assert intersection.fa.is_equivalent_to(ndfa)