// Standard function takes the query as an input and evaluates the associated function
void KBModActionPlugin::ExecuteAtom::execute(Environment& environment,
		RegistryPtr pregistry, const Tuple& parms,
		const InterpretationConstPtr interpretationPtr) {

	Registry& registry = *pregistry;

	assert(parms.size() > 0);

	std::string command;
	for (int i = 0; i < parms.size(); i++) {
		if (parms[i].isIntegerTerm()) {
			std::ostringstream a_int;
			a_int << parms[i].address;
			std::string int_str = a_int.str();
			command += int_str;
		} else if (parms[i].isConstantTerm() || parms[i].isVariableTerm()) {
			std::string termStr = registry.getTermStringByID(parms[i]);
			command += termStr;
		} else {
			std::string termStr = registry.getTermStringByID(parms[i]);
			if (termStr[0] != '\"' || termStr[termStr.size() - 1] != '\"') {
				throw PluginError(
						"The parameter of the atom #execute should be a quoted string!");
			}
			command += termStr.substr(1, termStr.size() - 2);
		}
	}

	system(command.c_str());

}
/**
 * @brief if the Tuple passed as the second parameter is present in the AnswerSets passed as first parameter
 */
bool ActionPluginFinalCallback::thisAnswerSetContainsThisAction(
		const AnswerSetPtr& answerSetPtr, const Tuple& action_tuple) {

	// used to have only the Action Atoms
	InterpretationPtr intr = InterpretationPtr(new Interpretation(registryPtr));
	intr->getStorage() |= ctxData.myAuxiliaryPredicateMask.mask()->getStorage();
	intr->getStorage() &= answerSetPtr->interpretation->getStorage();

	Interpretation::TrueBitIterator bit, bit_end;
	for (boost::tie(bit, bit_end) = intr->trueBits(); bit != bit_end; ++bit) {

		const Tuple & tuple = registryPtr->ogatoms.getByAddress(*bit).tuple;

		if (tuple.size() - 5 == action_tuple.size()) {

			bool different = false;

			for (int i = 0; i < action_tuple.size() && !different; i++)
				if (tuple[i + 1] != action_tuple[i])
					different = true;

			if (!different)
				return true;

		}

	}

	return false;

}
Exemple #3
0
 bool operator()(const Tuple &t1, const Tuple &t2) const {
   vector<size_t>::const_iterator it = this->order.begin();
   for (; it != this->order.end(); ++it) {
     if (*it >= t1.size()) {
       if (*it < t2.size()) {
         return false;
       }
     } else if (*it >= t2.size()) {
       return true;
     } else if (t1[*it] != t2[*it]) {
       return t1[*it] < t2[*it];
     }
   }
   if (this->total_ordering) {
     Tuple::const_iterator cit1 = t1.begin();
     Tuple::const_iterator cit2 = t2.begin();
     while (cit1 != t1.end() && cit2 != t2.end()) {
       if (*cit1 != *cit2) {
         return *cit1 < *cit2;
       }
       ++cit1;
       ++cit2;
     }
     if (cit1 == t1.end()) {
       return cit2 != t2.end();
     }
   }
   return false;
 }
Exemple #4
0
Adapt::Adapt(Tuple<Space *> spaces_, Tuple<int> proj_norms) : num_act_elems(-1), have_solutions(false), have_errors(false) 
{
  // sanity check
  if (proj_norms.size() > 0 && spaces_.size() != proj_norms.size()) 
    error("Mismatched numbers of spaces and projection types in Adapt::Adapt().");

  this->neq = spaces_.size();

  // sanity checks
  error_if(this->neq <= 0, "Too few components (%d), only %d supported.", this->neq, H2D_MAX_COMPONENTS);
  error_if(this->neq >= H2D_MAX_COMPONENTS, "Too many components (%d), only %d supported.", this->neq, H2D_MAX_COMPONENTS);
  for (int i = 0; i < this->neq; i++) {
    if (spaces_[i] == NULL) error("spaces[%d] is NULL in Adapt::Adapt().", i);
    this->spaces.push_back(spaces_[i]); 
  }

  // reset values
  memset(errors_squared, 0, sizeof(errors_squared));
  memset(form, 0, sizeof(form));
  memset(ord, 0, sizeof(ord));
  memset(sln, 0, sizeof(sln));
  memset(rsln, 0, sizeof(rsln));

  if (proj_norms.size() > 0) {
    for (int i = 0; i < this->neq; i++) {
      switch (proj_norms[i]) {
        case H2D_L2_NORM: form[i][i] = l2_form<double, scalar>; ord[i][i]  = l2_form<Ord, Ord>; break;
        case H2D_H1_NORM: form[i][i] = h1_form<double, scalar>; ord[i][i]  = h1_form<Ord, Ord>; break;
        case H2D_HCURL_NORM: form[i][i] = hcurl_form<double, scalar>; ord[i][i]  = hcurl_form<Ord, Ord>; break;
        case H2D_HDIV_NORM: form[i][i] = hdiv_form<double, scalar>; ord[i][i]  = hdiv_form<Ord, Ord>; break;
        default: error("Unknown projection type in Adapt::Adapt().");
      }
    }
  }
}
Exemple #5
0
	/**
	 * \param Tuple The first numeric tuple.
	 * \param Tuple The second numeric tuple.
	 * 
	 * \return Number The result of the dot product.
	 * 
	 * If one of the tuples is shorter, the remaining of the elements will be interpreted as 0.
	 */
	void VEC_DOT(Machine & machine){
		Number result = 0;
		Tuple b = machine.stack.popTuple();
		Tuple a = machine.stack.popTuple();
		Size min_size = a.size() < b.size() ? a.size() : b.size();
		for(Index i = 0; i < min_size; i++) result += a[i].asNumber() * b[i].asNumber();
		machine.stack.push(result);
	}
