diff --git a/src/main/java/fr/inria/corese/core/next/api/base/model/AbstractBNode.java b/src/main/java/fr/inria/corese/core/next/api/base/model/AbstractBNode.java new file mode 100644 index 000000000..069fce952 --- /dev/null +++ b/src/main/java/fr/inria/corese/core/next/api/base/model/AbstractBNode.java @@ -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(); + } +} \ No newline at end of file diff --git a/src/main/java/fr/inria/corese/core/next/impl/temp/CoreseAdaptedValueFactory.java b/src/main/java/fr/inria/corese/core/next/impl/temp/CoreseAdaptedValueFactory.java index d08cd8bad..266549182 100644 --- a/src/main/java/fr/inria/corese/core/next/impl/temp/CoreseAdaptedValueFactory.java +++ b/src/main/java/fr/inria/corese/core/next/impl/temp/CoreseAdaptedValueFactory.java @@ -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() { } @@ -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 diff --git a/src/main/java/fr/inria/corese/core/next/impl/temp/literal/CoreseBNode.java b/src/main/java/fr/inria/corese/core/next/impl/temp/literal/CoreseBNode.java new file mode 100644 index 000000000..bbadd0b57 --- /dev/null +++ b/src/main/java/fr/inria/corese/core/next/impl/temp/literal/CoreseBNode.java @@ -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; + } +} diff --git a/src/main/java/fr/inria/corese/core/sparql/datatype/CoreseBlankNode.java b/src/main/java/fr/inria/corese/core/sparql/datatype/CoreseBlankNode.java index dc4b6318a..8aef9e710 100755 --- a/src/main/java/fr/inria/corese/core/sparql/datatype/CoreseBlankNode.java +++ b/src/main/java/fr/inria/corese/core/sparql/datatype/CoreseBlankNode.java @@ -125,5 +125,5 @@ public boolean equals(Object obj) { final CoreseBlankNode other = (CoreseBlankNode) obj; return getLabel().equals(other.getLabel()); } - -} + +} \ No newline at end of file diff --git a/src/test/java/fr/inria/corese/core/next/api/model/ValueFactoryTest.java b/src/test/java/fr/inria/corese/core/next/api/model/ValueFactoryTest.java index 9cd84e924..4a903dbd6 100644 --- a/src/test/java/fr/inria/corese/core/next/api/model/ValueFactoryTest.java +++ b/src/test/java/fr/inria/corese/core/next/api/model/ValueFactoryTest.java @@ -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; @@ -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 diff --git a/src/test/java/fr/inria/corese/core/next/impl/temp/literal/CoreseBNodeTest.java b/src/test/java/fr/inria/corese/core/next/impl/temp/literal/CoreseBNodeTest.java new file mode 100644 index 000000000..0893faba0 --- /dev/null +++ b/src/test/java/fr/inria/corese/core/next/impl/temp/literal/CoreseBNodeTest.java @@ -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()); + } +}