Skip to content

Commit

Permalink
relations may use any valid target XDI address
Browse files Browse the repository at this point in the history
  • Loading branch information
peacekeeper committed Feb 24, 2017
1 parent b3ed403 commit 9ee6774
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 116 deletions.
13 changes: 6 additions & 7 deletions core/src/main/java/xdi2/core/impl/AbstractContextNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import xdi2.core.Statement;
import xdi2.core.Statement.ContextNodeStatement;
import xdi2.core.constants.XDIConstants;
import xdi2.core.constants.XDIDictionaryConstants;
import xdi2.core.exceptions.Xdi2GraphException;
import xdi2.core.features.nodetypes.XdiAbstractAttribute;
import xdi2.core.features.nodetypes.XdiAbstractContext;
Expand Down Expand Up @@ -813,8 +812,8 @@ protected void setContextNodeCheckValid(XDIArc XDIarc) throws Xdi2GraphException

if (XDIConstants.XDI_ADD_ROOT.toString().equals(XDIarc.toString())) throw new Xdi2GraphException("Invalid context node arc: " + XDIarc);

if (this.containsRelations(XDIDictionaryConstants.XDI_ADD_REF)) throw new Xdi2GraphException("Cannot add " + XDIarc + " context node to context node " + this.getXDIAddress() + " containing a " + XDIDictionaryConstants.XDI_ADD_REF + " relation.");
if (this.containsRelations(XDIDictionaryConstants.XDI_ADD_REP)) throw new Xdi2GraphException("Cannot add " + XDIarc + " context node to context node " + this.getXDIAddress() + " containing a " + XDIDictionaryConstants.XDI_ADD_REP + " relation.");
/* if (this.containsRelations(XDIDictionaryConstants.XDI_ADD_REF)) throw new Xdi2GraphException("Cannot add " + XDIarc + " context node to context node " + this.getXDIAddress() + " containing a " + XDIDictionaryConstants.XDI_ADD_REF + " relation.");
if (this.containsRelations(XDIDictionaryConstants.XDI_ADD_REP)) throw new Xdi2GraphException("Cannot add " + XDIarc + " context node to context node " + this.getXDIAddress() + " containing a " + XDIDictionaryConstants.XDI_ADD_REP + " relation.");*/

ContextNode tempContextNode = new DummyContextNode(this.getGraph(), this, XDIarc, null, null, null);
if (! XdiAbstractContext.isValid(tempContextNode)) throw new Xdi2GraphException("Invalid subgraph: " + XDIarc + " under subgraph " + this.getXDIAddress());
Expand All @@ -832,7 +831,7 @@ protected void setRelationCheckValid(XDIAddress XDIaddress, XDIAddress targetXDI
if (XDIConstants.STRING_CONTEXT.equals(XDIaddress.toString())) throw new Xdi2GraphException("Invalid relation arc: " + XDIaddress);
if (XDIConstants.XDI_ARC_LITERAL.equals(XDIaddress.toString())) throw new Xdi2GraphException("Invalid relation arc: " + XDIaddress);

if (! this.isEmpty()) {
/* if (! this.isEmpty()) {
if (XDIDictionaryConstants.XDI_ADD_REF.equals(XDIaddress)) {
Expand All @@ -849,7 +848,7 @@ protected void setRelationCheckValid(XDIAddress XDIaddress, XDIAddress targetXDI
if (this.containsRelations(XDIDictionaryConstants.XDI_ADD_REP)) throw new Xdi2GraphException("Cannot add " + XDIaddress + "/" + targetXDIAddress + " relation to context node " + this.getXDIAddress() + ", which already contains a $rep.");
}
}
}*/
}

/**
Expand All @@ -862,8 +861,8 @@ protected void setLiteralCheckValid(Object literalData) throws Xdi2GraphExceptio

if (! AbstractLiteralNode.isValidLiteralData(literalData)) throw new IllegalArgumentException("Invalid literal data: " + literalData.getClass().getSimpleName());

if (this.containsRelations(XDIDictionaryConstants.XDI_ADD_REF)) throw new Xdi2GraphException("Cannot add literal to context node " + this.getXDIAddress() + " containing a " + XDIDictionaryConstants.XDI_ADD_REF + " relation.");
if (this.containsRelations(XDIDictionaryConstants.XDI_ADD_REP)) throw new Xdi2GraphException("Cannot add literal to context node " + this.getXDIAddress() + " containing a " + XDIDictionaryConstants.XDI_ADD_REP + " relation.");
/* if (this.containsRelations(XDIDictionaryConstants.XDI_ADD_REF)) throw new Xdi2GraphException("Cannot add literal to context node " + this.getXDIAddress() + " containing a " + XDIDictionaryConstants.XDI_ADD_REF + " relation.");
if (this.containsRelations(XDIDictionaryConstants.XDI_ADD_REP)) throw new Xdi2GraphException("Cannot add literal to context node " + this.getXDIAddress() + " containing a " + XDIDictionaryConstants.XDI_ADD_REP + " relation.");*/
}