Exemple #6
0
void Adapt::init(Tuple<Space *> sp, Tuple<ProjNormType> proj_norms)
{
	_F_
	this->num = sp.size();
  this->proj_norms = proj_norms;

	this->spaces = new Space *[this->num];
	for (int i = 0; i < this->num; i++) spaces[i] = sp[i];

	this->sln = new Solution *[this->num];
	this->rsln = new Solution *[this->num];
	this->errors = new double *[this->num];
	this->norms = new double [this->num];
	for (int i = 0; i < this->num; i++) {
		this->sln[i] = NULL;
		this->rsln[i] = NULL;
		this->errors[i] = NULL;
		this->norms[i] = 0.0;
	}

	form = new_matrix<biform_val_t>(num, num);
	ord = new_matrix<biform_ord_t>(num, num);
	for (int i = 0; i < num; i++)
		for (int j = 0; j < num; j++) {
			if (i == j && proj_norms.size() > 0) {
				switch (proj_norms[i]) {
        case HERMES_H1_NORM: form[i][i] = h1_form<double, scalar>; ord[i][i]  = h1_form<Ord, Ord>; break;
        case HERMES_HCURL_NORM: form[i][i] = hcurl_form<double, scalar>; ord[i][i]  = hcurl_form<Ord, Ord>; break;
        default: error("Unknown projection type in Adapt::Adapt().");
        }  
			}
			else {
				form[i][j] = NULL;
				ord[i][j]  = NULL;
			}
		}

	esort = NULL;
	have_errors = false;

	// default parameters
	h_only = false;
	strategy = 0;
	max_order = H3D_MAX_ELEMENT_ORDER;
	aniso = true;
	exponent = 1.0 / 3.0;

	log_file = NULL;
}
Exemple #7
0
	/**
	 * \param Tuple The first numeric tuple.
	 * \param Tuple The second numeric tuple.
	 * 
	 * \return Tuple The result of an element wise multiplication.
	 * 
	 * If one of the tuples is shorter, the remaining of the elements will be interpreted as 0.
	 */
	void VEC_MUL(Machine & machine){
		Tuple & result = machine.globals[machine.nextInt8()].asTuple() = Tuple();
		Tuple  b = machine.stack.popTuple ();
		Number a = machine.stack.popNumber();
		for(Index i = 0; i < b.size(); i++) result.push(a * b[i].asNumber());
		machine.stack.push(result);
	}
