Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package fr.inria.corese.core.next.api.base.model;

import fr.inria.corese.core.next.api.BNode;

/**
* Abstract implementation of the {@link BNode} interface, providing common functionality for blank node representations.
* A blank node (BNode) https://www.w3.org/TR/rdf12-concepts/#section-blank-nodes
*/
public abstract class AbstractBNode implements BNode {

/**
* Returns the string value of this blank node, which is its unique identifier.
* This method is an implementation of {@link BNode#stringValue()} and simply returns the result of {@link #getID()}.
*
* @return The string value of the blank node (its unique identifier).
*/
@Override
public String stringValue() {
return getID();
}

/**
* Checks whether this blank node is equal to another object.
* Two blank nodes are considered equal if they are the same object in memory or if they have the same unique identifier (ID).
* This method is an implementation of {@link BNode#equals(Object)}.
*
* @param o The object to compare this blank node to.
* @return {@code true} if the two blank nodes are the same object or have the same unique identifier; {@code false} otherwise.
*/
@Override
public boolean equals(Object o) {
return this == o || o instanceof BNode
&& getID().equals(((BNode) o).getID());
}

/**
* Returns the hash code for this blank node. The hash code is based on the unique identifier of the blank node,
* using the hash code of the ID returned by {@link #getID()}.
* This method is an implementation of {@link BNode#hashCode()}.
*
* @return The hash code for this blank node.
*/
@Override
public int hashCode() {
return getID().hashCode();
}

/**
* Returns a string representation of this blank node in the form "_:{ID}" where {ID} is the unique identifier of the blank node.
*
* @return A string representing this blank node, prefixed with "_:" and followed by its unique identifier.
*/
@Override
public String toString() {
return "_:" + getID();
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry that using java.util.concurrent.ThreadLocalRandom may cause problems when we implement multithreading. I suggest replacing the ID generation with a call of java.util.UUID.randomUUID().toString()

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class CoreseAdaptedValueFactory implements ValueFactory {

private static Logger logger = LoggerFactory.getLogger(CoreseAdaptedValueFactory.class);

private final String nodeID = java.util.UUID.randomUUID().toString();

public CoreseAdaptedValueFactory() {
}

Expand All @@ -34,12 +36,12 @@ public IRI createIRI(String namespace, String localName) {

@Override
public BNode createBNode() {
return null;
return new CoreseBNode(nodeID);
}

@Override
public BNode createBNode(String nodeID) {
return null;
return new CoreseBNode(nodeID);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package fr.inria.corese.core.next.impl.temp.literal;

import fr.inria.corese.core.next.api.base.model.AbstractBNode;
import fr.inria.corese.core.next.impl.exception.IncorrectOperationException;
import fr.inria.corese.core.sparql.api.IDatatype;

/**
* An implementation of a blank node (BNode) used by Corese.
* A blank node (BNode) represents an unnamed node in an RDF graph, typically used to represent resources that do not have a globally unique identifier (IRI).
* This class extends {@link AbstractBNode} and provides constructors for creating a BNode either from a Corese blank node object or a given string identifier.
*/
public class CoreseBNode extends AbstractBNode {
/**
* The Corese object representing the blank node in the old API.
*/
private final fr.inria.corese.core.sparql.datatype.CoreseBlankNode coreseObject;

/**
* The unique identifier (ID) for the blank node.
*/
private String id;

/**
* Constructs a {@link CoreseBNode} instance from an {@link IDatatype} Corese object.
* The Corese object should be an instance of {@link fr.inria.corese.core.sparql.datatype.CoreseBlankNode}.
*
* @param coreseObject The {@link IDatatype} Corese object representing the blank node.
* @throws IncorrectOperationException If the provided {@link IDatatype} is not a valid {@link fr.inria.corese.core.sparql.datatype.CoreseBlankNode}.
*/
public CoreseBNode(IDatatype coreseObject) {
if (coreseObject instanceof fr.inria.corese.core.sparql.datatype.CoreseBlankNode) {
this.coreseObject = ( fr.inria.corese.core.sparql.datatype.CoreseBlankNode) coreseObject;
this.id = this.coreseObject.getID();
}
else {
throw new IncorrectOperationException("Cannot create CoreseLiteral from a non-literal Corese object");
}
}

/**
* Constructs a {@link CoreseBNode} instance from a string identifier.
* This constructor creates a {@link fr.inria.corese.core.sparql.datatype.CoreseBlankNode} from the provided string id.
*
* @param id The unique identifier for the blank node.
*/
public CoreseBNode(String id) {
this(new fr.inria.corese.core.sparql.datatype.CoreseBlankNode(id));
this.id = id;
}

/**
* Returns the unique identifier of the blank node.
*
* @return The ID of the blank node.
*/
@Override
public String getID() {
return id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,5 @@ public boolean equals(Object obj) {
final CoreseBlankNode other = (CoreseBlankNode) obj;
return getLabel().equals(other.getLabel());
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.inria.corese.core.next.api.model;

import fr.inria.corese.core.next.api.BNode;
import fr.inria.corese.core.next.api.Literal;
import fr.inria.corese.core.next.api.ValueFactory;
import fr.inria.corese.core.next.impl.exception.IncorrectFormatException;
Expand Down Expand Up @@ -40,6 +41,14 @@ public void testCreateIRI() {

@Test
public void testCreateBNode() {
String nodeId = "corese123";
BNode nodesCorese123 = this.valueFactory.createBNode(nodeId);
BNode nodesCoreseRandom = this.valueFactory.createBNode();

assertNotNull(nodesCorese123);
assertNotNull(nodesCoreseRandom);
assertNotNull(nodesCoreseRandom.getID());
assertEquals(nodesCorese123.getID(), "corese123");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package fr.inria.corese.core.next.impl.temp.literal;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import fr.inria.corese.core.sparql.datatype.CoreseBlankNode;

public class CoreseBNodeTest {

private static final String BNODE_ID = "bnodeCorese123";
private static final CoreseBlankNode coreseBlankNode = new CoreseBlankNode(BNODE_ID);

private CoreseBNode coreseBNodeFromCoreseObject;
private CoreseBNode coreseBNodeFromStringId;

@Before
public void setUp() {
coreseBNodeFromCoreseObject = new CoreseBNode(coreseBlankNode);
coreseBNodeFromStringId = new CoreseBNode(BNODE_ID);
}

@Test
public void testConstructorFromString() {
// Test creating CoreseBnode for a string ID
assertNotNull(coreseBNodeFromStringId);
assertEquals(BNODE_ID, coreseBNodeFromStringId.getID());
}

@Test
public void testConstructorFromCoreseObject() {
// Test creating CoreseBnode for a CoreseBlankNode (old API)
assertNotNull(coreseBNodeFromCoreseObject);
assertEquals(BNODE_ID, coreseBNodeFromCoreseObject.getID());
}

@Test
public void testToString() {
// Test the toString method to ensure it outputs the correct representation
String expectedString = "_:" + BNODE_ID;
assertEquals(expectedString, coreseBNodeFromStringId.toString());
}
}
Loading