Example #1
0
void
BayesBall::constructGraph (FactorGraph* fg) const
{
  const FacNodes& facNodes = fg_.facNodes();
  for (size_t i = 0; i < facNodes.size(); i++) {
    const BBNode* n = dag_.getNode (
        facNodes[i]->factor().argument (0));
    if (n->isMarkedAbove()) {
      fg->addFactor (facNodes[i]->factor());
    } else if (n->hasEvidence() && n->isVisited()) {
      VarIds varIds = { facNodes[i]->factor().argument (0) };
      Ranges ranges = { facNodes[i]->factor().range (0) };
      Params params (ranges[0], LogAware::noEvidence());
      params[n->getEvidence()] = LogAware::withEvidence();
      fg->addFactor (Factor (varIds, ranges, params));
    }
  }
  const VarNodes& varNodes = fg_.varNodes();
  for (size_t i = 0; i < varNodes.size(); i++) {
    if (varNodes[i]->hasEvidence()) {
      VarNode* vn = fg->getVarNode (varNodes[i]->varId());
      if (vn) {
        vn->setEvidence (varNodes[i]->getEvidence());
      }
    }
  }
}
Example #2
0
Params
BeliefProp::getPosterioriOf (VarId vid)
{
  if (runned_ == false) {
    runSolver();
  }
  assert (fg.getVarNode (vid));
  VarNode* var = fg.getVarNode (vid);
  Params probs;
  if (var->hasEvidence()) {
    probs.resize (var->range(), LogAware::noEvidence());
    probs[var->getEvidence()] = LogAware::withEvidence();
  } else {
    probs.resize (var->range(), LogAware::multIdenty());
    const BpLinks& links = getLinks (var);
    if (Globals::logDomain) {
      for (size_t i = 0; i < links.size(); i++) {
        probs += links[i]->message();
      }
      LogAware::normalize (probs);
      Util::exp (probs);
    } else {
      for (size_t i = 0; i < links.size(); i++) {
        probs *= links[i]->message();
      }
      LogAware::normalize (probs);
    }
  }
  return probs;
}
Example #3
0
ExpressionNode* handleIdentifier(const std::string& name)
{
    VarNode* node = new VarNode(name);
    node->setLocation(FileLocation(yylineno));
    return node;
#if 0
    if (SymbolTable::isVariableDeclared(name)) {
        return new VarNode(name);
    } else {
        if (SymbolTable::isFunctionDeclared(name)) {
            return new FuncRefNode(name);
        } else {
            // This is a small problem: we want the error below, to catch
            // usage of undeclared identifiers, but the function start section
            // would then generate errors, because we are not yet in the function
            // scope.

            // std::string e("unknown identifier ");
            // e += name;
            // yyerror(e.c_str());
            return new JustAnIdentifierNode(name);
        }
    }
#endif
}
Example #4
0
void AssgNodeTest::testLinks() {

	AssgNode anode(1);
	VarNode vnode("x");
	OpNode onode("+");
	
	// linkexprnode
	anode.linkExprNode(&onode);

	// linkvarnode
	anode.linkVarNode(&vnode);

	// getvarnode
	VarNode* vget = anode.getVarNode();
	CPPUNIT_ASSERT_EQUAL(&vnode, vget);
	string expname = "x";
	CPPUNIT_ASSERT_EQUAL(expname, vget->getName());

	// getexprnode
	OpNode* oget = (OpNode*)(anode.getExprNode());
	CPPUNIT_ASSERT_EQUAL(&onode, oget);
	string exponame = "+";
	CPPUNIT_ASSERT_EQUAL(exponame, oget->getName());

	return;
}
Example #5
0
Params
BeliefProp::getJointDistributionOf (const VarIds& jointVarIds)
{
  if (runned_ == false) {
    runSolver();
  }
  VarNode* vn = fg.getVarNode (jointVarIds[0]);
  const FacNodes& facNodes = vn->neighbors();
  size_t idx = facNodes.size();
  for (size_t i = 0; i < facNodes.size(); i++) {
    if (facNodes[i]->factor().contains (jointVarIds)) {
      idx = i;
      break;
    }
  }
  if (idx == facNodes.size()) {
    return getJointByConditioning (jointVarIds);
  }
  return getFactorJoint (facNodes[idx], jointVarIds);
}
Example #6
0
void TestOne::testNestedWhileAST() {
	parser.parse("procedure nestedWhile {while x{ while y{z = 2;}}}");
	CPPUNIT_ASSERT(ast->contains("nestedWhile"));
	
	ProcNode* procNode = ast->getProcNode("nestedWhile");
	CPPUNIT_ASSERT(procNode->hasChildren());
	
	StmtLstNode* stmtLst = procNode->getStmtLstNode();
	CPPUNIT_ASSERT(stmtLst->hasChildren());
	CPPUNIT_ASSERT(stmtLst->getChildren().at(0)->getNodeType() == WHILE_STMT_);

	WhileNode* firstWhile = (WhileNode*)stmtLst->getChildren().at(0);
	CPPUNIT_ASSERT(firstWhile->getChildren().size() == 2);
	CPPUNIT_ASSERT(firstWhile->getVarNode()->getName() == "x");
	CPPUNIT_ASSERT(firstWhile->getStmtNum() == 1);

	StmtLstNode* firstWhileStmtLst = firstWhile->getStmtLstNode();
	CPPUNIT_ASSERT(firstWhileStmtLst->hasChildren());
	CPPUNIT_ASSERT(firstWhileStmtLst->getChildren().size() == 1);
	
	WhileNode* secondWhile = (WhileNode*)firstWhileStmtLst->getChildren().at(0);
	CPPUNIT_ASSERT(secondWhile->getChildren().size() == 2);
	CPPUNIT_ASSERT(secondWhile->getVarNode()->getName() == "y");
	CPPUNIT_ASSERT(secondWhile->getStmtNum() == 2);
	
	StmtLstNode* secondWhileStmtLst = secondWhile->getStmtLstNode();
	CPPUNIT_ASSERT(secondWhileStmtLst->hasChildren());
	CPPUNIT_ASSERT(secondWhileStmtLst->getChildren().size() == 1);
	CPPUNIT_ASSERT(secondWhileStmtLst->getChildren().at(0)->getNodeType() == ASSIGN_STMT_);

	AssgNode* assign = (AssgNode*)secondWhileStmtLst->getChildren().at(0);
	CPPUNIT_ASSERT(assign->hasChildren());
	CPPUNIT_ASSERT(assign->getChildren().size() == 2);
	
	VarNode* modifiedVar = (VarNode*)assign->getChildren().at(0);
	CPPUNIT_ASSERT(modifiedVar->getName() == "z");

	ConstNode* constant = (ConstNode*)assign->getChildren().at(1);
	CPPUNIT_ASSERT(constant->getName() == "2");
}
void PrintEquelleASTVisitor::visit(VarNode& node)
{
    std::cout << node.name();
}
Example #8
0
void PrintASTVisitor::visit(VarNode& node)
{
    std::cout << indent() << "VarNode: " << node.name() << '\n';
}
Example #9
0
void Graph::toDotLines(std::string s, raw_ostream *stream) {

		Instruction *A;
		StringRef File;
		OpNode *op;
		VarNode *va;
		CallNode *ca;
		ostringstream label;
		MDNode *N;

		unsigned Line;


        (*stream) << "digraph \"DFG for \'" << s << "\'  \"{\n";
        (*stream) << "label=\"DFG for \'" << s << "\' \";\n";

        std::map<GraphNode*, int> DefinedNodes;

        for (std::set<GraphNode*>::iterator node = nodes.begin(), end = nodes.end(); node != end; node++) {
        		label.str("");
        		if ((op = dyn_cast<OpNode>((*node)))) {
        			if (op->getValue() != NULL) {
						if ((A = dyn_cast<Instruction>(op->getValue()))) {
							if ((N = A->getMetadata("dbg"))) {
								DILocation Loc(N);
								File = Loc.getFilename();
								Line = Loc.getLineNumber();
								label << File.str() << " " << Line;
							} else {
								label << "null";
							}
						}
        			}else
        				label << "null";
        		}else if ((va = dyn_cast<VarNode>((*node)))) {
        			if (va->getValue() != NULL) {
						if ((A = dyn_cast<Instruction>(va->getValue()))) {
							if ((N = A->getMetadata("dbg"))) {
								DILocation Loc(N);
								File = Loc.getFilename();
								Line = Loc.getLineNumber();
								label << File.str() << " " << Line;
							} else {
								label << "null";
							}
						}
        			}else
        				label << "null";
        		}else if ((ca = dyn_cast<CallNode>((*node)))) {
        			if (ca->getValue() != NULL) {
						if ((A = dyn_cast<Instruction>(ca->getValue()))) {
							if ((N = A->getMetadata("dbg"))) {
								DILocation Loc(N);
								File = Loc.getFilename();
								Line = Loc.getLineNumber();
								label << File.str() << " " << Line;
							} else {
								label << "null";
							}
						}
        			}else
        				label << "null";
        		}


        		if (DefinedNodes.count(*node) == 0) {
                        (*stream) << (*node)->getName() << "[shape=" << (*node)->getShape() << ",style=" << (*node)->getStyle() << ",label=\"" << label.str() << "\"]\n";
                        DefinedNodes[*node] = 1;
                }


                std::map<GraphNode*, edgeType> succs = (*node)->getSuccessors();

                for (std::map<GraphNode*, edgeType>::iterator succ = succs.begin(), s_end = succs.end(); succ != s_end; succ++) {
                					label.str("");
                					if ((op = dyn_cast<OpNode>((succ->first)))) {
                						if (op->getValue() != NULL) {
											if ((A = dyn_cast<Instruction>(op->getValue()))) {
												if ((N = A->getMetadata("dbg"))) {
													DILocation Loc(N);
													File = Loc.getFilename();
													Line = Loc.getLineNumber();
													label << File.str() << " " << Line;
												} else {
													label << "null";
												}
											}
                						}else
                						   label << "null";
                	        		}else if ((va = dyn_cast<VarNode>((succ->first)))) {
                	        			if (va->getValue() != NULL) {
											if ((A = dyn_cast<Instruction>(va->getValue()))) {
												if ((N = A->getMetadata("dbg"))) {
													DILocation Loc(N);
													File = Loc.getFilename();
													Line = Loc.getLineNumber();
													label << File.str() << " " << Line;
												} else {
													label << "null";
												}
											}
                	        			}else
                	        			   label << "null";
                	        		}else if ((ca = dyn_cast<CallNode>((succ->first)))) {
                	        			if (ca->getValue() != NULL) {
											if ((A = dyn_cast<Instruction>(ca->getValue()))) {
												if ((N = A->getMetadata("dbg"))) {
													DILocation Loc(N);
													File = Loc.getFilename();
													Line = Loc.getLineNumber();
													label << File.str() << " " << Line;
												} else {
													label << "null";
												}
											}
                	        			}else
                	        			  label << "null";
                	        		}


                        if (DefinedNodes.count(succ->first) == 0) {
                                (*stream) << (succ->first)->getName() << "[shape="
                                                << (succ->first)->getShape() << ",style="
                                                << (succ->first)->getStyle() << ",label=\""
                                                << label.str() << "\"]\n";
                                DefinedNodes[succ->first] = 1;
                        }

                        //Source
                        (*stream) << "\"" << (*node)->getName() << "\"";

                        (*stream) << "->";

                        //Destination
                        (*stream) << "\"" << (succ->first)->getName() << "\"";

                        if (succ->second == etControl)
                                (*stream) << " [style=dashed]";

                        (*stream) << "\n";

                }

        }

        (*stream) << "}\n\n";

}
Example #10
0
VarIds
readQueryAndEvidence (
    FactorGraph& fg,
    int argc,
    const char* argv[],
    int start)
{
  VarIds queryIds;
  for (int i = start; i < argc; i++) {
    const string& arg = argv[i];
    if (arg.find ('=') == std::string::npos) {
      if (Util::isInteger (arg) == false) {
        cerr << "error: `" << arg << "' " ;
        cerr << "is not a variable id" ;
        cerr << endl;
        exit (0);
      }
      VarId vid = Util::stringToUnsigned (arg);
      VarNode* queryVar = fg.getVarNode (vid);
      if (queryVar == false) {
        cerr << "error: unknow variable with id " ;
        cerr << "`" << vid << "'"  << endl;
        exit (0);
      }
      queryIds.push_back (vid);
    } else {
      size_t pos = arg.find ('=');
      string leftArg  = arg.substr (0, pos);
      string rightArg = arg.substr (pos + 1);
      if (leftArg.empty()) {
        cerr << "error: missing left argument" << endl;
        cerr << USAGE << endl;
        exit (0);
      }
      if (Util::isInteger (leftArg) == false) {
        cerr << "error: `" << leftArg << "' " ;
        cerr << "is not a variable id" << endl ;
        exit (0);
        continue;
      }
      VarId vid = Util::stringToUnsigned (leftArg);
      VarNode* observedVar = fg.getVarNode (vid);
      if (observedVar == false) {
        cerr << "error: unknow variable with id " ;
        cerr << "`" << vid << "'"  << endl;
        exit (0);
      }
      if (rightArg.empty()) {
        cerr << "error: missing right argument" << endl;
        cerr << USAGE << endl;
        exit (0);
      }
      if (Util::isInteger (rightArg) == false) {
        cerr << "error: `" << rightArg << "' " ;
        cerr << "is not a state index" << endl ;
        exit (0);
      }
      unsigned stateIdx = Util::stringToUnsigned (rightArg);
      if (observedVar->isValidState (stateIdx) == false) {
        cerr << "error: `" << stateIdx << "' " ;
        cerr << "is not a valid state index for variable with id " ;
        cerr << "`" << vid << "'"  << endl;
        exit (0);
      }
      observedVar->setEvidence (stateIdx);
    }
  }
  return queryIds;
}
Example #11
0
 // [<type> <name>]
 void visit( VarNode aNode ) {
   std::cout << " [" << show( aNode.getType() )
     << " " << aNode.getName() << "]";
 }
