bool suiScHelper::appendIdtf(const ScUri &elem, const ScUri &idtf, bool is_main) { Q_ASSERT(!elem.isEmpty() && !idtf.isEmpty()); // check if main idtf already exist ScUri nrel_idtf = keynode(key_nrel_identification); ScUri main_ = keynode(key_rrel_main); Q_ASSERT(!nrel_idtf.isEmpty() && !main_.isEmpty()); // get identifiers set ScUriVector set; ScTemplate idtf_set_templ = ScTemplate(scParamType(ScNode | ScConst), scParamType(ScArcCommon | ScConst | ScPositive), scParamUri(elem), scParamType(ScArcMain), scParamUri(nrel_idtf)); if (ensureExist(idtf_set_templ, set)) { if (is_main) { // trying to find and remove old main identifier ScUriVector main_res; ScTemplate main_templ = ScTemplate(scParamUri(set[0]), scParamType(ScArcMain), scParamType(ScLink), scParamType(ScArcMain), scParamUri(main_)); if (searchOneShot(main_templ, main_res)) { mMemoryInterface->erase_el(main_res[2]); } // setup new main identifier main_templ.set(2, scParamUri(idtf)); if (!genElStr(main_templ, main_res)) return false; } ScUriVector idtf_res; // is not main identifier if (!genElStr(ScTemplate(scParamUri(set[0]), scParamType(ScArcMain), scParamUri(idtf)), idtf_res)) return false; } else return false; return false; }
void suiKnowBase::initialize() { registerAllKeynodes(); // createMinimalBase(); // first of all we need to find identification* and main_ nodes resolveMainIdentificationKeynodes(); // resolve other keynodes ScHelperInterface *helper = suiRoot::getInstance()->scHelper(); for (KeynodesMap::iterator it = mKeynodesMap.begin(); it != mKeynodesMap.end(); ++it) { ScUri uri = helper->findElementByMainIdentifier(it.key()); if (!uri.isEmpty()) helper->appendKeynode(uri, it.value()); } }
QString suiScHelper::stringIdtf(const ScUri &uri, const ScUri &langUri) const { // get all identifiers ScUriVector idtfs; allIdtfs(uri, idtfs); // iterate all identifiers and trying to find with specified language for (ScUriVector::iterator it = idtfs.begin(); it != idtfs.end(); ++it) { ScUri idtfUri = *it; // check language if (checkInclusion(idtfUri, langUri)) { QString res; QIODevice *device = mMemoryInterface->open_content(idtfUri); if (device == 0) SuiExcept(SuiExceptionInvalidParameters, QString("Can't open content for sc-element with uri = %1").arg(idtfUri.value()), "QString suiScHelper::stringIdtf(const ScUri &uri, const ScUri &langUri) const"); res = mMemoryInterface->decode_str_content(*device); mMemoryInterface->close_content(device); return res; } } return QString(); }
bool suiScHelper::allIdtfs(const ScUri &elem, ScUriVector &result) const { if (elem.isEmpty()) SuiExcept(SuiExceptionInvalidParameters, "ScUir is empty", "bool suiScHelper::allIdtfs(const ScUri &elem, ScUriVector &result) const"); result.clear(); // get keynodes we need to work ScUri nrel_idtf = keynode(key_nrel_identification); // search idtfs set ScUriVector res_tmp; if (searchOneShot(ScTemplate() << ScElementType(ScNode | ScConst) << ScElementType(ScArcCommon | ScConst | ScPositive) << elem << ScElementType(ScArcMain) << nrel_idtf , res_tmp)) { // search all output arcs ScTemplate templ = ScTemplate() << res_tmp[0] << ScElementType(ScArcMain) << ScElementType(ScNode); ScIteratorInterface *iter = mMemoryInterface->create_iterator(templ); while (!iter->is_over()) { result.push_back(iter->value(2)); iter->next(); } } return !result.empty(); }
void suiKnowBase::loadSources(const QStringList &sourceDirs) { // load knowledge base from sources QString sDir; foreach(sDir, sourceDirs) loadSourceDirectory(sDir, true); // find all keynodes ScHelperInterface *helper = suiRoot::getInstance()->scHelper(); KeynodesMap::iterator it; for (it = mKeynodesMap.begin(); it != mKeynodesMap.end(); ++it) { // skip keynodes, that already found if (helper->hasKeynode(it.value())) continue; // try to find keynodes by main identifier ScUri uri = helper->findElementByMainIdentifier(it.key()); if (uri.isEmpty()) SuiExcept(SuiExceptionItemNotFound, QString("Can't find keynode '%1'").arg(it.key()), "void suiKnowBase::initialize(const QStringList &sourceDirs)"); } ScUri content_keynode = helper->keynode("/etc/content*"); // trying to search all loaded sources ScMemoryInterface *mem = suiRoot::getInstance()->scMemory(); ScSafeIterator scIt(mem, ScTemplate() << helper->keynode("/etc/source") << ScElementType(ScArcMain) << ScElementType(ScNode | ScConst)); while (!scIt.is_over()) { ScUriVector res; // trying to find content of source if (helper->searchOneShot(ScTemplate() << scIt.value(2) << ScElementType(ScArcCommon | ScConst | ScPositive) << ScElementType(0) // fix to link << ScElementType(ScArcMain) << content_keynode, res)) { suiRoot::getInstance()->translateDataToSc(res[2]); }else SuiExcept(SuiExceptionInternalError, "Can't find knowledge base source content", "void suiKnowBase::initialize(const QStringList &sourceDirs)"); scIt.next(); } }
ScUri SCgTranslateGWFToSc::resolveObject(SCgObjectInfo *objInfo) { // trying to get in local id's idToScUri::iterator it = mIdToScUri.find(objInfo->id()); if (it != mIdToScUri.end()) return it.value(); // if object has identifier, than try to find it ScUri res = SCgPlugin::rootInterface()->scHelper()->findElementByMainIdentifier(objInfo->idtfValue()); if (!res.isEmpty()) { mIdToScUri[objInfo->id()] = res; if (!mTranslatedObjects.contains(res)) mTranslatedObjects.append(res); return res; } // if object wasn't found, then create it if (objInfo->objectType() == SCgVisualObject::SCgBusType) { SCgBusInfo *bus_info = static_cast<SCgBusInfo*>(objInfo); res = mIdToScUri[bus_info->ownerId()]; mIdToScUri[objInfo->id()] = res; }else { ScElementType type = ScElementType(ScNode | ScConst); // just node and arc(pair) tags have type attribute if (objInfo->objectType() != SCgVisualObject::SCgContourType) { TypeAliasToScType::iterator itType = mTypeAliasToScType.find(objInfo->typeAlias()); if (itType == mTypeAliasToScType.end()) SuiExcept(SuiExceptionInternalError, QString("Unsupported type alias '%1'").arg(objInfo->typeAlias()), "ScUri SCgTranslateGWFToSc::resolveObject(SCgObjectInfo *objInfo)"); type = itType.value(); } res = mScMemory->gen_el(type); mIdToScUri[objInfo->id()] = res; mTranslatedObjects.append(res); } return res; }
bool SCgTranslateGWFToSc::translate(QIODevice &device, ScMemoryInterface *memory, const ScUri &set) { mScMemory = memory; Q_ASSERT(mScMemory != 0); QScopedPointer<ScHelperInterface> helper(SCgPlugin::rootInterface()->scHelper(mScMemory)); // read data from gwf GwfObjectInfoReader reader; if (!reader.read(&device)) { qDebug() << "Error while translate to sc-code: " << reader.lastError(); return false; } // iterate all objects and resolve them GwfObjectInfoReader::TypeToObjectsMap::const_iterator it; for (it = reader.objectsInfo().begin(); it != reader.objectsInfo().end(); ++it) { GwfObjectInfoReader::ObjectInfoList::const_iterator it_info; for (it_info = (*it).begin(); it_info != (*it).end(); ++it_info) resolveObject(*it_info); } // setup pair begin and end objects const GwfObjectInfoReader::ObjectInfoList &list = reader.objectsInfo()[SCgVisualObject::SCgPairType]; GwfObjectInfoReader::ObjectInfoList::const_iterator it_info; for (it_info = list.begin(); it_info != list.end(); ++it_info) { SCgPairInfo *pairInfo = static_cast<SCgPairInfo*>(*it_info); ScUri pair_uri = mIdToScUri[pairInfo->id()]; // get begin and end uri's ScUri b = mIdToScUri[pairInfo->beginObjectId()]; ScUri e = mIdToScUri[pairInfo->endObjectId()]; // setup begin and end mScMemory->set_beg(pair_uri, b); mScMemory->set_end(pair_uri, e); } // append objects into parent sets for (it = reader.objectsInfo().begin(); it != reader.objectsInfo().end(); ++it) for (it_info = (*it).begin(); it_info != (*it).end(); ++it_info) { SCgObjectInfo *info = *it_info; // check if object has a parent if (info->parentId() != "0" && !info->parentId().isEmpty()) { ScUri uri = mIdToScUri[info->id()]; ScUri parent = mIdToScUri[info->parentId()]; // generate 5 elements construction to append new arc between parent and child into output set ScUriVector res; ScTemplate templ(parent, ScElementType(ScArcMain | ScConst | ScPos), uri, ScElementType(ScArcMain | ScConst | ScPos), set); if (!helper->genElStr(templ, res)) SuiExcept(SuiExceptionInternalError, QString("Can't append '%1' into '%1'").arg(uri.value()).arg(parent.value()), "bool SCgTranslateGWFToSc::translate(QIODevice &device, ScMemoryInterface *memory, const ScUri &set)"); } } // append all translated objects into output set ScUriList::iterator it_list; ScUriVector res; for (it_list = mTranslatedObjects.begin(); it_list != mTranslatedObjects.end(); ++it_list) if (!helper->genElStr(ScTemplate() << set << ScElementType(ScArcMain | ScConst| ScPos) << *it_list, res)) SuiExcept(SuiExceptionInternalError, QString("Can't append '%1 into output set '%2").arg((*it_list).value()).arg(set.value()), "bool SCgTranslateGWFToSc::translate(QIODevice &device, ScMemoryInterface *memory, const ScUri &set)"); return true; }