/**
Expand Down
76 changes: 0 additions & 76 deletions core/src/main/java/xdi2/core/util/GraphUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,82 +61,6 @@ public static boolean ownsPeerRootXDIArc(Graph graph, XDIArc peerRootXDIArc) {
return xdiRoot instanceof XdiCommonRoot;
}

public static ContextNode dereference(ContextNode contextNode, boolean subgraph, boolean reference, boolean replacement, boolean identity) {

ContextNode dereferencedContextNode = contextNode;

while (true) {

if (reference) {

ContextNode referenceContextNode = Equivalence.getReferenceContextNode(dereferencedContextNode);
if (referenceContextNode != null) { dereferencedContextNode = referenceContextNode; continue; }
}

if (replacement) {

ContextNode replacementContextNode = Equivalence.getReplacementContextNode(dereferencedContextNode);
if (replacementContextNode != null) { dereferencedContextNode = replacementContextNode; continue; }
}

if (identity) {

ContextNode identityContextNode = Equivalence.getIdentityContextNode(dereferencedContextNode);
if (identityContextNode != null) { dereferencedContextNode = identityContextNode; continue; }
}

break;
}

return dereferencedContextNode;
}

public static ContextNode dereference(ContextNode contextNode, boolean subgraph) {

return dereference(contextNode, subgraph, true, true, false);
}

public static ContextNode dereference(ContextNode contextNode, XDIAddress XDIaddress, boolean subgraph, boolean reference, boolean replacement, boolean identity) {

for (XDIArc XDIarc : XDIaddress.getXDIArcs()) {

if (XdiInnerRoot.isValidXDIArc(XDIarc)) {

XDIAddress subject = XdiInnerRoot.getSubjectOfInnerRootXDIArc(XDIarc);
XDIAddress predicate = XdiInnerRoot.getPredicateOfInnerRootXDIArc(XDIarc);

ContextNode subjectContextNode = dereference(contextNode, subject, subgraph, reference, replacement, identity);
XdiInnerRoot xdiInnerRoot = XdiAbstractContext.fromContextNode(subjectContextNode).getXdiInnerRoot(predicate, false);
if (xdiInnerRoot == null) return null;

contextNode = xdiInnerRoot.getContextNode();
} else {

contextNode = contextNode.getContextNode(XDIarc);
if (contextNode == null) return null;
}

contextNode = dereference(contextNode, subgraph, reference, replacement, identity);
}

return contextNode;
}

public static ContextNode dereference(ContextNode contextNode, XDIAddress XDIaddress, boolean subgraph) {

return dereference(contextNode, XDIaddress, subgraph, true, true, false);
}

public static ContextNode dereference(Graph graph, XDIAddress XDIaddress, boolean subgraph, boolean reference, boolean replacement, boolean identity) {

return dereference(graph.getRootContextNode(), XDIaddress, subgraph, reference, replacement, identity);
}

public static ContextNode dereference(Graph graph, XDIAddress XDIaddress, boolean subgraph) {

return dereference(graph, XDIaddress, subgraph, true, true, false);
}

/**
* Creates a graph in memory.
* @return A graph.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,14 +639,14 @@ public void testIllegalarcs() throws Exception {

Equivalence.setReferenceContextNode(markus, XDIAddress.create("=!:uuid:1234"));

try { markus.setContextNode(XDIArc.create("<#email>")); fail(); } catch (Xdi2GraphException ex) { }
try { markus.setRelation(XDIAddress.create("#friend"), XDIAddress.create("=animesh")); fail(); } catch (Xdi2GraphException ex) { }
try { markus.setLiteralNode("hello"); fail(); } catch (Xdi2GraphException ex) { }
// try { markus.setContextNode(XDIArc.create("<#email>")); fail(); } catch (Xdi2GraphException ex) { }
// try { markus.setRelation(XDIAddress.create("#friend"), XDIAddress.create("=animesh")); fail(); } catch (Xdi2GraphException ex) { }
// try { markus.setLiteralNode("hello"); fail(); } catch (Xdi2GraphException ex) { }

Equivalence.getReferenceContextNode(markus).delete();
markus.setRelation(XDIAddress.create("#friend"), XDIAddress.create("=animesh"));

try { Equivalence.setReferenceContextNode(markus, XDIAddress.create("=!:uuid:1234")); fail(); } catch (Xdi2GraphException ex) { }
// try { Equivalence.setReferenceContextNode(markus, XDIAddress.create("=!:uuid:1234")); fail(); } catch (Xdi2GraphException ex) { }

graph20.close();
}
Expand Down
17 changes: 0 additions & 17 deletions core/src/test/java/xdi2/tests/core/util/GraphUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,4 @@ public void testOwnsPeerRootXDIArc() throws Exception {

graph.close();
}

public void testDereference() throws Exception {

Graph graph = MemoryGraphFactory.getInstance().parseGraph(TEST_GRAPH_DEREFERENCE);

ContextNode contextNode1111 = graph.getDeepContextNode(TEST_CONTEXTNODE_1111);
ContextNode contextNode1a1a = graph.getDeepContextNode(TEST_CONTEXTNODE_1a1a);

assertTrue(GraphUtil.dereference(contextNode1111, false).isRootContextNode());
assertTrue(GraphUtil.dereference(contextNode1a1a, false).isRootContextNode());
assertNotNull(graph.getDeepContextNode(TEST_CONTEXTNODE_1111_2222_CONTRACT));
assertNull(graph.getDeepContextNode(TEST_CONTEXTNODE_1a1a_2222_CONTRACT));
assertNotNull(GraphUtil.dereference(graph, TEST_CONTEXTNODE_1111_2222_CONTRACT, false));
assertNotNull(GraphUtil.dereference(graph, TEST_CONTEXTNODE_1a1a_2222_CONTRACT, false));

graph.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import xdi2.core.ContextNode;
import xdi2.core.Graph;
import xdi2.core.exceptions.Xdi2RuntimeException;
import xdi2.core.features.nodetypes.XdiAbstractContext;
import xdi2.core.features.nodetypes.XdiContext;
import xdi2.core.features.nodetypes.XdiPeerRoot;
import xdi2.core.syntax.XDIAddress;
import xdi2.core.syntax.XDIArc;
import xdi2.core.syntax.XDIStatement;
import xdi2.core.util.GraphUtil;
import xdi2.messaging.Message;
import xdi2.messaging.MessageEnvelope;
import xdi2.messaging.constants.XDIMessagingConstants;
import xdi2.messaging.container.AddressHandler;
import xdi2.messaging.container.MessagingContainer;
import xdi2.messaging.container.Prototype;
Expand All @@ -20,6 +26,9 @@
import xdi2.messaging.container.execution.ExecutionContext;
import xdi2.messaging.container.execution.ExecutionResult;
import xdi2.messaging.container.impl.AbstractMessagingContainer;
import xdi2.messaging.container.interceptor.impl.HasInterceptor;
import xdi2.messaging.container.interceptor.impl.RefInterceptor;
import xdi2.messaging.operations.Operation;

/**
* An XDI messaging container backed by some implementation of the Graph interface.
Expand Down Expand Up @@ -129,6 +138,10 @@ public StatementHandler getStatementHandler(XDIStatement statement) throws Xdi2M
return this.graphContextHandler;
}

/*
* Prototype
*/