Example #12
0
void ComputeSSO::PrintTainted(std::set<GraphNode*> tainted)
{
    //  errs()<<"\n\n Tainted Nodes: "<<tainted.size();


    for(set<GraphNode*>::iterator taintNode = tainted.begin();taintNode != tainted.end();++taintNode)
    {
        //errs()<<"\n Node Label : "<<(*taintNode)->getLabel();
        // errs()<<"--";
        if(isa<MemNode>(*taintNode))
        {
            // errs()<<"\n is mem node";
            //string nodeLab = (*taintNode)->getLabel();
           // string str = "sub_42BC4";
          //  std::size_t found = nodeLab.find(str);
            // if (found!=std::string::npos || 1)
            {

                MemNode * memNew = dyn_cast<MemNode>(*taintNode);
                Value * val = memNew->defLocation.second;
                std::set<Value*> aliases = memNew->getAliases();

                if(val)
                {
                    errs()<<"\n Sink Node Tainted : "<<(*taintNode)->getLabel();
                    if(isa<Instruction>(val))
                    {
                        Instruction * inst = dyn_cast<Instruction>(val);
                        string funcName = inst->getParent()->getParent()->getName();
                        errs()<<"\n Function: "<<funcName;
                    }
                    val->dump();
                }
                if(aliases.size()>0)
                    errs()<<"\n Sink Node Tainted : "<<(*taintNode)->getLabel();
                for(set<Value*>::iterator alVal = aliases.begin(); alVal != aliases.end();++alVal)
                {
                    if(isa<Instruction>(*alVal))
                    {
                        Instruction * inst = dyn_cast<Instruction>(*alVal);
                        string funcName = inst->getParent()->getParent()->getName();
                        errs()<<"\n Function: "<<funcName;
                    }
                    (*alVal)->dump();
                }
            }

        }
        if(isa<VarNode>(*taintNode))
        {
            VarNode * varNew = dyn_cast<VarNode>(*taintNode);
            Value * val = varNew->getValue(); //->defLocation.second;
            if(val)
            {
                errs()<<"\n Sink Node Tainted : "<<(*taintNode)->getLabel();
                if(isa<Instruction>(val))
                {
                    Instruction * inst = dyn_cast<Instruction>(val);
                    string funcName = inst->getParent()->getParent()->getName();
                    errs()<<"\n Function: "<<funcName;
                }
                val->dump();
            }
        }
        //if
    }
}