from rdflib import Graph
from module import Module

class Sparql(Module):

    def __init__(self, mInstanceIRI, moduleDir):
        Module.__init__(self,  "http://topbraid.org/sparqlmotionlib#sparql", mInstanceIRI, moduleDir)

        self.append = self.getParameter("append")

        if self.getParameter("queryFile") :
            self.constructQuery=open(self.moduleDir+"/"+self.getParameter("queryFile")).read()
        else :
            self.constructQuery=self.getParameter("queryString")

        if self.append:
            self.constructQuery=self.constructQuery + self.append

    def run(self):
        self.outputGraph = Graph()
        print "- querying ..."
        print self.constructQuery
        result = self.inputGraph.query(self.constructQuery)
        print "- merging graphs ..."    
        self.outputGraph = self.outputGraph + result.graph
