1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
package UR::Namespace::Command::Update::ClassDiagram;
use strict;
use warnings;
use UR;
our $VERSION = "0.47"; # UR $VERSION;
UR::Object::Type->define(
class_name => __PACKAGE__,
is => 'UR::Namespace::Command::Base',
has => [
data_source => {type => 'String', doc => 'Which datasource to use', is_optional => 1},
depth => { type => 'Integer', doc => 'Max distance of related classes to include. Default is 1. 0 means show only the named class(es), -1 means to include everything', is_optional => 1},
file => { type => 'String', doc => 'Pathname of the Umlet (.uxf) file' },
show_attributes => { type => 'Boolean', is_optional => 1, default => 1, doc => 'Include class attributes in the diagram' },
show_methods => { type => 'Boolean', is_optional => 1, default => 0, doc => 'Include methods in the diagram (not implemented yet' },
include_ur_object => { type => 'Boolean', is_optional => 1, default => 0, doc => 'Include UR::Object and UR::Entity in the diagram (default = no)' },
initial_name => {
is_many => 1,
is_optional => 1,
shell_args_position => 1
}
],
);
sub sub_command_sort_position { 4 };
sub help_brief {
"Update an Umlet diagram based on the current class definitions"
}
sub help_detail {
return <<EOS;
Creates a new Umlet diagram, or updates an existing diagram. Bare arguments
are taken as class names to include in the diagram. Other classes may be
included in the diagram based on their distance from the names classes
and the --depth parameter.
If an existing file is being updated, the position of existing elements
will not change.
EOS
}
# The max X coord to use when placing boxes. After this, move down a line and go back to the left
use constant MAX_X_AUTO_POSITION => 800;
sub execute {
my $self = shift;
my $params = shift;
#$DB::single = 1;
my $namespace = $self->namespace_name;
eval "use $namespace";
if ($@) {
$self->error_message("Failed to load module for $namespace: $@");
return;
}
my @initial_name_list = $self->initial_name;
my $diagram;
if (-f $params->{'file'}) {
$params->{'depth'} = 0 unless (exists $params->{'depth'}); # Default is just update what's there
$diagram = UR::Object::Umlet::Diagram->create_from_file($params->{'file'});
push @initial_name_list, map { $_->subject_id } UR::Object::Umlet::Class->get(diagram_name => $diagram->name);
} else {
$params->{'depth'} = 1 unless exists($params->{'depth'});
$diagram = UR::Object::Umlet::Diagram->create(name => $params->{'file'});
}
# FIXME this can get removed when attribute defaults work correctly
unless (exists $params->{'show_attributes'}) {
$self->show_attributes(1);
}
my @involved_classes;
foreach my $class_name ( @initial_name_list ) {
push @involved_classes, UR::Object::Type->get(class_name => $class_name);
}
push @involved_classes, $self->_get_related_classes_via_inheritance(
names => \@initial_name_list,
depth => $params->{'depth'},
);
push @involved_classes, $self->_get_related_classes_via_properties(
#names => [ map { $_->class_name } @involved_classes ],
names => \@initial_name_list,
depth => $params->{'depth'},
);
my %involved_class_names = map { $_->class_name => $_ } @involved_classes;
# The initial placement, and how much to move over for the next box
my($x_coord, $y_coord, $x_inc, $y_inc) = (20,20,40,40);
my @objs = sort { $b->y <=> $a->y or $b->x <=> $a->x } UR::Object::Umlet::Class->get();
if (@objs) {
my $maxobj = $objs[0];
$x_coord = $maxobj->x + $maxobj->width + $x_inc;
$y_coord = $maxobj->y + $maxobj->height + $y_inc;
}
# First, place all the classes
my @all_boxes = UR::Object::Umlet::Class->get( diagram_name => $diagram->name );
foreach my $class ( values %involved_class_names ) {
my $umlet_class = UR::Object::Umlet::Class->get(diagram_name => $diagram->name,
subject_id => $class->class_name);
my $created = 0;
unless ($umlet_class) {
$created = 1;
$umlet_class = UR::Object::Umlet::Class->create( diagram_name => $diagram->name,
subject_id => $class->class_name,
label => $class->class_name,
x => $x_coord,
y => $y_coord,
);
# add the attributes
if ($self->show_attributes) {
my $attributes = $umlet_class->attributes || [];
my %attributes_already_in_diagram = map { $_->{'name'} => 1 } @{ $attributes };
my %id_properties = map { $_ => 1 } $class->id_property_names;
my $line_count = scalar @$attributes;
foreach my $property_name ( $class->direct_property_names ) {
next if $attributes_already_in_diagram{$property_name};
$line_count++;
my $property = UR::Object::Property->get(class_name => $class->class_name, property_name => $property_name);
push @$attributes, { is_id => $id_properties{$property_name} ? '+' : ' ',
name => $property_name,
type => $property->data_type,
line => $line_count,
};
}
$umlet_class->attributes($attributes);
}
if ($self->show_methods) {
# Not implemented yet
# Use the same module the schemabrowser uses to get that info
}
# Make sure this box dosen't overlap other boxes
while(my $overlapped = $umlet_class->is_overlapping(@all_boxes) ) {
if ($umlet_class->x > MAX_X_AUTO_POSITION) {
$umlet_class->x(20);
$umlet_class->y( $umlet_class->y + $y_inc);
} else {
$umlet_class->x( $overlapped->x + $overlapped->width + $x_inc );
}
}
push @all_boxes, $umlet_class;
}
if ($created) {
$x_coord = $umlet_class->x + $umlet_class->width + $x_inc;
if ($x_coord > MAX_X_AUTO_POSITION) {
$x_coord = 20;
$y_coord += $y_inc;
}
}
}
# Next, connect the classes together
foreach my $class ( values %involved_class_names ) {
my @properties = grep { $_->is_delegated and $_->data_type} $class->all_property_metas();
foreach my $property ( @properties ) {
next unless (exists $involved_class_names{$property->data_type});
my @property_links = eval { $property->get_property_name_pairs_for_join };
next unless @property_links;
my $id_by = join(':', map { $_->[0] } @property_links);
my $their_id_by = join (':', map { $_->[1] } @property_links);
my $umlet_relation = UR::Object::Umlet::Relation->get( diagram_name => $diagram->name,
from_entity_name => $property->class_name,
to_entity_name => $property->data_type,
from_attribute_name => $id_by,
to_attribute_name => $their_id_by,
);
unless ($umlet_relation) {
$umlet_relation = UR::Object::Umlet::Relation->create( diagram_name => $diagram->name,
relation_type => '<-',
from_entity_name => $property->class_name,
to_entity_name => $property->data_type,
from_attribute_name => $id_by,
to_attribute_name => $their_id_by,
);
unless ($umlet_relation->connect_entity_attributes()) {
# This didn't link to anything on the diagram
$umlet_relation->delete;
}
}
}
foreach my $parent_class_name ( @{ $class->is } ) {
next unless ($involved_class_names{$parent_class_name});
my $umlet_relation = UR::Object::Umlet::Relation->get( diagram_name => $diagram->name,
from_entity_name => $class->class_name,
to_entity_name => $parent_class_name,
);
unless ($umlet_relation) {
$umlet_relation = UR::Object::Umlet::Relation->create( diagram_name => $diagram->name,
relation_type => '<<-',
from_entity_name => $class->class_name,
to_entity_name => $parent_class_name,
);
$umlet_relation->connect_entities();
}
}
}
$diagram->save_to_file($params->{'file'});
1;
}
sub _get_related_classes_via_properties {
my($self, %params) = @_;
return unless (@{$params{'names'}});
return unless $params{'depth'};
# Make sure the named classes are loaded
foreach ( @{ $params{'names'} } ) {
eval { $_->class };
}
# Get everything linked to the named things
my @related_names = grep { eval { $_->class } }
#grep { $_ }
map { $_->data_type }
map { UR::Object::Property->get(class_name => $_ ) }
@{ $params{'names'}};
push @related_names, grep { eval { $_->class } }
#grep { $_ }
map { $_->class_name }
map { UR::Object::Property->get(data_type => $_ ) }
@{ $params{'names'}};
return unless @related_names;
my @objs = map { UR::Object::Type->get(class_name => $_) } @related_names;
#my @related_names = grep { $_ } map { $_->$related_param } $related_class->get($item_param => $params{'names'});
#push @related_names, grep { $_ } map { $_->$item_param } $related_class->get($related_param => $params{'names'});
#return unless @related_names;
#
# my @objs = $item_class->get($item_param => \@related_names);
unless ($self->include_ur_object) {
# Prune out UR::Object and UR::Entity
@objs = grep { $_->class_name ne 'UR::Object' and $_->class_name ne 'UR::Entity' } @objs;
}
# make a recursive call to get the related objects by name
return ( @objs, $self->_get_related_classes_via_properties( %params, names => \@related_names, depth => --$params{'depth'}) );
}
sub _get_related_classes_via_inheritance {
my($self,%params) = @_;
return unless (@{$params{'names'}});
return unless $params{'depth'};
my @related_class_names;
foreach my $class_name ( @{ $params{'names'} } ) {
# get the class loaded
eval { $class_name->class };
if ($@) {
$self->warning_message("Problem loading class $class_name: $@");
next;
}
# Get this class' parents
#push @related_class_names, $class_name->parent_classes;
push @related_class_names, @{ $class_name->__meta__->is };
}
my @objs = map { $_->__meta__ } @related_class_names;
unless ($self->include_ur_object) {
# Prune out UR::Object and UR::Entity
@objs = grep { $_->class_name ne 'UR::Object' and $_->class_name ne 'UR::Entity' } @objs;
}
# make a recursive call to get their parents
return ( @objs,
$self->_get_related_classes_via_inheritance( %params,
names => \@related_class_names,
depth => --$params{'depth'},
)
);
}
1;
|