lang = $a['lang']; parent::__construct($a, $caller); } function ARC2_ExhibitJSONSerializerPlugin($a = '', &$caller) { $this->__construct($a, $caller); } function __init() { parent::__init(); if(isset($this->a['labelPreds'])) { foreach ( $this->a['labelPreds'] as $i=>$pred ) { $this->a['labelPreds'][$i] = $this->expandPName($pred); } } if(isset($this->a['linkPreds'])) { foreach ( $this->a['linkPreds'] as $i=>$pred ) { $this->a['linkPreds'][$i] = $this->expandPName($pred); } } if(isset($this->a['imagePreds'])) { foreach ( $this->a['imagePreds'] as $i=>$pred ) { $this->a['imagePreds'][$i] = $this->expandPName($pred); } } } /* * Serialize the index * */ public function serializeIndex($index) { $this->buildGGDOM(); $this->doneNodes = array(); $this->index = $index; foreach($index as $uri=>$preds) { $this->buildNodeFromSubject(array($uri=>$preds)); foreach($preds as $pred=>$objects) { if ( ( (isset($this->a['labelPreds'])) && ( ! in_array($pred , $this->a['labelPreds'] ) ) ) && ( (isset($this->a['linkPreds'])) && ( ! in_array($pred , $this->a['linkPreds'] ) ) ) && ( (isset($this->a['imagePreds'])) && ( ! in_array($pred , $this->a['imagePreds'] ) ) ) ) { foreach($objects as $object) { $this->buildNodeFromObject($object); $this->buildEdge($uri, $pred, $object); } } } } return $this->graphDOM->saveXML(); } /* * Figure out the GG node type */ public function ggNodeType($resource) { $nodeType = 'CircleTextNode'; $resNS = $this->getPrefix($this->getURI($resource)); $resTypes = $resource['http://www.w3.org/1999/02/22-rdf-syntax-ns#']; if(isset($this->a['nsNodeTypeMap'])) { if(isset($this->a['nsNodeTypeMap'][$resNS])) $color = $this->a['nsNodeTypeMap'][$resNS]; if(isset($this->a['nsNodeTypeMap'][$this->getPrefix($resNS)])) $color = $this->a['nsNodeTypeMap'][$this->getPrefix($resNS)]; } if($resTypes && isset($this->a['typeNodeTypeMap'])) { foreach($resTypes as $type) { if(isset($this->a['typeNodeTypeMap'][$type])) $color = $this->a['typeNodeTypeMap'][$type]; if(isset($this->a['typeNodeTypeMap'][$this->getPName($type)])) $color = $this->a['typeNodeTypeMap'][$this->getPName($type)]; } } return $nodeType; } /* * Figure out the GG node color */ public function ggNodeColor($resource) { $color = '0000ff'; $resNS = $this->getPrefix($this->getURI($resource)); $resTypes = $resource['http://www.w3.org/1999/02/22-rdf-syntax-ns#']; if(isset($this->a['nsColorMap'])) { if(isset($this->a['nsColorMap'][$resNS])) $color = $this->a['nsColorMap'][$resNS]; if(isset($this->a['nsColorMap'][$this->getPrefix($resNS)])) $color = $this->a['nsColorMap'][$this->getPrefix($resNS)]; } if($resTypes && isset($this->a['typeColorMap'])) { foreach($resTypes as $type) { if(isset($this->a['typeColorMap'][$type])) $color = $this->a['typeColorMap'][$type]; if(isset($this->a['typeColorMap'][$this->getPName($type)])) $color = $this->a['typeColorMap'][$this->getPName($type)]; } } return $color; } public function ggNodeTextColor($resource) { $color = '000000'; $resNS = $this->getPrefix($this->getURI($resource)); $resTypes = $resource['http://www.w3.org/1999/02/22-rdf-syntax-ns#']; if(isset($this->a['nsNodeTextColorMap'])) { if(isset($this->a['nsNodeTextColorMap'][$resNS])) $color = $this->a['nsNodeTextColorMap'][$resNS]; if(isset($this->a['nsNodeTextColorMap'][$this->getPrefix($resNS)])) return $this->a['nsNodeTextColorMap'][$this->getPrefix($resNS)]; } if($resTypes && isset($this->a['typeNodeTextColorMap'])) { foreach($resTypes as $type) { if(isset($this->a['typeNodeTextColorMap'][$type])) $color = $this->a['typeNodeTextColorMap'][$type]; if(isset($this->a['typeNodeTextColorMap'][$this->getPName($type)])) return $this->a['typeNodeTextColorMap'][$this->getPName($type)]; } } return $color; } public function ggPredTextColor($pred) { $predNS = $this->getNS($pred); $color = '000000'; if(isset($this->a['nsPredTextColorMap'])) { if(isset($this->a['nsPredTextColorMap'][$predNS])) $color = $this->a['nsPredTextColorMap'][$predNS]; if(isset($this->a['nsPredTextColorMap'][$this->getPrefix($predNS)])) $color = $this->a['nsPredTextColorMap'][$this->getPrefix($predNS)]; } if(isset($this->a['predPredTextColorMap'])) { if(isset($this->a['predPredTextColorMap'][$pred])) $color = $this->a['predPredTextColorMap'][$pred]; if(isset($this->a['predPredTextColorMap'][$this->getPName($pred)])) $color = $this->a['predPredTextColorMap'][$this->getPName($pred)]; } return $color; } public function ggNodeImage($resource) { if ( ! is_array($resource) ) { $r = $this->getURIRef($resource); if($r) $resource = $r; } if( is_array($resource) && isset($this->a['imagePreds']) ) { $uri = $this->getURI($resource); foreach($this->a['imagePreds'] as $imagePred) { if(isset($resource[$uri][$imagePred])) { return ($resource[$uri][$imagePred][0]['value']); } } } if(isset($this->a['nodeImage'])) { return $this->a['nodeImage']; } return ''; } /* * Produce an appropriate link for the node. */ public function ggNodeLink($resource) { if ( ! is_array($resource) ) { $r = $this->getURIRef($resource); if($r) $resource = $r; } if( is_array($resource) && isset($this->a['linkPreds']) ) { $uri = $this->getURI($resource); foreach($this->a['linkPreds'] as $linkPred) { if( isset($resource[$uri]) && isset($resource[$uri][$linkPred])) { return ($resource[$uri][$linkPred][0]['value']); } } } return ''; } public function ggScale($resource) { if(isset($this->a['scale'])) return $this->a['scale']; return ''; } public function ggLiteralNodeTextColor() { if(isset($this->a['literalNodeTextColor'])) { return $this->a['literalNodeTextColor']; } return '00ff00'; } public function ggLiteralNodeColor() { if(isset($this->a['literalNodeColor'])) { return $this->a['literalNodeColor']; } return 'ffffff'; } public function ggLiteralNodeType() { if(isset($this->a['literalNodeType'])) { return $this->a['literalNodeType']; } return 'SquareNode'; } public function ggLiteralNodeImage() { if(isset($this->a['literalNodeImage'])) { return $this->a['literalNodeImage']; } return ''; } public function getID($resource) { $keys = array_keys($resource); return sha1($keys[0] ); } public function buildGGDOM() { $this->graphDOM = new DOMDocument(); $root = $this->graphDOM->createElement('graph'); $root->setAttribute('title', $this->a['title']); $root->setAttribute('type', $this->a['type']); $root->setAttribute('bgcolor', $this->a['bgcolor']); $root->setAttribute('linecolor', $this->a['linecolor']); $root->setAttribute('viewmode', $this->a['viewmode']); $root->setAttribute('width', $this->a['width']); $root->setAttribute('height', $this->a['height']); $root->setAttribute('animation', $this->a['animation']); $root->setAttribute('hidelabel', $this->a['hidelabel']); $root->setAttribute('lock', $this->a['lock']); $root->setAttribute('action', $this->a['action']); $this->graphDOM->appendChild($root); } /* * Look for an appropriate label */ function getLabel($resource) { if ( ! is_array($resource) ) { $r = $this->getURIRef($resource); if($r) $resource = $r; } if( is_array($resource) ) { $uri = $this->getURI($resource); foreach($this->a['labelPreds'] as $labelPred) { if(isset($resource[$uri][$labelPred])) { return ($resource[$uri][$labelPred][0]['value']); } if(isset($resource[$uri][$this->expandPName($labelPred)])) { return ($resource[$uri][$this->expandPName($labelPred)][0]['value']); } } $keys = array_keys($resource); return $this->getPName($keys[0]) ? $this->getPName($keys[0]) : $keys[0] ; } else { return $this->getPName($resource); } } function buildNodeFromSubject($resource) { $id = $this->getID($resource); if (! in_array($id, $this->doneNodes)) { $node = $this->graphDOM->createElement('node'); $node->setAttribute('id', $id ); $node->setAttribute('color', $this->ggNodeColor($resource) ); $node->setAttribute('link', $this->ggNodeLink($resource)); $node->setAttribute('type', $this->ggNodeType($resource)); $node->setAttribute('text', $this->getLabel($resource)); $node->setAttribute('textcolor', $this->ggNodeTextColor($resource)); $node->setAttribute('image', $this->ggNodeImage($resource)); $node->setAttribute('scale', $this->ggScale($resource)); $this->graphDOM->documentElement->appendChild($node); $this->doneNodes[] = $id; } } function buildNodeFromObject($object) { if($object['type'] == 'literal') { $oNode = $this->graphDOM->createElement('node'); $oNode->setAttribute('id', sha1($object['value']) ); $oNode->setAttribute('color', $this->ggLiteralNodeColor() ); $oNode->setAttribute('type', $this->ggLiteralNodeType()); $oNode->setAttribute('text', $object['value']); $oNode->setAttribute('scale', $this->ggScale($object)); $oNode->setAttribute('textcolor', $this->ggLiteralNodeTextColor()); $oNode->setAttribute('image', $this->ggLiteralNodeImage($object)); $this->graphDOM->documentElement->appendChild($oNode); } else { $resource = $this->getURIRef($object['value']); if($resource) { $this->buildNodeFromSubject($resource); } else { $id = sha1($object['value']); if (! in_array($id, $this->doneNodes)) { $oNode = $this->graphDOM->createElement('node'); $oNode->setAttribute('text', 'Missing Data'); $this->doneNodes[] = $id; $oNode->setAttribute('id', $id ); $oNode->setAttribute('type', $this->ggNodeType($object['value'])); $oNode->setAttribute('textcolor', 'ff0000'); $oNode->setAttribute('color', 'ff0000'); $oNode->setAttribute('scale', $this->ggScale($object)); $oNode->setAttribute('image', $this->ggNodeImage($object)); $oNode->setAttribute('link', $this->ggNodeLink($object)); $this->graphDOM->documentElement->appendChild($oNode); } } } } function buildEdge($sURI, $pred, $object) { $edge = $this->graphDOM->createElement('edge'); $edge->setAttribute('label', $this->getLabel($pred)); $edge->setAttribute('textcolor', $this->ggPredTextColor($pred)); $edge->setAttribute('sourceNode', sha1($sURI)); $edge->setAttribute('targetNode', sha1($object['value']) ); $this->graphDOM->documentElement->appendChild($edge); } /* * From an object's URI, find the resource as a subject */ function getURIRef($uri) { if(isset($this->index[$uri])) { return array($uri=>$this->index[$uri]); } else { return false; } } function getURI($resource) { if(is_array($resource)) { if(isset($resource['value']) ) { if ($resource['type'] == 'uri') { return $resource['value']; } } else { $keys = array_keys($resource); return $keys[0]; } } else { return $resource; } } //manipulating ARC2's getPrefix function getNS($uri) { if (preg_match('/^(.*[\/\#])([a-z\_][a-z0-9\-\_]*)$/i', $uri, $m)) { return $m[1]; //return $this->getPrefix($m[1]) . ':' . $m[2]; } return 0; } } ?>