# Copyright (c) 2000-2001 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
__revision__ = '$Id: tags.py,v 1.4 2001/12/21 13:52:51 syt Exp $'
"""Tags module.

Handles relationships between elements.

Steps and transitions can tag elements to mark that they used them. Later,
steps and transitions that have use='yes' in their prototype will have
elements tagged by them filtered out automatically.
"""

from Ft.Rdf.Model import Model
from Ft.Rdf.Drivers.Memory import DbAdapter
from Ft.Rdf.Statement import Statement

TagsDb = DbAdapter('narval_tags')
TagsModel = Model(TagsDb)

def _get_id(element):
    # FIXME: element always  have an eid?
    return element.eid or str(hash(element))

def is_tagged(element, node):
    TagsDb.begin()
    answer = TagsModel.complete(_get_id(element),'tagged-by',node.tagger_id())
    TagsDb.commit()
    return answer


def tag_element(element, node):
    s = Statement(_get_id(element),'tagged-by',node.tagger_id())
    TagsDb.begin()
    TagsModel.add([s])
    TagsDb.commit()


def clear_tags(element):
    TagsDb.begin()
    TagsModel.removePattern(_get_id(element),'tagged-by',None)
    TagsDb.commit()

