示例#1
0
def main():  # pragma: no cover
    parser = create_parser()
    args = parser.parse_args()

    if args.mode != Mode.optimize and (args.type == QueryType.draco
                                       or args.type == QueryType.cql):
        print('Validation only works with full specs.', sys.stderr)
    else:
        logger.info(f'Processing query: {args.query.name} ...')

        if args.type == QueryType.asp:
            input_task = AspTask(args.query.read())
        else:
            # load a task from a spec provided by the user
            query_spec = json.load(args.query)
            d = args.base or os.path.dirname(args.query.name)
            if args.type == QueryType.draco:
                input_task = Task.from_obj(query_spec, d)
            elif args.type == QueryType.cql:
                input_task = Task.from_cql(query_spec, d)
            elif args.type == QueryType.vl:
                input_task = Task.from_vegalite(query_spec, d)

        if args.mode == Mode.violations:
            task = run(input_task,
                       debug=args.debug,
                       files=['define.lp', 'hard.lp', 'soft.lp', 'output.lp'],
                       silence_warnings=True)

            if task:
                print(task.violations, file=args.out)
        elif args.mode == Mode.valid:
            task = run(input_task,
                       debug=args.debug,
                       files=['define.lp', 'hard.lp', 'output.lp'],
                       silence_warnings=True)

            print('valid' if task else 'invalid', file=args.out)
        elif args.mode == Mode.optimize:
            task = run(input_task, debug=args.debug)

            if task:
                print(task.to_vegalite_json(), file=args.out)
                logger.info(f'Cost: {task.cost}')
                outname = 'stringIO' if isinstance(
                    args.out, io.StringIO) else args.out.name
                logger.info(f'Wrote Vega-Lite spec to {outname}')

    # close open files
    if args.query is not sys.stdin:
        args.query.close()

    if args.out is not sys.stdout:
        args.out.close()
示例#2
0
    def test_output_schema(self):
        json_files = [
            os.path.join(EXAMPLES_DIR, fname)
            for fname in os.listdir(EXAMPLES_DIR)
            if fname.endswith('.json') and not fname.endswith('.vl.json')
        ]

        with open('node_modules/vega-lite/build/vega-lite-schema.json') as sf:
            schema = json.load(sf)

            for fname in json_files:
                with open(fname, 'r') as f:
                    query_spec = json.load(f)
                    input_task = Task.from_cql(query_spec,
                                               os.path.dirname(f.name))
                    task = run(input_task)
                    validate(task.to_vegalite(), schema)
示例#3
0
def generate_visual_pairs(partial_full_data, weights):
    # Generate pairs that can be visualized by bug finders
    result = {}
    result["headers"] = {
        "first": {
            "title": "Draco",
            "subtitle": "Draco Prediction"
        },
        "second": {
            "title": "CQL",
            "subtitle": "Compassql Prediction"
        }
    }

    result["specs"] = []
    for case in partial_full_data:
        partial_spec, full_spec = partial_full_data[case]

        draco_rec = run(Task.from_cql(partial_spec), constants=weights)

        if draco_rec is None:
            logger.warning(f'Could not find a spec for {partial_spec}')

            result["specs"].append({
                "first": None,
                "second": full_spec,
                "properties": {
                    "input": partial_spec
                }
            })

            continue

        result["specs"].append({
            "first": draco_rec.to_vegalite(),
            "second": full_spec,
            "properties": {
                "input": partial_spec
            }
        })

    return result