Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
aeva
graph
Commits
4ad6c3d8
Commit
4ad6c3d8
authored
Mar 19, 2022
by
David Thompson
Browse files
WIP: Serialize arc data.
parent
d35bbcf9
Changes
2
Hide whitespace changes
Inline
Side-by-side
smtk/markup/json/jsonResource.cxx
View file @
4ad6c3d8
...
...
@@ -2,22 +2,124 @@
#include
"smtk/markup/Component.h"
#include
"smtk/markup/Resource.h"
#include
"smtk/markup/arcs/Groups.h"
#include
"smtk/markup/arcs/Labels.h"
#include
"smtk/markup/detail/NodeContainer.h"
#include
"smtk/markup/json/Helper.h"
#include
"smtk/markup/json/jsonComponent.h"
#include
"smtk/markup/json/jsonDomainMap.h"
#include
"smtk/common/json/jsonTypeMap.h"
#include
"smtk/resource/json/jsonResource.h"
#include
"smtk/graph/ArcMap.h"
#include
"smtk/string/Manager.h"
#include
"smtk/string/Token.h"
#include
"smtk/string/json/jsonManager.h"
#include
"smtk/string/json/jsonToken.h"
#if 0
namespace nlohmann
{
template<typename Src, typename Dst, typename ArcType>
struct adl_serializer<smtk::graph::Arcs<Src, Dst, ArcType>>
{
static smtk::graph::Arcs<Src, Dst, ArcType> from_json(const json& j)
{
auto resource = smtk::markup::json::Helper::instance().resource();
auto fromNode = std::dynamic_pointer_cast<Src>(
resource->find(j.at("from").get<smtk::common::UUID>()));
smtk::graph::Arcs<Src, Dst, ArcType> result(fromNode);
for (const auto& jTo : j.at("to"))
{
auto toNode = std::dynamic_pointer_cast<Dst>(resource->find(jTo.get<smtk::common::UUID>()));
if (toNode)
{
result.to().insert(toNode);
}
}
return result;
}
static void to_json(json& j, const smtk::graph::Arcs<Src, Dst, ArcType>& arcs)
{
j["from"] = arcs.from().id();
std::vector<smtk::common::UUID> toIds;
toIds.reserve(arcs.to().size());
for (const auto& to : arcs.to())
{
toIds.push_back(to);
}
j["to"] = toIds;
}
};
} // namespace nlohmann
#endif
namespace
smtk
{
namespace
markup
{
template
<
typename
Src
,
typename
Dst
,
typename
ArcType
>
void
to_json
(
nlohmann
::
json
&
j
,
const
smtk
::
graph
::
Arc
<
Src
,
Dst
,
ArcType
>&
arc
)
{
j
[
"from"
]
=
arc
.
from
().
id
();
j
[
"to"
]
=
arc
.
to
().
id
();
}
template
<
typename
Src
,
typename
Dst
,
typename
ArcType
>
void
to_json
(
nlohmann
::
json
&
j
,
const
smtk
::
graph
::
Arcs
<
Src
,
Dst
,
ArcType
>&
arcs
)
{
j
[
"from"
]
=
arcs
.
from
().
id
();
auto
&
dest
=
j
[
"to"
];
for
(
const
auto
&
node
:
arcs
.
to
())
{
dest
.
push_back
(
node
.
get
().
id
());
}
}
template
<
std
::
size_t
I
,
typename
Tuple
,
typename
ResourceType
>
inline
typename
std
::
enable_if
<
I
!=
std
::
tuple_size
<
Tuple
>::
value
,
void
>::
type
serializeArcsOfType
(
const
std
::
shared_ptr
<
ResourceType
>&
resource
,
nlohmann
::
json
&
arcs
)
{
using
ArcType
=
typename
std
::
tuple_element
<
I
,
Tuple
>::
type
;
auto
arcType
=
smtk
::
common
::
typeName
<
ArcType
>
();
try
{
const
auto
&
arcEntry
(
resource
->
arcs
().
template
get
<
ArcType
>());
auto
jarr
=
nlohmann
::
json
::
array
();
for
(
const
auto
&
entry
:
arcEntry
.
data
())
{
nlohmann
::
json
j
=
entry
.
second
;
jarr
.
push_back
(
j
);
}
arcs
[
arcType
]
=
jarr
;
}
catch
(
std
::
domain_error
&
)
{
// No entry, so do nothing.
}
// Now process the next entry in the tuple:
serializeArcsOfType
<
I
+
1
,
Tuple
>
(
resource
,
arcs
);
}
template
<
std
::
size_t
I
,
typename
Tuple
,
typename
ResourceType
>
inline
typename
std
::
enable_if
<
I
==
std
::
tuple_size
<
Tuple
>::
value
,
void
>::
type
serializeArcsOfType
(
const
std
::
shared_ptr
<
ResourceType
>&
resource
,
nlohmann
::
json
&
arcs
)
{
}
template
<
typename
ResourceType
>
nlohmann
::
json
serializeArcs
(
const
std
::
shared_ptr
<
ResourceType
>&
resource
)
{
auto
arcs
=
nlohmann
::
json
::
object
();
serializeArcsOfType
<
0
,
typename
ResourceType
::
TypeTraits
::
ArcTypes
>
(
resource
,
arcs
);
return
arcs
;
}
void
to_json
(
nlohmann
::
json
&
j
,
const
smtk
::
markup
::
Resource
::
Ptr
&
resource
)
{
// Add version number and other information inherited from parent.
...
...
@@ -39,7 +141,10 @@ void to_json(nlohmann::json& j, const smtk::markup::Resource::Ptr& resource)
j
[
"nodes"
]
=
jnodes
;
// Record arcs.
// TODO: Visit arcs?
// We can't just do this: j["arcs"] = resource->arcs(); because the
// arc classes don't share a common base type. (If they did, we could
// use the same trick smtk::common::Links does.)
j
[
"arcs"
]
=
serializeArcs
(
resource
);
// Record string-token hashes.
// Some nodes may use string tokens, so we must serialize that map if it exists.
...
...
@@ -62,6 +167,27 @@ void from_json(const nlohmann::json& j, smtk::markup::Resource::Ptr& resource)
auto
tmp
=
std
::
static_pointer_cast
<
smtk
::
resource
::
Resource
>
(
resource
);
smtk
::
resource
::
from_json
(
j
,
tmp
);
// resource->m_domains = j.at("domains")
// resource->arcs() = j.at("arcs");
// Insert nodes, then initialize them (so they can freely assume
// other nodes are present when configuring themselves).
for
(
const
auto
&
jnode
:
j
.
at
(
"nodes"
))
{
auto
node
=
resource
->
nodeFactory
().
makeFromName
(
jnode
.
at
(
"type"
).
get
<
std
::
string
>
(),
resource
,
jnode
.
at
(
"id"
).
get
<
smtk
::
common
::
UUID
>
());
resource
->
add
(
node
);
}
auto
&
helper
=
json
::
Helper
::
instance
();
for
(
const
auto
&
jnode
:
j
.
at
(
"nodes"
))
{
auto
uid
=
jnode
.
at
(
"id"
).
get
<
smtk
::
common
::
UUID
>
();
auto
node
=
std
::
dynamic_pointer_cast
<
smtk
::
markup
::
Component
>
(
resource
->
find
(
uid
));
if
(
node
)
{
node
->
initialize
(
jnode
,
helper
);
}
}
}
}
// namespace markup
...
...
smtk/markup/testing/cxx/TestResource.cxx
View file @
4ad6c3d8
...
...
@@ -22,13 +22,13 @@ int TestResource(int argc, char** argv)
auto
label
=
resource
->
createNode
<
Label
>
();
label
->
setName
(
"foo"
);
/*
auto group = std::make_shared<Group>(nullptr, smtk::common::UUID::random());
auto
group
=
std
::
make_shared
<
Group
>
(
resource
,
smtk
::
common
::
UUID
::
random
());
group
->
setName
(
"barf"
);
resource
->
add
(
group
);
*
/
/
*
auto group = resource->createNode<Group>();
group->setName("barf");
*/
auto
components
=
resource
->
filter
(
"*"
);
std
::
size_t
nn
=
components
.
size
();
std
::
cout
<<
"Created "
<<
resource
<<
" with "
<<
nn
<<
" components.
\n
"
;
...
...
@@ -44,6 +44,12 @@ int TestResource(int argc, char** argv)
// resource->connect<LabelSubject>(*label, *group);
label
->
get
<
LabelSubject
>
().
insert
(
*
group
);
// group->get<Labels>().insert(*label);
// Verify that both arcs (Labels and LabelSubject) were created.
test
(
label
->
get
<
LabelSubject
>
().
to
().
id
()
==
group
->
id
(),
"Label has no subject."
);
test
(
group
->
get
<
Labels
>
().
count
()
==
1
,
"Group has no label or multiple labels."
);
test
(
group
->
get
<
Labels
>
().
to
().
find
(
*
label
)
!=
group
->
get
<
Labels
>
().
to
().
end
(),
"Group does not have label."
);
auto
write
=
smtk
::
markup
::
Write
::
create
();
write
->
parameters
()
->
associations
()
->
appendValue
(
resource
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment