Skip to content

Commit

Permalink
Moved relation-name-construction to new utils file.
Browse files Browse the repository at this point in the history
  • Loading branch information
azreika committed Nov 13, 2020
1 parent 4c61b86 commit d225693
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 59 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Expand Up @@ -205,6 +205,8 @@ souffle_sources = \
ast2ram/ProvenanceClauseTranslator.h \
ast2ram/ProvenanceTranslator.cpp \
ast2ram/ProvenanceTranslator.h \
ast2ram/utility/Utils.cpp \
ast2ram/utility/Utils.h \
ast2ram/ValueIndex.cpp \
ast2ram/ValueIndex.h \
ast2ram/ValueTranslator.cpp \
Expand Down
1 change: 1 addition & 0 deletions src/ast/utility/Utils.h
Expand Up @@ -40,6 +40,7 @@ class TranslationUnit;
class Type;
class Variable;
class RecordInit;

namespace analysis {
class TypeAnalysis;
}
Expand Down
23 changes: 1 addition & 22 deletions src/ast2ram/AstToRamTranslator.cpp
Expand Up @@ -60,6 +60,7 @@
#include "ast2ram/Location.h"
#include "ast2ram/ValueIndex.h"
#include "ast2ram/ValueTranslator.h"
#include "ast2ram/utility/Utils.h"
#include "ram/Call.h"
#include "ram/Clear.h"
#include "ram/Condition.h"
Expand Down Expand Up @@ -196,23 +197,6 @@ std::vector<std::map<std::string, std::string>> AstToRamTranslator::getOutputDir
return outputDirectives;
}

std::string AstToRamTranslator::getConcreteRelationName(const ast::Atom* atom) {
return getRelationName(atom->getQualifiedName());
}

std::string AstToRamTranslator::getConcreteRelationName(
const ast::Relation* rel, const std::string relationNamePrefix) {
return relationNamePrefix + getRelationName(rel->getQualifiedName());
}

std::string AstToRamTranslator::getDeltaRelationName(const ast::Relation* rel) {
return getConcreteRelationName(rel, "@delta_");
}

std::string AstToRamTranslator::getNewRelationName(const ast::Relation* rel) {
return getConcreteRelationName(rel, "@new_");
}

Own<ram::Expression> AstToRamTranslator::translateValue(const ast::Argument* arg, const ValueIndex& index) {
if (arg == nullptr) return nullptr;
return ValueTranslator::translate(*this, index, getSymbolTable(), *arg);
Expand Down Expand Up @@ -357,11 +341,6 @@ void AstToRamTranslator::nameUnnamedVariables(ast::Clause* clause) {
}
}

/** converts the given relation identifier into a relation name */
std::string AstToRamTranslator::getRelationName(const ast::QualifiedName& id) {
return toString(join(id.getQualifiers(), "."));
}

