Skip to content
Open
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
3 changes: 3 additions & 0 deletions lib/grakn/answer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,8 @@ defmodule Grakn.Answer do
def unwrap({:schemaConcept_getLabel_res, %Session.SchemaConcept.GetLabel.Res{label: label}}),
do: label

def unwrap({:thing_isInferred_res, %Session.Thing.IsInferred.Res{inferred: inferred}}),
do: inferred

def unwrap(other), do: other
end
2 changes: 2 additions & 0 deletions lib/grakn/concept/action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Grakn.Concept.Action do
| :get_schema_concept
| :get_attribute_types
| :concept_label
| :is_inferred?
@type t :: %__MODULE__{name: name()}

defstruct [:name]
Expand All @@ -24,6 +25,7 @@ defmodule Grakn.Concept.Action do
def get_schema_concept, do: new(:get_schema_concept)
def get_attribute_types, do: new(:get_attribute_types)
def concept_label, do: new(:concept_label)
def is_inferred?, do: new(:is_inferred?)
end

defimpl DBConnection.Query, for: Grakn.Concept.Action do
Expand Down
13 changes: 12 additions & 1 deletion lib/grakn/concept/thing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,25 @@ defmodule Grakn.Concept.Thing do
@doc """
Get all attributes of the specified types associated with this instance
"""
@spec get_attributes(Concept.t(), [String.t()], DBConnection.t()) ::
@spec get_attributes(Concept.t(), [String.t()], DBConnection.t(), keyword()) ::
{:ok, any()} | {:error, any()}
def get_attributes(%{id: concept_id} = concept, attribute_types, conn, opts \\ []) do
with :ok <- assert_is_thing(concept) do
DBConnection.execute(conn, Action.attributes_by_type(), [concept_id, attribute_types], opts)
end
end

@doc """
Check if a given instance is inferred (i.e. implicit)
"""
@spec is_inferred?(Concept.t(), DBConnection.t(), keyword()) ::
{:ok, any()} | {:error, any()}
def is_inferred?(%{id: concept_id} = concept, conn, opts \\ []) do
with :ok <- assert_is_thing(concept) do
DBConnection.execute(conn, Action.is_inferred?(), [concept_id], opts)
end
end

defp assert_is_thing(concept) do
if Concept.is_thing(concept) do
:ok
Expand Down
8 changes: 8 additions & 0 deletions lib/grakn/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ defmodule Grakn.Transaction do
end
end

def is_inferred?(tx, concept_id) when is_binary(concept_id) do
send_request(tx, Request.is_inferred?(concept_id))

with {:ok, %{res: answer}} <- get_response(tx) do
{:ok, Grakn.Answer.unwrap(answer)}
end
end

def concept_label(tx, concept_id) when is_binary(concept_id) do
send_request(tx, Request.concept_label(concept_id))

Expand Down
4 changes: 4 additions & 0 deletions lib/grakn/transaction/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ defmodule Grakn.Transaction.Request do
concept_method_request(concept_id, :type_attributes_req, Session.Type.Attributes.Req.new())
end

def is_inferred?(concept_id) do
concept_method_request(concept_id, :thing_isInferred_req, Session.Thing.IsInferred.Req.new())
end

def concept_label(concept_id) do
concept_method_request(
concept_id,
Expand Down
39 changes: 39 additions & 0 deletions test/grakn/concept/thing_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,43 @@ defmodule Grakn.Concept.ThingTest do
assert "alex" === name
end
end

describe "is_inferred?/3" do
test "we can detect inferred atttributes", context do
assert {:ok, _} =
Grakn.transaction(
context[:conn],
fn conn ->
Grakn.query!(
conn,
Query.graql(
"insert $p isa person, has identifier \"1234\", has name \"alex\";"
)
)
end,
keyspace: @keyspace,
type: Grakn.Transaction.Type.write()
)

assert {:ok, true} ===
Grakn.transaction(
context[:conn],
fn conn ->
[%{"is_named" => is_named}] =
Grakn.query!(
conn,
Query.graql(
"match $p isa person, has identifier \"1234\"; $p has is_named $is_named; get;"
),
include_inferences: true
)

{:ok, value} = Concept.Thing.is_inferred?(is_named, conn)
value
end,
keyspace: @keyspace,
type: Grakn.Transaction.Type.write()
)
end
end
end
14 changes: 13 additions & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,20 @@ defmodule TestHelper do

defp define_base_schema(conn) do
Grakn.query!(conn, Query.graql("define name sub attribute, datatype string;"))
Grakn.query!(conn, Query.graql("define is_named sub attribute, datatype boolean;"))
Grakn.query!(conn, Query.graql("define identifier sub attribute, datatype string;"))
Grakn.query!(conn, Query.graql("define person sub entity, has name, has identifier;"))

Grakn.query!(
conn,
Query.graql("define person sub entity, has name, has is_named, has identifier;")
)

Grakn.query!(
conn,
Query.graql(
"define r1 sub rule, when { $p isa person; $p has name $name; }, then { $p has is_named true; };"
)
)
end
end

Expand Down