void NameResolver::resolveUnboundNames() { // Resolve unresolved global names. AtomSet seen; for (size_t i = 0; i < unresolved_names_.length(); i++) { NameProxy *proxy = unresolved_names_[i]; Symbol *sym = globals_->lookup(proxy->name()); if (!sym) { AtomSet::Insert p = seen.findForAdd(proxy->name()); if (p.found()) continue; seen.add(p, proxy->name()); if (!cc_.options().SkipResolution) { cc_.report(proxy->loc(), rmsg::name_not_found) << proxy->name(); } continue; } proxy->bind(sym); } }
void TestRacerNRQL::runRacerRetrieveTest() { std::stringstream sst; Tuple tup; tup.push_back(Term("X")); tup.push_back(Term("Y")); AtomSet as; AtomPtr ap1(new Atom("foo(X)")); AtomPtr ap2(new Atom("moo(X,Y)")); as.insert(ap1); as.insert(ap2); KBManager kb("DEFAULT"); DLQuery::shared_pointer dlq(new DLQuery(Ontology::createOntology(test),as,tup)); Query q(kb,dlq,Term(""),Term(""),Term(""),Term(""),AtomSet()); NRQLRetrieve<NRQLConjunctionBuilder> nrql(q); sst << nrql; std::string s = sst.str(); std::cout << s << std::endl; CPPUNIT_ASSERT(s == "(retrieve ($?X $?Y) (and ($?X $?Y |http://www.test.com/test#moo|) ($?X |http://www.test.com/test#foo|)) :abox DEFAULT)"); }
void PositionConstraint::setup(const AtomSet& atoms) { // find position in tau array corresponding to atom name1 is1_ = atoms.is(name1_); ia1_ = atoms.ia(name1_); assert(is1_>=0); assert(ia1_>=0); }
bool Sequencer::adjoin(Seq* newseq) { if (bContradictory) { delete newseq; return false; } if (newseq->isEmpty()) { delete newseq; seq.flush(); bContradictory = true; return false; } if (newseq->isTrivial()) { delete newseq; return true; } int cur = seq.size(); seq.push(newseq); AtomSet common; LOOP: while (cur < seq.size()) { for (size_t i=cur; 0 < i--; ) { if (seq[cur]->implies(*seq[i])) { delete seq[i]; seq.erase(seq.begin() + i); cur--; } else if (seq[i]->implies(*seq[cur])) { delete seq[cur]; seq.erase(seq.begin() + cur); goto LOOP; } } for (size_t j=cur; 0 < j--; ) { Seq res; res.conj = seq[cur]->conj | seq[j]->conj; res.disj = seq[cur]->disj | seq[j]->disj; common = res.conj & res.disj; if (common.size() == 1) { res.conj.erase(res.conj.find(common[0])); res.disj.erase(res.disj.find(common[0])); if (res.isEmpty()) { seq.flush(); seq.push(new Seq(res)); bContradictory = true; return false; } seq.push(new Seq(res)); } } cur++; } return true; }
void ConstraintSet::enforce(AtomSet& atoms) { vector<vector<double> > r0,rp,v0; setup(atoms); atoms.get_positions(r0); rp=r0; atoms.get_velocities(v0); enforce_r(r0,rp); atoms.set_positions(rp); enforce_v(r0,v0); atoms.set_velocities(v0); }
void TorsionConstraint::setup(const AtomSet& atoms) { // find position in tau array corresponding to atom name1 is1_ = atoms.is(name1_); ia1_ = atoms.ia(name1_); assert(is1_>=0); assert(ia1_>=0); m1_ = atoms.species_list[is1_]->mass() * 1822.89; assert(m1_>0.0); m1_inv_ = 1.0 / m1_; is2_ = atoms.is(name2_); ia2_ = atoms.ia(name2_); assert(is2_>=0); assert(ia2_>=0); m2_ = atoms.species_list[is2_]->mass() * 1822.89; assert(m2_>0.0); m2_inv_ = 1.0 / m2_; is3_ = atoms.is(name3_); ia3_ = atoms.ia(name3_); assert(is3_>=0); assert(ia3_>=0); m3_ = atoms.species_list[is3_]->mass() * 1822.89; assert(m3_>0.0); m3_inv_ = 1.0 / m3_; is4_ = atoms.is(name4_); ia4_ = atoms.ia(name4_); assert(is4_>=0); assert(ia4_>=0); m4_ = atoms.species_list[is4_]->mass() * 1822.89; assert(m4_>0.0); m4_inv_ = 1.0 / m4_; }
PosAtomSet* PosLogProg::nextPosSmodel(Smodels* smodels){ if (smodels->model ()){ // Returns 0 when there is no more smodel AtomSet atomSet; // the non-possibilistic stable model Node* nd = smodels->program.atoms.head(); for (; nd; nd = nd->next){ if (nd->atom->Bpos) { // The atom is in the stable model AtomExt * ae = (AtomExt*) nd->atom; // cast atomSet.insert(ae->getMyAtom()); } } return this->possCn(&atomSet); //the correponding possibilistic stable model } else return NULL; // there is no more possibilistic smodel }
int AtomSet::operator< (const AtomSet& atomset2) const { if (this->size() < atomset2.size()) // < { return true; } else if (this->size() > atomset2.size()) // > { return false; } else // same size, they can still be < or >= { // find first mismatch std::pair<AtomSet::const_iterator, AtomSet::const_iterator> result; result = std::mismatch(this->begin(), this->end(), atomset2.begin()); // no mismatch: ==, otw. check if the found mismatch is < or >= return result.first == this->end() ? false : *result.first < *result.second; } }
PosRuleList* AtomSet::reduct_more(){ PosRuleList* reduct= new PosRuleList; PosRuleVector* rules; PosRule* rule; AtomSet::iterator i = this->begin(); for (; i != this->end(); i++) { rules = (*i)->getHeadRules(); PosRuleVector::iterator iterRule = rules->begin(); for (; iterRule != rules->end(); iterRule++){ rule = *iterRule; AtomSet* rAtomSet = rule->getBodyMinus(); if (rAtomSet->emptyIntersection(this)){ if (this->includes(rule->getBodyPlus())){ reduct->push_front(rule); rule->setCounter();// gives the number of positive atoms in each rule (which are not in the - initialy empty - possModel) } } } } return reduct; }
void TestRacerNRQL::runRacerPremiseRetrieveTest() { std::stringstream sst; Tuple tup; tup.push_back(Term("X")); tup.push_back(Term("Y")); AtomSet as; AtomPtr ap1(new Atom("foo(X)")); AtomPtr ap2(new Atom("moo(X,Y)")); as.insert(ap1); as.insert(ap2); AtomSet ints; AtomPtr ap4(new Atom("pc(foo,a)")); AtomPtr ap5(new Atom("pr(moo,a,b)")); ints.insert(ap4); ints.insert(ap5); KBManager kb("DEFAULT"); DLQuery::shared_pointer dlq(new DLQuery(Ontology::createOntology(test),as,tup)); Query q(kb,dlq,Term("pc"),Term(""),Term("pr"),Term(""),ints); NRQLRetrieveUnderPremise<NRQLConjunctionBuilder> nrql(q); sst << nrql; std::string s = sst.str(); std::cout << s << std::endl; URI u(testuri, true); // absolute pathname CPPUNIT_ASSERT(s == "(retrieve-under-premise ((related |http://www.test.com/test#a| |http://www.test.com/test#b| |http://www.test.com/test#moo|) (instance |http://www.test.com/test#a| |http://www.test.com/test#foo|)) ($?X $?Y) (and ($?X $?Y |http://www.test.com/test#moo|) ($?X |http://www.test.com/test#foo|)) :abox |" + u.getString() + "|)"); }
void UniqueLinSolveAtom::retrieve(const Query& query, Answer& answer) throw (PluginError) { Tuple parms = query.getInputTuple(); std::string matrixPred = ""; std::string constantPred = ""; int argc = 2; char* argv[] = {"-linkname", "math -mathlink"}; //check number and type of arguments if (parms.size()!= 2) { throw PluginError("Wrong number of arguments"); } else { if(parms[0].isSymbol() && parms[1].isSymbol()) { matrixPred = parms[0].getString(); //std::cout << "Matrixpraedikat: " << matrixPred << std::endl; constantPred = parms[1].getString(); //std::cout << "Vektorpraedikat: " << vectorPred << std::endl; } else { throw PluginError("Wrong type of arguments"); } } //get complete Interpretation of given predicates in query AtomSet totalInt = query.getInterpretation(); AtomSet matrixInt; AtomSet constantInt; if (totalInt.empty()) { throw PluginError("Could not find any interpretion"); } else { // separate interpretation into facts of first predicate (matrix) totalInt.matchPredicate(matrixPred, matrixInt); // and into facts of second predicate (vector) totalInt.matchPredicate(constantPred, constantInt); } int mRows = 0; int mColumns = 0; int cRows = 0; int cColumns = 0; evaluateMatrix(matrixInt, mRows, mColumns); evaluateVector(constantInt, cRows, cColumns); if(mRows != cRows) throw PluginError("Coefficient matrix and target vector(s) or matrix do not have the same dimensions."); std::vector <std::vector <std::string> > matrix(mRows); for(int i = 0; i < mRows; i++) matrix[i].resize(mColumns); std::vector <std::vector <std::string> > constants(cRows); for (int i = 0; i < cRows; i++) constants[i].resize(cColumns); //write the values of the Atoms in the Interpretation into std::vectors for further processing convertMatrixToVector(matrixInt, mRows, mColumns, matrix); convertMatrixToVector(constantInt, cRows, cColumns, constants); //check if matrix and target vector or matrix are fully defined checkVector(matrix, mRows, mColumns, matrixPred); checkVector(constants, cRows, cColumns, constantPred); //convert matrix to MatrixRank-expression and calculate rank of coefficient matrix A std::string coeffMRankExpr = toMatrixRankExpr(matrix, mRows, mColumns); //std::cout << "MatrixRank expression: " << coeffMRankExpr << std::endl; int coeffMRank = calculateRank(argc, argv, coeffMRankExpr); //convert matrix A and target b to MatrixRank-expression and calculate rank //of extended coefficient matrix [A,b] std::string extendedMRankExpr = toMatrixRankExpr(matrix, mRows, mColumns, constants, cRows, cColumns); //std::cout << "Extended MatrixRank expression: " << extendedMRankExpr << std::endl; int extCoeffMRank = calculateRank(argc, argv, extendedMRankExpr); //compare calculated ranks and number of matrix colums, iff they are equal, //a unique solution for the matrix equation exists if ((coeffMRank == extCoeffMRank) && (coeffMRank == mColumns)) { std::string linSolExpr = toLinearSolveExpr(matrix, mRows, mColumns, constants, cRows, cColumns); std::vector <std::string> result; result = calculateSolution(argc, argv, linSolExpr); if(result.size() != mColumns*cColumns) throw PluginError("Wrong number of arguments in result vector"); Tuple out; int index = 0; //fill the result values with correct indices into Tuple out //and add all Tuples to Answer for (int r = 1; r <= mColumns; r++) { for(int c = 1; c<= cColumns; c++) { out.push_back(Term(r)); out.push_back(Term(c)); out.push_back(Term(result[index],true)); answer.addTuple(out); out.clear(); index++; } } } }
bool AtomSet::operator== (const AtomSet& atomset2) const { return ((this->size() == atomset2.size()) && std::equal(this->begin(), this->end(), atomset2.begin())); }
void ExtendedIdListItemSet::createExtendedIdListItemSet(AtomSet itemSet, AtomSet innerItemSet) { AtomList itemList; itemSet.addAtomsToAtomList(itemList); innerItemSet.addAtomsToAtomList(itemList); _atomSets = {AtomSet(itemList)}; }
void ExtendedIdListItemSet::createPreviousNextItemSet(AtomSet itemSet, AtomSet innerItemSet) { AtomList previousItemList, nextItemList; itemSet.addAtomsToAtomList(previousItemList); innerItemSet.addAtomsToAtomList(nextItemList); _atomSets = {AtomSet(previousItemList), AtomSet(nextItemList)}; }
bool ConstraintSet::define_constraint(AtomSet &atoms, int argc, char **argv) { enum constraint_type { unknown, position_type, distance_type, angle_type, torsion_type } type = unknown; const double position_tolerance = 1.0e-7; const double distance_tolerance = 1.0e-7; const double angle_tolerance = 1.0e-4; const bool oncoutpe = ctxt_.oncoutpe(); // argv[0] == "constraint" // argv[1] == "define" // argv[2] == {"position", "distance", "angle", "torsion"} // argv[3] == constraint name // argv[4-(5,6,7)] == atom names // argv[{5,6,7}] == {distance,angle,angle} // argv[{6,7,8}] == velocity if ( argc < 2 ) { if ( oncoutpe ) { cout << " Use: constraint define position constraint_name atom_name" << endl; cout << " Use: constraint define distance constraint_name " << "atom_name1 atom_name2 distance_value [velocity]" << endl; cout << " constraint define angle constraint_name " << "name1 name2 name3 angle_value [velocity]" << endl; cout << " constraint define torsion constraint_name " << "name1 name2 name3 name4 angle_value" << " [velocity] " << endl; } return false; } const string constraint_type = argv[2]; if ( constraint_type == "position" ) { type = position_type; } else if ( constraint_type == "distance" ) { type = distance_type; } else if ( constraint_type == "angle" ) { type = angle_type; } else if ( constraint_type == "torsion" ) { type = torsion_type; } else { if ( oncoutpe ) cout << " Incorrect constraint type " << constraint_type << endl; return false; } if ( type == position_type ) { // define position name A if ( argc != 5 ) { if ( oncoutpe ) cout << " Incorrect number of arguments for position constraint" << endl; return false; } string name = argv[3]; string name1 = argv[4]; Atom *a1 = atoms.findAtom(name1); if ( a1 == 0 ) { if ( oncoutpe ) { cout << " ConstraintSet: could not find atom " << name1 << endl; cout << " ConstraintSet: could not define constraint" << endl; } return false; } // check if constraint is already defined bool found = false; Constraint *pc = 0; for ( int i = 0; i < constraint_list.size(); i++ ) { pc = constraint_list[i]; assert(pc != 0); // check if a constraint with same name or with same atom is defined if ( pc->type() == "position" ) found = ( pc->name() == name ) || ( pc->names(0) == name1 ); } if ( found ) { if ( oncoutpe ) cout << " ConstraintSet: constraint is already defined:\n" << " cannot define constraint" << endl; return false; } else { PositionConstraint *c = new PositionConstraint(name,name1,position_tolerance); constraint_list.push_back(c); } } else if ( type == distance_type ) { // define distance name A B value // define distance name A B value velocity if ( argc < 7 || argc > 8 ) { if ( oncoutpe ) cout << " Incorrect number of arguments for distance constraint" << endl; return false; } double distance, velocity=0.0; string name = argv[3]; string name1 = argv[4]; string name2 = argv[5]; Atom *a1 = atoms.findAtom(name1); Atom *a2 = atoms.findAtom(name2); if ( a1 == 0 || a2 == 0 ) { if ( oncoutpe ) { if ( a1 == 0 ) cout << " ConstraintSet: could not find atom " << name1 << endl; if ( a2 == 0 ) cout << " ConstraintSet: could not find atom " << name2 << endl; cout << " ConstraintSet: could not define constraint" << endl; } return false; } if ( name1 == name2 ) { if ( oncoutpe ) cout << " ConstraintSet: cannot define distance constraint between " << name1 << " and " << name2 << endl; return false; } distance = atof(argv[6]); if ( argc == 8 ) { velocity = atof(argv[7]); } if ( distance <= 0.0 ) { if ( oncoutpe ) cout << " ConstraintSet: distance must be positive" << endl << " ConstraintSet: could not define constraint" << endl; return false; } // check if constraint is already defined bool found = false; Constraint *pc = 0; for ( int i = 0; i < constraint_list.size(); i++ ) { pc = constraint_list[i]; assert(pc != 0); // check if a constraint (name1,name2) or (name2,name1) is defined if ( pc->type() == "distance" ) found = ( pc->names(0) == name1 && pc->names(1) == name2 ) || ( pc->names(1) == name1 && pc->names(0) == name2 ) || ( pc->name() == name ); } if ( found ) { if ( oncoutpe ) cout << " ConstraintSet: constraint is already defined:\n" << " cannot define constraint" << endl; return false; } else { DistanceConstraint *c = new DistanceConstraint(name,name1,name2,distance, velocity,distance_tolerance); constraint_list.push_back(c); } } else if ( type == angle_type ) { // constraint define angle name A B C value // constraint define angle name A B C value velocity if ( argc < 8 || argc > 9 ) { if ( oncoutpe ) cout << " Incorrect number of arguments for angle constraint" << endl; return false; } string name = argv[3]; string name1 = argv[4]; string name2 = argv[5]; string name3 = argv[6]; Atom *a1 = atoms.findAtom(name1); Atom *a2 = atoms.findAtom(name2); Atom *a3 = atoms.findAtom(name3); if ( a1 == 0 || a2 == 0 || a3 == 0 ) { if ( oncoutpe ) { if ( a1 == 0 ) cout << " ConstraintSet: could not find atom " << name1 << endl; if ( a2 == 0 ) cout << " ConstraintSet: could not find atom " << name2 << endl; if ( a3 == 0 ) cout << " ConstraintSet: could not find atom " << name3 << endl; cout << " ConstraintSet: could not define constraint" << endl; } return false; } if ( name1 == name2 || name1 == name3 || name2 == name3) { if ( oncoutpe ) cout << " ConstraintSet: cannot define angle constraint between " << name1 << " " << name2 << " and " << name3 << endl; return false; } const double angle = atof(argv[7]); double velocity = 0.0; if ( argc == 9 ) { velocity = atof(argv[8]); } if ( angle < 0.0 || angle > 180.0 ) { if ( oncoutpe ) cout << " ConstraintSet: angle must be in [0,180]" << endl << " ConstraintSet: could not define constraint" << endl; return false; } // check if equivalent constraint is already defined bool found = false; Constraint *pc = 0; for ( int i = 0; i < constraint_list.size(); i++ ) { pc = constraint_list[i]; assert(pc != 0); // check if a constraint (name1,name2,name3) or // (name3,name2,name1) is defined if ( pc->type() == "angle" ) found = ( pc->names(0) == name1 && pc->names(1) == name2 && pc->names(2) == name3 ) || ( pc->names(0) == name3 && pc->names(1) == name2 && pc->names(2) == name1 ) || ( pc->name() == name ); } if ( found ) { if ( oncoutpe ) cout << " ConstraintSet:set_constraint: an angle constraint " << name1 << " " << name2 << " " << name3 << " was found" << endl << " ConstraintSet: cannot define constraint" << endl; return false; } else { AngleConstraint *c = new AngleConstraint(name, name1,name2,name3,angle, velocity,angle_tolerance); constraint_list.push_back(c); } } else if ( type == torsion_type ) { // constraint define torsion name A B C D angle // constraint define torsion name A B C D angle velocity if ( argc < 9 || argc > 10 ) { if ( oncoutpe ) cout << " Incorrect number of arguments for torsion constraint" << endl; return false; } string name = argv[3]; string name1 = argv[4]; string name2 = argv[5]; string name3 = argv[6]; string name4 = argv[7]; Atom *a1 = atoms.findAtom(name1); Atom *a2 = atoms.findAtom(name2); Atom *a3 = atoms.findAtom(name3); Atom *a4 = atoms.findAtom(name4); if ( a1 == 0 || a2 == 0 || a3 == 0 || a4 == 0 ) { if ( oncoutpe ) { if ( a1 == 0 ) cout << " ConstraintSet: could not find atom " << name1 << endl; if ( a2 == 0 ) cout << " ConstraintSet: could not find atom " << name2 << endl; if ( a3 == 0 ) cout << " ConstraintSet: could not find atom " << name3 << endl; if ( a4 == 0 ) cout << " ConstraintSet: could not find atom " << name4 << endl; cout << " ConstraintSet: could not define constraint" << endl; } return false; } if ( name1 == name2 || name1 == name3 || name1 == name4 || name2 == name3 || name2 == name4 || name3 == name4 ) { if ( oncoutpe ) cout << " ConstraintSet: cannot define torsion constraint using " << name1 << " " << name2 << " " << name3 << " " << name4 << endl; return false; } double angle = atof(argv[8]); if ( angle > 180.0 ) while ( angle > 180.0 ) angle -= 360.0; else if ( angle < -180.0 ) while ( angle < -180.0 ) angle += 360.0; double velocity = 0.0; if ( argc == 10 ) { velocity = atof(argv[9]); } // check if equivalent constraint is already defined bool found = false; Constraint *pc = 0; for ( int i = 0; i < constraint_list.size(); i++ ) { pc = constraint_list[i]; assert(pc != 0); // check if an equivalent constraint (name1,name2,name3,name4) or // (name4,name3,name2,name1) is defined if ( pc->type() == "angle" ) found = ( pc->names(0) == name1 && pc->names(1) == name2 && pc->names(2) == name3 && pc->names(3) == name4 ) || ( pc->names(0) == name4 && pc->names(1) == name3 && pc->names(2) == name2 && pc->names(3) == name1 ) || ( pc->name() == name ); } if ( found ) { if ( oncoutpe ) cout << " ConstraintSet: a torsion constraint " << name1 << " " << name2 << " " << name3 << " " << name4 << " is already defined" << endl << " ConstraintSet: cannot define constraint" << endl; return false; } else { TorsionConstraint *c = new TorsionConstraint(name,name1,name2,name3,name4, angle,velocity,angle_tolerance); constraint_list.push_back(c); } } else { if ( oncoutpe ) cout << " ConstraintSet::set_constraint: internal error" << endl; return false; } // update total number of blocked degrees of freedom ndofs_ += constraint_list.back()->dofs(); return true; }