VecOwn<ram::Statement> AstToRamTranslator::translateSCC(size_t scc, size_t idx) {
// make a new ram statement for the current SCC
VecOwn<ram::Statement> current;
Expand Down
16 changes: 0 additions & 16 deletions src/ast2ram/AstToRamTranslator.h
Expand Up @@ -95,13 +95,6 @@ class AstToRamTranslator {
/** translate an AST argument to a RAM value */
Own<ram::Expression> translateValue(const ast::Argument* arg, const ValueIndex& index);

/** Get the corresponding concretised RAM relation name for the atom */
static std::string getConcreteRelationName(const ast::Atom* atom);

/** Get the corresponding concretised RAM relation name for the relation */
static std::string getConcreteRelationName(
const ast::Relation* rel, const std::string relationNamePrefix = "");

/** determine the auxiliary for relations */
size_t getEvaluationArity(const ast::Atom* atom) const;

Expand Down Expand Up @@ -170,15 +163,6 @@ class AstToRamTranslator {
/** replace ADTs with special records */
static bool removeADTs(const ast::TranslationUnit& translationUnit);

/** converts the given relation identifier into a relation name */
static std::string getRelationName(const ast::QualifiedName& id);

/** Get the corresponding RAM delta relation name for the relation */
static std::string getDeltaRelationName(const ast::Relation* rel);

/** Get the corresponding RAM 'new' relation name for the relation */
static std::string getNewRelationName(const ast::Relation* rel);

// TODO (b-scholz): revisit / refactor so that only one directive is translated
std::vector<std::map<std::string, std::string>> getInputDirectives(const ast::Relation* rel);

Expand Down
31 changes: 14 additions & 17 deletions src/ast2ram/ClauseTranslator.cpp
Expand Up @@ -30,6 +30,7 @@
#include "ast2ram/AstToRamTranslator.h"
#include "ast2ram/Location.h"
#include "ast2ram/ValueIndex.h"
#include "ast2ram/utility/Utils.h"
#include "ram/Aggregate.h"
#include "ram/Break.h"
#include "ram/Conjunction.h"
Expand Down Expand Up @@ -71,7 +72,7 @@ Own<ram::Statement> ClauseTranslator::translateClause(
}

// create a fact statement
return mk<ram::Query>(mk<ram::Project>(translator.getConcreteRelationName(head), std::move(values)));
return mk<ram::Query>(mk<ram::Project>(getConcreteRelationName(head), std::move(values)));
}

// the rest should be rules
Expand Down Expand Up @@ -187,8 +188,8 @@ Own<ram::Statement> ClauseTranslator::translateClause(
auto expr = translator.translateValue(agg->getTargetExpression(), *valueIndex);

// add Ram-Aggregation layer
op = mk<ram::Aggregate>(std::move(op), agg->getFinalType().value(),
translator.getConcreteRelationName(atom), expr ? std::move(expr) : mk<ram::UndefValue>(),
op = mk<ram::Aggregate>(std::move(op), agg->getFinalType().value(), getConcreteRelationName(atom),
expr ? std::move(expr) : mk<ram::UndefValue>(),
aggCond ? std::move(aggCond) : mk<ram::True>(), level);
} else if (const auto* func = dynamic_cast<const ast::IntrinsicFunctor*>(cur)) {
VecOwn<ram::Expression> args;
Expand Down Expand Up @@ -236,14 +237,13 @@ Own<ram::Statement> ClauseTranslator::translateClause(

// add check for emptiness for an atom
op = mk<ram::Filter>(
mk<ram::Negation>(mk<ram::EmptinessCheck>(translator.getConcreteRelationName(atom))),
std::move(op));
mk<ram::Negation>(mk<ram::EmptinessCheck>(getConcreteRelationName(atom))), std::move(op));

// add a scan level
if (atom->getArity() != 0 && !isAllArgsUnnamed) {
if (head->getArity() == 0) {
op = mk<ram::Break>(mk<ram::Negation>(mk<ram::EmptinessCheck>(
translator.getConcreteRelationName(head))),
op = mk<ram::Break>(
mk<ram::Negation>(mk<ram::EmptinessCheck>(getConcreteRelationName(head))),
std::move(op));
}
if (Global::config().has("profile")) {
Expand All @@ -257,10 +257,9 @@ Own<ram::Statement> ClauseTranslator::translateClause(
ss << stringify(toString(*atom)) << ';';
ss << stringify(toString(originalClause)) << ';';
ss << level << ';';
op = mk<ram::Scan>(
translator.getConcreteRelationName(atom), level, std::move(op), ss.str());
op = mk<ram::Scan>(getConcreteRelationName(atom), level, std::move(op), ss.str());
} else {
op = mk<ram::Scan>(translator.getConcreteRelationName(atom), level, std::move(op));
op = mk<ram::Scan>(getConcreteRelationName(atom), level, std::move(op));
}
}

Expand Down Expand Up @@ -295,12 +294,10 @@ Own<ram::Operation> ClauseTranslator::createOperation(const ast::Clause& clause)
values.push_back(translator.translateValue(arg, *valueIndex));
}

Own<ram::Operation> project =
mk<ram::Project>(translator.getConcreteRelationName(head), std::move(values));
Own<ram::Operation> project = mk<ram::Project>(getConcreteRelationName(head), std::move(values));

if (head->getArity() == 0) {
project = mk<ram::Filter>(
mk<ram::EmptinessCheck>(translator.getConcreteRelationName(head)), std::move(project));
project = mk<ram::Filter>(mk<ram::EmptinessCheck>(getConcreteRelationName(head)), std::move(project));
}

// build up insertion call
Expand All @@ -313,7 +310,7 @@ Own<ram::Condition> ClauseTranslator::createCondition(const ast::Clause& origina
// add stopping criteria for nullary relations
// (if it contains already the null tuple, don't re-compute)
if (head->getArity() == 0) {
return mk<ram::EmptinessCheck>(translator.getConcreteRelationName(head));
return mk<ram::EmptinessCheck>(getConcreteRelationName(head));
}
return nullptr;
}
Expand Down Expand Up @@ -429,7 +426,7 @@ void ClauseTranslator::createValueIndex(const ast::Clause& clause) {

// index each value in the atom
indexValues(atom, atom->getArguments(), nodeLevel,
translator.lookupRelation(translator.getConcreteRelationName(atom)));
translator.lookupRelation(getConcreteRelationName(atom)));
}

// add aggregation functions
Expand Down Expand Up @@ -464,7 +461,7 @@ void ClauseTranslator::createValueIndex(const ast::Clause& clause) {
for (auto* arg : atom->getArguments()) {
if (const auto* var = dynamic_cast<const ast::Variable*>(arg)) {
valueIndex->addVarReference(
*var, *aggLoc, (int)pos, translator.getConcreteRelationName(atom));
*var, *aggLoc, (int)pos, getConcreteRelationName(atom));
}
++pos;
}
Expand Down
8 changes: 4 additions & 4 deletions src/ast2ram/ConstraintTranslator.cpp
Expand Up @@ -19,6 +19,7 @@
#include "ast/analysis/AuxArity.h"
#include "ast2ram/AstToRamTranslator.h"
#include "ast2ram/ValueIndex.h"
#include "ast2ram/utility/Utils.h"
#include "ram/Constraint.h"
#include "ram/EmptinessCheck.h"
#include "ram/ExistenceCheck.h"
Expand Down Expand Up @@ -65,7 +66,7 @@ Own<ram::Condition> ConstraintTranslator::visitProvenanceNegation(const ast::Pro
}
}
return mk<ram::Negation>(
mk<ram::ProvenanceExistenceCheck>(translator.getConcreteRelationName(atom), std::move(values)));
mk<ram::ProvenanceExistenceCheck>(getConcreteRelationName(atom), std::move(values)));
}

Own<ram::Condition> ConstraintTranslator::visitNegation(const ast::Negation& neg) {
Expand All @@ -76,7 +77,7 @@ Own<ram::Condition> ConstraintTranslator::visitNegation(const ast::Negation& neg

if (arity == 0) {
// for a nullary, negation is a simple emptiness check
return mk<ram::EmptinessCheck>(translator.getConcreteRelationName(atom));
return mk<ram::EmptinessCheck>(getConcreteRelationName(atom));
}

// else, we construct the atom and create a negation
Expand All @@ -88,7 +89,6 @@ Own<ram::Condition> ConstraintTranslator::visitNegation(const ast::Negation& neg
for (size_t i = 0; i < auxiliaryArity; i++) {
values.push_back(mk<ram::UndefValue>());
}
return mk<ram::Negation>(
mk<ram::ExistenceCheck>(translator.getConcreteRelationName(atom), std::move(values)));
return mk<ram::Negation>(mk<ram::ExistenceCheck>(getConcreteRelationName(atom), std::move(values)));
}
} // namespace souffle::ast2ram
1 change: 1 addition & 0 deletions src/ast2ram/ProvenanceTranslator.cpp
Expand Up @@ -23,6 +23,7 @@
#include "ast/utility/Visitor.h"
#include "ast2ram/ProvenanceClauseTranslator.h"
#include "ast2ram/ValueIndex.h"
#include "ast2ram/utility/Utils.h"
#include "ram/ExistenceCheck.h"
#include "ram/Filter.h"
#include "ram/Negation.h"
Expand Down
46 changes: 46 additions & 0 deletions src/ast2ram/utility/Utils.cpp
@@ -0,0 +1,46 @@
/*
* Souffle - A Datalog Compiler
* Copyright (c) 2020 The Souffle Developers. All rights reserved
* Licensed under the Universal Permissive License v 1.0 as shown at:
* - https://opensource.org/licenses/UPL
* - <souffle root>/licenses/SOUFFLE-UPL.txt
*/

/************************************************************************
*
* @file Utils.cpp
*
* A collection of utilities used in translation
*
***********************************************************************/

#include "ast2ram/utility/Utils.h"
#include "ast/Atom.h"
#include "ast/QualifiedName.h"
#include "ast/Relation.h"
#include "souffle/utility/StringUtil.h"
#include <string>

namespace souffle::ast2ram {

std::string getConcreteRelationName(const ast::Atom* atom) {
return getRelationName(atom->getQualifiedName());
}

std::string getConcreteRelationName(const ast::Relation* rel, const std::string relationNamePrefix) {
return relationNamePrefix + getRelationName(rel->getQualifiedName());
}

std::string getDeltaRelationName(const ast::Relation* rel) {
return getConcreteRelationName(rel, "@delta_");
}

std::string getNewRelationName(const ast::Relation* rel) {
return getConcreteRelationName(rel, "@new_");
}

std::string getRelationName(const ast::QualifiedName& id) {
return toString(join(id.getQualifiers(), "."));
}

} // namespace souffle::ast2ram
44 changes: 44 additions & 0 deletions src/ast2ram/utility/Utils.h
@@ -0,0 +1,44 @@
/*
* Souffle - A Datalog Compiler
* Copyright (c) 2020 The Souffle Developers. All rights reserved
* Licensed under the Universal Permissive License v 1.0 as shown at:
* - https://opensource.org/licenses/UPL
* - <souffle root>/licenses/SOUFFLE-UPL.txt
*/

/************************************************************************
*
* @file Utils.h
*
* A collection of utilities used in translation
*
***********************************************************************/

#pragma once

#include <string>

namespace souffle::ast {
class Atom;
class QualifiedName;
class Relation;
} // namespace souffle::ast

namespace souffle::ast2ram {

/** Get the corresponding concretised RAM relation name for the atom */
std::string getConcreteRelationName(const ast::Atom* atom);

/** Get the corresponding concretised RAM relation name for the relation */
std::string getConcreteRelationName(const ast::Relation* rel, const std::string relationNamePrefix = "");

/** converts the given relation identifier into a relation name */
std::string getRelationName(const ast::QualifiedName& id);

/** Get the corresponding RAM delta relation name for the relation */
std::string getDeltaRelationName(const ast::Relation* rel);

/** Get the corresponding RAM 'new' relation name for the relation */
std::string getNewRelationName(const ast::Relation* rel);

} // namespace souffle::ast2ram

0 comments on commit d225693

Please sign in to comment.