template<class T> void Tuple<T>::append(const Tuple<T>& t) {
    int old_sz = sz;
    reallocate(t.size()+size());
    assert(alloc_sz >= sz);
    for(int i=0; i<t.sz; i++)
	data[i+old_sz] = t.data[i];
}
Exemple #9
0
void Relation::joinTuples(Schema &s1, Schema &s2, Tuple t1, Tuple t2){
	Tuple t3 = t1;
	for(int i = 0; i < (int)t2.size(); i++)
		if(!s1.contains(s2.attributes[i]))
			t3.push_back(t2[i]);
	tuples.insert(t3);
}
Exemple #10
0
bool Tuple::operator < (const Tuple &right) const {
  if (this->size() < right.size()) {
    return true;
  }
  if (this->size() == right.size()) {
    for (std::size_t i = 0; i < this->size(); ++i) {
      if ((*this)[i] < right[i]) {
        return true;
      }
      if (right[i] < (*this)[i]) {
        return false;
      }
    }
  }
  return false;
}
void TupleStorageSubBlock::paranoidInsertTypeCheck(const Tuple &tuple, const AllowedTypeConversion atc) {
#ifdef QUICKSTEP_DEBUG
  assert(relation_.size() == tuple.size());

  Tuple::const_iterator value_it = tuple.begin();
  CatalogRelation::const_iterator attr_it = relation_.begin();

  while (value_it != tuple.end()) {
    switch (atc) {
      case kNone:
        assert(value_it->getType().equals(attr_it->getType()));
        break;
      case kSafe:
        assert(value_it->getType().isSafelyCoercibleTo(attr_it->getType()));
        break;
      case kUnsafe:
        assert(value_it->getType().isCoercibleTo(attr_it->getType()));
        break;
    }

    ++value_it;
    ++attr_it;
  }
#endif
}
bool toVector(PyObjectPtr obj, std::vector<double>& result)
{
    bool knownType = true;

    if(NdArray::check(obj))
    {
        NdArray ndarray = {obj};
        const unsigned size = ndarray.size();
        result.resize(size);

        const int ndim = ndarray.ndim();
        if(ndim != 1)
            throw std::runtime_error("Array object has " + std::to_string(ndim)
                                     + " dimensions, expected 1");
        if(!ndarray.isDouble())
            throw std::runtime_error("Array object does not contain doubles");

        for(unsigned i = 0; i < size; i++)
            result[i] = ndarray.get(i);
    }
    else if(List::check(obj))
    {
        List list = {obj};
        const unsigned size = list.size();
        result.resize(size);

        for(unsigned i = 0; i < size; i++)
        {
            if(!list.isDouble(i))
                throw std::runtime_error(
                    "List object contains item that is not a double at index "
                    + std::to_string(i));
            result[i] = list.get(i);
            throwPythonException();
        }
    }
    else if(Tuple::check(obj))
    {
        Tuple tuple = {obj};
        const unsigned size = tuple.size();
        result.resize(size);

        for(unsigned i = 0; i < size; i++)
        {
            if(!tuple.isDouble(i))
                throw std::runtime_error(
                    "Tuple object contains item that is not a double at index "
                    + std::to_string(i));
            result[i] = tuple.get(i);
            throwPythonException();
        }
    }
    else
    {
        knownType = false;
    }

    return knownType;
}
Exemple #13
0
std::pair<ConstraintTree*,ConstraintTree*>
ConstraintTree::split (const Tuple& tuple)
{
  // assumes that my log vars are already on top
  LogVars lvs (logVars_.begin(), logVars_.begin() + tuple.size());
  ConstraintTree tempCt (logVars_, {tuple});
  return split (lvs, &tempCt, lvs);
}
Exemple #14
0
 bool operator()(const Tuple& tup, std::vector<size_t>& mv) const {
   if ((*this)(tup)) {
     mv.resize(size);
     std::iota(mv.begin(), mv.end(), tup.size() - size);
     return true;
   }
   return false;
 }
Exemple #15
0
// Newton's method for an arbitrary number of equations.
bool NonlinSystem::solve_newton(Tuple<Solution*> u_prev, double newton_tol, 
                                int newton_max_iter, bool verbose, 
                                Tuple<MeshFunction*> mesh_fns) 
{
  // sanity checks
  int n = u_prev.size();
  if (n != this->wf->neq) 
    error("The number of solutions in newton_solve() must match the number of equation in the PDE system.");
  if (this->spaces == NULL) error("spaces is NULL in solve_newton().");
  for (int i=0; i < n; i++) {
    if (this->spaces[i] == NULL) error("spaces[%d] is NULL in solve_newton().", i);
  }
  int n_mesh_fns;
  if (mesh_fns == Tuple<MeshFunction*>()) n_mesh_fns = 0;
  else n_mesh_fns = mesh_fns.size();
  for (int i=0; i<n_mesh_fns; i++) {
    if (mesh_fns[i] == NULL) error("a filter is NULL in solve_newton().");
  }

  int it = 1;
  double res_l2_norm;
  do
  {
    info("---- Newton iter %d:", it); 

    // reinitialize filters
    for (int i=0; i < n_mesh_fns; i++) mesh_fns[i]->reinit();

    // assemble the Jacobian matrix and residual vector,
    // solve the system
    this->assemble();
    this->solve(u_prev);

    // calculate the l2-norm of residual vector
    res_l2_norm = this->get_residual_l2_norm();
    if (verbose) printf("---- Newton iter %d, ndof %d, res. l2 norm %g\n", 
                        it, this->get_num_dofs(), res_l2_norm);

    it++;
  }
  while (res_l2_norm > newton_tol && it <= newton_max_iter);

  // returning "true" if converged, otherwise returning "false"
  if (it <= newton_max_iter) return true;
  else return false;
}
template<class T> bool Tuple<T>::operator == (const Tuple<T>& b) const 
{
 int i;
 if (sz != b.size()) return false;
 for (i=0; i<sz; i++)
   if (!(data[i] == b[i+1])) return false;
 return true;  
}
template<class T> void Tuple<T>::join(Tuple<T>& t) {
    int old_sz = sz;
    reallocate(t.size()+size());
    assert(alloc_sz >= sz);
    for(int i=0; i<t.sz; i++)
	data[i+old_sz] = t.data[i];
    t.clear();
}
Exemple #18
0
 bool operator()(const Tuple& tup) const {
   if (tup.size() >= size) {
     auto& tarr = static_types_array<T...>::arr;
     auto begin = tup.begin();
     return std::equal(begin, begin + size, tarr.begin(), types_only_eq);
   }
   return false;
 }
