示例#1
0
void SCsTranslator::processSentenceLevel1(pANTLR3_BASE_TREE node)
{
    unsigned int nodesCount = node->getChildCount(node);
    assert(nodesCount == 3);

    pANTLR3_BASE_TREE node_obj = (pANTLR3_BASE_TREE)node->getChild(node, 0);
    pANTLR3_BASE_TREE node_pred = (pANTLR3_BASE_TREE)node->getChild(node, 1);
    pANTLR3_BASE_TREE node_subj = (pANTLR3_BASE_TREE)node->getChild(node, 2);

    pANTLR3_COMMON_TOKEN tok_pred = node_pred->getToken(node_pred);

    if (tok_pred->type != ID_SYSTEM)
    {
        THROW_EXCEPT(Exception::ERR_PARSE,
                     String("Invalid predicate '") + ((const char*) node_pred->getText(node_pred)->chars) + "' in simple sentence",
                     mParams.fileName,
                     tok_pred->getLine(tok_pred));
    }

    sElement *el_obj = parseElementTree(node_obj);
    sElement *el_subj = parseElementTree(node_subj);

    // determine arc type
    sc_type type = sc_type_edge_common;
    String pred = GET_NODE_TEXT(node_pred);
    size_t n = pred.find_first_of("#");
    if (n != pred.npos)
        type = _getArcPreffixType(pred.substr(0, n));

    _addEdge(el_obj, el_subj, type, false, pred);
}
示例#2
0
sElement* SCsTranslator::parseElementTree(pANTLR3_BASE_TREE tree, const String *assignIdtf)
{
    pANTLR3_COMMON_TOKEN tok = tree->getToken(tree);
    assert(tok);

    sElement *res = 0;
    if (tok->type == ID_SYSTEM)
        res = _addNode(GET_NODE_TEXT(tree));

    if (tok->type == SEP_LPAR)
    {
        assert(tree->getChildCount(tree) >= 3);
        pANTLR3_BASE_TREE node_obj = (pANTLR3_BASE_TREE)tree->getChild(tree, 0);
        pANTLR3_BASE_TREE node_pred = (pANTLR3_BASE_TREE)tree->getChild(tree, 1);
        pANTLR3_BASE_TREE node_subj = (pANTLR3_BASE_TREE)tree->getChild(tree, 2);

        String pred = GET_NODE_TEXT(node_pred);
        sElement *src = parseElementTree(node_obj);
        sElement *trg = parseElementTree(node_subj);

        assert(src && trg);

        res = _addEdge(src, trg, _getTypeByConnector(pred), _isConnectorReversed(pred), "");
    }

    if (tok->type == LINK)
    {
        String data = GET_NODE_TEXT(tree);
        CHECK_LINK_DATA(data);
		res = _addLinkFile(assignIdtf ? *assignIdtf : "", data.substr(1, data.size() - 2));
    }

    if (tok->type == CONTENT)
    {
        res = _addNode(assignIdtf ? *assignIdtf : "", sc_type_node_struct);

        String content = GET_NODE_TEXT(tree);
        content = content.substr(1, content.size() - 2);

        if (StringUtil::startsWith(content, "*", false) && StringUtil::endsWith(content, "*", false))
        {
            // parse contour data
            String data = content.substr(1, content.size() - 2);
            bool autoFormatInfo = mParams.autoFormatInfo;
            String fileName = mParams.fileName;

            // check if link to file
            if (StringUtil::startsWith(data, "^\"", false))
            {
                String name;
                bool result = false;
				if (_getAbsFilePath(trimContentData(data), name))
                {
                    fileName = name;
                    std::ifstream ifs(name.c_str());
                    if (ifs.is_open())
                    {
                        data = String((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
                        ifs.close();
                        result = true;
                    } else {
                        THROW_EXCEPT(Exception::ERR_PARSE,
                                     "Can't open file " << name,
                                     mParams.fileName,
                                     tok->getLine(tok));
                    }
                }
            }

            // parse data
            if (!data.empty())
            {
                SCsTranslator translator(mContext);
                translator.mParams.autoFormatInfo = autoFormatInfo;
                translator.mParams.fileName = fileName;
                translator.processString(data);

                // now we need to get all created elements and create arcs to them
                tElementSet::iterator it, itEnd = translator.mElementSet.end();
                for (it = translator.mElementSet.begin(); it != itEnd; ++it)
                {
                    if ((*it)->ignore) continue;

                    sElement *el = new sElement();
                    el->ignore = true;
                    el->addr = (*it)->addr;

                    mElementSet.insert(el);
                    _addEdge(res, el, sc_type_arc_pos_const_perm, false, "");
                }

                // merge identifiers map
                mSysIdtfAddrs.insert(translator.mSysIdtfAddrs.begin(), translator.mSysIdtfAddrs.end());
                mLocalIdtfAddrs.insert(translator.mLocalIdtfAddrs.begin(), translator.mLocalIdtfAddrs.end());
            }


        }
		else
        {
            if (StringUtil::startsWith(content, "^\"", false))
            {
				String data = trimContentData(content);
                CHECK_LINK_DATA(data);
				sBuffer buffer;
								
				if (parseContentBinaryData(data, buffer))
				{
					res = _addLink("", buffer);
				}
				else
				{
					res = _addLinkFile("", data);
				}
            }
            else
            {
                content = StringUtil::replaceAll(content, "\\[", "[");
                content = StringUtil::replaceAll(content, "\\]", "]");
                CHECK_LINK_DATA(content);
                res = _addLinkString("", content);
            }
        }
    }

    if (tok->type == SEP_LTUPLE || tok->type == SEP_LSET)
    {
        res = _addNode("", sc_type_node_tuple);
        processAttrsIdtfList(false, tree, res, "->", tok->type == SEP_LTUPLE);
    }

    // now process internal sentences
    uint32 n = tree->getChildCount(tree);
    for (uint32 i = 0; i < n; ++i)
    {
        pANTLR3_BASE_TREE internal = (pANTLR3_BASE_TREE)tree->getChild(tree, i);
        pANTLR3_COMMON_TOKEN tok = internal->getToken(internal);

        if (tok->type != SEP_LINT) continue;

        // process internal sentences
        uint32 nc = internal->getChildCount(internal);
        for (uint32 j = 0; j < nc; ++j)
        {
            pANTLR3_BASE_TREE node_pred = (pANTLR3_BASE_TREE)internal->getChild(internal, j);
            String connector = GET_NODE_TEXT(node_pred);
            processAttrsIdtfList(false, node_pred, res, connector, false);
        }
    }

    return res;
}
	MaxFlowMinCost& AddEdge(int from, int to, T cap, T cost) {
		_addEdge(from, to, cap, cost);
		_addEdge(to, from, 0, -cost);
		return *this;
	}