1 19 20 package com.hp.hpl.jena.ontology.impl; 23 24 25 import com.hp.hpl.jena.rdf.listeners.StatementListener; 28 import com.hp.hpl.jena.rdf.model.*; 29 import com.hp.hpl.jena.rdf.model.impl.*; 30 import com.hp.hpl.jena.reasoner.*; 31 import com.hp.hpl.jena.util.iterator.*; 32 import com.hp.hpl.jena.vocabulary.*; 33 import com.hp.hpl.jena.ontology.*; 34 import com.hp.hpl.jena.ontology.event.*; 35 import com.hp.hpl.jena.graph.*; 36 import com.hp.hpl.jena.graph.compose.MultiUnion; 37 import com.hp.hpl.jena.graph.query.*; 38 import com.hp.hpl.jena.enhanced.*; 39 import com.hp.hpl.jena.shared.*; 40 41 import java.io.*; 42 import java.util.*; 43 44 import org.apache.commons.logging.Log; 45 import org.apache.commons.logging.LogFactory; 46 47 48 49 59 public class OntModelImpl 60 extends ModelCom 61 implements OntModel 62 { 63 66 70 static public String owlSyntaxCheckerClassName = "com.hp.hpl.jena.ontology.tidy.JenaChecker"; 71 72 73 76 static private Log s_log = LogFactory.getLog( OntModelImpl.class ); 77 78 80 static private Class owlSyntaxCheckerClass; 81 82 85 86 protected OntModelSpec m_spec; 87 88 89 protected Set m_imported = new HashSet(); 90 91 92 protected BindingQueryPlan m_individualsQueryNoInf0; 93 94 95 protected BindingQueryPlan m_individualsQueryNoInf1; 96 97 98 protected boolean m_strictMode = true; 99 100 101 protected MultiUnion m_union = new MultiUnion(); 102 103 104 protected ImportsListener m_importsListener = null; 105 106 107 protected OntEventManager m_ontEventMgr = null; 108 109 110 private Model m_deductionsModel = null; 111 112 113 116 117 129 public OntModelImpl( OntModelSpec spec, Model model ) { 130 this( spec, makeBaseModel( spec, model ), false ); 131 } 132 133 137 public OntModelImpl( OntModelSpec spec ) { 138 this( spec, spec.createBaseModel(), false ); 139 } 140 141 148 private OntModelImpl( OntModelSpec spec, Model model, boolean overloadingTrick ) { 149 super( generateGraph( spec, model.getGraph() ), BuiltinPersonalities.model ); 151 m_spec = spec; 152 153 m_union = (getGraph() instanceof MultiUnion) ? 155 ((MultiUnion) getGraph()) : 156 (MultiUnion) ((InfGraph) getGraph()).getRawGraph(); 157 158 m_individualsQueryNoInf0 = queryXTypeOfType( getProfile().CLASS() ); 160 m_individualsQueryNoInf1 = queryXTypeOfType( getProfile().RESTRICTION() ); 161 162 if (getDocumentManager().useDeclaredPrefixes()) { 164 withDefaultMappings( getDocumentManager().getDeclaredPrefixMapping() ); 165 } 166 167 getDocumentManager().loadImports( this ); 169 170 rebind(); 172 } 173 174 175 176 179 191 public OntDocumentManager getDocumentManager() { 192 return m_spec.getDocumentManager(); 193 } 194 195 196 215 public ExtendedIterator listOntologies() { 216 checkProfileEntry( getProfile().ONTOLOGY(), "ONTOLOGY" ); 217 return UniqueExtendedIterator.create( 218 findByTypeAs( getProfile().ONTOLOGY(), Ontology.class ) ); 219 } 220 221 222 242 public ExtendedIterator listOntProperties() { 243 return UniqueExtendedIterator.create( 244 findByTypeAs( RDF.Property, OntProperty.class ) ); 245 } 246 247 248 268 public ExtendedIterator listObjectProperties() { 269 checkProfileEntry( getProfile().OBJECT_PROPERTY(), "OBJECT_PROPERTY" ); 270 return UniqueExtendedIterator.create( 271 findByTypeAs( getProfile().OBJECT_PROPERTY(), ObjectProperty.class ) ); 272 } 273 274 275 295 public ExtendedIterator listDatatypeProperties() { 296 checkProfileEntry( getProfile().DATATYPE_PROPERTY(), "DATATYPE_PROPERTY" ); 297 return UniqueExtendedIterator.create( 298 findByTypeAs( getProfile().DATATYPE_PROPERTY(), DatatypeProperty.class ) ); 299 } 300 301 302 317 public ExtendedIterator listFunctionalProperties() { 318 checkProfileEntry( getProfile().FUNCTIONAL_PROPERTY(), "FUNCTIONAL_PROPERTY" ); 319 return UniqueExtendedIterator.create( 320 findByTypeAs( getProfile().FUNCTIONAL_PROPERTY(), FunctionalProperty.class ) ); 321 } 322 323 324 337 public ExtendedIterator listTransitiveProperties() { 338 checkProfileEntry( getProfile().TRANSITIVE_PROPERTY(), "TRANSITIVE_PROPERTY" ); 339 return UniqueExtendedIterator.create( 340 findByTypeAs( getProfile().TRANSITIVE_PROPERTY(), TransitiveProperty.class ) ); 341 } 342 343 344 357 public ExtendedIterator listSymmetricProperties() { 358 checkProfileEntry( getProfile().SYMMETRIC_PROPERTY(), "SYMMETRIC_PROPERTY" ); 359 return UniqueExtendedIterator.create( 360 findByTypeAs( getProfile().SYMMETRIC_PROPERTY(), SymmetricProperty.class ) ); 361 } 362 363 364 377 public ExtendedIterator listInverseFunctionalProperties() { 378 checkProfileEntry( getProfile().INVERSE_FUNCTIONAL_PROPERTY(), "INVERSE_FUNCTIONAL_PROPERTY" ); 379 return UniqueExtendedIterator.create( 380 findByTypeAs( getProfile().INVERSE_FUNCTIONAL_PROPERTY(), InverseFunctionalProperty.class ) ); 381 } 382 383 384 398 public ExtendedIterator listIndividuals() { 399 boolean supportsIndAsThing = false; 403 if (getGraph() instanceof InfGraph) { 404 supportsIndAsThing = ((InfGraph) getGraph()).getReasoner() 405 .getReasonerCapabilities() 406 .contains( null, ReasonerVocabulary.supportsP, ReasonerVocabulary.individualAsThingP ); 407 } 408 if (!supportsIndAsThing || (getProfile().THING() == null) || getProfile().CLASS().equals( RDFS.Class )) { 409 ExtendedIterator indivI = queryFor( m_individualsQueryNoInf0, null, Individual.class ); 411 412 if (m_individualsQueryNoInf1 != null) { 413 indivI = indivI.andThen( queryFor( m_individualsQueryNoInf1, null, Individual.class ) ); 415 } 416 417 if (getProfile().THING() != null) { 419 indivI = indivI.andThen( findByTypeAs( getProfile().THING(), Individual.class ) ); 420 } 421 422 return UniqueExtendedIterator.create( indivI ); 423 } 424 else { 425 return UniqueExtendedIterator.create( 427 findByTypeAs( getProfile().THING(), Individual.class ) ); 428 } 429 } 430 431 432 448 public ExtendedIterator listClasses() { 449 return UniqueExtendedIterator.create( 450 findByTypeAs( getProfile().getClassDescriptionTypes(), OntClass.class ) ); 451 } 452 453 454 462 public ExtendedIterator listHierarchyRootClasses() { 463 if (getReasoner() != null) { 465 Model conf = getReasoner().getReasonerCapabilities(); 466 if (conf.contains( null, ReasonerVocabulary.supportsP, ReasonerVocabulary.directSubClassOf ) && 467 getProfile().THING() != null) 468 { 469 return listStatements( null, ReasonerVocabulary.directSubClassOf, getProfile().THING() ) 471 .mapWith( new OntResourceImpl.SubjectAsMapper( OntClass.class )); 472 } 473 } 474 475 return listClasses() 477 .filterDrop( new Filter() { 478 public boolean accept( Object o ) { 479 return ((OntResource) o).isOntLanguageTerm(); 480 }} ) 481 .filterKeep( new Filter() { 482 public boolean accept( Object o ) { 483 return ((OntClass) o).isHierarchyRoot(); 484 }} ) 485 ; 486 } 487 488 489 504 public ExtendedIterator listEnumeratedClasses() { 505 checkProfileEntry( getProfile().ONE_OF(), "ONE_OF" ); 506 return UniqueExtendedIterator.create( 507 findByDefiningPropertyAs( getProfile().ONE_OF(), EnumeratedClass.class ) ); 508 } 509 510 511 526 public ExtendedIterator listUnionClasses() { 527 checkProfileEntry( getProfile().UNION_OF(), "UNION_OF" ); 528 return UniqueExtendedIterator.create( 529 findByDefiningPropertyAs( getProfile().UNION_OF(), UnionClass.class ) ); 530 } 531 532 533 548 public ExtendedIterator listComplementClasses() { 549 checkProfileEntry( getProfile().COMPLEMENT_OF(), "COMPLEMENT_OF" ); 550 return UniqueExtendedIterator.create( 551 findByDefiningPropertyAs( getProfile().COMPLEMENT_OF(), ComplementClass.class ) ); 552 } 553 554 555 570 public ExtendedIterator listIntersectionClasses() { 571 checkProfileEntry( getProfile().INTERSECTION_OF(), "INTERSECTION_OF" ); 572 return UniqueExtendedIterator.create( 573 findByDefiningPropertyAs( getProfile().INTERSECTION_OF(), IntersectionClass.class ) ); 574 } 575 576 577 591 public ExtendedIterator listNamedClasses() { 592 return listClasses().filterDrop( 593 new Filter() { 594 public boolean accept( Object x ) { 595 return ((Resource) x).isAnon(); 596 } 597 } 598 ); 599 } 600 601 602 617 public ExtendedIterator listRestrictions() { 618 checkProfileEntry( getProfile().RESTRICTION(), "RESTRICTION" ); 619 return UniqueExtendedIterator.create( 620 findByTypeAs( getProfile().RESTRICTION(), Restriction.class ) ); 621 } 622 623 624 637 public ExtendedIterator listAllDifferent() { 638 checkProfileEntry( getProfile().ALL_DIFFERENT(), "ALL_DIFFERENT" ); 639 return UniqueExtendedIterator.create( 640 findByTypeAs( getProfile().ALL_DIFFERENT(), AllDifferent.class ) ); 641 } 642 643 648 public ExtendedIterator listDataRanges() { 649 checkProfileEntry( getProfile().DATARANGE(), "DATARANGE" ); 650 return UniqueExtendedIterator.create( 651 findByTypeAs( getProfile().DATARANGE(), DataRange.class ) ); 652 } 653 654 655 670 public ExtendedIterator listAnnotationProperties() { 671 checkProfileEntry( getProfile().ANNOTATION_PROPERTY(), "ANNOTATION_PROPERTY" ); 672 Resource r = getProfile().ANNOTATION_PROPERTY(); 673 674 if (r == null) { 675 return NullIterator.instance; 676 } 677 else { 678 return UniqueExtendedIterator.create( 679 findByType( r ) 680 .andThen( WrappedIterator.create( getProfile().getAnnotationProperties() ) ) 681 .mapWith( new SubjectNodeAs( AnnotationProperty.class ) ) ); 682 } 683 } 684 685 686 697 public Ontology getOntology( String uri ) { 698 return (Ontology) findByURIAs( uri, Ontology.class ); 699 } 700 701 702 712 public Individual getIndividual( String uri ) { 713 return (Individual) findByURIAs( uri, Individual.class ); 714 } 715 716 717 727 public OntProperty getOntProperty( String uri ) { 728 return (OntProperty) findByURIAs( uri, OntProperty.class ); 729 } 730 731 732 742 public ObjectProperty getObjectProperty( String uri ) { 743 return (ObjectProperty) findByURIAs( uri, ObjectProperty.class ); 744 } 745 746 747 754 public TransitiveProperty getTransitiveProperty( String uri ) { 755 return (TransitiveProperty) findByURIAs( uri, TransitiveProperty.class ); 756 } 757 758 759 766 public SymmetricProperty getSymmetricProperty( String uri ) { 767 return (SymmetricProperty) findByURIAs( uri, SymmetricProperty.class ); 768 } 769 770 771 778 public InverseFunctionalProperty getInverseFunctionalProperty( String uri ) { 779 return (InverseFunctionalProperty) findByURIAs( uri, InverseFunctionalProperty.class ); 780 } 781 782 783 793 public DatatypeProperty getDatatypeProperty( String uri ) { 794 return (DatatypeProperty) findByURIAs( uri, DatatypeProperty.class ); 795 } 796 797 798 808 public AnnotationProperty getAnnotationProperty( String uri ) { 809 return (AnnotationProperty) findByURIAs( uri, AnnotationProperty.class ); 810 } 811 812 813 823 public OntClass getOntClass( String uri ) { 824 return (OntClass) findByURIAs( uri, OntClass.class ); 825 } 826 827 828 835 public ComplementClass getComplementClass( String uri ) { 836 return (ComplementClass) findByURIAs( uri, ComplementClass.class ); 837 } 838 839 840 847 public EnumeratedClass getEnumeratedClass( String uri ) { 848 return (EnumeratedClass) findByURIAs( uri, EnumeratedClass.class ); 849 } 850 851 852 859 public UnionClass getUnionClass( String uri ) { 860 return (UnionClass) findByURIAs( uri, UnionClass.class ); 861 } 862 863 864 871 public IntersectionClass getIntersectionClass( String uri ) { 872 return (IntersectionClass) findByURIAs( uri, IntersectionClass.class ); 873 } 874 875 876 886 public Restriction getRestriction( String uri ) { 887 return (Restriction) findByURIAs( uri, Restriction.class ); 888 } 889 890 891 900 public HasValueRestriction getHasValueRestriction( String uri ) { 901 return (HasValueRestriction) findByURIAs( uri, HasValueRestriction.class ); 902 } 903 904 905 914 public SomeValuesFromRestriction getSomeValuesFromRestriction( String uri ) { 915 return (SomeValuesFromRestriction) findByURIAs( uri, SomeValuesFromRestriction.class ); 916 } 917 918 919 928 public AllValuesFromRestriction getAllValuesFromRestriction( String uri ) { 929 return (AllValuesFromRestriction) findByURIAs( uri, AllValuesFromRestriction.class ); 930 } 931 932 933 942 public CardinalityRestriction getCardinalityRestriction( String uri ) { 943 return (CardinalityRestriction) findByURIAs( uri, CardinalityRestriction.class ); 944 } 945 946 947 956 public MinCardinalityRestriction getMinCardinalityRestriction( String uri ) { 957 return (MinCardinalityRestriction) findByURIAs( uri, MinCardinalityRestriction.class ); 958 } 959 960 961 970 public MaxCardinalityRestriction getMaxCardinalityRestriction( String uri ) { 971 return (MaxCardinalityRestriction) findByURIAs( uri, MaxCardinalityRestriction.class ); 972 } 973 974 975 985 public QualifiedRestriction getQualifiedRestriction( String uri ) { 986 return (QualifiedRestriction) findByURIAs( uri, QualifiedRestriction.class ); 987 } 988 989 990 1000 public CardinalityQRestriction getCardinalityQRestriction( String uri ) { 1001 return (CardinalityQRestriction) findByURIAs( uri, CardinalityQRestriction.class ); 1002 } 1003 1004 1005 1015 public MinCardinalityQRestriction getMinCardinalityQRestriction( String uri ) { 1016 return (MinCardinalityQRestriction) findByURIAs( uri, MinCardinalityQRestriction.class ); 1017 } 1018 1019 1020 1030 public MaxCardinalityQRestriction getMaxCardinalityQRestriction( String uri ) { 1031 return (MaxCardinalityQRestriction) findByURIAs( uri, MaxCardinalityQRestriction.class ); 1032 } 1033 1034 1035 1046 public Ontology createOntology( String uri ) { 1047 checkProfileEntry( getProfile().ONTOLOGY(), "ONTOLOGY" ); 1048 return (Ontology) createOntResource( Ontology.class, getProfile().ONTOLOGY(), uri ); 1049 } 1050 1051 1052 1061 public Individual createIndividual( Resource cls ) { 1062 return (Individual) createOntResource( Individual.class, cls, null ); 1063 } 1064 1065 1066 1077 public Individual createIndividual( String uri, Resource cls ) { 1078 return (Individual) createOntResource( Individual.class, cls, uri ); 1079 } 1080 1081 1082 1093 public OntProperty createOntProperty( String uri ) { 1094 Property p = createProperty( uri ); 1095 p.addProperty( RDF.type, getProfile().PROPERTY() ); 1096 return (OntProperty) p.as( OntProperty.class ); 1097 } 1098 1099 1100 1110 public ObjectProperty createObjectProperty( String uri ) { 1111 return createObjectProperty( uri, false ); 1112 } 1113 1114 1115 1129 public ObjectProperty createObjectProperty( String uri, boolean functional ) { 1130 checkProfileEntry( getProfile().OBJECT_PROPERTY(), "OBJECT_PROPERTY" ); 1131 ObjectProperty p = (ObjectProperty) createOntResource( ObjectProperty.class, getProfile().OBJECT_PROPERTY(), uri ); 1132 1133 if (functional) { 1134 checkProfileEntry( getProfile().FUNCTIONAL_PROPERTY(), "FUNCTIONAL_PROPERTY" ); 1135 p.addProperty( RDF.type, getProfile().FUNCTIONAL_PROPERTY() ); 1136 } 1137 1138 return p; 1139 } 1140 1141 1142 1148 public TransitiveProperty createTransitiveProperty( String uri ) { 1149 return createTransitiveProperty( uri, false ); 1150 } 1151 1152 1153 1162 public TransitiveProperty createTransitiveProperty( String uri, boolean functional ) { 1163 checkProfileEntry( getProfile().TRANSITIVE_PROPERTY(), "TRANSITIVE_PROPERTY" ); 1164 TransitiveProperty p = (TransitiveProperty) createOntResource( TransitiveProperty.class, getProfile().TRANSITIVE_PROPERTY(), uri ); 1165 1166 if (functional) { 1167 checkProfileEntry( getProfile().FUNCTIONAL_PROPERTY(), "FUNCTIONAL_PROPERTY" ); 1168 p.addProperty( RDF.type, getProfile().FUNCTIONAL_PROPERTY() ); 1169 } 1170 1171 return p; 1172 } 1173 1174 1175 1181 public SymmetricProperty createSymmetricProperty( String uri ) { 1182 return createSymmetricProperty( uri, false ); 1183 } 1184 1185 1186 1193 public SymmetricProperty createSymmetricProperty( String uri, boolean functional ) { 1194 checkProfileEntry( getProfile().SYMMETRIC_PROPERTY(), "SYMMETRIC_PROPERTY" ); 1195 SymmetricProperty p = (SymmetricProperty) createOntResource( SymmetricProperty.class, getProfile().SYMMETRIC_PROPERTY(), uri ); 1196 1197 if (functional) { 1198 checkProfileEntry( getProfile().FUNCTIONAL_PROPERTY(), "FUNCTIONAL_PROPERTY" ); 1199 p.addProperty( RDF.type, getProfile().FUNCTIONAL_PROPERTY() ); 1200 } 1201 1202 return p; 1203 } 1204 1205 1206 1212 public InverseFunctionalProperty createInverseFunctionalProperty( String uri ) { 1213 return createInverseFunctionalProperty( uri, false ); 1214 } 1215 1216 1217 1224 public InverseFunctionalProperty createInverseFunctionalProperty( String uri, boolean functional ) { 1225 checkProfileEntry( getProfile().INVERSE_FUNCTIONAL_PROPERTY(), "INVERSE_FUNCTIONAL_PROPERTY" ); 1226 InverseFunctionalProperty p = (InverseFunctionalProperty) createOntResource( InverseFunctionalProperty.class, getProfile().INVERSE_FUNCTIONAL_PROPERTY(), uri ); 1227 1228 if (functional) { 1229 checkProfileEntry( getProfile().FUNCTIONAL_PROPERTY(), "FUNCTIONAL_PROPERTY" ); 1230 p.addProperty( RDF.type, getProfile().FUNCTIONAL_PROPERTY() ); 1231 } 1232 1233 return p; 1234 } 1235 1236 1237 1247 public DatatypeProperty createDatatypeProperty( String uri ) { 1248 return createDatatypeProperty( uri, false ); 1249 } 1250 1251 1252 1266 public DatatypeProperty createDatatypeProperty( String uri, boolean functional ) { 1267 checkProfileEntry( getProfile().DATATYPE_PROPERTY(), "DATATYPE_PROPERTY" ); 1268 DatatypeProperty p = (DatatypeProperty) createOntResource( DatatypeProperty.class, getProfile().DATATYPE_PROPERTY(), uri ); 1269 1270 if (functional) { 1271 checkProfileEntry( getProfile().FUNCTIONAL_PROPERTY(), "FUNCTIONAL_PROPERTY" ); 1272 p.addProperty( RDF.type, getProfile().FUNCTIONAL_PROPERTY() ); 1273 } 1274 1275 return p; 1276 } 1277 1278 1279 1289 public AnnotationProperty createAnnotationProperty( String uri ) { 1290 checkProfileEntry( getProfile().ANNOTATION_PROPERTY(), "ANNOTATION_PROPERTY" ); 1291 return (AnnotationProperty) createOntResource( AnnotationProperty.class, getProfile().ANNOTATION_PROPERTY(), uri ); 1292 } 1293 1294 1295 1304 public OntClass createClass() { 1305 checkProfileEntry( getProfile().CLASS(), "CLASS" ); 1306 return (OntClass) createOntResource( OntClass.class, getProfile().CLASS(), null ); 1307 } 1308 1309 1310 1320 public OntClass createClass( String uri ) { 1321 checkProfileEntry( getProfile().CLASS(), "CLASS" ); 1322 return (OntClass) createOntResource( OntClass.class, getProfile().CLASS(), uri ); 1323 } 1324 1325 1326 1332 public ComplementClass createComplementClass( String uri, Resource cls ) { 1333 checkProfileEntry( getProfile().CLASS(), "CLASS" ); 1334 OntClass c = (OntClass) createOntResource( OntClass.class, getProfile().CLASS(), uri ); 1335 1336 checkProfileEntry( getProfile().COMPLEMENT_OF(), "COMPLEMENT_OF" ); 1337 c.addProperty( getProfile().COMPLEMENT_OF(), (cls == null) ? getProfile().NOTHING() : cls ); 1339 1340 return (ComplementClass) c.as( ComplementClass.class ); 1341 } 1342 1343 1344 1350 public EnumeratedClass createEnumeratedClass( String uri, RDFList members ) { 1351 checkProfileEntry( getProfile().CLASS(), "CLASS" ); 1352 OntClass c = (OntClass) createOntResource( OntClass.class, getProfile().CLASS(), uri ); 1353 1354 checkProfileEntry( getProfile().ONE_OF(), "ONE_OF" ); 1355 c.addProperty( getProfile().ONE_OF(), (members == null) ? createList() : members ); 1356 1357 return (EnumeratedClass) c.as( EnumeratedClass.class ); 1358 } 1359 1360 1361 1367 public UnionClass createUnionClass( String uri, RDFList members ) { 1368 checkProfileEntry( getProfile().CLASS(), "CLASS" ); 1369 OntClass c = (OntClass) createOntResource( OntClass.class, getProfile().CLASS(), uri ); 1370 1371 checkProfileEntry( getProfile().UNION_OF(), "UNION_OF" ); 1372 c.addProperty( getProfile().UNION_OF(), (members == null) ? createList() : members ); 1373 1374 return (UnionClass) c.as( UnionClass.class ); 1375 } 1376 1377 1378 1384 public IntersectionClass createIntersectionClass( String uri, RDFList members ) { 1385 checkProfileEntry( getProfile().CLASS(), "CLASS" ); 1386 OntClass c = (OntClass) createOntResource( OntClass.class, getProfile().CLASS(), uri ); 1387 1388 checkProfileEntry( getProfile().INTERSECTION_OF(), "INTERSECTION_OF" ); 1389 c.addProperty( getProfile().INTERSECTION_OF(), (members == null) ? createList() : members ); 1390 1391 return (IntersectionClass) c.as( IntersectionClass.class ); 1392 } 1393 1394 1395 1405 public Restriction createRestriction( Property p ) { 1406 checkProfileEntry( getProfile().RESTRICTION(), "RESTRICTION" ); 1407 Restriction r = (Restriction) createOntResource( Restriction.class, getProfile().RESTRICTION(), null ); 1408 if (p != null) { 1409 r.setOnProperty( p ); 1410 } 1411 1412 return r; 1413 } 1414 1415 1416 1427 public Restriction createRestriction( String uri, Property p ) { 1428 checkProfileEntry( getProfile().RESTRICTION(), "RESTRICTION" ); 1429 Restriction r = (Restriction) createOntResource( Restriction.class, getProfile().RESTRICTION(), uri ); 1430 if (p != null) { 1431 r.setOnProperty( p ); 1432 } 1433 1434 return r; 1435 } 1436 1437 1438 1448 public HasValueRestriction createHasValueRestriction( String uri, Property prop, RDFNode value ) { 1449 checkProfileEntry( getProfile().RESTRICTION(), "RESTRICTION" ); 1450 Restriction r = (Restriction) createOntResource( Restriction.class, getProfile().RESTRICTION(), uri ); 1451 1452 if (prop == null || value == null) { 1453 throw new IllegalArgumentException ( "Cannot create hasValueRestriction with a null property or value" ); 1454 } 1455 1456 checkProfileEntry( getProfile().HAS_VALUE(), "HAS_VALUE" ); 1457 r.addProperty( getProfile().ON_PROPERTY(), prop ); 1458 r.addProperty( getProfile().HAS_VALUE(), value ); 1459 1460 return (HasValueRestriction) r.as( HasValueRestriction.class ); 1461 } 1462 1463 1464 1474 public SomeValuesFromRestriction createSomeValuesFromRestriction( String uri, Property prop, Resource cls ) { 1475 checkProfileEntry( getProfile().RESTRICTION(), "RESTRICTION" ); 1476 Restriction r = (Restriction) createOntResource( Restriction.class, getProfile().RESTRICTION(), uri ); 1477 1478 if (prop == null || cls == null) { 1479 throw new IllegalArgumentException ( "Cannot create someValuesFromRestriction with a null property or class" ); 1480 } 1481 1482 checkProfileEntry( getProfile().SOME_VALUES_FROM(), "SOME_VALUES_FROM" ); 1483 r.addProperty( getProfile().ON_PROPERTY(), prop ); 1484 r.addProperty( getProfile().SOME_VALUES_FROM(), cls ); 1485 1486 return (SomeValuesFromRestriction) r.as( SomeValuesFromRestriction.class ); 1487 } 1488 1489 1490 1500 public AllValuesFromRestriction createAllValuesFromRestriction( String uri, Property prop, Resource cls ) { 1501 checkProfileEntry( getProfile().RESTRICTION(), "RESTRICTION" ); 1502 Restriction r = (Restriction) createOntResource( Restriction.class, getProfile().RESTRICTION(), uri ); 1503 1504 if (prop == null || cls == null) { 1505 throw new IllegalArgumentException ( "Cannot create allValuesFromRestriction with a null property or class" ); 1506 } 1507 1508 checkProfileEntry( getProfile().ALL_VALUES_FROM(), "ALL_VALUES_FROM" ); 1509 r.addProperty( getProfile().ON_PROPERTY(), prop ); 1510 r.addProperty( getProfile().ALL_VALUES_FROM(), cls ); 1511 1512 return (AllValuesFromRestriction) r.as( AllValuesFromRestriction.class ); 1513 } 1514 1515 1516 1526 public CardinalityRestriction createCardinalityRestriction( String uri, Property prop, int cardinality ) { 1527 checkProfileEntry( getProfile().RESTRICTION(), "RESTRICTION" ); 1528 Restriction r = (Restriction) createOntResource( Restriction.class, getProfile().RESTRICTION(), uri ); 1529 1530 if (prop == null) { 1531 throw new IllegalArgumentException ( "Cannot create cardinalityRestriction with a null property" ); 1532 } 1533 1534 checkProfileEntry( getProfile().CARDINALITY(), "CARDINALITY" ); 1535 r.addProperty( getProfile().ON_PROPERTY(), prop ); 1536 r.addProperty( getProfile().CARDINALITY(), createTypedLiteral( cardinality ) ); 1537 1538 return (CardinalityRestriction) r.as( CardinalityRestriction.class ); 1539 } 1540 1541 1542 1552 public MinCardinalityRestriction createMinCardinalityRestriction( String uri, Property prop, int cardinality ) { 1553 checkProfileEntry( getProfile().RESTRICTION(), "RESTRICTION" ); 1554 Restriction r = (Restriction) createOntResource( Restriction.class, getProfile().RESTRICTION(), uri ); 1555 1556 if (prop == null) { 1557 throw new IllegalArgumentException ( "Cannot create minCardinalityRestriction with a null property" ); 1558 } 1559 1560 checkProfileEntry( getProfile().MIN_CARDINALITY(), "MIN_CARDINALITY" ); 1561 r.addProperty( getProfile().ON_PROPERTY(), prop ); 1562 r.addProperty( getProfile().MIN_CARDINALITY(), createTypedLiteral( cardinality ) ); 1563 1564 return (MinCardinalityRestriction) r.as( MinCardinalityRestriction.class ); 1565 } 1566 1567 1568 1578 public MaxCardinalityRestriction createMaxCardinalityRestriction( String uri, Property prop, int cardinality ) { 1579 checkProfileEntry( getProfile().RESTRICTION(), "RESTRICTION" ); 1580 Restriction r = (Restriction) createOntResource( Restriction.class, getProfile().RESTRICTION(), uri ); 1581 1582 if (prop == null) { 1583 throw new IllegalArgumentException ( "Cannot create maxCardinalityRestriction with a null property" ); 1584 } 1585 1586 checkProfileEntry( getProfile().MAX_CARDINALITY(), "MAX_CARDINALITY" ); 1587 r.addProperty( getProfile().ON_PROPERTY(), prop ); 1588 r.addProperty( getProfile().MAX_CARDINALITY(), createTypedLiteral( cardinality ) ); 1589 1590 return (MaxCardinalityRestriction) r.as( MaxCardinalityRestriction.class ); 1591 } 1592 1593 1594 1606 public MaxCardinalityQRestriction createMaxCardinalityQRestriction( String uri, Property prop, int cardinality, OntClass cls ) { 1607 checkProfileEntry( getProfile().RESTRICTION(), "RESTRICTION" ); 1608 checkProfileEntry( getProfile().ON_PROPERTY(), "ON_PROPERTY" ); 1609 checkProfileEntry( getProfile().MAX_CARDINALITY_Q(), "MAX_CARDINALITY_Q" ); 1610 checkProfileEntry( getProfile().HAS_CLASS_Q(), "HAS_CLASS_Q" ); 1611 1612 if (prop == null) { 1613 throw new IllegalArgumentException ( "Cannot create MaxCardinalityQRestriction with a null property" ); 1614 } 1615 if (cls == null) { 1616 throw new IllegalArgumentException ( "Cannot create MaxCardinalityQRestriction with a null class" ); 1617 } 1618 1619 Restriction r = (Restriction) createOntResource( Restriction.class, getProfile().RESTRICTION(), uri ); 1620 1621 r.addProperty( getProfile().ON_PROPERTY(), prop ); 1622 r.addProperty( getProfile().MAX_CARDINALITY_Q(), createTypedLiteral( cardinality ) ); 1623 r.addProperty( getProfile().HAS_CLASS_Q(), cls ); 1624 1625 return (MaxCardinalityQRestriction) r.as( MaxCardinalityQRestriction.class ); 1626 } 1627 1628 1629 1641 public MinCardinalityQRestriction createMinCardinalityQRestriction( String uri, Property prop, int cardinality, OntClass cls ) { 1642 checkProfileEntry( getProfile().RESTRICTION(), "RESTRICTION" ); 1643 checkProfileEntry( getProfile().ON_PROPERTY(), "ON_PROPERTY" ); 1644 checkProfileEntry( getProfile().MIN_CARDINALITY_Q(), "MIN_CARDINALITY_Q" ); 1645 checkProfileEntry( getProfile().HAS_CLASS_Q(), "HAS_CLASS_Q" ); 1646 1647 if (prop == null) { 1648 throw new IllegalArgumentException ( "Cannot create MinCardinalityQRestriction with a null property" ); 1649 } 1650 if (cls == null) { 1651 throw new IllegalArgumentException ( "Cannot create MinCardinalityQRestriction with a null class" ); 1652 } 1653 1654 Restriction r = (Restriction) createOntResource( Restriction.class, getProfile().RESTRICTION(), uri ); 1655 1656 r.addProperty( getProfile().ON_PROPERTY(), prop ); 1657 r.addProperty( getProfile().MIN_CARDINALITY_Q(), createTypedLiteral( cardinality ) ); 1658 r.addProperty( getProfile().HAS_CLASS_Q(), cls ); 1659 1660 return (MinCardinalityQRestriction) r.as( MinCardinalityQRestriction.class ); 1661 } 1662 1663 1664 1676 public CardinalityQRestriction createCardinalityQRestriction( String uri, Property prop, int cardinality, OntClass cls ) { 1677 checkProfileEntry( getProfile().RESTRICTION(), "RESTRICTION" ); 1678 checkProfileEntry( getProfile().ON_PROPERTY(), "ON_PROPERTY" ); 1679 checkProfileEntry( getProfile().CARDINALITY_Q(), "CARDINALITY_Q" ); 1680 checkProfileEntry( getProfile().HAS_CLASS_Q(), "HAS_CLASS_Q" ); 1681 1682 if (prop == null) { 1683 throw new IllegalArgumentException ( "Cannot create CardinalityQRestriction with a null property" ); 1684 } 1685 if (cls == null) { 1686 throw new IllegalArgumentException ( "Cannot create CardinalityQRestriction with a null class" ); 1687 } 1688 1689 Restriction r = (Restriction) createOntResource( Restriction.class, getProfile().RESTRICTION(), uri ); 1690 1691 r.addProperty( getProfile().ON_PROPERTY(), prop ); 1692 r.addProperty( getProfile().CARDINALITY_Q(), createTypedLiteral( cardinality ) ); 1693 r.addProperty( getProfile().HAS_CLASS_Q(), cls ); 1694 1695 return (CardinalityQRestriction) r.as( CardinalityQRestriction.class ); 1696 } 1697 1698 1699 1707 public DataRange createDataRange( RDFList literals ) { 1708 checkProfileEntry( getProfile().DATARANGE(), "DATARANGE" ); 1709 DataRange d = (DataRange) createOntResource( DataRange.class, getProfile().DATARANGE(), null ); 1710 1711 checkProfileEntry( getProfile().ONE_OF(), "ONE_OF" ); 1712 d.addProperty( getProfile().ONE_OF(), (literals == null) ? createList() : literals ); 1713 1714 return d; 1715 } 1716 1717 1718 1728 public AllDifferent createAllDifferent() { 1729 return createAllDifferent( null ); 1730 } 1731 1732 1733 1743 public AllDifferent createAllDifferent( RDFList differentMembers ) { 1744 checkProfileEntry( getProfile().ALL_DIFFERENT(), "ALL_DIFFERENT" ); 1745 AllDifferent ad = (AllDifferent) createOntResource( AllDifferent.class, getProfile().ALL_DIFFERENT(), null ); 1746 1747 ad.setDistinctMembers( (differentMembers == null) ? createList() : differentMembers ); 1748 1749 return ad; 1750 } 1751 1752 1753 1776 public OntResource createOntResource( Class javaClass, Resource rdfType, String uri ) { 1777 return (OntResource) getResourceWithType( uri, rdfType ).as( javaClass ); 1778 } 1779 1780 1786 public OntResource createOntResource( String uri ) { 1787 return (OntResource) getResource( uri ).as( OntResource.class ); 1788 } 1789 1790 1791 1796 public RDFList createList() { 1797 Resource list = getResource( getProfile().NIL().getURI() ); 1798 1799 return (RDFList) list.as( RDFList.class ); 1800 } 1801 1802 1803 1811 public Profile getProfile() { 1812 return m_spec.getProfile(); 1813 } 1814 1815 1816 1827 public boolean hasLoadedImport( String uri ) { 1828 return m_imported.contains( uri ); 1829 } 1830 1831 1832 1840 public void addLoadedImport( String uri ) { 1841 m_imported.add( uri ); 1842 } 1843 1844 1845 1853 public void removeLoadedImport( String uri ) { 1854 m_imported.remove( uri ); 1855 } 1856 1857 1858 1868 public Set listImportedOntologyURIs() { 1869 return listImportedOntologyURIs( false ); 1870 } 1871 1872 1873 1887 public Set listImportedOntologyURIs( boolean closure ) { 1888 Set results = new HashSet(); 1889 List queue = new ArrayList(); 1890 queue.add( getBaseModel() ); 1891 1892 while (!queue.isEmpty()) { 1893 Model m = (Model) queue.remove( 0 ); 1894 1895 if (getProfile().ONTOLOGY() != null && getProfile().IMPORTS() != null) { 1897 StmtIterator i = m.listStatements(null, getProfile().IMPORTS(), (RDFNode)null); 1898 while (i.hasNext()) { 1899 Statement s = i.nextStatement(); 1900 String uri = s.getResource().getURI(); 1901 1902 if (!results.contains( uri )) { 1903 results.add( uri ); 1905 1906 Model mi = getDocumentManager().getModel( uri ); 1908 if (closure && mi != null && !queue.contains( mi )) { 1909 queue.add( mi ); 1910 } 1911 } 1912 } 1913 } 1914 } 1915 1916 return results; 1917 } 1918 1919 1920 1928 public ModelMaker getImportModelMaker() { 1929 return m_spec.getImportModelMaker(); 1930 } 1931 1932 1935 public ModelMaker getModelMaker() { 1936 return getImportModelMaker(); 1937 } 1938 1939 1966 public Resource getOWLLanguageLevel( List problems ) { 1967 initSyntaxCheckerClass(); 1968 try { 1969 return 1970 ((OWLSyntaxChecker)owlSyntaxCheckerClass.newInstance()). 1971 getOWLLanguageLevel(this, problems); 1972 } 1973 catch (InstantiationException e){ 1974 throw new BrokenException("Syntax Checker misconfigured: ",e); 1975 } 1976 catch (IllegalAccessException e){ 1977 throw new BrokenException("Syntax Checker misconfigured: ",e); 1978 } 1979 } 1980 1981 1982 1987 public Model read( String uri ) { 1988 return read( uri, null ); 1989 } 1990 1991 1997 public Model read( Reader reader, String base ) { 1998 super.read( reader, base ); 1999 2000 getDocumentManager().loadImports( this ); 2001 rebind(); 2002 return this; 2003 } 2004 2005 2011 public Model read(InputStream reader, String base) { 2012 super.read( reader, base ); 2013 2014 getDocumentManager().loadImports( this ); 2015 rebind(); 2016 return this; 2017 } 2018 2019 2026 public Model read( String uri, String syntax ) { 2027 return read( uri, uri, syntax ); 2028 } 2029 2030 2038 public Model read( String uri, String base, String syntax ) { 2039 if (s_log.isDebugEnabled()) { 2041 s_log.debug( "Noting already loaded import URI " + uri ); 2042 } 2043 addLoadedImport( uri ); 2044 2045 String sourceURL = getDocumentManager().doAltURLMapping( uri ); 2046 super.read( sourceURL, base, syntax ); 2047 2048 getDocumentManager().addModel( uri, this ); 2050 2051 getDocumentManager().loadImports( this ); 2053 rebind(); 2054 return this; 2055 } 2056 2057 2065 public Model read(Reader reader, String base, String syntax) { 2066 super.read( reader, base, syntax ); 2067 2068 getDocumentManager().loadImports( this ); 2069 rebind(); 2070 return this; 2071 } 2072 2073 2081 public Model read(InputStream reader, String base, String syntax) { 2082 super.read( reader, base, syntax ); 2083 2084 getDocumentManager().loadImports( this ); 2085 rebind(); 2086 return this; 2087 } 2088 2089 2090 2098 public List getSubGraphs() { 2099 return getUnionGraph().getSubGraphs(); 2100 } 2101 2102 2103 2111 public ExtendedIterator listImportedModels() { 2112 ExtendedIterator i = WrappedIterator.create( getSubGraphs().iterator() ); 2113 2114 return i.mapWith( new Map1() { 2115 public Object map1(Object o) { 2116 Graph g = (Graph) o; 2117 Model temp = ModelFactory.createModelForGraph( g ); 2118 return ModelFactory.createOntologyModel( m_spec, temp ); 2119 }} ); 2120 } 2121 2122 2123 2132 public OntModel getImportedModel( String uri ) { 2133 if (listImportedOntologyURIs( true ).contains( uri )) { 2134 Model mi = getDocumentManager().getModel( uri ); 2135 2136 if (mi != null) { 2137 if (mi instanceof OntModel) { 2138 return (OntModel) mi; 2140 } 2141 else { 2142 return ModelFactory.createOntologyModel( m_spec, mi ); 2144 } 2145 } 2146 } 2147 2148 return null; 2149 } 2150 2151 2152 2160 public Graph getBaseGraph() { 2161 return getUnionGraph().getBaseGraph(); 2162 } 2163 2164 2165 2176 public Model getBaseModel() { 2177 Model result = ModelFactory.createModelForGraph( getBaseGraph() ); 2178 result.setNsPrefixes( this ); 2179 return result; 2180 } 2181 2182 2183 2192 public void addSubModel( Model model) { 2193 addSubModel( model, true ); 2194 } 2195 2196 2197 2206 public void addSubModel( Model model, boolean rebind ) { 2207 getUnionGraph().addGraph( model.getGraph() ); 2208 if (rebind) { 2209 rebind(); 2210 } 2211 } 2212 2213 2214 2224 public void removeSubModel( Model model ) { 2225 removeSubModel( model, true ); 2226 } 2227 2228 2229 2238 public void removeSubModel( Model model, boolean rebind ) { 2239 Graph subG = model.getGraph(); 2240 2241 if (subG instanceof MultiUnion) { 2242 subG = ((MultiUnion) subG).getBaseGraph(); 2244 } 2245 2246 getUnionGraph().removeGraph( subG ); 2247 if (rebind) { 2248 rebind(); 2249 } 2250 } 2251 2252 2253 2261 public boolean isInBaseModel( RDFNode node ) { 2262 Node n = node.asNode(); 2263 Graph b = getBaseGraph(); 2264 return b.contains( n, Node.ANY, Node.ANY ) || 2265 b.contains( Node.ANY, n, Node.ANY ) || 2266 b.contains( Node.ANY, Node.ANY, n ); 2267 } 2268 2269 2270 2278 public boolean isInBaseModel( Statement stmt ) { 2279 Node s = stmt.getSubject().asNode(); 2280 Node p = stmt.getPredicate().asNode(); 2281 Node o = stmt.getObject().asNode(); 2282 Graph b = getBaseGraph(); 2283 return b.contains( s, p, o ); 2284 } 2285 2286 2287 2298 public boolean strictMode() { 2299 return m_strictMode; 2300 } 2301 2302 2303 2311 public void setStrictMode( boolean strict ) { 2312 m_strictMode = strict; 2313 } 2314 2315 2316 2324 public void setDynamicImports( boolean dynamic ) { 2325 if (dynamic) { 2326 if (m_importsListener == null) { 2327 m_importsListener = new ImportsListener(); 2329 register( m_importsListener ); 2330 } 2331 } 2332 else { 2333 if (m_importsListener != null) { 2334 unregister( m_importsListener ); 2336 m_importsListener = null; 2337 } 2338 } 2339 } 2340 2341 2342 2347 public boolean getDynamicImports() { 2348 return m_importsListener != null; 2349 } 2350 2351 2352 2356 public OntModelSpec getSpecification() { 2357 return m_spec; 2358 } 2359 2360 2361 2366 public OntEventManager getEventManager() { 2367 if (m_ontEventMgr == null) { 2368 m_ontEventMgr = new OntEventManager( this ); 2369 } 2370 2371 return m_ontEventMgr; 2372 } 2373 2374 2375 2389 public ExtendedIterator queryFor( BindingQueryPlan query, List altQueries, Class asKey ) { 2390 GetBinding firstBinding = new GetBinding( 0 ); 2391 2392 ExtendedIterator mainQuery = query.executeBindings().mapWith( firstBinding ); 2394 2395 if (altQueries != null) { 2397 for (Iterator i = altQueries.iterator(); i.hasNext(); ) { 2398 ExtendedIterator altQuery = ((BindingQueryPlan) i.next()).executeBindings().mapWith( firstBinding ); 2399 mainQuery = mainQuery.andThen( altQuery ); 2400 } 2401 } 2402 2403 return mainQuery.filterKeep( new SubjectNodeCanAs( asKey ) ) 2405 .mapWith( new SubjectNodeAs( asKey ) ); 2406 } 2407 2408 2410 public Model write( Writer writer ) { return getBaseModel().write( writer ); } 2411 public Model write( Writer writer, String lang ) { return getBaseModel().write( writer, lang ); } 2412 public Model write( Writer writer, String lang, String base ) { return getBaseModel().write( writer, lang, base ); } 2413 public Model write( OutputStream out ) { return getBaseModel().write( out ); } 2414 public Model write( OutputStream out, String lang ) { return getBaseModel().write( out, lang ); } 2415 public Model write( OutputStream out, String lang, String base) { return getBaseModel().write( out, lang, base ); } 2416 2417 public Model writeAll( Writer writer, String lang, String base ) { 2418 return super.write( writer, lang, base ); 2419 } 2420 2421 public Model writeAll( OutputStream out, String lang, String base ) { 2422 return super.write( out, lang, base ); 2423 } 2424 2425 2426 2428 2432 public Model getRawModel() { 2433 return getBaseModel(); 2434 } 2435 2436 2439 public Reasoner getReasoner() { 2440 return (getGraph() instanceof InfGraph) ? ((InfGraph) getGraph()).getReasoner() : null; 2441 } 2442 2443 2450 public void rebind() { 2451 if (getGraph() instanceof InfGraph) { 2452 ((InfGraph) getGraph()).rebind(); 2453 } 2454 } 2455 2456 2464 public void prepare() { 2465 if (getGraph() instanceof InfGraph) { 2466 ((InfGraph) getGraph()).prepare(); 2467 } 2468 } 2469 2470 2476 public void reset() { 2477 if (getGraph() instanceof InfGraph) { 2478 ((InfGraph) getGraph()).reset(); 2479 } 2480 } 2481 2482 2492 public Model getDeductionsModel() { 2493 if (m_deductionsModel == null) { 2494 InfGraph infGraph = getInfGraph(); 2495 if (infGraph != null) { 2496 Graph deductionsGraph = infGraph.getDeductionsGraph(); 2497 if (deductionsGraph != null) { 2498 m_deductionsModel = ModelFactory.createModelForGraph( deductionsGraph ); 2499 } 2500 } 2501 } 2502 return m_deductionsModel; 2503 } 2504 2505 2506 2512 public ValidityReport validate() { 2513 return (getGraph() instanceof InfGraph) ? ((InfGraph) getGraph()).validate() : null; 2514 } 2515 2516 2533 public StmtIterator listStatements( Resource subject, Property predicate, RDFNode object, Model posit ) { 2534 if (getGraph() instanceof InfGraph) { 2535 Iterator iter = ((InfGraph) getGraph()).find(subject.asNode(), predicate.asNode(), object.asNode(), posit.getGraph()); 2536 return IteratorFactory.asStmtIterator(iter,this); 2537 } 2538 else { 2539 return null; 2540 } 2541 } 2542 2543 2548 public void setDerivationLogging(boolean logOn) { 2549 if (getGraph() instanceof InfGraph) { 2550 ((InfGraph) getGraph()).setDerivationLogging( logOn ); 2551 } 2552 } 2553 2554 2561 public Iterator getDerivation(Statement statement) { 2562 return (getGraph() instanceof InfGraph) ? ((InfGraph) getGraph()).getDerivation( statement.asTriple() ) : null; 2563 } 2564 2565 2566 2567 2570 2571 private static void initSyntaxCheckerClass() { 2572 if (owlSyntaxCheckerClass == null ) { 2573 try { 2574 owlSyntaxCheckerClass = Class.forName(owlSyntaxCheckerClassName); 2575 OWLSyntaxChecker chk = (OWLSyntaxChecker)owlSyntaxCheckerClass.newInstance(); 2576 } 2577 catch (Exception e){ 2578 throw new ConfigException("owlsyntax.jar must be on the classpath.",e); 2579 } 2580 } 2581 } 2582 2583 2589 private static Graph generateGraph( OntModelSpec spec, Graph base ) { 2590 MultiUnion u = new MultiUnion(); 2592 u.addGraph( base ); 2593 u.setBaseGraph( base ); 2594 2595 Reasoner r = spec.getReasoner(); 2596 return r == null ? (Graph) u : r.bind( u ); 2598 } 2599 2600 2601 2605 protected MultiUnion getUnionGraph() { 2606 return m_union; 2607 } 2608 2609 2610 2611 protected Resource findByURIAs( String uri, Class asKey ) { 2612 if (uri == null) { 2613 throw new IllegalArgumentException ( "Cannot get() ontology value with a null URI" ); 2614 } 2615 2616 Node n = Node.createURI( uri ); 2617 2618 if (getGraph().contains( n, Node.ANY, Node.ANY )) { 2619 try { 2621 return (Resource) getNodeAs( n, asKey ); 2622 } 2623 catch (ConversionException ignore) {} 2624 } 2625 2626 return null; 2628 } 2629 2630 2640 protected ExtendedIterator findByType( Resource type ) { 2641 return getGraph().find( null, RDF.type.asNode(), type.asNode() ); 2642 } 2643 2644 2645 2657 protected ExtendedIterator findByType( Resource type, Iterator alternates ) { 2658 ExtendedIterator i = findByType( type ); 2659 2660 if (alternates != null) { 2662 while (alternates.hasNext()) { 2663 i = i.andThen( findByType( (Resource) alternates.next() ) ); 2664 } 2665 } 2666 2667 return UniqueExtendedIterator.create( i ); 2668 } 2669 2670 2671 2684 protected ExtendedIterator findByTypeAs( Resource type, Iterator types, Class asKey ) { 2685 return findByType( type, types ).mapWith( new SubjectNodeAs( asKey ) ); 2686 } 2687 2688 2689 2701 protected ExtendedIterator findByTypeAs( Iterator types, Class asKey ) { 2702 return findByTypeAs( (Resource) types.next(), types, asKey ); 2703 } 2704 2705 2706 2718 protected ExtendedIterator findByTypeAs( Resource type, Class asKey ) { 2719 return findByType( type ).mapWith( new SubjectNodeAs( asKey ) ); 2720 } 2721 2722 2723 2732 protected BindingQueryPlan queryXTypeOfType( Resource type ) { 2733 if (type != null) { 2734 Query q = new Query(); 2735 q.addMatch( Query.Y, RDF.type.asNode(), type.asNode() ); 2737 q.addMatch( Query.X, RDF.type.asNode(), Query.Y ); 2738 2739 return queryHandler().prepareBindings( q, new Node[] {Query.X} ); 2740 } 2741 else { 2742 return null; 2743 } 2744 } 2745 2746 2747 2755 protected ExtendedIterator findByDefiningProperty( Property p ) { 2756 return getGraph().find( null, p.getNode(), null ); 2757 } 2758 2759 2760 2770 protected ExtendedIterator findByDefiningPropertyAs( Property p, Class asKey ) { 2771 return findByDefiningProperty( p ).mapWith( new SubjectNodeAs( asKey ) ); 2772 } 2773 2774 2775 2785 protected Resource getResourceWithType( String uri, Resource rdfType ) { 2786 Resource r = getResource( uri ); 2787 if (rdfType != null) { 2788 r.addProperty( RDF.type, rdfType ); 2789 } 2790 return r; 2791 } 2792 2793 2794 2800 public OntResource getOntResource( String uri ) { 2801 Resource r = getResource( uri ); 2802 if (containsResource( r )) { 2803 return (OntResource) r.as( OntResource.class ); 2804 } 2805 return null; 2806 } 2807 2808 2815 public OntResource getOntResource( Resource res ) { 2816 return (OntResource) res.inModel( this ).as( OntResource.class ); 2817 } 2818 2819 2826 protected void checkProfileEntry( Object profileTerm, String desc ) { 2827 if (profileTerm == null) { 2828 throw new ProfileException( desc, getProfile() ); 2830 } 2831 } 2832 2833 2834 2840 protected void checkListMembersRdfType( RDFList list, Resource rdfType ) { 2841 if (strictMode() && ! ((Boolean ) list.reduce( new RdfTypeTestFn( rdfType), Boolean.TRUE )).booleanValue()) { 2842 throw new LanguageConsistencyException( "The members of the given list are expected to be of rdf:type " + rdfType.toString() ); 2844 } 2845 } 2846 2847 2851 private static Model makeBaseModel( OntModelSpec spec, Model model ) { 2852 return model == null ? spec.createBaseModel() : model; 2853 } 2854 2855 2856 2861 private InfGraph getInfGraph() { 2862 return (getGraph() instanceof InfGraph) ? ((InfGraph) getGraph()) : null; 2863 } 2864 2865 2866 2870 2871 protected class SubjectNodeAs implements Map1 2872 { 2873 protected Class m_asKey; 2874 2875 protected SubjectNodeAs( Class asKey ) { m_asKey = asKey; } 2876 2877 public Object map1( Object x ) { 2878 Node n = (x instanceof Triple) 2879 ? ((Triple) x).getSubject() 2880 : ((x instanceof EnhNode) ? ((EnhNode) x).asNode() : (Node) x); 2881 return getNodeAs( n, m_asKey ); 2882 } 2883 2884 } 2885 2886 2887 protected class SubjectNodeCanAs implements Filter 2888 { 2889 protected Class m_asKey; 2890 protected SubjectNodeCanAs( Class asKey ) { m_asKey = asKey; } 2891 2892 public boolean accept( Object x ) { 2893 Node n = (x instanceof Triple) 2894 ? ((Triple) x).getSubject() 2895 : ((x instanceof EnhNode) ? ((EnhNode) x).asNode() : (Node) x); 2896 try { 2897 getNodeAs( n, m_asKey ); 2898 } 2899 catch (Exception ignore) { 2900 return false; 2901 } 2902 2903 return true; 2904 } 2905 2906 } 2907 2908 2909 protected class GetBinding implements Map1 2910 { 2911 protected int m_index; 2912 protected GetBinding( int index ) { m_index = index; } 2913 public Object map1( Object x ) { return ((List) x).get( m_index ); } 2914 } 2915 2916 2917 protected class RdfTypeTestFn implements RDFList.ReduceFn 2918 { 2919 protected Resource m_type; 2920 protected RdfTypeTestFn( Resource type ) { m_type = type; } 2921 public Object reduce( RDFNode node, Object accumulator ) { 2922 Boolean acc = (Boolean ) accumulator; 2923 if (acc.booleanValue()) { 2924 Resource r = (Resource) node; 2926 return new Boolean ( r.hasProperty( RDF.type, m_type ) ); 2927 } 2928 else { 2929 return acc; 2930 } 2931 } 2932 } 2933 2934 2935 protected class ImportsListener 2936 extends StatementListener 2937 { 2938 public void addedStatement( Statement added ) { 2939 if (added.getPredicate().equals( getProfile().IMPORTS() )) { 2940 getDocumentManager().loadImport( OntModelImpl.this, added.getResource().getURI() ); 2941 } 2942 } 2943 2944 public void removedStatement( Statement removed ) { 2945 if (removed.getPredicate().equals( getProfile().IMPORTS() )) { 2946 getDocumentManager().unloadImport( OntModelImpl.this, removed.getResource().getURI() ); 2947 } 2948 } 2949 } 2950} 2951 2952 2953 2982 | Popular Tags |