ScAddr findMainIdtf(ScMemoryContext & ctx, ScAddr const & elAddr, ScAddr const langAddr) { assert(elAddr.isValid()); assert(langAddr.isValid()); ScAddr result; ScIterator5Ptr it5 = ctx.iterator5( elAddr, SC_TYPE(sc_type_arc_common | sc_type_const), SC_TYPE(sc_type_link), SC_TYPE(sc_type_arc_pos_const_perm), Keynodes::nrel_main_idtf ); while (it5->next()) { if (ctx.helperCheckArc(langAddr, it5->value(2), sc_type_arc_pos_const_perm)) { result = it5->value(2); break; } } return result; }
sc_result ScAgentAction::run(ScAddr const & listenAddr, ScAddr const & startArcAddr) { ScAddr cmdAddr = mMemoryCtx.getEdgeTarget(startArcAddr); if (cmdAddr.isValid()) { if (mMemoryCtx.helperCheckArc(mCmdClassAddr, cmdAddr, sc_type_arc_pos_const_perm)) { mMemoryCtx.eraseElement(startArcAddr); ScAddr progressAddr = mMemoryCtx.createEdge(sc_type_arc_pos_const_perm, msCommandProgressdAddr, cmdAddr); assert(progressAddr.isValid()); ScAddr resultAddr = mMemoryCtx.createNode(sc_type_const | sc_type_node_struct); assert(resultAddr.isValid()); runImpl(cmdAddr, resultAddr); mMemoryCtx.eraseElement(progressAddr); ScAddr const commonArc = mMemoryCtx.createEdge(sc_type_const | sc_type_arc_common, cmdAddr, resultAddr); assert(commonArc.isValid()); ScAddr const arc = mMemoryCtx.createEdge(sc_type_arc_pos_const_perm, msNrelResult, commonArc); assert(arc.isValid()); mMemoryCtx.createEdge(sc_type_arc_pos_const_perm, msCommandFinishedAddr, cmdAddr); return SC_RESULT_OK; } } return SC_RESULT_ERROR; }
ScAddr ScMemoryContext::getEdgeTarget(ScAddr const & edgeAddr) const { check_expr(isValid()); ScAddr addr; if (sc_memory_get_arc_end(mContext, edgeAddr.mRealAddr, &addr.mRealAddr) != SC_RESULT_OK) addr.reset(); return addr; }
bool addToSet(ScMemoryContext & ctx, ScAddr const & setAddr, ScAddr const & elAddr) { if (ctx.helperCheckArc(setAddr, elAddr, sc_type_arc_pos_const_perm)) return false; ScAddr arcAddr = ctx.createArc(sc_type_arc_pos_const_perm, setAddr, elAddr); assert(arcAddr.isValid()); return true; }
bool ScMemoryContext::helperResolveSystemIdtf(std::string const & sysIdtf, ScAddr & outAddr, bool bForceCreation /*= false*/) { check_expr(isValid()); outAddr.reset(); bool result = helperFindBySystemIdtf(sysIdtf, outAddr); if (!result && bForceCreation) { outAddr = createNode(sc_type_const); if (outAddr.isValid()) result = helperSetSystemIdtf(sysIdtf, outAddr); } return result; }
bool ScMemoryContext::getEdgeInfo(ScAddr const & edgeAddr, ScAddr & outSourceAddr, ScAddr & outTargetAddr) const { check_expr(isValid()); if (sc_memory_get_arc_info(mContext, *edgeAddr, &outSourceAddr.mRealAddr, &outTargetAddr.mRealAddr) != SC_RESULT_OK) { outSourceAddr.reset(); outTargetAddr.reset(); return false; } return true; }
void setMass(ScMemoryContext & ctx, ScAddr const & objAddr, ScAddr const & valueAddr) { ScAddr massAddr; ScIterator5Ptr itMass = ctx.iterator5(objAddr, SC_TYPE(sc_type_arc_common | sc_type_const), SC_TYPE(sc_type_const | sc_type_node | sc_type_node_abstract), SC_TYPE(sc_type_arc_pos_const_perm), Keynodes::nrel_mass); /// TODO: check if there is a more than one result if (itMass->next()) { massAddr = itMass->value(2); } else { massAddr = ctx.createNode(sc_type_const | sc_type_node_abstract); assert(massAddr.isValid()); ScAddr arcCommon = ctx.createArc(sc_type_const | sc_type_arc_common, objAddr, massAddr); assert(arcCommon.isValid()); ScAddr arc = ctx.createArc(sc_type_arc_pos_const_perm, Keynodes::nrel_mass, arcCommon); assert(arc.isValid()); } ScIterator5Ptr itValue = ctx.iterator5(massAddr, SC_TYPE(sc_type_arc_pos_const_perm), SC_TYPE(sc_type_link), SC_TYPE(sc_type_arc_pos_const_perm), Keynodes::rrel_gram); if (itValue->next()) { ScAddr linkAddr = itValue->value(2); ScStream stream; bool res = ctx.getLinkContent(valueAddr, stream); assert(res); res = ctx.setLinkContent(linkAddr, stream); assert(res); } else { ScAddr arc = ctx.createArc(sc_type_arc_pos_const_perm, massAddr, valueAddr); assert(arc.isValid()); arc = ctx.createArc(sc_type_arc_pos_const_perm, Keynodes::rrel_gram, arc); assert(arc.isValid()); } }
bool ScStruct::append(ScAddr const & elAddr, ScAddr const & attrAddr) { check_expr(mContext); if (!hasElement(elAddr)) { ScAddr const edge = mContext->createEdge(sc_type_arc_pos_const_perm, mAddr, elAddr); if (edge.isValid()) { ScAddr const edge2 = mContext->createEdge(sc_type_arc_pos_const_perm, attrAddr, edge); if (edge2.isValid()) return true; // cleanup mContext->eraseElement(edge); } } return false; }
std::string ScMemoryContext::helperGetSystemIdtf(ScAddr const & addr) { check_expr(isValid()); ScAddr idtfLink; if (sc_helper_get_system_identifier_link(mContext, *addr, &idtfLink.mRealAddr) == SC_RESULT_OK) { if (idtfLink.isValid()) { ScStream stream; if (getLinkContent(idtfLink, stream)) { std::string result; if (StreamConverter::streamToString(stream, result)) { return result; } } } } return std::string(""); }
void test_base_commands() { { ScAddr addr = sctpClient.CreateNode(sc_type_node | sc_type_node_class); g_assert(addr.IsValid()); g_assert(sctpClient.IsElement(addr)); g_assert(sctpClient.GetElementType(addr) == (sc_type_node | sc_type_node_class)); g_assert(sctpClient.EraseElement(addr)); g_assert(!sctpClient.IsElement(addr)); } { ScAddr addr1 = sctpClient.CreateNode(sc_type_node); ScAddr addr2 = sctpClient.CreateNode(sc_type_node); ScAddr arc = sctpClient.CreateArc(sc_type_arc_pos_const_perm, addr1, addr2); g_assert(arc.IsValid()); g_assert(sctpClient.GetElementType(arc) == sc_type_arc_pos_const_perm); ScAddr a1, a2; g_assert(sctpClient.GetArcInfo(arc, a1, a2)); g_assert(a1 == addr1); g_assert(a2 == addr2); } { ScAddr link = sctpClient.CreateLink(); char * data = "Test data"; IScStreamPtr stream(new ScStream(data, (sc_uint32)strlen(data), SC_STREAM_FLAG_READ | SC_STREAM_FLAG_SEEK)); g_assert(link.IsValid()); g_assert(sctpClient.SetLinkContent(link, stream)); g_assert(stream->Seek(SC_STREAM_SEEK_SET, 0)); IScStreamPtr stream2; g_assert(sctpClient.GetLinkContent(link, stream2)); g_assert(stream->Size() == stream2->Size()); for (sc_uint32 i = 0; i < stream->Size(); ++i) { char v1, v2; size_t r1, r2; g_assert(stream->Read(&v1, sizeof(v1), r1)); g_assert(stream2->Read(&v2, sizeof(v2), r2)); g_assert(r1 == r2); g_assert(v1 == v2); } } }
ScIterator3Ptr createIterator(ScTemplateConstr3 const & constr) { ScTemplateItemValue const * values = constr.getValues(); ScTemplateItemValue const & value0 = values[0]; ScTemplateItemValue const & value1 = values[1]; ScTemplateItemValue const & value2 = values[2]; ScAddr const addr0 = resolveAddr(value0); ScAddr const addr1 = resolveAddr(value1); ScAddr const addr2 = resolveAddr(value2); if (addr0.isValid()) { if (addr2.isValid()) // F_A_F { return mContext.iterator3(addr0, *value1.mTypeValue.upConstType(), addr2); } else // F_A_A { return mContext.iterator3(addr0, *value1.mTypeValue.upConstType(), *value2.mTypeValue.upConstType()); } } else if (addr2.isValid()) // A_A_F { return mContext.iterator3(*value0.mTypeValue.upConstType(), *value1.mTypeValue.upConstType(), addr2); } else if (addr1.isValid()) // A_F_A { return mContext.iterator3(*value0.mTypeValue.upConstType(), addr1, *value2.mTypeValue.upConstType()); } else // unknown iterator type { error("Unknown iterator type"); } return ScIterator3Ptr(); }
void test_iterators() { { ScAddr addr = sctpClient.CreateNode(0); ScAddr addr1 = sctpClient.CreateNode(0); ScAddr addr2 = sctpClient.CreateNode(0); ScAddr addr3 = sctpClient.CreateNode(0); g_assert(addr.IsValid()); g_assert(addr1.IsValid()); g_assert(addr2.IsValid()); g_assert(addr3.IsValid()); ScAddr arc1 = sctpClient.CreateArc(sc_type_arc_pos_const_perm, addr, addr1); ScAddr arc2 = sctpClient.CreateArc(sc_type_arc_pos_const_perm, addr, addr2); ScAddr arc3 = sctpClient.CreateArc(sc_type_arc_pos_const_perm, addr, addr3); g_assert(arc1.IsValid()); g_assert(arc2.IsValid()); g_assert(arc3.IsValid()); sctp::IteratorPtr iter = sctpClient.Iterator3(addr, sc_type_arc_pos_const_perm, sc_type_node); g_assert(iter->next()); g_assert(iter->getValue(0) == addr); g_assert(iter->getValue(1) == arc3); g_assert(iter->getValue(2) == addr3); g_assert(iter->next()); g_assert(iter->getValue(0) == addr); g_assert(iter->getValue(1) == arc2); g_assert(iter->getValue(2) == addr2); g_assert(iter->next()); g_assert(iter->getValue(0) == addr); g_assert(iter->getValue(1) == arc1); g_assert(iter->getValue(2) == addr1); g_assert(!iter->next()); } { ScAddr addr = sctpClient.CreateNode(0); ScAddr addr1 = sctpClient.CreateNode(0); ScAddr addr2 = sctpClient.CreateNode(0); ScAddr addr3 = sctpClient.CreateNode(0); g_assert(addr.IsValid()); g_assert(addr1.IsValid()); g_assert(addr2.IsValid()); g_assert(addr3.IsValid()); ScAddr arc1 = sctpClient.CreateArc(sc_type_arc_pos_const_perm, addr, addr1); ScAddr arc2 = sctpClient.CreateArc(sc_type_arc_pos_const_perm, addr2, arc1); ScAddr arc3 = sctpClient.CreateArc(sc_type_arc_pos_const_perm, addr3, arc1); g_assert(arc1.IsValid()); g_assert(arc2.IsValid()); g_assert(arc3.IsValid()); sctp::IteratorPtr iter = sctpClient.Iterator5(addr, sc_type_arc_pos_const_perm, addr1, sc_type_arc_pos_const_perm, sc_type_node); g_assert(iter->next()); g_assert(iter->getValue(0) == addr); g_assert(iter->getValue(1) == arc1); g_assert(iter->getValue(2) == addr1); g_assert(iter->getValue(3) == arc3); g_assert(iter->getValue(4) == addr3); g_assert(iter->next()); g_assert(iter->getValue(0) == addr); g_assert(iter->getValue(1) == arc1); g_assert(iter->getValue(2) == addr1); g_assert(iter->getValue(3) == arc2); g_assert(iter->getValue(4) == addr2); g_assert(!iter->next()); } }