-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOCVDotRepresentations.m
169 lines (144 loc) · 4.17 KB
/
OCVDotRepresentations.m
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
//
// OCVDotRepresentations.m
// ObjCViz
//
// Created by Olivier Gutknecht on 12/17/06.
// Copyright 2006 No Distance. See LICENSE.txt file.
//
#import "OCVDotRepresentations.h"
#import "OCVField.h"
@implementation NSObject (DotRepresentation)
-(BOOL)processFields
{
// We could do smart processing here based on [obj class] and [[NSBundle bundleForClass:[obj class]] bundlePath]
// to know if we really want to process the class or not
return YES;
}
-(NSString*)dotName
{
return [NSString stringWithFormat:@"L%p",self];
}
-(NSArray *)OCV_primitiveFields
{
NSArray *primitiveFields = [[self OCV_fields] filteredArrayUsingPredicate: [NSPredicate predicateWithBlock: ^(OCVField *field, NSDictionary *bindings){
if ([field isPrimitive])
{
//ignore isa
return (BOOL)!([[field name] isEqualToString: @"isa"]);
}
return NO;
}]];
return primitiveFields;
}
-(NSString*)OCV_label
{
NSArray *primitiveFields = [self OCV_primitiveFields];
NSString *labelValue = nil;
NSString *classString = NSStringFromClass([self class]);
if ([primitiveFields count] == 0)
{
labelValue = classString;
}
else
{
NSMutableString *dotName = [NSMutableString stringWithFormat: @"{%@}|", classString];
for (OCVField *field in primitiveFields)
{
[dotName appendString: [NSString stringWithFormat: @"{%@=%@}|", [field name], [field primitiveValueDescriptionForObject: self]]];
}
//there'll be a trailing pipe
[dotName deleteCharactersInRange: NSMakeRange([dotName length] - 1, 1)];
labelValue = [[dotName mutableCopy] autorelease];
}
return labelValue;
}
-(NSString *)OCV_shape
{
if ([[self OCV_primitiveFields] count] == 0)
{
return @"box";
}
return @"Mrecord";
}
-(void)appendDotRepresentationToString:(NSMutableString*)s withContext:(OCVContext*)context
{
[s appendFormat:@"%@ [label=\"%@\", style=rounded, shape=%@];\n",[self dotName],[self OCV_label], [self OCV_shape]];
}
@end
@implementation NSNull (DotRepresentation)
-(void)appendDotRepresentationToString:(NSMutableString*)s withContext:(OCVContext*)context
{
[s appendFormat:@"%@ [label=\"nil\"];\n",[self dotName]];
}
@end
@implementation NSString (DotRepresentation)
-(BOOL)processFields
{
return NO;
}
-(void)appendDotRepresentationToString:(NSMutableString*)s withContext:(OCVContext*)context
{
[s appendFormat:@"%@ [label=\"@\\\"%@\\\"\"];\n",[self dotName],[self description]];
}
@end
@implementation NSDictionary (DotRepresentation)
-(BOOL)processFields
{
return NO;
}
-(void)appendDotRepresentationToString:(NSMutableString*)s withContext:(OCVContext*)context
{
NSInteger i;
NSString* dotName = [self dotName];
NSArray* keys = [self allKeys];
NSUInteger c = [keys count];
[s appendFormat:@"\n%@ [label=\"{%@|{",dotName,[self class]];
for (i=0;i<c;i++)
{
if (i!=0)
[s appendString:@"|"];
[s appendFormat:@"<f%ld> %@",(long)i,[keys objectAtIndex:i]];
}
[s appendString:@"}}\",shape=Mrecord];\n"];
for (i=0;i<c;i++)
{
id ref = [self objectForKey:[keys objectAtIndex:i]];
if([ref isEqual:[NSNull null]])
continue;
[s appendFormat:@"%@:f%ld -> %@",dotName,(long)i,[ref dotName]];
[s appendFormat:@"[label=\"%@\",fontsize=12];\n",[keys objectAtIndex:i]];
[context appendGraphvizRepresentationFor:ref toString:s];
}
[s appendString:@"\n"];
}
@end
@implementation NSArray (DotRepresentation)
-(BOOL)processFields
{
return NO;
}
-(void)appendDotRepresentationToString:(NSMutableString*)s withContext:(OCVContext*)context
{
int i;
NSUInteger c = [self count];
NSString* dotName = [self dotName];
[s appendFormat:@"\n%@ [label=\"{%@|{",dotName,[self class]];
for (i=0;i<c;i++)
{
if (i!=0)
[s appendString:@"|"];
[s appendFormat:@"<f%d>",i];
}
[s appendString:@"}}\",shape=Mrecord];\n"];
for (i=0;i<c;i++)
{
id ref = [self objectAtIndex:i];
if([ref isEqual:[NSNull null]])
continue;
[s appendFormat:@"%@:f%d -> %@",dotName,i,[ref dotName]];
[s appendFormat:@"[label=\"%d\",fontsize=12];\n",i];
[context appendGraphvizRepresentationFor:ref toString:s];
}
[s appendString:@"\n"];
}
@end