Skip to content

Commit e4e8135

Browse files
committed
adding blank node to corese new api
1 parent 0725198 commit e4e8135

3 files changed

Lines changed: 159 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package fr.inria.corese.core.next.api.base.model;
2+
3+
import fr.inria.corese.core.next.api.BNode;
4+
5+
/**
6+
* Abstract implementation of the {@link BNode} interface, providing common functionality for blank node representations.
7+
* A blank node (BNode) https://www.w3.org/TR/rdf12-concepts/#section-blank-nodes
8+
*/
9+
public abstract class AbstractBNode implements BNode {
10+
11+
/**
12+
* Returns the string value of this blank node, which is its unique identifier.
13+
* This method is an implementation of {@link BNode#stringValue()} and simply returns the result of {@link #getID()}.
14+
*
15+
* @return The string value of the blank node (its unique identifier).
16+
*/
17+
@Override
18+
public String stringValue() {
19+
return getID();
20+
}
21+
22+
/**
23+
* Checks whether this blank node is equal to another object.
24+
* Two blank nodes are considered equal if they are the same object in memory or if they have the same unique identifier (ID).
25+
* This method is an implementation of {@link BNode#equals(Object)}.
26+
*
27+
* @param o The object to compare this blank node to.
28+
* @return {@code true} if the two blank nodes are the same object or have the same unique identifier; {@code false} otherwise.
29+
*/
30+
@Override
31+
public boolean equals(Object o) {
32+
return this == o || o instanceof BNode
33+
&& getID().equals(((BNode) o).getID());
34+
}
35+
36+
/**
37+
* Returns the hash code for this blank node. The hash code is based on the unique identifier of the blank node,
38+
* using the hash code of the ID returned by {@link #getID()}.
39+
* This method is an implementation of {@link BNode#hashCode()}.
40+
*
41+
* @return The hash code for this blank node.
42+
*/
43+
@Override
44+
public int hashCode() {
45+
return getID().hashCode();
46+
}
47+
48+
/**
49+
* Returns a string representation of this blank node in the form "_:{ID}" where {ID} is the unique identifier of the blank node.
50+
*
51+
* @return A string representing this blank node, prefixed with "_:" and followed by its unique identifier.
52+
*/
53+
@Override
54+
public String toString() {
55+
return "_:" + getID();
56+
}
57+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package fr.inria.corese.core.next.impl.temp.literal;
2+
3+
import fr.inria.corese.core.next.api.base.model.AbstractBNode;
4+
import fr.inria.corese.core.next.impl.exception.IncorrectOperationException;
5+
import fr.inria.corese.core.sparql.api.IDatatype;
6+
7+
/**
8+
* An implementation of a blank node (BNode) used by Corese.
9+
* 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).
10+
* This class extends {@link AbstractBNode} and provides constructors for creating a BNode either from a Corese blank node object or a given string identifier.
11+
*/
12+
public class CoreseBNode extends AbstractBNode {
13+
/**
14+
* The Corese object representing the blank node in the old API.
15+
*/
16+
private final fr.inria.corese.core.sparql.datatype.CoreseBlankNode coreseObject;
17+
18+
/**
19+
* The unique identifier (ID) for the blank node.
20+
*/
21+
private String id;
22+
23+
/**
24+
* Constructs a {@link CoreseBNode} instance from an {@link IDatatype} Corese object.
25+
* The Corese object should be an instance of {@link fr.inria.corese.core.sparql.datatype.CoreseBlankNode}.
26+
*
27+
* @param coreseObject The {@link IDatatype} Corese object representing the blank node.
28+
* @throws IncorrectOperationException If the provided {@link IDatatype} is not a valid {@link fr.inria.corese.core.sparql.datatype.CoreseBlankNode}.
29+
*/
30+
public CoreseBNode(IDatatype coreseObject) {
31+
if (coreseObject instanceof fr.inria.corese.core.sparql.datatype.CoreseBlankNode) {
32+
this.coreseObject = ( fr.inria.corese.core.sparql.datatype.CoreseBlankNode) coreseObject;
33+
this.id = this.coreseObject.getID();
34+
}
35+
else {
36+
throw new IncorrectOperationException("Cannot create CoreseLiteral from a non-literal Corese object");
37+
}
38+
}
39+
40+
/**
41+
* Constructs a {@link CoreseBNode} instance from a string identifier.
42+
* This constructor creates a {@link fr.inria.corese.core.sparql.datatype.CoreseBlankNode} from the provided string id.
43+
*
44+
* @param id The unique identifier for the blank node.
45+
*/
46+
public CoreseBNode(String id) {
47+
this(new fr.inria.corese.core.sparql.datatype.CoreseBlankNode(id));
48+
this.id = id;
49+
}
50+
51+
/**
52+
* Returns the unique identifier of the blank node.
53+
*
54+
* @return The ID of the blank node.
55+
*/
56+
@Override
57+
public String getID() {
58+
return id;
59+
}
60+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package fr.inria.corese.core.next.impl.temp.literal;
2+
3+
import static org.junit.Assert.*;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import fr.inria.corese.core.sparql.datatype.CoreseBlankNode;
7+
8+
public class CoreseBNodeTest {
9+
10+
private static final String BNODE_ID = "bnodeCorese123";
11+
private static final CoreseBlankNode coreseBlankNode = new CoreseBlankNode(BNODE_ID);
12+
13+
private CoreseBNode coreseBNodeFromCoreseObject;
14+
private CoreseBNode coreseBNodeFromStringId;
15+
16+
@Before
17+
public void setUp() {
18+
coreseBNodeFromCoreseObject = new CoreseBNode(coreseBlankNode);
19+
coreseBNodeFromStringId = new CoreseBNode(BNODE_ID);
20+
}
21+
22+
@Test
23+
public void testConstructorFromString() {
24+
// Test creating CoreseBnode for a string ID
25+
assertNotNull(coreseBNodeFromStringId);
26+
assertEquals(BNODE_ID, coreseBNodeFromStringId.getID());
27+
}
28+
29+
@Test
30+
public void testConstructorFromCoreseObject() {
31+
// Test creating CoreseBnode for a CoreseBlankNode (old API)
32+
assertNotNull(coreseBNodeFromCoreseObject);
33+
assertEquals(BNODE_ID, coreseBNodeFromCoreseObject.getID());
34+
}
35+
36+
@Test
37+
public void testToString() {
38+
// Test the toString method to ensure it outputs the correct representation
39+
String expectedString = "_:" + BNODE_ID;
40+
assertEquals(expectedString, coreseBNodeFromStringId.toString());
41+
}
42+
}

0 commit comments

Comments
 (0)