Exemple #19
0
void Filter::init(Tuple<MeshFunction*> solutions)
{
	this->num = solutions.size();
	if(num > 10)
		error("Attempt to create an instance of Filter with more than 10 MeshFunctions."); 
	for(int i = 0; i < this->num; i++)
		this->sln[i] = solutions.at(i);
  this->init();
}
Exemple #20
0
void Paving::save(const std::string filename, const Names &titles, const Names &names) const {
  if (empty()) return;
  Tuple vs;
  for (nat v = 0; v < names.size(); ++v) {
    nat n = varbox_.var(names[v]);
    if (n < nvars())
      vs.push_back(n);
  }
  std::ostringstream os;
  os << filename;
  for (nat i = 0; i < vs.size(); ++i)
    os << "_" << varbox_.name(vs[i]);
  os << ".dat";
  if (vs.empty()) {
    vs.resize(varbox_.size());
    for (nat v = 0; v < varbox_.size(); ++v)
      vs[v] = v;
  }
  std::ofstream f;
  f.open(os.str().c_str(), std::ofstream::out);
  f << "## File: " << os.str() << std::endl;
  f << "## Type: " << type_ << std::endl;
  f << "## Vars:" << std::endl;
  nat width = 2 * Kodiak::precision();
  for (nat i = 0; i < vs.size(); ++i)
    f << std::setw(width) << varbox_.name(vs[i]);
  f << std::endl;
  for (nat i = 0; i < vs.size(); ++i)
    f << std::setw(width) << varbox_.box()[vs[i]].inf();
  f << std::endl;
  for (nat i = 0; i < vs.size(); ++i)
    f << std::setw(width) << varbox_.box()[vs[i]].sup();
  f << std::endl;
  f << std::endl;
  for (nat i = 0; i < titles.size(); ++i) {
    f << "## " << titles[i] << ": " << size(i) << " boxes " << std::endl;
    if (i < boxes_.size() && boxes_[i].size() > 0)
      save_boxes(f, boxes_[i], vs, width);
    else
      f << std::endl;
  }
  f.close();
  std::cout << "Kodiak (save): Boxes were saved in file " << os.str() << std::endl;
}
Exemple #21
0
	/**
	 * \param Tuple The first numeric tuple.
	 * \param Tuple The second numeric tuple.
	 * 
	 * \return Tuple The result of an element wise addition.
	 * 
	 * If one of the tuples is shorter, the remaining of the elements will be interpreted as 0.
	 */
	void VEC_ADD(Machine & machine){
		Tuple b = machine.stack.popTuple();
		Tuple a = machine.stack.popTuple();
		Size min_size = a.size() < b.size() ? a.size() : b.size();
		Size max_size = a.size() < b.size() ? b.size() : a.size();
		Tuple & largest = a.size() < b.size() ? b : a;
		Tuple result(max_size);
		for(Index i = 0       ; i < min_size; i++) result.push(a[i].asNumber() + b[i].asNumber());
		for(Index i = min_size; i < max_size; i++) result.push(largest[i].asNumber());
		machine.stack.push(result);
	}
