Get a resource with core API


#1

Hi,

If I have an instance with a resource, so in Graql

my-thing has-resource name

How would I get the name of my thing using Core API? What would be the best way to get multiple resources?

ciao


#2

Hi Katso,

Getting Resources When not Knowing the Entity:

There are there ways to get resources. The simplest via it’s ID (if you know the id):

<V> Resource<V> getResource(String id);

You can also do so by the literal value but this returns a collection of resources:

<V> Collection<Resource<V>> getResourcesByValue(V value);
//Example:
graph.getResourcesByValue("bob"); // Will get you all the resources with the value "bob"

Finally you can do so by the resource type. This will return a single resource because resources are unique to their type:

<V> Resource<V> getResource(V value, ResourceType<V> type);
//For Example:
graph.getResource("bob", graph.getResourceType("name")); //This will return a single resource with the value bob of type name

Getting Resources When Knowing The Entity

If you have an entity and want to get a resource of a particular type attached to that entity you can do so with:

Collection<Resource<?>> resources(ResourceType ... resourceTypes);
//Example
myThing.resources(graph.getResourceType("name"));

Getting From The Resource To The Entity

If you have a resource you can get the entitie(s) attached to it with:

resource.owner();
resource.ownerInstances();

Hope that helps,

Filipe