private List<ID> findMappedTypes(ID subject, UID context, Multimap<UID, STMT> properties) { List<ID> types = new ArrayList<ID>(); if (properties.containsKey(RDF.type)) { for (STMT stmt : properties.get(RDF.type)) { NODE type = stmt.getObject(); if (type instanceof UID && configuration.getMappedClasses((UID) type) != null) { types.add((UID) type); } } } return types; }
@Nullable private Class<?> convertClassReference(UID uid, Class<?> targetClass) { List<MappedClass> mappedClasses = configuration.getMappedClasses(uid); boolean foundMatch = false; for (MappedClass mappedClass : mappedClasses) { Class<?> clazz = mappedClass.getJavaClass(); if (targetClass.isAssignableFrom(clazz)) { targetClass = clazz; foundMatch = true; } } if (foundMatch) { return targetClass; } else { return null; } }
@Nullable @SuppressWarnings("unchecked") private <T> Class<? extends T> matchType(Collection<ID> types, Class<T> targetType) { if (types.isEmpty()) { return targetType; } else { Class<? extends T> result = targetType; boolean foundMatch = false; for (ID type : types) { if (type instanceof UID) { UID uid = (UID) type; List<MappedClass> classes = configuration.getMappedClasses(uid); if (classes != null) { for (MappedClass mappedClass : classes) { Class<?> clazz = mappedClass.getJavaClass(); if ((result == null || result.isAssignableFrom(clazz)) && !clazz.isInterface()) { foundMatch = true; result = (Class<? extends T>) clazz; } } } } } if (foundMatch) { return result; } else { return null; } } }
private MappedClass resolveMappedClass(MappedClass mappedClass, PropertiesMap properties) { for (STMT stmt : properties.getDirect().get(RDF.type)) { if (stmt.getObject().isURI()) { List<MappedClass> mappedClasses = configuration.getMappedClasses(stmt.getObject().asURI()); for (MappedClass mc : mappedClasses) { if (!mc.equals(mappedClass) && mappedClass.getJavaClass().isAssignableFrom(mc.getJavaClass())) { return mc; } } } } return mappedClass; }
public ConfigurationOntology(Configuration configuration) { Set<UID> types = new HashSet<UID>(); Multimap<UID, UID> directSubtypes = MultimapFactory.<UID, UID> createWithSet(); Multimap<UID, UID> directSupertypes = MultimapFactory.<UID, UID> createWithSet(); for (MappedClass mappedClass : configuration.getMappedClasses()) { types.add(mappedClass.getUID()); for (MappedClass superClass : mappedClass.getMappedSuperClasses()) { directSupertypes.put(mappedClass.getUID(), superClass.getUID()); directSubtypes.put(superClass.getUID(), mappedClass.getUID()); } // ClassMapping classMapping = // mappedClass.getJavaClass().getAnnotation(ClassMapping.class); // if (!classMapping.parent().equals("")) { // UID parent = new UID(classMapping.parent()); // directSupertypes.put(mappedClass.getUID(), parent); // directSubtypes.put(parent, mappedClass.getUID()); // } } initializeTypeHierarchy(types, directSubtypes, directSupertypes); }
public void export() { SessionFactoryImpl sessionFactory = new SessionFactoryImpl(); sessionFactory.setConfiguration(new DefaultConfiguration(RDFSClass.class.getPackage(), OWLClass.class.getPackage())); sessionFactory.setRepository(repository); sessionFactory.initialize(); Session session = sessionFactory.openSession(); if (ontology != null) { Ontology ont = new Ontology(new UID(ontology)); if (ontologyImports != null) { for (String oimport : ontologyImports) { ont.addImport(new Ontology(new UID(oimport))); } } session.save(ont); } Map<UID, RDFSResource> resources = new HashMap<UID, RDFSResource>(); resources.put(RDF.List, new RDFSClass<RDFSResource>(RDF.List)); resources.put(RDF.first, new RDFProperty(RDF.first)); resources.put(RDF.rest, new RDFProperty(RDF.rest)); resources.put(RDF.type, new RDFProperty(RDF.type)); resources.put(RDFS.label, new RDFProperty(RDFS.label)); resources.put(RDFS.comment, new RDFProperty(RDFS.comment)); resources.put(RDFS.Resource, new RDFSClass<Object>(RDFS.Resource)); // process classes for (MappedClass mappedClass : configuration.getMappedClasses()) { processClass(session, mappedClass.getJavaClass(), resources); } // save resources session.saveAll(resources.values().toArray()); }
@Test public void GetMappedClasses_For_Unknown() { Configuration configuration = new DefaultConfiguration(); assertEquals(Collections.emptyList(), configuration.getMappedClasses(new UID(TEST.NS))); }
@Test public void MappedClass_Has_IdProperty() { for (MappedClass mappedClass : configuration.getMappedClasses()) { assertTrue(mappedClass.getIdProperty() != null); } }
private void collectTypes() { for (MappedClass mappedClass : configuration.getMappedClasses()) { if (mappedClass.isEnum()) { // skip enum types continue; } EntityType entityType = (EntityType) createType(mappedClass.getJavaClass()); for (MappedPath mappedPath : mappedClass.getProperties()) { MappedProperty<?> mappedProperty = mappedPath.getMappedProperty(); Class<?> type = mappedPath.getMappedProperty().getType(); Type propertyType = null; if (type.equals(Map.class)) { propertyType = new SimpleType(Types.MAP, createType(mappedProperty.getKeyType()), createType(mappedProperty.getComponentType())); } else if (type.equals(List.class)) { propertyType = new SimpleType(Types.LIST, createType(mappedProperty.getComponentType())); } else if (type.equals(Set.class)) { propertyType = new SimpleType(Types.SET, createType(mappedProperty.getComponentType())); } else if (type.equals(Collection.class)) { propertyType = new SimpleType(Types.COLLECTION, createType(mappedProperty.getComponentType())); } else { propertyType = createType(type); } entityType.addProperty(createProperty(entityType, mappedPath.getName(), propertyType, mappedProperty.getAnnotations())); } } }
boolean matched = false; for (STMT typeStmt : typeStmts) { for (MappedClass cl : configuration.getMappedClasses(typeStmt.getObject().asURI())) { if (componentType.isAssignableFrom(cl.getJavaClass())) { matched = true;
@Test public void UID_availability() { ConfigurationBuilder builder = new ConfigurationBuilder(); builder.addClass(Person.class); builder.addClass(Department.class); builder.addClass(Company.class); builder.addClass(Labeled.class); Configuration configuration = builder.build(); for (MappedClass mc : configuration.getMappedClasses()) { assertNotNull(mc.getUID()); } }