/* PUBLIC */
Plist formula_to_clauses(Formula f)
{
  Plist lst = NULL;
  if (f->type == AND_FORM) {
    int i;
    for (i = 0; i < f->arity; i++)
      lst = plist_append(lst, formula_to_clause(f->kids[i]));
  }
  else if (f->type == OR_FORM || f->type == NOT_FORM || f->type == ATOM_FORM)
    lst = plist_append(lst, formula_to_clause(f));
  return lst;
}  /* formula_to_clauses */
Exemplo n.º 2
0
/* PUBLIC */
Plist process_distinct_terms(Plist distinct)
{
  Plist p;
  Plist forms = NULL;
  for (p = distinct; p; p = p->next) {
    Term list = p->v;
    if (!proper_listterm(list))
      fatal_error("process_distinct_terms: lists must be proper, e.g., [a,b,c].\n");
    while (!nil_term(list)) {
      Term a = ARG(list,0);
      Term rest = ARG(list,1);
      while (!nil_term(rest)) {
	Term b = ARG(rest,0);
	Term neq = build_unary_term_safe(not_sym(),
					 build_binary_term_safe(eq_sym(),
								copy_term(a),
								copy_term(b)));
	Formula f = term_to_formula(neq);
	zap_term(neq);
	forms = plist_append(forms, f);
	rest = ARG(rest, 1);
      }
      list = ARG(list, 1);
    }
  }
  return forms;
}  /* process_distinct_terms */
Exemplo n.º 3
0
/* PUBLIC */
Plist plist_of_subterms(Term t)
{
  Plist subterms = NULL;
  int i;
  for (i = 0; i < ARITY(t); i++)
    subterms = plist_append(subterms, ARG(t,i));
  return subterms;
}  /* plist_of_subterms */
static
Plist cd_given(Clause given, Plist lst)
{
  Plist resolvents = NULL;
  Plist p;

  for (p = lst; p != NULL; p = p->next) {
    Clause other = p->v;

    Clause result = cd(given, other);
    if (result)
      resolvents = plist_append(resolvents, result);

    result = cd(other, given);
    if (result)
      resolvents = plist_append(resolvents, result);
  }
  return resolvents;
}  /* cd_given */
/* PUBLIC */
Plist nonneg_clauses(Plist clauses)
{
  Plist nonneg = NULL;
  Plist p;
  for (p = clauses; p; p = p->next) {
    Topform c = p->v;
    if (!negative_clause(c->literals))
      nonneg = plist_append(nonneg, c);
  }
  return nonneg;
}  /* nonneg_clauses */
static
Plist pickers_for_clause(Topform c)
{
  Plist pickers = NULL;
  int i;
  for (i = 0; i < NUM_PICKERS; i++) {
    Picker p = &(Pickers[i]);
    if (p->ratio_part != 0 && (!c || (*(p->recognizer))(c)))
      pickers = plist_append(pickers, p);
  }
  return pickers;
}  /* pickers_for_clause */
/* PUBLIC */
Plist listterm_to_tlist(Term t)
{
  if (!proper_listterm(t))
    return NULL;
  else {
    Plist p = NULL;
    while (cons_term(t)) {
      p = plist_append(p, ARG(t, 0));
      t = ARG(t,1);
    }
    return p;
  }
}  /* listterm_to_tlist */
int main(int argc, char **argv)
{
  FILE *rule_fp;
  Topform c;
  Term t;
  int i;
  Clause_eval compiled_rule;
  Plist rules = NULL;

  init_standard_ladr();
  init_weight(NULL, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0);
  i = register_attribute("label",  STRING_ATTRIBUTE);  /* ignore these */
  i = register_attribute("answer", TERM_ATTRIBUTE);  /* ignore these */

  rule_fp = fopen(argv[1], "r");
  if (rule_fp == NULL)
    fatal_error("test_clause_eval, rule file cannot be opened for reading");

  t = read_term(rule_fp, stderr);  /* get first rule */

  while (t != NULL) {
    compiled_rule = compile_clause_eval_rule(t);
    rules = plist_append(rules, compiled_rule);
    fwrite_term_nl(stdout, t);
    zap_term(t);
    t = read_term(rule_fp, stderr);
  }
  fclose(rule_fp);

  /* Evaluate each clause on stdin. */

  c = read_clause(stdin, stderr);

  while (c != NULL && !end_of_list_clause(c)) {
    Plist p;
    c->weight = clause_weight(c->literals);
    for (p = rules; p; p = p->next) {
      Clause_eval rule = p->v;
      BOOL result = eval_clause_in_rule(c, rule);
      printf("%d  (wt=%.3f) : ", result, c->weight);
      fwrite_clause(stdout, c, CL_FORM_BARE);
    }
    printf("\n");
    
    zap_topform(c);
    c = read_clause(stdin, stderr);
  }
  
  exit(0);
}  /* main */
Exemplo n.º 9
0
/* PUBLIC */
Plist free_vars_term(Term t, Plist vars)
{
  if (VARIABLE(t))
    fatal_error("free_vars_term, VARIABLE term");

  if (ARITY(t) == 0) {
    if (variable_name(sn_to_str(SYMNUM(t))) && !tlist_member(t, vars))
      vars = plist_append(vars, copy_term(t));
  }
  else {
    int i;
    for (i = 0; i < ARITY(t); i++)
      vars = free_vars_term(ARG(t,i), vars);
  }
  return vars;
}  /* free_vars_term */
Exemplo n.º 10
0
static void load_plugin_conf(conf_llist *plugin)
{
	DIR *d;

	/* init plugin list */
	plist_create(plugin);

	/* read configs */
	d = opendir(plugin_dir);
	if (d) {
		struct dirent *e;

		while ((e = readdir(d))) {
			plugin_conf_t config;
			char fname[PATH_MAX];

			// Don't run backup files, hidden files, or dirs
			if (e->d_name[0] == '.' || count_dots(e->d_name) > 1)
				continue;

			snprintf(fname, sizeof(fname), "%s%s",
				plugin_dir, e->d_name);

			clear_pconfig(&config);
			if (load_pconfig(&config, fname) == 0) {
				/* Push onto config list only if active */
				if (config.active == A_YES)
					plist_append(plugin, &config);
				else
					free_pconfig(&config);
			} else
				syslog(LOG_ERR, 
					"Skipping %s plugin due to errors",
					e->d_name);
		}
		closedir(d);
	}
}
Exemplo n.º 11
0
static
int possible_model(void)
{
  if (flag(Opt->arithmetic)) {
    if (!check_with_arithmetic(Ground_clauses))
      return SEARCH_GO_NO_MODELS;
  }
  else if (!check_that_ground_clauses_are_true())
    fatal_error("possible_model, bad model found");

  {
    static int next_message = 1;
    Total_models++;
    Mstats.current_models++;

    if (flag(Opt->return_models)) {
      Term modelterm = interp_term();
      Interp model = compile_interp(modelterm, FALSE);
      zap_term(modelterm);
      Models = plist_append(Models, model);
    }

    if (flag(Opt->print_models))
      print_model_standard(stdout, TRUE);
    else if (flag(Opt->print_models_tabular))
      p_model(FALSE);
    else if (next_message == Total_models) {
      printf("\nModel %d has been found.\n", Total_models);
      next_message *= 10;
    }
    fflush(stdout);
    if (parm(Opt->max_models) != -1 && Total_models >= parm(Opt->max_models))
      return SEARCH_MAX_MODELS;
    else
      return SEARCH_GO_MODELS;
  }
}  /* possible_model */
Exemplo n.º 12
0
static void reconfigure(void)
{
	int rc;
	daemon_conf_t tdc;
	conf_llist tmp_plugin;
	lnode *tpconf;

	/* Read new daemon config */
	rc = load_config(&tdc, config_file);
	if (rc == 0) {
		if (tdc.q_depth > daemon_config.q_depth) {
			increase_queue_depth(tdc.q_depth);
			daemon_config.q_depth = tdc.q_depth;
		}
		daemon_config.overflow_action = tdc.overflow_action;
		reset_suspended();
		/* We just fill these in because they are used by this
		 * same thread when we return
		 */
		daemon_config.node_name_format = tdc.node_name_format;
		free((char *)daemon_config.name);
		daemon_config.name = tdc.name;
	}

	/* The idea for handling SIGHUP to children goes like this:
	 * 1) load the current config in temp list
	 * 2) mark all in real list unchecked
	 * 3) for each one in tmp list, scan old list
	 * 4) if new, start it, append to list, mark done
	 * 5) else check if there was a change to active state
	 * 6) if so, copy config over and start
	 * 7) If no change, send sighup to non-builtins and mark done
	 * 8) Finally, scan real list for unchecked, terminate and deactivate
	 */
	load_plugin_conf(&tmp_plugin);
	plist_mark_all_unchecked(&plugin_conf);

	plist_first(&tmp_plugin);
	tpconf = plist_get_cur(&tmp_plugin);
	while (tpconf && tpconf->p) {
		lnode *opconf;
		
		opconf = plist_find_name(&plugin_conf, tpconf->p->name);
		if (opconf == NULL) {
			/* We have a new service */
			if (tpconf->p->active == A_YES) {
				tpconf->p->checked = 1;
				plist_last(&plugin_conf);
				plist_append(&plugin_conf, tpconf->p);
				free(tpconf->p);
				tpconf->p = NULL;
				start_one_plugin(plist_get_cur(&plugin_conf));
			}
		} else {
			if (opconf->p->active == tpconf->p->active) {
				if (opconf->p->type == S_ALWAYS)
					kill(opconf->p->pid, SIGHUP);
				opconf->p->checked = 1;
			} else {
				/* A change in state */
				if (tpconf->p->active == A_YES) {
					/* starting - copy config and exec */
					free_pconfig(opconf->p);
					free(opconf->p);
					opconf->p = tpconf->p;
					opconf->p->checked = 1;
					start_one_plugin(opconf);
					tpconf->p = NULL;
				}
			}
		}

		tpconf = plist_next(&tmp_plugin);
	}

	/* Now see what's left over */
	while ( (tpconf = plist_find_unchecked(&plugin_conf)) ) {
		/* Anything not checked is something removed from the config */
		tpconf->p->active = A_NO;
		kill(tpconf->p->pid, SIGTERM);
		tpconf->p->pid = 0;
		tpconf->p->checked = 1;
	}
	
	/* Release memory from temp config */
	plist_first(&tmp_plugin);
	tpconf = plist_get_cur(&tmp_plugin);
	while (tpconf) {
		free_pconfig(tpconf->p);
		tpconf = plist_next(&tmp_plugin);
	}
	plist_clear(&tmp_plugin);
}
int main(int argc, char **argv)
{
  Plist sos, goals, usable, p;
  int given_count = 0;
  int generated_count = 0;
  int kept_count = 0;
  int just_att_id;

  init_standard_ladr();

  just_att_id = register_attribute("just", TERM_ATTRIBUTE);

  sos    = read_clause_list(stdin, stderr, TRUE);
  goals  = read_clause_list(stdin, stderr, TRUE);
  usable = NULL;

  fwrite_clause_list(stdout, sos, "sos", CL_FORM_BARE);
  fwrite_clause_list(stdout, goals, "goals", CL_FORM_BARE);

  for (p = sos; p; p = p->next) {
    Clause c = p->v;
    Term just = get_term_attribute(c->attributes, just_att_id, 1);
    c->weight = clause_symbol_count(c);
    if (just) {
      int i;
      for (i = 0; i < ARITY(just); i++) {
	Term arg = ARG(just,i);
	int val;
	if (term_to_int(arg, &val)) {
	  printf("justfication arg = %d\n", val);
	}
      }
    }
  }

  while (sos != NULL) {
    Plist resolvents, p;
    Clause given = extract_given_clause(&sos);  /* updates sos */
    given_count++;
    printf("given #%d (wt=%d): ", given_count, given->weight);
    fwrite_clause(stdout, given, CL_FORM_STD);
    usable = plist_append(usable, given);

    resolvents = cd_given(given, usable);
    for (p = resolvents; p; p = p->next) {
      Clause res = p->v;
      generated_count++;
      if (subsumed_by_member(res, usable) ||
	  subsumed_by_member(res, sos))
	delete_clause(res);
      else {
	Clause empty_clause;
	kept_count++;
	assign_clause_id(res);
	res->weight = clause_symbol_count(res);
    
	sos = plist_append(sos, res);
	printf("kept (wt=%d): ", res->weight);
	fwrite_clause(stdout, res, CL_FORM_BARE);

	empty_clause = conflicts_with_member(res, goals);
	if (empty_clause) {
	  Plist proof = get_clause_ancestors(empty_clause);
	  printf("\n-------- PROOF --------\n");
	  for (p = proof; p; p = p->next)
	    fwrite_clause(stdout, p->v, CL_FORM_BARE);
	  printf("\n-------- end of proof  -------\n");
	  zap_plist(proof);  /* shallow */
	  exit(0);
	}
      }
    }
  }
  printf("exiting %s\n", PROGRAM_NAME);
  exit(0);
}  /* main */