private void exportEnumerationTypeInfo(IfcEnumerationTypeInfo typeInfo) { String typeUri = super.formatTypeName(typeInfo); Resource typeResource = createUriResource(typeUri); if (!context.isEnabledOption(Ifc2RdfConversionOptionsEnum.ForceConvertEnumerationValuesToString)) { adapter.exportTriple(typeResource, RDF.type, OWL.Class); List<Resource> nodes = new ArrayList<>(); List<String> enumValues = typeInfo.getValues(); for (String value : enumValues) { nodes.add(super.createUriResource(super.formatOntologyName(value))); } RDFList rdfList = super.createList(nodes); adapter.exportTriple(typeResource, OWL.oneOf, rdfList); } else { adapter.exportTriple(typeResource, RDF.type, RDFS.Datatype); adapter.exportTriple(typeResource, RDFS.subClassOf, XSD.xstring); // List<String> values = typeInfo.getValues(); // String valueString = StringUtils.collectionToString(values, "(", ")", String.format("\"%%s\"^^%s", RdfVocabulary.XSD_STRING.getShortForm()), " "); // adapter.writeRdfTriple(ExportAdapter.CURRENT_SUBJECT, OWL.oneOf, valueString); } }
private void exportSelectTypeInfo(IfcSelectTypeInfo typeInfo) { Resource typeResource = super.createUriResource(super.formatTypeName(typeInfo)); adapter.exportTriple(typeResource, RDF.type, OWL.Class); List<String> subTypeNames = typeInfo.getSelectTypeInfoNames(); List<Resource> nodes = new ArrayList<>(); for (String typeName : subTypeNames) { nodes.add(super.createUriResource(super.formatOntologyName(typeName))); } RDFList rdfList = super.createList(nodes); // See samples: [2, p.250] adapter.exportTriple(typeResource, OWL.unionOf, rdfList); // See samples: [2, pp.135-136] // for (String subTypeName : subTypeNames) { // writeSentence(generateName(subTypeName), RDFS.subClassOf, typeName); // } }
private void exportLogicalTypeInfo(IfcLogicalTypeInfo typeInfo) { String typeUri = super.formatExpressOntologyName(typeInfo.getName()); Resource typeResource = createUriResource(typeUri); adapter.exportTriple(typeResource, RDF.type, OWL.Class); List<String> enumValues = typeInfo.getValues(); List<RDFNode> enumValueNodes = new ArrayList<>(); for (String value : enumValues) { enumValueNodes.add(super.createUriResource(super.formatExpressOntologyName(value))); } if (owlProfileList.supportsRdfProperty(OWL.oneOf, EnumSet.of(RdfTripleObjectTypeEnum.ZeroOrOneOrMany))) { RDFList rdfList = super.createList(enumValueNodes); adapter.exportTriple(typeResource, OWL.oneOf, rdfList); } else { // if (!context.isEnabledOption(Ifc2RdfConversionOptionsEnum.ForceConvertLogicalValuesToString)) { enumValueNodes.stream().forEach(node -> adapter.exportTriple((Resource)node, RDF.type, typeResource)); } }
private void exportEnumerationTypeInfo(IfcEnumerationTypeInfo typeInfo) { String typeUri = super.formatTypeName(typeInfo); Resource typeResource = createUriResource(typeUri); adapter.exportTriple(typeResource, RDF.type, OWL.Class); adapter.exportTriple(typeResource, RDFS.subClassOf, Ifc2RdfVocabulary.EXPRESS.Enum); List<String> enumValues = typeInfo.getValues(); List<RDFNode> enumValueNodes = new ArrayList<>(); for (String value : enumValues) { enumValueNodes.add(super.createUriResource(super.formatOntologyName(value))); } if (owlProfileList.supportsRdfProperty(OWL.oneOf, EnumSet.of(RdfTripleObjectTypeEnum.ZeroOrOneOrMany))) { Resource equivalentTypeResource = super.createAnonResource(); adapter.exportTriple(typeResource, OWL.equivalentClass, equivalentTypeResource); RDFList rdfList = super.createList(enumValueNodes); adapter.exportTriple(equivalentTypeResource, OWL.oneOf, rdfList); } else { // if (!context.isEnabledOption(Ifc2RdfConversionOptionsEnum.ForceConvertEnumerationValuesToString)) { enumValueNodes.stream().forEach(node -> adapter.exportTriple((Resource)node, RDF.type, typeResource)); } }
private void exportSelectTypeInfo(IfcSelectTypeInfo typeInfo) { Resource typeResource = super.createUriResource(super.formatTypeName(typeInfo)); adapter.exportTriple(typeResource, RDF.type, OWL.Class); adapter.exportTriple(typeResource, RDFS.subClassOf, Ifc2RdfVocabulary.EXPRESS.Select); List<String> subTypeNames = typeInfo.getSelectTypeInfoNames(); List<Resource> subTypeResources = new ArrayList<>(); for (String typeName : subTypeNames) { subTypeResources.add(super.createUriResource(super.formatOntologyName(typeName))); } if (owlProfileList.supportsRdfProperty(OWL.unionOf, EnumSet.of(RdfTripleObjectTypeEnum.ZeroOrOneOrMany)) && subTypeResources.size() > 1) { RDFList rdfList = super.createList(subTypeResources); // See samples: [2, p.250] adapter.exportTriple(typeResource, OWL.unionOf, rdfList); } else { subTypeResources.stream().forEach(subTypeResource -> adapter.exportTriple((Resource)subTypeResource, RDFS.subClassOf, typeResource)); } }
/** * Call this method to declare the listed Individuals to be known to be * different * * @param instances * @return */ public List<ModelError> addDifferentInstances(List<ConceptName> instances) { RDFNode[] nodeArray = new RDFNode[instances.size()]; int idx = 0; Iterator<ConceptName> itr = instances.iterator(); while (itr.hasNext()) { ConceptName nme = itr.next(); if (nme == null) { addError(0, "Instance " + (idx + 1) + " is not specified; can't set all different."); return getErrorsFinal(); } Individual inst = getOrCreateIndividual(0, idx + 1, nme); if (inst == null) { addError(0, "Instance " + (idx + 1) + " is not specified; can't set all different."); return getErrorsFinal(); } nodeArray[idx++] = inst; } RDFList differentMembers = getJenaModel().createList(nodeArray); getJenaModel().createAllDifferent(differentMembers); return getErrorsFinal(); }
/** * Call this method to convert a SadlUnionClass to a Jena UnionClass. * * @param argIdx * -- the argument index of SadlUnionClass in the original call * to ModelManager * @param union * -- the encapsulated list of classes to place in the UnionClass * @return -- the UnionClass if successful else null if not */ private UnionClass createUnionClass(int argIdx, SadlUnionClass union) { List<ConceptIdentifier> unionClasses = union.getClasses(); if (unionClasses != null) { RDFNode[] clsarray = new RDFNode[unionClasses.size()]; for (int i = 0; i < unionClasses.size(); i++) { ConceptIdentifier clsName = unionClasses.get(i); OntClass cls = conceptIdentifierToOntClass(argIdx, i, clsName); if (cls != null) { clsarray[i] = cls; } else { return null; } } RDFList classes = getJenaModel().createList(clsarray); if (!classes.isEmpty()) { return getJenaModel().createUnionClass(null, classes); } } return null; }
/** * Call this method to convert a SadlIntersectionClass to a Jena * IntersectionClass. * * @param argIdx * -- the argument index of the SadlIntersectionClass in the * original call to ModelManager * @param intersect * -- the encapuslated list of classes to place in the * IntersectionClass * @return -- the IntersectionClass if successful else null if not */ private IntersectionClass createIntersectionClass(int argIdx, SadlIntersectionClass intersect) { List<ConceptIdentifier> intersectClasses = intersect.getClasses(); if (intersectClasses != null) { RDFList classes = getJenaModel().createList(); for (int i = 0; i < intersectClasses.size(); i++) { ConceptIdentifier clsName = intersectClasses.get(i); OntClass cls = conceptIdentifierToOntClass(argIdx, i, clsName); if (cls != null) { classes = classes.with(cls); } } if (!classes.isEmpty()) { return getJenaModel().createIntersectionClass(null, classes); } } return null; }
private String getIntersectionClassRangeString(IntersectionClass intersectionClass) { String rslt = ""; int cnt = 0; RDFList iclsses = intersectionClass.getOperands(); if (iclsses != null) { ExtendedIterator<RDFNode> eitr = iclsses.iterator(); while (eitr.hasNext()) { RDFNode node = eitr.next(); if (cnt > 0) rslt += " and "; if (node.canAs(OntClass.class)) { rslt += ontClassToString(node.as(OntClass.class), null); } cnt++; } if (cnt > 1) { rslt = "{" + rslt + "}"; } } return rslt; }
private String getUnionClassRangeString(UnionClass unionClass) { String rslt = ""; int cnt = 0; RDFList uclsses = unionClass.getOperands(); if (uclsses != null) { ExtendedIterator<RDFNode> eitr = uclsses.iterator(); while (eitr.hasNext()) { RDFNode node = eitr.next(); if (cnt > 0) rslt += " or "; if (node.canAs(OntClass.class)) { rslt += ontClassToString(node.as(OntClass.class), null); } cnt++; } if (cnt > 1) { rslt = "{" + rslt + "}"; } } return rslt; }
private EnumeratedClass sadlExplicitValuesToEnumeratedClass(EList<SadlExplicitValue> values) throws JenaProcessorException { List<RDFNode> nodevals = new ArrayList<RDFNode>(); for (int i = 0; i < values.size(); i++) { SadlExplicitValue value = values.get(i); RDFNode nodeval = sadlExplicitValueToRdfNode(value, null, true); if (nodeval.canAs(Individual.class)) { nodevals.add(nodeval.as(Individual.class)); } else { nodevals.add(nodeval); } } RDFNode[] enumedArray = nodevals.toArray(new RDFNode[nodevals.size()]); RDFList rdfl = getTheJenaModel().createList(enumedArray); EnumeratedClass enumCls = getTheJenaModel().createEnumeratedClass(null, rdfl); return enumCls; }
public Exercise33Jena() { ontModel = readModel(MODEL_INPUT_EX33_JENA); OntClass norwegianCity = ontModel.getOntClass(NS + "NorwegianCity"); OntClass city = ontModel.getOntClass(NS + "City"); RDFList intersectingClasses = ontModel.createList(); intersectingClasses = intersectingClasses.with(city); Property hasMainLanguage = ontModel.getProperty(NS, "hasMainLanguage"); Restriction hasMainLanguageRestriction = ontModel.createRestriction(hasMainLanguage); Resource norwegian = ontModel.getOntResource(NS + "Norwegian"); hasMainLanguageRestriction.convertToHasValueRestriction(norwegian); intersectingClasses.add(hasMainLanguageRestriction); norwegianCity.getEquivalentClass().convertToIntersectionClass(intersectingClasses); individuals = getIndividuals(ontModel); }
private RDFList getCardinalityValueList(int min, int max) { List<Literal> literals = new ArrayList<>(); for (int i = min; i <= max; ++i) { literals.add(getJenaModel().createTypedLiteral(i)); } return super.createList(literals); }
private OntResource addClassToUnionClass(OntModel model, OntResource existingCls, OntResource cls) { if (existingCls != null && !existingCls.equals(cls)) { RDFList classes = null; if (existingCls.canAs(UnionClass.class)) { try { classes = ((UnionClass) existingCls.as(UnionClass.class)) .getOperands(); if (classes.contains(cls)) { return existingCls; } classes = classes.with(cls); } catch (Exception e) { // don't know why this is happening logger.error("Union class error that hasn't been resolved or understood."); return cls; } } else { if (cls.equals(existingCls)) { return existingCls; } classes = model.createList(); classes = classes.with(existingCls); classes = classes.with(cls); } OntResource unionClass = model.createUnionClass(null, classes); return unionClass; } else { return cls; } }
private void createEquivalentClass(OntClass rolecls, OntClass supercls, Restriction restrict) { if (supercls != null && restrict != null) { RDFNode[] memberList = new RDFNode[2]; memberList[0] = supercls; memberList[1] = restrict; RDFList members = getJenaModel().createList(memberList); IntersectionClass eqcls = getJenaModel().createIntersectionClass( null, members); rolecls.setEquivalentClass(eqcls); } else if (restrict != null) { rolecls.setEquivalentClass(restrict); } }
private void createEquivalentClass(OntClass rolecls, OntClass supercls, List<Restriction> restrict) { int base = supercls != null ? 1 : 0; RDFNode[] memberList = new RDFNode[base + restrict.size()]; if (base > 0) { memberList[0] = supercls; } for (int i = 0; i < restrict.size(); i++) { memberList[base + i] = restrict.get(i); } RDFList members = getJenaModel().createList(memberList); IntersectionClass eqcls = getJenaModel().createIntersectionClass(null, members); rolecls.setEquivalentClass(eqcls); }
private OntResource addClassToUnionClass(OntResource existingCls, OntResource cls) { if (existingCls != null && !existingCls.equals(cls)) { if (existingCls.canAs(OntClass.class) && classIsSubclassOf(existingCls.as(OntClass.class), cls, true)) { return cls; } else if (cls.canAs(OntClass.class) && classIsSubclassOf(cls.as(OntClass.class), existingCls, true)) { return existingCls; } else { RDFList classes = null; if (existingCls.canAs(UnionClass.class)) { try { UnionClass ucls = existingCls.as(UnionClass.class); ucls.addOperand(cls); return ucls; } catch (Exception e) { // don't know why this is happening logger.error("Union class error that hasn't been resolved or understood."); return cls; } } else { if (cls.equals(existingCls)) { return existingCls; } classes = getJenaModel().createList(); classes = classes.with(existingCls); classes = classes.with(cls); } OntResource unionClass = getJenaModel().createUnionClass(null, classes); return unionClass; } } else { return cls; } }
@Test public void test() { OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); // Create the Classes : animal, plant, sheep, grass, vegetarian; OntClass animal = model.createClass(NS + "Animal"); OntClass plant = model.createClass(NS + "Plant"); OntClass sheep = model.createClass(NS + "Sheep"); OntClass grass = model.createClass(NS + "Grass"); OntClass vegetarian = model.createClass(NS + "Vegetarian"); // Set sheep as subClass of animal, set grass as subClass of plant; animal.addSubClass(sheep); plant.addSubClass(grass); // Create the object property eat and part_of (there domain and range are owl:Thing); ObjectProperty eat = model.createObjectProperty(NS + "eat"); ObjectProperty partOf = model.createObjectProperty(NS + "partOf"); // Create Restriction : eatAllGrass, set sheep as its subclass; AllValuesFromRestriction avr = model.createAllValuesFromRestriction(null, eat, grass); avr.addSubClass(sheep); // Create Restriction : partofSomePlant; partofSomeAnimal; SomeValuesFromRestriction plantPart = model.createSomeValuesFromRestriction(null, partOf, plant); SomeValuesFromRestriction animalPart = model.createSomeValuesFromRestriction(null, partOf, animal); // Create the Union Class meat : (animal, partofSomeAnimal); RDFNode[] nodes1 = {animal, animalPart}; RDFList meatList = model.createList(nodes1); UnionClass meat = model.createUnionClass(null, meatList); // Create the Union Class vegetable : (plant, partofSomePlant): RDFNode[] nodes2 = {plant, plantPart}; RDFList vegetableList = model.createList(nodes2); UnionClass vegetable = model.createUnionClass(null, vegetableList); model.write(System.out, "RDF/XML-ABBREV"); model.write(System.out, "N3"); }
private OntClass createRdfsDatatype(String newDatatypeUri, List<RDFNode> unionOfTypes, com.hp.hpl.jena.rdf.model.Resource onDatatype, SadlDataTypeFacet facet) throws JenaProcessorException { OntClass datatype = getTheJenaModel().createOntResource(OntClass.class, RDFS.Datatype, newDatatypeUri); OntClass equivClass = getTheJenaModel().createOntResource(OntClass.class, RDFS.Datatype, null); if (onDatatype != null) { equivClass.addProperty(OWL2.onDatatype, onDatatype); if (facet != null) { com.hp.hpl.jena.rdf.model.Resource restrictions = facetsToRestrictionNode(newDatatypeUri, facet); // Create a list containing the restrictions RDFList list = getTheJenaModel().createList(new RDFNode[] { restrictions }); equivClass.addProperty(OWL2.withRestrictions, list); } } else if (unionOfTypes != null) { Iterator<RDFNode> iter = unionOfTypes.iterator(); RDFList collection = getTheJenaModel().createList(); while (iter.hasNext()) { RDFNode dt = iter.next(); collection = collection.with(dt); } equivClass.addProperty(OWL.unionOf, collection); } else { throw new JenaProcessorException("Invalid arguments to createRdfsDatatype"); } datatype.addEquivalentClass(equivClass); TypeMapper.getInstance().getSafeTypeByName(newDatatypeUri); return datatype; }
private IntersectionClass createIntersectionClass(RDFNode... members) throws JenaProcessorException { RDFList classes = getTheJenaModel().createList(members); if (!classes.isEmpty()) { IntersectionClass intersectCls = getTheJenaModel().createIntersectionClass(null, classes); logger.debug("New intersection class created"); return intersectCls; } throw new JenaProcessorException("createIntersectionClass called with empty list of classes"); }
protected RDFList createList(Collection<? extends RDFNode> resources) { return jenaModel.createList(resources.iterator()); }
public String getObjectPropSomeValueFrom(Restriction restrict) { String key = ""; if (restrict.isSomeValuesFromRestriction()) { String some = ""; SomeValuesFromRestriction someValueFrom = restrict.asSomeValuesFromRestriction(); OntProperty prop = someValueFrom.getOnProperty(); some += prop; OntClass fromCls = (OntClass) someValueFrom.getSomeValuesFrom(); some += " some ("; if (fromCls.isUnionClass()) { // or ExtendedIterator<? extends OntClass> operandsItor = fromCls.asUnionClass().listOperands(); OntClass disjClass = null; int count = 0; while (operandsItor.hasNext()) { disjClass = operandsItor.next(); if (count == 0) some += disjClass; else some += (" or " + disjClass); count ++; } } else if (fromCls.hasProperty( RDF.type, RDFS.Datatype )) { // the type is rdfs:datatype Property owlWithRestrictions = ResourceFactory.createProperty(OWL.getURI(), "withRestrictions"); Property minInclusive = ResourceFactory.createProperty(XSD.getURI(), "minInclusive"); Property maxInclusive = ResourceFactory.createProperty(XSD.getURI(), "maxInclusive"); Resource wr = fromCls.getProperty(owlWithRestrictions).getResource(); RDFList wrl = wr.as(RDFList.class); int min = Integer.MIN_VALUE, max = Integer.MAX_VALUE; // Note: a simple assumption here, only elements w/ min or max in the list! for (Iterator<RDFNode> k = wrl.iterator(); k.hasNext();) { Resource wrClause = (Resource) k.next(); if (wrClause.getProperty(minInclusive) != null) { Literal minValue = wrClause.getProperty(minInclusive).getLiteral(); if (minValue != null) min = minValue.getInt(); } if (wrClause.getProperty(maxInclusive) != null) { Literal maxValue = wrClause.getProperty(maxInclusive).getLiteral(); if (maxValue != null) max = maxValue.getInt(); } } some += "[" + min + ", " + max + "]"; } else { some += fromCls; } some += ")"; int index = restrictionMap.size() + 1; key = "R" + index; restrictionMap.put(key, some); } return key; }
/** * Call this method to convert a SadlEnumeratedClass to a Jena * EnumeratedClass. * * @param argIdx * -- the argument index of the SadlEnumeratedClass in the * orginal call to ModelManager * @param clsName * -- the class to which the instances should belong * @param enumerated * -- the encapsulated list of individuals to place in the * EnumeratedClass * @return -- the EnumeratedClass if successful else null if not */ private EnumeratedClass createEnumeratedClass(int argIdx, ConceptName clsName, SadlEnumeratedClass enumerated) { List<ConceptName> enumeratedInstances = enumerated.getInstances(); OntClass cls = null; if (clsName != null) { cls = getOrCreateOntClass(argIdx + 1, 0, clsName); } List<Individual> enumedInst = new ArrayList<Individual>(); for (int i = 0; i < enumeratedInstances.size(); i++) { ConceptName inst = enumeratedInstances.get(i); Resource io; try { io = getOntResourceInExistingModel(inst); Individual indiv = null; if (io != null) { ConceptType it = getOntResourceConceptType(io); if (!it.equals(ConceptType.INDIVIDUAL)) { addError(argIdx, "'" + inst.toString() + "' already exists and is of type " + it.toString()); } indiv = io.as(Individual.class); removePendingError(inst, ConceptType.INDIVIDUAL); } else { indiv = getJenaModel().createIndividual(getUri(inst), cls); inst.setNamespace(indiv.getNameSpace()); removePendingError(inst, ConceptType.INDIVIDUAL); } if (indiv != null) { if (cls != null) { indiv.addOntClass(cls); } enumedInst.add(indiv); } } catch (ConfigurationException e) { addError(argIdx, "Unexpected error creating instance '" + inst.toString() + "': " + e.getMessage()); } } if (!enumedInst.isEmpty()) { Individual[] enumedArray = enumedInst .toArray(new Individual[enumedInst.size()]); RDFList rdfl = getJenaModel().createList(enumedArray); EnumeratedClass enumCls = getJenaModel().createEnumeratedClass( null, rdfl); return enumCls; } else { addError(argIdx, "Unable to generate enumerated class because instance list is empty."); } return null; }
private OntResource createUnionOfClasses(OntResource cls, ExtendedIterator<? extends OntResource> ditr) throws JenaProcessorException { OntResource unionClass = null; RDFList classes = null; boolean allEqual = true; while (ditr.hasNext()) { OntResource existingCls = ditr.next(); if (!existingCls.canAs(OntResource.class)) { throw new JenaProcessorException( "Unable to '" + existingCls.toString() + "' to OntResource to put into union of classes"); } if (existingCls.equals(cls)) { continue; } else { allEqual = false; } if (existingCls.as(OntResource.class).canAs(UnionClass.class)) { if (classes != null) { classes.append(existingCls.as(UnionClass.class).getOperands()); } else { try { existingCls.as(UnionClass.class).addOperand(cls); unionClass = existingCls.as(UnionClass.class); } catch (Exception e) { // don't know why this is happening logger.error("Union class error that hasn't been resolved or understood."); return cls; } } } else { if (classes == null) { classes = getTheJenaModel().createList(); } classes = classes.with(existingCls.as(OntResource.class)); classes = classes.with(cls); } } if (allEqual) { return cls; } if (classes != null) { unionClass = getTheJenaModel().createUnionClass(null, classes); } return unionClass; }