package provide xotcl::registry::registry 0.8

package require xotcl::trace
package require xotcl::rdf::triple
package require xotcl::rdf::tripleRecreator
package require xotcl::mos::agent

Class Registry -superclass Agent

Registry instproc init args {
  next
  [self] exportProcs register query queryProperty
  RDFParser [self]::parser
  TripleVisitor [self]::tripleVisitor -parser [self]::parser
  [self]::tripleVisitor descriptionAsBag 0
  [self] array set services {}
}

Registry instproc register {rdfScript} {
  #[self] showCall
  [[self]::tripleVisitor set parser] parse $rdfScript
  [self]::tripleVisitor interpretNodeTree [self]::parser::topNode
  [self]::tripleVisitor resetWithoutDB
  foreach serviceTriple [[self]::tripleVisitor::db queryPredicate \
			   "http://nestroy.wi-inf.uni-essen.de/schema/service#name"] {
    set service [$serviceTriple object]
    if {[info exists services($service)]} {
      puts stderr "we have already such a service '$service'"
      # hier koennte man ueberlegen, den service zu loeschen oder nicht
      # zZT: loesche altes service
    }
    puts stderr "REGISTRY: registering $service with [$serviceTriple subject]"
    [self] set services($service) [$serviceTriple subject];
  }
}

Registry instproc query {service} {
  [self] showCall
  if {[info exists services($service)]} {
    set s [set [self]::services($service)]
    return [[Place getInstance]::rdfCreator createFromTriples [[self]::tripleVisitor::db querySubject $s]]
  }
}

Registry instproc queryProperty {args} {
  # returns first service with matching properties
  [self] showCall
  foreach s [[self] array names services] {
    set success 1
    foreach {att value} $args {
      set t [[self]::tripleVisitor::db queryPredicateOnSubject $att [set [self]::services($s)]]
      if {$t == "" || [$t object] != $value} {
	set success 0
	break
      }
    }
    if {$success} {
      set r [[self] query $s]
      return $r
    } else {
      return ""
    }
  }
}


