Пример #1
0
static void wildcard_predicate(parser_context *context)
{
    enter_state(context, ST_WILDCARD_PREDICATE);

    skip_ws(context);
    if('*' == get_char(context))
    {
        context->result.code = JSONPATH_SUCCESS;
        consume_char(context);
        add_predicate(context, WILDCARD);
    }
    else
    {
        unexpected_value(context, '*');
    }
}
Пример #2
0
static void subscript_predicate(parser_context *context)
{
    enter_state(context, ST_SUBSCRIPT_PREDICATE);

    size_t mark = context->cursor;
    uint_fast32_t subscript = integer(context);
    if(JSONPATH_SUCCESS != context->result.code)
    {
        reset(context, mark);
        return;
    }
    skip_ws(context);
    if(']' != get_char(context))
    {
        reset(context, mark);
        context->result.code = ERR_EXTRA_JUNK_AFTER_PREDICATE;
        return;
    }
    predicate *pred = add_predicate(context, SUBSCRIPT);
    pred->subscript.index = (size_t)subscript;
}
Пример #3
0
int main(int argc, char **argv) {
  int i, j;
  samp_table_t *table = &samp_table;

  char *fr_args[] = {"Person", "Person", NULL};
  char *other_args[] = {"Person", NULL};


  rand_reset(); // May be reset in options
  //  decode_options(argc, argv);
  init_samp_table(table);

  sort_table_t *sort_table = &table->sort_table;
  var_table_t *var_table = &table->var_table;
  atom_table_t *atom_table = &table->atom_table;

  /* Declare all sorts: */
  add_sort(sort_table, "Person");

  /* Declare all constants: */
  add_constant("Ann", "Person", table);
  add_constant("Bob", "Person", table);
  add_constant("Carl", "Person", table);
  add_constant("Dee", "Person", table);
  add_constant("Earl", "Person", table);
  add_constant("Fran", "Person", table);

  /* The "witness" arg is simply a direct (1) / indirect (0) indicator: */

  /* Declare all predicates: */
  add_predicate("Fr", fr_args, 0, table);
  add_predicate("Sm", other_args, 0, table);
  add_predicate("Ca", other_args, 0, table);
  add_predicate("Frl", other_args, 0, table);

  double weight = 1.0;
  double maxweight = DBL_MAX;
  input_atom_t *atom;
  input_fmla_t *fmla;
  input_formula_t *formula;

  /* Now add the formulae - we should inspect this for regularities
     that can be incorporated into a higher-level API.  */

  // add [x] ~Fr(x,x);
  char *var[] = { "x", NULL };
  char *args1[] = { "x", "x", NULL };
  atom = make_atom("Fr", args1, 0);
  fmla = make_fmla(NOT, atom_to_fmla(atom), NULL);
  formula = make_formula(var, fmla);
  add_cnf(NULL, formula, maxweight, NULL, 1);


  // add [x, y, z] Fr(x, y) and Fr(y, z) => Fr(x, z)  0.7;
  char *vars2[] = { "x", "y", "z", NULL };
  char *a1[] = { "x", "y", NULL };
  char *a2[] = { "y", "z", NULL };
  char *a3[] = { "x", "z", NULL };

  fmla = make_fmla(AND,
		   atom_to_fmla(make_atom("Fr", a1, 0)),
		   atom_to_fmla(make_atom("Fr", a2, 0)));
  fmla = make_fmla(IMPLIES, fmla, atom_to_fmla(make_atom("Fr", a3, 0)));
  formula = make_formula(vars2, fmla);
  add_cnf(NULL, formula, 0.7, NULL, 1);



  // add [x, y] Fr(x, y) => ~Frl(x);
  input_atom_t *atom1;
  char *vars3[] = { "x", "y", NULL };
  char *b1[] = { "x", "y", NULL };
  char *b2[] = { "x",  NULL };
  char *b3[] = { "y",  NULL };
  atom = make_atom("Fr", b1, 0);
  atom1 = make_atom("Frl", b2, 0);
  fmla = make_fmla(NOT, atom_to_fmla(atom1), NULL);
  fmla = make_fmla(IMPLIES, atom_to_fmla(atom), fmla);
  formula = make_formula(vars3, fmla);
  add_cnf(NULL, formula, maxweight, NULL, 1);



  // add [x] Frl(x) => Sm(x)  2.3;
  fmla = make_fmla(IMPLIES, atom_to_fmla(make_atom("Frl", b2, 0)), atom_to_fmla(make_atom("Sm", b2, 0)));
  formula = make_formula(b2, fmla);
  add_cnf(NULL, formula, 2.3, NULL, 1);



  // add [x] Sm(x) => Ca(x)  1.5;
  fmla = make_fmla(IMPLIES, atom_to_fmla(make_atom("Sm", b2, 0)), atom_to_fmla(make_atom("Ca", b2, 0)));
  formula = make_formula(b2, fmla);
  add_cnf(NULL, formula, 1.5, NULL, 1);



  // add [x, y] Fr(x, y) implies (Sm(x) iff Sm(y))  1.1;
  fmla = make_fmla(IMPLIES,
		   atom_to_fmla(make_atom("Sm", b2, 0)),
		   make_fmla(IFF,
			     atom_to_fmla(make_atom("Sm", b2, 0)),
			     atom_to_fmla(make_atom("Sm", b3, 0)))
		   );
  formula = make_formula(b1, fmla);
  add_cnf(NULL, formula, 1.1, NULL, 1);



  //add Fr(Ann, Bob);
  char *ann_bob[] = { "Ann", "Bob", NULL };
  formula = make_formula(NULL, atom_to_fmla(make_atom("Fr", ann_bob, 0)));
  add_cnf(NULL, formula, DBL_MAX, NULL, 1);



  //add Fr(Bob, Carl);
  char *bob_carl[] = { "Bob", "Carl", NULL };
  formula = make_formula(NULL, atom_to_fmla(make_atom("Fr", bob_carl, 0)));
  add_cnf(NULL, formula, DBL_MAX, NULL, 1);



  //add Fr(Dee, Earl);
  char *dee_earl[] = { "Dee", "Earl", NULL };
  formula = make_formula(NULL, atom_to_fmla(make_atom("Fr", dee_earl, 0)));
  add_cnf(NULL, formula, DBL_MAX, NULL, 1);


  // Now solve:
  mc_sat(table, lazy_mcsat(), 10000, // get_max_samples(),
	 get_sa_probability(), get_sa_temperature(),
	 get_rvar_probability(), get_max_flips(),
	 get_max_extra_flips(), get_mcsat_timeout(),
	 get_burn_in_steps(), get_samp_interval());

  // Print the results:
  dumptable(ALL, table);
}
Пример #4
0
static void slice_predicate(parser_context *context)
{
    enter_state(context, ST_SLICE_PREDICATE);

    int_fast32_t from = INT_FAST32_MIN;
    int_fast32_t to = INT_FAST32_MAX;
    int_fast32_t extent = 1;
    predicate *pred = add_predicate(context, SLICE);

    if(!look_for(context, ":"))
    {
        parser_trace("slice: uh oh! no ':' found, aborting...");
        context->result.code = ERR_UNSUPPORTED_PRED_TYPE;
        return;
    }

    skip_ws(context);
    if(isdigit(get_char(context)) || '-' == get_char(context) || '+' == get_char(context))
    {
        parser_trace("slice: parsing from value...");
        from = signed_integer(context);
        if(JSONPATH_SUCCESS != context->result.code)
        {
            parser_trace("slice: uh oh! couldn't parse from value, aborting...");
            return;
        }
        parser_trace("slice: found from value: %d", to);
        pred->slice.specified |= SLICE_FROM;
    }
    else
    {
        parser_trace("slice: no from value specified");
    }
    skip_ws(context);
    if(':' != get_char(context))
    {
        parser_trace("slice: uh oh! missing ':' between from and to, aborting...");
        unexpected_value(context, ':');
        return;
    }
    consume_char(context);
    skip_ws(context);
    if(isdigit(get_char(context)) || '-' == get_char(context) || '+' == get_char(context))
    {
        parser_trace("slice: parsing to value...");
        to = signed_integer(context);
        if(JSONPATH_SUCCESS != context->result.code)
        {
            parser_trace("slice: uh oh! couldn't parse to value, aborting...");
            return;
        }
        parser_trace("slice: found to value: %d", to);
        pred->slice.specified |= SLICE_TO;
    }
    else
    {
        parser_trace("slice: no to value specified");
    }
    skip_ws(context);
    if(':' == get_char(context))
    {
        consume_char(context);
        skip_ws(context);
        if(isdigit(get_char(context)) || '-' == get_char(context) || '+' == get_char(context))
        {
            parser_trace("slice: parsing step value...");
            extent = signed_integer(context);
            if(JSONPATH_SUCCESS != context->result.code)
            {
                parser_trace("slice: uh oh! couldn't parse step value, aborting...");
                return;
            }
            if(0 == extent)
            {
                parser_trace("slice: uh oh! couldn't parse step value, aborting...");
                context->result.code = ERR_STEP_CANNOT_BE_ZERO;
                return;
            }
            parser_trace("slice: found step value: %d", extent);
            pred->slice.specified |= SLICE_STEP;
        }
        else
        {
            parser_trace("slice: no step value specified");
        }

    }

    context->result.code = JSONPATH_SUCCESS;
    pred->slice.from = from;
    pred->slice.to = to;
    pred->slice.step = extent;
}