@Override
  public void run(String... args) throws Exception {
    final File schemeFile = new File(args[0]);
    final CypherParts parts = new CypherParts();
    final List<List<?>> lists = readScheme(schemeFile);
    for (final List<?> top : lists) {
      final Symbol symbol = (Symbol) top.get(0);
      switch (symbol.getName()) {
          // ignore 'define'
          // ignore 'display'
          // ignore 'set!'
        case "define":
        case "display":
        case "set!":
          break;
          // InheritanceLink is mapped to "rdfs:subClassOf"
        case "InheritanceLink":
          log.info("INHERITANCE {}", top);
          inheritanceToCypher(parts, top);
          break;
          // EvaluationLink
        case "EvaluationLink":
          log.info("EVALUATION {}", top);
          evaluationToCypher(parts, top);
          break;
          // MemberLink/2 gene:GeneNode concept:ConceptNode
          // MemberLink is mapped to "rdf:type" ("a" in TURTLE-speak)
        case "MemberLink":
          log.info("MEMBER {}", top);
          memberToCypher(parts, top);
          break;
        default:
          log.error("Unknown symbol in '{}'", top);
      }
    }
    log.info("Cypher:\n{}", parts);

    final GraphDatabaseService graphDb = graphDb();
    try (final Transaction tx = graphDb.beginTx()) {
      log.info("Ensuring constraints and indexes...");
      graphDb.execute("CREATE CONSTRAINT ON (n:opencog_Concept) ASSERT n.href IS UNIQUE");
      graphDb.execute("CREATE CONSTRAINT ON (n:opencog_Gene) ASSERT n.href IS UNIQUE");
      graphDb.execute("CREATE CONSTRAINT ON (n:opencog_Predicate) ASSERT n.href IS UNIQUE");
      graphDb.execute("CREATE CONSTRAINT ON (n:opencog_Phrase) ASSERT n.href IS UNIQUE");

      graphDb.execute("CREATE INDEX ON :opencog_Concept(prefLabel)");
      graphDb.execute("CREATE INDEX ON :opencog_Gene(prefLabel)");
      graphDb.execute("CREATE INDEX ON :opencog_Predicate(prefLabel)");
      graphDb.execute("CREATE INDEX ON :opencog_Phrase(prefLabel)");

      tx.success();
    }
    log.info("Ensured constraints and indexes.");

    try (final Transaction tx = graphDb.beginTx()) {
      for (CypherPart node : parts.nodes.values()) {
        String cypher = "";
        for (String matchDependency : node.matchDependencies) {
          cypher += matchDependency + "\n";
        }
        cypher += node.toString();
        log.info("Creating node {}: {}", node.varName, cypher);
        graphDb.execute(cypher, node.param);
      }

      for (CypherPart relationship : parts.relationships) {
        String cypher = "";
        for (String matchDependency : relationship.matchDependencies) {
          cypher += matchDependency + "\n";
        }
        cypher += relationship.toString();
        log.info("Creating relationship: {}", cypher);
        graphDb.execute(cypher, relationship.param);
      }
      tx.success();
    }
    log.info("Done");
  }
Exemple #2
0
 public static String nsToFilename(Symbol symbol) {
   return nsToFilename(symbol.getName());
 }