GOBO
view release on metacpan or search on metacpan
GOBO/Graph.pm view on Meta::CPAN
- Returns: ArrayRef[GOBO::InstanceNode], where each member is an instance belonging to this graph
=cut
sub instances {
my $self = shift;
#$self->node_index->nodes_by_metaclass('instance');
return [values %{$self->instance_h}];
}
=head2 add_term
- Arguments: Str or GOBO::Node
- Returns: GOBO::TermNode
- Side effects: adds the object to the list of terms referenced in this graph. Forces the class to be GOBO::TermNode
=cut
sub add_term {
my $self = shift;
my $n = $self->term_noderef(@_);
$self->term_h->{$n->id} = $n;
return $n;
}
=head2 add_relation
- Arguments: Str or GOBO::Node
- Returns: GOBO::RelationNode
- Side effects: adds the object to the list of relations referenced in this graph. Forces the class to be GOBO::RelationNode
=cut
sub add_relation {
my $self = shift;
my $n = $self->relation_noderef(@_);
$self->relation_h->{$n->id} = $n;
return $n;
}
=head2 add_instance
- Arguments: Str or GOBO::Node
- Returns: GOBO::InstanceNode
adds the object to the list of instances referenced in this
graph. Forces the class to be GOBO::InstanceNode
=cut
sub add_instance {
my $self = shift;
my $n = $self->instance_noderef(@_);
$self->instance_h->{$n->id} = $n;
return $n;
}
=head2 remove_node
- Arguments: node GOBO::Node, cascade Bool[OPT]
unlinks the node from this graph
If cascade is 0 or undef, any links to or from this node will remain as dangling links.
If cascade is set, then links to and from this node will also be deleted
=cut
sub remove_node {
my $self = shift;
my $n = shift;
my $cascade = shift;
#my $id = ref($n) ? $n->id : $n;
my $id = $n->id;
if ($self->term_h->{$id}) {
delete $self->term_h->{$id};
}
if ($self->instance_h->{$id}) {
delete $self->instance_h->{$id};
}
if ($self->relation_h->{$id}) {
delete $self->relation_h->{$id};
}
if ($cascade) {
$self->remove_link($_) foreach @{$self->get_outgoing_links($n)};
$self->remove_link($_) foreach @{$self->get_incoming_links($n)};
}
return $self->node_index->remove_node($n);
}
sub add_formula { my $self = shift; push(@{$self->formulae},@_) }
=head2 get_outgoing_links (subject GOBO::Node, relation GOBO::RelationNode OPTIONAL)
given a subject (child), get target (parent) links
if relation is specified, also filters results on relation
=cut
sub get_outgoing_links {
my $self = shift;
my $n = shift;
my $rel = shift;
my @sl = @{$self->link_ix->statements_by_node_id(ref($n) ? $n->id : $n) || []};
# if x = a AND r(b), then x r b
if (ref($n) && $n->isa('GOBO::ClassExpression::Intersection')) {
foreach (@{$n->arguments}) {
if ($_->isa('GOBO::ClassExpression::RelationalExpression')) {
push(@sl, new GOBO::LinkStatement(node=>$n,relation=>$_->relation,target=>$_->target));
}
else {
push(@sl, new GOBO::LinkStatement(node=>$n,relation=>'is_a',target=>$_));
}
}
}
if ($rel) {
# TODO: use indexes to make this faster
my $rid = ref($rel) ? $rel->id : $rel;
@sl = grep {$_->relation->id eq $rid} @sl;
}
return \@sl;
}
# @Deprecated
*get_target_links = \&get_outgoing_links;
=head2 get_incoming_links (subject GOBO::Node, relation GOBO::RelationNode OPTIONAL)
given a subject (child), get target (parent) links
if relation is specified, also filters results on relation
=cut
sub get_incoming_links {
my $self = shift;
my $n = shift;
my $rel = shift;
my @sl = @{$self->link_ix->statements_by_target_id(ref($n) ? $n->id : $n) || []};
if ($rel) {
# TODO: use indexes to make this faster
my $rid = ref($rel) ? $rel->id : $rel;
( run in 1.033 second using v1.01-cache-2.11-cpan-2398b32b56e )