// (c) Tuomas J. Lukka package org.fenfire.structure; import java.util.Iterator; import org.fenfire.vocab.RDF; import org.fenfire.vocab.STRUCTLINK; import org.fenfire.swamp.*; import java.util.*; /** Some utility methods for handling StructLink. * XXX Should these be static or with the graph and constgraph here? * Or both? * How often will we change Graph / ConstGraph objects? Will we * always recreate everything? */ public class StructLink { ConstGraph constGraph; Graph graph; public StructLink(ConstGraph g) { if(g instanceof Graph) this.graph = (Graph)g; this.constGraph = g; } /** Copy the iterator into a set, then return an iterator * into the set. * XXX Generalize into utility routine */ private Iterator copyIterator(Iterator it) { Set s = new HashSet(); while(it.hasNext()) s.add(it.next()); return s.iterator(); } /** Remove all structlink associations of the given node. */ public void detach(Object node) { Iterator it = copyIterator( graph.findN_11X_Iter(node, STRUCTLINK.linkedTo)); while(it.hasNext()) { Object other = it.next(); detach(node, 1, other); } it = copyIterator( graph.findN_X11_Iter(STRUCTLINK.linkedTo, node)); while(it.hasNext()) { Object other = it.next(); detach(node, -1, other); } } /** Detach the two nodes. * Throws an error if not associated. */ public void detach(Object node1, int side, Object node2) { if(side > 0) detach(node1, node2); else detach(node2, node1); } /** Detach the two nodes (directional!). * Throws an error if not associated. * This is not symmetric: detach(n1, n2) is different from detach(n2, n1) */ public void detach(Object node1, Object node2) { graph.rm_111(node1, STRUCTLINK.linkedTo, node2); } /** Associate the given nodes. * A node cannot be associated to itself - will return if * this is tried. * If the nodes are already associated, does nothing. */ public void associate(Object node1, int side, Object node2) { if(side > 0) associate(node1, node2); else associate(node2, node1); } /** Associate the given nodes. * A node cannot be associated to itself - will return if * this is tried. * If the nodes are already associated, does nothing. */ public void associate(Object node1, Object node2) { if(node1 == node2) return; graph.add(node1, STRUCTLINK.linkedTo, node2); } /** Get an iterator over the associations of the given node. */ public Iterator getAssociations(Object node, int side) { if(side > 0) return constGraph.findN_11X_Iter(node, STRUCTLINK.linkedTo); else return constGraph.findN_X11_Iter(STRUCTLINK.linkedTo, node); } }