Exemple #22
0
	/// \deprecated_mitproto{VEC_ADD}
	void VADD(Machine & machine){
		Tuple b = machine.stack.popTuple();
		Tuple a = machine.stack.popTuple();
		Size min_size = a.size() < b.size() ? a.size() : b.size();
		Size max_size = a.size() < b.size() ? b.size() : a.size();
		Tuple & largest = a.size() < b.size() ? b : a;
		Tuple & result = machine.globals[machine.nextInt8()].asTuple() = Tuple(max_size);
		for(Index i = 0       ; i < min_size; i++) result.push(a[i].asNumber() + b[i].asNumber());
		for(Index i = min_size; i < max_size; i++) result.push(largest[i].asNumber());
		machine.stack.push(result);
	}
void LinearProblem::set_spaces(Tuple<Space *> sp)
{
	_F_
	int n = sp.size();
	if (n <= 0 || n > wf->neq) EXIT("Bad number of spaces.");
        if (n != this->wf->neq) 
          error("Number of spaces must match the number of equations in LinearProblem::set_spaces()");
	for (int i = 0; i < n; i++) spaces[i] = sp[i];
	memset(sp_seq, -1, sizeof(int) * wf->neq);
}
Exemple #24
0
	/**
	 * \param Tuple The numeric tuple to substract from.
	 * \param Tuple The numeric tuple to substract.
	 * 
	 * \return Tuple The result of an element wise subtraction.
	 * 
	 * If one of the tuples is shorter, the remaining of the elements will be interpreted as 0.
	 */
	void VEC_SUB(Machine & machine){
		Tuple b = machine.stack.popTuple();
		Tuple a = machine.stack.popTuple();
		Size min_size     = a.size() < b.size() ? a.size() : b.size();
		Size max_size     = a.size() < b.size() ? b.size() : a.size();
		Tuple & largest     = a.size() < b.size() ? b        : a       ;
		Number padding_sign = a.size() < b.size() ? -1       : 1       ;
		Tuple result(max_size);
		for(Index i = 0       ; i < min_size; i++) result.push(a[i].asNumber() - b[i].asNumber());
		for(Index i = min_size; i < max_size; i++) result.push(padding_sign * largest[i].asNumber());
		machine.stack.push(result);
	}
Exemple #25
0
	/// \deprecated_mitproto
	void VSLICE(Machine & machine){
		Tuple & result = machine.globals[machine.nextInt8()].asTuple() = Tuple();
		Tuple source = machine.stack.popTuple();
		Index start = machine.stack.popNumber();
		Index stop  = machine.stack.popNumber();
		start = start >= 0 ? start : source.size() + start;
		stop  = stop  >= 0 ? stop  : source.size() + stop ;
		for(Index i = start; i < stop; i++) result.push(source[i].asNumber());
		machine.stack.push(result);
	}
Exemple #26
0
NonlinSystem::NonlinSystem(WeakForm* wf_, CommonSolver* solver_, Tuple<Space*> spaces_)
{
  int n = spaces_.size();
  if (n != wf_->neq) 
    error("Number of spaces does not match number of equations in LinSystem::LinSystem().");
  this->init_lin(wf_, solver_);
  this->init_spaces(spaces_);
  //this->alloc_and_zero_vectors();
  this->init_nonlin();
}
Exemple #27
0
Tuple Database::joinTuple(set<pair<int, int>> pairs, Tuple t1, Tuple t2){
	Tuple newTuple;
	for (int i = 0; i<t1.size(); i++){
		newTuple.push_back(t1[i]);
	}
	for (auto i : t2){
		newTuple.push_back(i);
	}
	return newTuple;
}
Exemple #28
0
 bool operator()(const Tuple& tup) const {
   auto& tarr = static_types_array<T...>::arr;
   if (tup.size() >= (sizeof...(T) - wc_count)) {
     auto fpush = [](const typename Tuple::const_iterator&) {};
     auto fcommit = [] {};
     auto frollback = [] {};
     return match(tup.begin(), tup.end(), tarr.begin(), tarr.end(),
            fpush, fcommit, frollback);
   }
   return false;
 }
Exemple #29
0
elimination_info::elimination_info(const Tuple<Relation> &sets) {
    int max_arity=0;
    for(int i = 1; i <= sets.size(); i++) {
        Relation R = sets[i];
        assert(R.is_set());
        for(DNF_Iterator D(R.query_DNF()); D; D++)
            for(Constraint_Iterator c(*D); c; c++)
                max_arity = omega::max(max_arity,max_fs_arity(*c));
    }
    never_eliminate_lt = max_arity;
}
Exemple #30
0
bool Tuple::operator == (const Tuple &right) const {
  if (this->size() != right.size()) {
    return false;
  }
  for (std::size_t i = 0; i < this->size(); ++i) {
    if ((*this)[i] != right[i]) {
      return false;
    }
  }
  return true;
}