@Override
public GraphMessagingContainer instanceFor(PrototypingContext prototypingContext) throws Xdi2MessagingException {

Expand Down Expand Up @@ -158,6 +171,50 @@ public GraphMessagingContainer instanceFor(PrototypingContext prototypingContext
return (GraphMessagingContainer) messagingContainer;
}

/*
* Convenience methods
*/

public static ContextNode get(Graph graph, XDIAddress XDIaddress) {

GraphMessagingContainer graphMessagingContainer = new GraphMessagingContainer(graph);
graphMessagingContainer.getInterceptors().addInterceptor(new RefInterceptor());
graphMessagingContainer.getInterceptors().addInterceptor(new HasInterceptor());

MessageEnvelope messageEnvelope = new MessageEnvelope();
Message message = messageEnvelope.createMessage();
Operation operation = message.createGetOperation(XDIaddress);
operation.setParameter(XDIMessagingConstants.XDI_ADD_OPERATION_PARAMETER_DEREF, Boolean.TRUE);
operation.setParameter(XDIMessagingConstants.XDI_ADD_OPERATION_PARAMETER_DEHAS, Boolean.TRUE);

ExecutionContext executionContext = ExecutionContext.createExecutionContext();
ExecutionResult executionResult = ExecutionResult.createExecutionResult(messageEnvelope);

try {

graphMessagingContainer.execute(message.getMessageEnvelope(), executionContext, executionResult);
} catch (Xdi2MessagingException ex) {

throw new Xdi2RuntimeException(ex.getMessage(), ex);
}

Graph resultGraph = executionResult.makeLightMessagingResponse().getResultGraph();
return resultGraph.getDeepContextNode(XDIaddress);
}

@SuppressWarnings("unchecked")
public static <T extends XdiContext<T>> XdiContext<T> getXdiContext(Graph graph, XDIAddress XDIaddress) {

ContextNode contextNode = get(graph, XDIaddress);
if (contextNode == null) return null;

return (XdiContext<T>) XdiAbstractContext.fromContextNode(contextNode);
}

/*
* Getters and setters
*/

public Graph getGraph() {

return this.graph;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private void processConnect(LinkContractTemplate linkContractTemplate, ConnectOp
Map<XDIArc, Object> variableValues = operation.getVariableValues();

variableValues.put(XDILinkContractConstants.XDI_ARC_V_FROM, operation.getMessage().getSenderXDIAddress());
variableValues.put(XDILinkContractConstants.XDI_ARC_V_TO, operation.getMessage().getToXDIAddress());
variableValues.put(XDILinkContractConstants.XDI_ARC_V_TO, executionContext.getCurrentMessagingContainer().getOwnerXDIAddress());

// instantiate link contract

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@
import xdi2.core.features.linkcontracts.instance.LinkContract;
import xdi2.core.features.linkcontracts.instantiation.LinkContractInstantiation;
import xdi2.core.features.linkcontracts.template.LinkContractTemplate;
import xdi2.core.features.nodetypes.XdiCommonRoot;
import xdi2.core.features.nodetypes.XdiEntityCollection;
import xdi2.core.features.nodetypes.XdiEntityInstanceUnordered;
import xdi2.core.features.nodetypes.XdiInnerRoot;
import xdi2.core.syntax.XDIAddress;
import xdi2.core.syntax.XDIArc;
import xdi2.core.syntax.XDIStatement;
import xdi2.core.util.CopyUtil;
import xdi2.core.util.XDIAddressUtil;
import xdi2.core.util.CopyUtil.ConcatXDIAddressCopyStrategy;
import xdi2.core.util.iterators.IterableIterator;
import xdi2.messaging.Message;
import xdi2.messaging.MessageEnvelope;
Expand All @@ -39,7 +35,6 @@
import xdi2.messaging.container.interceptor.MessageEnvelopeInterceptor;
import xdi2.messaging.container.interceptor.impl.AbstractInterceptor;
import xdi2.messaging.operations.Operation;
import xdi2.messaging.util.MessagingCloneUtil;

/**
* This interceptor can add defer results to a messaging container and execution result.
Expand Down Expand Up @@ -107,11 +102,9 @@ public InterceptorResult after(MessageEnvelope messageEnvelope, ExecutionContext

if (this.getTargetGraph(executionContext) != null) {

XdiInnerRoot xdiInnerRoot = XdiCommonRoot.findCommonRoot(this.getTargetGraph(executionContext)).getInnerRoot(message.getFromXDIAddress(), messagingContainer.getOwnerXDIAddress(), true);

CopyUtil.copyGraph(MessagingCloneUtil.cloneMessage(message, false).getMessageEnvelope().getGraph(), this.getTargetGraph(executionContext), new ConcatXDIAddressCopyStrategy(xdiInnerRoot.getContextNode().getXDIAddress()));
CopyUtil.copyGraph(message.getMessageEnvelope().getGraph(), this.getTargetGraph(executionContext), null);
XdiEntityCollection xdiMessageIndex = Index.getEntityIndex(this.getTargetGraph(executionContext), XDIMessagingConstants.XDI_ARC_MSG, true);
Index.setEntityIndexAggregation(xdiMessageIndex, XDIAddressUtil.concatXDIAddresses(xdiInnerRoot.getContextNode().getXDIAddress(), message.getXdiEntity().getXDIAddress()));
Index.setEntityIndexAggregation(xdiMessageIndex, message.getXdiEntity().getXDIAddress());
}

// create a deferred push link contract?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import xdi2.core.features.policy.evaluation.PolicyEvaluationContext;
import xdi2.core.syntax.XDIAddress;
import xdi2.core.syntax.XDIStatement;
import xdi2.core.util.GraphUtil;
import xdi2.core.util.XDIAddressUtil;
import xdi2.core.util.iterators.CompositeIterator;
import xdi2.core.util.iterators.IterableIterator;
Expand All @@ -32,6 +31,7 @@
import xdi2.messaging.container.execution.ExecutionContext;
import xdi2.messaging.container.execution.ExecutionResult;
import xdi2.messaging.container.impl.AbstractMessagingContainer;
import xdi2.messaging.container.impl.graph.GraphMessagingContainer;
import xdi2.messaging.container.interceptor.InterceptorResult;
import xdi2.messaging.container.interceptor.MessageInterceptor;
import xdi2.messaging.container.interceptor.OperationInterceptor;
Expand Down Expand Up @@ -96,7 +96,7 @@ public InterceptorResult before(Message message, ExecutionContext executionConte
return InterceptorResult.DEFAULT;
}

ContextNode linkContractContextNode = GraphUtil.dereference(this.getLinkContractsGraph(executionContext), linkContractXDIAddress, true);
ContextNode linkContractContextNode = GraphMessagingContainer.get(this.getLinkContractsGraph(executionContext), linkContractXDIAddress);
XdiEntity xdiEntity = linkContractContextNode == null ? null : XdiAbstractEntity.fromContextNode(linkContractContextNode);
LinkContract linkContract = xdiEntity == null ? null : LinkContract.fromXdiEntity(xdiEntity);

Expand Down

0 comments on commit 9ee6774

Please sign in to comment.