/** * Returns the normalized serialization string of a graph, to be used * e.g. for signatures and encryptions. */ public static String serialize(Graph graph, CopyStrategy copyStrategy) { return serialize(graph.getRootContextNode(), copyStrategy); }
/** * Given a graph, finds and returns the XDI common root. * @param graph The graph. * @return The XDI common root. */ public static XdiCommonRoot findCommonRoot(Graph graph) { ContextNode commonRootContextNode = graph.getRootContextNode(false); return new XdiCommonRoot(commonRootContextNode); }
@Override /* * TODO: This is inefficient for a large number of context nodes in the graph. */ public ReadOnlyIterator<Relation> getIncomingRelations() { return new SelectingIterator<Relation> (this.getGraph().getRootContextNode(true).getAllRelations()) { @Override public boolean select(Relation relation) { return relation.follow().equals(AbstractContextNode.this); } }; }
/** * Given a graph, lists all link contract templates. * @param graph The graph. * @return An iterator over link contract templates. */ public static ReadOnlyIterator<LinkContractTemplate> getAllLinkContractTemplates(Graph graph) { ContextNode root = graph.getRootContextNode(true); Iterator<ContextNode> allContextNodes = root.getAllContextNodes(); return new MappingContextNodeLinkContractTemplateIterator(allContextNodes); }
public static XdiEntityCollection getEntityIndex(Graph graph, XDIArc indexXDIArc, boolean create) { XDIArc xdiEntityCollectionXDIArc = XdiEntityCollection.createXDIArc(indexXDIArc); ContextNode contextNode = create ? graph.getRootContextNode().setContextNode(xdiEntityCollectionXDIArc) : graph.getRootContextNode().getContextNode(xdiEntityCollectionXDIArc); if (contextNode == null) return null; return XdiEntityCollection.fromContextNode(contextNode); }
public static XdiAttributeCollection getAttributeIndex(Graph graph, XDIArc indexXDIArc, boolean create) { XDIArc xdiAttributeCollectionXDIArc = XdiAttributeCollection.createXDIArc(indexXDIArc); ContextNode contextNode = create ? graph.getRootContextNode().getContextNode(xdiAttributeCollectionXDIArc) : graph.getRootContextNode().getContextNode(xdiAttributeCollectionXDIArc); if (contextNode == null) return null; return XdiAttributeCollection.fromContextNode(contextNode); }
/** * Creates a clone of the given graph with the same contents. * @param graph The graph to clone. * @return The cloned graph. */ public static Graph cloneGraph(Graph graph) { Graph newGraph = graphFactory.openGraph(); CopyUtil.copyContextNode(graph.getRootContextNode(true), newGraph, null); return newGraph; }
@Override public XdiCommonRoot findCommonRoot() { return new XdiCommonRoot(this.getContextNode().getGraph().getRootContextNode(false)); }
@Override public Writer write(Graph graph, Writer writer) throws IOException { // write the statements JsonObject json = makeJson(graph.getRootContextNode(true), this.isWriteImplied()); JsonWriter jsonWriter = new JsonWriter(writer); if (this.isWritePretty()) jsonWriter.setIndent(" "); gson.toJson(json, jsonWriter); jsonWriter.flush(); jsonWriter.close(); writer.flush(); return writer; }
private void read(Graph graph, BufferedReader bufferedReader, State state) throws IOException, Xdi2ParseException { JsonElement jsonGraphElement = gson.getAdapter(JsonObject.class).fromJson(bufferedReader); if (! (jsonGraphElement instanceof JsonObject)) throw new Xdi2ParseException("JSON must be an object: " + jsonGraphElement); this.read(graph.getRootContextNode(), (JsonObject) jsonGraphElement, state); }
/** * Given a graph, lists all digests. * @param graph The graph. * @return An iterator over digests. */ public static Iterator<Digest> getAllDigests(Graph graph) { ContextNode root = graph.getRootContextNode(true); Iterator<ContextNode> allContextNodes = root.getAllContextNodes(); return new MappingXdiAttributeDigestIterator(new MappingContextNodeXdiAttributeIterator(allContextNodes)); }
/** * Given a graph, lists all signatures. * @param graph The graph. * @return An iterator over signatures. */ public static Iterator<Signature> getAllSignatures(Graph graph) { ContextNode root = graph.getRootContextNode(true); Iterator<ContextNode> allContextNodes = root.getAllContextNodes(); return new MappingXdiAttributeSignatureIterator(new MappingContextNodeXdiAttributeIterator(allContextNodes)); }
private static void assertEqualsGraphs(Graph graph1, Graph graph2) throws Exception { assertEquals(graph1.getRootContextNode().getAllStatementCount(), graph2.getRootContextNode().getAllStatementCount()); assertEquals(graph1.getRootContextNode().getAllContextNodeCount(), graph2.getRootContextNode().getAllContextNodeCount()); assertEquals(graph1.getRootContextNode().getAllRelationCount(), graph2.getRootContextNode().getAllRelationCount()); assertEquals(graph1.getRootContextNode().getAllLiteralCount(), graph2.getRootContextNode().getAllLiteralCount()); Iterator<Statement> s1 = graph1.getRootContextNode().getAllStatements(); Iterator<Statement> s2 = graph2.getRootContextNode().getAllStatements(); while (s1.hasNext()) { XDIStatement s = s1.next().getXDIStatement(); assertTrue(s.toString(), graph2.containsStatement(s)); } while (s2.hasNext()) { XDIStatement s = s2.next().getXDIStatement(); assertTrue(s.toString(), graph1.containsStatement(s)); } assertEquals(graph1, graph2); }
public void testReadJson() throws Exception { Graph graph3 = this.getGraphFactory().openGraph(this.getClass().getName() + "-graph-3"); XDIReader reader = XDIReaderRegistry.forFormat("XDI/JSON", null); reader.read(graph3, AbstractGraphTest.class.getResourceAsStream("test.json")).close(); testGraph(graph3); graph3.getRootContextNode().clear(); assertTrue(graph3.isEmpty()); graph3.close(); }
public void testDeleteCyclicRelation() throws Exception { Graph graph31 = this.getGraphFactory().openGraph(this.getClass().getName() + "-graph-31"); graph31.setStatement(XDIStatement.create("=a=b=c=d=e/+x/=a=b=c")); graph31.setStatement(XDIStatement.create("=m=n=o/+y/=a=b=c=d")); graph31.getDeepContextNode(XDIAddress.create("=a=b")).delete(); assertEquals(graph31.getRootContextNode().getAllContextNodeCount(), 4); assertEquals(graph31.getRootContextNode().getAllRelationCount(), 0); assertEquals(graph31.getRootContextNode().getAllStatementCount(), 4); graph31.close(); }
public void testTimestampsOnContextNode() throws Exception { Graph graph = MemoryGraphFactory.getInstance().openGraph(); ContextNode contextNode = graph.getRootContextNode().setContextNode(XDIArc.create("=markus")); GregorianCalendar calendar = new GregorianCalendar(2010, 11, 22, 11, 22, 33); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); Date timestamp = calendar.getTime(); Timestamps.setTimestamp(XdiAbstractContext.fromContextNode(contextNode), timestamp); assertEquals(timestamp, Timestamps.getTimestamp(XdiAbstractContext.fromContextNode(contextNode))); graph.close(); } }
private static void testGraphsEqual(Graph graph1, Graph graph2) throws Exception { ContextNode rootContextNode1 = graph1.getRootContextNode(); ContextNode rootContextNode2 = graph2.getRootContextNode(); assertTrue(rootContextNode1.getContextNodeCount() == rootContextNode2.getContextNodeCount()); assertTrue(rootContextNode1.getAllContextNodeCount() == rootContextNode2.getAllContextNodeCount()); assertTrue(rootContextNode1.getAllRelationCount() == rootContextNode2.getAllRelationCount()); assertTrue(rootContextNode1.getAllLiteralCount() == rootContextNode2.getAllLiteralCount()); } }
public void testDeleteDeep() throws Exception { Graph graph12 = this.getGraphFactory().openGraph(this.getClass().getName() + "-graph-12"); assertEquals(graph12.getRootContextNode(), graph12.getDeepContextNode(XDIConstants.XDI_ADD_ROOT)); assertEquals(graph12.getRootContextNode().getXDIAddress(), XDIConstants.XDI_ADD_ROOT); graph12.setDeepContextNode(XDIAddress.create("=markus")).setRelation(XDIAddress.create("#friend"), XDIAddress.create("=someone")); graph12.getDeepContextNode(XDIAddress.create("=markus")).delete(); graph12.setDeepContextNode(XDIAddress.create("=markus")); assertNull(graph12.getDeepContextNode(XDIAddress.create("=markus")).getRelation(XDIAddress.create("#friend"))); graph12.close(); }
public void testCommonRoot() throws Exception { Graph graph = MemoryGraphFactory.getInstance().openGraph(); assertEquals(XdiCommonRoot.findCommonRoot(graph).getContextNode(), graph.getRootContextNode()); assertEquals(XdiCommonRoot.findCommonRoot(graph).getContextNode().getXDIAddress(), XDIConstants.XDI_ADD_ROOT); graph.close(); } }
public void testNoExceptions() throws Exception { Graph graph17 = this.getGraphFactory().openGraph(this.getClass().getName() + "-graph-17"); graph17.setStatement(XDIStatement.create("=markus<#email>/&/\"Markus Sabadello\"")); graph17.setStatement(XDIStatement.create("=markus/#friend/=neustar=les")); ContextNode root = graph17.getRootContextNode(); ContextNode markus = graph17.getDeepContextNode(XDIAddress.create("=markus")); ContextNode les = graph17.getDeepContextNode(XDIAddress.create("=neustar=les")); root.setContextNode(XDIArc.create("=markus")); markus.setDeepContextNode(XDIAddress.create("<#email>")).setLiteralNode("test"); markus.setRelation(XDIAddress.create("#friend"), les); graph17.close(); }