def testObjectProperty(self):
        graph = rdflib.ConjunctiveGraph(getStore())

        eq1 = grrm2.EquilibriumStructure.new(graph, "http://example.com/eq1")
        ts1 = grrm2.TransitionState.new(graph, "http://example.com/ts1")
        blessdiss1 = grrm2.BarrierlessDissociated.new(
            graph, "http://example.com/blessdiss1")
        bdiss1 = grrm2.BarrierDissociated.new(graph,
                                              "http://example.com/bdiss1")
        ic1 = grrm2.InterconversionStep.new(graph, "http://example.com/ic1")
        iconv1 = grrm2.Interconversion.new(graph, "http://example.com/iconv1")
        rundata1 = grrm2.RunData.new(graph, "http://example.com/rundata1")
        run1 = grrm2.Run.new(graph, "http://example.com/run1")
        mol1 = grrm2.Molecule.new(graph, "http://example.com/mol1")
        runin1 = grrm2.RunInput.new(graph, "http://example.com/runin1")
        runout1 = grrm2.RunOutput.new(graph, "http://example.com/runout1")
        icres1 = grrm2.InterconversionResult.new(graph,
                                                 "http://example.com/icres1")

        grrm2.runInput(run1).add(eq1)
        grrm2.runInput(run1).add(ts1)
        grrm2.runInput(run1).add(ic1)
        grrm2.runInput(run1).add(blessdiss1)
        grrm2.runInput(run1).add(bdiss1)
        grrm2.runInput(run1).add(icres1)
        grrm2.runInput(run1).add(mol1)
        grrm2.runInput(run1).add(runin1)
        grrm2.runInput(run1).add(rundata1)

        self.assertRaises(Exception, grrm2.runInput(run1).add, iconv1)
        self.assertRaises(Exception, grrm2.runInput(run1).add, runout1)
        self.assertRaises(Exception, grrm2.runInput(run1).add, run1)

        grrm2.runOutput(run1).add(eq1)
        grrm2.runOutput(run1).add(ts1)
        grrm2.runOutput(run1).add(ic1)
        grrm2.runOutput(run1).add(blessdiss1)
        grrm2.runOutput(run1).add(bdiss1)
        grrm2.runOutput(run1).add(iconv1)
        grrm2.runOutput(run1).add(icres1)
        grrm2.runOutput(run1).add(mol1)
        grrm2.runOutput(run1).add(runout1)

        self.assertRaises(Exception, grrm2.runOutput(run1).add, runin1)
        self.assertRaises(Exception, grrm2.runOutput(run1).add, rundata1)
        self.assertRaises(Exception, grrm2.runOutput(run1).add, run1)

        grrm2.interconversionStart(iconv1).set(eq1)
        grrm2.interconversionEnd(iconv1).set(ts1)

        self.assertRaises(Exception,
                          grrm2.interconversionStart(iconv1).set, run1)
        self.assertRaises(Exception,
                          grrm2.interconversionEnd(iconv1).set, run1)
    def _getInterconversionEnergy(self, request, uri, interconversion_uuid):
        graph = graphstore.graph()
        try:
            route = grrm2.Interconversion.get(
                graph, utils.uuidToUri(interconversion_uuid))
        except:
            raise http.Http404

        output = {}

        start = grrm2.interconversionStart(route).get()[0]
        end = grrm2.interconversionEnd(route).get()[0]

        energies = {}
        for step in grrm2.interconversionStep(route).getAll():
            step = step[0]
            energies[grrm2.stepNumber(step).get()] = grrm2.energy(step).get()

        energies[0] = grrm2.energy(start).get()
        energies[max(energies.keys()) + 1] = grrm2.energy(end).get()

        output["energies"] = energies

        return http.HttpResponse(simplejson.dumps(output),
                                 mimetype='application/javascript')
    def dispatch(self, request, **args):
        current_uri = args["uri"]
        graph = graphstore.graph()

        try:
            iconv = grrm2.Interconversion.get(graph, current_uri)
        except:
            return None

        ctx = {}

        ctx["steps"] = []
        ctx["start"] = ResultInfo()
        ctx["end"] = ResultInfo()

        start = grrm2.interconversionStart(iconv).get()[0]
        ctx["start"].uri = start.uri()
        ctx["start"].link = "/resources/%7B" + utils.uriToUuid(
            start.uri()) + "%7D"
        iconstorage = filestorage.ResourceIcon(
            start, settings=settings.filestorage_settings)
        ctx["start"].icon = iconstorage.url(filestorage.ResourceIcon.STATIC)
        ctx["start"].energy = grrm2.energy(start).get()
        ctx["start"].label = _getLabel(start)

        end = grrm2.interconversionEnd(iconv).get()[0]
        ctx["end"].uri = end.uri()
        ctx["end"].link = "/resources/%7B" + utils.uriToUuid(end.uri()) + "%7D"
        iconstorage = filestorage.ResourceIcon(
            end, settings=settings.filestorage_settings)
        ctx["end"].icon = iconstorage.url(filestorage.ResourceIcon.STATIC)
        ctx["end"].energy = grrm2.energy(end).get()
        ctx["end"].label = _getLabel(end)

        for s in grrm2.interconversionStep(iconv).getAll():
            step = s[0]
            iconstorage = filestorage.ResourceIcon(
                step, settings=settings.filestorage_settings)
            info = StepInfo()
            info.link = "/resources/%7B" + utils.uriToUuid(step.uri()) + "%7D"
            info.icon = iconstorage.url(filestorage.ResourceIcon.STATIC)
            info.uri = step.uri()
            info.energy = grrm2.energy(step).get()
            info.number = grrm2.stepNumber(step).get()
            ctx["steps"].append(info)

        def cmpFunc(x, y):
            return cmp(x.number, y.number)

        ctx["steps"] = sorted(ctx["steps"], cmp=cmpFunc)

        return ctx
    def __init__(self, graph, parsers, mapper):
        self._mapper = mapper
        self._parsers = parsers
        self._graph = graph
        print "Resolving connections"
        for routeinfo in self._mapper.allInterconversionAdditionalInfos():
            start, end = routeinfo.startStructureLabel(
            ), routeinfo.endStructureLabel()
            print "found " + str(start) + " - " + str(end)
            route = routeinfo.interconversion()
            try:
                start_molecule = self._mapper.structureLabelToMolecule(start)
                grrm2.interconversionStart(route).set(start_molecule)
            except KeyError:
                print "Unknown start molecule for interconversion " + str(
                    route)

            try:
                end_molecule = self._mapper.structureLabelToMolecule(end)
                grrm2.interconversionEnd(route).set(end_molecule)
            except KeyError:
                print "Unknown end molecule for interconversion " + str(route)
    def dispatch(self, request, **args):
        current_uri = args["uri"]
        graph = graphstore.graph()
 
        try:
            route = grrm2.Interconversion.get(graph, current_uri)
        except:
            return None

        start = grrm2.interconversionStart(route).get()[0]
        end = grrm2.interconversionEnd(route).get()[0]
        
        iconstorage_start = filestorage.ResourceIcon(start, settings=settings.filestorage_settings)
        iconstorage_end = filestorage.ResourceIcon(end, settings=settings.filestorage_settings)
        iconstorage_route = filestorage.ResourceIcon(route, settings=settings.filestorage_settings)

        ctx = {}
        ctx["startUrl"] = "/resources/%7B"+utils.uriToUuid(start.uri())+"%7D"
        ctx["endUrl"] = "/resources/%7B"+utils.uriToUuid(end.uri())+"%7D"
        ctx["startIcon"] = iconstorage_start.url(filestorage.ResourceIcon.STATIC)
        ctx["endIcon"] = iconstorage_end.url(filestorage.ResourceIcon.STATIC)
        ctx["connectionRouteIcon"] = iconstorage_route.url(filestorage.ResourceIcon.ANIMATED)

        ctx["startStructureLabel"] = _getLabel(start)
        ctx["endStructureLabel"] = _getLabel(end)
    
        energies = {}
        for step in grrm2.interconversionStep(route).getAll():
            step = step[0]
            energies[grrm2.stepNumber(step).get()] = grrm2.energy(step).get()

        energies[0] = grrm2.energy(start).get()
        energies[max(energies.keys())+1] = grrm2.energy(end).get()

        chart_query = http.QueryDict("").copy()
        chart_query.update( {
                "cht": "lxy",
                "chs": "400x400",
                "chd": "t:"+",".join([ str(x) for x in energies.keys()])+"|"+",".join( [str(x) for x in energies.values()]),
                "chds": str(min(energies.keys()))+","+str(max(energies.keys()))+","+str(min(energies.values()))+","+str(max(energies.values())),
                "chco": "3072F3,ff0000,00aaaa",
                "chls": "2,4,1",
                "chf": "bg,s,F0F0FF",
                "chm": "s,FF0000,0,-1,5|s,0000ff,1,-1,5|s,00aa00,2,-1,5",
                "chxt": "x,y",
                "chxr": "0,"+str(min(energies.keys()))+","+str(max(energies.keys()))+"|1,"+str(min(energies.values()))+","+str(max(energies.values())),
            } )
        ctx["chartUrl"] = "http://chart.apis.google.com/chart?"+chart_query.urlencode()


        return ctx
    def dispatch(self, request, uri):
        context = {}
        graph = graphstore.graph()

        try:
            molecule = grrm2.Molecule.get(graph, uri)
        except:
            return None

        context["moleculeInfo"] = self._getMoleculeInfo(molecule)

        if context["moleculeInfo"].isInterconversionStep:
            interconversion_step = grrm2.InterconversionStep.tryCast(molecule)
            prev_step = grrm2.prevInterconversionStep(
                interconversion_step).get()
            next_step = grrm2.nextInterconversionStep(
                interconversion_step).get()

            interconversion = grrm2.interconversionStepOf(
                interconversion_step).get()
            if interconversion and interconversion[0]:
                start_structure = grrm2.interconversionStart(
                    interconversion[0]).get()
                if start_structure and start_structure[0]:
                    start_structure = start_structure[0]

                end_structure = grrm2.interconversionEnd(
                    interconversion[0]).get()
                if end_structure and end_structure[0]:
                    end_structure = end_structure[0]

            if prev_step and prev_step[0]:
                context["prevStepInfo"] = self._getMoleculeInfo(prev_step[0])
            else:
                context["prevStepInfo"] = self._getMoleculeInfo(
                    start_structure)

            if next_step and next_step[0]:
                context["nextStepInfo"] = self._getMoleculeInfo(next_step[0])
            else:
                context["nextStepInfo"] = self._getMoleculeInfo(end_structure)

        else:
            context["isInterconversion"] = False
            context["fromRoutes"] = self._getFromRoutesContext(molecule)
            context["toRoutes"] = self._getToRoutesContext(molecule)

        return context
    def _getFromRoutesContext(self, molecule):

        icres = grrm2.InterconversionResult.tryCast(molecule)

        context = []
        iconvs = grrm2.interconversionEndOf(icres).getAll()
        if iconvs is None:
            return context

        for iconv in iconvs:
            iconv = iconv[0]
            start = grrm2.interconversionStart(iconv).get()[0]

            route_info = RouteInfo()
            route_info.link = "/resources/%7B" + utils.uriToUuid(
                iconv.uri()) + "%7D"

            route_info.targetLink = "/resources/%7B" + utils.uriToUuid(
                start.uri()) + "%7D"
            route_info.targetIcon = filestorage.ResourceIcon(
                start, settings=settings.filestorage_settings).url(
                    filestorage.ResourceIcon.STATIC)
            struct_number = grrm2.structureNumber(start).get()
            if grrm2.EquilibriumStructure.tryCast(start):
                struct_type = "EQ"
            elif grrm2.TransitionState.tryCast(start):
                struct_type = "TS"
            elif grrm2.BarrierlessDissociated.tryCast(start):
                struct_type = "DDC"
            elif grrm2.BarrierDissociated.tryCast(start):
                struct_type = "UDC"

            route_info.targetStructureLabel = struct_type + str(struct_number)
            route_info.targetEnergy = grrm2.energy(start).get()

            route_info.energyDifference = grrm2.energy(
                start).get() - grrm2.energy(molecule).get()

            route_info.icon = filestorage.ResourceIcon(
                iconv, settings=settings.filestorage_settings).url(
                    filestorage.ResourceIcon.ANIMATED)

            context.append(route_info)

        return context
runs = list(grrm2.Run.all(graph))

if len(runs) != 1:
    raise Exception("Run length != 1")

storage = filestorage.ResourceStorage(runs[0], web_accessible=True, settings=settings.filestorage_settings)
print "writing file : "+storage.path("connectivity","csv")
f=file(storage.path("connectivity","csv"),"w")
for icresult in grrm2.InterconversionResult.all(graph):
    label=_getLabel(icresult)
    f.write("v,"+label+","+utils.uriToUuid(icresult.uri())+"\n")


for iconv in grrm2.Interconversion.all(graph):
    start = grrm2.interconversionStart(iconv).get()[0]
    if grrm2.interconversionStart(iconv).get() is not None:
        start = grrm2.interconversionStart(iconv).get()[0]
    else:
        continue

    if grrm2.interconversionEnd(iconv).get() is not None:
        end = grrm2.interconversionEnd(iconv).get()[0]
    else:
        continue

    start_energy = grrm2.energy(start).get()
    end_energy = grrm2.energy(end).get()

    f.write("e,"+str(end_energy-start_energy)+","+_getLabel(start)+","+_getLabel(end)+","+utils.uriToUuid(iconv.uri())+"\n")