plugin_utils.cpp
来自「ncbi源码」· C++ 代码 · 共 1,523 行 · 第 1/4 页
CPP
1,523 行
dynamic_cast<const CSerialObject*>(obj); if (o) { return (val.GetObjectSubtype() == o->GetThisTypeInfo()->GetName()); } else { return false; } } }} break; } return true;}//// check to see that a plugin argument's values match a plugin// argument's constraints//bool CPluginUtils::CheckConstraints(const CPluginArg& arg, CConvertCache* cache){ // we always succeed if the constraint is not set if ( !arg.IsSetConstraint() || arg.GetConstraint().size() == 0) { return true; } switch (arg.GetType()) { case CPluginArg::eInteger: if (arg.GetConstraint().size() > 1) { // we support only a single constraint for built-in types LOG_POST(Error << "CPluginArg::CheckConstraint(): " "built-in types support only one concurrent constraint"); return false; } else { const CPluginValueConstraint& cons = *arg.GetConstraint().front(); // // integral types may be constrained by lower, upper, range, // or set constraints // switch (cons.Which()) { case CPluginValueConstraint::e_Lower: // greater than or equal to the lower range return (arg.AsInteger() >= NStr::StringToInt(cons.GetLower())); case CPluginValueConstraint::e_Upper: // less than or equal to the lower range return (arg.AsInteger() <= NStr::StringToInt(cons.GetUpper())); case CPluginValueConstraint::e_Range: // bounded by the range of values specified {{ int val = arg.AsInteger(); int lower = NStr::StringToInt(cons.GetRange().GetLower()); if (val < lower) { return false; } int upper = NStr::StringToInt(cons.GetRange().GetUpper()); return (val <= upper); }} case CPluginValueConstraint::e_Set: // must match an item in the set // FIXME: we use a linear scan here {{ int val = arg.AsInteger(); string val_str = NStr::IntToString(val); CPluginValueConstraint::TSet::const_iterator iter = std::find(cons.GetSet().begin(), cons.GetSet().end(), val_str); return (iter != cons.GetSet().end()); }} default: LOG_POST(Error << "CPluginArg::CheckConstraint(): " "invalid constraint type for " << arg.GetName() << ": " << (int)cons.Which()); return false; } } break; case CPluginArg::eDouble: if (arg.GetConstraint().size() > 1) { // we support only a single constraint for built-in types LOG_POST(Error << "CPluginArg::CheckConstraint(): " "built-in types support only one concurrent constraint"); return false; } else { const CPluginValueConstraint& cons = *arg.GetConstraint().front(); // // real types may be constrained by lower, upper, range, or set // constraints // switch (cons.Which()) { case CPluginValueConstraint::e_Lower: // greater than or equal to the lower range return (arg.AsDouble() >= NStr::StringToDouble(cons.GetLower())); case CPluginValueConstraint::e_Upper: // less than or equal to the lower range return (arg.AsDouble() <= NStr::StringToDouble(cons.GetUpper())); case CPluginValueConstraint::e_Range: // bounded by the range of values specified {{ double val = arg.AsDouble(); double lower = NStr::StringToDouble(cons.GetRange().GetLower()); if (val < lower) { return false; } double upper = NStr::StringToDouble(cons.GetRange().GetUpper()); return (val <= upper); }} case CPluginValueConstraint::e_Set: // must match an item in the set // FIXME: we use a linear scan here {{ double val = arg.AsDouble(); string val_str = NStr::DoubleToString(val); CPluginValueConstraint::TSet::const_iterator iter = std::find(cons.GetSet().begin(), cons.GetSet().end(), val_str); return (iter != cons.GetSet().end()); }} default: LOG_POST(Error << "CPluginArg::CheckConstraint(): " "invalid constraint type for " << arg.GetName() << ": " << (int)cons.Which()); return false; } } break; case CPluginArg::eString: if (arg.GetConstraint().size() > 1) { // we support only a single constraint for built-in types LOG_POST(Error << "CPluginArg::CheckConstraint(): " "built-in types support only one concurrent constraint"); return false; } else { const CPluginValueConstraint& cons = *arg.GetConstraint().front(); // // integral types may be constrained by lower, upper, range, or set // constraints // switch (cons.Which()) { case CPluginValueConstraint::e_Lower: // greater than or equal to the lower range return (arg.AsString() >= cons.GetLower()); case CPluginValueConstraint::e_Upper: // less than or equal to the lower range return (arg.AsString() <= cons.GetUpper()); case CPluginValueConstraint::e_Range: // bounded by the range of values specified {{ string val = arg.AsString(); string lower = cons.GetRange().GetLower(); if (val < lower) { return false; } string upper = cons.GetRange().GetUpper(); return (val <= upper); }} case CPluginValueConstraint::e_Set: // must match an item in the set // FIXME: we use a linear scan here {{ string val = arg.AsString(); CPluginValueConstraint::TSet::const_iterator iter = std::find(cons.GetSet().begin(), cons.GetSet().end(), val); return (iter != cons.GetSet().end()); }} default: LOG_POST(Error << "CPluginArg::CheckConstraint(): " "invalid constraint type for " << arg.GetName() << ": " << (int)cons.Which()); return false; } } break; case CPluginArg::eBoolean: // no constraints make sense for boolean arguments LOG_POST(Error << "CPluginArg::CheckConstraint(): " "constraints are invalid for Boolean arguments"); return false; case CPluginArg::eObject: case CPluginArg::eDocument: {{ CTypeConstIterator<CPluginValue> iter(arg); for ( ; iter; ++iter) { const CObject* obj = iter->GetObject(); const IDocument* doc = iter->GetDocument(); if ( !obj || !doc ) { return false; } CScope& scope = doc->GetScope(); if ( !CheckConstraints(arg, scope, *obj, cache) ) { return false; } } return true; }} default: LOG_POST(Error << "CPluginUtils::CheckConstraint(): " "unhandled argument type"); break; } return false;}bool CPluginUtils::CheckConstraints(const CPluginArg& arg, CScope& scope, const CObject& obj, CConvertCache* cache){ // // we may have multiple constraints, to answer questions such as "Give // me all sequences that are proteins of at least 100 bases" and "Give // me a set of sequences, all of the same molecule type, that are of // sequence type raw" // ITERATE (CPluginArg::TConstraint, c_iter, arg.GetConstraint()) { const CPluginValueConstraint& cons = **c_iter; switch (cons.Which()) { // // constraints for bioseq types // case CPluginValueConstraint::e_Seq_repr: // constrain the sequence representation to be of a certain type {{ CObjConverter::TObjList bioseqs; CObjectConverter::Convert(scope, obj, CBioseq::GetTypeInfo(), bioseqs); ITERATE(CObjConverter::TObjList, iter, bioseqs) { const CBioseq* bioseq = dynamic_cast<const CBioseq*>(iter->GetPointer()); if ( !bioseq ) { return false; } if (std::find(cons.GetSeq_repr().begin(), cons.GetSeq_repr().end(), bioseq->GetInst().GetRepr()) == cons.GetSeq_repr().end() ) { return false; } } }} break; case CPluginValueConstraint::e_Seq_mol: // constrain the molecule type to be of a certain type {{ CObjConverter::TObjList bioseqs; CObjectConverter::Convert(scope, obj, CBioseq::GetTypeInfo(), bioseqs); ITERATE(CObjConverter::TObjList, iter, bioseqs) { const CBioseq* bioseq = dynamic_cast<const CBioseq*>(iter->GetPointer()); if ( !bioseq ) { return false; } if (std::find(cons.GetSeq_mol().begin(), cons.GetSeq_mol().end(), bioseq->GetInst().GetMol()) == cons.GetSeq_mol().end() ) { return false; } } }} break; case CPluginValueConstraint::e_Seq_mol_same_type: // constrain a set of values such that their molecule types are all // the same {{ const CBioseq* prev = NULL; CObjConverter::TObjList bioseqs; CObjectConverter::Convert(scope, obj, CBioseq::GetTypeInfo(), bioseqs); ITERATE(CObjConverter::TObjList, iter, bioseqs) { const CBioseq* bioseq = dynamic_cast<const CBioseq*>(iter->GetPointer()); if ( !bioseq ) { return false; } if ( !prev ) { prev = bioseq; } else { if (prev->GetInst().GetMol() != bioseq->GetInst().GetMol()) { return false; } } } }} break; case CPluginValueConstraint::e_Seq_length_lower: // constrain a set of sequences such that their lengths are at // least some bound {{ TSeqPos min_len = cons.GetSeq_length_lower(); CObjConverter::TObjList bioseqs; CObjectConverter::Convert(scope, obj, CBioseq::GetTypeInfo(), bioseqs); ITERATE(CObjConverter::TObjList, iter, bioseqs) { const CBioseq* bioseq = dynamic_cast<const CBioseq*>(iter->GetPointer()); if ( !bioseq ) { return false; } if (bioseq->GetInst().GetLength() < min_len) { return false; } } }} break; case CPluginValueConstraint::e_Seq_length_upper: // constrain a set of sequences such that their lengths are no // greater than some bound {{ TSeqPos max_len = cons.GetSeq_length_upper(); CObjConverter::TObjList bioseqs; CObjectConverter::Convert(scope, obj, CBioseq::GetTypeInfo(), bioseqs); ITERATE(CObjConverter::TObjList, iter, bioseqs) { const CBioseq* bioseq = dynamic_cast<const CBioseq*>(iter->GetPointer()); if ( !bioseq ) { return false; } if (bioseq->GetInst().GetLength() > max_len) { return false; } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?