FREObject LNLeapDevice::getConfigString(uint32_t len, const uint8_t* key) { std::string keyString( key, key+len ); std::string valueString(controller->config().getString(keyString)); std::vector<uint8_t> valueVector(valueString.begin(), valueString.end()); uint8_t *valueArray = &valueVector[0]; FREObject freReturnValue; FRENewObjectFromUTF8(valueString.length(), valueArray, &freReturnValue); return freReturnValue; }
ScriptPromise BluetoothGATTCharacteristic::writeValue(ScriptState* scriptState, const DOMArrayPiece& value) { WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptState); // Partial implementation of writeValue algorithm: // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothgattcharacteristic-writevalue // If bytes is more than 512 bytes long (the maximum length of an attribute // value, per Long Attribute Values) return a promise rejected with an // InvalidModificationError and abort. if (value.byteLength() > 512) return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(InvalidModificationError, "Value can't exceed 512 bytes.")); // Let valueVector be a copy of the bytes held by value. WebVector<uint8_t> valueVector(value.bytes(), value.byteLength()); ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); ScriptPromise promise = resolver->promise(); webbluetooth->writeValue(m_webCharacteristic->characteristicInstanceID, valueVector, new CallbackPromiseAdapter<void, BluetoothError>(resolver)); return promise; }
void CExInfEngine< INF_ENGINE, MODEL, FLAV, FALLBACK_ENGINE1, FALLBACK_ENGINE2 >::MarginalNodes( int const *query, int querySize, int notExpandJPD ) { int i, j; if( querySize== 0 ) { PNL_THROW( CInconsistentSize, "query nodes vector should not be empty" ); } saved_query.assign( query, query + querySize ); if ( evidence == 0 ) { evidence_mine = true; EnterEvidence( evidence = CEvidence::Create( graphical_model, intVector(), valueVector() ) ); } active_components.resize( 0 ); query_dispenser.resize( 0 ); query_dispenser.resize( decomposition.size() ); for ( i = 0; i < querySize; ++i ) { query_dispenser[orig2comp[query[i]]].push_back( orig2idx[query[i]] ); } for ( i = decomposition.size(); i--; ) { if ( query_dispenser[i].size() ) { active_components.push_back( i ); } } EDistributionType dt=dtTabular; bool determined = false; for ( i = active_components.size(); i--; ) { j = active_components[i]; engines[j] -> MarginalNodes( &query_dispenser[j].front(), query_dispenser[j].size(), 1 ); if(!determined) { dt = engines[j]->GetQueryJPD()->GetDistributionType(); if(dt != dtScalar) determined = true; } } if(!(dt == dtTabular || dt == dtGaussian || dt == dtScalar)) PNL_THROW(CNotImplemented, "we can not support this type of potentials"); CPotential *pot1; CPotential const *pot2=NULL; intVector dom; intVector obsIndices; for(i=0; i<querySize; i++) { if(evidence->IsNodeObserved(query[i])) { obsIndices.push_back(i); } } if(query_JPD) delete query_JPD; if ( active_components.size() > 1 ) { if((dt == dtTabular) || (dt == dtScalar)) { query_JPD = CTabularPotential::CreateUnitFunctionDistribution( saved_query, graphical_model->GetModelDomain(), 1, obsIndices ); } else if(dt == dtGaussian) { query_JPD = CGaussianPotential::CreateUnitFunctionDistribution( saved_query, graphical_model->GetModelDomain(), 1, obsIndices ); } // query_JPD = pot2->ShrinkObservedNodes( evidence ); // delete( pot2 ); for ( i = active_components.size(); i--; ) { pot2 = engines[active_components[i]]->GetQueryJPD(); pot2->GetDomain( &dom ); for ( j = dom.size(); j--; ) { dom[j] = decomposition[active_components[i]][dom[j]]; } pot1 = (CPotential *)CPotential::CopyWithNewDomain( pot2, dom, graphical_model->GetModelDomain() ); *query_JPD *= *pot1; delete( pot1 ); } } else { pot2 = engines[active_components[0]]->GetQueryJPD(); pot2->GetDomain( &dom ); for ( j = dom.size(); j--; ) { dom[j] = decomposition[active_components[0]][dom[j]]; } query_JPD = (CPotential *)CPotential::CopyWithNewDomain( pot2, dom, graphical_model->GetModelDomain() ); } query_JPD->Normalize(); }
int testRandomFactors() { int ret = TRS_OK; int nnodes = 0; int i; while(nnodes <= 0) { trsiRead( &nnodes, "5", "Number of nodes in Model" ); } //create node types int seed1 = pnlTestRandSeed(); //create string to display the value char *value = new char[20]; #if 0 value = _itoa(seed1, value, 10); #else sprintf( value, "%d", seed1 ); #endif trsiRead(&seed1, value, "Seed for srand to define NodeTypes etc."); delete []value; trsWrite(TW_CON|TW_RUN|TW_DEBUG|TW_LST, "seed for rand = %d\n", seed1); //create 2 node types and model domain for them nodeTypeVector modelNodeType; modelNodeType.resize(2); modelNodeType[0] = CNodeType( 1, 4 ); modelNodeType[1] = CNodeType( 1, 3 ); intVector NodeAssociat; NodeAssociat.assign(nnodes, 0); for( i = 0; i < nnodes; i++ ) { float rand = pnlRand( 0.0f, 1.0f ); if( rand < 0.5f ) { NodeAssociat[i] = 1; } } CModelDomain* pMDDiscr = CModelDomain::Create( modelNodeType, NodeAssociat ); //create random graph - number of nodes for every node is rand too int lowBorder = nnodes - 1; int upperBorder = int((nnodes * (nnodes - 1))/2); int numEdges = pnlRand( lowBorder, upperBorder ); mark: CGraph* pGraph = tCreateRandomDAG( nnodes, numEdges, 1 ); if ( pGraph->NumberOfConnectivityComponents() != 1 ) { delete pGraph; goto mark; } CBNet* pDiscrBNet = CBNet::CreateWithRandomMatrices( pGraph, pMDDiscr ); //start jtree inference just for checking //the model is valid for inference and all operations can be made CEvidence* pDiscrEmptyEvid = CEvidence::Create( pMDDiscr, 0, NULL, valueVector() ); CJtreeInfEngine* pDiscrInf = CJtreeInfEngine::Create( pDiscrBNet ); pDiscrInf->EnterEvidence( pDiscrEmptyEvid ); const CPotential* pot = NULL; for( i = 0; i < nnodes; i++ ) { intVector domain; pDiscrBNet->GetFactor(i)->GetDomain( &domain ); pDiscrInf->MarginalNodes( &domain.front(), domain.size() ); pot = pDiscrInf->GetQueryJPD(); } //make copy of Graph for using with other models pGraph = CGraph::Copy( pDiscrBNet->GetGraph() ); delete pDiscrInf; delete pDiscrBNet; delete pDiscrEmptyEvid; delete pMDDiscr; //create gaussian model domain modelNodeType[0] = CNodeType( 0, 4 ); modelNodeType[1] = CNodeType( 0, 2 ); CModelDomain* pMDCont = CModelDomain::Create( modelNodeType, NodeAssociat ); CBNet* pContBNet = CBNet::CreateWithRandomMatrices( pGraph, pMDCont ); CEvidence* pContEmptyEvid = CEvidence::Create( pMDCont, 0, NULL, valueVector() ); CNaiveInfEngine* pContInf = CNaiveInfEngine::Create( pContBNet ); pContInf->EnterEvidence( pContEmptyEvid ); for( i = 0; i < nnodes; i++ ) { intVector domain; pContBNet->GetFactor(i)->GetDomain( &domain ); pContInf->MarginalNodes( &domain.front(), domain.size() ); pot = pContInf->GetQueryJPD(); } pGraph = CGraph::Copy(pContBNet->GetGraph()); delete pContInf; delete pContBNet; delete pContEmptyEvid; delete pMDCont; //find the node that haven't any parents //and change its node type for it to create Conditional Gaussian CPD int numOfNodeWithoutParents = -1; intVector parents; parents.reserve(nnodes); for( i = 0; i < nnodes; i++ ) { pGraph->GetParents( i, &parents ); if( parents.size() == 0 ) { numOfNodeWithoutParents = i; break; } } //change node type of this node, make it discrete CNodeType ntTab = CNodeType( 1,4 ); modelNodeType.push_back( ntTab ); NodeAssociat[numOfNodeWithoutParents] = 2; //need to change this model domain CModelDomain* pMDCondGau = CModelDomain::Create( modelNodeType, NodeAssociat ); CBNet* pCondGauBNet = CBNet::CreateWithRandomMatrices( pGraph, pMDCondGau ); //need to create evidence for all gaussian nodes intVector obsNodes; obsNodes.reserve(nnodes); int numGauVals = 0; for( i = 0; i < numOfNodeWithoutParents; i++ ) { int GauSize = pMDCondGau->GetVariableType(i)->GetNodeSize(); numGauVals += GauSize; obsNodes.push_back( i ); } for( i = numOfNodeWithoutParents + 1; i < nnodes; i++ ) { int GauSize = pMDCondGau->GetVariableType(i)->GetNodeSize(); numGauVals += GauSize; obsNodes.push_back( i ); } valueVector obsGauVals; obsGauVals.resize( numGauVals ); floatVector obsGauValsFl; obsGauValsFl.resize( numGauVals); pnlRand( numGauVals, &obsGauValsFl.front(), -3.0f, 3.0f); //fill the valueVector for( i = 0; i < numGauVals; i++ ) { obsGauVals[i].SetFlt(obsGauValsFl[i]); } CEvidence* pCondGauEvid = CEvidence::Create( pMDCondGau, obsNodes, obsGauVals ); CJtreeInfEngine* pCondGauInf = CJtreeInfEngine::Create( pCondGauBNet ); pCondGauInf->EnterEvidence( pCondGauEvid ); pCondGauInf->MarginalNodes( &numOfNodeWithoutParents, 1 ); pot = pCondGauInf->GetQueryJPD(); pot->Dump(); delete pCondGauInf; delete pCondGauBNet; delete pCondGauEvid; delete pMDCondGau; return trsResult( ret, ret == TRS_OK ? "No errors" : "Bad test on RandomFactors"); }