static
Clause conflicts(Clause a, Clause b)
{
  if (!unit_clause(a) || !unit_clause(b))
    return NULL;
  else if (a->literals->sign == b->literals->sign)
    return NULL;
  else {
    Clause empty = NULL;
    Term a_atom = a->literals->atom;
    Term b_atom = b->literals->atom;
    Context ca = get_context();
    Context cb = get_context();
    Trail tr = NULL;
    if (unify(a_atom, ca, b_atom, cb, &tr)) {
      Ilist j = NULL;
      undo_subst(tr);
      empty = get_clause();

      j = ilist_append(j, a->id);
      j = ilist_append(j, 1);
      j = ilist_append(j, b->id);
      j = ilist_append(j, 1);
      empty->justification = resolve_just(j, BINARY_RES_JUST);
      upward_clause_links(empty);
      assign_clause_id(empty);
    }
    free_context(ca);
    free_context(cb);
    return empty;
  }
}  /* conflicts */
static
Clause cd(Clause maj, Clause min)
{
  if (!unit_clause(maj) || !unit_clause(min))
    return NULL;
  else if (!maj->literals->sign || !min->literals->sign)
    return NULL;
  else {
    Term a = ARG(maj->literals->atom,0);
    Term b = ARG(min->literals->atom,0);
    if (ARITY(a) != 2)
      return NULL;
    else {
      Clause resolvent = NULL;
      Term a0 = ARG(a,0);
      Term a1 = ARG(a,1);
      Context ca = get_context();
      Context cb = get_context();
      Trail tr = NULL;
      if (unify(a0, ca, b, cb, &tr)) {
	Term r = apply(a1, ca);
	Term r_atom = build_unary_term(SYMNUM(maj->literals->atom), r);
	Literal r_literal = get_literal();
	Ilist j = NULL;
	r_literal->sign = TRUE;
	r_literal->atom = r_atom;
	resolvent = get_clause();
	append_literal(resolvent, r_literal);
	
	j = ilist_append(j, maj->id);
	j = ilist_append(j, 1);
	j = ilist_append(j, min->id);
	j = ilist_append(j, 1);

	resolvent->justification = resolve_just(j, BINARY_RES_JUST);
	upward_clause_links(resolvent);
	renumber_variables(resolvent, MAX_VARS);

	undo_subst(tr);
      }
      free_context(ca);
      free_context(cb);
      return resolvent;
    }
  }
}  /* cd */
static
Clause resolve(Clash first, Just_type rule)
{
  Clause r = get_clause();
  Clause nuc =  first->nuc_lit->atom->container;
  Ilist j = ilist_append(NULL, nuc->id);
  Clash p;
  int n;

  /* First, include literals in the nucleus. */
  for (p = first; p != NULL; p = p->next, n++) {
    if (!p->clashed)
      append_literal(r, apply_lit(p->nuc_lit, p->nuc_subst));
  }

  r->attributes = cat_att(r->attributes,
			  inheritable_att_instances(nuc->attributes,
						    first->nuc_subst));

  /* Next, include literals in the satellites. */

  n = 1;  /* n-th nucleus literal, starting with 1 */
  for (p = first; p != NULL; p = p->next, n++) {
    if (p->clashed) {
      Literal lit;
      Clause sat = p->sat_lit->atom->container;
      j = ilist_append(j, n);
      j = ilist_append(j, sat->id);
      j = ilist_append(j, lit_position(sat, p->sat_lit));
      for (lit = sat->literals; lit != NULL; lit = lit->next) {
	if (lit != p->sat_lit)
	  append_literal(r, apply_lit(lit,  p->sat_subst));
      }
      r->attributes = cat_att(r->attributes,
			      inheritable_att_instances(sat->attributes,
							p->sat_subst));
    }
  }
  r->justification = resolve_just(j, rule);
  upward_clause_links(r);
  return r;
}  /* resolve */
Beispiel #4
0
/*****************************************************************************
  Returns a set containing all the variables defined by the clauses of this
  flwor expr.
******************************************************************************/
void flwor_expr::get_vars(std::vector<var_expr*>& vars) const
{
  csize numClauses = num_clauses();

  for (csize i = 0; i < numClauses; ++i)
  {
    const flwor_clause& c = *get_clause(i);

    switch (c.get_kind())
    {
    case flwor_clause::for_clause:
    {
      const for_clause* fc = static_cast<const for_clause *>(&c);

      vars.push_back(fc->get_var());

      if (fc->get_pos_var() != NULL)
        vars.push_back(fc->get_pos_var());

      break;
    }
    case flwor_clause::let_clause:
    {
      const let_clause* lc = static_cast<const let_clause *>(&c);
      vars.push_back(lc->get_var());
      break;
    }
    case flwor_clause::window_clause:
    {
      const window_clause* wc = static_cast<const window_clause *>(&c);

      vars.push_back(wc->get_var());

      if (wc->get_win_start() != NULL)
      {
        const flwor_wincond* cond = wc->get_win_start();
        const flwor_wincond::vars& condvars = cond->get_out_vars();

        if (condvars.posvar != NULL) vars.push_back(condvars.posvar);
        if (condvars.curr != NULL) vars.push_back(condvars.curr);
        if (condvars.prev != NULL) vars.push_back(condvars.prev);
        if (condvars.next != NULL) vars.push_back(condvars.next);
      }

      if (wc->get_win_stop() != NULL)
      {
        const flwor_wincond* cond = wc->get_win_stop();
        const flwor_wincond::vars& condvars = cond->get_out_vars();

        if (condvars.posvar != NULL) vars.push_back(condvars.posvar);
        if (condvars.curr != NULL) vars.push_back(condvars.curr);
        if (condvars.prev != NULL) vars.push_back(condvars.prev);
        if (condvars.next != NULL) vars.push_back(condvars.next);
      }

      break;
    }
    case flwor_clause::groupby_clause:
    {
      const groupby_clause* gc = static_cast<const groupby_clause *>(&c);

      flwor_clause::rebind_list_t::const_iterator ite = gc->beginGroupVars();
      flwor_clause::rebind_list_t::const_iterator end = gc->endGroupVars();

      for (; ite != end; ++ite)
      {
        vars.push_back((*ite).second);
      }

      ite = gc->beginNonGroupVars();
      end = gc->endNonGroupVars();

      for (; ite != end; ++ite)
      {
        vars.push_back((*ite).second);
      }

      break;
    }
    case flwor_clause::count_clause:
    {
      const count_clause* cc = static_cast<const count_clause *>(&c);
      vars.push_back(cc->get_var());
      break;
    }
    default:
      break;
    }
  }
}
Beispiel #5
0
bool flwor_expr::compute_is_general()
{
  bool has_where = false;
  bool has_order = false;
  bool has_group = false;

  csize numClauses = num_clauses();

  for (csize i = 0; i < numClauses; ++i)
  {
    const flwor_clause* c = get_clause(i);

    switch (c->get_kind())
    {
    case flwor_clause::for_clause:
    case flwor_clause::let_clause:
    {
      if (has_group || has_where || has_order)
        return true;

      const forlet_clause* flc = static_cast<const forlet_clause*>(c);

      if (flc->is_allowing_empty())
        return true;

      break;
    }
    case flwor_clause::window_clause:
    {
      return true;
    }
    case flwor_clause::where_clause:
    {
      if (has_where || has_group || has_order)
        return true;

      has_where = true;
      break;
    }
    case flwor_clause::orderby_clause:
    {
      if (has_order)
        return true;

      has_order = true;
      break;
    }
    case flwor_clause::groupby_clause:
    {
      if (has_group || has_order)
        return true;

      has_group = true;
      break;
    }
    case flwor_clause::count_clause:
    {
      return true;
    }
    case flwor_clause::materialize_clause:
    {
      break;
    }
    default:
    {
      ZORBA_ASSERT(false);
    }
    }
  }

  return false;
}