一句话:提出对象图编程 OGO,用 Cypher 声明式查询直接读写整个对象堆,并为 Java 实现两种原型。
现有集合查询(如 LINQ)只能以集合为数据源,难以声明式访问整个对象堆和运行时状态,反射与全状态断言也较繁琐。本文提出对象图编程(OGO):将堆建模为对象图,节点是对象,边是对象间关系;采用 Cypher 作为查询语言,把整个对象图视作单一集合,用于查询集合、内省运行时状态和编写访问全程序状态的断言。作者为 Java 实现两个原型:把对象图转译为 Neo4j 数据库执行 Cypher 查询,以及自研内存图查询引擎直接查询对象堆。最后用 OGO 重写大型开源项目中的数百条语句,报告经验与性能。
We introduce Object Graph Programming (OGO), which enables reading and modifying an object graph (i.e., the entire state of the object heap) via declarative queries. OGO models the objects and their relations in the heap as an object graph thereby treating the heap as a graph database: each node in the graph is an object (e.g., an instance of a class or an instance of a metadata class) and each edge is a relation between objects (e.g., a field of one object references another object). We leverage Cypher, the most popular query language for graph databases, as OGO's query language. Unlike LINQ, which uses collections (e.g., List) as a source of data, OGO views the entire object graph as a single "collection". OGO is ideal for querying collections (just like LINQ), introspecting the runtime system state (e.g., finding all instances of a given class or accessing fields via reflection), and writing assertions that have access to the entire program state. We prototyped OGO for Java in two ways: (a) by translating an object graph into a Neo4j database on which we run Cypher queries, and (b) by implementing our own in-memory graph query engine that directly queries the object heap. We used OGO to rewrite hundreds of statements in large open-source projects into OGO queries. We report our experience and performance of our prototypes.