Exemple #1
0
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();
}
Exemple #2
0
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;
}
Exemple #3
0
bool suiScHelper::checkInclusion(const ScUri &uri, const ScUri &setUri) const
{
    ScSafeIterator iter(mMemoryInterface,
                        ScTemplate() << setUri << ScElementType(ScArcMask) << uri);

    return !iter.is_over();
}
Exemple #4
0
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();
    }
}
Exemple #5
0
ScUri suiScHelper::searchFormat(const ScUri &uri) const
{
    // get format keynode
    ScUri group_format = keynode("/ui/format");

    // search all input arcs into uri
    ScSafeIterator iter(mMemoryInterface, ScTemplate() << ScElementType(ScNode | ScConst)
                                                       << ScElementType(ScArcMain)
                                                       << uri);
    while (!iter.is_over())
    {
        ScUriVector res;
        if (searchOneShot(ScTemplate() << group_format << ScElementType(ScArcMain) << iter.value(0), res))
            return res[2];
        iter.next();
    }

    return ScUri();
}
Exemple #6
0
// FIXME
ScUri suiScHelper::findElementByMainIdentifier(const QString &idtf) const
{
    ScUriList contents;

    // get all contents that same to our identifier
    mMemoryInterface->find_str_content(idtf, contents);

    if (contents.size() == 0)   return ScUri::empty();

    ScUri keynode_main_ = keynode("/etc/main_");
    ScUri keynode_idtf = keynode("/etc/identification*");

    ScUriList::iterator it;
    for (it = contents.begin(); it != contents.end(); ++it)
    {
        // find set where current sc-link is a main identifier
        ScUriVector res;
        if (searchOneShot(ScTemplate(ScElementType(ScNode | ScConst),
                                     ScElementType(ScArcMain),
                                     *it,
                                     ScElementType(ScArcMain),
                                     keynode_main_), res))
        {
            // get object by founded identifiers set
            if (searchOneShot(ScTemplate(res[0],
                                         ScElementType(ScArcCommon | ScConst),
                                         ScElementType(ScNode | ScConst),
                                         ScElementType(ScArcMain),
                                         keynode_idtf), res))
            {
                return res[2];
            }else
                SuiExcept(SuiExceptionInternalError,
                          QString("Invalid knowledge base"),
                          "ScUri suiScHelper::findElementByMainIdentifier(const QString &idtf) const");
        }

    }

    return ScUri::empty();
}
Exemple #7
0
ScUri suiScHelper::appendFormatKeynode(const QString &formatName)
{
    KeynodesMap::iterator it = mKeynodesMap.find(QString("/ui/format/%1").arg(formatName));
    if (it != mKeynodesMap.end()) return it.value();

    ScUri res = mMemoryInterface->gen_el(ScElementType(ScNode | ScConst));
    // include it into formats set
    ScUriVector v;
    if (!genElStr(ScTemplate() << keynode("/ui/format") << ScElementType(ScArcMain) << res, v))
        SuiExcept(SuiExceptionInternalError,
                  QString("Can't include format '%1' into formats set").arg(formatName),
                  "ScUri suiScHelper::appendFormatKeynode(const QString &formatName)");
    appendKeynode(res, QString("/ui/format/%1").arg(formatName));

    return res;
}
Exemple #8
0
void suiKnowBase::resolveMainIdentificationKeynodes()
{
    // get sc-links with identification* and main_ strings
    ScMemoryInterface *memory = suiRoot::getInstance()->scMemory();
    ScHelperInterface *helper = suiRoot::getInstance()->scHelper();

    QString idtf_alias = "identification*";
    QString main_alias = "main_";

    ScUriList idtf_candidates;
    if (!memory->find_str_content(idtf_alias, idtf_candidates))
        SuiExcept(SuiExceptionItemNotFound,
                  QString("Can't find sc-link with content: %1").arg(idtf_alias),
                  "void suiKnowBase::resolveMainIdentificationKeynodes()");

    ScUriList main_candidates;
    if (!memory->find_str_content(main_alias, main_candidates))
        SuiExcept(SuiExceptionFileNotFound,
                  QString("Can't find sc-link with content: %1").arg(main_alias),
                  "void suiKnowBase::resolveMainIdentificationKeynodes()");

    // all founded objects can be a candidates, so try them all
    for (ScUriList::iterator idtf_it = idtf_candidates.begin(); idtf_it != idtf_candidates.end(); ++idtf_it)
        for (ScUriList::iterator main_it = main_candidates.begin(); main_it != main_candidates.end(); ++main_it)
        {
            // iterate all possible identification sets for node identification*
            ScSafeIterator it_idtf_set(memory,
                                       ScTemplate() << ScElementType(ScNode | ScConst | ScTuple)
                                       << ScElementType(ScArcMain)
                                       << *idtf_it
                                       << ScElementType(ScArcMain)
                                       << ScElementType(ScNode | ScConst | ScRole));
            while (!it_idtf_set.is_over())
            {
                // now check if founded attribute (value with index 4) also
                // exist in any construction when we trying to find
                // identification set for main_ node
                ScSafeIterator it_main_set(memory,
                                           ScTemplate() << ScElementType(ScNode | ScConst)
                                           << ScElementType(ScArcMain)
                                           << *main_it
                                           << ScElementType(ScArcMain)
                                           << it_idtf_set.value(4));
                while (!it_main_set.is_over())
                {
                    // trying to find idetification* node
                    ScSafeIterator it1(memory,
                                       ScTemplate() << it_idtf_set.value(0)
                                       << ScElementType(ScArcCommon | ScConst)
                                       << ScElementType(ScNode | ScConst));

                    memory->_printEl(it_idtf_set.value(0));
                    while (!it1.is_over())
                    {
                        // check if it have output arc into common arc from identifiers set
                        // to identification* node
                        if (helper->checkExist(ScTemplate() << it1.value(2)
                                               << ScElementType(ScArcMain)
                                               << it1.value(1)))
                        {
                            // now we just have founded candidate to identification* (it1.value(2)) node
                            // to ensure we just need to check if it links to common arc
                            // from identification set for main_ node
                            if (helper->checkExist(ScTemplate() << it_main_set.value(0)
                                                   << ScElementType(ScArcCommon | ScConst)
                                                   << it_idtf_set.value(4)
                                                   << ScElementType(ScArcMain)
                                                   << it1.value(2)))
                            {
                                helper->appendKeynode(it_idtf_set.value(4), "/etc/main_");
                                helper->appendKeynode(it1.value(2), "/etc/identification*");
                                return;
                            }
                        }

                        it1.next();
                    }

                    it_main_set.next();
                }


                it_idtf_set.next();
            }
        }

}
Exemple #9
0
void suiKnowBase::loadSourceFile(const QString& filename)
{
    QFile file(filename);
    QFileInfo finfo(filename);

    ScMemoryInterface *sc_memory = suiRoot::getInstance()->scMemory();
    ScHelperInterface *sc_helper = suiRoot::getInstance()->scHelper();

    //get needed keynodes
    ScUri content_keynode = sc_helper->keynode("/etc/content*");
    ScUri path_keynode = sc_helper->keynode("/etc/path*");
    ScUri source_keynode = sc_helper->keynode("/etc/source");


    QString formatAlias = "/ui/format/" + finfo.suffix().toLower();

    ScUri format_keynode;
    if (!sc_helper->hasKeynode(formatAlias))
    {
        qDebug() << "format " << finfo.suffix() << " doesn't supported";
        return;
    }else
        format_keynode = sc_helper->keynode(formatAlias);

    ScUri source_node = sc_memory->gen_el(ScElementType(ScNode | ScConst));
    // Create link and write content to it.
    ScUri source_content = sc_memory->gen_el(ScElementType(ScLink | ScConst));
    sc_memory->set_content(source_content, file);

    // include sc-link into format set
    ScUriVector r;
    if (!sc_helper->genElStr(ScTemplate() << format_keynode << ScElementType(ScArcMain) << source_content, r))
        SuiExcept(SuiExceptionInternalError,
                  QString("Can't include sc-link into '%1' set").arg(formatAlias),
                  "void suiKnowBase::loadSourceFile(const QString& filename)");

    // Create node for storing file path.
    ScUri source_path_node = sc_memory->gen_el(ScElementType(ScLink | ScConst));
    QByteArray ba = filename.toUtf8();
    QBuffer buffer(&ba);
    sc_memory->set_content(source_path_node, buffer);


    ScUriVector v;
    sc_helper->genElStr(ScTemplate() << source_keynode
                        << ScElementType(ScArcMain)
                        << source_node, v);

    sc_helper->genElStr(ScTemplate() << source_node
                        << ScElementType(ScArcCommon | ScConst | ScPositive)
                        << source_content
                        << ScElementType(ScArcMain)
                        << content_keynode, v);

    sc_helper->genElStr(ScTemplate() << source_node
                        << ScElementType(ScArcCommon | ScConst | ScPositive)
                        << source_path_node
                        << ScElementType(ScArcMain)
                        << path_keynode, v);

}
Exemple #10
0
suiRoot::suiRoot(QMainWindow *mainWindow, QObject *parent) :
    QObject(parent),
    mMainWindow(mainWindow),
    mComponentsManager(0),
    mPluginManager(0),
    mMemoryInterface(0),
    mScHelper(0),
    mKnowBase(0),
    mKpm(0),
    mMainScWindow(0)
{
    Q_ASSERT(mInstance == 0);
    mInstance = this;

    mComponentsManager = new suiComponentsManager(this);
    mPluginManager = new suiPluginManager(this);

    // load plugins
    QDir pluginsDir(qApp->applicationDirPath());
#if defined(Q_OS_WIN)
    pluginsDir.cdUp();
    pluginsDir.cd("plugins");
#elif defined(Q_OS_LINUX)
    pluginsDir.cd("plugins");
#endif    
    // load sc plugins
    mPluginManager->loadPluginsFromDir(pluginsDir.absoluteFilePath("sc"), false);
    scMemory()->initialize("../kb");

    // create knowledge base control class
    mKnowBase = new suiKnowBase(this);
    mKnowBase->initialize();
    // load ui plugins
    mPluginManager->loadPluginsFromDir(pluginsDir.absoluteFilePath("ui"), true);
    // load sources
    //mKnowBase->loadSources(QStringList());// << "../repo/sources");

    // create main window
    UiComponentFactoryInterfaceList res;
    Q_ASSERT(mComponentsManager->factoriesList(UiComponentMainWindow, res));
    mMainScWindow = qobject_cast<UiMainWindowInterface*>(res.front()->createInstance());
    mMainScWindow->setUri(mScHelper->keynode("/ui/main_window"));

    mMainWindow->setCentralWidget(mMainScWindow->widget());

    // initialize knowledge processing machine
    mKpm = new suiKpm(this);
    mKpm->initialize();


    // >>>>> test
    // show main menu on main window
    ScUri main_menu = scHelper()->keynode("/ui/main_menu");
    ScUri decomp = scHelper()->keynode("/etc/decomposition*");

    // get set of child items for main menu
    ScUriVector v;
    if (scHelper()->searchOneShot(ScTemplate() << ScElementType(ScNode | ScConst)
                              << ScElementType(ScArcCommon | ScConst)
                              << main_menu
                              << ScElementType(ScArcMain)
                              << decomp, v))
    {
        ScUriList objects;
        ScTemplate templ;
        templ << v[0] << ScElementType(ScArcMain) << ScElementType(ScNode | ScConst);
        ScSafeIterator it(scMemory(), templ);
        while (!it.is_over())
        {
            objects << it.value(2);
            it.next();
        }

        mMainScWindow->showObjects(objects);
    }

    // <<<<<<< test
}
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;
}