Ejemplo n.º 1
0
Relation Relation::naturalJoin(Relation relation1, Relation relation2){
    Relation newRelation;
    vector<pair<int, int>> pairIndexes;
    //Determine which columns relations have in common
    for (int i=0; i<relation1.schema.size(); i++) {
        for (int j=0; j<relation2.schema.size(); j++) {
            if(relation1.schema[i].getTokenType() == ID && relation2.schema[j].getTokenType() == ID &&
               !relation1.schema[i].getTokensValue().compare(relation2.schema[j].getTokensValue())){
                pairIndexes.push_back(make_pair(i, j));
            }
        }
    }
    
    for (int i=0; i<relation1.rows.size(); i++) {
        for (int j=0; j<relation2.rows.size(); j++) {
            bool equal = true;
            for (int k=0; k<pairIndexes.size(); k++) {
                if (relation1.getRowAtIndex(i).elements[pairIndexes[k].first].getTokensValue().compare(relation2.getRowAtIndex(j).elements[pairIndexes[k].second].getTokensValue())) {
                    equal = false;
                }
            }
            if (equal) {
                vector<Token> newVector = relation1.getRowAtIndex(i).elements;
                vector<Token> vectorToProject = relation2.getRowAtIndex(j).elements;
                
                //Eliminate Intersected Columns
                for (int k=pairIndexes.size() - 1.0; k>=0; k--) {
                    vectorToProject.erase(vectorToProject.begin() + pairIndexes[k].second);
                }
                
                newVector.insert(newVector.end(), vectorToProject.begin() ,vectorToProject.end());
                Tuple newTuple(newVector);
                newRelation.addTuple(newTuple);
            }
        }
    }
    
    vector<Token> newSchema = relation1.schema;
    vector<Token> schemaToEdit = relation2.schema;
    
    //Eliminate intercept columns in the schema
    for (int i=pairIndexes.size()-1.0; i>=0; i--) {
        schemaToEdit.erase(schemaToEdit.begin() + pairIndexes[i].second);
    }
    newSchema.insert(newSchema.end(), schemaToEdit.begin(), schemaToEdit.end());
    newRelation.schema = newSchema;
    
    return newRelation;
}
Ejemplo n.º 2
0
Relation Relation::crossProduct(Relation relation1, Relation relation2){
    Relation newRelation;
    for (int i=0; i<relation1.rows.size(); i++) {
        for (int j=0; j<relation2.rows.size(); j++) {
            vector<Token> newVector = relation1.getRowAtIndex(i).elements;
            newVector.insert(newVector.end(), relation2.getRowAtIndex(i).elements.begin() ,relation2.getRowAtIndex(j).elements.end());
            Tuple newTuple(newVector);
            newRelation.addTuple(newTuple);
        }
    }
    
    vector<Token> newSchema = relation1.schema;
    newSchema.insert(newSchema.end(), relation2.schema.begin(), relation2.schema.end());
    
    newRelation.schema = newSchema;
    
    return newRelation;
}
Ejemplo n.º 3
0
void BoltSumBytes::run()
{
	int counter = 0;
	std::unordered_map<std::string,unsigned int> protocolMap;

	while(!killRunThread)
	{
		tupleQueueLock.lock();
		if ( !tupleQueue.empty() )
		{
			Tuple tuple = tupleQueue.front();
			tupleQueue.pop_front();
			tupleQueueLock.unlock();

			if(tuple.getNumElements() != 3)
			{
				continue;
			}

			std::string protocol = tuple.getElement(0);
			unsigned int sent = atoi(tuple.getElement(1).c_str());
			unsigned int recv = atoi(tuple.getElement(2).c_str());

			unsigned int sum = protocolMap[protocol];
			sum = sum + sent + recv;

			protocolMap[protocol] = sum;
			std::stringstream ss;
			ss << protocol << std::endl;
			ss << protocolMap[protocol] << std::endl;
			Tuple newTuple(ss.str());

			std::cout << newTuple.getSingleStringComa() << std::endl;
			emit(newTuple);

		}
		else
		{
			tupleQueueLock.unlock();
		}
	}
}
Ejemplo n.º 4
0
void Database::addTuples(DatalogProgram dp){
	for (int i = 0; i < dp.facts.size(); i++){
		Tuple newTuple(dp.facts[i]);
		myRelations[dp.facts[i].name].myTuples.insert(newTuple);
	}
}
Relation Relation::rename(pair<vector<Token>, vector<Token> >& ruleParameters, Relation& targetRelation)
{
    Relation newRelation = (*this);
    const int FIRST = 0;
    const int SECOND = 1;
    vector<pair<Token, vector<vector<int> > > > myMap;
    for(int i = 0; i < ruleParameters.first.size(); i++) // Make a mapping of the rule parameters to find matches
    {
        vector<vector<int> > intVec;
        vector<int> firstVec;
        vector<int> secondVec;
        intVec.push_back(firstVec);
        intVec.push_back(secondVec);
        bool insert = true;
        for(int k = 0; k < myMap.size(); k++)
        {
            if(ruleParameters.first[i].getTokensValue() == myMap[k].first.getTokensValue()) // If token from first tvector is in map
            {
                Schema* tSchema = targetRelation.getSchema();
                Token thisToken = (*tSchema)[myMap[k].second[FIRST][FIRST]];
                tSchema->renameTokenAt(i, thisToken); // Rename right-side arguments that will be joined
                myMap[k].second[FIRST].push_back(i);
                insert = false; // don't insert that token
            }
        }
        if(insert)
        {
            intVec[FIRST].push_back(i);
            myMap.push_back(pair<Token, vector<vector<int> > >(ruleParameters.first[i], intVec));
        }
    }

    for(int i = 0; i < myMap.size(); i++) // if there are duplicates in the second vector, add to int vector
    {
        for(int j = 0; j < ruleParameters.second.size(); j++)
        {
            if(myMap[i].first.getTokensValue() == ruleParameters.second[j].getTokensValue())
            {
                myMap[i].second[SECOND].push_back(j);
            }
            else
            {
                Schema* tSchema = targetRelation.getSchema();
                if((*tSchema)[myMap[i].second[FIRST][FIRST]].getTokensValue() == (*schema)[j].getTokensValue());
                {
                    string newValue = (*schema)[j].getTokensValue() + "$";
                    Token newToken = (*schema)[j];
                    newToken.setTokenValue(newValue);
                    schema->renameTokenAt(j, newToken);
                }
            }
        }
    }

    Schema newSchema(*schema);
    for(int i = 0; i < myMap.size(); i++) // Rename the Schema
    {
        for(int j = 0; j < myMap[i].second[SECOND].size(); j++)
        {
            Schema* tSchema = targetRelation.getSchema();
            Token thisToken = (*tSchema)[myMap[i].second[FIRST][FIRST]];
            newSchema.renameTokenAt(myMap[i].second[SECOND][j], thisToken);
        }
    }
    newRelation.setSchema(newSchema);
    set<Tuple>* newTuples = new set<Tuple>();

    for(set<Tuple>::iterator it = tuples->begin(); it != tuples->end(); it++) // Copy tuples with the renamed Schema
    {
        Tuple thisTuple = (*it);
        Tuple newTuple(thisTuple, newSchema);
        newTuples->insert(newTuple);
    }
    set<Tuple>* deleteTuples = newRelation.getTuples();
    newRelation.setTuples(newTuples);
    delete deleteTuples;

    return newRelation;
}
void Relation::insertTuples(ConstantList* inputConstantList)
{
    Tuple newTuple(schema,inputConstantList);
    tuples->insert(newTuple);
}