Created
July 24, 2012 08:12
-
-
Save ib84/3168761 to your computer and use it in GitHub Desktop.
Implicit conversions of HGHandle for improved usability of HyperGraphDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object HGHandleHacks { | |
implicit def richHandle(handle: HGHandle)(implicit graph: HyperGraph) = new { | |
// DEREFERENCING | |
// dereference and type cast - may throw ClassCastException | |
def d[T](implicit graph: HyperGraph): T = | |
graph.get(handle).asInstanceOf[T] | |
// typesafe dereferencing. Returns an Option[T], i.e. either Some(aT) or None | |
def ds[T](implicit graph: HyperGraph): Option[T] = | |
try { Option(graph.get(handle).asInstanceOf[T])} | |
catch { case _ => None } | |
// LINKING | |
// <-> linking operator | |
def <->(handle2: HGHandle*)(implicit graph: HyperGraph): HGHandle = | |
graph.add(new HGPlainLink(handle :: handle2.toList: _*)) | |
// new HGRel | |
def newRel(rel: String, h2: HGHandle*)(implicit graph: HyperGraph): HGHandle = | |
graph.add(new HGRel(rel, handle :: h2.toList: _*)) | |
// TYPES & TYPE BASED QUERYING | |
// get Class file of atom | |
def getType(implicit graph: HyperGraph): Class[_] = | |
graph.getTypeSystem.getClassForType(graph.getType(handle)) // what is Class<?> / Class[_] ? that's a type constructor no? | |
// return all atoms of same type | |
def typeAlikes(implicit graph: HyperGraph): mutable.Buffer[HGHandle] = | |
JavaConversions.asScalaBuffer(hg.findAll[HGHandle](graph, sameTypeQC)) //(ev.erasure)) | |
// shorthand query condition of same type | |
def sameTypeQC: HGQueryCondition = new AtomTypeCondition(getType) | |
// query only on atoms of same type | |
def queryOnSameType(queryCond: HGQueryCondition)(implicit g: HyperGraph): java.util.List[HGHandle] = | |
hg.findAll[HGHandle](g, hg.and(sameTypeQC, queryCond)) | |
// TRAVERSALS | |
// start traversal departing from handle, optionally override named default params | |
def traverse(onLink: HGAtomPredicate = null, | |
sibling: HGAtomPredicate = null, | |
prec: Boolean = true, suc: Boolean = true, | |
rev: Boolean = false, dfs: Boolean = true | |
)(implicit graph: HyperGraph): HGTraversal = | |
{ | |
val alGen: HGALGenerator = new DefaultALGenerator(graph, onLink, sibling, prec, suc, rev) | |
val trav: HGTraversal = if (dfs) new HGDepthFirstTraversal(handle, alGen) | |
else new HGBreadthFirstTraversal(handle, alGen) | |
trav | |
} | |
// parametrized traversal, traverses on Links of Type L and on siblings of Type A | |
def typeTraverse[L <: HGLink, A](prec: Boolean = true, suc: Boolean = true, | |
rev: Boolean = false, dfs: Boolean = true) | |
(implicit graph: HyperGraph, | |
evL: Manifest[L], evA: Manifest[A]): HGTraversal = | |
traverse( onLink = new AtomTypeCondition(evL.erasure), | |
sibling = new AtomTypeCondition(evA.erasure), | |
prec = prec, suc = suc, rev = rev, dfs = dfs)(graph) | |
// findShortestPath - between handle and given target handle. Various overridable named default parameters. | |
def findShortestPathTo(target:HGHandle, | |
algen:HGALGenerator = new DefaultALGenerator(graph, null, null), | |
weight:Mapping[HGHandle, java.lang.Double] = null, | |
distanceMatrix:java.util.Map[HGHandle, | |
java.lang.Double] = null, | |
precMatr: java.util.Map[HGHandle, HGHandle]=null) | |
:(Seq[HGHandle],Double) = { | |
val intDistMatrx = | |
if (distanceMatrix != null) distanceMatrix | |
else new util.LinkedHashMap[HGHandle, java.lang.Double](); | |
val pathLenght = | |
GraphClassics.dijkstra(handle, target, | |
algen, weight, intDistMatrx, precMatr) | |
val shortestPath = intDistMatrx.map{case (handle, double) => handle}.toSeq | |
(shortestPath, pathLenght) | |
} | |
// NEIGHBOURHOOD | |
// returns handles to all links pointing to this handle | |
def incidentLinks: java.util.List[HGHandle] = | |
hg.findAll[HGHandle](graph, hg.incident(handle)) | |
// returns siblings of each Link pointing to this handle | |
def neighbours = | |
incidentLinks.map(linkHandle => hg.findAll[HGHandle](graph, hg.target(linkHandle)). | |
filter(h => !h.equals(handle))) | |
// returns siblings flattend into a set | |
def neighbourSet = | |
incidentLinks.view.map(linkHandle => hg.findAll[HGHandle](graph, hg.target(linkHandle)). | |
filter(h => !h.equals(handle))). | |
flatten.toSet | |
// intersection of type brothers in the neighbourhood | |
def neighbourBrothers = neighbourSet.intersect(typeAlikes.toSet) | |
// MISC | |
// return an Option of HGLink referenced (or not) by handle. | |
def getLink: Option[HGLink] = ds[HGLink] | |
// define an unused handle | |
def define[T](obj:T)(implicit graph: HyperGraph) = | |
graph.define(handle, obj) | |
// replace atom of handle with obj of same type T as previous atom | |
def replace[T](obj:T)(implicit graph: HyperGraph) = | |
graph.replace(handle, obj) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment