@Override public void buildSchema(GraknTx tx) { key = tx.putAttributeType("name", AttributeType.DataType.STRING); geoEntity = tx.putRole("geo-entity"); entityLocation = tx.putRole("entity-location"); isLocatedIn = tx.putRelationshipType("is-located-in") .relates(geoEntity).relates(entityLocation); geographicalObject = tx.putEntityType("geoObject") .plays(geoEntity) .plays(entityLocation); geographicalObject.has(key); continent = tx.putEntityType("continent") .sup(geographicalObject) .plays(entityLocation); country = tx.putEntityType("country") .sup(geographicalObject) .plays(geoEntity) .plays(entityLocation); region = tx.putEntityType("region") .sup(geographicalObject) .plays(geoEntity) .plays(entityLocation); city = tx.putEntityType("city") .sup(geographicalObject) .plays(geoEntity) .plays(entityLocation); university = tx.putEntityType("university") .plays(geoEntity); university.has(key); }
@Override protected Entity newInstance(EntityType type) { return type.create(); } }
@Override public Set<Keyspace> keyspaces() { try (GraknTx graph = systemKeyspaceSession.transaction(GraknTxType.WRITE)) { AttributeType<String> keyspaceName = graph.getSchemaConcept(KEYSPACE_RESOURCE); return graph.<EntityType>getSchemaConcept(KEYSPACE_ENTITY).instances() .flatMap(keyspace -> keyspace.attributes(keyspaceName)) .map(name -> (String) name.value()) .map(Keyspace::of) .collect(Collectors.toSet()); } }
.plays(productionWithCluster).plays(productionBeingDirected).plays(productionWithCast) .plays(productionWithGenre); production.has(title); production.has(tmdbVoteCount); production.has(tmdbVoteAverage); production.has(releaseDate); production.has(runtime); movie = tx.putEntityType("movie").sup(production); tx.putEntityType("tv-show").sup(production); .plays(director).plays(actor).plays(characterBeingPlayed); person.has(gender); person.has(name); person.has(realName); genre = tx.putEntityType("genre").plays(genreOfProduction); genre.key(name); .plays(characterBeingPlayed); character.has(name); language.has(name); cluster = tx.putEntityType("cluster").plays(clusterOfProduction);
@Override public Concept visit(OWLSubClassOfAxiom axiom) { OWLClassExpression subclass = axiom.getSubClass(); EntityType subtype; if (subclass.isOWLClass()) { subtype = migrator.entityType(subclass.asOWLClass()); } else { // TODO - we need a strategy to support class expressions, e.g. as constraints // on instances return null; } OWLClassExpression superclass = axiom.getSuperClass(); EntityType supertype; if (superclass.isOWLClass()) { supertype = migrator.entityType(superclass.asOWLClass()); } else { // TODO - we need a strategy to support class expressions, e.g. as constraints // on instances return null; } if (!supertype.equals(subtype.sup())) { subtype.sup(supertype); } return null; }
@Override public Concept visit(OWLObjectPropertyRangeAxiom axiom) { if (!axiom.getProperty().isOWLObjectProperty()) { return null; } RelationshipType objectRelation = migrator.relation(axiom.getProperty().asOWLObjectProperty()); if (axiom.getRange().isOWLClass()) { EntityType entityType = migrator.entityType(axiom.getRange().asOWLClass()); Role range = migrator.objectRole(objectRelation); objectRelation.relates(range); migrator.owlThingEntityType().deletePlays(range); entityType.plays(range); } return objectRelation; }
public Role entityRole(EntityType entityType, AttributeType<?> attributeType) { Role role = tx.putRole(namer.entityRole(attributeType.getLabel())); entityType.plays(role); return role; }
private EntityType entityType() { return random.choose(tx.admin().getMetaEntityType().subs().collect(toSet())); }
/** * Loads the system schema inside the provided {@link GraknTx}. * * @param tx The tx to contain the system schema */ private void loadSystemSchema(GraknTx tx){ //Keyspace data AttributeType<String> keyspaceName = tx.putAttributeType("keyspace-name", AttributeType.DataType.STRING); tx.putEntityType("keyspace").key(keyspaceName); } }
public Entity putEntity(String id, EntityType type) { Entity current = getEntity(id, owlIriResource()); if(current != null) return current; Label hasIriResourceId = Label.of(OwlModel.IRI.owlname()); AttributeType<String> iriResource = owlIriResource(); Role hasIriOwner = entityRole(type, iriResource); Role hasIriValue = resourceRole(iriResource); RelationshipType hasIriRelation = tx.putRelationshipType(namer.resourceRelation(hasIriResourceId)) .relates(hasIriOwner).relates(hasIriValue); Entity entity = type.addEntity(); Attribute attributeInstance = iriResource.putAttribute(id); hasIriRelation.addRelationship() .addRolePlayer(hasIriOwner, entity) .addRolePlayer(hasIriValue, attributeInstance); return entity; }
/** * Make the second argument the super of the first argument * * @throws GraqlQueryException if the types are different, or setting the super to be a meta-type */ public static void setSuper(SchemaConcept subConcept, SchemaConcept superConcept) { if (superConcept.isEntityType()) { subConcept.asEntityType().sup(superConcept.asEntityType()); } else if (superConcept.isRelationshipType()) { subConcept.asRelationshipType().sup(superConcept.asRelationshipType()); } else if (superConcept.isRole()) { subConcept.asRole().sup(superConcept.asRole()); } else if (superConcept.isAttributeType()) { subConcept.asAttributeType().sup(superConcept.asAttributeType()); } else if (superConcept.isRule()) { subConcept.asRule().sup(superConcept.asRule()); } else { throw GraqlQueryException.insertMetaType(subConcept.label(), superConcept); } }
@Override public Concept visit(OWLObjectPropertyDomainAxiom axiom) { if (!axiom.getProperty().isOWLObjectProperty()) { return null; } RelationshipType objectRelation = migrator.relation(axiom.getProperty().asOWLObjectProperty()); if (axiom.getDomain().isOWLClass()) { EntityType entityType = migrator.entityType(axiom.getDomain().asOWLClass()); Role domain = migrator.subjectRole(objectRelation); migrator.owlThingEntityType().deletePlays(domain); entityType.plays(domain); objectRelation.relates(domain); // System.out.println("Replaced domain thing with " + entityType.getLabel()); } return objectRelation; }
public RelationshipType relation(OWLObjectProperty property) { RelationshipType relType = tx.putRelationshipType(namer.objectPropertyName(property.getIRI())); Role subjectRole = subjectRole(relType); Role objectRole = objectRole(relType); relType.relates(subjectRole); relType.relates(objectRole); EntityType top = this.owlThingEntityType(); top.plays(subjectRole); top.plays(objectRole); return relType; }
/** * Helper method to get the types to be included in the query scope * * @return stream of Concept Types */ private Stream<Type> scopeTypes() { // Get all types if query.inTypes() is empty, else get all scoped types of each meta type. // Only include attributes and implicit "has-xxx" relationships when user specifically asked for them. if (!query.in().isPresent() || query.in().get().isEmpty()) { ImmutableSet.Builder<Type> typeBuilder = ImmutableSet.builder(); if (scopeIncludesAttributes()) { tx.admin().getMetaConcept().subs().forEach(typeBuilder::add); } else { tx.admin().getMetaEntityType().subs().forEach(typeBuilder::add); tx.admin().getMetaRelationType().subs() .filter(relationshipType -> !relationshipType.isImplicit()).forEach(typeBuilder::add); } return typeBuilder.build().stream(); } else { Stream<Type> subTypes = query.in().get().stream().map(label -> { Type type = tx.getType(label); if (type == null) throw GraqlQueryException.labelNotFound(label); return type; }).flatMap(Type::subs); if (!scopeIncludesAttributes()) { subTypes = subTypes.filter(relationshipType -> !relationshipType.isImplicit()); } return subTypes; } }
.has(key); .sup(baseEntity) .plays(seller) .plays(payee) .has(nationality); .sup(person); .sup(baseEntity) .plays(transactionItem) .plays(ownedItem); .sup(weapon) .has(propulsion); .sup(weapon) //sup(rocket)? .has(propulsion); .sup(baseEntity) .plays(buyer) .plays(owner) .plays(enemyTarget) .plays(payer) .plays(enemySource) .has(alignment);
/** * Make the second argument the super of the first argument * * @throws GraqlQueryException if the types are different, or setting the super to be a meta-type */ private static void setSuper(SchemaConcept subConcept, SchemaConcept superConcept) { if (superConcept.isEntityType()) { subConcept.asEntityType().sup(superConcept.asEntityType()); } else if (superConcept.isRelationshipType()) { subConcept.asRelationshipType().sup(superConcept.asRelationshipType()); } else if (superConcept.isRole()) { subConcept.asRole().sup(superConcept.asRole()); } else if (superConcept.isAttributeType()) { subConcept.asAttributeType().sup(superConcept.asAttributeType()); } else if (superConcept.isRule()) { subConcept.asRule().sup(superConcept.asRule()); } else { throw GraqlQueryException.insertMetaType(subConcept.getLabel(), superConcept); } } }
public static Thing putEntityWithResource(GraknTx tx, String id, EntityType type, Label key) { Thing inst = type.create(); putResource(inst, tx.getSchemaConcept(key), id); return inst; }
public static void printWholeCompletionMessage(MigrationOptions options){ System.out.println("Migration complete."); if(options.isVerbose()) { System.out.println("Gathering information about migrated data. If in a hurry, you can ctrl+c now."); GraknTx tx = new Grakn(options.getUri()).session(options.getKeyspace()).transaction(GraknTxType.WRITE); QueryBuilder qb = tx.graql(); StringBuilder builder = new StringBuilder(); builder.append("Graph schema contains:\n"); builder.append("\t ").append(tx.admin().getMetaEntityType().instances().count()).append(" entity types\n"); builder.append("\t ").append(tx.admin().getMetaRelationType().instances().count()).append(" relation types\n"); builder.append("\t ").append(tx.admin().getMetaRole().subs().count()).append(" roles\n\n"); builder.append("\t ").append(tx.admin().getMetaAttributeType().instances().count()).append(" resource types\n"); builder.append("\t ").append(tx.admin().getMetaRule().subs().count()).append(" rules\n\n"); builder.append("Graph data contains:\n"); builder.append("\t ").append(qb.match(var("x").isa(label(ENTITY.getLabel()))).aggregate(count()).execute()).append(" entities\n"); builder.append("\t ").append(qb.match(var("x").isa(label(RELATIONSHIP.getLabel()))).aggregate(count()).execute()).append(" relations\n"); builder.append("\t ").append(qb.match(var("x").isa(label(ATTRIBUTE.getLabel()))).aggregate(count()).execute()).append(" resources\n"); builder.append("\t ").append(qb.match(var("x").isa(label(RULE.getLabel()))).aggregate(count()).execute()).append(" rules\n\n"); System.out.println(builder); tx.close(); } }
@Override protected void buildInstances(GraknTx tx) { godfather = movie.create(); putResource(godfather, title, "Godfather"); putResource(godfather, tmdbVoteCount, 1000L); theMuppets = movie.create(); putResource(theMuppets, title, "The Muppets"); putResource(theMuppets, tmdbVoteCount, 100L); apocalypseNow = movie.create(); putResource(apocalypseNow, title, "Apocalypse Now"); putResource(apocalypseNow, tmdbVoteCount, 400L); putResource(apocalypseNow, tmdbVoteAverage, 8.4); heat = movie.create(); putResource(heat, title, "Heat"); hocusPocus = movie.create(); putResource(hocusPocus, title, "Hocus Pocus"); putResource(hocusPocus, tmdbVoteCount, 435L); spy = movie.create(); putResource(spy, title, "Spy"); putResource(spy, releaseDate, LocalDate.of(1986, 3, 3).atStartOfDay()); chineseCoffee = movie.create(); putResource(chineseCoffee, title, "Chinese Coffee"); putResource(chineseCoffee, tmdbVoteCount, 5L);